« blib/lib/SQL/Translator/Producer/HTML.pm
↑ Summary
blib/lib/SQL/Translator/Parser/DBI/PostgreSQL.pm »
| 1 | package SQL::Translator::Producer::ClassDBI; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | our $DEBUG; | ||
| 6 | our $VERSION = '1.66'; | ||
| 7 | $DEBUG = 1 unless defined $DEBUG; | ||
| 8 | |||
| 9 | use SQL::Translator::Schema::Constants; | ||
| 10 | use SQL::Translator::Utils qw(debug header_comment); | ||
| 11 | use Data::Dumper; | ||
| 12 | |||
| 13 | my %CDBI_auto_pkgs = ( | ||
| 14 | MySQL => 'mysql', | ||
| 15 | PostgreSQL => 'Pg', | ||
| 16 | Oracle => 'Oracle', | ||
| 17 | ); | ||
| 18 | |||
| 19 | sub produce { | ||
| 20 | my $t = shift; | ||
| 21 | local $DEBUG = $t->debug; | ||
| 22 | my $no_comments = $t->no_comments; | ||
| 23 | my $schema = $t->schema; | ||
| 24 | my $args = $t->producer_args; | ||
| 25 | my @create; | ||
| 26 | |||
| 27 | if (my $fmt = $args->{'format_pkg_name'}) { | ||
| 28 | $t->format_package_name($fmt); | ||
| 29 | } | ||
| 30 | |||
| 31 | if (my $fmt = $args->{'format_fk_name'}) { | ||
| 32 | $t->format_fk_name($fmt); | ||
| 33 | } | ||
| 34 | |||
| 35 | my $db_user = $args->{'db_user'} || ''; | ||
| 36 | my $db_pass = $args->{'db_password'} || ''; | ||
| 37 | my $main_pkg_name = $args->{'package_name'} || | ||
| 38 | |||
| 39 | # $args->{'main_pkg_name'} || # keep this? undocumented | ||
| 40 | $t->format_package_name('DBI'); | ||
| 41 | my $header = header_comment(__PACKAGE__, "# "); | ||
| 42 | my $parser_type = (split /::/, $t->parser_type)[-1]; | ||
| 43 | my $from = $CDBI_auto_pkgs{$parser_type} || ''; | ||
| 44 | my $dsn = $args->{'dsn'} || sprintf( | ||
| 45 | 'dbi:%s:_', $CDBI_auto_pkgs{$parser_type} | ||
| 46 | ? $CDBI_auto_pkgs{$parser_type} | ||
| 47 | : $parser_type | ||
| 48 | ); | ||
| 49 | my $sep = '# ' . '-' x 67; | ||
| 50 | |||
| 51 | # | ||
| 52 | # Identify "link tables" (have only PK and FK fields). | ||
| 53 | # | ||
| 54 | my %linkable; | ||
| 55 | my %linktable; | ||
| 56 | for my $table ($schema->get_tables) { | ||
| 57 | debug("PKG: Table = ", $table->name, "\n"); | ||
| 58 | my $is_link = 1; | ||
| 59 | for my $field ($table->get_fields) { | ||
| 60 | unless ($field->is_primary_key or $field->is_foreign_key) { | ||
| 61 | $is_link = 0; | ||
| 62 | last; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | next unless $is_link; | ||
| 67 | |||
| 68 | foreach my $left ($table->get_fields) { | ||
| 69 | next unless $left->is_foreign_key; | ||
| 70 | my $lfk = $left->foreign_key_reference or next; | ||
| 71 | my $lr_table = $schema->get_table($lfk->reference_table) | ||
| 72 | or next; | ||
| 73 | my $lr_field_name = ($lfk->reference_fields)[0]; | ||
| 74 | my $lr_field = $lr_table->get_field($lr_field_name); | ||
| 75 | next unless $lr_field->is_primary_key; | ||
| 76 | |||
| 77 | foreach my $right ($table->get_fields) { | ||
| 78 | next if $left->name eq $right->name; | ||
| 79 | |||
| 80 | my $rfk = $right->foreign_key_reference or next; | ||
| 81 | my $rr_table = $schema->get_table($rfk->reference_table) | ||
| 82 | or next; | ||
| 83 | my $rr_field_name = ($rfk->reference_fields)[0]; | ||
| 84 | my $rr_field = $rr_table->get_field($rr_field_name); | ||
| 85 | next unless $rr_field->is_primary_key; | ||
| 86 | |||
| 87 | $linkable{ $lr_table->name }{ $rr_table->name } = $table; | ||
| 88 | $linkable{ $rr_table->name }{ $lr_table->name } = $table; | ||
| 89 | $linktable{ $table->name } = $table; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | # | ||
| 95 | # Iterate over all tables | ||
| 96 | # | ||
| 97 | my (%packages, $order); | ||
| 98 | for my $table ($schema->get_tables) { | ||
| 99 | my $table_name = $table->name or next; | ||
| 100 | |||
| 101 | my $table_pkg_name = join '::', $main_pkg_name, $t->format_package_name($table_name); | ||
| 102 | $packages{$table_pkg_name} = { | ||
| 103 | order => ++$order, | ||
| 104 | pkg_name => $table_pkg_name, | ||
| 105 | base => $main_pkg_name, | ||
| 106 | table => $table_name, | ||
| 107 | }; | ||
| 108 | |||
| 109 | # | ||
| 110 | # Primary key may have a different accessor method name | ||
| 111 | # | ||
| 112 | # if ( my $constraint = $table->primary_key ) { | ||
| 113 | # my $field = ( $constraint->fields )[0]; | ||
| 114 | # $packages{ $table_pkg_name }{'_columns_primary'} = $field; | ||
| 115 | # | ||
| 116 | # if ( my $pk_xform = $t->format_pk_name ) { | ||
| 117 | # my $pk_name = $pk_xform->( $table_pkg_name, $field ); | ||
| 118 | # | ||
| 119 | # $packages{$table_pkg_name}{'pk_accessor'} = | ||
| 120 | # "#\n# Primary key accessor\n#\n" | ||
| 121 | # . "sub $pk_name {\n shift->$field\n}\n\n"; | ||
| 122 | # } | ||
| 123 | # } | ||
| 124 | |||
| 125 | my $is_data = 0; | ||
| 126 | foreach my $field ($table->get_fields) { | ||
| 127 | if (!$field->is_foreign_key and !$field->is_primary_key) { | ||
| 128 | push @{ $packages{$table_pkg_name}{'_columns_essential'} }, $field->name; | ||
| 129 | $is_data++; | ||
| 130 | } elsif (!$field->is_primary_key) { | ||
| 131 | push @{ $packages{$table_pkg_name}{'_columns_others'} }, $field->name; | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | my %linked; | ||
| 136 | if ($is_data) { | ||
| 137 | foreach my $link (keys %{ $linkable{$table_name} }) { | ||
| 138 | my $linkmethodname; | ||
| 139 | |||
| 140 | if (my $fk_xform = $t->format_fk_name) { | ||
| 141 | |||
| 142 | # ADD CALLBACK FOR PLURALIZATION MANGLING HERE | ||
| 143 | $linkmethodname | ||
| 144 | = $fk_xform->($linkable{$table_name}{$link}->name, ($schema->get_table($link)->primary_key->fields)[0]) | ||
| 145 | . 's'; | ||
| 146 | } else { | ||
| 147 | # ADD CALLBACK FOR PLURALIZATION MANGLING HERE | ||
| 148 | $linkmethodname | ||
| 149 | = $linkable{$table_name}{$link}->name . '_' . ($schema->get_table($link)->primary_key->fields)[0] . 's'; | ||
| 150 | } | ||
| 151 | |||
| 152 | my @rk_fields = (); | ||
| 153 | my @lk_fields = (); | ||
| 154 | foreach my $field ($linkable{$table_name}{$link}->get_fields) { | ||
| 155 | next unless $field->is_foreign_key; | ||
| 156 | |||
| 157 | next | ||
| 158 | unless ($field->foreign_key_reference->reference_table eq $table_name | ||
| 159 | || $field->foreign_key_reference->reference_table eq $link); | ||
| 160 | |||
| 161 | push @lk_fields, ($field->foreign_key_reference->reference_fields)[0] | ||
| 162 | if $field->foreign_key_reference->reference_table eq $link; | ||
| 163 | |||
| 164 | push @rk_fields, $field->name | ||
| 165 | if $field->foreign_key_reference->reference_table eq $table_name; | ||
| 166 | } | ||
| 167 | |||
| 168 | # | ||
| 169 | # If one possible traversal via link table. | ||
| 170 | # | ||
| 171 | if (scalar(@rk_fields) == 1 and scalar(@lk_fields) == 1) { | ||
| 172 | foreach my $rk_field (@rk_fields) { | ||
| 173 | push @{ $packages{$table_pkg_name}{'has_many'}{$link} }, | ||
| 174 | "sub " | ||
| 175 | . $linkmethodname | ||
| 176 | . " { my \$self = shift; " | ||
| 177 | . "return map \$_->" | ||
| 178 | . ($schema->get_table($link)->primary_key->fields)[0] | ||
| 179 | . ", \$self->" | ||
| 180 | . $linkable{$table_name}{$link}->name . "_" | ||
| 181 | . $rk_field | ||
| 182 | . " }\n\n"; | ||
| 183 | } | ||
| 184 | |||
| 185 | # | ||
| 186 | # Else there is more than one way to traverse it. | ||
| 187 | # ack! Let's treat these types of link tables as | ||
| 188 | # a many-to-one (easier) | ||
| 189 | # | ||
| 190 | # NOTE: we need to rethink the link method name, | ||
| 191 | # as the cardinality has shifted on us. | ||
| 192 | # | ||
| 193 | } elsif (scalar(@rk_fields) == 1) { | ||
| 194 | foreach my $rk_field (@rk_fields) { | ||
| 195 | # | ||
| 196 | # ADD CALLBACK FOR PLURALIZATION MANGLING HERE | ||
| 197 | # | ||
| 198 | push @{ $packages{$table_pkg_name}{'has_many'}{$link} }, | ||
| 199 | "sub " | ||
| 200 | . $linkable{$table_name}{$link}->name | ||
| 201 | . "s { my \$self = shift; return \$self->" | ||
| 202 | . $linkable{$table_name}{$link}->name . "_" | ||
| 203 | . $rk_field | ||
| 204 | . "(\@_) }\n\n"; | ||
| 205 | } | ||
| 206 | } elsif (scalar(@lk_fields) == 1) { | ||
| 207 | # | ||
| 208 | # These will be taken care of on the other end... | ||
| 209 | # | ||
| 210 | } else { | ||
| 211 | # | ||
| 212 | # Many many many. Need multiple iterations here, | ||
| 213 | # data structure revision to handle N FK sources. | ||
| 214 | # This code has not been tested and likely doesn't | ||
| 215 | # work here. | ||
| 216 | # | ||
| 217 | foreach my $rk_field (@rk_fields) { | ||
| 218 | |||
| 219 | # ADD CALLBACK FOR PLURALIZATION MANGLING HERE | ||
| 220 | push @{ $packages{$table_pkg_name}{'has_many'}{$link} }, | ||
| 221 | "sub " | ||
| 222 | . $linkable{$table_name}{$link}->name . "_" | ||
| 223 | . $rk_field | ||
| 224 | . "s { my \$self = shift; return \$self->" | ||
| 225 | . $linkable{$table_name}{$link}->name . "_" | ||
| 226 | . $rk_field | ||
| 227 | . "(\@_) }\n\n"; | ||
| 228 | } | ||
| 229 | } | ||
| 230 | } | ||
| 231 | } | ||
| 232 | |||
| 233 | # | ||
| 234 | # Use foreign keys to set up "has_a/has_many" relationships. | ||
| 235 | # | ||
| 236 | foreach my $field ($table->get_fields) { | ||
| 237 | if ($field->is_foreign_key) { | ||
| 238 | my $table_name = $table->name; | ||
| 239 | my $field_name = $field->name; | ||
| 240 | |||
| 241 | # my $fk_method = $t->format_fk_name( $table_name, $field_name ); | ||
| 242 | my $fk_method = join('::', $table_pkg_name, $t->format_fk_name($table_name, $field_name)); | ||
| 243 | my $fk = $field->foreign_key_reference; | ||
| 244 | my $ref_table = $fk->reference_table; | ||
| 245 | my $ref_pkg = $t->format_package_name($ref_table); | ||
| 246 | my $ref_field = ($fk->reference_fields)[0]; | ||
| 247 | |||
| 248 | # my $fk_method = join('::', | ||
| 249 | # $table_pkg_name, $t->format_fk_name( $ref_table ) | ||
| 250 | # ); | ||
| 251 | |||
| 252 | push @{ $packages{$table_pkg_name}{'has_a'} }, | ||
| 253 | "$table_pkg_name->has_a(\n" | ||
| 254 | . " $field_name => '$ref_pkg'\n);\n\n" | ||
| 255 | . "sub $fk_method {\n" | ||
| 256 | . " return shift->$field_name\n}\n\n"; | ||
| 257 | |||
| 258 | # if there weren't M-M relationships via the has_many | ||
| 259 | # being set up here, create nice pluralized method alias | ||
| 260 | # rather for user as alt. to ugly tablename_fieldname name | ||
| 261 | # | ||
| 262 | # if ( !$packages{$ref_pkg}{'has_many'}{$table_name} ) { | ||
| 263 | # # | ||
| 264 | # # ADD CALLBACK FOR PLURALIZATION MANGLING HERE | ||
| 265 | # # | ||
| 266 | # push @{ $packages{$ref_pkg}{'has_many'}{$table_name} }, | ||
| 267 | # "sub ${table_name}s {\n " . | ||
| 268 | # "return shift->$table_name\_$field_name\n}\n\n"; | ||
| 269 | # # else ugly | ||
| 270 | # } | ||
| 271 | # else { | ||
| 272 | # } | ||
| 273 | |||
| 274 | push @{ $packages{$ref_pkg}{'has_many'}{$table_name} }, | ||
| 275 | "$ref_pkg->has_many(\n '${table_name}_${field_name}', " . "'$table_pkg_name' => '$field_name'\n);\n\n"; | ||
| 276 | |||
| 277 | } | ||
| 278 | } | ||
| 279 | } | ||
| 280 | |||
| 281 | # | ||
| 282 | # Now build up text of package. | ||
| 283 | # | ||
| 284 | my $base_pkg = sprintf('Class::DBI%s', $from ? "::$from" : ''); | ||
| 285 | push @create, | ||
| 286 | join("\n", | ||
| 287 | "package $main_pkg_name;\n", | ||
| 288 | $header, "use strict;", | ||
| 289 | "use base '$base_pkg';\n", | ||
| 290 | "$main_pkg_name->set_db('Main', '$dsn', '$db_user', '$db_pass');\n\n", | ||
| 291 | ); | ||
| 292 | |||
| 293 | for my $pkg_name ( | ||
| 294 | sort { $packages{$a}{'order'} <=> $packages{$b}{'order'} } | ||
| 295 | keys %packages | ||
| 296 | ) { | ||
| 297 | my $pkg = $packages{$pkg_name} or next; | ||
| 298 | next unless $pkg->{'pkg_name'}; | ||
| 299 | |||
| 300 | push @create, | ||
| 301 | join("\n", | ||
| 302 | $sep, | ||
| 303 | "package " . $pkg->{'pkg_name'} . ";", | ||
| 304 | "use base '" . $pkg->{'base'} . "';", | ||
| 305 | "use Class::DBI::Pager;\n\n", | ||
| 306 | ); | ||
| 307 | |||
| 308 | if ($from) { | ||
| 309 | push @create, join('', $pkg->{'pkg_name'}, "->set_up_table('", $pkg->{'table'}, "');\n\n"); | ||
| 310 | } else { | ||
| 311 | my $table = $schema->get_table($pkg->{'table'}); | ||
| 312 | my @field_names = map { $_->name } $table->get_fields; | ||
| 313 | |||
| 314 | push @create, | ||
| 315 | join("\n", | ||
| 316 | $pkg_name . "->table('" . $pkg->{'table'} . "');\n", | ||
| 317 | $pkg_name . "->columns(All => qw/" . join(' ', @field_names) . "/);\n\n", | ||
| 318 | ); | ||
| 319 | } | ||
| 320 | |||
| 321 | push @create, "\n"; | ||
| 322 | |||
| 323 | if (my $pk = $pkg->{'pk_accessor'}) { | ||
| 324 | push @create, $pk; | ||
| 325 | } | ||
| 326 | |||
| 327 | if (my @has_a = @{ $pkg->{'has_a'} || [] }) { | ||
| 328 | push @create, $_ for @has_a; | ||
| 329 | } | ||
| 330 | |||
| 331 | foreach my $has_many_key (keys %{ $pkg->{'has_many'} }) { | ||
| 332 | if (my @has_many = @{ $pkg->{'has_many'}{$has_many_key} || [] }) { | ||
| 333 | push @create, $_ for @has_many; | ||
| 334 | } | ||
| 335 | } | ||
| 336 | } | ||
| 337 | |||
| 338 | push @create, "1;\n"; | ||
| 339 | |||
| 340 | return wantarray | ||
| 341 | ? @create | ||
| 342 | : join('', @create); | ||
| 343 | } | ||
| 344 | |||
| 345 | 1; | ||
| 346 | |||
| 347 | =pod | ||
| 348 | |||
| 349 | =head1 NAME | ||
| 350 | |||
| 351 | SQL::Translator::Producer::ClassDBI - create Class::DBI classes from schema | ||
| 352 | |||
| 353 | =head1 SYNOPSIS | ||
| 354 | |||
| 355 | Use this producer as you would any other from SQL::Translator. See | ||
| 356 | L<SQL::Translator> for details. | ||
| 357 | |||
| 358 | This package uses SQL::Translator's formatting methods | ||
| 359 | format_package_name(), format_pk_name(), format_fk_name(), and | ||
| 360 | format_table_name() as it creates classes, one per table in the schema | ||
| 361 | provided. An additional base class is also created for database connectivity | ||
| 362 | configuration. See L<Class::DBI> for details on how this works. | ||
| 363 | |||
| 364 | =head1 AUTHORS | ||
| 365 | |||
| 366 | Allen Day E<lt>allenday@ucla.eduE<gt>, | ||
| 367 | Ying Zhang E<lt>zyolive@yahoo.comE<gt>, | ||
| 368 | Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>. |