X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator%2FProducer%2FClassDBI.pm;h=986867595c3496b87fdfe299a048146b50845bb6;hb=4ab3763d2ad756c236b757306989cafa08e7f35e;hp=378d8622ef3ed0a3a91dcade501ac9554b7b7312;hpb=ede3a3eff20681c8b3dcdb34ec28a5381ef1e776;p=dbsrgits%2FSQL-Translator.git diff --git a/lib/SQL/Translator/Producer/ClassDBI.pm b/lib/SQL/Translator/Producer/ClassDBI.pm index 378d862..9868675 100755 --- a/lib/SQL/Translator/Producer/ClassDBI.pm +++ b/lib/SQL/Translator/Producer/ClassDBI.pm @@ -1,10 +1,7 @@ package SQL::Translator::Producer::ClassDBI; # ------------------------------------------------------------------- -# $Id: ClassDBI.pm,v 1.31 2003-08-13 17:13:53 kycl4rk Exp $ -# ------------------------------------------------------------------- -# Copyright (C) 2003 Allen Day , -# Ying Zhang +# Copyright (C) 2002-2009 SQLFairy Authors # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as @@ -23,17 +20,17 @@ package SQL::Translator::Producer::ClassDBI; use strict; use vars qw[ $VERSION $DEBUG ]; -$VERSION = sprintf "%d.%02d", q$Revision: 1.31 $ =~ /(\d+)\.(\d+)/; +$VERSION = '1.59'; $DEBUG = 1 unless defined $DEBUG; use SQL::Translator::Schema::Constants; -use SQL::Translator::Utils qw(header_comment); -use Text::Autoformat; +use SQL::Translator::Utils qw(debug header_comment); +use Data::Dumper; my %CDBI_auto_pkgs = ( - MySQL => 'mysql', - PostgreSQL => 'Pg', - Oracle => 'Oracle', + MySQL => 'mysql', + PostgreSQL => 'Pg', + Oracle => 'Oracle', ); # ------------------------------------------------------------------- @@ -44,27 +41,36 @@ sub produce { my $no_comments = $t->no_comments; my $schema = $t->schema; my $args = $t->producer_args; + if ( my $fmt = $args->{'format_pkg_name'} ) { + $t->format_package_name( $fmt ); + } + if ( my $fmt = $args->{'format_fk_name'} ) { + $t->format_fk_name( $fmt ); + } my $db_user = $args->{'db_user'} || ''; - my $db_pass = $args->{'db_pass'} || ''; - my $main_pkg_name = $args->{'main_pkg_name'} || + my $db_pass = $args->{'db_password'} || ''; + my $main_pkg_name = $args->{'package_name'} || + # $args->{'main_pkg_name'} || # keep this? undocumented $t->format_package_name('DBI'); my $header = header_comment( __PACKAGE__, "# " ); my $parser_type = ( split /::/, $t->parser_type )[-1]; my $from = $CDBI_auto_pkgs{$parser_type} || ''; - my $dsn = $args->{'dsn'} || sprintf( 'dbi:%s:_', - $CDBI_auto_pkgs{$parser_type} - ? $CDBI_auto_pkgs{$parser_type} - : $parser_type ); - my $sep = '# ' . '-' x 67; + my $dsn = $args->{'dsn'} || sprintf( 'dbi:%s:_', + $CDBI_auto_pkgs{ $parser_type } + ? $CDBI_auto_pkgs{ $parser_type } : $parser_type + ); + my $sep = '# ' . '-' x 67; + # # Identify "link tables" (have only PK and FK fields). # my %linkable; my %linktable; - foreach my $table ( $schema->get_tables ) { + for my $table ( $schema->get_tables ) { + debug("PKG: Table = ", $table->name, "\n"); my $is_link = 1; - foreach my $field ( $table->get_fields ) { + for my $field ( $table->get_fields ) { unless ( $field->is_primary_key or $field->is_foreign_key ) { $is_link = 0; last; @@ -100,14 +106,14 @@ sub produce { } # - # Iterate over all tables. + # Iterate over all tables # my ( %packages, $order ); for my $table ( $schema->get_tables ) { my $table_name = $table->name or next; - my $table_pkg_name = $t->format_package_name($table_name); - $packages{$table_pkg_name} = { + my $table_pkg_name = join '::', $main_pkg_name, $t->format_package_name($table_name); + $packages{ $table_pkg_name } = { order => ++$order, pkg_name => $table_pkg_name, base => $main_pkg_name, @@ -117,18 +123,18 @@ sub produce { # # Primary key may have a differenct accessor method name # - if ( my $constraint = $table->primary_key ) { - my $field = ( $constraint->fields )[0]; - $packages{ $table_pkg_name }{'_columns_primary'} = $field; - - if ( my $pk_xform = $t->format_pk_name ) { - my $pk_name = $pk_xform->( $table_pkg_name, $field ); - - $packages{$table_pkg_name}{'pk_accessor'} = - "#\n# Primary key accessor\n#\n" - . "sub $pk_name {\n shift->$field\n}\n\n"; - } - } +# if ( my $constraint = $table->primary_key ) { +# my $field = ( $constraint->fields )[0]; +# $packages{ $table_pkg_name }{'_columns_primary'} = $field; +# +# if ( my $pk_xform = $t->format_pk_name ) { +# my $pk_name = $pk_xform->( $table_pkg_name, $field ); +# +# $packages{$table_pkg_name}{'pk_accessor'} = +# "#\n# Primary key accessor\n#\n" +# . "sub $pk_name {\n shift->$field\n}\n\n"; +# } +# } my $is_data = 0; foreach my $field ( $table->get_fields ) { @@ -149,19 +155,19 @@ sub produce { my $linkmethodname; if ( my $fk_xform = $t->format_fk_name ) { + # ADD CALLBACK FOR PLURALIZATION MANGLING HERE $linkmethodname = $fk_xform->( - $linkable{$table_name}{$link}->name, - ( $schema->get_table($link)->primary_key->fields )[0] + $linkable{ $table_name }{ $link }->name, + ( $schema->get_table( $link )->primary_key->fields )[0] ) . 's'; } else { - # ADD CALLBACK FOR PLURALIZATION MANGLING HERE $linkmethodname = - $linkable{$table_name}{$link}->name . '_' - . ( $schema->get_table($link)->primary_key->fields )[0] + $linkable{ $table_name }{ $link }->name . '_' + . ( $schema->get_table( $link )->primary_key->fields )[0] . 's'; } @@ -171,15 +177,18 @@ sub produce { { next unless $field->is_foreign_key; - next - unless ( $field->foreign_key_reference->reference_table eq + next unless ( + $field->foreign_key_reference->reference_table eq $table_name - || $field->foreign_key_reference->reference_table eq - $link ); + || + $field->foreign_key_reference->reference_table eq $link + ); + push @lk_fields, ( $field->foreign_key_reference->reference_fields )[0] if $field->foreign_key_reference->reference_table eq $link; + push @rk_fields, $field->name if $field->foreign_key_reference->reference_table eq $table_name; @@ -206,16 +215,17 @@ sub produce { # # Else there is more than one way to traverse it. # ack! Let's treat these types of link tables as - # a many-to-one (easier). + # a many-to-one (easier) # - # NOTE: We need to rethink the link method name, + # NOTE: we need to rethink the link method name, # as the cardinality has shifted on us. # } elsif ( scalar(@rk_fields) == 1 ) { foreach my $rk_field (@rk_fields) { - + # # ADD CALLBACK FOR PLURALIZATION MANGLING HERE + # push @{ $packages{$table_pkg_name}{'has_many'}{$link} }, "sub " . $linkable{$table_name}{$link}->name @@ -226,7 +236,9 @@ sub produce { } } elsif ( scalar(@lk_fields) == 1 ) { + # # These will be taken care of on the other end... + # } else { # @@ -236,7 +248,6 @@ sub produce { # work here. # foreach my $rk_field (@rk_fields) { - # ADD CALLBACK FOR PLURALIZATION MANGLING HERE push @{ $packages{$table_pkg_name}{'has_many'}{$link} }, "sub " @@ -258,46 +269,45 @@ sub produce { if ( $field->is_foreign_key ) { my $table_name = $table->name; my $field_name = $field->name; - my $fk_method = $t->format_fk_name( $table_name, $field_name ) - || $field_name; +# my $fk_method = $t->format_fk_name( $table_name, $field_name ); + my $fk_method = join('::', $table_pkg_name, + $t->format_fk_name( $table_name, $field_name ) + ); my $fk = $field->foreign_key_reference; my $ref_table = $fk->reference_table; my $ref_pkg = $t->format_package_name($ref_table); my $ref_field = ( $fk->reference_fields )[0]; +# my $fk_method = join('::', +# $table_pkg_name, $t->format_fk_name( $ref_table ) +# ); push @{ $packages{$table_pkg_name}{'has_a'} }, - "$table_pkg_name->has_a(\n" - . " $field_name => '$ref_pkg'\n);\n\n" - . "sub $fk_method {\n" - . " return shift->$field_name\n}\n\n" + "$table_pkg_name->has_a(\n" + . " $field_name => '$ref_pkg'\n);\n\n" + . "sub $fk_method {\n" + . " return shift->$field_name\n}\n\n" ; - # - # If this table "has a" to the other, then it follows - # that the other table "has many" of this one, right? - # - # No... there is the possibility of 1-1 cardinality - # - # If there weren't M-M relationships via the has_many + # if there weren't M-M relationships via the has_many # being set up here, create nice pluralized method alias - # rather for user as alt. to ugly tablename_fieldname name. + # rather for user as alt. to ugly tablename_fieldname name # - if ( !$packages{$ref_pkg}{'has_many'}{$table_name} ) { - - # ADD CALLBACK FOR PLURALIZATION MANGLING HERE - push @{ $packages{$ref_pkg}{'has_many'}{$table_name} }, - "sub ${table_name}s {\n ". - "return shift->$table_name\_$field_name\n}\n\n" - ; - } - else { - ; # nothing? why is this here? -ky - } +# if ( !$packages{$ref_pkg}{'has_many'}{$table_name} ) { +# # +# # ADD CALLBACK FOR PLURALIZATION MANGLING HERE +# # +# push @{ $packages{$ref_pkg}{'has_many'}{$table_name} }, +# "sub ${table_name}s {\n " . +# "return shift->$table_name\_$field_name\n}\n\n"; +# # else ugly +# } +# else { +# } push @{ $packages{$ref_pkg}{'has_many'}{$table_name} }, - "$ref_pkg->has_many(\n '${table_name}_${field_name}',\n" - . " '$table_pkg_name' => '$field_name'\n);\n\n" - ; + "$ref_pkg->has_many(\n '${table_name}_${field_name}', " + . "'$table_pkg_name' => '$field_name'\n);\n\n"; + } } } @@ -318,7 +328,8 @@ sub produce { sort { $packages{ $a }{'order'} <=> $packages{ $b }{'order'} } keys %packages ) { - my $pkg = $packages{ $pkg_name }; + my $pkg = $packages{$pkg_name} or next; + next unless $pkg->{'pkg_name'}; $create .= join ( "\n", $sep, @@ -327,50 +338,22 @@ sub produce { "use Class::DBI::Pager;\n\n", ); - # - # The approach here is to do lazy loading on the expensive - # columns (expensive defined as those columns which require - # construction of a referenced object) fields which are - # strictly data (ie, not references) are treated as essential - # b/c they don't require much time to set up. - # - $create .= $pkg_name . "->table('" . $pkg->{'table'} . "');\n\n"; - - # set up primary key field - if ( $pkg->{'_columns_primary'} ) { - $create .= $pkg_name - . "->columns(\n Primary => qw/" - . $pkg->{'_columns_primary'} . "/\n);\n\n"; - } - else { - die "Class::DBI isn't going to like that you don't have". - " a primary key field for table " . $pkg->{'table'} . - " in package '$pkg_name'"; - } - - # - # Set up non-FK fields to be populated at construction. - # - if ( $pkg->{'_columns_essential'} ) { - $create .= $pkg_name - . "->columns(\n" - . autoformat( ' Essential => qw/' . - join ( ' ', @{ $pkg->{'_columns_essential'} } ) . '/' - ) . ");\n\n" - ; - } + if ( $from ) { + $create .= + $pkg->{'pkg_name'}."->set_up_table('".$pkg->{'table'}."');\n\n"; + } + else { + my $table = $schema->get_table( $pkg->{'table'} ); + my @field_names = map { $_->name } $table->get_fields; + + $create .= join("\n", + $pkg_name."->table('".$pkg->{'table'}."');\n", + $pkg_name."->columns(All => qw/". + join(' ', @field_names)."/);\n\n", + ); + } - # - # Set up FK fields for lazy loading on request. - # - if ( $pkg->{'_columns_others'} ) { - $create .= $pkg_name - . "->columns(\n" - . autoformat( ' Others => qw/' . - join ( ' ', @{ $pkg->{'_columns_others'} } ) . '/' - ) . ");\n\n" - ; - } + $create .= "\n"; if ( my $pk = $pkg->{'pk_accessor'} ) { $create .= $pk; @@ -415,6 +398,6 @@ configuration. See L for details on how this works. =head1 AUTHORS -Allen Day Eallenday@ucla.eduE +Allen Day Eallenday@ucla.eduE, Ying Zhang Ezyolive@yahoo.comE, -Ken Y. Clark Ekclark@cpan.org. +Ken Y. Clark Ekclark@cpan.orgE.