Fixed error for: "Use of uninitialized value in string eq at
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / ClassDBI.pm
1 package SQL::Translator::Producer::ClassDBI;
2
3 # -------------------------------------------------------------------
4 # $Id: ClassDBI.pm,v 1.2 2003-04-19 01:32:27 kycl4rk Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2003 Ying Zhang <zyolive@yahoo.com>,
7 #                    Allen Day <allenday@ucla.edu>,
8 #
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.
12 #
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.
17 #
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
21 # 02111-1307  USA
22 # -------------------------------------------------------------------
23
24 use strict;
25 use vars qw[ $VERSION $DEBUG ];
26 $VERSION = sprintf "%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/;
27 $DEBUG   = 1 unless defined $DEBUG;
28
29 use Data::Dumper;
30
31 sub produce {
32   my ($translator, $data) = @_;
33   $DEBUG                  = $translator->debug;
34   my $no_comments         = $translator->no_comments;
35
36   my $create; 
37   unless ( $no_comments ) {
38         $create .= sprintf "##\n## Created by %s\n## Created on %s\n##\n\n",
39           __PACKAGE__, scalar localtime;
40   }
41
42   $create .= "package " . $translator->format_package_name('DBI'). ";\n\n";
43
44   $create .= "my \$USER = \'\';\n";
45   $create .= "my \$PASS = \'\';\n\n";
46
47   my $from = _from($translator->parser_type());
48
49   $create .= "use base \'Class::DBI::" .$from. "\';\n\n";
50
51   $create .= $translator->format_package_name('DBI'). "->set_db(\'Main', \'dbi:" .$from. ":_\', \$USER,\$PASS,);\n\n";
52   $create .= "1;\n\n\n";
53
54   for my $table (keys %{$data}) {
55         my $table_data = $data->{$table};
56         my @fields =  keys %{$table_data->{'fields'}};
57
58
59         $create .= "##\n## Package:" .$translator->format_package_name($table). "\n##\n" unless $no_comments;
60         $create .= "package ". $translator->format_package_name($table). ";\n";
61
62         $create .= "use base \'Chado::DBI\';\n";
63         $create .= "use mixin \'Class::DBI::Join\';\n";
64         $create .= "use Class::DBI::Pager;\n\n";
65
66         $create .= $translator->format_package_name($table). " -> set_up_table('$table');\n\n";
67
68         
69
70         #
71         # Primary key?
72         #
73         my @constraints;
74         
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'};
81
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";
84           }
85                         
86         }
87
88         #
89         # Foreign key?
90         #
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'};
97                         
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";
101           }
102         }
103         
104         $create .= "1;\n\n\n";
105   }
106   
107   open( FILE, '>DBI.pm') or die( "Can't open file : $!");
108   print ( FILE $create);
109   close ( FILE ) or die( "Can't close file : $!");                                                         
110
111 }
112
113
114 sub _from {
115   my $from = shift;
116   my @temp = split(/::/, $from);
117   $from = $temp[$#temp];
118
119   if ( $from eq 'MySQL') {
120         $from = lc($from);
121   } elsif ( $from eq 'PostgreSQL') {
122         $from = 'Pg';
123   } elsif ( $from eq 'Oracle') {
124
125   } else {
126         print "Eoorr\n";
127   }
128
129   return $from;
130 }
131
132 1;
133
134 __END__
135
136 =head1 NAME
137
138 SQL::Translator::Producer::ClassDBI - Translate SQL schemata into Class::DBI classes
139
140 =head1 SYNOPSIS
141
142 Use this producer as you would any other from SQL::Translator.  See
143 L<SQL::Translator> for details.
144
145 This package utilizes SQL::Translator's formatting methods
146 format_package_name(), format_pk_name(), format_fk_name(), and
147 format_table_name() as it creates classes, one per table in the schema
148 provided.  An additional base class is also created for database connectivity
149 configuration.  See L<Class::DBI> for details on how this works.
150
151 =head1 AUTHOR
152
153 Ying Zhang <zyolive@yahoo.com>, Allen Day <allenday@ucla.edu>