1 package SQL::Translator::Producer::DBIx::Class::File;
5 SQL::Translator::Producer::DBIx::Class::File - DBIx::Class file producer
11 my $t = SQL::Translator->new( parser => '...',
12 producer => 'DBIx::Class::File' );
13 print $translator->translate( $file );
17 Creates a DBIx::Class::Schema for use with DBIx::Class
22 use vars qw[ $VERSION $DEBUG $WARN ];
24 $DEBUG = 0 unless defined $DEBUG;
26 use SQL::Translator::Schema::Constants;
27 use SQL::Translator::Utils qw(header_comment);
29 ## Skip all column type translation, as we want to use whatever the parser got.
31 ## Translate parsers -> PK::Auto::Foo, however
34 MySQL => 'PK::Auto::MySQL',
35 PostgreSQL => 'PK::Auto::Pg',
36 DB2 => 'PK::Auto::DB2',
37 Oracle => 'PK::Auto::Oracle',
42 my ($translator) = @_;
43 $DEBUG = $translator->debug;
44 $WARN = $translator->show_warnings;
45 my $no_comments = $translator->no_comments;
46 my $add_drop_table = $translator->add_drop_table;
47 my $schema = $translator->schema;
50 # Steal the XML producers "prefix" arg for our namespace?
51 my $dbixschema = $translator->producer_args()->{prefix} ||
52 $schema->name || 'My::Schema';
53 my $pkclass = $parser2PK{$translator->parser_type} || '';
56 $tt_vars{dbixschema} = $dbixschema;
57 $tt_vars{pkclass} = $pkclass;
59 my $schemaoutput .= << "DATA";
61 package ${dbixschema};
62 use base 'DBIx::Class::Schema';
69 foreach my $table ($schema->get_tables)
71 my $tname = $table->name;
74 package ${dbixschema}::${tname};
75 use base 'DBIx::Class';
79 __PACKAGE__->load_components(qw/${pkclass} Core/);
80 __PACKAGE__->table('${tname}');
87 is_auto_increment => $_->is_auto_increment,
88 is_foreign_key => $_->is_foreign_key,
89 is_nullable => $_->is_nullable,
90 default_value => $_->default_value,
91 data_type => $_->data_type,
94 } ($table->get_fields);
96 $output .= "\n__PACKAGE__->add_columns(";
97 foreach my $f (@fields)
99 local $Data::Dumper::Terse = 1;
100 $output .= "\n '" . (keys %$f)[0] . "' => " ;
102 Data::Dumper->Dump([values %$f],
106 $output .= $colinfo . ",";
110 my $pk = $table->primary_key;
113 my @pk = map { $_->name } ($pk->fields);
114 $output .= "__PACKAGE__->set_primary_key(";
115 $output .= "'" . join("', '", @pk) . "');\n";
118 foreach my $cont ($table->get_constraints)
120 # print Data::Dumper::Dumper($cont->type);
121 if($cont->type =~ /foreign key/i)
123 # $output .= "\n__PACKAGE__->belongs_to('" .
124 # $cont->fields->[0]->name . "', '" .
125 # "${dbixschema}::" . $cont->reference_table . "');\n";
127 $tableextras{$table->name} .= "\n__PACKAGE__->belongs_to('" .
128 $cont->fields->[0]->name . "', '" .
129 "${dbixschema}::" . $cont->reference_table . "');\n";
131 my $other = "\n__PACKAGE__->has_many('" .
132 "get_" . $table->name. "', '" .
133 "${dbixschema}::" . $table->name. "', '" .
134 $cont->fields->[0]->name . "');";
135 $tableextras{$cont->reference_table} .= $other;
139 $tableoutput{$table->name} .= $output;
142 foreach my $to (keys %tableoutput)
144 $output .= $tableoutput{$to};
145 $schemaoutput .= "\n__PACKAGE__->register_class('${to}', '${dbixschema}::${to}');\n";
148 foreach my $te (keys %tableextras)
150 $output .= "\npackage ${dbixschema}::$te;\n";
151 $output .= $tableextras{$te} . "\n";
152 # $tableoutput{$te} .= $tableextras{$te} . "\n";
156 return "${output}\n\n${schemaoutput}\n1;\n";