From: Rafael Kitover Date: Sat, 22 May 2010 12:35:34 +0000 (-0400) Subject: add qualify_objects option X-Git-Tag: 0.07000~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=692193496082e4b69bcd80d02999371d20d874ba;p=dbsrgits%2FDBIx-Class-Schema-Loader.git add qualify_objects option --- diff --git a/Changes b/Changes index 7bb4147..001c0c4 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,6 @@ Revision history for Perl extension DBIx::Class::Schema::Loader + - added 'qualify_objects' option to prepend db_schema to table names - fix for negative numeric default values - sequence is detected for Oracle - fix for SQLite is_auto_increment detection when table is empty (hobbs) @@ -15,7 +16,7 @@ Revision history for Perl extension DBIx::Class::Schema::Loader Firebird/InterBase 'unquoted_ddl' options in favor of it. - support CamelCase table names and column names (in case-preserving mode) at the v7 naming level - - rewrite datetime default functions as \'CURRENT_TIMESTAMP' where + - rewrite datetime default functions as \'current_timestamp' where possible (except for Sybase ASE) to ease cross-deployment - use column_info instead of select to get Oracle column list (RT#42281) - match quotes in MySQL parser in more places (RT#42101) diff --git a/TODO b/TODO index 81dabd3..bd120ef 100644 --- a/TODO +++ b/TODO @@ -30,7 +30,7 @@ - add hashref form of components to control which components are added to which classes - check rel accessors for method conflicts - - add an option to add extra code to Result classes + - add an option to add extra code to Result classes (maybe...) - redo in-memory schema as an @INC coderef rather than temp files - add option to filter out views - support columns with names like "ro'd p/n" diff --git a/lib/DBIx/Class/Schema/Loader/Base.pm b/lib/DBIx/Class/Schema/Loader/Base.pm index e0587ca..fd06499 100644 --- a/lib/DBIx/Class/Schema/Loader/Base.pm +++ b/lib/DBIx/Class/Schema/Loader/Base.pm @@ -65,6 +65,7 @@ __PACKAGE__->mk_group_ro_accessors('simple', qw/ datetime_locale config_file loader_class + qualify_objects /); @@ -466,6 +467,11 @@ case-sensitive collation will turn this option on unconditionally. Currently the drivers for SQLite, mysql, MSSQL and Firebird/InterBase support setting this option. +=head1 qualify_objects + +Set to true to prepend the L to table names for C<< +__PACKAGE__->table >> calls, and to some other things like Oracle sequences. + =head1 METHODS None of these methods are intended for direct invocation by regular @@ -1502,7 +1508,7 @@ sub _setup_src_meta { $table_name = \ $self->_quote_table_name($table_name); } - $self->_dbic_stmt($table_class,'table',$table_name); + $self->_dbic_stmt($table_class, 'table', ($self->qualify_objects ? ($self->db_schema . '.') : '') . $table_name); my $cols = $self->_table_columns($table); my $col_info = $self->__columns_info_for($table); diff --git a/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm b/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm index 55e6a4e..55f3aac 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm @@ -156,10 +156,11 @@ AND upper(trigger_type) LIKE '%BEFORE EACH ROW%' AND lower(triggering_event) LIK $result->{$col_name}{is_auto_increment} = 1; - if (my ($seq_name) = $trigger_body =~ /"?(\w+)"?\.nextval/i) { - $seq_name = $self->_lc($seq_name); + if (my ($seq_schema, $seq_name) = $trigger_body =~ /(?:\."?(\w+)"?)?"?(\w+)"?\.nextval/i) { + $seq_schema = $self->_lc($seq_schema) || $self->db_schema; + $seq_name = $self->_lc($seq_name); - $result->{$col_name}{sequence} = $seq_name; + $result->{$col_name}{sequence} = ($self->qualify_objects ? ($seq_schema . '.') : '') . $seq_name; } } diff --git a/lib/DBIx/Class/Schema/Loader/DBI/SQLite.pm b/lib/DBIx/Class/Schema/Loader/DBI/SQLite.pm index d22ff6b..c29f4d3 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/SQLite.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/SQLite.pm @@ -49,6 +49,22 @@ sub rescan { $self->next::method($schema); } +# A hack so that qualify_objects can be tested on SQLite, SQLite does not +# actually have schemas. +{ + sub _table_as_sql { + my $self = shift; + local $self->{db_schema}; + return $self->next::method(@_); + } + + sub _table_pk_info { + my $self = shift; + local $self->{db_schema}; + return $self->next::method(@_); + } +} + sub _columns_info_for { my $self = shift; my ($table) = @_; diff --git a/t/23dumpmore.t b/t/23dumpmore.t index 2e2294e..c02c279 100644 --- a/t/23dumpmore.t +++ b/t/23dumpmore.t @@ -360,6 +360,22 @@ rmtree($DUMP_PATH, 1, 1); do_dump_test( classname => 'DBICTest::DumpMore::1', + options => { db_schema => 'foo_schema', qualify_objects => 1, use_namespaces => 1 }, + warnings => [ + qr/Dumping manual schema for DBICTest::DumpMore::1 to directory /, + qr/Schema dump completed/, + ], + regexes => { + 'Result/Foo' => [ + qr/^\Q__PACKAGE__->table("foo_schema.foo");\E/m, + ], + }, +); + +rmtree($DUMP_PATH, 1, 1); + +do_dump_test( + classname => 'DBICTest::DumpMore::1', options => { use_namespaces => 1 }, warnings => [ qr/Dumping manual schema for DBICTest::DumpMore::1 to directory /, @@ -470,3 +486,4 @@ do_dump_test( done_testing; END { rmtree($DUMP_PATH, 1, 1) unless $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP} } +# vim:et sts=4 sw=4 tw=0: