1 package SQL::Translator::Producer::ClassDBI;
3 # -------------------------------------------------------------------
4 # $Id: ClassDBI.pm,v 1.39 2003-12-04 18:48:33 kycl4rk Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2003 Allen Day <allenday@ucla.edu>,
7 # Ying Zhang <zyolive@yahoo.com>
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.
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.
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
22 # -------------------------------------------------------------------
25 use vars qw[ $VERSION $DEBUG ];
26 $VERSION = sprintf "%d.%02d", q$Revision: 1.39 $ =~ /(\d+)\.(\d+)/;
27 $DEBUG = 1 unless defined $DEBUG;
29 use SQL::Translator::Schema::Constants;
30 use SQL::Translator::Utils qw(header_comment);
33 my %CDBI_auto_pkgs = (
39 # -------------------------------------------------------------------
43 local $DEBUG = $t->debug;
44 my $no_comments = $t->no_comments;
45 my $schema = $t->schema;
46 my $args = $t->producer_args;
47 my $db_user = $args->{'db_user'} || '';
48 my $db_pass = $args->{'db_pass'} || '';
49 my $main_pkg_name = $args->{'main_pkg_name'} ||
50 $t->format_package_name('DBI');
51 my $header = header_comment( __PACKAGE__, "# " );
52 my $parser_type = ( split /::/, $t->parser_type )[-1];
53 my $from = $CDBI_auto_pkgs{$parser_type} || '';
54 my $dsn = $args->{'dsn'} || sprintf( 'dbi:%s:_',
55 $CDBI_auto_pkgs{ $parser_type }
56 ? $CDBI_auto_pkgs{ $parser_type } : $parser_type
58 my $sep = '# ' . '-' x 67;
61 # Identify "link tables" (have only PK and FK fields).
65 foreach my $table ( $schema->get_tables ) {
67 foreach my $field ( $table->get_fields ) {
68 unless ( $field->is_primary_key or $field->is_foreign_key ) {
76 foreach my $left ( $table->get_fields ) {
77 next unless $left->is_foreign_key;
78 my $lfk = $left->foreign_key_reference or next;
79 my $lr_table = $schema->get_table( $lfk->reference_table )
81 my $lr_field_name = ( $lfk->reference_fields )[0];
82 my $lr_field = $lr_table->get_field($lr_field_name);
83 next unless $lr_field->is_primary_key;
85 foreach my $right ( $table->get_fields ) {
86 next if $left->name eq $right->name;
88 my $rfk = $right->foreign_key_reference or next;
89 my $rr_table = $schema->get_table( $rfk->reference_table )
91 my $rr_field_name = ( $rfk->reference_fields )[0];
92 my $rr_field = $rr_table->get_field($rr_field_name);
93 next unless $rr_field->is_primary_key;
95 $linkable{ $lr_table->name }{ $rr_table->name } = $table;
96 $linkable{ $rr_table->name }{ $lr_table->name } = $table;
97 $linktable{ $table->name } = $table;
103 # Iterate over all tables
105 my ( %packages, $order );
106 for my $table ( $schema->get_tables ) {
107 my $table_name = $table->name or next;
109 my $table_pkg_name = $t->format_package_name($table_name);
110 $packages{$table_pkg_name} = {
112 pkg_name => $table_pkg_name,
113 base => $main_pkg_name,
114 table => $table_name,
118 # Primary key may have a differenct accessor method name
120 if ( my $constraint = $table->primary_key ) {
121 my $field = ( $constraint->fields )[0];
122 $packages{ $table_pkg_name }{'_columns_primary'} = $field;
124 if ( my $pk_xform = $t->format_pk_name ) {
125 my $pk_name = $pk_xform->( $table_pkg_name, $field );
127 $packages{$table_pkg_name}{'pk_accessor'} =
128 "#\n# Primary key accessor\n#\n"
129 . "sub $pk_name {\n shift->$field\n}\n\n";
134 foreach my $field ( $table->get_fields ) {
135 if ( !$field->is_foreign_key and !$field->is_primary_key ) {
136 push @{ $packages{$table_pkg_name}{'_columns_essential'} },
140 elsif ( !$field->is_primary_key ) {
141 push @{ $packages{$table_pkg_name}{'_columns_others'} },
148 foreach my $link ( keys %{ $linkable{$table_name} } ) {
151 if ( my $fk_xform = $t->format_fk_name ) {
153 # ADD CALLBACK FOR PLURALIZATION MANGLING HERE
154 $linkmethodname = $fk_xform->(
155 $linkable{ $table_name }{ $link }->name,
156 ( $schema->get_table( $link )->primary_key->fields )[0]
161 # ADD CALLBACK FOR PLURALIZATION MANGLING HERE
163 $linkable{ $table_name }{ $link }->name . '_'
164 . ( $schema->get_table( $link )->primary_key->fields )[0]
170 foreach my $field ( $linkable{$table_name}{$link}->get_fields )
172 next unless $field->is_foreign_key;
175 $field->foreign_key_reference->reference_table eq
178 $field->foreign_key_reference->reference_table eq $link
182 ( $field->foreign_key_reference->reference_fields )[0]
183 if $field->foreign_key_reference->reference_table eq
186 push @rk_fields, $field->name
187 if $field->foreign_key_reference->reference_table eq
192 # If one possible traversal via link table.
194 if ( scalar(@rk_fields) == 1 and scalar(@lk_fields) == 1 ) {
195 foreach my $rk_field (@rk_fields) {
196 push @{ $packages{$table_pkg_name}{'has_many'}{$link} },
199 . " { my \$self = shift; "
201 . ( $schema->get_table($link)->primary_key->fields )
204 . $linkable{$table_name}{$link}->name . "_"
210 # Else there is more than one way to traverse it.
211 # ack! Let's treat these types of link tables as
212 # a many-to-one (easier)
214 # NOTE: we need to rethink the link method name,
215 # as the cardinality has shifted on us.
218 elsif ( scalar(@rk_fields) == 1 ) {
219 foreach my $rk_field (@rk_fields) {
221 # ADD CALLBACK FOR PLURALIZATION MANGLING HERE
223 push @{ $packages{$table_pkg_name}{'has_many'}{$link} },
225 . $linkable{$table_name}{$link}->name
226 . "s { my \$self = shift; return \$self->"
227 . $linkable{$table_name}{$link}->name . "_"
232 elsif ( scalar(@lk_fields) == 1 ) {
234 # These will be taken care of on the other end...
239 # Many many many. Need multiple iterations here,
240 # data structure revision to handle N FK sources.
241 # This code has not been tested and likely doesn't
244 foreach my $rk_field (@rk_fields) {
245 # ADD CALLBACK FOR PLURALIZATION MANGLING HERE
246 push @{ $packages{$table_pkg_name}{'has_many'}{$link} },
248 . $linkable{$table_name}{$link}->name . "_"
250 . "s { my \$self = shift; return \$self->"
251 . $linkable{$table_name}{$link}->name . "_"
260 # Use foreign keys to set up "has_a/has_many" relationships.
262 foreach my $field ( $table->get_fields ) {
263 if ( $field->is_foreign_key ) {
264 my $table_name = $table->name;
265 my $field_name = $field->name;
266 my $fk_method = $t->format_fk_name( $table_name, $field_name );
267 my $fk = $field->foreign_key_reference;
268 my $ref_table = $fk->reference_table;
269 my $ref_pkg = $t->format_package_name($ref_table);
270 my $ref_field = ( $fk->reference_fields )[0];
272 push @{ $packages{$table_pkg_name}{'has_a'} },
273 "$table_pkg_name->has_a(\n"
274 . " $field_name => '$ref_pkg'\n);\n\n"
275 . "sub $fk_method {\n"
276 . " return shift->$field_name\n}\n\n";
278 # if there weren't M-M relationships via the has_many
279 # being set up here, create nice pluralized method alias
280 # rather for user as alt. to ugly tablename_fieldname name
282 if ( !$packages{$ref_pkg}{'has_many'}{$table_name} ) {
284 # ADD CALLBACK FOR PLURALIZATION MANGLING HERE
286 push @{ $packages{$ref_pkg}{'has_many'}{$table_name} },
287 "sub ${table_name}s {\n " .
288 "return shift->$table_name\_$field_name\n}\n\n";
294 push @{ $packages{$ref_pkg}{'has_many'}{$table_name} },
295 "$ref_pkg->has_many(\n '${table_name}_${field_name}', "
296 . "'$table_pkg_name' => '$field_name'\n);\n\n";
303 # Now build up text of package.
305 my $base_pkg = sprintf( 'Class::DBI%s', $from ? "::$from" : '' );
306 $create .= join ( "\n",
307 "package $main_pkg_name;\n",
310 "use base '$base_pkg';\n",
311 "$main_pkg_name->set_db('Main', '$dsn', '$db_user', '$db_pass');\n\n",
315 sort { $packages{ $a }{'order'} <=> $packages{ $b }{'order'} }
318 my $pkg = $packages{$pkg_name} or next;
319 next unless $pkg->{'pkg_name'};
321 $create .= join ( "\n",
323 "package " . $pkg->{'pkg_name'} . ";",
324 "use base '" . $pkg->{'base'} . "';",
325 "use Class::DBI::Pager;\n\n",
330 $pkg->{'pkg_name'}."->set_up_table('".$pkg->{'table'}."');\n\n";
333 my $table = $schema->get_table( $pkg->{'table'} );
334 my @field_names = map { $_->name } $table->get_fields;
336 $create .= join("\n",
337 $pkg_name."->table('".$pkg->{'table'}."');\n",
338 $pkg_name."->columns(All => qw/".
339 join(' ', @field_names)."/);\n\n",
345 if ( my $pk = $pkg->{'pk_accessor'} ) {
349 if ( my @has_a = @{ $pkg->{'has_a'} || [] } ) {
350 $create .= $_ for @has_a;
353 foreach my $has_many_key ( keys %{ $pkg->{'has_many'} } ) {
354 if ( my @has_many = @{ $pkg->{'has_many'}{$has_many_key} || [] } ) {
355 $create .= $_ for @has_many;
367 # -------------------------------------------------------------------
373 SQL::Translator::Producer::ClassDBI - create Class::DBI classes from schema
377 Use this producer as you would any other from SQL::Translator. See
378 L<SQL::Translator> for details.
380 This package utilizes SQL::Translator's formatting methods
381 format_package_name(), format_pk_name(), format_fk_name(), and
382 format_table_name() as it creates classes, one per table in the schema
383 provided. An additional base class is also created for database connectivity
384 configuration. See L<Class::DBI> for details on how this works.
388 Allen Day E<lt>allenday@ucla.eduE<gt>,
389 Ying Zhang E<lt>zyolive@yahoo.comE<gt>,
390 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.