X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator%2FProducer%2FDBIx%2FClass%2FFile.pm;h=5099a134628355ce5acb88285bd4fa4461bbb09c;hb=d4daee7b54e38e4b3d3d0a77759bddc1a4ede6e5;hp=736bd7324ce4f5ab08900bbf3f43ca26f2f017b8;hpb=f8a88db3f8e08b54a5930944cfa9bb80085b921d;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/SQL/Translator/Producer/DBIx/Class/File.pm b/lib/SQL/Translator/Producer/DBIx/Class/File.pm index 736bd73..5099a13 100644 --- a/lib/SQL/Translator/Producer/DBIx/Class/File.pm +++ b/lib/SQL/Translator/Producer/DBIx/Class/File.pm @@ -8,7 +8,7 @@ SQL::Translator::Producer::DBIx::Class::File - DBIx::Class file producer use SQL::Translator; - my $t = SQL::Translator->new( parser => '...', + my $t = SQL::Translator->new( parser => '...', producer => 'DBIx::Class::File' ); print $translator->translate( $file ); @@ -25,6 +25,7 @@ $DEBUG = 0 unless defined $DEBUG; use SQL::Translator::Schema::Constants; use SQL::Translator::Utils qw(header_comment); +use Data::Dumper (); ## Skip all column type translation, as we want to use whatever the parser got. @@ -48,7 +49,7 @@ sub produce my $output = ''; # Steal the XML producers "prefix" arg for our namespace? - my $dbixschema = $translator->producer_args()->{prefix} || + my $dbixschema = $translator->producer_args()->{prefix} || $schema->name || 'My::Schema'; my $pkclass = $parser2PK{$translator->parser_type} || ''; @@ -57,8 +58,11 @@ sub produce $tt_vars{pkclass} = $pkclass; my $schemaoutput .= << "DATA"; + package ${dbixschema}; use base 'DBIx::Class::Schema'; +use strict; +use warnings; DATA my %tableoutput = (); @@ -67,15 +71,18 @@ DATA { my $tname = $table->name; my $output .= qq{ + package ${dbixschema}::${tname}; use base 'DBIx::Class'; +use strict; +use warnings; __PACKAGE__->load_components(qw/${pkclass} Core/); __PACKAGE__->table('${tname}'); }; - my @fields = map + my @fields = map { { $_->name => { name => $_->name, is_auto_increment => $_->is_auto_increment, @@ -92,7 +99,7 @@ __PACKAGE__->table('${tname}'); { local $Data::Dumper::Terse = 1; $output .= "\n '" . (keys %$f)[0] . "' => " ; - my $colinfo = + my $colinfo = Data::Dumper->Dump([values %$f], [''] # keys %$f] ); @@ -106,7 +113,7 @@ __PACKAGE__->table('${tname}'); { my @pk = map { $_->name } ($pk->fields); $output .= "__PACKAGE__->set_primary_key("; - $output .= "'" . join("', '", @pk) . "');"; + $output .= "'" . join("', '", @pk) . "');\n"; } foreach my $cont ($table->get_constraints) @@ -114,10 +121,14 @@ __PACKAGE__->table('${tname}'); # print Data::Dumper::Dumper($cont->type); if($cont->type =~ /foreign key/i) { - $output .= "\n__PACKAGE__->belongs_to('" . +# $output .= "\n__PACKAGE__->belongs_to('" . +# $cont->fields->[0]->name . "', '" . +# "${dbixschema}::" . $cont->reference_table . "');\n"; + + $tableextras{$table->name} .= "\n__PACKAGE__->belongs_to('" . $cont->fields->[0]->name . "', '" . "${dbixschema}::" . $cont->reference_table . "');\n"; - + my $other = "\n__PACKAGE__->has_many('" . "get_" . $table->name. "', '" . "${dbixschema}::" . $table->name. "', '" . @@ -126,23 +137,22 @@ __PACKAGE__->table('${tname}'); } } - $tableoutput{$table->name} = $output; + $tableoutput{$table->name} .= $output; } - foreach my $te (keys %tableextras) + foreach my $to (keys %tableoutput) { - $tableoutput{$te} .= $tableextras{$te} . "\n"; + $output .= $tableoutput{$to}; + $schemaoutput .= "\n__PACKAGE__->register_class('${to}', '${dbixschema}::${to}');\n"; } - $schemaoutput .= "\n__PACKAGE__->load_classes("; - foreach my $to (keys %tableoutput) + foreach my $te (keys %tableextras) { - $output .= $tableoutput{$to}; - $schemaoutput .= "'${to}',"; + $output .= "\npackage ${dbixschema}::$te;\n"; + $output .= $tableextras{$te} . "\n"; +# $tableoutput{$te} .= $tableextras{$te} . "\n"; } - chop $schemaoutput; - $schemaoutput .= ");\n"; # print "$output\n"; - return $schemaoutput . $output . "\n1;\n"; + return "${output}\n\n${schemaoutput}\n1;\n"; }