line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
|
|
|
|
|
|
=head1 FURTHER QUESTIONS? |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>. |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE> |
26
|
|
|
|
|
|
|
by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can |
27
|
|
|
|
|
|
|
redistribute it and/or modify it under the same terms as the |
28
|
|
|
|
|
|
|
L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>. |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=cut |
31
|
|
|
|
|
|
|
|
32
|
2
|
|
|
2
|
|
1545
|
use strict; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
136
|
|
33
|
|
|
|
|
|
|
our ($VERSION, $DEBUG, $WARN); |
34
|
|
|
|
|
|
|
$VERSION = '0.1'; |
35
|
|
|
|
|
|
|
$DEBUG = 0 unless defined $DEBUG; |
36
|
|
|
|
|
|
|
|
37
|
2
|
|
|
2
|
|
879
|
use SQL::Translator::Schema::Constants; |
|
2
|
|
|
|
|
1345
|
|
|
2
|
|
|
|
|
130
|
|
38
|
2
|
|
|
2
|
|
14
|
use SQL::Translator::Utils qw(header_comment); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
65
|
|
39
|
2
|
|
|
2
|
|
10
|
use Data::Dumper (); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
1358
|
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
## Skip all column type translation, as we want to use whatever the parser got. |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
## Translate parsers -> PK::Auto::Foo, however |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
my %parser2PK = ( |
46
|
|
|
|
|
|
|
MySQL => 'PK::Auto::MySQL', |
47
|
|
|
|
|
|
|
PostgreSQL => 'PK::Auto::Pg', |
48
|
|
|
|
|
|
|
DB2 => 'PK::Auto::DB2', |
49
|
|
|
|
|
|
|
Oracle => 'PK::Auto::Oracle', |
50
|
|
|
|
|
|
|
); |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub produce |
53
|
|
|
|
|
|
|
{ |
54
|
0
|
|
|
0
|
0
|
|
my ($translator) = @_; |
55
|
0
|
|
|
|
|
|
$DEBUG = $translator->debug; |
56
|
0
|
|
|
|
|
|
$WARN = $translator->show_warnings; |
57
|
0
|
|
|
|
|
|
my $no_comments = $translator->no_comments; |
58
|
0
|
|
|
|
|
|
my $add_drop_table = $translator->add_drop_table; |
59
|
0
|
|
|
|
|
|
my $schema = $translator->schema; |
60
|
0
|
|
|
|
|
|
my $output = ''; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# Steal the XML producers "prefix" arg for our namespace? |
63
|
|
|
|
|
|
|
my $dbixschema = $translator->producer_args()->{prefix} || |
64
|
0
|
|
0
|
|
|
|
$schema->name || 'My::Schema'; |
65
|
0
|
|
0
|
|
|
|
my $pkclass = $parser2PK{$translator->parser_type} || ''; |
66
|
|
|
|
|
|
|
|
67
|
0
|
|
|
|
|
|
my %tt_vars = (); |
68
|
0
|
|
|
|
|
|
$tt_vars{dbixschema} = $dbixschema; |
69
|
0
|
|
|
|
|
|
$tt_vars{pkclass} = $pkclass; |
70
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
|
my $schemaoutput .= << "DATA"; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
package ${dbixschema}; |
74
|
|
|
|
|
|
|
use base 'DBIx::Class::Schema'; |
75
|
|
|
|
|
|
|
use strict; |
76
|
|
|
|
|
|
|
use warnings; |
77
|
|
|
|
|
|
|
DATA |
78
|
|
|
|
|
|
|
|
79
|
0
|
|
|
|
|
|
my %tableoutput = (); |
80
|
0
|
|
|
|
|
|
my %tableextras = (); |
81
|
0
|
|
|
|
|
|
foreach my $table ($schema->get_tables) |
82
|
|
|
|
|
|
|
{ |
83
|
0
|
|
|
|
|
|
my $tname = $table->name; |
84
|
0
|
|
|
|
|
|
my $output .= qq{ |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
package ${dbixschema}::${tname}; |
87
|
|
|
|
|
|
|
use base 'DBIx::Class'; |
88
|
|
|
|
|
|
|
use strict; |
89
|
|
|
|
|
|
|
use warnings; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
__PACKAGE__->load_components(qw/${pkclass} Core/); |
92
|
|
|
|
|
|
|
__PACKAGE__->table('${tname}'); |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
}; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
my @fields = map |
97
|
0
|
|
|
|
|
|
{ { $_->name => { |
|
0
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
name => $_->name, |
99
|
|
|
|
|
|
|
is_auto_increment => $_->is_auto_increment, |
100
|
|
|
|
|
|
|
is_foreign_key => $_->is_foreign_key, |
101
|
|
|
|
|
|
|
is_nullable => $_->is_nullable, |
102
|
|
|
|
|
|
|
default_value => $_->default_value, |
103
|
|
|
|
|
|
|
data_type => $_->data_type, |
104
|
|
|
|
|
|
|
size => $_->size, |
105
|
|
|
|
|
|
|
} } |
106
|
|
|
|
|
|
|
} ($table->get_fields); |
107
|
|
|
|
|
|
|
|
108
|
0
|
|
|
|
|
|
$output .= "\n__PACKAGE__->add_columns("; |
109
|
0
|
|
|
|
|
|
foreach my $f (@fields) |
110
|
|
|
|
|
|
|
{ |
111
|
0
|
|
|
|
|
|
local $Data::Dumper::Terse = 1; |
112
|
0
|
|
|
|
|
|
$output .= "\n '" . (keys %$f)[0] . "' => " ; |
113
|
0
|
|
|
|
|
|
my $colinfo = |
114
|
|
|
|
|
|
|
Data::Dumper->Dump([values %$f], |
115
|
|
|
|
|
|
|
[''] # keys %$f] |
116
|
|
|
|
|
|
|
); |
117
|
0
|
|
|
|
|
|
chomp($colinfo); |
118
|
0
|
|
|
|
|
|
$output .= $colinfo . ","; |
119
|
|
|
|
|
|
|
} |
120
|
0
|
|
|
|
|
|
$output .= "\n);\n"; |
121
|
|
|
|
|
|
|
|
122
|
0
|
|
|
|
|
|
my $pk = $table->primary_key; |
123
|
0
|
0
|
|
|
|
|
if($pk) |
124
|
|
|
|
|
|
|
{ |
125
|
0
|
|
|
|
|
|
my @pk = map { $_->name } ($pk->fields); |
|
0
|
|
|
|
|
|
|
126
|
0
|
|
|
|
|
|
$output .= "__PACKAGE__->set_primary_key("; |
127
|
0
|
|
|
|
|
|
$output .= "'" . join("', '", @pk) . "');\n"; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
0
|
|
|
|
|
|
foreach my $cont ($table->get_constraints) |
131
|
|
|
|
|
|
|
{ |
132
|
|
|
|
|
|
|
# print Data::Dumper::Dumper($cont->type); |
133
|
0
|
0
|
|
|
|
|
if($cont->type =~ /foreign key/i) |
134
|
|
|
|
|
|
|
{ |
135
|
|
|
|
|
|
|
# $output .= "\n__PACKAGE__->belongs_to('" . |
136
|
|
|
|
|
|
|
# $cont->fields->[0]->name . "', '" . |
137
|
|
|
|
|
|
|
# "${dbixschema}::" . $cont->reference_table . "');\n"; |
138
|
|
|
|
|
|
|
|
139
|
0
|
|
|
|
|
|
$tableextras{$table->name} .= "\n__PACKAGE__->belongs_to('" . |
140
|
|
|
|
|
|
|
$cont->fields->[0]->name . "', '" . |
141
|
|
|
|
|
|
|
"${dbixschema}::" . $cont->reference_table . "');\n"; |
142
|
|
|
|
|
|
|
|
143
|
0
|
|
|
|
|
|
my $other = "\n__PACKAGE__->has_many('" . |
144
|
|
|
|
|
|
|
"get_" . $table->name. "', '" . |
145
|
|
|
|
|
|
|
"${dbixschema}::" . $table->name. "', '" . |
146
|
|
|
|
|
|
|
$cont->fields->[0]->name . "');"; |
147
|
0
|
|
|
|
|
|
$tableextras{$cont->reference_table} .= $other; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
0
|
|
|
|
|
|
$tableoutput{$table->name} .= $output; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
0
|
|
|
|
|
|
foreach my $to (keys %tableoutput) |
155
|
|
|
|
|
|
|
{ |
156
|
0
|
|
|
|
|
|
$output .= $tableoutput{$to}; |
157
|
0
|
|
|
|
|
|
$schemaoutput .= "\n__PACKAGE__->register_class('${to}', '${dbixschema}::${to}');\n"; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
0
|
|
|
|
|
|
foreach my $te (keys %tableextras) |
161
|
|
|
|
|
|
|
{ |
162
|
0
|
|
|
|
|
|
$output .= "\npackage ${dbixschema}::$te;\n"; |
163
|
0
|
|
|
|
|
|
$output .= $tableextras{$te} . "\n"; |
164
|
|
|
|
|
|
|
# $tableoutput{$te} .= $tableextras{$te} . "\n"; |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
# print "$output\n"; |
168
|
0
|
|
|
|
|
|
return "${output}\n\n${schemaoutput}\n1;\n"; |
169
|
|
|
|
|
|
|
} |