Commit | Line | Data |
c990f24a |
1 | package SQL::Translator::Producer::DBIx::Class::File; |
2 | |
3 | =head1 NAME |
4 | |
5 | SQL::Translator::Producer::DBIx::Class::File - DBIx::Class file producer |
6 | |
7 | =head1 SYNOPSIS |
8 | |
9 | use SQL::Translator; |
10 | |
75d07914 |
11 | my $t = SQL::Translator->new( parser => '...', |
c990f24a |
12 | producer => 'DBIx::Class::File' ); |
13 | print $translator->translate( $file ); |
14 | |
15 | =head1 DESCRIPTION |
16 | |
17 | Creates a DBIx::Class::Schema for use with DBIx::Class |
18 | |
19 | =cut |
20 | |
21 | use strict; |
22 | use vars qw[ $VERSION $DEBUG $WARN ]; |
23 | $VERSION = '0.1'; |
24 | $DEBUG = 0 unless defined $DEBUG; |
25 | |
26 | use SQL::Translator::Schema::Constants; |
27 | use SQL::Translator::Utils qw(header_comment); |
28 | |
29 | ## Skip all column type translation, as we want to use whatever the parser got. |
30 | |
31 | ## Translate parsers -> PK::Auto::Foo, however |
32 | |
33 | my %parser2PK = ( |
34 | MySQL => 'PK::Auto::MySQL', |
35 | PostgreSQL => 'PK::Auto::Pg', |
36 | DB2 => 'PK::Auto::DB2', |
37 | Oracle => 'PK::Auto::Oracle', |
38 | ); |
39 | |
40 | sub produce |
41 | { |
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; |
48 | my $output = ''; |
49 | |
50 | # Steal the XML producers "prefix" arg for our namespace? |
75d07914 |
51 | my $dbixschema = $translator->producer_args()->{prefix} || |
c990f24a |
52 | $schema->name || 'My::Schema'; |
53 | my $pkclass = $parser2PK{$translator->parser_type} || ''; |
54 | |
55 | my %tt_vars = (); |
56 | $tt_vars{dbixschema} = $dbixschema; |
57 | $tt_vars{pkclass} = $pkclass; |
58 | |
59 | my $schemaoutput .= << "DATA"; |
8dc441df |
60 | |
c990f24a |
61 | package ${dbixschema}; |
62 | use base 'DBIx::Class::Schema'; |
8dc441df |
63 | use strict; |
64 | use warnings; |
c990f24a |
65 | DATA |
66 | |
67 | my %tableoutput = (); |
68 | my %tableextras = (); |
69 | foreach my $table ($schema->get_tables) |
70 | { |
71 | my $tname = $table->name; |
72 | my $output .= qq{ |
8dc441df |
73 | |
c990f24a |
74 | package ${dbixschema}::${tname}; |
75 | use base 'DBIx::Class'; |
8dc441df |
76 | use strict; |
77 | use warnings; |
c990f24a |
78 | |
79 | __PACKAGE__->load_components(qw/${pkclass} Core/); |
80 | __PACKAGE__->table('${tname}'); |
81 | |
82 | }; |
83 | |
75d07914 |
84 | my @fields = map |
c990f24a |
85 | { { $_->name => { |
86 | name => $_->name, |
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, |
92 | size => $_->size, |
93 | } } |
94 | } ($table->get_fields); |
95 | |
96 | $output .= "\n__PACKAGE__->add_columns("; |
97 | foreach my $f (@fields) |
98 | { |
99 | local $Data::Dumper::Terse = 1; |
100 | $output .= "\n '" . (keys %$f)[0] . "' => " ; |
75d07914 |
101 | my $colinfo = |
c990f24a |
102 | Data::Dumper->Dump([values %$f], |
103 | [''] # keys %$f] |
104 | ); |
105 | chomp($colinfo); |
106 | $output .= $colinfo . ","; |
107 | } |
108 | $output .= "\n);\n"; |
109 | |
110 | my $pk = $table->primary_key; |
111 | if($pk) |
112 | { |
113 | my @pk = map { $_->name } ($pk->fields); |
114 | $output .= "__PACKAGE__->set_primary_key("; |
8dc441df |
115 | $output .= "'" . join("', '", @pk) . "');\n"; |
c990f24a |
116 | } |
117 | |
118 | foreach my $cont ($table->get_constraints) |
119 | { |
120 | # print Data::Dumper::Dumper($cont->type); |
121 | if($cont->type =~ /foreign key/i) |
122 | { |
75d07914 |
123 | # $output .= "\n__PACKAGE__->belongs_to('" . |
8dc441df |
124 | # $cont->fields->[0]->name . "', '" . |
125 | # "${dbixschema}::" . $cont->reference_table . "');\n"; |
126 | |
75d07914 |
127 | $tableextras{$table->name} .= "\n__PACKAGE__->belongs_to('" . |
c990f24a |
128 | $cont->fields->[0]->name . "', '" . |
129 | "${dbixschema}::" . $cont->reference_table . "');\n"; |
130 | |
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; |
136 | } |
137 | } |
138 | |
8dc441df |
139 | $tableoutput{$table->name} .= $output; |
c990f24a |
140 | } |
141 | |
8dc441df |
142 | foreach my $to (keys %tableoutput) |
c990f24a |
143 | { |
8dc441df |
144 | $output .= $tableoutput{$to}; |
75d07914 |
145 | $schemaoutput .= "\n__PACKAGE__->register_class('${to}', '${dbixschema}::${to}');\n"; |
c990f24a |
146 | } |
147 | |
8dc441df |
148 | foreach my $te (keys %tableextras) |
c990f24a |
149 | { |
8dc441df |
150 | $output .= "\npackage ${dbixschema}::$te;\n"; |
151 | $output .= $tableextras{$te} . "\n"; |
152 | # $tableoutput{$te} .= $tableextras{$te} . "\n"; |
c990f24a |
153 | } |
154 | |
f8a88db3 |
155 | # print "$output\n"; |
8dc441df |
156 | return "${output}\n\n${schemaoutput}\n1;\n"; |
c990f24a |
157 | } |