1 package SQL::Translator::Producer::ClassDBI;
4 use vars qw[ $VERSION $DEBUG ];
6 $DEBUG = 1 unless defined $DEBUG;
8 use SQL::Translator::Schema::Constants;
9 use SQL::Translator::Utils qw(debug header_comment);
12 my %CDBI_auto_pkgs = (
18 # -------------------------------------------------------------------
21 local $DEBUG = $t->debug;
22 my $no_comments = $t->no_comments;
23 my $schema = $t->schema;
24 my $args = $t->producer_args;
27 if ( my $fmt = $args->{'format_pkg_name'} ) {
28 $t->format_package_name( $fmt );
31 if ( my $fmt = $args->{'format_fk_name'} ) {
32 $t->format_fk_name( $fmt );
35 my $db_user = $args->{'db_user'} || '';
36 my $db_pass = $args->{'db_password'} || '';
37 my $main_pkg_name = $args->{'package_name'} ||
38 # $args->{'main_pkg_name'} || # keep this? undocumented
39 $t->format_package_name('DBI');
40 my $header = header_comment( __PACKAGE__, "# " );
41 my $parser_type = ( split /::/, $t->parser_type )[-1];
42 my $from = $CDBI_auto_pkgs{$parser_type} || '';
43 my $dsn = $args->{'dsn'} || sprintf( 'dbi:%s:_',
44 $CDBI_auto_pkgs{ $parser_type }
45 ? $CDBI_auto_pkgs{ $parser_type } : $parser_type
47 my $sep = '# ' . '-' x 67;
51 # Identify "link tables" (have only PK and FK fields).
55 for my $table ( $schema->get_tables ) {
56 debug("PKG: Table = ", $table->name, "\n");
58 for my $field ( $table->get_fields ) {
59 unless ( $field->is_primary_key or $field->is_foreign_key ) {
67 foreach my $left ( $table->get_fields ) {
68 next unless $left->is_foreign_key;
69 my $lfk = $left->foreign_key_reference or next;
70 my $lr_table = $schema->get_table( $lfk->reference_table )
72 my $lr_field_name = ( $lfk->reference_fields )[0];
73 my $lr_field = $lr_table->get_field($lr_field_name);
74 next unless $lr_field->is_primary_key;
76 foreach my $right ( $table->get_fields ) {
77 next if $left->name eq $right->name;
79 my $rfk = $right->foreign_key_reference or next;
80 my $rr_table = $schema->get_table( $rfk->reference_table )
82 my $rr_field_name = ( $rfk->reference_fields )[0];
83 my $rr_field = $rr_table->get_field($rr_field_name);
84 next unless $rr_field->is_primary_key;
86 $linkable{ $lr_table->name }{ $rr_table->name } = $table;
87 $linkable{ $rr_table->name }{ $lr_table->name } = $table;
88 $linktable{ $table->name } = $table;
94 # Iterate over all tables
96 my ( %packages, $order );
97 for my $table ( $schema->get_tables ) {
98 my $table_name = $table->name or next;
100 my $table_pkg_name = join '::', $main_pkg_name, $t->format_package_name($table_name);
101 $packages{ $table_pkg_name } = {
103 pkg_name => $table_pkg_name,
104 base => $main_pkg_name,
105 table => $table_name,
109 # Primary key may have a differenct accessor method name
111 # if ( my $constraint = $table->primary_key ) {
112 # my $field = ( $constraint->fields )[0];
113 # $packages{ $table_pkg_name }{'_columns_primary'} = $field;
115 # if ( my $pk_xform = $t->format_pk_name ) {
116 # my $pk_name = $pk_xform->( $table_pkg_name, $field );
118 # $packages{$table_pkg_name}{'pk_accessor'} =
119 # "#\n# Primary key accessor\n#\n"
120 # . "sub $pk_name {\n shift->$field\n}\n\n";
125 foreach my $field ( $table->get_fields ) {
126 if ( !$field->is_foreign_key and !$field->is_primary_key ) {
127 push @{ $packages{$table_pkg_name}{'_columns_essential'} },
131 elsif ( !$field->is_primary_key ) {
132 push @{ $packages{$table_pkg_name}{'_columns_others'} },
139 foreach my $link ( keys %{ $linkable{$table_name} } ) {
142 if ( my $fk_xform = $t->format_fk_name ) {
144 # ADD CALLBACK FOR PLURALIZATION MANGLING HERE
145 $linkmethodname = $fk_xform->(
146 $linkable{ $table_name }{ $link }->name,
147 ( $schema->get_table( $link )->primary_key->fields )[0]
152 # ADD CALLBACK FOR PLURALIZATION MANGLING HERE
154 $linkable{ $table_name }{ $link }->name . '_'
155 . ( $schema->get_table( $link )->primary_key->fields )[0]
161 foreach my $field ( $linkable{$table_name}{$link}->get_fields )
163 next unless $field->is_foreign_key;
166 $field->foreign_key_reference->reference_table eq
169 $field->foreign_key_reference->reference_table eq $link
173 ( $field->foreign_key_reference->reference_fields )[0]
174 if $field->foreign_key_reference->reference_table eq
177 push @rk_fields, $field->name
178 if $field->foreign_key_reference->reference_table eq
183 # If one possible traversal via link table.
185 if ( scalar(@rk_fields) == 1 and scalar(@lk_fields) == 1 ) {
186 foreach my $rk_field (@rk_fields) {
187 push @{ $packages{$table_pkg_name}{'has_many'}{$link} },
190 . " { my \$self = shift; "
192 . ( $schema->get_table($link)->primary_key->fields )
195 . $linkable{$table_name}{$link}->name . "_"
201 # Else there is more than one way to traverse it.
202 # ack! Let's treat these types of link tables as
203 # a many-to-one (easier)
205 # NOTE: we need to rethink the link method name,
206 # as the cardinality has shifted on us.
209 elsif ( scalar(@rk_fields) == 1 ) {
210 foreach my $rk_field (@rk_fields) {
212 # ADD CALLBACK FOR PLURALIZATION MANGLING HERE
214 push @{ $packages{$table_pkg_name}{'has_many'}{$link} },
216 . $linkable{$table_name}{$link}->name
217 . "s { my \$self = shift; return \$self->"
218 . $linkable{$table_name}{$link}->name . "_"
223 elsif ( scalar(@lk_fields) == 1 ) {
225 # These will be taken care of on the other end...
230 # Many many many. Need multiple iterations here,
231 # data structure revision to handle N FK sources.
232 # This code has not been tested and likely doesn't
235 foreach my $rk_field (@rk_fields) {
236 # ADD CALLBACK FOR PLURALIZATION MANGLING HERE
237 push @{ $packages{$table_pkg_name}{'has_many'}{$link} },
239 . $linkable{$table_name}{$link}->name . "_"
241 . "s { my \$self = shift; return \$self->"
242 . $linkable{$table_name}{$link}->name . "_"
251 # Use foreign keys to set up "has_a/has_many" relationships.
253 foreach my $field ( $table->get_fields ) {
254 if ( $field->is_foreign_key ) {
255 my $table_name = $table->name;
256 my $field_name = $field->name;
257 # my $fk_method = $t->format_fk_name( $table_name, $field_name );
258 my $fk_method = join('::', $table_pkg_name,
259 $t->format_fk_name( $table_name, $field_name )
261 my $fk = $field->foreign_key_reference;
262 my $ref_table = $fk->reference_table;
263 my $ref_pkg = $t->format_package_name($ref_table);
264 my $ref_field = ( $fk->reference_fields )[0];
265 # my $fk_method = join('::',
266 # $table_pkg_name, $t->format_fk_name( $ref_table )
269 push @{ $packages{$table_pkg_name}{'has_a'} },
270 "$table_pkg_name->has_a(\n"
271 . " $field_name => '$ref_pkg'\n);\n\n"
272 . "sub $fk_method {\n"
273 . " return shift->$field_name\n}\n\n"
276 # if there weren't M-M relationships via the has_many
277 # being set up here, create nice pluralized method alias
278 # rather for user as alt. to ugly tablename_fieldname name
280 # if ( !$packages{$ref_pkg}{'has_many'}{$table_name} ) {
282 # # ADD CALLBACK FOR PLURALIZATION MANGLING HERE
284 # push @{ $packages{$ref_pkg}{'has_many'}{$table_name} },
285 # "sub ${table_name}s {\n " .
286 # "return shift->$table_name\_$field_name\n}\n\n";
292 push @{ $packages{$ref_pkg}{'has_many'}{$table_name} },
293 "$ref_pkg->has_many(\n '${table_name}_${field_name}', "
294 . "'$table_pkg_name' => '$field_name'\n);\n\n";
301 # Now build up text of package.
303 my $base_pkg = sprintf( 'Class::DBI%s', $from ? "::$from" : '' );
304 push @create, join ( "\n",
305 "package $main_pkg_name;\n",
308 "use base '$base_pkg';\n",
309 "$main_pkg_name->set_db('Main', '$dsn', '$db_user', '$db_pass');\n\n",
313 sort { $packages{ $a }{'order'} <=> $packages{ $b }{'order'} }
316 my $pkg = $packages{$pkg_name} or next;
317 next unless $pkg->{'pkg_name'};
319 push @create, join ( "\n",
321 "package " . $pkg->{'pkg_name'} . ";",
322 "use base '" . $pkg->{'base'} . "';",
323 "use Class::DBI::Pager;\n\n",
327 push @create, join('',
335 my $table = $schema->get_table( $pkg->{'table'} );
336 my @field_names = map { $_->name } $table->get_fields;
338 push @create, join("\n",
339 $pkg_name."->table('".$pkg->{'table'}."');\n",
340 $pkg_name."->columns(All => qw/".
341 join(' ', @field_names)."/);\n\n",
347 if ( my $pk = $pkg->{'pk_accessor'} ) {
351 if ( my @has_a = @{ $pkg->{'has_a'} || [] } ) {
352 push @create, $_ for @has_a;
355 foreach my $has_many_key ( keys %{ $pkg->{'has_many'} } ) {
356 if ( my @has_many = @{ $pkg->{'has_many'}{$has_many_key} || [] } ) {
357 push @create, $_ for @has_many;
362 push @create, "1;\n";
371 # -------------------------------------------------------------------
377 SQL::Translator::Producer::ClassDBI - create Class::DBI classes from schema
381 Use this producer as you would any other from SQL::Translator. See
382 L<SQL::Translator> for details.
384 This package uses SQL::Translator's formatting methods
385 format_package_name(), format_pk_name(), format_fk_name(), and
386 format_table_name() as it creates classes, one per table in the schema
387 provided. An additional base class is also created for database connectivity
388 configuration. See L<Class::DBI> for details on how this works.
392 Allen Day E<lt>allenday@ucla.eduE<gt>,
393 Ying Zhang E<lt>zyolive@yahoo.comE<gt>,
394 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.