1 package SQL::Translator::Producer::ClassDBI;
3 # -------------------------------------------------------------------
4 # $Id: ClassDBI.pm,v 1.3 2003-04-19 22:58:12 allenday Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2003 Ying Zhang <zyolive@yahoo.com>,
7 # Allen Day <allenday@ucla.edu>,
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.3 $ =~ /(\d+)\.(\d+)/;
27 $DEBUG = 1 unless defined $DEBUG;
32 my ($translator, $data) = @_;
33 $DEBUG = $translator->debug;
34 my $no_comments = $translator->no_comments;
37 unless ( $no_comments ) {
38 $create .= sprintf "##\n## Created by %s\n## Created on %s\n##\n\n",
39 __PACKAGE__, scalar localtime;
42 $create .= "package " . $translator->format_package_name('DBI'). ";\n\n";
44 $create .= "my \$USER = \'\';\n";
45 $create .= "my \$PASS = \'\';\n\n";
47 my $from = _from($translator->parser_type());
49 $create .= "use base \'Class::DBI::" .$from. "\';\n\n";
51 $create .= $translator->format_package_name('DBI'). "->set_db(\'Main', \'dbi:" .$from. ":_\', \$USER,\$PASS,);\n\n";
52 $create .= "1;\n\n\n";
54 for my $table (keys %{$data}) {
55 my $table_data = $data->{$table};
56 my @fields = keys %{$table_data->{'fields'}};
59 $create .= "##\n## Package:" .$translator->format_package_name($table). "\n##\n" unless $no_comments;
60 $create .= "package ". $translator->format_package_name($table). ";\n";
62 $create .= "use base \'Chado::DBI\';\n";
63 $create .= "use mixin \'Class::DBI::Join\';\n";
64 $create .= "use Class::DBI::Pager;\n\n";
66 $create .= $translator->format_package_name($table). " -> set_up_table('$table');\n\n";
75 for my $constraint ( @{ $table_data->{'constraints'} } ) {
76 #my $name = $constraint->{'name'} || '';
77 my $type = $constraint->{'type'};
78 my $fields = $constraint->{'fields'};
79 my $ref_table = $constraint->{'reference_table'};
80 my $ref_fields = $constraint->{'reference_fields'};
82 if ( $type eq 'primary_key') {
83 $create .= "sub " .$translator->format_pk_name($translator->format_package_name($table), $fields[0]). "{ shift -> $fields[0] }\n\n";
91 for (my $i = 0; $i < scalar(@fields); $i++) {
92 my $field = $fields[$i];
93 my $field_data = $table_data->{'fields'}->{$field}->{'constraints'};
94 my $type = $field_data->[1]->{'type'} || '';
95 my $ref_table = $field_data->[1]->{'reference_table'};
96 my $ref_fields = $field_data->[1]->{'reference_fields'};
98 if ($type eq 'foreign_key') {
99 $create .= $translator->format_package_name($table). " -> hasa(" .$translator->format_package_name($ref_table). " => \'@$ref_fields\');\n";
100 $create .= "sub " .$translator->format_fk_name($ref_table, @$ref_fields). "{ return shift -> @$ref_fields }\n\n";
104 $create .= "1;\n\n\n";
113 my @temp = split(/::/, $from);
114 $from = $temp[$#temp];
116 if ( $from eq 'MySQL') {
118 } elsif ( $from eq 'PostgreSQL') {
120 } elsif ( $from eq 'Oracle') {
123 die "__PACKAGE__ can't handle vendor $from";
135 SQL::Translator::Producer::ClassDBI - Translate SQL schemata into Class::DBI classes
139 Use this producer as you would any other from SQL::Translator. See
140 L<SQL::Translator> for details.
142 This package utilizes SQL::Translator's formatting methods
143 format_package_name(), format_pk_name(), format_fk_name(), and
144 format_table_name() as it creates classes, one per table in the schema
145 provided. An additional base class is also created for database connectivity
146 configuration. See L<Class::DBI> for details on how this works.
150 Ying Zhang <zyolive@yahoo.com>, Allen Day <allenday@ucla.edu>