asdf
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / ClassDBI.pm
1 package SQL::Translator::Producer::ClassDBI;
2
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>,
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.3 $ =~ /(\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   return $create;
108 }
109
110
111 sub _from {
112   my $from = shift;
113   my @temp = split(/::/, $from);
114   $from = $temp[$#temp];
115
116   if ( $from eq 'MySQL') {
117         $from = lc($from);
118   } elsif ( $from eq 'PostgreSQL') {
119         $from = 'Pg';
120   } elsif ( $from eq 'Oracle') {
121         $from = 'Oracle';
122   } else {
123         die "__PACKAGE__ can't handle vendor $from";
124   }
125
126   return $from;
127 }
128
129 1;
130
131 __END__
132
133 =head1 NAME
134
135 SQL::Translator::Producer::ClassDBI - Translate SQL schemata into Class::DBI classes
136
137 =head1 SYNOPSIS
138
139 Use this producer as you would any other from SQL::Translator.  See
140 L<SQL::Translator> for details.
141
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.
147
148 =head1 AUTHOR
149
150 Ying Zhang <zyolive@yahoo.com>, Allen Day <allenday@ucla.edu>