1 package SQL::Translator::Producer::ClassDBI;
3 # -------------------------------------------------------------------
4 # $Id: ClassDBI.pm,v 1.26 2003-07-09 05:59:24 allenday 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.26 $ =~ /(\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 = $t->format_package_name('DBI');
50 my $header = header_comment(__PACKAGE__, "# ");
51 my $parser_type = ( split /::/, $t->parser_type )[-1];
52 my $from = $CDBI_auto_pkgs{ $parser_type } || '';
53 my $dsn = $args->{'dsn'} || sprintf( 'dbi:%s:_',
54 $CDBI_auto_pkgs{ $parser_type }
55 ? $CDBI_auto_pkgs{ $parser_type } : $parser_type
57 my $sep = '# ' . '-' x 67;
60 # Identify "link tables" (have only PK and FK fields).
64 foreach my $table ( $schema->get_tables ) {
66 foreach my $field ( $table->get_fields ) {
67 unless ( $field->is_primary_key or $field->is_foreign_key ) {
75 foreach my $left ( $table->get_fields ) {
76 next unless $left->is_foreign_key;
77 my $lfk = $left->foreign_key_reference or next;
78 my $lr_table = $schema->get_table( $lfk->reference_table )
80 my $lr_field_name = ($lfk->reference_fields)[0];
81 my $lr_field = $lr_table->get_field($lr_field_name);
82 next unless $lr_field->is_primary_key;
84 foreach my $right ( $table->get_fields ) {
85 next if $left->name eq $right->name;
87 my $rfk = $right->foreign_key_reference or next;
88 my $rr_table = $schema->get_table( $rfk->reference_table )
90 my $rr_field_name = ($rfk->reference_fields)[0];
91 my $rr_field = $rr_table->get_field($rr_field_name);
92 next unless $rr_field->is_primary_key;
94 $linkable{ $lr_table->name }{ $rr_table->name } = $table;
95 $linkable{ $rr_table->name }{ $lr_table->name } = $table;
96 $linktable{ $table->name } = $table;
102 # Iterate over all tables
104 my ( %packages, $order );
105 for my $table ( $schema->get_tables ) {
106 my $table_name = $table->name or next;
108 my $table_pkg_name = $t->format_package_name($table_name);
109 $packages{ $table_pkg_name } = {
111 pkg_name => $table_pkg_name,
112 base => $main_pkg_name,
113 table => $table_name,
117 # Primary key may have a differenct accessor method name
119 if ( my $pk_xform = $t->format_pk_name ) {
120 if ( my $constraint = $table->primary_key ) {
121 my $field = ($constraint->fields)[0];
122 my $pk_name = $pk_xform->($table_pkg_name, $field);
124 $packages{ $table_pkg_name }{'pk_accessor'} =
125 "#\n# Primary key accessor\n#\n".
126 "sub $pk_name {\n shift->$field\n}\n\n"
132 # Use foreign keys to set up "has_a/has_many" relationships.
135 foreach my $field ( $table->get_fields ) {
136 $is_data++ if !$field->is_foreign_key and !$field->is_primary_key;
137 if ( $field->is_foreign_key ) {
138 my $table_name = $table->name;
139 my $field_name = $field->name;
140 my $fk_method = $t->format_fk_name($table_name, $field_name);
141 my $fk = $field->foreign_key_reference;
142 my $ref_table = $fk->reference_table;
143 my $ref_pkg = $t->format_package_name($ref_table);
144 my $ref_field = ($fk->reference_fields)[0];
146 push @{ $packages{ $table_pkg_name }{'has_a'} },
147 "$table_pkg_name->has_a(\n".
148 " $field_name => '$ref_pkg'\n);\n\n".
149 "sub $fk_method {\n".
150 " return shift->$field_name\n}\n\n"
154 # If this table "has a" to the other, then it follows
155 # that the other table "has many" of this one, right?
157 # No... there is the possibility of 1-1 cardinality
158 push @{ $packages{ $ref_pkg }{'has_many'} },
159 "$ref_pkg->has_many(\n '${table_name}_${field_name}', ".
160 "'$table_pkg_name' => '$field_name'\n);\n\n"
167 foreach my $link ( keys %{ $linkable{ $table_name } } ) {
170 # ADD CALLBACK FOR PLURALIZATION MANGLING HERE
171 if ( my $fk_xform = $t->format_fk_name ){
172 $linkmethodname = $fk_xform->($linkable{$table_name}{$link}->name,
173 ($schema->get_table($link)->primary_key->fields)[0]).'s';
175 $linkmethodname = $linkable{$table_name}{$link}->name.'_'.
176 ($schema->get_table($link)->primary_key->fields)[0].'s';
179 #$create .= $field->name. "\n";
180 #$create .= $field->foreign_key_reference->reference_table. "\n";
181 #$create .= $linkable{ $table_name }{ $link }->name. "\n";
182 #$create .= $table_name. "\n";
183 #$create .= $link. "\n";
184 #$create .= "***\n\n";
188 foreach my $field ($linkable{$table_name}{$link}->get_fields){
189 next unless $field->is_foreign_key;
192 $field->foreign_key_reference->reference_table eq $table_name
194 $field->foreign_key_reference->reference_table eq $link
196 push @lk_fields, ($field->foreign_key_reference->reference_fields)[0]
197 if $field->foreign_key_reference->reference_table eq $link;
198 push @rk_fields, $field->name
199 if $field->foreign_key_reference->reference_table eq $table_name;
202 #if one possible traversal via link table
203 if(scalar(@rk_fields) == 1 and scalar(@lk_fields) == 1){
204 foreach my $rk_field (@rk_fields){
205 push @{ $packages{ $table_pkg_name }{'has_many'} },
206 "sub ".$linkmethodname." { my \$self = shift; ".
208 ($schema->get_table($link)->primary_key->fields)[0].
209 ", \$self->".$linkable{$table_name}{$link}->name.
210 "_".$rk_field." }\n\n";
212 #else there is more than one way to traverse it. ack!
213 #let's treat these types of link tables as a many-to-one (easier)
215 #NOTE: we need to rethink the link method name, as the cardinality
217 } elsif(scalar(@rk_fields) == 1){
218 foreach my $rk_field (@rk_fields){
219 push @{ $packages{ $table_pkg_name }{'has_many'} },
220 "sub " . $linkable{$table_name}{$link}->name .
221 "s { my \$self = shift; return \$self->" .
222 $linkable{$table_name}{$link}->name . "_" .
223 $rk_field . "(\@_) }\n\n";
225 } elsif(scalar(@lk_fields) == 1){
226 #these will be taken care of on the other end...
228 foreach my $rk_field (@rk_fields){
229 push @{ $packages{ $table_pkg_name }{'has_many'} },
230 "sub " . $linkable{$table_name}{$link}->name . "_" . $rk_field .
231 "s { my \$self = shift; return \$self->" .
232 $linkable{$table_name}{$link}->name . "_" .
233 $rk_field . "(\@_) }\n\n";
241 # Now build up text of package.
243 my $base_pkg = sprintf( 'Class::DBI%s', $from ? "::$from" : '' );
244 $create .= join("\n",
245 "package $main_pkg_name;\n",
248 "use base '$base_pkg';\n",
249 "$main_pkg_name->set_db('Main', '$dsn', '$db_user', '$db_pass');\n\n",
253 sort { $packages{ $a }{'order'} <=> $packages{ $b }{'order'} }
256 my $pkg = $packages{ $pkg_name };
258 $create .= join("\n",
260 "package ".$pkg->{'pkg_name'}.";",
261 "use base '".$pkg->{'base'}."';",
262 "use Class::DBI::Pager;\n\n",
267 $pkg->{'pkg_name'}."->set_up_table('".$pkg->{'table'}."');\n\n";
270 my $table = $schema->get_table( $pkg->{'table'} );
271 my @field_names = map { $_->name } $table->get_fields;
273 $create .= join("\n",
274 $pkg_name."->table('".$pkg->{'table'}."');\n",
275 $pkg_name."->columns(All => qw/".
276 join(' ', @field_names)."/);\n\n",
280 if ( my $pk = $pkg->{'pk_accessor'} ) {
284 if ( my @has_a = @{ $pkg->{'has_a'} || [] } ) {
285 $create .= $_ for @has_a;
288 if ( my @has_many = @{ $pkg->{'has_many'} || [] } ) {
289 $create .= $_ for @has_many;
300 # -------------------------------------------------------------------
306 SQL::Translator::Producer::ClassDBI - create Class::DBI classes from schema
310 Use this producer as you would any other from SQL::Translator. See
311 L<SQL::Translator> for details.
313 This package utilizes SQL::Translator's formatting methods
314 format_package_name(), format_pk_name(), format_fk_name(), and
315 format_table_name() as it creates classes, one per table in the schema
316 provided. An additional base class is also created for database connectivity
317 configuration. See L<Class::DBI> for details on how this works.
321 Allen Day E<lt>allenday@ucla.eduE<gt>
322 Ying Zhang E<lt>zyolive@yahoo.comE<gt>,
323 Ken Y. Clark E<lt>kclark@cpan.org<gt>.