1 package SQL::Translator::Producer::ClassDBI;
3 # -------------------------------------------------------------------
4 # $Id: ClassDBI.pm,v 1.24 2003-06-27 02:28:11 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.24 $ =~ /(\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 # -------------------------------------------------------------------
42 local $DEBUG = $t->debug;
43 my $no_comments = $t->no_comments;
44 my $schema = $t->schema;
45 my $args = $t->producer_args;
46 my $db_user = $args->{'db_user'} || '';
47 my $db_pass = $args->{'db_pass'} || '';
48 my $main_pkg_name = $t->format_package_name('DBI');
49 my $header = header_comment(__PACKAGE__, "# ");
50 my $parser_type = ( split /::/, $t->parser_type )[-1];
51 my $from = $CDBI_auto_pkgs{ $parser_type } || '';
52 my $dsn = $args->{'dsn'} || sprintf( 'dbi:%s:_',
53 $CDBI_auto_pkgs{ $parser_type }
54 ? $CDBI_auto_pkgs{ $parser_type } : $parser_type
56 my $sep = '# ' . '-' x 67;
59 # Iterate over all tables
61 my ( %packages, $order );
62 for my $table ( $schema->get_tables ) {
63 my $table_name = $table->name or next;
65 my $table_pkg_name = $t->format_package_name($table_name);
66 $packages{ $table_pkg_name } = {
68 pkg_name => $table_pkg_name,
69 base => $main_pkg_name,
74 # Primary key may have a differenct accessor method name
76 if ( my $pk_xform = $t->format_pk_name ) {
77 if ( my $constraint = $table->primary_key ) {
78 my $field = ($constraint->fields)[0];
79 my $pk_name = $pk_xform->($table_pkg_name, $field);
81 $packages{ $table_pkg_name }{'pk_accessor'} =
82 "#\n# Primary key accessor\n#\n".
83 "sub $pk_name {\n shift->$field\n}\n\n"
89 # Use foreign keys to set up "has_a/has_many" relationships.
91 foreach my $field ( $table->get_fields ) {
92 if ( $field->is_foreign_key ) {
93 my $table_name = $table->name;
94 my $field_name = $field->name;
95 my $fk_method = $t->format_fk_name($table_name, $field_name);
96 my $fk = $field->foreign_key_reference;
97 my $ref_table = $fk->reference_table;
98 my $ref_pkg = $t->format_package_name($ref_table);
99 my $ref_field = ($fk->reference_fields)[0];
101 push @{ $packages{ $table_pkg_name }{'has_a'} },
102 "$table_pkg_name->has_a(\n".
103 " $field_name => '$ref_pkg'\n);\n\n".
104 "sub $fk_method {\n".
105 " return shift->$field_name\n}\n\n"
109 # If this table "has a" to the other, then it follows
110 # that the other table "has many" of this one, right?
112 push @{ $packages{ $ref_pkg }{'has_many'} },
113 "$ref_pkg->has_many(\n '${table_name}_${field_name}', ".
114 "'$table_pkg_name' => '$field_name'\n);\n\n"
120 # Identify link tables, defined as tables that have
121 # only PK and FK fields.
125 # foreach my $table ( $schema->get_tables ) {
127 # foreach my $field ( $table->get_fields ) {
128 # unless ( $field->is_primary_key or $field->is_foreign_key ) {
135 # foreach my $left ( $table->get_fields ) {
136 # next unless $left->is_foreign_key and $schema->get_table(
137 # $left->foreign_key_reference->reference_table
140 # next unless $left->is_foreign_key and $schema->get_table(
141 # $left->foreign_key_reference->reference_table
143 # ($left->foreign_key_reference->reference_fields)[0]
146 # foreach my $right ( $table->get_fields ) {
148 # next if $left->name eq $right->name;
150 # next unless $right->is_foreign_key and
151 # $schema->get_table(
152 # $right->foreign_key_reference->reference_table
156 # next unless $right->is_foreign_key and
157 # $schema->get_table(
158 # $right->foreign_key_reference->reference_table
160 # ($right->foreign_key_reference->reference_fields)[0]
166 # $left->foreign_key_reference->reference_table
168 # $right->foreign_key_reference->reference_table
172 # $right->foreign_key_reference->reference_table
174 # $left->foreign_key_reference->reference_table
177 # $linktable{ $table->name } = $table;
184 # # Generate many-to-many linking methods for data tables
187 # for ( $table->get_fields ) {
188 # $is_data++ if !$_->is_foreign_key and !$_->is_primary_key;
193 # foreach my $link ( keys %{ $linkable{ $table->name } } ) {
194 # my $linkmethodname =
195 # "_".$t->format_fk_name($table->name,$link)."_refs"
198 # $create .= $t->format_package_name($table->name).
199 # "->has_many('$linkmethodname','".
200 # $t->format_package_name(
201 # $linkable{$table->name}{$link}->name
203 # ($schema->get_table($link)->primary_key->fields)[0]."');\n"
206 # $create .= "sub ". $t->format_fk_name($table,$link).
207 # # HARDCODED 's' HERE.
208 # # ADD CALLBACK FOR PLURALIZATION MANGLING
209 # "s {\n my \$self = shift; return map \$_->".$link.
210 # ", \$self->".$linkmethodname.";\n}\n\n"
216 my $base_pkg = sprintf( 'Class::DBI%s', $from ? "::$from" : '' );
217 my $create = join("\n",
218 "package $main_pkg_name;\n",
221 "use base '$base_pkg';\n",
222 "$main_pkg_name->set_db('Main', '$dsn', '$db_user', '$db_pass');\n\n",
226 sort { $packages{ $a }{'order'} <=> $packages{ $b }{'order'} }
229 my $pkg = $packages{ $pkg_name };
231 $create .= join("\n",
233 "package ".$pkg->{'pkg_name'}.";",
234 "use base '".$pkg->{'base'}."';",
235 "use Class::DBI::Pager;\n\n",
240 $pkg->{'pkg_name'}."->set_up_table('".$pkg->{'table'}."');\n\n";
243 my $table = $schema->get_table( $pkg->{'table'} );
244 my @field_names = map { $_->name } $table->get_fields;
246 $create .= join("\n",
247 $pkg_name."->table('".$pkg->{'table'}."');\n",
248 $pkg_name."->columns(All => qw/".
249 join(' ', @field_names)."/);\n\n",
253 if ( my $pk = $pkg->{'pk_accessor'} ) {
257 if ( my @has_a = @{ $pkg->{'has_a'} || [] } ) {
258 $create .= $_ for @has_a;
261 if ( my @has_many = @{ $pkg->{'has_many'} || [] } ) {
262 $create .= $_ for @has_many;
273 # -------------------------------------------------------------------
279 SQL::Translator::Producer::ClassDBI - create Class::DBI classes from schema
283 Use this producer as you would any other from SQL::Translator. See
284 L<SQL::Translator> for details.
286 This package utilizes SQL::Translator's formatting methods
287 format_package_name(), format_pk_name(), format_fk_name(), and
288 format_table_name() as it creates classes, one per table in the schema
289 provided. An additional base class is also created for database connectivity
290 configuration. See L<Class::DBI> for details on how this works.
294 Allen Day E<lt>allenday@ucla.eduE<gt>
295 Ying Zhang E<lt>zyolive@yahoo.comE<gt>,
296 Ken Y. Clark E<lt>kclark@cpan.org<gt>.