blib/lib/SQL/Translator.pm

filter: Statementstmt 92.6% 289 / 312 Branchbran 75.3% 116 / 154 Conditioncond 40.2% 33 / 82 MC/DCmcdc 21.7% 10 / 46 Subroutinesub 97.9% 47 / 48 Podpod 30.7% 4 / 13 totaltotal 76.1% 499 / 655 CC 111 SCAR 30.9
CC
111
Cov
80%
translate
75.6
load
10.1
_tool
10.1
CRAP 22.0
« blib/lib/SQL/Translator/Parser/JSON.pm ↑ Summary blib/lib/SQL/Translator/Producer/MySQL.pm »
1 package SQL::Translator;
2  
3 71 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 71 our ($DEFAULT_SUB, $DEBUG, $ERROR);
5  
6 71 our $VERSION = '1.66';
7 71 $VERSION =~ tr/_//d;
8 71 ▶$DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $SQL::Translator::DEBUG 71 0 71
9 71 $ERROR = "";
10  
11 71 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 71 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 71 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 71 ▶use File::Spec::Functions qw(catfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 71 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 71 ▶use IO::Dir;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 71 ▶use Sub::Quote qw(quote_sub);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 71 ▶use SQL::Translator::Producer;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 71 ▶use SQL::Translator::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 71 ▶use SQL::Translator::Utils qw(throw ex2err carp_ro normalize_quote_options);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 49 ▶$DEFAULT_SUB = sub { $_[0]->schema }
Subroutine __ANON__: called CC=1 SCAR=0.0
24 71 ▶ unless defined $DEFAULT_SUB;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $SQL::Translator::DEFAULT_SUB 71 0 71
25  
26 71 with qw(
27 SQL::Translator::Role::Debug
28 SQL::Translator::Role::Error
29 SQL::Translator::Role::BuildArgs
30 );
31  
32 141 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=8 SCAR=22.9
33 141 my $orig = shift;
34 141 my $self = shift;
35 141 my $config = $self->$orig(@_);
36  
37 # If a 'parser' or 'from' parameter is passed in, use that as the
38 # parser; if a 'producer' or 'to' parameter is passed in, use that
39 # as the producer; both default to $DEFAULT_SUB.
40 141 ▶ $config->{parser} ||= $config->{from} if defined $config->{from};
Branch: 1 branch
BranchTrueFalseTotal
if defined $config->{"from"} 27 114 141
Condition33%1/3
$config->{"parser"} ||= $config->{"from"}
l!l&&r!l&&!r
0270
MC/DC0%0/2
$config->{"parser"} ||= $config->{"from"}
$config->{"parser"} $config->{"from"}
Truth table33%1/3
A ||= B
A= $config->{"parser"}B= $config->{"from"}
ABresult
1X1
011
000
41 141 ▶ $config->{producer} ||= $config->{to} if defined $config->{to};
Branch: 1 branch
BranchTrueFalseTotal
if defined $config->{"to"} 18 123 141
Condition33%1/3
$config->{"producer"} ||= $config->{"to"}
l!l&&r!l&&!r
0180
MC/DC0%0/2
$config->{"producer"} ||= $config->{"to"}
$config->{"producer"} $config->{"to"}
Truth table33%1/3
A ||= B
A= $config->{"producer"}B= $config->{"to"}
ABresult
1X1
011
000
42  
43 141 ▶ $config->{filename} ||= $config->{file} if defined $config->{file};
Branch: 1 branch
BranchTrueFalseTotal
if defined $config->{"file"} 0 141 141
Condition0%0/3
$config->{"filename"} ||= $config->{"file"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$config->{"filename"} ||= $config->{"file"}
$config->{"filename"} $config->{"file"}
Truth table0%0/3
A ||= B
A= $config->{"filename"}B= $config->{"file"}
ABresult
1X1
011
000
44  
45 141 my $quote = normalize_quote_options($config);
46 140 ▶ $config->{quote_identifiers} = $quote if defined $quote;
Branch: 1 branch
BranchTrueFalseTotal
if defined $quote 17 123 140
47  
48 140 return $config;
49 71 };
50  
51 140 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
52 140 my ($self) = @_;
53  
54 # Make sure all the tool-related stuff is set up
55 140 foreach my $tool (qw(producer parser)) {
56 280 $self->$tool($self->$tool);
57 }
58 }
59  
60 has $_ => (
61 is => 'rw',
62 default => quote_sub(q{ 0 }),
63 coerce => quote_sub(q{ $_[0] ? 1 : 0 }),
64 71 ) foreach qw(add_drop_table no_comments show_warnings trace validate);
65  
66 # quote_identifiers is on by default, use a 0-but-true as indicator
67 # so we can allow individual producers to change the default
68 71 has quote_identifiers => (
69 is => 'rw',
70 default => quote_sub(q{ '0E0' }),
71 coerce => quote_sub(q{ $_[0] || 0 }),
72 );
73  
74 11 ▶sub quote_table_names {
Subroutine quote_table_names: called CC=4 SCAR=22.5
Pod documented
75 11 ▶ (@_ > 1 and ($_[1] xor $_[0]->quote_identifiers))
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 && ($_[1] ^^ $_[0]->quote_identifiers) ? : 0 11 11
Condition14%1/7
@_ > 1 and $_[1] ^^ $_[0]->quote_identifiers
!ll&&!rl&&r
1100
$_[1] xor $_[0]->quote_identifiers
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/3
@_ > 1 and $_[1] ^^ $_[0]->quote_identifiers
@_ > 1 $_[1] $_[0]->quote_identifiers
Truth table20%1/5
A and B xor C
A= @_ > 1B= $_[1]C= $_[0]->quote_identifiers
ABCresult
0XX0
1110
1000
1101
1011
76 ? croak 'Using quote_table_names as a setter is no longer supported'
77 : $_[0]->quote_identifiers;
78 }
79  
80 11 ▶sub quote_field_names {
Subroutine quote_field_names: called CC=4 SCAR=22.5
Pod documented
81 11 ▶ (@_ > 1 and ($_[1] xor $_[0]->quote_identifiers))
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 && ($_[1] ^^ $_[0]->quote_identifiers) ? : 0 11 11
Condition14%1/7
@_ > 1 and $_[1] ^^ $_[0]->quote_identifiers
!ll&&!rl&&r
1100
$_[1] xor $_[0]->quote_identifiers
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/3
@_ > 1 and $_[1] ^^ $_[0]->quote_identifiers
@_ > 1 $_[1] $_[0]->quote_identifiers
Truth table20%1/5
A and B xor C
A= @_ > 1B= $_[1]C= $_[0]->quote_identifiers
ABCresult
0XX0
1110
1000
1101
1011
82 ? croak 'Using quote_field_names as a setter is no longer supported'
83 : $_[0]->quote_identifiers;
84 }
85  
86 87 ▶after quote_identifiers => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
87 87 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 15 72 87
88  
89 # synchronize for old code reaching directly into guts
90 15 ▶ $_[0]->{quote_table_names} = $_[0]->{quote_field_names} = $_[1] ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 14 1 15
91 }
92 71 };
93  
94 118 ▶has producer => (is => 'rw', default => sub {$DEFAULT_SUB});
Subroutine __ANON__: called CC=1 SCAR=0.0
95  
96 440 ▶around producer => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
97 440 my $orig = shift;
98 shift->_tool(
99 {
100 440 orig => $orig,
101 name => 'producer',
102 path => "SQL::Translator::Producer",
103 default_sub => "produce",
104 },
105 @_
106 );
107 71 };
108  
109 71 has producer_type => (is => 'rwp', init_arg => undef);
110  
111 71 around producer_type => carp_ro('producer_type');
112  
113 71 has producer_args => (is => 'rw', default => quote_sub(q{ +{} }));
114  
115 104 ▶around producer_args => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
116 104 my $orig = shift;
117 104 shift->_args($orig, @_);
118 71 };
119  
120 105 ▶has parser => (is => 'rw', default => sub {$DEFAULT_SUB});
Subroutine __ANON__: called CC=1 SCAR=0.0
121  
122 465 ▶around parser => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
123 465 my $orig = shift;
124 shift->_tool(
125 {
126 465 orig => $orig,
127 name => 'parser',
128 path => "SQL::Translator::Parser",
129 default_sub => "parse",
130 },
131 @_
132 );
133 71 };
134  
135 71 has parser_type => (is => 'rwp', init_arg => undef);
136  
137 71 around parser_type => carp_ro('parser_type');
138  
139 71 has parser_args => (is => 'rw', default => quote_sub(q{ +{} }));
140  
141 108 ▶around parser_args => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
142 108 my $orig = shift;
143 108 shift->_args($orig, @_);
144 71 };
145  
146 has filters => (
147 is => 'rw',
148 default => quote_sub(q{ [] }),
149 140 ▶ coerce => sub {
Subroutine __ANON__: called CC=7 SCAR=19.9
150 140 my @filters;
151  
152 # Set. Convert args to list of [\&code,@args]
153 140 ▶ foreach (@{ $_[0] || [] }) {
Condition50%1/2
$_[0] || []
l!l
1400
MC/DC0%0/1
$_[0] || []
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
154 10 ▶ my ($filt, @args) = ref($_) eq "ARRAY" ? @$_ : $_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 5 5 10
155 10 ▶ if (isa($filt, "CODE")) {
Branch: 1 branch
BranchTrueFalseTotal
if (&SQL::Translator::isa($filt, 'CODE')) { } 5 5 10
156 5 push @filters, [ $filt, @args ];
157 5 next;
158 } else {
159 5 ▶ __PACKAGE__->debug("Adding $filt filter. Args:" . Dumper(\@args) . "\n")
Branch: 1 branch
BranchTrueFalseTotal
if __PACKAGE__->debugging 0 5 5
160 if __PACKAGE__->debugging;
161 5 ▶ $filt = _load_sub("$filt\::filter", "SQL::Translator::Filter")
Condition33%1/3
_load_sub("${filt}::filter", 'SQL::Translator::Filter') || throw(__PACKAGE__->error)
l!l&&r!l&&!r
500
MC/DC0%0/2
_load_sub("${filt}::filter", 'SQL::Translator::Filter') || throw(__PACKAGE__->error)
_load_sub("${filt}::filter", 'SQL::Translator::Filter') throw(__PACKAGE__->error)
Truth table33%1/3
A || B
A= _load_sub("${filt}::filter", 'SQL::Translator::Filter')B= throw(__PACKAGE__->error)
ABresult
1X1
011
000
162 || throw(__PACKAGE__->error);
163 5 push @filters, [ $filt, @args ];
164 }
165 }
166 140 return \@filters;
167 },
168 71 );
169  
170 115 ▶around filters => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
171 115 my $orig = shift;
172 115 my $self = shift;
173 0 ▶ return @{ $self->$orig([ @{ $self->$orig }, @_ ]) } if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 115 115
174 115 return @{ $self->$orig };
175 71 };
176  
177 has filename => (
178 is => 'rw',
179 60 ▶ isa => sub {
Subroutine __ANON__: called CC=6 SCAR=18.5
180 60 ▶ foreach my $filename (ref($_[0]) eq 'ARRAY' ? @{ $_[0] } : $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 1 59 60
181 60 ▶ if (-d $filename) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $filename) { } 0 60 60
elsif (not -f _ && -r _) { } 4 56 60
182 0 throw("Cannot use directory '$filename' as input source");
183 ▶ } elsif (not -f _ && -r _ ) {
Condition66%2/3
-f _ && -r _
!ll&&!rl&&r
4056
MC/DC50%1/2
-f _ && -r _
-f _ -r _
Truth table66%2/3
A && B
A= -f _B= -r _
ABresult
0X0
100
111
184 4 throw("Cannot use '$filename' as input source: " . "file does not exist or is not readable.");
185 }
186 }
187 },
188 71 );
189  
190 71 around filename => \&ex2err;
191  
192 has data => (
193 is => 'rw',
194 builder => 1,
195 lazy => 1,
196 113 ▶ coerce => sub {
Subroutine __ANON__: called CC=5 SCAR=21.7
197  
198 # Set $self->data based on what was passed in. We will
199 # accept a number of things; do our best to get it right.
200 113 my $data = shift;
201 113 ▶ if (isa($data, 'ARRAY')) {
Branch: 2 branches
BranchTrueFalseTotal
if (&SQL::Translator::isa($data, 'ARRAY')) { } 0 113 113
elsif (&SQL::Translator::isa($data, 'GLOB')) { } 0 113 113
202 0 $data = join '', @$data;
203 } elsif (isa($data, 'GLOB')) {
204 0 ▶ seek($data, 0, 0) if eof($data);
Branch: 1 branch
BranchTrueFalseTotal
if eof $data 0 0 0
205 0 local $/;
206 0 $data = <$data>;
207 }
208 113 ▶ return isa($data, 'SCALAR') ? $data : \$data;
Branch: 1 branch
BranchTrueFalseTotal
&SQL::Translator::isa($data, 'SCALAR') ? : 67 46 113
209 },
210 71 );
211  
212 170 ▶around data => sub {
Subroutine __ANON__: called CC=4 SCAR=15.0
213 170 my $orig = shift;
214 170 my $self = shift;
215  
216 170 ▶ if (@_ > 1 && !ref $_[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ > 1 and not ref $_[0]) { } 0 170 170
elsif (@_) { } 53 117 170
Condition33%1/3
@_ > 1 and not ref $_[0]
!ll&&!rl&&r
17000
MC/DC0%0/2
@_ > 1 and not ref $_[0]
@_ > 1 not ref $_[0]
Truth table33%1/3
A and not(B)
A= @_ > 1B= not ref $_[0]
ABresult
0X0
100
111
217 0 return $self->$orig(\join('', @_));
218 } elsif (@_) {
219 53 return $self->$orig(@_);
220 }
221 117 return ex2err($orig, $self);
222 71 };
223  
224 54 ▶sub _build_data {
Subroutine _build_data: called CC=6 SCAR=18.2
225 54 my $self = shift;
226  
227 # If we have a filename but no data yet, populate.
228 54 ▶ if (my $filename = $self->filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $filename = $self->filename) 50 4 54
229 50 $self->debug("Opening '$filename' to get contents.\n");
230 50 local $/;
231 50 my $data;
232  
233 50 ▶ my @files = ref($filename) eq 'ARRAY' ? @$filename : ($filename);
Branch: 1 branch
BranchTrueFalseTotal
ref $filename eq 'ARRAY' ? : 0 50 50
234  
235 50 foreach my $file (@files) {
236 50 ▶ open my $fh, '<', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or throw("Can't read file '${file}': $!") 50 0 50
237 or throw("Can't read file '$file': $!");
238  
239 50 $data .= <$fh>;
240  
241 50 ▶ close $fh or throw("Can't close file '$file': $!");
Branch: 1 branch
BranchTrueFalseTotal
close $fh or throw("Can't close file '${file}': $!") 50 0 50
242 }
243  
244 50 return \$data;
245 }
246 }
247  
248 71 has schema => (
249 is => 'lazy',
250 init_arg => undef,
251 clearer => 'reset',
252 predicate => '_has_schema',
253 );
254  
255 71 around schema => carp_ro('schema');
256  
257 1 ▶around reset => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
258 1 my $orig = shift;
259 1 my $self = shift;
260 1 $self->$orig(@_);
261 1 return 1;
262 71 };
263  
264 123 ▶sub _build_schema { SQL::Translator::Schema->new(translator => shift) }
Subroutine _build_schema: called CC=1 SCAR=0.0
265  
266 117 ▶sub translate {
Subroutine translate: called CC=37 SCAR=43.3
Pod documented
267 117 my $self = shift;
268 117 my ($args, $parser, $parser_type, $producer, $producer_type);
269 117 my ($parser_output, $producer_output, @producer_output);
270  
271 # Parse arguments
272 117 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 50 67 117
273  
274 # Passed a reference to a hash?
275 50 ▶ if (isa($_[0], 'HASH')) {
Branch: 4 branches
BranchTrueFalseTotal
if (&SQL::Translator::isa($_[0], 'HASH')) { } 0 50 50
elsif (&SQL::Translator::isa($_[0], 'GLOB')) { } 0 50 50
elsif (&SQL::Translator::isa($_[0], 'SCALAR')) { } 17 33 50
elsif (not ref $_[0]) { } 33 0 33
276  
277 # yep, a hashref
278 0 $self->debug("translate: Got a hashref\n");
279 0 $args = $_[0];
280 }
281  
282 # Passed a GLOB reference, i.e., filehandle
283 elsif (isa($_[0], 'GLOB')) {
284 0 $self->debug("translate: Got a GLOB reference\n");
285 0 $self->data($_[0]);
286 }
287  
288 # Passed a reference to a string containing the data
289 elsif (isa($_[0], 'SCALAR')) {
290  
291 # passed a ref to a string
292 17 $self->debug("translate: Got a SCALAR reference (string)\n");
293 17 $self->data($_[0]);
294 }
295  
296 # Not a reference; treat it as a filename
297 elsif (!ref $_[0]) {
298  
299 # Not a ref, it's a filename
300 33 $self->debug("translate: Got a filename\n");
301 33 $self->filename($_[0]);
302 }
303  
304 # Passed something else entirely.
305 else {
306 # We're not impressed. Take your empty string and leave.
307 # return "";
308  
309 # Actually, if data, parser, and producer are set, then we
310 # can continue. Too bad, because I like my comment
311 # (above)...
312 0 ▶ return ""
Branch: 1 branch
BranchTrueFalseTotal
unless $self->data and $self->producer and $self->parser 0 0 0
Condition0%0/6
$self->data and $self->producer and $self->parser
!ll&&!rl&&r
000
$self->data and $self->producer
!ll&&!rl&&r
000
MC/DC0%0/3
$self->data and $self->producer and $self->parser
$self->data $self->producer $self->parser
Truth table0%0/4
A and B and C
A= $self->dataB= $self->producerC= $self->parser
ABCresult
0XX0
10X0
1100
1111
313 unless ($self->data
314 && $self->producer
315 && $self->parser);
316 }
317 } else {
318 # You must pass in a hash, or you get nothing.
319 67 ▶ return "" if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 67 67
320 67 $args = {@_};
321 }
322  
323 # ----------------------------------------------------------------------
324 # Can specify the data to be transformed using "filename", "file",
325 # "data", or "datasource".
326 # ----------------------------------------------------------------------
327 117 ▶ if (my $filename = ($args->{'filename'} || $args->{'file'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $filename = $args->{"filename"} || $args->{"file"}) 20 97 117
Condition100%3/3
$args->{'filename'} || $args->{'file'}
l!l&&r!l&&!r
17397
MC/DC100%2/2
$args->{'filename'} || $args->{'file'}
$args->{'filename'} $args->{'file'}
Truth table100%3/3
A || B
A= $args->{'filename'}B= $args->{'file'}
ABresult
1X1
011
000
328 20 $self->filename($filename);
329 }
330  
331 117 ▶ if (my $data = ($args->{'data'} || $args->{'datasource'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = $args->{"data"} || $args->{"datasource"}) 36 81 117
Condition66%2/3
$args->{'data'} || $args->{'datasource'}
l!l&&r!l&&!r
36081
MC/DC50%1/2
$args->{'data'} || $args->{'datasource'}
$args->{'data'} $args->{'datasource'}
Truth table66%2/3
A || B
A= $args->{'data'}B= $args->{'datasource'}
ABresult
1X1
011
000
332 36 $self->data($data);
333 }
334  
335 # ----------------------------------------------------------------
336 # Get the data.
337 # ----------------------------------------------------------------
338 117 my $data = $self->data;
339  
340 # ----------------------------------------------------------------
341 # Local reference to the parser subroutine
342 # ----------------------------------------------------------------
343 117 ▶ if ($parser = ($args->{'parser'} || $args->{'from'})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parser = $args->{"parser"} || $args->{"from"}) 33 84 117
Condition100%3/3
$args->{'parser'} || $args->{'from'}
l!l&&r!l&&!r
161784
MC/DC100%2/2
$args->{'parser'} || $args->{'from'}
$args->{'parser'} $args->{'from'}
Truth table100%3/3
A || B
A= $args->{'parser'}B= $args->{'from'}
ABresult
1X1
011
000
344 33 $self->parser($parser);
345 }
346 117 $parser = $self->parser;
347 117 $parser_type = $self->parser_type;
348  
349 # ----------------------------------------------------------------
350 # Local reference to the producer subroutine
351 # ----------------------------------------------------------------
352 117 ▶ if ($producer = ($args->{'producer'} || $args->{'to'})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($producer = $args->{"producer"} || $args->{"to"}) 42 75 117
Condition100%3/3
$args->{'producer'} || $args->{'to'}
l!l&&r!l&&!r
251775
MC/DC100%2/2
$args->{'producer'} || $args->{'to'}
$args->{'producer'} $args->{'to'}
Truth table100%3/3
A || B
A= $args->{'producer'}B= $args->{'to'}
ABresult
1X1
011
000
353 42 $self->producer($producer);
354 }
355 117 $producer = $self->producer;
356 117 $producer_type = $self->producer_type;
357  
358 # ----------------------------------------------------------------
359 # Execute the parser, the filters and then execute the producer.
360 # Allowances are made for each piece to die, or fail to compile,
361 # since the referenced subroutines could be almost anything. In
362 # the future, each of these might happen in a Safe environment,
363 # depending on how paranoid we want to be.
364 # ----------------------------------------------------------------
365  
366 # Run parser
367 117 ▶ unless ($self->_has_schema) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_schema) 82 35 117
368 82 eval { $parser_output = $parser->($self, $$data) };
369 82 ▶ if ($@ || !$parser_output) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $parser_output) 2 80 82
Condition66%2/3
$@ or not $parser_output
l!l&&r!l&&!r
0280
MC/DC50%1/2
$@ or not $parser_output
$@ not $parser_output
Truth table66%2/3
A or not(B)
A= $@B= not $parser_output
ABresult
1X1
011
000
370 2 ▶ my $msg = sprintf "translate: Error with parser '%s': %s", $parser_type, ($@) ? $@ : " no results";
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 2 2
371 2 return $self->error($msg);
372 }
373 }
374 115 ▶ $self->debug("Schema =\n", Dumper($self->schema), "\n")
Branch: 1 branch
BranchTrueFalseTotal
if $self->debugging 0 115 115
375 if $self->debugging;
376  
377 # Validate the schema if asked to.
378 115 ▶ if ($self->validate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->validate) 0 115 115
379 0 my $schema = $self->schema;
380 0 ▶ return $self->error('Invalid schema') unless $schema->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema->is_valid 0 0 0
381 }
382  
383 # Run filters
384 115 my $filt_num = 0;
385 115 foreach ($self->filters) {
386 10 $filt_num++;
387 10 my ($code, @args) = @$_;
388 10 eval { $code->($self->schema, @args) };
389 10 ▶ my $err = $@ || $self->error || 0;
Condition40%2/5
$@ || $self->error || 0
l!l
010
$@ or $self->error
l!l&&r!l&&!r
0010
MC/DC0%0/2
$@ || $self->error || 0
$@ $self->error
Truth table33%1/3
A or B
A= $@B= $self->error
ABresult
1X1
011
000
390 10 ▶ return $self->error("Error with filter $filt_num : $err") if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 10 10
391 }
392  
393 # Run producer
394 # Calling wantarray in the eval no work, wrong scope.
395 115 ▶ my $wantarray = wantarray ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 10 105 115
396 115 eval {
397 115 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) { } 10 105 115
398 10 @producer_output = $producer->($self);
399 } else {
400 105 $producer_output = $producer->($self);
401 }
402 };
403 115 ▶ if ($@ || !($producer_output || @producer_output)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $producer_output || @producer_output) 0 115 115
Condition50%3/6
$@ or not $producer_output || @producer_output
l!l&&r!l&&!r
00115
$producer_output || @producer_output
l!l&&r!l&&!r
105100
MC/DC0%0/3
$@ or not $producer_output || @producer_output
$@ $producer_output @producer_output
Truth table50%2/4
A or not(B || C)
A= $@B= $producer_outputC= @producer_output
ABCresult
1XX1
0001
01X0
0010
404 0 ▶ my $err = $@ || $self->error || "no results";
Condition0%0/5
$@ || $self->error || 'no results'
l!l
00
$@ or $self->error
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ || $self->error || 'no results'
$@ $self->error
Truth table0%0/3
A or B
A= $@B= $self->error
ABresult
1X1
011
000
405 0 my $msg = "translate: Error with producer '$producer_type': $err";
406 0 return $self->error($msg);
407 }
408  
409 115 ▶ return wantarray ? @producer_output : $producer_output;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 10 105 115
410 }
411  
412 8 ▶sub list_parsers {
Subroutine list_parsers: called CC=1 SCAR=0.0
Pod undocumented
413 8 return shift->_list("parser");
414 }
415  
416 0 ▶sub list_producers {
Subroutine list_producers: not called CC=1 SCAR=6.9
Pod undocumented
417 0 return shift->_list("producer");
418 }
419  
420 # ======================================================================
421 # Private Methods
422 # ======================================================================
423  
424 # ----------------------------------------------------------------------
425 # _args($type, \%args);
426 #
427 # Gets or sets ${type}_args. Called by parser_args and producer_args.
428 # ----------------------------------------------------------------------
429 212 ▶sub _args {
Subroutine _args: called CC=4 SCAR=14.1
430 212 my $self = shift;
431 212 my $orig = shift;
432  
433 212 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 209 212
434  
435 # If the first argument is an explicit undef (remember, we
436 # don't get here unless there is stuff in @_), then we clear
437 # out the producer_args hash.
438 3 ▶ if (!defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[0]) 0 3 3
439 0 shift @_;
440 0 $self->$orig({});
441 }
442  
443 3 ▶ my $args = isa($_[0], 'HASH') ? shift : {@_};
Branch: 1 branch
BranchTrueFalseTotal
&SQL::Translator::isa($_[0], 'HASH') ? : 1 2 3
444 3 return $self->$orig({ %{ $self->$orig }, %$args });
445 }
446  
447 209 return $self->$orig;
448 }
449  
450 # ----------------------------------------------------------------------
451 # Does the get/set work for parser and producer. e.g.
452 # return $self->_tool({
453 # name => 'producer',
454 # path => "SQL::Translator::Producer",
455 # default_sub => "produce",
456 # }, @_);
457 # ----------------------------------------------------------------------
458 905 ▶sub _tool {
Subroutine _tool: called CC=10 SCAR=23.2
459 905 my ($self, $args) = (shift, shift);
460 905 my $name = $args->{name};
461 905 my $orig = $args->{orig};
462 905 ▶ return $self->{$name} unless @_; # get accessor
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 522 383 905
463  
464 383 my $path = $args->{path};
465 383 my $default_sub = $args->{default_sub};
466 383 my $tool = shift;
467  
468 # passed an anonymous subroutine reference
469 383 ▶ if (isa($tool, 'CODE')) {
Branch: 1 branch
BranchTrueFalseTotal
if (&SQL::Translator::isa($tool, 'CODE')) { } 226 157 383
470 226 $self->$orig($tool);
471 226 $self->${ \"_set_${name}_type" }("CODE");
472 226 $self->debug("Got $name: code ref\n");
473 }
474  
475 # Module name was passed directly
476 # We try to load the name; if it doesn't load, there's a
477 # possibility that it has a function name attached to it,
478 # so we give it a go.
479 else {
480 157 ▶ $tool =~ s/-/::/g if $tool !~ /::/;
Branch: 1 branch
BranchTrueFalseTotal
unless $tool =~ /::/ 156 1 157
481 157 my ($code, $sub);
482 157 ($code, $sub) = _load_sub("$tool\::$default_sub", $path);
483 157 ▶ unless ($code) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code) 1 156 157
484 1 ▶ if (__PACKAGE__->error =~ m/Can't find module/) {
Branch: 1 branch
BranchTrueFalseTotal
if (__PACKAGE__->error =~ /Can't find module/) { } 1 0 1
485  
486 # Mod not found so try sub
487 1 ▶ ($code, $sub) = _load_sub("$tool", $path) unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 1 0 1
488 1 ▶ die "Can't load $name subroutine '$tool' : " . __PACKAGE__->error
Branch: 1 branch
BranchTrueFalseTotal
unless $code 0 1 1
489 unless $code;
490 } else {
491 0 die "Can't load $name '$tool' : " . __PACKAGE__->error;
492 }
493 }
494  
495 # get code reference and assign
496 157 my (undef, $module, undef) = $sub =~ m/((.*)::)?(\w+)$/;
497 157 $self->$orig($code);
498 157 ▶ $self->${ \"_set_$name\_type" }($sub eq "CODE" ? "CODE" : $module);
Branch: 1 branch
BranchTrueFalseTotal
$sub eq 'CODE' ? : 0 157 157
499 157 $self->debug("Got $name: $sub\n");
500 }
501  
502 # At this point, $self->{$name} contains a subroutine
503 # reference that is ready to run
504  
505 # Anything left? If so, it's args
506 383 my $meth = "$name\_args";
507 383 ▶ $self->$meth(@_) if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 382 383
508  
509 383 return $self->{$name};
510 }
511  
512 # ----------------------------------------------------------------------
513 # _list($type)
514 # ----------------------------------------------------------------------
515 8 ▶sub _list {
Subroutine _list: called CC=5 SCAR=16.2
516 8 my $self = shift;
517 8 ▶ my $type = shift || return ();
Condition50%1/2
shift() || (return ())
l!l
80
MC/DC0%0/1
shift() || (return ())
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
518 8 my $uctype = ucfirst lc $type;
519  
520 #
521 # First find all the directories where SQL::Translator
522 # parsers or producers (the "type") appear to live.
523 #
524 8 ▶ load("SQL::Translator::$uctype") or return ();
Branch: 1 branch
BranchTrueFalseTotal
load("SQL::Translator::$uctype") or return () 8 0 8
525 8 my $path = catfile "SQL", "Translator", $uctype;
526 8 my @dirs;
527 8 for (@INC) {
528 64 my $dir = catfile $_, $path;
529 64 $self->debug("_list_${type}s searching $dir\n");
530 64 ▶ next unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 40 24 64
531 24 push @dirs, $dir;
532 }
533  
534 #
535 # Now use File::File::find to look recursively in those
536 # directories for all the *.pm files, then present them
537 # with the slashes turned into dashes.
538 #
539 8 my %found;
540 find(
541 672 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.4
542 672 ▶ if (-f && m/\.pm$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $_ and /\.pm$/) 576 96 672
Condition66%2/3
-f $_ and /\.pm$/
!ll&&!rl&&r
960576
MC/DC50%1/2
-f $_ and /\.pm$/
-f $_ /\.pm$/
Truth table66%2/3
A and B
A= -f $_B= /\.pm$/
ABresult
0X0
100
111
543 576 my $mod = $_;
544 576 $mod =~ s/\.pm$//;
545 576 my $cur_dir = $File::Find::dir;
546 576 my $base_dir = quotemeta catfile 'SQL', 'Translator', $uctype;
547  
548 #
549 # See if the current directory is below the base directory.
550 #
551 576 ▶ if ($cur_dir =~ m/$base_dir(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_dir =~ /$base_dir(.*)/) { } 576 0 576
552 576 $cur_dir = $1;
553 576 $cur_dir =~ s!^/!!; # kill leading slash
554 576 $cur_dir =~ s!/!-!g; # turn other slashes into dashes
555 } else {
556 0 $cur_dir = '';
557 }
558  
559 576 ▶ $found{ join '-', map { $_ || () } $cur_dir, $mod } = 1;
Condition66%2/3
$_ || ()
l!l&&r!l&&!r
7923600
MC/DC0%0/2
$_ || ()
$_ ()
Truth table66%2/3
A || B
A= $_B= ()
ABresult
1X1
011
000
560 }
561 },
562 @dirs
563 8 );
564  
565 8 return sort { lc $a cmp lc $b } keys %found;
566 }
567  
568 # ----------------------------------------------------------------------
569 # load(MODULE [,PATH[,PATH]...])
570 #
571 # Loads a Perl module. Short circuits if a module is already loaded.
572 #
573 # MODULE - is the name of the module to load.
574 #
575 # PATH - optional list of 'package paths' to look for the module in. e.g
576 # If you called load('Super::Foo' => 'My', 'Other') it will
577 # try to load the mod Super::Foo then My::Super::Foo then Other::Super::Foo.
578 #
579 # Returns package name of the module actually loaded or false and sets error.
580 #
581 # Note, you can't load a name from the root namespace (ie one without '::' in
582 # it), therefore a single word name without a path fails.
583 # ----------------------------------------------------------------------
584 171 ▶sub load {
Subroutine load: called CC=10 SCAR=23.2
Pod undocumented
585 171 my $name = shift;
586 171 my @path;
587 171 ▶ push @path, "" if $name =~ /::/; # Empty path to check name on its own first
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /::/ 33 138 171
588 171 ▶ push @path, @_ if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 163 8 171
589  
590 171 foreach (@path) {
591 195 ▶ my $module = $_ ? "$_\::$name" : $name;
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 162 33 195
592 195 my $file = $module;
593 195 $file =~ s[::][/]g;
594 195 $file .= ".pm";
595 195 __PACKAGE__->debug("Loading $name as $file\n");
596 195 ▶ return $module if $INC{$file}; # Already loaded
Branch: 1 branch
BranchTrueFalseTotal
if $INC{$file} 131 64 195
597  
598 64 eval { require $file };
599 64 ▶ next if $@ =~ /Can't locate $file in \@INC/;
Branch: 1 branch
BranchTrueFalseTotal
if $@ =~ /Can't locate $file in \@INC/ 25 39 64
600 39 ▶ eval { $module->import() } unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 39 0 39
601 39 ▶ return __PACKAGE__->error("Error loading $name as $module : $@")
Branch: 1 branch
BranchTrueFalseTotal
if $@ and not $@ =~ /"SQL::Translator::Producer" is not exported/ 0 39 39
Condition33%1/3
$@ and not $@ =~ /"SQL::Translator::Producer" is not exported/
!ll&&!rl&&r
3900
MC/DC0%0/2
$@ and not $@ =~ /"SQL::Translator::Producer" is not exported/
$@ not $@ =~ /"SQL::Translator::Producer" is not exported/
Truth table33%1/3
A and not(B)
A= $@B= not $@ =~ /"SQL::Translator::Producer" is not exported/
ABresult
0X0
100
111
602 if $@ && $@ !~ /"SQL::Translator::Producer" is not exported/;
603  
604 39 return $module; # Module loaded ok
605 }
606  
607 1 return __PACKAGE__->error("Can't find module $name. Path:" . join(",", @path));
608 }
609  
610 # ----------------------------------------------------------------------
611 # Load the sub name given (including package), optionally using a base package
612 # path. Returns code ref and name of sub loaded, including its package.
613 # (\&code, $sub) = load_sub( 'MySQL::produce', "SQL::Translator::Producer" );
614 # (\&code, $sub) = load_sub( 'MySQL::produce', @path );
615 # ----------------------------------------------------------------------
616 163 ▶sub _load_sub {
Subroutine _load_sub: called CC=3 SCAR=11.0
617 163 my ($tool, @path) = @_;
618  
619 163 my (undef, $module, $func_name) = $tool =~ m/((.*)::)?(\w+)$/;
620 163 ▶ if (my $module = load($module => @path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $module = load($module, @path)) 162 1 163
621 162 my $sub = "$module\::$func_name";
622 162 ▶ return wantarray ? (\&{$sub}, $sub) : \&$sub;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 157 5 162
623 }
624 1 return undef;
625 }
626  
627 6 ▶sub format_table_name {
Subroutine format_table_name: called CC=1 SCAR=0.0
Pod undocumented
628 6 return shift->_format_name('_format_table_name', @_);
629 }
630  
631 6 ▶sub format_package_name {
Subroutine format_package_name: called CC=1 SCAR=0.0
Pod undocumented
632 6 return shift->_format_name('_format_package_name', @_);
633 }
634  
635 6 ▶sub format_fk_name {
Subroutine format_fk_name: called CC=1 SCAR=0.0
Pod undocumented
636 6 return shift->_format_name('_format_fk_name', @_);
637 }
638  
639 6 ▶sub format_pk_name {
Subroutine format_pk_name: called CC=1 SCAR=0.0
Pod undocumented
640 6 return shift->_format_name('_format_pk_name', @_);
641 }
642  
643 # ----------------------------------------------------------------------
644 # The other format_*_name methods rely on this one. It optionally
645 # accepts a subroutine ref as the first argument (or uses an identity
646 # sub if one isn't provided or it doesn't already exist), and applies
647 # it to the rest of the arguments (if any).
648 # ----------------------------------------------------------------------
649 24 ▶sub _format_name {
Subroutine _format_name: called CC=4 SCAR=13.9
650 24 my $self = shift;
651 24 my $field = shift;
652 24 my @args = @_;
653  
654 24 ▶ if (ref($args[0]) eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $args[0] eq 'CODE') { } 12 12 24
elsif (not exists $self->{$field}) { } 4 8 12
655 12 $self->{$field} = shift @args;
656 } elsif (!exists $self->{$field}) {
657 4 ▶ $self->{$field} = sub { return shift };
Subroutine __ANON__: called CC=1 SCAR=0.0
658 }
659  
660 24 ▶ return @args ? $self->{$field}->(@args) : $self->{$field};
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 16 8 24
661 }
662  
663 885 ▶sub isa($$) {
Subroutine isa: called CC=1 SCAR=0.0
Pod undocumented
664 885 my ($ref, $type) = @_;
665 885 return UNIVERSAL::isa($ref, $type);
666 }
667  
668 9 ▶sub version {
Subroutine version: called CC=1 SCAR=0.0
Pod documented
669 9 my $self = shift;
670 9 return $VERSION;
671 }
672  
673 # Must come after all 'has' declarations
674 71 around new => \&ex2err;
675  
676 71 1;
677  
678 # ----------------------------------------------------------------------
679 # Who killed the pork chops?
680 # What price bananas?
681 # Are you my Angel?
682 # Allen Ginsberg
683 # ----------------------------------------------------------------------
684  
685 =pod
686  
687 =head1 NAME
688  
689 SQL::Translator - manipulate structured data definitions (SQL and more)
690  
691 =head1 SYNOPSIS
692  
693 use SQL::Translator;
694  
695 my $translator = SQL::Translator->new(
696 # Print debug info
697 debug => 1,
698 # Print Parse::RecDescent trace
699 trace => 0,
700 # Don't include comments in output
701 no_comments => 0,
702 # Print name mutations, conflicts
703 show_warnings => 0,
704 # Add "drop table" statements
705 add_drop_table => 1,
706 # to quote or not to quote, thats the question
707 quote_identifiers => 1,
708 # Validate schema object
709 validate => 1,
710 # Make all table names CAPS in producers which support this option
711 format_table_name => sub {my $tablename = shift; return uc($tablename)},
712 # Null-op formatting, only here for documentation's sake
713 format_package_name => sub {return shift},
714 format_fk_name => sub {return shift},
715 format_pk_name => sub {return shift},
716 );
717  
718 my $output = $translator->translate(
719 from => 'MySQL',
720 to => 'Oracle',
721 # Or an arrayref of filenames, i.e. [ $file1, $file2, $file3 ]
722 filename => $file,
723 ) or die $translator->error;
724  
725 print $output;
726  
727 =head1 DESCRIPTION
728  
729 This documentation covers the API for SQL::Translator. For a more general
730 discussion of how to use the modules and scripts, please see
731 L<SQL::Translator::Manual>.
732  
733 SQL::Translator is a group of Perl modules that converts
734 vendor-specific SQL table definitions into other formats, such as
735 other vendor-specific SQL, ER diagrams, documentation (POD and HTML),
736 XML, and Class::DBI classes. The main focus of SQL::Translator is
737 SQL, but parsers exist for other structured data formats, including
738 Excel spreadsheets and arbitrarily delimited text files. Through the
739 separation of the code into parsers and producers with an object model
740 in between, it's possible to combine any parser with any producer, to
741 plug in custom parsers or producers, or to manipulate the parsed data
742 via the built-in object model. Presently only the definition parts of
743 SQL are handled (CREATE, ALTER), not the manipulation of data (INSERT,
744 UPDATE, DELETE).
745  
746 =head1 CONSTRUCTOR
747  
748 =head2 new
749  
750 The constructor is called C<new>, and accepts a optional hash of options.
751 Valid options are:
752  
753 =over 4
754  
755 =item *
756  
757 parser / from
758  
759 =item *
760  
761 parser_args
762  
763 =item *
764  
765 producer / to
766  
767 =item *
768  
769 producer_args
770  
771 =item *
772  
773 filters
774  
775 =item *
776  
777 filename / file
778  
779 =item *
780  
781 data
782  
783 =item *
784  
785 debug
786  
787 =item *
788  
789 add_drop_table
790  
791 =item *
792  
793 quote_identifiers
794  
795 =item *
796  
797 quote_table_names (DEPRECATED)
798  
799 =item *
800  
801 quote_field_names (DEPRECATED)
802  
803 =item *
804  
805 no_comments
806  
807 =item *
808  
809 trace
810  
811 =item *
812  
813 validate
814  
815 =back
816  
817 All options are, well, optional; these attributes can be set via
818 instance methods. Internally, they are; no (non-syntactical)
819 advantage is gained by passing options to the constructor.
820  
821 =head1 METHODS
822  
823 =head2 add_drop_table
824  
825 Toggles whether or not to add "DROP TABLE" statements just before the
826 create definitions.
827  
828 =head2 quote_identifiers
829  
830 Toggles whether or not to quote identifiers (table, column, constraint, etc.)
831 with a quoting mechanism suitable for the chosen Producer. The default (true)
832 is to quote them.
833  
834 =head2 quote_table_names
835  
836 DEPRECATED - A legacy proxy to L</quote_identifiers>
837  
838 =head2 quote_field_names
839  
840 DEPRECATED - A legacy proxy to L</quote_identifiers>
841  
842 =head2 no_comments
843  
844 Toggles whether to print comments in the output. Accepts a true or false
845 value, returns the current value.
846  
847 =head2 producer
848  
849 The C<producer> method is an accessor/mutator, used to retrieve or
850 define what subroutine is called to produce the output. A subroutine
851 defined as a producer will be invoked as a function (I<not a method>)
852 and passed its container C<SQL::Translator> instance, which it should
853 call the C<schema> method on, to get the C<SQL::Translator::Schema>
854 generated by the parser. It is expected that the function transform the
855 schema structure to a string. The C<SQL::Translator> instance is also useful
856 for informational purposes; for example, the type of the parser can be
857 retrieved using the C<parser_type> method, and the C<error> and
858 C<debug> methods can be called when needed.
859  
860 When defining a producer, one of several things can be passed in: A
861 module name (e.g., C<My::Groovy::Producer>), a module name relative to
862 the C<SQL::Translator::Producer> namespace (e.g., C<MySQL>), a module
863 name and function combination (C<My::Groovy::Producer::transmogrify>),
864 or a reference to an anonymous subroutine. If a full module name is
865 passed in (for the purposes of this method, a string containing "::"
866 is considered to be a module name), it is treated as a package, and a
867 function called "produce" will be invoked: C<$modulename::produce>.
868 If $modulename cannot be loaded, the final portion is stripped off and
869 treated as a function. In other words, if there is no file named
870 F<My/Groovy/Producer/transmogrify.pm>, C<SQL::Translator> will attempt
871 to load F<My/Groovy/Producer.pm> and use C<transmogrify> as the name of
872 the function, instead of the default C<produce>.
873  
874 my $tr = SQL::Translator->new;
875  
876 # This will invoke My::Groovy::Producer::produce($tr, $data)
877 $tr->producer("My::Groovy::Producer");
878  
879 # This will invoke SQL::Translator::Producer::Sybase::produce($tr, $data)
880 $tr->producer("Sybase");
881  
882 # This will invoke My::Groovy::Producer::transmogrify($tr, $data),
883 # assuming that My::Groovy::Producer::transmogrify is not a module
884 # on disk.
885 $tr->producer("My::Groovy::Producer::transmogrify");
886  
887 # This will invoke the referenced subroutine directly, as
888 # $subref->($tr, $data);
889 $tr->producer(\&my_producer);
890  
891 There is also a method named C<producer_type>, which is a string
892 containing the classname to which the above C<produce> function
893 belongs. In the case of anonymous subroutines, this method returns
894 the string "CODE".
895  
896 Finally, there is a method named C<producer_args>, which is both an
897 accessor and a mutator. Arbitrary data may be stored in name => value
898 pairs for the producer subroutine to access:
899  
900 sub My::Random::producer {
901 my ($tr, $data) = @_;
902 my $pr_args = $tr->producer_args();
903  
904 # $pr_args is a hashref.
905  
906 Extra data passed to the C<producer> method is passed to
907 C<producer_args>:
908  
909 $tr->producer("xSV", delimiter => ',\s*');
910  
911 # In SQL::Translator::Producer::xSV:
912 my $args = $tr->producer_args;
913 my $delimiter = $args->{'delimiter'}; # value is ,\s*
914  
915 =head2 parser
916  
917 The C<parser> method defines or retrieves a subroutine that will be
918 called to perform the parsing. The basic idea is the same as that of
919 C<producer> (see above), except the default subroutine name is
920 "parse", and will be invoked as C<$module_name::parse($tr, $data)>.
921 Also, the parser subroutine will be passed a string containing the
922 entirety of the data to be parsed.
923  
924 # Invokes SQL::Translator::Parser::MySQL::parse()
925 $tr->parser("MySQL");
926  
927 # Invokes My::Groovy::Parser::parse()
928 $tr->parser("My::Groovy::Parser");
929  
930 # Invoke an anonymous subroutine directly
931 $tr->parser(sub {
932 my $dumper = Data::Dumper->new([ $_[1] ], [ "SQL" ]);
933 $dumper->Purity(1)->Terse(1)->Deepcopy(1);
934 return $dumper->Dump;
935 });
936  
937 There is also C<parser_type> and C<parser_args>, which perform
938 analogously to C<producer_type> and C<producer_args>
939  
940 =head2 filters
941  
942 Set or retrieve the filters to run over the schema during the
943 translation, before the producer creates its output. Filters are sub
944 routines called, in order, with the schema object to filter as the 1st
945 arg and a hash of options (passed as a list) for the rest of the args.
946 They are free to do whatever they want to the schema object, which will be
947 handed to any following filters, then used by the producer.
948  
949 Filters are set as an array, which gives the order they run in.
950 Like parsers and producers, they can be defined by a module name, a
951 module name relative to the SQL::Translator::Filter namespace, a module
952 name and function name together or a reference to an anonymous subroutine.
953 When using a module name a function called C<filter> will be invoked in
954 that package to do the work.
955  
956 To pass args to the filter set it as an array ref with the 1st value giving
957 the filter (name or sub) and the rest its args. e.g.
958  
959 $tr->filters(
960 sub {
961 my $schema = shift;
962 # Do stuff to schema here!
963 },
964 DropFKeys,
965 [ "Names", table => 'lc' ],
966 [ "Foo", foo => "bar", hello => "world" ],
967 [ "Filter5" ],
968 );
969  
970 Although you normally set them in the constructor, which calls
971 through to filters. i.e.
972  
973 my $translator = SQL::Translator->new(
974 ...
975 filters => [
976 sub { ... },
977 [ "Names", table => 'lc' ],
978 ],
979 ...
980 );
981  
982 See F<t/36-filters.t> for more examples.
983  
984 Multiple set calls to filters are cumulative with new filters added to
985 the end of the current list.
986  
987 Returns the filters as a list of array refs, the 1st value being a
988 reference to the filter sub and the rest its args.
989  
990 =head2 show_warnings
991  
992 Toggles whether to print warnings of name conflicts, identifier
993 mutations, etc. Probably only generated by producers to let the user
994 know when something won't translate very smoothly (e.g., MySQL "enum"
995 fields into Oracle). Accepts a true or false value, returns the
996 current value.
997  
998 =head2 translate
999  
1000 The C<translate> method calls the subroutine referenced by the
1001 C<parser> data member, then calls any C<filters> and finally calls
1002 the C<producer> sub routine (these members are described above).
1003 It accepts as arguments a number of things, in key => value format,
1004 including (potentially) a parser and a producer (they are passed
1005 directly to the C<parser> and C<producer> methods).
1006  
1007 Here is how the parameter list to C<translate> is parsed:
1008  
1009 =over
1010  
1011 =item *
1012  
1013 1 argument means it's the data to be parsed; which could be a string
1014 (filename) or a reference to a scalar (a string stored in memory), or a
1015 reference to a hash, which is parsed as being more than one argument
1016 (see next section).
1017  
1018 # Parse the file /path/to/datafile
1019 my $output = $tr->translate("/path/to/datafile");
1020  
1021 # Parse the data contained in the string $data
1022 my $output = $tr->translate(\$data);
1023  
1024 =item *
1025  
1026 More than 1 argument means its a hash of things, and it might be
1027 setting a parser, producer, or datasource (this key is named
1028 "filename" or "file" if it's a file, or "data" for a SCALAR reference.
1029  
1030 # As above, parse /path/to/datafile, but with different producers
1031 for my $prod ("MySQL", "XML", "Sybase") {
1032 print $tr->translate(
1033 producer => $prod,
1034 filename => "/path/to/datafile",
1035 );
1036 }
1037  
1038 # The filename hash key could also be:
1039 datasource => \$data,
1040  
1041 You get the idea.
1042  
1043 =back
1044  
1045 =head2 filename, data
1046  
1047 Using the C<filename> method, the filename of the data to be parsed
1048 can be set. This method can be used in conjunction with the C<data>
1049 method, below. If both the C<filename> and C<data> methods are
1050 invoked as mutators, the data set in the C<data> method is used.
1051  
1052 $tr->filename("/my/data/files/create.sql");
1053  
1054 or:
1055  
1056 my $create_script = do {
1057 local $/;
1058 open CREATE, "/my/data/files/create.sql" or die $!;
1059 <CREATE>;
1060 };
1061 $tr->data(\$create_script);
1062  
1063 C<filename> takes a string, which is interpreted as a filename.
1064 C<data> takes a reference to a string, which is used as the data to be
1065 parsed. If a filename is set, then that file is opened and read when
1066 the C<translate> method is called, as long as the data instance
1067 variable is not set.
1068  
1069 =head2 schema
1070  
1071 Returns the SQL::Translator::Schema object.
1072  
1073 =head2 trace
1074  
1075 Turns on/off the tracing option of Parse::RecDescent.
1076  
1077 =head2 validate
1078  
1079 Whether or not to validate the schema object after parsing and before
1080 producing.
1081  
1082 =head2 version
1083  
1084 Returns the version of the SQL::Translator release.
1085  
1086 =head1 AUTHORS
1087  
1088 See the included AUTHORS file:
1089 L<http://search.cpan.org/dist/SQL-Translator/AUTHORS>
1090  
1091 =head1 GETTING HELP/SUPPORT
1092  
1093 If you are stuck with a problem or have doubts about a particular
1094 approach do not hesitate to contact us via any of the following
1095 options (the list is sorted by "fastest response time"):
1096  
1097 =over
1098  
1099 =item * IRC: irc.perl.org#sql-translator
1100  
1101 =for html
1102 <a href="https://chat.mibbit.com/#sql-translator@irc.perl.org">(click for instant chatroom login)</a>
1103  
1104 =item * Mailing list: L<http://lists.scsys.co.uk/mailman/listinfo/dbix-class>
1105  
1106 =item * RT Bug Tracker: L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=SQL-Translator>
1107  
1108 =back
1109  
1110 =head1 HOW TO CONTRIBUTE
1111  
1112 Contributions are always welcome, in all usable forms (we especially
1113 welcome documentation improvements). The delivery methods include git-
1114 or unified-diff formatted patches, GitHub pull requests, or plain bug
1115 reports either via RT or the Mailing list. Contributors are generally
1116 granted access to the official repository after their first several
1117 patches pass successful review. Don't hesitate to
1118 L<contact|/GETTING HELP/SUPPORT> us with any further questions you may
1119 have.
1120  
1121 This project is maintained in a git repository. The code and related tools are
1122 accessible at the following locations:
1123  
1124 =over
1125  
1126 =item * Official repo: L<git://git.shadowcat.co.uk/dbsrgits/SQL-Translator.git>
1127  
1128 =item * Official gitweb: L<http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/SQL-Translator.git>
1129  
1130 =item * GitHub mirror: L<https://github.com/dbsrgits/SQL-Translator>
1131  
1132 =item * Authorized committers: L<ssh://dbsrgits@git.shadowcat.co.uk/sql-translator.git>
1133  
1134 =item * Travis-CI log: L<https://travis-ci.org/dbsrgits/sql-translator/builds>
1135  
1136 =for html
1137 &#x21AA; Stable branch CI status: <img src="https://secure.travis-ci.org/dbsrgits/sql-translator.png?branch=master"></img>
1138  
1139 =back
1140  
1141 =head1 COPYRIGHT
1142  
1143 Copyright 2012 the SQL::Translator authors, as listed in L</AUTHORS>.
1144  
1145 =head1 LICENSE
1146  
1147 This library is free software and may be distributed under the same terms as
1148 Perl 5 itself.
1149  
1150 =head1 PRAISE
1151  
1152 If you find this module useful, please use
1153 L<http://cpanratings.perl.org/rate/?distribution=SQL-Translator> to rate it.
1154  
1155 =head1 SEE ALSO
1156  
1157 L<perl>,
1158 L<SQL::Translator::Parser>,
1159 L<SQL::Translator::Producer>,
1160 L<Parse::RecDescent>,
1161 L<GD>,
1162 L<GraphViz>,
1163 L<Text::RecordParser>,
1164 L<Class::DBI>,
1165 L<XML::Writer>.
« blib/lib/SQL/Translator/Parser/JSON.pm ↑ Summary blib/lib/SQL/Translator/Producer/MySQL.pm »