fix a spelling mistake and abstract some code into functions
[dbsrgits/SQL-Translator-2.0-ish.git] / lib / SQL / Translator / Parser / DBI.pm
1 package SQL::Translator::Parser::DBI;
2 use namespace::autoclean;
3 use Moose::Role;
4 use MooseX::Types::Moose qw(Maybe Str);
5 use DBI::Const::GetInfoType;
6 use DBI::Const::GetInfo::ANSI;
7 use DBI::Const::GetInfoReturn;
8 use aliased 'SQL::Translator::Object::Column';
9 use aliased 'SQL::Translator::Object::Index';
10 use aliased 'SQL::Translator::Object::Table';
11 use aliased 'SQL::Translator::Object::View';
12
13 has 'quoter' => (
14     is => 'rw',
15     isa => Str,
16     required => 1,
17     lazy => 1,
18     default => sub { shift->dbh->get_info(29) || q{"} }
19 );
20
21 has 'namesep' => (
22     is => 'rw',
23     isa => Str,
24     required => 1,
25     lazy => 1,
26     default => sub { shift->dbh->get_info(41) || '.' }
27 );
28
29 has 'schema_name' => (
30     is => 'rw',
31     isa => Maybe[Str],
32     required => 0,
33     lazy => 1,
34     default => undef
35 );
36
37 has 'catalog_name' => (
38     is => 'rw',
39     isa => Maybe[Str],
40     required => 0,
41     lazy => 1,
42     default => undef
43 );
44
45 sub _subclass {
46     my $self = shift;
47
48     my $dbtype = $self->dbh->get_info($GetInfoType{SQL_DBMS_NAME}) || $self->dbh->{Driver}{Name};
49
50     my $class = __PACKAGE__ . '::'. $dbtype;
51     Class::MOP::load_class($class);
52     $class->meta->apply($self);
53 }
54
55 sub _is_auto_increment { 0 }
56
57 sub _column_default_value {
58     my $self = shift;
59     my $column_info = shift;
60
61     return $column_info->{COLUMN_DEF};
62 }
63
64 sub _add_tables {
65     my $self = shift;
66     my $schema = shift;
67
68     my $sth = $self->dbh->table_info($self->catalog_name, $self->schema_name, '%', 'TABLE,VIEW');
69     while (my $table_info = $sth->fetchrow_hashref) {
70         if ($table_info->{TABLE_TYPE} eq 'TABLE') {
71             my $table = Table->new({ name => $table_info->{TABLE_NAME} });
72             $schema->add_table($table);
73             $self->_add_columns($table);
74             $self->_add_primary_key($table);
75         }
76         elsif ($table_info->{TABLE_TYPE} eq 'VIEW') {
77             my $sql = $self->_get_view_sql($table_info->{TABLE_NAME});
78             my $view = View->new({ name => $table_info->{TABLE_NAME}, sql => $sql });
79             $schema->add_view($view);
80             $self->_add_columns($view);
81         }
82     }
83 }
84
85 sub _add_columns {
86     my $self  = shift;
87     my $table = shift;
88
89     my $sth = $self->dbh->column_info($self->catalog_name, $self->schema_name, $table->name, '%');
90     while (my $column_info = $sth->fetchrow_hashref) {
91         my $column = Column->new({ name => $column_info->{COLUMN_NAME},
92                                    data_type => $column_info->{DATA_TYPE},
93                                    size => $column_info->{COLUMN_SIZE},
94                                    default_value => $self->_column_default_value($column_info),
95                                    is_auto_increment => $self->_is_auto_increment($column_info),
96                                    is_nullable => $column_info->{NULLABLE},
97                                  });
98         $table->add_column($column);
99     }
100 }
101
102 sub _add_primary_key {
103     my $self = shift;
104     my $table = shift;
105
106     my $pk_info = $self->dbh->primary_key_info($self->catalog_name, $self->schema_name, $table->name);
107     my ($pk_name, @pk_cols);
108     while (my $pk_col = $pk_info->fetchrow_hashref) {
109         $pk_name = $pk_col->{PK_NAME};
110         push @pk_cols, $pk_col->{COLUMN_NAME};
111     }
112     my $index = Index->new({ name => $pk_name, type => 'PRIMARY_KEY' });
113     $index->add_column($table->get_column($_)) for @pk_cols;
114     $table->add_index($index);
115 }
116
117 1;