Revision history for Perl extension DBIx::Class::Schema::Loader
+ - suppress 'bad table or view' warnings for filtered tables/views
- croak if several tables reduce to an identical moniker (ribasushi)
- better type info for Sybase ASE
- better type info for Pg: sets sequence for serials, handles numerics
sub load {
my $self = shift;
- $self->_load_tables($self->_tables_list);
+ $self->_load_tables(
+ $self->_tables_list({ constraint => $self->constraint, exclude => $self->exclude })
+ );
}
=head2 rescan
$self->_relbuilder->{schema} = $schema;
my @created;
- my @current = $self->_tables_list;
- foreach my $table ($self->_tables_list) {
+ my @current = $self->_tables_list({ constraint => $self->constraint, exclude => $self->exclude });
+ foreach my $table (@current) {
if(!exists $self->{_tables}->{$table}) {
push(@created, $table);
}
sub _load_tables {
my ($self, @tables) = @_;
- # First, use _tables_list with constraint and exclude
- # to get a list of tables to operate on
-
- my $constraint = $self->constraint;
- my $exclude = $self->exclude;
-
- @tables = grep { /$constraint/ } @tables if $constraint;
- @tables = grep { ! /$exclude/ } @tables if $exclude;
-
# Save the new tables to the tables list
foreach (@tables) {
$self->{_tables}->{$_} = 1;
$self->_make_src_class($_) for @tables;
-
# sanity-check for moniker clashes
my $inverse_moniker_idx;
for (keys %{$self->monikers}) {
unless $table_class eq $old_class;
}
- my $table_normalized = lc $table;
+# this was a bad idea, should be ok now without it
+# my $table_normalized = lc $table;
+# $self->classes->{$table_normalized} = $table_class;
+# $self->monikers->{$table_normalized} = $table_moniker;
+
$self->classes->{$table} = $table_class;
- $self->classes->{$table_normalized} = $table_class;
$self->monikers->{$table} = $table_moniker;
- $self->monikers->{$table_normalized} = $table_moniker;
$self->_use ($table_class, @{$self->additional_classes});
$self->_inject($table_class, @{$self->left_base_classes});
=cut
1;
+# vim:et sts=4 sw=4 tw=0:
# Returns an array of table names
sub _tables_list {
- my $self = shift;
+ my ($self, $opts) = (shift, shift);
my ($table, $type) = @_ ? @_ : ('%', '%');
}
s/$qt//g for @tables;
- return $self->_filter_tables(@tables);
+ return $self->_filter_tables(\@tables, $opts);
}
-# ignore bad tables and views
+# apply constraint/exclude and ignore bad tables and views
sub _filter_tables {
- my ($self, @tables) = @_;
+ my ($self, $tables, $opts) = @_;
+ my @tables = @$tables;
my @filtered_tables;
+ $opts ||= {};
+ my $constraint = $opts->{constraint};
+ my $exclude = $opts->{exclude};
+
+ @tables = grep { /$constraint/ } @$tables if defined $constraint;
+ @tables = grep { ! /$exclude/ } @$tables if defined $exclude;
+
for my $table (@tables) {
eval {
my $sth = $self->_sth_for($table, undef, \'1 = 0');
# DBD::DB2 doesn't follow the DBI API for ->tables
sub _tables_list {
- my $self = shift;
+ my ($self, $opts) = @_;
my $dbh = $self->schema->storage->dbh;
my @tables = map { lc } $dbh->tables(
s/\Q$self->{_quoter}\E//g for @tables;
s/^.*\Q$self->{_namesep}\E// for @tables;
- return @tables;
+ return $self->_filter_tables(\@tables, $opts);
}
sub _table_pk_info {
=cut
sub _tables_list {
- my $self = shift;
+ my ($self, $opts) = @_;
my $dbh = $self->schema->storage->dbh;
my $sth = $dbh->prepare(<<'EOF');
EOF
$sth->execute($self->db_schema);
- my @tables = map lc $_, map @$_, @{ $sth->fetchall_arrayref };
+ my @tables = map @$_, @{ $sth->fetchall_arrayref };
- return $self->_filter_tables(@tables);
+ return $self->_filter_tables(\@tables, $opts);
}
sub _table_pk_info {
my $fk = $row->{FK_NAME};
push @{$local_cols->{$fk}}, lc $row->{FKCOLUMN_NAME};
push @{$remote_cols->{$fk}}, lc $row->{PKCOLUMN_NAME};
- $remote_table->{$fk} = lc $row->{PKTABLE_NAME};
+ $remote_table->{$fk} = $row->{PKTABLE_NAME};
}
foreach my $fk (keys %$remote_table) {
FROM information_schema.constraint_column_usage ccu
JOIN information_schema.table_constraints tc on (ccu.constraint_name = tc.constraint_name)
JOIN information_schema.key_column_usage kcu on (ccu.constraint_name = kcu.constraint_name and ccu.column_name = kcu.column_name)
-wHERE lower(ccu.table_name) = @{[ $dbh->quote($table) ]} AND constraint_type = 'UNIQUE' ORDER BY kcu.ordinal_position
+wHERE lower(ccu.table_name) = @{[ $dbh->quote(lc $table) ]} AND constraint_type = 'UNIQUE' ORDER BY kcu.ordinal_position
});
$sth->execute;
my $constraints;
my $sth = $dbh->prepare(qq{
SELECT column_name
FROM information_schema.columns
-WHERE columnproperty(object_id(@{[ $dbh->quote($table) ]}, 'U'), @{[ $dbh->quote($col) ]}, 'IsIdentity') = 1
-AND lower(table_name) = @{[ $dbh->quote($table) ]} AND lower(column_name) = @{[ $dbh->quote($col) ]}
+WHERE columnproperty(object_id(@{[ $dbh->quote(lc $table) ]}, 'U'), @{[ $dbh->quote(lc $col) ]}, 'IsIdentity') = 1
+AND lower(table_name) = @{[ $dbh->quote(lc $table) ]} AND lower(column_name) = @{[ $dbh->quote(lc $col) ]}
});
if (eval { $sth->execute; $sth->fetchrow_array }) {
$info->{is_auto_increment} = 1;
$sth = $dbh->prepare(qq{
SELECT column_default
FROM information_schema.columns
-wHERE lower(table_name) = @{[ $dbh->quote($table) ]} AND lower(column_name) = @{[ $dbh->quote($col) ]}
+wHERE lower(table_name) = @{[ $dbh->quote(lc $table) ]} AND lower(column_name) = @{[ $dbh->quote(lc $col) ]}
});
my ($default) = eval { $sth->execute; $sth->fetchrow_array };
=cut
1;
+# vim:et sts=4 sw=4 tw=0:
}
sub _tables_list {
- my $self = shift;
+ my ($self, $opts) = @_;
- return $self->next::method(undef, undef);
+ return $self->next::method($opts, undef, undef);
}
=head1 SEE ALSO
}
sub _tables_list {
- my $self = shift;
+ my ($self, $opts) = @_;
my $dbh = $self->schema->storage->dbh;
if $table =~ /\A(\w+)\z/;
}
- return $self->_filter_tables(@tables);
+ return $self->_filter_tables(\@tables, $opts);
}
sub _table_uniq_info {
}
sub _tables_list {
- my $self = shift;
+ my ($self, $opts) = @_;
my $dbh = $self->schema->storage->dbh;
my $sth = $dbh->prepare(<<'EOF');
my @tables = map @$_, @{ $sth->fetchall_arrayref };
- return $self->_filter_tables(@tables);
+ return $self->_filter_tables(\@tables, $opts);
}
# check for IDENTITY columns
}
sub _tables_list {
- my $self = shift;
+ my ($self, $opts) = @_;
my $dbh = $self->schema->storage->dbh;
my $sth = $dbh->prepare("SELECT * FROM sqlite_master");
push @tables, $row->{tbl_name};
}
$sth->finish;
- return $self->_filter_tables(@tables);
+ return $self->_filter_tables(\@tables, $opts);
}
=head1 SEE ALSO
=cut
sub _tables_list {
- my $self = shift;
+ my ($self, $opts) = @_;
- return $self->next::method(undef, undef);
+ return $self->next::method($opts, undef, undef);
}
sub _table_fk_info {
'mssql_loader_test5',
'mssql_loader_test6',
],
- count => 11,
+ count => 10,
run => sub {
my ($schema, $monikers, $classes) = @_;
ok ((my $rsrc = $schema->resultset($monikers->{mssql_loader_test5})->result_source),
'got result_source');
- is $rsrc->name, 'mssql_loader_test5',
- 'table name is lowercased';
+## not anymore
+# is $rsrc->name, 'mssql_loader_test5',
+# 'table name is lowercased';
is_deeply [ $rsrc->columns ], [qw/id foocol barcol/],
'column names are lowercased';
dat VARCHAR(8),
from_id INTEGER,
to_id INTEGER,
- PRIMARY KEY (id1,id2)
+ PRIMARY KEY (id1,id2),
FOREIGN KEY (from_id) REFERENCES loader_test4 (id),
FOREIGN KEY (to_id) REFERENCES loader_test4 (id)
) $self->{innodb}
);
$self->drop_tables;
+ $self->drop_tables; # twice for good measure
my $dbh = $self->dbconnect(1);