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
19 =head1 FURTHER QUESTIONS?
21 Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
23 =head1 COPYRIGHT AND LICENSE
25 This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
26 by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
27 redistribute it and/or modify it under the same terms as the
28 L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.
33 our ($VERSION, $DEBUG, $WARN);
35 $DEBUG = 0 unless defined $DEBUG;
37 use SQL::Translator::Schema::Constants;
38 use SQL::Translator::Utils qw(header_comment);
41 ## Skip all column type translation, as we want to use whatever the parser got.
43 ## Translate parsers -> PK::Auto::Foo, however
46 MySQL => 'PK::Auto::MySQL',
47 PostgreSQL => 'PK::Auto::Pg',
48 DB2 => 'PK::Auto::DB2',
49 Oracle => 'PK::Auto::Oracle',
54 my ($translator) = @_;
55 $DEBUG = $translator->debug;
56 $WARN = $translator->show_warnings;
57 my $no_comments = $translator->no_comments;
58 my $add_drop_table = $translator->add_drop_table;
59 my $schema = $translator->schema;
62 # Steal the XML producers "prefix" arg for our namespace?
63 my $dbixschema = $translator->producer_args()->{prefix} ||
64 $schema->name || 'My::Schema';
65 my $pkclass = $parser2PK{$translator->parser_type} || '';
68 $tt_vars{dbixschema} = $dbixschema;
69 $tt_vars{pkclass} = $pkclass;
71 my $schemaoutput .= << "DATA";
73 package ${dbixschema};
74 use base 'DBIx::Class::Schema';
81 foreach my $table ($schema->get_tables)
83 my $tname = $table->name;
86 package ${dbixschema}::${tname};
87 use base 'DBIx::Class';
91 __PACKAGE__->load_components(qw/${pkclass} Core/);
92 __PACKAGE__->table('${tname}');
99 is_auto_increment => $_->is_auto_increment,
100 is_foreign_key => $_->is_foreign_key,
101 is_nullable => $_->is_nullable,
102 default_value => $_->default_value,
103 data_type => $_->data_type,
106 } ($table->get_fields);
108 $output .= "\n__PACKAGE__->add_columns(";
109 foreach my $f (@fields)
111 local $Data::Dumper::Terse = 1;
112 $output .= "\n '" . (keys %$f)[0] . "' => " ;
114 Data::Dumper->Dump([values %$f],
118 $output .= $colinfo . ",";
122 my $pk = $table->primary_key;
125 my @pk = map { $_->name } ($pk->fields);
126 $output .= "__PACKAGE__->set_primary_key(";
127 $output .= "'" . join("', '", @pk) . "');\n";
130 foreach my $cont ($table->get_constraints)
132 # print Data::Dumper::Dumper($cont->type);
133 if($cont->type =~ /foreign key/i)
135 # $output .= "\n__PACKAGE__->belongs_to('" .
136 # $cont->fields->[0]->name . "', '" .
137 # "${dbixschema}::" . $cont->reference_table . "');\n";
139 $tableextras{$table->name} .= "\n__PACKAGE__->belongs_to('" .
140 $cont->fields->[0]->name . "', '" .
141 "${dbixschema}::" . $cont->reference_table . "');\n";
143 my $other = "\n__PACKAGE__->has_many('" .
144 "get_" . $table->name. "', '" .
145 "${dbixschema}::" . $table->name. "', '" .
146 $cont->fields->[0]->name . "');";
147 $tableextras{$cont->reference_table} .= $other;
151 $tableoutput{$table->name} .= $output;
154 foreach my $to (keys %tableoutput)
156 $output .= $tableoutput{$to};
157 $schemaoutput .= "\n__PACKAGE__->register_class('${to}', '${dbixschema}::${to}');\n";
160 foreach my $te (keys %tableextras)
162 $output .= "\npackage ${dbixschema}::$te;\n";
163 $output .= $tableextras{$te} . "\n";
164 # $tableoutput{$te} .= $tableextras{$te} . "\n";
168 return "${output}\n\n${schemaoutput}\n1;\n";