1 package SQL::Translator::Parser::DBI;
2 use namespace::autoclean;
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';
18 default => sub { shift->dbh->get_info(29) || q{"} }
26 default => sub { shift->dbh->get_info(41) || '.' }
29 has 'schema_name' => (
37 has 'catalog_name' => (
48 my $dbtype = $self->dbh->get_info($GetInfoType{SQL_DBMS_NAME}) || $self->dbh->{Driver}{Name};
50 my $class = __PACKAGE__ . '::'. $dbtype;
51 Class::MOP::load_class($class);
52 $class->meta->apply($self);
55 sub _is_auto_increment { 0 }
57 sub _column_default_value {
59 my $column_info = shift;
61 return $column_info->{COLUMN_DEF};
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);
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);
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},
98 $table->add_column($column);
102 sub _add_primary_key {
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};
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);