blib/lib/SQL/Translator/Utils.pm

filter: Statementstmt 83.5% 142 / 170 Branchbran 61.5% 48 / 78 Conditioncond 75.0% 24 / 32 MC/DCmcdc 66.6% 12 / 18 Subroutinesub 97.0% 33 / 34 Podpod 78.5% 11 / 14 totaltotal 78.0% 270 / 346 CC 55 SCAR 23.1
CC
55
Cov
76%
parse_dbms_version
39.8
_find_co_root
20.0
normalize_quote_options
19.6
CRAP 10.1
« blib/lib/SQL/Translator/Filter/Globals.pm ↑ Summary blib/lib/SQL/Translator/Producer/SQLite.pm »
1 package SQL::Translator::Utils;
2  
3 83 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 83 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 83 ▶use Digest::SHA qw( sha1_hex );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 83 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 83 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 83 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 83 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 83 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 83 our $VERSION = '1.66';
13  
14 83 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 83 our @EXPORT_OK = qw(
16 debug normalize_name header_comment parse_list_arg truncate_id_uniquely
17 $DEFAULT_COMMENT parse_mysql_version parse_dbms_version
18 ddl_parser_instance batch_alter_table_statements
19 uniq throw ex2err carp_ro
20 normalize_quote_options
21 );
22 83 ▶use constant COLLISION_TAG_LENGTH => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 83 our $DEFAULT_COMMENT = '--';
25  
26 2644 ▶sub debug {
Subroutine debug: called CC=3 SCAR=15.6
Pod documented
27 2644 my ($pkg, $file, $line, $sub) = caller(0);
28 {
29 83 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2644 ▶ return unless ${"$pkg\::DEBUG"};
Branch: 1 branch
BranchTrueFalseTotal
unless ${"${pkg}::DEBUG";} 2644 0 2644
31 }
32  
33 0 $sub =~ s/^$pkg\:://;
34  
35 0 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
36 0 my $x = shift;
37 0 chomp $x;
38 0 $x =~ s/\bPKG\b/$pkg/g;
39 0 $x =~ s/\bLINE\b/$line/g;
40 0 $x =~ s/\bSUB\b/$sub/g;
41  
42 #warn '[' . $x . "]\n";
43 0 print STDERR '[' . $x . "]\n";
44 }
45 }
46  
47 21 ▶sub normalize_name {
Subroutine normalize_name: called CC=2 SCAR=7.0
Pod documented
48 21 ▶ my $name = shift or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $name = shift() or return "" 21 0 21
49  
50 # The name can only begin with a-zA-Z_; if there's anything
51 # else, prefix with _
52 21 $name =~ s/^([^a-zA-Z_])/_$1/;
53  
54 # anything other than a-zA-Z0-9_ in the non-first position
55 # needs to be turned into _
56 21 $name =~ tr/[a-zA-Z0-9_]/_/c;
57  
58 # All duplicated _ need to be squashed into one.
59 21 $name =~ tr/_/_/s;
60  
61 # Trim a trailing _
62 21 $name =~ s/_$//;
63  
64 21 return $name;
65 }
66  
67 601 ▶sub normalize_quote_options {
Subroutine normalize_quote_options: called CC=15 SCAR=29.7
Pod undocumented
68 601 my $config = shift;
69  
70 601 my $quote;
71 601 ▶ if (defined $config->{quote_identifiers}) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $config->{'quote_identifiers'}) { } 233 368 601
elsif (defined $config->{'quote_table_names'} xor defined $config->{'quote_field_names'}) { } 10 358 368
elsif (defined $config->{'quote_table_names'}) { } 114 244 358
72 233 $quote = $config->{quote_identifiers};
73  
74 233 for (qw/quote_table_names quote_field_names/) {
75 carp "Ignoring deprecated parameter '$_', since 'quote_identifiers' is supplied"
76 466 ▶ if defined $config->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if defined $config->{$_} 0 466 466
77 }
78 }
79  
80 # Legacy one set the other is not
81 ▶ elsif (defined $config->{'quote_table_names'} xor defined $config->{'quote_field_names'}) {
Condition75%3/4
defined $config->{"quote_table_names"} xor defined $config->{"quote_field_names"}
l&&rl&&!r!l&&r!l&&!r
114100244
MC/DC100%2/2
defined $config->{"quote_table_names"} xor defined $config->{"quote_field_names"}
defined $config->{"quote_table_names"} defined $config->{"quote_field_names"}
Truth table75%3/4
A xor B
A= defined $config->{"quote_table_names"}B= defined $config->{"quote_field_names"}
ABresult
110
101
011
000
82 10 ▶ if (defined $config->{'quote_table_names'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config->{'quote_table_names'}) { } 10 0 10
83 carp
84 "Explicitly disabling the deprecated 'quote_table_names' implies disabling 'quote_identifiers' which in turn implies disabling 'quote_field_names'"
85 10 ▶ unless $config->{'quote_table_names'};
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{"quote_table_names"} 0 10 10
86 10 ▶ $quote = $config->{'quote_table_names'} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$config->{'quote_table_names'} ? : 10 0 10
87 } else {
88 carp
89 "Explicitly disabling the deprecated 'quote_field_names' implies disabling 'quote_identifiers' which in turn implies disabling 'quote_table_names'"
90 0 ▶ unless $config->{'quote_field_names'};
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{"quote_field_names"} 0 0 0
91 0 ▶ $quote = $config->{'quote_field_names'} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$config->{'quote_field_names'} ? : 0 0 0
92 }
93 }
94  
95 # Legacy both are set
96 elsif (defined $config->{'quote_table_names'}) {
97 croak 'Setting quote_table_names and quote_field_names to conflicting values is no longer supported'
98 114 ▶ if ($config->{'quote_table_names'} xor $config->{'quote_field_names'});
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"quote_table_names"} xor $config->{"quote_field_names"} 1 113 114
Condition75%3/4
$config->{"quote_table_names"} xor $config->{"quote_field_names"}
l&&rl&&!r!l&&r!l&&!r
710142
MC/DC100%2/2
$config->{"quote_table_names"} xor $config->{"quote_field_names"}
$config->{"quote_table_names"} $config->{"quote_field_names"}
Truth table75%3/4
A xor B
A= $config->{"quote_table_names"}B= $config->{"quote_field_names"}
ABresult
110
101
011
000
99  
100 113 ▶ $quote = $config->{'quote_table_names'} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$config->{'quote_table_names'} ? : 71 42 113
101 }
102  
103 600 return $quote;
104 }
105  
106 16 ▶sub header_comment {
Subroutine header_comment: called CC=4 SCAR=13.9
Pod documented
107 16 ▶ my $producer = shift || caller;
Condition66%2/3
shift() || caller
l!l&&r!l&&!r
2140
MC/DC0%0/2
shift() || caller
shift() caller
Truth table66%2/3
A || B
A= shift()B= caller
ABresult
1X1
011
000
108 16 my $comment_char = shift;
109 16 my $now = scalar localtime;
110  
111 16 ▶ $comment_char = $DEFAULT_COMMENT
Branch: 1 branch
BranchTrueFalseTotal
unless defined $comment_char 9 7 16
112 unless defined $comment_char;
113  
114 16 my $header_comment = <<"HEADER_COMMENT";
115 ${comment_char}
116 ${comment_char} Created by $producer
117 ${comment_char} Created on $now
118 ${comment_char}
119 HEADER_COMMENT
120  
121 # Any additional stuff passed in
122 16 for my $additional_comment (@_) {
123 1 $header_comment .= "${comment_char} ${additional_comment}\n";
124 }
125  
126 16 return $header_comment;
127 }
128  
129 14287 ▶sub parse_list_arg {
Subroutine parse_list_arg: called CC=4 SCAR=13.9
Pod documented
130 14287 ▶ my $list = UNIVERSAL::isa($_[0], 'ARRAY') ? shift : [@_];
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], 'ARRAY') ? : 3822 10465 14287
131  
132 #
133 # This protects stringification of references.
134 #
135 14287 ▶ if (any { ref $_ } @$list) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { ref $_; } , @$list)) { } 449 13838 14287
136 449 return $list;
137 }
138 #
139 # This processes string-like arguments.
140 #
141 else {
142 return [
143 3287 map { s/^\s+|\s+$//g; $_ }
144 3238 map { split /,/ }
145 13838 ▶ grep { defined && length } @$list
Condition100%3/3
defined $_ && length $_
!ll&&!rl&&r
282573238
MC/DC100%2/2
defined $_ && length $_
defined $_ length $_
Truth table100%3/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
146 ];
147 }
148 }
149  
150 132 ▶sub truncate_id_uniquely {
Subroutine truncate_id_uniquely: called CC=3 SCAR=11.0
Pod documented
151 132 my ($desired_name, $max_symbol_length) = @_;
152  
153 132 ▶ return $desired_name
Branch: 1 branch
BranchTrueFalseTotal
unless defined $desired_name and length $desired_name > $max_symbol_length 117 15 132
Condition66%2/3
defined $desired_name and length $desired_name > $max_symbol_length
!ll&&!rl&&r
011715
MC/DC50%1/2
defined $desired_name and length $desired_name > $max_symbol_length
defined $desired_name length $desired_name > $max_symbol_length
Truth table66%2/3
A and B
A= defined $desired_nameB= length $desired_name > $max_symbol_length
ABresult
0X0
100
111
154 unless defined $desired_name && length $desired_name > $max_symbol_length;
155  
156 15 my $truncated_name = substr $desired_name, 0, $max_symbol_length - COLLISION_TAG_LENGTH - 1;
157  
158 # Hex isn't the most space-efficient, but it skirts around allowed
159 # charset issues
160 15 my $digest = sha1_hex($desired_name);
161 15 my $collision_tag = substr $digest, 0, COLLISION_TAG_LENGTH;
162  
163 15 return $truncated_name . '_' . $collision_tag;
164 }
165  
166 65 ▶sub parse_mysql_version {
Subroutine parse_mysql_version: called CC=10 SCAR=23.0
Pod documented
167 65 my ($v, $target) = @_;
168  
169 65 ▶ return undef unless $v;
Branch: 1 branch
BranchTrueFalseTotal
unless $v 50 15 65
170  
171 15 ▶ $target ||= 'perl';
Condition100%2/2
$target ||= "perl"
l!l
141
MC/DC100%1/1
$target ||= "perl"
$target
Truth table100%2/2
A
A= $target
Aresult
11
00
172  
173 15 my @vers;
174  
175 # X.Y.Z style
176 15 ▶ if ($v =~ / ^ (\d+) \. (\d{1,3}) (?: \. (\d{1,3}) )? $ /x) {
Branch: 3 branches
BranchTrueFalseTotal
if ($v =~ / ^ (\d+) \. (\d{1,3}) (?: \. (\d{1,3}) )? $ /x) { } 8 7 15
elsif ($v =~ / ^ (\d) (\d{2}) (\d{2}) $ /x) { } 4 3 7
elsif ($v =~ / ^ (\d+) (?: \. (\d{3}) (\d{3}) )? $ /x) { } 2 1 3
177 8 push @vers, $1, $2, $3;
178 }
179  
180 # XYYZZ (mysql) style
181 elsif ($v =~ / ^ (\d) (\d{2}) (\d{2}) $ /x) {
182 4 push @vers, $1, $2, $3;
183 }
184  
185 # XX.YYYZZZ (perl) style or simply X
186 elsif ($v =~ / ^ (\d+) (?: \. (\d{3}) (\d{3}) )? $ /x) {
187 2 push @vers, $1, $2, $3;
188 } else {
189 #how do I croak sanely here?
190 1 die "Unparseable MySQL version '$v'";
191 }
192  
193 14 ▶ if ($target eq 'perl') {
Branch: 2 branches
BranchTrueFalseTotal
if ($target eq 'perl') { } 7 7 14
elsif ($target eq 'mysql') { } 7 0 7
194 7 ▶ return sprintf('%d.%03d%03d', map { $_ || 0 } (@vers));
Condition100%2/2
$_ || 0
l!l
156
MC/DC100%1/1
$_ || 0
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
195 } elsif ($target eq 'mysql') {
196 7 ▶ return sprintf('%d%02d%02d', map { $_ || 0 } (@vers));
Condition100%2/2
$_ || 0
l!l
174
MC/DC100%1/1
$_ || 0
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
197 } else {
198 #how do I croak sanely here?
199 0 die "Unknown version target '$target'";
200 }
201 }
202  
203 18 ▶sub parse_dbms_version {
Subroutine parse_dbms_version: called CC=7 SCAR=36.8
Pod documented
204 18 my ($v, $target) = @_;
205  
206 18 ▶ return undef unless $v;
Branch: 1 branch
BranchTrueFalseTotal
unless $v 18 0 18
207  
208 0 my @vers;
209  
210 # X.Y.Z style
211 0 ▶ if ($v =~ / ^ (\d+) \. (\d{1,3}) (?: \. (\d{1,3}) )? $ /x) {
Branch: 2 branches
BranchTrueFalseTotal
if ($v =~ / ^ (\d+) \. (\d{1,3}) (?: \. (\d{1,3}) )? $ /x) { } 0 0 0
elsif ($v =~ / ^ (\d+) (?: \. (\d{3}) (\d{3}) )? $ /x) { } 0 0 0
212 0 push @vers, $1, $2, $3;
213 }
214  
215 # XX.YYYZZZ (perl) style or simply X
216 elsif ($v =~ / ^ (\d+) (?: \. (\d{3}) (\d{3}) )? $ /x) {
217 0 push @vers, $1, $2, $3;
218 } else {
219 #how do I croak sanely here?
220 0 die "Unparseable database server version '$v'";
221 }
222  
223 0 ▶ if ($target eq 'perl') {
Branch: 2 branches
BranchTrueFalseTotal
if ($target eq 'perl') { } 0 0 0
elsif ($target eq 'native') { } 0 0 0
224 0 ▶ return sprintf('%d.%03d%03d', map { $_ || 0 } (@vers));
Condition0%0/2
$_ || 0
l!l
00
MC/DC0%0/1
$_ || 0
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
225 } elsif ($target eq 'native') {
226 0 return join '.' => grep defined, @vers;
227 } else {
228 #how do I croak sanely here?
229 0 die "Unknown version target '$target'";
230 }
231 }
232  
233 #my ($parsers_libdir, $checkout_dir);
234 73 ▶sub ddl_parser_instance {
Subroutine ddl_parser_instance: called CC=4 SCAR=13.9
Pod undocumented
235  
236 73 my $type = shift;
237  
238 # it may differ from our caller, even though currently this is not the case
239 73 ▶ eval "require SQL::Translator::Parser::$type"
Branch: 1 branch
BranchTrueFalseTotal
eval "require SQL::Translator::Parser::$type" or die "Unable to load grammar-spec container SQL::Translator::Parser::${type}:\n$@" 73 0 73
240 or die "Unable to load grammar-spec container SQL::Translator::Parser::$type:\n$@";
241  
242 # handle DB2 in a special way, since the grammar source was lost :(
243 73 ▶ if ($type eq 'DB2') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "DB2") 2 71 73
244 2 require SQL::Translator::Parser::DB2::Grammar;
245 2 return SQL::Translator::Parser::DB2::Grammar->new;
246 }
247  
248 71 require Parse::RecDescent;
249 71 return Parse::RecDescent->new(do {
250 83 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
251 71 ▶ ${"SQL::Translator::Parser::${type}::GRAMMAR"}
Condition50%1/2
${"SQL::Translator::Parser::${type}::GRAMMAR";} || die("No \$SQL::Translator::Parser::${type}::GRAMMAR defined, unable to instantiate PRD parser\n")
l!l
710
MC/DC0%0/1
${"SQL::Translator::Parser::${type}::GRAMMAR";} || die("No \$SQL::Translator::Parser::${type}::GRAMMAR defined, unable to instantiate PRD parser\n")
${"SQL::Translator::Parser::${type}::GRAMMAR";}
Truth table50%1/2
A
A= ${"SQL::Translator::Parser::${type}::GRAMMAR";}
Aresult
11
00
252 || die "No \$SQL::Translator::Parser::${type}::GRAMMAR defined, unable to instantiate PRD parser\n";
253 });
254  
255 # this is disabled until RT#74593 is resolved
256  
257 =begin sadness
258  
259 unless ($parsers_libdir) {
260  
261 # are we in a checkout?
262 if ($checkout_dir = _find_co_root()) {
263 $parsers_libdir = File::Spec->catdir($checkout_dir, 'share', 'PrecompiledParsers');
264 }
265 else {
266 require File::ShareDir;
267 $parsers_libdir = File::Spec->catdir(
268 File::ShareDir::dist_dir('SQL-Translator'),
269 'PrecompiledParsers'
270 );
271 }
272  
273 unshift @INC, $parsers_libdir;
274 }
275  
276 my $precompiled_mod = "Parse::RecDescent::DDL::SQLT::$type";
277  
278 # FIXME FIXME FIXME
279 # Parse::RecDescent has horrible architecture where each precompiled parser
280 # instance shares global state with all its siblings
281 # What we do here is gross, but scarily efficient - the parser compilation
282 # is much much slower than an unload/reload cycle
283 require Class::Unload;
284 Class::Unload->unload($precompiled_mod);
285  
286 # There is also a sub-namespace that P::RD uses, but simply unsetting
287 # $^W to stop redefine warnings seems to be enough
288 #Class::Unload->unload("Parse::RecDescent::$precompiled_mod");
289  
290 eval "local \$^W; require $precompiled_mod" or do {
291 if ($checkout_dir) {
292 die "Unable to find precompiled grammar for $type - run Makefile.PL to generate it\n";
293 }
294 else {
295 die "Unable to load precompiled grammar for $type... this is not supposed to happen if you are not in a checkout, please file a bugreport:\n$@"
296 }
297 };
298  
299 my $grammar_spec_fn = $INC{"SQL/Translator/Parser/$type.pm"};
300 my $precompiled_fn = $INC{"Parse/RecDescent/DDL/SQLT/$type.pm"};
301  
302 if (
303 (stat($grammar_spec_fn))[9]
304 >
305 (stat($precompiled_fn))[9]
306 ) {
307 die (
308 "Grammar spec '$grammar_spec_fn' is newer than precompiled parser '$precompiled_fn'"
309 . ($checkout_dir
310 ? " - run Makefile.PL to regenerate stale versions\n"
311 : "... this is not supposed to happen if you are not in a checkout, please file a bugreport\n"
312 )
313 );
314 }
315  
316 return $precompiled_mod->new;
317  
318 =end sadness
319  
320 =cut
321  
322 }
323  
324 # Try to determine the root of a checkout/untar if possible
325 # or return undef
326 0 ▶sub _find_co_root {
Subroutine _find_co_root: not called CC=4 SCAR=30.0
327  
328 0 my @mod_parts = split /::/, (__PACKAGE__ . '.pm');
329 0 my $rel_path = join('/', @mod_parts); # %INC stores paths with / regardless of OS
330  
331 0 ▶ return undef unless ($INC{$rel_path});
Branch: 1 branch
BranchTrueFalseTotal
unless $INC{$rel_path} 0 0 0
332  
333 # a bit convoluted, but what we do here essentially is:
334 # - get the file name of this particular module
335 # - do 'cd ..' as many times as necessary to get to lib/SQL/Translator/../../..
336  
337 0 my $root = (File::Spec::Unix->splitpath($INC{$rel_path}))[1];
338 0 for (1 .. @mod_parts) {
339 0 $root = File::Spec->catdir($root, File::Spec->updir);
340 }
341  
342 0 ▶ return (-f File::Spec->catfile($root, 'Makefile.PL'))
Branch: 1 branch
BranchTrueFalseTotal
-f 'File::Spec'->catfile($root, 'Makefile.PL') ? : 0 0 0
343 ? $root
344 : undef;
345 }
346  
347 {
348  
349 package SQL::Translator::Utils::Error;
350  
351 use overload
352 24 ▶ '""' => sub { ${ $_[0] } },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
353 83 fallback => 1;
354  
355 24 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
356 24 my ($class, $msg) = @_;
357 24 bless \$msg, $class;
358 }
359 }
360  
361 83 ▶sub uniq {
Subroutine uniq: called CC=2 SCAR=7.0
Pod undocumented
362 937 my (%seen, $seen_undef, $numeric_preserving_copy);
363 937 ▶ grep { not(defined $_ ? $seen{ $numeric_preserving_copy = $_ }++ : $seen_undef++) } @_;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 1001 0 1001
364 }
365  
366 24 ▶sub throw {
Subroutine throw: called CC=1 SCAR=0.0
Pod documented
367 24 die SQL::Translator::Utils::Error->new($_[0]);
368 }
369  
370 48694 ▶sub ex2err {
Subroutine ex2err: called CC=1 SCAR=0.0
Pod documented
371 48694 my ($orig, $self, @args) = @_;
372 48694 ▶ return try {
Subroutine __ANON__: called CC=1 SCAR=0.0
373 48694 $self->$orig(@args);
374 25 ▶ } catch {
Subroutine __ANON__: called CC=3 SCAR=11.1
375 25 ▶ die $_ unless blessed($_) && $_->isa("SQL::Translator::Utils::Error");
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($_) and $_->isa("SQL::Translator::Utils::Error") 1 24 25
Condition66%2/3
builtin::blessed($_) and $_->isa("SQL::Translator::Utils::Error")
!ll&&!rl&&r
1024
MC/DC50%1/2
builtin::blessed($_) and $_->isa("SQL::Translator::Utils::Error")
builtin::blessed($_) $_->isa("SQL::Translator::Utils::Error")
Truth table66%2/3
A and B
A= builtin::blessed($_)B= $_->isa("SQL::Translator::Utils::Error")
ABresult
0X0
100
111
376 24 $self->error("$_");
377 48694 };
378 }
379  
380 441 ▶sub carp_ro {
Subroutine carp_ro: called CC=1 SCAR=0.0
Pod documented
381 441 my ($name) = @_;
382 686 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
383 686 my ($orig, $self) = (shift, shift);
384 686 ▶ carp "'$name' is a read-only accessor" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 686 686
385 686 return $self->$orig;
386 441 };
387 }
388  
389 51 ▶sub batch_alter_table_statements {
Subroutine batch_alter_table_statements: called CC=5 SCAR=16.1
Pod documented
390 51 my ($diff_hash, $options, @meths) = @_;
391  
392 51 ▶ @meths = qw(
Branch: 1 branch
BranchTrueFalseTotal
unless @meths 47 4 51
393 rename_table
394 alter_drop_constraint
395 alter_drop_index
396 drop_field
397 add_field
398 alter_field
399 rename_field
400 alter_create_index
401 alter_create_constraint
402 alter_table
403 ) unless @meths;
404  
405 51 my $package = caller;
406  
407 return map {
408 75 ▶ my $meth = $package->can($_) or die "$package cant $_";
Branch: 1 branch
BranchTrueFalseTotal
my $meth = $package->can($_) or die "$package cant $_" 75 0 75
409 75 ▶ map { $meth->(ref $_ eq 'ARRAY' ? @$_ : $_, $options) } @{ $diff_hash->{$_} }
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 36 78 114
410 51 ▶ } grep { @{ $diff_hash->{$_} || [] } } @meths;
Condition100%2/2
$diff_hash->{$_} || []
l!l
42464
MC/DC100%1/1
$diff_hash->{$_} || []
$diff_hash->{$_}
Truth table100%2/2
A
A= $diff_hash->{$_}
Aresult
11
00
411 }
412  
413 83 1;
414  
415 =pod
416  
417 =head1 NAME
418  
419 SQL::Translator::Utils - SQL::Translator Utility functions
420  
421 =head1 SYNOPSIS
422  
423 use SQL::Translator::Utils qw(debug);
424 debug("PKG: Bad things happened");
425  
426 =head1 DESCSIPTION
427  
428 C<SQL::Translator::Utils> contains utility functions designed to be
429 used from the other modules within the C<SQL::Translator> modules.
430  
431 Nothing is exported by default.
432  
433 =head1 EXPORTED FUNCTIONS AND CONSTANTS
434  
435 =head2 debug
436  
437 C<debug> takes 0 or more messages, which will be sent to STDERR using
438 C<warn>. Occurances of the strings I<PKG>, I<SUB>, and I<LINE>
439 will be replaced by the calling package, subroutine, and line number,
440 respectively, as reported by C<caller(1)>.
441  
442 For example, from within C<foo> in F<SQL/Translator.pm>, at line 666:
443  
444 debug("PKG: Error reading file at SUB/LINE");
445  
446 Will warn
447  
448 [SQL::Translator: Error reading file at foo/666]
449  
450 The entire message is enclosed within C<[> and C<]> for visual clarity
451 when STDERR is intermixed with STDOUT.
452  
453 =head2 normalize_name
454  
455 C<normalize_name> takes a string and ensures that it is suitable for
456 use as an identifier. This means: ensure that it starts with a letter
457 or underscore, and that the rest of the string consists of only
458 letters, numbers, and underscores. A string that begins with
459 something other than [a-zA-Z] will be prefixer with an underscore, and
460 all other characters in the string will be replaced with underscores.
461 Finally, a trailing underscore will be removed, because that's ugly.
462  
463 normalize_name("Hello, world");
464  
465 Produces:
466  
467 Hello_world
468  
469 A more useful example, from the C<SQL::Translator::Parser::Excel> test
470 suite:
471  
472 normalize_name("silly field (with random characters)");
473  
474 returns:
475  
476 silly_field_with_random_characters
477  
478 =head2 header_comment
479  
480 Create the header comment. Takes 1 mandatory argument (the producer
481 classname), an optional comment character (defaults to $DEFAULT_COMMENT),
482 and 0 or more additional comments, which will be appended to the header,
483 prefixed with the comment character. If additional comments are provided,
484 then a comment string must be provided ($DEFAULT_COMMENT is exported for
485 this use). For example, this:
486  
487 package My::Producer;
488  
489 use SQL::Translator::Utils qw(header_comment $DEFAULT_COMMENT);
490  
491 print header_comment(__PACKAGE__,
492 $DEFAULT_COMMENT,
493 "Hi mom!");
494  
495 produces:
496  
497 --
498 -- Created by My::Prodcuer
499 -- Created on Fri Apr 25 06:56:02 2003
500 --
501 -- Hi mom!
502 --
503  
504 Note the gratuitous spacing.
505  
506 =head2 parse_list_arg
507  
508 Takes a string, list or arrayref (all of which could contain
509 comma-separated values) and returns an array reference of the values.
510 All of the following will return equivalent values:
511  
512 parse_list_arg('id');
513 parse_list_arg('id', 'name');
514 parse_list_arg( 'id, name' );
515 parse_list_arg( [ 'id', 'name' ] );
516 parse_list_arg( qw[ id name ] );
517  
518 =head2 truncate_id_uniquely
519  
520 Takes a string ($desired_name) and int ($max_symbol_length). Truncates
521 $desired_name to $max_symbol_length by including part of the hash of
522 the full name at the end of the truncated name, giving a high
523 probability that the symbol will be unique. For example,
524  
525 truncate_id_uniquely( 'a' x 100, 64 )
526 truncate_id_uniquely( 'a' x 99 . 'b', 64 );
527 truncate_id_uniquely( 'a' x 99, 64 )
528  
529 Will give three different results; specifically:
530  
531 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_7f900025
532 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_6191e39a
533 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_8cd96af2
534  
535 =head2 $DEFAULT_COMMENT
536  
537 This is the default comment string, '--' by default. Useful for
538 C<header_comment>.
539  
540 =head2 parse_mysql_version
541  
542 Used by both L<Parser::MySQL|SQL::Translator::Parser::MySQL> and
543 L<Producer::MySQL|SQL::Translator::Producer::MySQL> in order to provide a
544 consistent format for both C<< parser_args->{mysql_parser_version} >> and
545 C<< producer_args->{mysql_version} >> respectively. Takes any of the following
546 version specifications:
547  
548 5.0.3
549 4.1
550 3.23.2
551 5
552 5.001005 (perl style)
553 30201 (mysql style)
554  
555 =head2 parse_dbms_version
556  
557 Takes a version string (X.Y.Z) or perl style (XX.YYYZZZ) and a target ('perl'
558 or 'native') transforms the string to the given target style.
559 to
560  
561 =head2 throw
562  
563 Throws the provided string as an object that will stringify back to the
564 original string. This stops it from being mangled by L<Moo>'s C<isa>
565 code.
566  
567 =head2 ex2err
568  
569 Wraps an attribute accessor to catch any exception raised using
570 L</throw> and store them in C<< $self->error() >>, finally returning
571 undef. A reference to this function can be passed directly to
572 L<Moo/around>.
573  
574 around foo => \&ex2err;
575  
576 around bar => sub {
577 my ($orig, $self) = (shift, shift);
578 return ex2err($orig, $self, @_) if @_;
579 ...
580 };
581  
582 =head2 carp_ro
583  
584 Takes a field name and returns a reference to a function can be used
585 L<around|Moo/around> a read-only accessor to make it L<carp|Carp>
586 instead of die when passed an argument.
587  
588 =head2 batch_alter_table_statements
589  
590 Takes diff and argument hashes as passed to
591 L<batch_alter_table|SQL::Translator::Diff/batch_alter_table($table, $hash, $args) (optional)>
592 and an optional list of producer functions to call on the calling package.
593 Returns the list of statements returned by the producer functions.
594  
595 If no producer functions are specified, the following functions in the
596 calling package are called:
597  
598 =over
599  
600 =item 1. rename_table
601  
602 =item 2. alter_drop_constraint
603  
604 =item 3. alter_drop_index
605  
606 =item 4. drop_field
607  
608 =item 5. add_field
609  
610 =item 5. alter_field
611  
612 =item 6. rename_field
613  
614 =item 7. alter_create_index
615  
616 =item 8. alter_create_constraint
617  
618 =item 9. alter_table
619  
620 =back
621  
622 If the corresponding array in the hash has any elements, but the
623 caller doesn't implement that function, an exception is thrown.
624  
625 =head1 AUTHORS
626  
627 Darren Chamberlain E<lt>darren@cpan.orgE<gt>,
628 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
629  
630 =cut
« blib/lib/SQL/Translator/Filter/Globals.pm ↑ Summary blib/lib/SQL/Translator/Producer/SQLite.pm »