Commit | Line | Data |
c990f24a |
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 | package ${dbixschema}; |
61 | use base 'DBIx::Class::Schema'; |
62 | DATA |
63 | |
64 | my %tableoutput = (); |
65 | my %tableextras = (); |
66 | foreach my $table ($schema->get_tables) |
67 | { |
68 | my $tname = $table->name; |
69 | my $output .= qq{ |
70 | package ${dbixschema}::${tname}; |
71 | use base 'DBIx::Class'; |
72 | |
73 | __PACKAGE__->load_components(qw/${pkclass} Core/); |
74 | __PACKAGE__->table('${tname}'); |
75 | |
76 | }; |
77 | |
78 | my @fields = map |
79 | { { $_->name => { |
80 | name => $_->name, |
81 | is_auto_increment => $_->is_auto_increment, |
82 | is_foreign_key => $_->is_foreign_key, |
83 | is_nullable => $_->is_nullable, |
84 | default_value => $_->default_value, |
85 | data_type => $_->data_type, |
86 | size => $_->size, |
87 | } } |
88 | } ($table->get_fields); |
89 | |
90 | $output .= "\n__PACKAGE__->add_columns("; |
91 | foreach my $f (@fields) |
92 | { |
93 | local $Data::Dumper::Terse = 1; |
94 | $output .= "\n '" . (keys %$f)[0] . "' => " ; |
95 | my $colinfo = |
96 | Data::Dumper->Dump([values %$f], |
97 | [''] # keys %$f] |
98 | ); |
99 | chomp($colinfo); |
100 | $output .= $colinfo . ","; |
101 | } |
102 | $output .= "\n);\n"; |
103 | |
104 | my $pk = $table->primary_key; |
105 | if($pk) |
106 | { |
107 | my @pk = map { $_->name } ($pk->fields); |
108 | $output .= "__PACKAGE__->set_primary_key("; |
109 | $output .= "'" . join("', '", @pk) . "');"; |
110 | } |
111 | |
112 | foreach my $cont ($table->get_constraints) |
113 | { |
114 | # print Data::Dumper::Dumper($cont->type); |
115 | if($cont->type =~ /foreign key/i) |
116 | { |
117 | $output .= "\n__PACKAGE__->belongs_to('" . |
118 | $cont->fields->[0]->name . "', '" . |
119 | "${dbixschema}::" . $cont->reference_table . "');\n"; |
120 | |
121 | my $other = "\n__PACKAGE__->has_many('" . |
122 | "get_" . $table->name. "', '" . |
123 | "${dbixschema}::" . $table->name. "', '" . |
124 | $cont->fields->[0]->name . "');"; |
125 | $tableextras{$cont->reference_table} .= $other; |
126 | } |
127 | } |
128 | |
129 | $tableoutput{$table->name} = $output; |
130 | } |
131 | |
132 | foreach my $te (keys %tableextras) |
133 | { |
134 | $tableoutput{$te} .= $tableextras{$te} . "\n"; |
135 | } |
136 | |
137 | |
138 | foreach my $to (keys %tableoutput) |
139 | { |
140 | $output .= $tableoutput{$to}; |
141 | } |
142 | |
143 | print "$output\n"; |
144 | return $output; |
145 | } |