blib/lib/SQL/Translator.pm

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