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";
60 package ${dbixschema};
61 use base 'DBIx::Class::Schema';
66 foreach my $table ($schema->get_tables)
68 my $tname = $table->name;
70 package ${dbixschema}::${tname};
71 use base 'DBIx::Class';
73 __PACKAGE__->load_components(qw/${pkclass} Core/);
74 __PACKAGE__->table('${tname}');
81 is_auto_increment => $_->is_auto_increment,
82 is_foreign_key => $_->is_foreign_key,
83 is_nullable => $_->is_nullable,
84 default_value => $_->default_value,
85 data_type => $_->data_type,
88 } ($table->get_fields);
90 $output .= "\n__PACKAGE__->add_columns(";
91 foreach my $f (@fields)
93 local $Data::Dumper::Terse = 1;
94 $output .= "\n '" . (keys %$f)[0] . "' => " ;
96 Data::Dumper->Dump([values %$f],
100 $output .= $colinfo . ",";
104 my $pk = $table->primary_key;
107 my @pk = map { $_->name } ($pk->fields);
108 $output .= "__PACKAGE__->set_primary_key(";
109 $output .= "'" . join("', '", @pk) . "');";
112 foreach my $cont ($table->get_constraints)
114 # print Data::Dumper::Dumper($cont->type);
115 if($cont->type =~ /foreign key/i)
117 $output .= "\n__PACKAGE__->belongs_to('" .
118 $cont->fields->[0]->name . "', '" .
119 "${dbixschema}::" . $cont->reference_table . "');\n";
121 my $other = "\n__PACKAGE__->has_many('" .
122 "get_" . $table->name. "', '" .
123 "${dbixschema}::" . $table->name. "', '" .
124 $cont->fields->[0]->name . "');";
125 $tableextras{$cont->reference_table} .= $other;
129 $tableoutput{$table->name} = $output;
132 foreach my $te (keys %tableextras)
134 $tableoutput{$te} .= $tableextras{$te} . "\n";
137 $schemaoutput .= "\n__PACKAGE__->load_classes(";
138 foreach my $to (keys %tableoutput)
140 $output .= $tableoutput{$to};
141 $schemaoutput .= "'${to}',";
144 $schemaoutput .= ");\n";
147 return $schemaoutput . $output . "\n1;\n";