From: Brandon Black Date: Wed, 25 Jan 2006 02:04:02 +0000 (+0000) Subject: more source cleanup and minor fix stuff for schema-loader X-Git-Tag: 0.03000~42 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=66742793d9a84c20778f2b6925d6a499a81469d3;p=dbsrgits%2FDBIx-Class-Schema-Loader.git more source cleanup and minor fix stuff for schema-loader --- diff --git a/lib/DBIx/Class/Schema/Loader/DB2.pm b/lib/DBIx/Class/Schema/Loader/DB2.pm index 10e7ca3..1f1a07e 100644 --- a/lib/DBIx/Class/Schema/Loader/DB2.pm +++ b/lib/DBIx/Class/Schema/Loader/DB2.pm @@ -70,7 +70,7 @@ SQL $sth->execute($db_schema, $tabname) or die; my @cols = map { lc } map { @$_ } @{$sth->fetchall_arrayref}; - $sth->finish; + undef $sth; $sth = $dbh->prepare(<<'SQL') or die; SELECT kcu.COLNAME @@ -83,8 +83,6 @@ SQL my @pri = map { lc } map { @$_ } @{$sth->fetchall_arrayref}; - $sth->finish; - return ( \@cols, \@pri ); } @@ -100,26 +98,25 @@ FROM SYSIBM.SYSRELS SR WHERE SR.TBNAME = ? SQL foreach my $table ( $class->tables ) { - if ($sth->execute(uc $table)) { - while(my $res = $sth->fetchrow_arrayref()) { - my ($colcount, $other, $other_column, $column) = - map { lc } @$res; - - my @self_cols = split(' ',$column); - my @other_cols = split(' ',$other_column); - if(@self_cols != $colcount || @other_cols != $colcount) { - die "Column count discrepancy while getting rel info"; - } - - my %cond; - for(my $i = 0; $i < @self_cols; $i++) { - $cond{$other_cols[$i]} = $self_cols[$i]; - } - - eval { $class->_loader_make_relations ($table, $other, \%cond); }; - warn qq/\# belongs_to_many failed "$@"\n\n/ - if $@ && $class->_loader_debug; + next if ! $sth->execute(uc $table); + while(my $res = $sth->fetchrow_arrayref()) { + my ($colcount, $other, $other_column, $column) = + map { lc } @$res; + + my @self_cols = split(' ',$column); + my @other_cols = split(' ',$other_column); + if(@self_cols != $colcount || @other_cols != $colcount) { + die "Column count discrepancy while getting rel info"; } + + my %cond; + for(my $i = 0; $i < @self_cols; $i++) { + $cond{$other_cols[$i]} = $self_cols[$i]; + } + + eval { $class->_loader_make_relations ($table, $other, \%cond); }; + warn qq/\# belongs_to_many failed "$@"\n\n/ + if $@ && $class->_loader_debug; } } diff --git a/lib/DBIx/Class/Schema/Loader/Generic.pm b/lib/DBIx/Class/Schema/Loader/Generic.pm index 04c593a..8ae53e0 100644 --- a/lib/DBIx/Class/Schema/Loader/Generic.pm +++ b/lib/DBIx/Class/Schema/Loader/Generic.pm @@ -10,8 +10,8 @@ use Lingua::EN::Inflect; require DBIx::Class::Core; -__PACKAGE__->mk_classdata('_loader_data'); -__PACKAGE__->mk_classdata('_loader_debug' => 0); +__PACKAGE__->mk_classaccessor('_loader_data'); +__PACKAGE__->mk_classaccessor('_loader_debug' => 0); =head1 NAME @@ -106,7 +106,6 @@ sub _load_from_connection { left_base => $left_base, constraint => $args{constraint} || '.*', exclude => $args{exclude}, - relationships => $args{relationships}, inflect => $args{inflect}, db_schema => $args{db_schema} || '', drop_db_schema => $args{drop_db_schema}, @@ -118,7 +117,7 @@ sub _load_from_connection { warn qq/\### START DBIx::Class::Schema::Loader dump ###\n/ if $class->_loader_debug; $class->_loader_load_classes; - $class->_loader_relationships if $class->_loader_data->{relationships}; + $class->_loader_relationships if $args{relationships}; warn qq/\### END DBIx::Class::Schema::Loader dump ###\n/ if $class->_loader_debug; $class->storage->dbh->disconnect; # XXX this should be ->storage->disconnect later? @@ -162,9 +161,17 @@ sub tables { # Overload in your driver class sub _loader_db_classes { croak "ABSTRACT METHOD" } +# not a class method. +sub _loader_stringify_hash { + my $href = shift; + + return '{ ' . + join(q{, }, map("$_ => $href->{$_}", keys %$href)) + . ' }'; +} + # Setup has_a and has_many relationships sub _loader_make_relations { - use Data::Dumper; my ( $class, $table, $other, $cond ) = @_; my $table_class = $class->_loader_find_table_class($table); @@ -190,11 +197,15 @@ sub _loader_make_relations { my $rev_cond = { reverse %$cond }; + my $cond_printable = _loader_stringify_hash($cond) + if $class->_loader_debug; + my $rev_cond_printable = _loader_stringify_hash($rev_cond) + if $class->_loader_debug; + warn qq/\# Belongs_to relationship\n/ if $class->_loader_debug; warn qq/$table_class->belongs_to( '$other_relname' => '$other_class',/ - . Dumper($cond) - . qq/);\n\n/ + . qq/$cond_printable);\n\n/ if $class->_loader_debug; $table_class->belongs_to( $other_relname => $other_class, $cond); @@ -202,7 +213,7 @@ sub _loader_make_relations { warn qq/\# Has_many relationship\n/ if $class->_loader_debug; warn qq/$other_class->has_many( '$table_relname' => '$table_class',/ - . Dumper($rev_cond) + . qq/$rev_cond_printable);\n\n/ . qq/);\n\n/ if $class->_loader_debug; diff --git a/lib/DBIx/Class/Schema/Loader/mysql.pm b/lib/DBIx/Class/Schema/Loader/mysql.pm index fb7bb26..834d895 100644 --- a/lib/DBIx/Class/Schema/Loader/mysql.pm +++ b/lib/DBIx/Class/Schema/Loader/mysql.pm @@ -64,7 +64,7 @@ sub _loader_relationships { if @cols != @f_cols; my $cond = {}; - for(my $i = 0 ; $i < @cols; $i++) { + for(my $i = 0; $i < @cols; $i++) { $cond->{$f_cols[$i]} = $cols[$i]; } diff --git a/t/dbixcsl_common_tests.pm b/t/dbixcsl_common_tests.pm index 7014df6..3bf8726 100644 --- a/t/dbixcsl_common_tests.pm +++ b/t/dbixcsl_common_tests.pm @@ -198,24 +198,24 @@ sub create { CREATE TABLE loader_test1 ( id $self->{auto_inc_pk}, dat VARCHAR(32) - ) $self->{innodb}; + ) $self->{innodb} }, - q{ INSERT INTO loader_test1 (dat) VALUES('foo'); }, - q{ INSERT INTO loader_test1 (dat) VALUES('bar'); }, - q{ INSERT INTO loader_test1 (dat) VALUES('baz'); }, + q{ INSERT INTO loader_test1 (dat) VALUES('foo') }, + q{ INSERT INTO loader_test1 (dat) VALUES('bar') }, + q{ INSERT INTO loader_test1 (dat) VALUES('baz') }, qq{ CREATE TABLE loader_test2 ( id $self->{auto_inc_pk}, dat VARCHAR(32) - ) $self->{innodb}; + ) $self->{innodb} }, - q{ INSERT INTO loader_test2 (dat) VALUES('aaa'); }, - q{ INSERT INTO loader_test2 (dat) VALUES('bbb'); }, - q{ INSERT INTO loader_test2 (dat) VALUES('ccc'); }, - q{ INSERT INTO loader_test2 (dat) VALUES('ddd'); }, + q{ INSERT INTO loader_test2 (dat) VALUES('aaa') }, + q{ INSERT INTO loader_test2 (dat) VALUES('bbb') }, + q{ INSERT INTO loader_test2 (dat) VALUES('ccc') }, + q{ INSERT INTO loader_test2 (dat) VALUES('ddd') }, ); my @statements_reltests = ( @@ -223,13 +223,13 @@ sub create { CREATE TABLE loader_test3 ( id INTEGER NOT NULL PRIMARY KEY, dat VARCHAR(32) - ) $self->{innodb}; + ) $self->{innodb} }, - q{ INSERT INTO loader_test3 (id,dat) VALUES(1,'aaa'); }, - q{ INSERT INTO loader_test3 (id,dat) VALUES(2,'bbb'); }, - q{ INSERT INTO loader_test3 (id,dat) VALUES(3,'ccc'); }, - q{ INSERT INTO loader_test3 (id,dat) VALUES(4,'ddd'); }, + q{ INSERT INTO loader_test3 (id,dat) VALUES(1,'aaa') }, + q{ INSERT INTO loader_test3 (id,dat) VALUES(2,'bbb') }, + q{ INSERT INTO loader_test3 (id,dat) VALUES(3,'ccc') }, + q{ INSERT INTO loader_test3 (id,dat) VALUES(4,'ddd') }, qq{ CREATE TABLE loader_test4 ( @@ -237,13 +237,13 @@ sub create { fkid INTEGER NOT NULL, dat VARCHAR(32), FOREIGN KEY (fkid) REFERENCES loader_test3 (id) - ) $self->{innodb}; + ) $self->{innodb} }, - q{ INSERT INTO loader_test4 (id,fkid,dat) VALUES(123,1,'aaa'); }, - q{ INSERT INTO loader_test4 (id,fkid,dat) VALUES(124,2,'bbb'); }, - q{ INSERT INTO loader_test4 (id,fkid,dat) VALUES(125,3,'ccc'); }, - q{ INSERT INTO loader_test4 (id,fkid,dat) VALUES(126,4,'ddd'); }, + q{ INSERT INTO loader_test4 (id,fkid,dat) VALUES(123,1,'aaa') }, + q{ INSERT INTO loader_test4 (id,fkid,dat) VALUES(124,2,'bbb') }, + q{ INSERT INTO loader_test4 (id,fkid,dat) VALUES(125,3,'ccc') }, + q{ INSERT INTO loader_test4 (id,fkid,dat) VALUES(126,4,'ddd') }, qq{ CREATE TABLE loader_test5 ( @@ -251,10 +251,10 @@ sub create { id2 INTEGER NOT NULL, -- , id2 INTEGER REFERENCES loader_test1, dat VARCHAR(8), PRIMARY KEY (id1,id2) - ) $self->{innodb}; + ) $self->{innodb} }, - q{ INSERT INTO loader_test5 (id1,id2,dat) VALUES (1,1,'aaa'); }, + q{ INSERT INTO loader_test5 (id1,id2,dat) VALUES (1,1,'aaa') }, qq{ CREATE TABLE loader_test6 ( @@ -264,21 +264,21 @@ sub create { dat VARCHAR(8), FOREIGN KEY (loader_test2) REFERENCES loader_test2 (id), FOREIGN KEY (id, id2 ) REFERENCES loader_test5 (id1,id2) - ) $self->{innodb}; + ) $self->{innodb} }, (q{ INSERT INTO loader_test6 (id, id2,loader_test2,dat) } . - q{ VALUES (1, 1,1,'aaa'); }), + q{ VALUES (1, 1,1,'aaa') }), qq{ CREATE TABLE loader_test7 ( id INTEGER NOT NULL PRIMARY KEY, id2 VARCHAR(8) NOT NULL UNIQUE, dat VARCHAR(8) - ) $self->{innodb}; + ) $self->{innodb} }, - q{ INSERT INTO loader_test7 (id,id2,dat) VALUES (1,'aaa','bbb'); }, + q{ INSERT INTO loader_test7 (id,id2,dat) VALUES (1,'aaa','bbb') }, qq{ CREATE TABLE loader_test8 ( @@ -286,16 +286,16 @@ sub create { loader_test7 VARCHAR(8) NOT NULL, dat VARCHAR(8), FOREIGN KEY (loader_test7) REFERENCES loader_test7 (id2) - ) $self->{innodb}; + ) $self->{innodb} }, (q{ INSERT INTO loader_test8 (id,loader_test7,dat) } . - q{ VALUES (1,'aaa','bbb'); }), + q{ VALUES (1,'aaa','bbb') }), qq{ CREATE TABLE loader_test9 ( loader_test9 VARCHAR(8) NOT NULL - ) $self->{innodb}; + ) $self->{innodb} }, ); @@ -305,7 +305,7 @@ sub create { id10 $self->{auto_inc_pk}, subject VARCHAR(8), loader_test11 INTEGER - ) $self->{innodb}; + ) $self->{innodb} }, qq{ @@ -314,12 +314,12 @@ sub create { message VARCHAR(8) DEFAULT 'foo', loader_test10 INTEGER, FOREIGN KEY (loader_test10) REFERENCES loader_test10 (id10) - ) $self->{innodb}; + ) $self->{innodb} }, (q{ ALTER TABLE loader_test10 ADD CONSTRAINT } . q{ loader_test11_fk FOREIGN KEY (loader_test11) } . - q{ REFERENCES loader_test11 (id11); }), + q{ REFERENCES loader_test11 (id11) }), ); $self->drop_tables;