Commit | Line | Data |
f42e7027 |
1 | package SQL::Translator::Producer::ClassDBI; |
2 | |
3 | # ------------------------------------------------------------------- |
829d7837 |
4 | # $Id: ClassDBI.pm,v 1.13 2003-06-10 15:06:04 kycl4rk Exp $ |
f42e7027 |
5 | # ------------------------------------------------------------------- |
5f054727 |
6 | # Copyright (C) 2003 Allen Day <allenday@ucla.edu>, |
7 | # Ying Zhang <zyolive@yahoo.com> |
f42e7027 |
8 | # |
9 | # This program is free software; you can redistribute it and/or |
10 | # modify it under the terms of the GNU General Public License as |
11 | # published by the Free Software Foundation; version 2. |
12 | # |
13 | # This program is distributed in the hope that it will be useful, but |
14 | # WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | # General Public License for more details. |
17 | # |
18 | # You should have received a copy of the GNU General Public License |
19 | # along with this program; if not, write to the Free Software |
20 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
21 | # 02111-1307 USA |
22 | # ------------------------------------------------------------------- |
23 | |
24 | use strict; |
25 | use vars qw[ $VERSION $DEBUG ]; |
829d7837 |
26 | $VERSION = sprintf "%d.%02d", q$Revision: 1.13 $ =~ /(\d+)\.(\d+)/; |
f42e7027 |
27 | $DEBUG = 1 unless defined $DEBUG; |
28 | |
6c45056f |
29 | use SQL::Translator::Schema::Constants; |
5ee19df8 |
30 | use SQL::Translator::Utils qw(header_comment); |
f42e7027 |
31 | use Data::Dumper; |
32 | |
33 | sub produce { |
6c45056f |
34 | my ($translator, $data) = @_; |
35 | local $DEBUG = $translator->debug; |
36 | my $no_comments = $translator->no_comments; |
37 | my $schema = $translator->schema; |
df602cfb |
38 | |
6c45056f |
39 | my $create; |
40 | $create .= header_comment(__PACKAGE__, "# ") unless ($no_comments); |
df602cfb |
41 | |
6c45056f |
42 | $create .= "package " . $translator->format_package_name('DBI'). ";\n\n"; |
df602cfb |
43 | |
6c45056f |
44 | $create .= "my \$USER = '';\n"; |
45 | $create .= "my \$PASS = '';\n\n"; |
df602cfb |
46 | |
6c45056f |
47 | my $from = _from($translator->parser_type()); |
df602cfb |
48 | |
6c45056f |
49 | $create .= "use base 'Class::DBI::$from';\n\n" . |
50 | $translator->format_package_name('DBI') . |
51 | "->set_db('Main', 'dbi:$from:_', \$USER, \$PASS);\n\n"; |
df602cfb |
52 | |
6c45056f |
53 | # |
54 | # Iterate over all tables |
55 | # |
56 | for my $table ( $schema->get_tables ) { |
57 | my $table_name = $table->name or next; |
58 | my %pk; |
59 | |
60 | unless ( $no_comments ) { |
df602cfb |
61 | $create .= |
6c45056f |
62 | "#\n# Package: " . |
df602cfb |
63 | $translator->format_package_name($table_name). |
64 | "\n#\n" |
6c45056f |
65 | } |
66 | |
829d7837 |
67 | $create .= "package ". |
68 | $translator->format_package_name($table_name).";\n"; |
df602cfb |
69 | $create .= "use base ".$translator->format_package_name('DBI').";\n"; |
6c45056f |
70 | $create .= "use mixin 'Class::DBI::Join';\n"; |
71 | $create .= "use Class::DBI::Pager;\n\n"; |
829d7837 |
72 | $create .= $translator->format_package_name($table_name). |
73 | "->set_up_table('$table_name');\n\n"; |
df602cfb |
74 | |
6c45056f |
75 | # |
76 | # Primary key? |
77 | # |
78 | foreach my $constraint ( $table->get_constraints ) { |
79 | next unless $constraint->type eq PRIMARY_KEY; |
80 | my $field = ($constraint->fields)[0]; |
df602cfb |
81 | |
6c45056f |
82 | $pk{ $table_name } = $field; |
83 | $create .= "sub " .$translator->format_pk_name( |
84 | $translator->format_package_name( $table_name ), |
85 | $field |
86 | ) . " { shift->".$field." }\n\n"; |
87 | } |
df602cfb |
88 | |
6c45056f |
89 | # |
90 | # Find foreign keys |
91 | # |
92 | foreach my $field ( $table->get_fields ) { |
93 | if ( $field->is_foreign_key ) { |
94 | my $field_name = $field->name; |
95 | my $fk = $field->foreign_key_reference; |
96 | my $ref_table = $fk->reference_table; |
97 | my @ref_fields = $fk->reference_fields; |
98 | my @fields = $fk->fields; |
99 | |
100 | $create .= $translator->format_package_name($table_name). |
226d3a81 |
101 | "->has_a(\n " . |
6c45056f |
102 | $translator->format_package_name($ref_table). |
103 | " => '$field_name'\n);\n\n"; |
104 | $create .= "sub " . |
105 | $translator->format_fk_name($ref_table, $field_name). |
106 | " {\n return shift->$field_name\n}\n\n"; |
107 | } |
108 | } |
df602cfb |
109 | |
7813f003 |
110 | # |
111 | # Identify link tables, defined as tables that have only PK and FK |
112 | # fields |
113 | # |
114 | my %linkable; |
829d7837 |
115 | foreach my $table ( $schema->get_tables) { |
116 | my $is_link = 1; |
117 | foreach my $field ($table->get_fields){ |
118 | unless ( $field->is_primary_key or $field->is_foreign_key ) { |
119 | $is_link = 0; |
120 | last; |
121 | } |
122 | } |
123 | |
124 | if ( $is_link ) { |
125 | foreach my $left ( $table->get_fields ) { |
126 | next unless $left->is_foreign_key and |
127 | $schema->get_table ( |
128 | $left->foreign_key_reference->reference_table |
129 | )->get_field( |
130 | ($left->foreign_key_reference->reference_fields)[0] |
131 | )->is_primary_key; |
132 | |
133 | foreach my $right ( $table->get_fields ) { |
134 | #skip the diagonal |
135 | next if $left->name eq $right->name; |
136 | next unless $right->is_foreign_key and |
137 | $schema->get_table( |
138 | $right->foreign_key_reference->reference_table |
139 | )->get_field( |
140 | ($right->foreign_key_reference->reference_fields)[0] |
141 | )->is_primary_key; |
142 | |
143 | $linkable{ |
144 | $left->foreign_key_reference->reference_table |
145 | }{ |
146 | $right->foreign_key_reference->reference_table |
147 | } = $table; |
148 | |
149 | $linkable{ |
150 | $right->foreign_key_reference->reference_table |
151 | }{ |
152 | $left->foreign_key_reference->reference_table |
153 | } = $table; |
154 | |
155 | # if($left->foreign_key_reference->reference_table eq 'feature' and |
156 | # $right->foreign_key_reference->reference_table eq 'pub'){ |
157 | # warn $left->foreign_key_reference->reference_table . " to " . $right->foreign_key_reference->reference_table . " via " . $table->name; |
158 | # warn "\tleft: ".$left->name; |
159 | # warn "\tright: ".$right->name; |
160 | # } |
161 | } |
162 | } |
163 | } |
7813f003 |
164 | } |
165 | |
166 | |
167 | # |
168 | # Generate many-to-many linking methods for data tables |
169 | # |
df602cfb |
170 | my $is_data = 0; |
829d7837 |
171 | for ( $table->get_fields ) { |
172 | $is_data++ if !$_->is_foreign_key and !$_->is_primary_key; |
173 | } |
174 | |
7813f003 |
175 | my %linked; |
829d7837 |
176 | if ( $is_data ) { |
177 | foreach my $link ( keys %{ $linkable{ $table->name } } ) { |
178 | my $linkmethodname = |
179 | "_". $translator->format_fk_name($table->name,$link)."_refs"; |
180 | |
181 | |
182 | $create .= $translator->format_package_name($table->name). |
183 | "->has_many('$linkmethodname','". |
184 | $translator->format_package_name( |
185 | $linkable{ $table->name }{ $link }->name |
186 | ) . "','" . $link . "');\n"; |
187 | |
188 | $create .= "sub ". $translator->format_fk_name($table,$link). |
189 | # HARDCODED 's' HERE. ADD CALLBACK |
190 | # FOR PLURALIZATION MANGLING |
191 | "s {\n my \$self = shift; return map \$_->".$link. |
192 | ", \$self->".$linkmethodname.";\n}\n\n"; |
193 | } |
194 | } |
195 | } |
6c45056f |
196 | |
197 | $create .= '1;'; |
198 | |
6c45056f |
199 | return $create; |
f42e7027 |
200 | } |
201 | |
202 | |
203 | sub _from { |
6c45056f |
204 | my $from = shift; |
205 | my @temp = split(/::/, $from); |
206 | $from = $temp[$#temp]; |
df602cfb |
207 | |
6c45056f |
208 | if ( $from eq 'MySQL') { |
209 | $from = lc($from); |
210 | } elsif ( $from eq 'PostgreSQL') { |
211 | $from = 'Pg'; |
212 | } elsif ( $from eq 'Oracle') { |
213 | $from = 'Oracle'; |
214 | } else { |
215 | die "__PACKAGE__ can't handle vendor $from"; |
216 | } |
df602cfb |
217 | |
6c45056f |
218 | return $from; |
f42e7027 |
219 | } |
220 | |
221 | 1; |
222 | |
223 | __END__ |
224 | |
225 | =head1 NAME |
226 | |
6c45056f |
227 | SQL::Translator::Producer::ClassDBI - |
228 | Translate SQL schemata into Class::DBI classes |
f42e7027 |
229 | |
230 | =head1 SYNOPSIS |
231 | |
1eee27d3 |
232 | Use this producer as you would any other from SQL::Translator. See |
233 | L<SQL::Translator> for details. |
f42e7027 |
234 | |
1eee27d3 |
235 | This package utilizes SQL::Translator's formatting methods |
236 | format_package_name(), format_pk_name(), format_fk_name(), and |
237 | format_table_name() as it creates classes, one per table in the schema |
238 | provided. An additional base class is also created for database connectivity |
239 | configuration. See L<Class::DBI> for details on how this works. |
f42e7027 |
240 | |
6c45056f |
241 | =head1 AUTHORS |
f42e7027 |
242 | |
6c45056f |
243 | Allen Day E<lt>allenday@ucla.eduE<gt> |
5f054727 |
244 | Ying Zhang E<lt>zyolive@yahoo.comE<gt>, |
6c45056f |
245 | Ken Y. Clark E<lt>kclark@cpan.org<gt>. |