From: Brandon Black Date: Sat, 21 Jan 2006 23:59:16 +0000 (+0000) Subject: tests and docs updates X-Git-Tag: 0.03000~50 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=fbd834649351808228912ceb798f0832af58a30c;p=dbsrgits%2FDBIx-Class-Schema-Loader.git tests and docs updates --- diff --git a/lib/DBIx/Class/Schema/Loader.pm b/lib/DBIx/Class/Schema/Loader.pm index 5238c78..5dfde5c 100644 --- a/lib/DBIx/Class/Schema/Loader.pm +++ b/lib/DBIx/Class/Schema/Loader.pm @@ -42,12 +42,12 @@ DBIx::Class::Schema::Loader - Dynamic definition of a DBIx::Class::Schema my $schema1 = My::Schema->connect( $dsn, $user, $password, $attrs); # -or- - my $schema1 = My::Schema->connect(); + my $schema1 = "My::Schema"; # ^^ defaults to dsn/user/pass from load_from_connection() =head1 DESCRIPTION -DBIx::Class::Schema::Loader automate the definition of a +DBIx::Class::Schema::Loader automates the definition of a DBIx::Class::Schema by scanning table schemas and setting up columns and primary keys. @@ -56,14 +56,14 @@ L for more, and L for notes on writing your own db-specific subclass for an unsupported db. -L, L, and L are now -obsolete, use L and this module instead. ;) +This module obsoletes L for L version 0.5 +and later. =cut =head1 METHODS -=head2 new +=head2 load_from_connection Example in Synopsis above demonstrates the available arguments. For detailed information on the arguments, see the @@ -74,10 +74,9 @@ L documentation. sub load_from_connection { my ( $class, %args ) = @_; - foreach (qw/namespace dsn/) { - die qq/Argument $_ is required/ if ! $args{$_}; - } + die qq/dsn argument is required/ if ! $args{dsn}; + $args{namespace} ||= $class; $args{namespace} =~ s/(.*)::$/$1/; my $dsn = $args{dsn}; @@ -86,7 +85,8 @@ sub load_from_connection { my $impl = "DBIx::Class::Schema::Loader::" . $driver; $impl->require or - die qq/Couldn't require loader class "$impl", "$UNIVERSAL::require::ERROR"/; + die qq/Couldn't require loader class "$impl",/ . + qq/"$UNIVERSAL::require::ERROR"/; push(@ISA, $impl); $class->_load_from_connection(%args); @@ -94,7 +94,9 @@ sub load_from_connection { =head1 AUTHOR -Sebastian Riedel, C +Brandon Black, C + +Sebastian Riedel, C (DBIx::Class::Loader, which this module is branched from) Based upon the work of IKEBE Tomohiro diff --git a/lib/DBIx/Class/Schema/Loader/Generic.pm b/lib/DBIx/Class/Schema/Loader/Generic.pm index d79776a..28d05bb 100644 --- a/lib/DBIx/Class/Schema/Loader/Generic.pm +++ b/lib/DBIx/Class/Schema/Loader/Generic.pm @@ -12,8 +12,6 @@ require DBIx::Class::Core; __PACKAGE__->mk_classdata('loader_data'); -# XXX convert all usage of $class/$self->debug to ->debug_loader - =head1 NAME DBIx::Class::Schema::Loader::Generic - Generic DBIx::Class::Schema::Loader Implementation. @@ -104,7 +102,7 @@ sub _load_from_connection { $class->loader_data({ _datasource => [ $args{dsn}, $args{user}, $args{password}, $args{options} ], - _namespace => $args{namespace} || $class, + _namespace => $args{namespace}, _additional => $additional, _additional_base => $additional_base, _left_base => $left_base, @@ -119,10 +117,10 @@ sub _load_from_connection { }); $class->connection(@{$class->loader_data->{_datasource}}); - warn qq/\### START DBIx::Class::Schema::Loader dump ###\n/ if $class->debug; + warn qq/\### START DBIx::Class::Schema::Loader dump ###\n/ if $class->debug_loader; $class->_load_classes; $class->_relationships if $class->loader_data->{_relationships}; - warn qq/\### END DBIx::Class::Schema::Loader dump ###\n/ if $class->debug; + warn qq/\### END DBIx::Class::Schema::Loader dump ###\n/ if $class->debug_loader; $class->storage->dbh->disconnect; # XXX this should be ->storage->disconnect later? 1; @@ -136,18 +134,25 @@ sub _find_table_class { # Returns the moniker for a given table name, # for use in $conn->resultset($moniker) + +=head3 moniker + +Returns the moniker for a given literal table name. Used +as $schema->resultset($moniker), etc. + +=cut sub moniker { my ( $class, $table ) = @_; return $class->loader_data->{MONIKERS}->{$table}; } -=head3 debug +=head3 debug_loader -Overload to enable debug messages. +Overload to enable Loader debug messages. =cut -sub debug { 0 } +sub debug_loader { 0 } =head3 tables @@ -171,13 +176,13 @@ sub _belongs_to_many { my $table_class = $class->_find_table_class($table); my $other_class = $class->_find_table_class($other); - warn qq/\# Belongs_to relationship\n/ if $class->debug; + warn qq/\# Belongs_to relationship\n/ if $class->debug_loader; if($other_column) { warn qq/$table_class->belongs_to( '$column' => '$other_class',/ . qq/ { "foreign.$other_column" => "self.$column" },/ . qq/ { accessor => 'filter' });\n\n/ - if $class->debug; + if $class->debug_loader; $table_class->belongs_to( $column => $other_class, { "foreign.$other_column" => "self.$column" }, { accessor => 'filter' } @@ -185,7 +190,7 @@ sub _belongs_to_many { } else { warn qq/$table_class->belongs_to( '$column' => '$other_class' );\n\n/ - if $class->debug; + if $class->debug_loader; $table_class->belongs_to( $column => $other_class ); } @@ -195,12 +200,12 @@ sub _belongs_to_many { if $class->loader_data->{_inflect} and exists $class->loader_data->{_inflect}->{ lc $table_class_base }; - warn qq/\# Has_many relationship\n/ if $class->debug; + warn qq/\# Has_many relationship\n/ if $class->debug_loader; if($other_column) { warn qq/$other_class->has_many( '$plural' => '$table_class',/ . qq/ { "foreign.$column" => "self.$other_column" } );\n\n/ - if $class->debug; + if $class->debug_loader; $other_class->has_many( $plural => $table_class, { "foreign.$column" => "self.$other_column" } ); @@ -208,7 +213,7 @@ sub _belongs_to_many { else { warn qq/$other_class->has_many( '$plural' => '$table_class',/ . qq/'$other_column' );\n\n/ - if $class->debug; + if $class->debug_loader; $other_class->has_many( $plural => $table_class, $column ); } } @@ -250,7 +255,7 @@ sub _load_classes { $class->inject_base( $table_class, 'DBIx::Class::Core' ); $_->require for @db_classes; $class->inject_base( $table_class, $_ ) for @db_classes; - warn qq/\# Initializing table "$table_name_db_schema" as "$table_class"\n/ if $class->debug; + warn qq/\# Initializing table "$table_name_db_schema" as "$table_class"\n/ if $class->debug_loader; $table_class->table(lc $table_name_db_schema); my ( $cols, $pks ) = $class->_table_info($table_name_db_schema); @@ -259,12 +264,12 @@ sub _load_classes { $table_class->set_primary_key(@$pks) if @$pks; my $code = "package $table_class;\n$additional_base$additional$left_base"; - warn qq/$code/ if $class->debug; - warn qq/$table_class->table('$table_name_db_schema');\n/ if $class->debug; + warn qq/$code/ if $class->debug_loader; + warn qq/$table_class->table('$table_name_db_schema');\n/ if $class->debug_loader; my $columns = join "', '", @$cols; - warn qq/$table_class->add_columns('$columns')\n/ if $class->debug; + warn qq/$table_class->add_columns('$columns')\n/ if $class->debug_loader; my $primaries = join "', '", @$pks; - warn qq/$table_class->set_primary_key('$primaries')\n/ if $class->debug && @$pks; + warn qq/$table_class->set_primary_key('$primaries')\n/ if $class->debug_loader && @$pks; eval $code; croak qq/Couldn't load additional classes "$@"/ if $@; unshift @{"$table_class\::ISA"}, $_ foreach ( @{ $class->loader_data->{_left_base} } ); @@ -292,7 +297,7 @@ sub _relationships { eval { $class->_belongs_to_many( $table, $column, $other, $other_column ) }; warn qq/\# belongs_to_many failed "$@"\n\n/ - if $@ && $class->debug; + if $@ && $class->debug_loader; } } } diff --git a/lib/DBIx/Class/Schema/Loader/SQLite.pm b/lib/DBIx/Class/Schema/Loader/SQLite.pm index 6f8fa4f..d8f8cca 100644 --- a/lib/DBIx/Class/Schema/Loader/SQLite.pm +++ b/lib/DBIx/Class/Schema/Loader/SQLite.pm @@ -79,10 +79,10 @@ SELECT sql FROM sqlite_master WHERE tbl_name = ? if ( $col =~ /^(\w+).*REFERENCES\s+(\w+)\s*(\w+)?/i ) { chomp $col; warn qq/\# Found foreign key definition "$col"\n\n/ - if $class->debug; + if $class->debug_loader; eval { $class->_belongs_to_many( $table, $1, $2, $3 ) }; warn qq/\# belongs_to_many failed "$@"\n\n/ - if $@ && $class->debug; + if $@ && $class->debug_loader; } } } diff --git a/lib/DBIx/Class/Schema/Loader/Writing.pm b/lib/DBIx/Class/Schema/Loader/Writing.pm index 8b25d4b..9d93b0f 100644 --- a/lib/DBIx/Class/Schema/Loader/Writing.pm +++ b/lib/DBIx/Class/Schema/Loader/Writing.pm @@ -16,7 +16,6 @@ DBIx::Class::Schema::Loader::Writing - Loader subclass writing guide use strict; use base 'DBIx::Class::Schema::Loader::Generic'; - use DBI; use Carp; sub _db_classes { @@ -25,21 +24,21 @@ DBIx::Class::Schema::Loader::Writing - Loader subclass writing guide } sub _tables { - my $self = shift; - my $dbh = $self->{_storage}->dbh; + my $class = shift; + my $dbh = $class->storage->dbh; return $dbh->tables; # Your DBD may need something different } sub _table_info { - my ( $self, $table ) = @_; + my ( $class, $table ) = @_; ... return ( \@cols, \@primary ); } sub _relationships { - my $self = shift; + my $class = shift; ... - $self->_belongs_to_many($table, $f_key, $f_table, $f_column); + $class->_belongs_to_many($table, $f_key, $f_table, $f_column); # For each relationship you want to set up ($f_column is # optional, default is $f_table's primary key) ... diff --git a/lib/DBIx/Class/Schema/Loader/mysql.pm b/lib/DBIx/Class/Schema/Loader/mysql.pm index 9c045ba..8c941f8 100644 --- a/lib/DBIx/Class/Schema/Loader/mysql.pm +++ b/lib/DBIx/Class/Schema/Loader/mysql.pm @@ -59,7 +59,7 @@ sub _relationships { my $remote_column = shift @cols; eval { $class->_belongs_to_many( $table, $column, $remote_table, $remote_column) }; - warn qq/\# belongs_to_many failed "$@"\n\n/ if $@ && $class->debug; + warn qq/\# belongs_to_many failed "$@"\n\n/ if $@ && $class->debug_loader; } $sth->finish; diff --git a/t/10sqlite_common.t b/t/10sqlite_common.t index 7208977..c942bd4 100644 --- a/t/10sqlite_common.t +++ b/t/10sqlite_common.t @@ -1,12 +1,12 @@ use strict; use lib qw( ./t ); -use dbixcl_common_tests; +use dbixcsl_common_tests; eval { require DBD::SQLite }; my $class = $@ ? 'SQLite2' : 'SQLite'; { - my $tester = dbixcl_common_tests->new( + my $tester = dbixcsl_common_tests->new( vendor => 'SQLite', auto_inc_pk => 'INTEGER NOT NULL PRIMARY KEY', dsn => "dbi:$class:dbname=./t/sqlite_test", diff --git a/t/11mysql_common.t b/t/11mysql_common.t index f20479a..5acba32 100644 --- a/t/11mysql_common.t +++ b/t/11mysql_common.t @@ -1,6 +1,6 @@ use strict; use lib qw( . ./t ); -use dbixcl_common_tests; +use dbixcsl_common_tests; my $database = $ENV{MYSQL_NAME} || ''; my $user = $ENV{MYSQL_USER} || ''; @@ -9,7 +9,7 @@ my $test_innodb = $ENV{MYSQL_TEST_INNODB} || 0; my $skip_rels_msg = 'You need to set the MYSQL_TEST_INNODB environment variable to test relationships'; -my $tester = dbixcl_common_tests->new( +my $tester = dbixcsl_common_tests->new( vendor => 'Mysql', auto_inc_pk => 'INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT', innodb => q{Engine='InnoDB'}, diff --git a/t/12pg_common.t b/t/12pg_common.t index 56a4194..198ad92 100644 --- a/t/12pg_common.t +++ b/t/12pg_common.t @@ -1,12 +1,12 @@ use strict; use lib qw( . ./t ); -use dbixcl_common_tests; +use dbixcsl_common_tests; my $database = $ENV{PG_NAME} || ''; my $user = $ENV{PG_USER} || ''; my $password = $ENV{PG_PASS} || ''; -my $tester = dbixcl_common_tests->new( +my $tester = dbixcsl_common_tests->new( vendor => 'Pg', auto_inc_pk => 'SERIAL NOT NULL PRIMARY KEY', dsn => "dbi:Pg:dbname=$database", diff --git a/t/13db2_common.t b/t/13db2_common.t index de5318f..1214300 100644 --- a/t/13db2_common.t +++ b/t/13db2_common.t @@ -1,12 +1,12 @@ use strict; use lib qw( . ./t ); -use dbixcl_common_tests; +use dbixcsl_common_tests; my $database = $ENV{DB2_NAME} || ''; my $user = $ENV{DB2_USER} || ''; my $password = $ENV{DB2_PASS} || ''; -my $tester = dbixcl_common_tests->new( +my $tester = dbixcsl_common_tests->new( vendor => 'DB2', auto_inc_pk => 'SERIAL NOT NULL PRIMARY KEY', dsn => "dbi:DB2:$database", diff --git a/t/dbixcl_common_tests.pm b/t/dbixcsl_common_tests.pm similarity index 99% rename from t/dbixcl_common_tests.pm rename to t/dbixcsl_common_tests.pm index 2135175..04d01ff 100644 --- a/t/dbixcl_common_tests.pm +++ b/t/dbixcsl_common_tests.pm @@ -1,4 +1,4 @@ -package dbixcl_common_tests; +package dbixcsl_common_tests; use strict; @@ -40,7 +40,7 @@ sub run_tests { $self->create(); - my $namespace = 'DBIXCL_Test_' . $self->{vendor}; + my $namespace = 'DBIXCSL_Test_' . $self->{vendor}; my $debug = ($self->{verbose} > 1) ? 1 : 0;