use delgation onto Parser and Producer instead of after BUILD
[dbsrgits/SQL-Translator-2.0-ish.git] / lib / SQL / Translator / Parser / DBI.pm
CommitLineData
b8657f04 1package SQL::Translator::Parser::DBI;
abb2c327 2use namespace::autoclean;
684763c1 3use Moose::Role;
4use MooseX::Types::Moose qw(Maybe Str);
b8657f04 5use DBI::Const::GetInfoType;
c4ec1b63 6use DBI::Const::GetInfo::ANSI;
7use DBI::Const::GetInfoReturn;
44547961 8use aliased 'SQL::Translator::Object::Column';
47b211fd 9use aliased 'SQL::Translator::Object::ForeignKey';
92403c5e 10use aliased 'SQL::Translator::Object::Index';
47b211fd 11use aliased 'SQL::Translator::Object::PrimaryKey';
44547961 12use aliased 'SQL::Translator::Object::Table';
13use aliased 'SQL::Translator::Object::View';
b8657f04 14
684763c1 15has 'quoter' => (
44547961 16 is => 'rw',
17 isa => Str,
44547961 18 lazy => 1,
19 default => sub { shift->dbh->get_info(29) || q{"} }
b8657f04 20);
21
684763c1 22has 'namesep' => (
44547961 23 is => 'rw',
24 isa => Str,
44547961 25 lazy => 1,
26 default => sub { shift->dbh->get_info(41) || '.' }
b8657f04 27);
28
684763c1 29has 'schema_name' => (
44547961 30 is => 'rw',
31 isa => Maybe[Str],
44547961 32 lazy => 1,
33 default => undef
684763c1 34);
35
36has 'catalog_name' => (
44547961 37 is => 'rw',
38 isa => Maybe[Str],
44547961 39 lazy => 1,
40 default => undef
b8657f04 41);
42
684763c1 43sub _subclass {
b8657f04 44 my $self = shift;
45
684763c1 46 my $dbtype = $self->dbh->get_info($GetInfoType{SQL_DBMS_NAME}) || $self->dbh->{Driver}{Name};
b8657f04 47
684763c1 48 my $class = __PACKAGE__ . '::'. $dbtype;
49 Class::MOP::load_class($class);
50 $class->meta->apply($self);
51}
52
641ce7d8 53sub _is_auto_increment { 0 }
54
55sub _column_default_value {
56 my $self = shift;
57 my $column_info = shift;
58
59 return $column_info->{COLUMN_DEF};
60}
61
684763c1 62sub _add_tables {
63 my $self = shift;
64 my $schema = shift;
65
e3280be8 66 my $sth = $self->dbh->table_info($self->catalog_name, $self->schema_name, '%', "TABLE,VIEW,'LOCAL TEMPORARY','GLOBAL TEMPORARY'");
684763c1 67 while (my $table_info = $sth->fetchrow_hashref) {
e3280be8 68 if ($table_info->{TABLE_TYPE} =~ /^(TABLE|LOCAL TEMPORARY|GLOBAL TEMPORARY)$/) {
69 my $temp = $table_info->{TABLE_TYPE} =~ /TEMPORARY$/ ? 1 : 0;
70 my $table = Table->new({ name => $table_info->{TABLE_NAME}, temporary => $temp });
684763c1 71 $schema->add_table($table);
72 $self->_add_columns($table);
73 $self->_add_primary_key($table);
92403c5e 74 $self->_add_indexes($table);
684763c1 75 }
76 elsif ($table_info->{TABLE_TYPE} eq 'VIEW') {
77 my $sql = $self->_get_view_sql($table_info->{TABLE_NAME});
44547961 78 my $view = View->new({ name => $table_info->{TABLE_NAME}, sql => $sql });
79 $schema->add_view($view);
80 $self->_add_columns($view);
684763c1 81 }
82 }
2179164f 83 $self->_add_foreign_keys($schema->get_table($_), $schema) for $schema->table_ids;
684763c1 84}
b8657f04 85
684763c1 86sub _add_columns {
87 my $self = shift;
88 my $table = shift;
b8657f04 89
684763c1 90 my $sth = $self->dbh->column_info($self->catalog_name, $self->schema_name, $table->name, '%');
641ce7d8 91 while (my $column_info = $sth->fetchrow_hashref) {
92 my $column = Column->new({ name => $column_info->{COLUMN_NAME},
93 data_type => $column_info->{DATA_TYPE},
94 size => $column_info->{COLUMN_SIZE},
95 default_value => $self->_column_default_value($column_info),
96 is_auto_increment => $self->_is_auto_increment($column_info),
97 is_nullable => $column_info->{NULLABLE},
98 });
684763c1 99 $table->add_column($column);
684763c1 100 }
101}
102
103sub _add_primary_key {
104 my $self = shift;
105 my $table = shift;
b8657f04 106
684763c1 107 my $pk_info = $self->dbh->primary_key_info($self->catalog_name, $self->schema_name, $table->name);
684763c1 108 my ($pk_name, @pk_cols);
109 while (my $pk_col = $pk_info->fetchrow_hashref) {
110 $pk_name = $pk_col->{PK_NAME};
111 push @pk_cols, $pk_col->{COLUMN_NAME};
aad333cd 112 }
47b211fd 113 my $pk = PrimaryKey->new({ name => $pk_name });
114 $pk->add_column($table->get_column($_)) for @pk_cols;
115 $table->add_index($pk);
116}
117
2179164f 118sub _add_foreign_keys {
47b211fd 119 my $self = shift;
120 my $table = shift;
121 my $schema = shift;
122
123 my $fk_info = $self->dbh->foreign_key_info($self->catalog_name, $self->schema_name, $table->name, $self->catalog_name, $self->schema_name, undef);
124 return unless $fk_info;
125
126 my $fk_data;
127 while (my $fk_col = $fk_info->fetchrow_hashref) {
128 my $fk_name = $fk_col->{FK_NAME};
129
130 push @{$fk_data->{$fk_name}{columns}}, $fk_col->{FK_COLUMN_NAME};
131 $fk_data->{$fk_name}{table} = $fk_col->{FK_TABLE_NAME};
132 $fk_data->{$fk_name}{uk} = $schema->get_table($fk_col->{UK_TABLE_NAME})->get_index($fk_col->{UK_NAME});
133 }
134
135 foreach my $fk_name (keys %$fk_data) {
136 my $fk = ForeignKey->new({ name => $fk_name, references => $fk_data->{$fk_name}{uk} });
137 $fk->add_column($schema->get_table($fk_data->{$fk_name}{table})->get_column($_)) for @{$fk_data->{$fk_name}{columns}};
138 $table->add_constraint($fk);
139 }
b8657f04 140}
141
92403c5e 142sub _add_indexes {
143 my $self = shift;
144 my $table = shift;
145
146 my $index_info = $self->dbh->statistics_info($self->catalog_name, $self->schema_name, $table->name, 1, 0);
147
148 my ($index_name, $index_type, @index_cols);
149 while (my $index_col = $index_info->fetchrow_hashref) {
150 $index_name = $index_col->{INDEX_NAME};
8e50a529 151 $index_type = $index_col->{NON_UNIQUE} ? 'NORMAL' : 'UNIQUE';
92403c5e 152 push @index_cols, $index_col->{COLUMN_NAME};
153 }
e283ffd7 154 return if $table->exists_index($index_name);
92403c5e 155 my $index = Index->new({ name => $index_name, type => $index_type });
156 $index->add_column($table->get_column($_)) for @index_cols;
157 $table->add_index($index);
158}
159
b8657f04 1601;