cfc8d926ff674cc163692938f0b5d1f5addd000f
[dbsrgits/DBIx-Class.git] / lib / SQL / Translator / Producer / DBIx / Class / File.pm
1 package SQL::Translator::Producer::DBIx::Class::File;
2
3 =head1 NAME
4
5 SQL::Translator::Producer::DBIx::Class::File - DBIx::Class file producer
6
7 =head1 SYNOPSIS
8
9   use SQL::Translator;
10
11   my $t = SQL::Translator->new( parser => '...', 
12                                 producer => 'DBIx::Class::File' );
13   print $translator->translate( $file );
14
15 =head1 DESCRIPTION
16
17 Creates a DBIx::Class::Schema for use with DBIx::Class
18
19 =cut
20
21 use strict;
22 use vars qw[ $VERSION $DEBUG $WARN ];
23 $VERSION = '0.1';
24 $DEBUG   = 0 unless defined $DEBUG;
25
26 use SQL::Translator::Schema::Constants;
27 use SQL::Translator::Utils qw(header_comment);
28
29 ## Skip all column type translation, as we want to use whatever the parser got.
30
31 ## Translate parsers -> PK::Auto::Foo, however
32
33 my %parser2PK = (
34                  MySQL       => 'PK::Auto::MySQL',
35                  PostgreSQL  => 'PK::Auto::Pg',
36                  DB2         => 'PK::Auto::DB2',
37                  Oracle      => 'PK::Auto::Oracle',
38                  );
39
40 sub produce
41 {
42     my ($translator) = @_;
43     $DEBUG             = $translator->debug;
44     $WARN              = $translator->show_warnings;
45     my $no_comments    = $translator->no_comments;
46     my $add_drop_table = $translator->add_drop_table;
47     my $schema         = $translator->schema;
48     my $output         = '';
49
50     # Steal the XML producers "prefix" arg for our namespace?
51     my $dbixschema     = $translator->producer_args()->{prefix} || 
52         $schema->name || 'My::Schema';
53     my $pkclass = $parser2PK{$translator->parser_type} || '';
54
55     my %tt_vars = ();
56     $tt_vars{dbixschema} = $dbixschema;
57     $tt_vars{pkclass} = $pkclass;
58
59     my $schemaoutput .= << "DATA";
60
61 package ${dbixschema};
62 use base 'DBIx::Class::Schema';
63 use strict;
64 use warnings;
65 DATA
66
67     my %tableoutput = ();
68     my %tableextras = ();
69     foreach my $table ($schema->get_tables)
70     {
71         my $tname = $table->name;
72         my $output .= qq{
73
74 package ${dbixschema}::${tname};
75 use base 'DBIx::Class';
76 use strict;
77 use warnings;
78
79 __PACKAGE__->load_components(qw/${pkclass} Core/);
80 __PACKAGE__->table('${tname}');
81
82 };
83
84         my @fields = map 
85         { { $_->name  => {
86             name              => $_->name,
87             is_auto_increment => $_->is_auto_increment,
88             is_foreign_key    => $_->is_foreign_key,
89             is_nullable       => $_->is_nullable,
90             default_value     => $_->default_value,
91             data_type         => $_->data_type,
92             size              => $_->size,
93         } }
94          } ($table->get_fields);
95
96         $output .= "\n__PACKAGE__->add_columns(";
97         foreach my $f (@fields)
98         {
99             local $Data::Dumper::Terse = 1;
100             $output .= "\n    '" . (keys %$f)[0] . "' => " ;
101             my $colinfo = 
102                 Data::Dumper->Dump([values %$f],
103                                    [''] # keys   %$f]
104                                    );
105             chomp($colinfo);
106             $output .= $colinfo . ",";
107         }
108         $output .= "\n);\n";
109
110         my $pk = $table->primary_key;
111         if($pk)
112         {
113             my @pk = map { $_->name } ($pk->fields);
114             $output .= "__PACKAGE__->set_primary_key(";
115             $output .= "'" . join("', '", @pk) . "');\n";
116         }
117
118         foreach my $cont ($table->get_constraints)
119         {
120 #            print Data::Dumper::Dumper($cont->type);
121             if($cont->type =~ /foreign key/i)
122             {
123 #                 $output .= "\n__PACKAGE__->belongs_to('" . 
124 #                     $cont->fields->[0]->name . "', '" .
125 #                     "${dbixschema}::" . $cont->reference_table . "');\n";
126
127                 $tableextras{$table->name} .= "\n__PACKAGE__->belongs_to('" . 
128                     $cont->fields->[0]->name . "', '" .
129                     "${dbixschema}::" . $cont->reference_table . "');\n";
130                 
131                 my $other = "\n__PACKAGE__->has_many('" .
132                     "get_" . $table->name. "', '" .
133                     "${dbixschema}::" . $table->name. "', '" .
134                     $cont->fields->[0]->name . "');";
135                 $tableextras{$cont->reference_table} .= $other;
136             }
137         }
138
139         $tableoutput{$table->name} .= $output;
140     }
141
142     foreach my $to (keys %tableoutput)
143     {
144         $output .= $tableoutput{$to};
145         $schemaoutput .= "\n__PACKAGE__->register_class('${to}', '${dbixschema}::${to}');\n"; 
146     }
147
148     foreach my $te (keys %tableextras)
149     {
150         $output .= "\npackage ${dbixschema}::$te;\n";
151         $output .= $tableextras{$te} . "\n";
152 #        $tableoutput{$te} .= $tableextras{$te} . "\n";
153     }
154
155 #    print "$output\n";
156     return "${output}\n\n${schemaoutput}\n1;\n";
157 }