From: Rafael Kitover Date: Thu, 7 Jan 2010 08:28:44 +0000 (+0000) Subject: clean up the query from table stuff X-Git-Tag: 0.04999_14~10 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=075aff9752f9c6ec020cc040b6f70e131be0f4ad;hp=59f547384b29161d10f97f931f9149e26abd13ca;p=dbsrgits%2FDBIx-Class-Schema-Loader.git clean up the query from table stuff --- diff --git a/lib/DBIx/Class/Schema/Loader/DBI.pm b/lib/DBIx/Class/Schema/Loader/DBI.pm index 699f37f..8a06554 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI.pm @@ -99,7 +99,27 @@ sub _tables_list { } s/$qt//g for @tables; - return @tables; + return $self->_filter_tables(@tables); +} + +# ignore bad tables and views +sub _filter_tables { + my ($self, @tables) = @_; + + my @filtered_tables; + + for my $table (@tables) { + my $sth = $self->_sth_for($table, undef, \'1 = 0'); + eval { $sth->execute }; + if (not $@) { + push @filtered_tables, $table; + } + else { + warn "Bad table or view '$table', ignoring: $@\n"; + } + } + + return @filtered_tables; } =head2 load @@ -116,12 +136,9 @@ sub load { $self->next::method(@_); } -# Returns an arrayref of column names -sub _table_columns { +sub _table_as_sql { my ($self, $table) = @_; - my $dbh = $self->schema->storage->dbh; - if($self->{db_schema}) { $table = $self->{db_schema} . $self->{_namesep} . $self->_quote_table_name($table); @@ -129,7 +146,25 @@ sub _table_columns { $table = $self->_quote_table_name($table); } - my $sth = $dbh->prepare($self->schema->storage->sql_maker->select(\$table, undef, \'1 = 0')); + return $table; +} + +sub _sth_for { + my ($self, $table, $fields, $where) = @_; + + my $dbh = $self->schema->storage->dbh; + + my $sth = $dbh->prepare($self->schema->storage->sql_maker + ->select(\$self->_table_as_sql($table), $fields, $where)); + + return $sth; +} + +# Returns an arrayref of column names +sub _table_columns { + my ($self, $table) = @_; + + my $sth = $self->_sth_for($table, undef, \'1 = 0'); $sth->execute; my $retval = \@{$sth->{NAME_lc}}; $sth->finish; @@ -252,11 +287,8 @@ sub _columns_info_for { return \%result if !$@ && scalar keys %result; } - if($self->db_schema) { - $table = $self->db_schema . $self->{_namesep} . $table; - } my %result; - my $sth = $dbh->prepare($self->schema->storage->sql_maker->select($table, undef, \'1 = 0')); + my $sth = $self->_sth_for($table, undef, \'1 = 0'); $sth->execute; my @columns = @{$sth->{NAME_lc}}; for my $i ( 0 .. $#columns ){ diff --git a/lib/DBIx/Class/Schema/Loader/DBI/MSSQL.pm b/lib/DBIx/Class/Schema/Loader/DBI/MSSQL.pm index 5007cbe..ca55379 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/MSSQL.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/MSSQL.pm @@ -38,36 +38,6 @@ sub _setup { $self->_set_quote_char_and_name_sep; } -# drop bad tables when constructing list -sub _tables_list { - my $self = shift; - - my @tables = $self->next::method(@_); - my @filtered_tables; - - for my $table (@tables) { - my $full_quoted_table; - if($self->{db_schema}) { - $full_quoted_table = $self->{db_schema} . $self->{_namesep} . - $self->_quote_table_name($table); - } else { - $full_quoted_table = $self->_quote_table_name($table); - } - my $dbh = $self->schema->storage->dbh; - my $sth = $dbh->prepare($self->schema->storage->sql_maker - ->select(\$full_quoted_table, undef, \'1 = 0')); - eval { $sth->execute }; - if (not $@) { - push @filtered_tables, $table; - } - else { - warn "Bad table or view '$table', ignoring.\n"; - } - } - - return @filtered_tables; -} - # remove 'IDENTITY' from column data_type sub _columns_info_for { my $self = shift; diff --git a/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm b/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm index 19be9aa..7ed0c60 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm @@ -46,15 +46,10 @@ sub _setup { } } - -sub _table_columns { +sub _table_as_sql { my ($self, $table) = @_; - my $dbh = $self->schema->storage->dbh; - - my $sth = $dbh->prepare($self->schema->storage->sql_maker->select($table, undef, \'1 = 0')); - $sth->execute; - return \@{$sth->{NAME_lc}}; + return $self->_quote_table_name($table); } sub _tables_list { @@ -75,7 +70,7 @@ sub _tables_list { push @tables, $1 if $table =~ /\A(\w+)\z/; } - return @tables; + return $self->_filter_tables(@tables); } sub _table_uniq_info { diff --git a/lib/DBIx/Class/Schema/Loader/DBI/Sybase/Microsoft_SQL_Server.pm b/lib/DBIx/Class/Schema/Loader/DBI/Sybase/Microsoft_SQL_Server.pm index 0bc39f0..2602d26 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/Sybase/Microsoft_SQL_Server.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/Sybase/Microsoft_SQL_Server.pm @@ -30,7 +30,7 @@ sub _tables_list { my $dbh = $self->schema->storage->dbh; my @tables = $dbh->tables(undef, $self->db_schema, $table, $type); - return @tables; + return $self->_filter_tables(@tables); } =head1 SEE ALSO diff --git a/lib/DBIx/Class/Schema/Loader/DBI/mysql.pm b/lib/DBIx/Class/Schema/Loader/DBI/mysql.pm index 1a20bcb..07bf133 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/mysql.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/mysql.pm @@ -77,7 +77,7 @@ sub _mysql_table_get_keys { if(!exists($self->{_cache}->{_mysql_keys}->{$table})) { my %keydata; my $dbh = $self->schema->storage->dbh; - my $sth = $dbh->prepare("SHOW INDEX FROM `$table`"); + my $sth = $dbh->prepare('SHOW INDEX FROM '.$self->_table_as_sql($table)); $sth->execute; while(my $row = $sth->fetchrow_hashref) { next if $row->{Non_unique}; diff --git a/t/backcompat/0.04006/lib/dbixcsl_common_tests.pm b/t/backcompat/0.04006/lib/dbixcsl_common_tests.pm index 42c53fa..6947cb7 100644 --- a/t/backcompat/0.04006/lib/dbixcsl_common_tests.pm +++ b/t/backcompat/0.04006/lib/dbixcsl_common_tests.pm @@ -89,6 +89,7 @@ sub run_tests { my $warn_count = 0; $warn_count++ if grep /ResultSetManager/, @loader_warnings; $warn_count++ if grep /Dynamic schema detected/, @loader_warnings; + $warn_count++ for grep /^Bad table or view/, @loader_warnings; if($self->{skip_rels}) { is(scalar(@loader_warnings), $warn_count) diff --git a/t/lib/dbixcsl_common_tests.pm b/t/lib/dbixcsl_common_tests.pm index c7a78de..f4af33d 100644 --- a/t/lib/dbixcsl_common_tests.pm +++ b/t/lib/dbixcsl_common_tests.pm @@ -137,6 +137,8 @@ sub setup_schema { my $warn_count = 2; $warn_count++ if grep /ResultSetManager/, @loader_warnings; + $warn_count++ for grep /^Bad table or view/, @loader_warnings; + if($self->{skip_rels}) { SKIP: { is(scalar(@loader_warnings), $warn_count, "No loader warnings")