blib/lib/SQL/Translator/Producer/MySQL.pm

filter: Statementstmt 96.8% 396 / 409 Branchbran 84.3% 172 / 204 Conditioncond 72.2% 78 / 108 MC/DCmcdc 45.5% 31 / 68 Subroutinesub 100% 32 / 32 Podpod 45.4% 10 / 22 totaltotal 85.2% 719 / 843 CC 161 SCAR 29.9
CC
161
Cov
90%
create_constraint
49.5
create_field
36.3
generate_table_options
13.9
CRAP 19.8
« blib/lib/SQL/Translator.pm ↑ Summary blib/lib/SQL/Translator/Parser/DBI.pm »
1 package SQL::Translator::Producer::MySQL;
2  
3 =head1 NAME
4  
5 SQL::Translator::Producer::MySQL - MySQL-specific producer for SQL::Translator
6  
7 =head1 SYNOPSIS
8  
9 Use via SQL::Translator:
10  
11 use SQL::Translator;
12  
13 my $t = SQL::Translator->new( parser => '...', producer => 'MySQL', '...' );
14 $t->translate;
15  
16 =head1 DESCRIPTION
17  
18 This module will produce text output of the schema suitable for MySQL.
19 There are still some issues to be worked out with syntax differences
20 between MySQL versions 3 and 4 ("SET foreign_key_checks," character sets
21 for fields, etc.).
22  
23 =head1 ARGUMENTS
24  
25 This producer takes a single optional producer_arg C<mysql_version>, which
26 provides the desired version for the target database. By default MySQL v3 is
27 assumed, and statements pertaining to any features introduced in later versions
28 (e.g. CREATE VIEW) are not produced.
29  
30 Valid version specifiers for C<mysql_version> are listed L<here|SQL::Translator::Utils/parse_mysql_version>
31  
32 =head2 Table Types
33  
34 Normally the tables will be created without any explicit table type given and
35 so will use the MySQL default.
36  
37 Any tables involved in foreign key constraints automatically get a table type
38 of InnoDB, unless this is overridden by setting the C<mysql_table_type> extra
39 attribute explicitly on the table.
40  
41 =head2 Extra attributes.
42  
43 The producer recognises the following extra attributes on the Schema objects.
44  
45 =over 4
46  
47 =item B<field.list>
48  
49 Set the list of allowed values for Enum fields.
50  
51 =item B<field.binary>, B<field.unsigned>, B<field.zerofill>
52  
53 Set the MySQL field options of the same name.
54  
55 =item B<field.renamed_from>, B<table.renamed_from>
56  
57 Use when producing diffs to indicate that the current table/field has been
58 renamed from the old name as given in the attribute value.
59  
60 =item B<table.mysql_table_type>
61  
62 Set the type of the table e.g. 'InnoDB', 'MyISAM'. This will be
63 automatically set for tables involved in foreign key constraints if it is
64 not already set explicitly. See L<"Table Types">.
65  
66 Please note that the C<ENGINE> option is the preferred method of specifying
67 the MySQL storage engine to use, but this method still works for backwards
68 compatibility.
69  
70 =item B<table.mysql_charset>, B<table.mysql_collate>
71  
72 Set the tables default character set and collation order.
73  
74 =item B<field.mysql_charset>, B<field.mysql_collate>
75  
76 Set the fields character set and collation order.
77  
78 =back
79  
80 =cut
81  
82 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 12 our ($DEBUG, %used_names);
85 12 our $VERSION = '1.66';
86 12 ▶$DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $SQL::Translator::Producer::MySQL::DEBUG 12 0 12
87  
88 # Maximum length for most identifiers is 64, according to:
89 # http://dev.mysql.com/doc/refman/4.1/en/identifiers.html
90 # http://dev.mysql.com/doc/refman/5.0/en/identifiers.html
91 12 my $DEFAULT_MAX_ID_LENGTH = 64;
92  
93 12 ▶use base qw(SQL::Translator::Producer);
Subroutine BEGIN: called CC=1 SCAR=0.0
94 12 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
95 12 ▶use SQL::Translator::Schema::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
96 12 ▶use SQL::Translator::Generator::DDL::MySQL;
Subroutine BEGIN: called CC=1 SCAR=0.0
97 12 ▶use SQL::Translator::Utils qw(debug header_comment
Subroutine BEGIN: called CC=1 SCAR=0.0
98 truncate_id_uniquely parse_mysql_version
99 batch_alter_table_statements
100 normalize_quote_options
101 12 );
102  
103 #
104 # Use only lowercase for the keys (e.g. "long" and not "LONG")
105 #
106 12 my %translate = (
107 #
108 # Oracle types
109 #
110 varchar2 => 'varchar',
111 long => 'text',
112 clob => 'longtext',
113  
114 #
115 # Sybase types
116 #
117 int => 'integer',
118 money => 'float',
119 real => 'double',
120 comment => 'text',
121 bit => 'tinyint',
122  
123 #
124 # Access types
125 #
126 'long integer' => 'integer',
127 'text' => 'text',
128 'datetime' => 'datetime',
129  
130 #
131 # PostgreSQL types
132 #
133 bytea => 'BLOB',
134 );
135  
136 #
137 # Column types that do not support length attribute
138 #
139 12 my @no_length_attr = qw/
140 date time timestamp datetime year
141 /;
142  
143 50 ▶sub preprocess_schema {
Subroutine preprocess_schema: called CC=13 SCAR=25.7
Pod undocumented
144 50 my ($schema) = @_;
145  
146 # extra->{mysql_table_type} used to be the type. It belongs in options, so
147 # move it if we find it. Return Engine type if found in extra or options
148 # Similarly for mysql_charset and mysql_collate
149 599 ▶ my $extra_to_options = sub {
Subroutine __ANON__: called CC=9 SCAR=22.2
150 599 my ($table, $extra_name, $opt_name) = @_;
151  
152 599 my $extra = $table->extra;
153  
154 599 my $extra_type = delete $extra->{$extra_name};
155  
156 # Now just to find if there is already an Engine or Type option...
157 # and lets normalize it to ENGINE since:
158 #
159 # The ENGINE table option specifies the storage engine for the table.
160 # TYPE is a synonym, but ENGINE is the preferred option name.
161 #
162  
163 599 my $options = $table->options;
164  
165 # If multiple option names, normalize to the first one
166 599 ▶ if (ref $opt_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opt_name) 303 296 599
167 303 OPT_NAME: for (@$opt_name[ 1 .. $#$opt_name ]) {
168 303 for my $idx (0 .. $#{$options}) {
169 263 my ($key, $value) = %{ $options->[$idx] };
170  
171 263 ▶ if (uc $key eq $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (uc $key eq $_) 0 263 263
172 0 $options->[$idx] = { $opt_name->[0] => $value };
173 0 last OPT_NAME;
174 }
175 }
176 }
177 303 $opt_name = $opt_name->[0];
178  
179 }
180  
181 # This assumes that there isn't both a Type and an Engine option.
182 OPTION:
183 599 for my $idx (0 .. $#{$options}) {
184 453 my ($key, $value) = %{ $options->[$idx] };
185  
186 453 ▶ next unless uc $key eq $opt_name;
Branch: 1 branch
BranchTrueFalseTotal
unless uc $key eq $opt_name 214 239 453
187  
188 # make sure case is right on option name
189 239 delete $options->[$idx]{$key};
190 239 ▶ return $options->[$idx]{$opt_name} = $value || $extra_type;
Condition33%1/3
$value || $extra_type
l!l&&r!l&&!r
23900
MC/DC0%0/2
$value || $extra_type
$value $extra_type
Truth table33%1/3
A || B
A= $valueB= $extra_type
ABresult
1X1
011
000
191  
192 }
193  
194 360 ▶ if ($extra_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extra_type) 8 352 360
195 8 push @$options, { $opt_name => $extra_type };
196 8 return $extra_type;
197 }
198  
199 50 };
200  
201 # Names are only specific to a given schema
202 50 local %used_names = ();
203  
204 #
205 # Work out which tables need to be InnoDB to support foreign key
206 # constraints. We do this first as we need InnoDB at both ends.
207 #
208 50 foreach my $table ($schema->get_tables) {
209  
210 148 $extra_to_options->($table, 'mysql_table_type', [ 'ENGINE', 'TYPE' ]);
211 148 $extra_to_options->($table, 'mysql_charset', 'CHARACTER SET');
212 148 $extra_to_options->($table, 'mysql_collate', 'COLLATE');
213  
214 148 foreach my $c ($table->get_constraints) {
215 291 ▶ next unless $c->type eq FOREIGN_KEY;
Branch: 1 branch
BranchTrueFalseTotal
unless $c->type eq "FOREIGN KEY" 201 90 291
216  
217 # Normalize constraint names here.
218 90 my $c_name = $c->name;
219  
220 # Give the constraint a name if it doesn't have one, so it doesn't feel
221 # left out
222 90 ▶ $c_name = $table->name . '_fk' unless length $c_name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $c_name 14 76 90
223  
224 90 $c->name(next_unused_name($c_name));
225  
226 90 for my $meth (qw/table reference_table/) {
227 180 ▶ my $table = $schema->get_table($c->$meth) || next;
Condition100%2/2
$schema->get_table($c->$meth) || (next)
l!l
15525
MC/DC100%1/1
$schema->get_table($c->$meth) || (next)
$schema->get_table($c->$meth)
Truth table100%2/2
A
A= $schema->get_table($c->$meth)
Aresult
11
00
228  
229 # This normalizes the types to ENGINE and returns the value if its there
230 next
231 155 ▶ if $extra_to_options->($table, 'mysql_table_type', [ 'ENGINE', 'TYPE' ]);
Branch: 1 branch
BranchTrueFalseTotal
if &$extra_to_options($table, "mysql_table_type", ["ENGINE", "TYPE"]) 143 12 155
232 12 $table->options({ 'ENGINE' => 'InnoDB' });
233 }
234 } # foreach constraints
235  
236 148 my %map = (mysql_collate => 'collate', mysql_charset => 'character set');
237 148 foreach my $f ($table->get_fields) {
238 492 my $extra = $f->extra;
239 492 for (keys %map) {
240 $extra->{ $map{$_} } = delete $extra->{$_}
241 984 ▶ if exists $extra->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $extra->{$_} 3 981 984
242 }
243  
244 492 my @size = $f->size;
245 492 ▶ if (!$size[0] && $f->data_type =~ /char$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $size[0] and $f->data_type =~ /char$/) 10 482 492
Condition100%3/3
not $size[0] and $f->data_type =~ /char$/
!ll&&!rl&&r
37211010
MC/DC100%2/2
not $size[0] and $f->data_type =~ /char$/
not $size[0] $f->data_type =~ /char$/
Truth table100%3/3
not(A) and B
A= not $size[0]B= $f->data_type =~ /char$/
ABresult
0X0
100
111
246 10 $f->size((255));
247 }
248 }
249  
250 }
251 }
252  
253 {
254 12 my ($quoting_generator, $nonquoting_generator);
255  
256 824 ▶ sub _generator {
Subroutine _generator: called CC=5 SCAR=16.3
257 824 my $options = shift;
258 824 ▶ return $options->{generator} if exists $options->{generator};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"generator"} 530 294 824
259  
260 294 ▶ return normalize_quote_options($options)
Branch: 1 branch
BranchTrueFalseTotal
normalize_quote_options($options) ? : 91 203 294
Condition66%4/6
normalize_quote_options($options) ? $quoting_generator ||= "SQL::Translator::Generator::DDL::MySQL"->new : $nonquoting_generator ||= "SQL::Translator::Generator::DDL::MySQL"->new("quote_chars", [])
l!l&&r!l&&!r
37250
$quoting_generator ||= "SQL::Translator::Generator::DDL::MySQL"->new
l!l&&r!l&&!r
8380
MC/DC0%0/2
normalize_quote_options($options) ? $quoting_generator ||= "SQL::Translator::Generator::DDL::MySQL"->new : $nonquoting_generator ||= "SQL::Translator::Generator::DDL::MySQL"->new("quote_chars", [])
normalize_quote_options($options) ? $quoting_generator ||= "SQL::Translator::Generator::DDL::MySQL"->new : $nonquoting_generator "SQL::Translator::Generator::DDL::MySQL"->new("quote_chars", [])
MC/DC0%0/2
$quoting_generator ||= "SQL::Translator::Generator::DDL::MySQL"->new
$quoting_generator "SQL::Translator::Generator::DDL::MySQL"->new
Truth table66%2/3
A ||= B
A= normalize_quote_options($options) ? $quoting_generator ||= "SQL::Translator::Generator::DDL::MySQL"->new : $nonquoting_generatorB= "SQL::Translator::Generator::DDL::MySQL"->new("quote_chars", [])
ABresult
1X1
011
000
Truth table66%2/3
A ||= B
A= $quoting_generatorB= "SQL::Translator::Generator::DDL::MySQL"->new
ABresult
1X1
011
000
261 ? $quoting_generator ||= SQL::Translator::Generator::DDL::MySQL->new()
262 : $nonquoting_generator ||= SQL::Translator::Generator::DDL::MySQL->new(quote_chars => [],);
263 }
264 }
265  
266 12 ▶sub produce {
Subroutine produce: called CC=13 SCAR=25.7
Pod documented
267 22 my $translator = shift;
268 22 local $DEBUG = $translator->debug;
269 22 local %used_names;
270 22 my $no_comments = $translator->no_comments;
271 22 my $add_drop_table = $translator->add_drop_table;
272 22 my $schema = $translator->schema;
273 22 ▶ my $show_warnings = $translator->show_warnings || 0;
Condition100%2/2
$translator->show_warnings || 0
l!l
715
MC/DC100%1/1
$translator->show_warnings || 0
$translator->show_warnings
Truth table100%2/2
A
A= $translator->show_warnings
Aresult
11
00
274 22 my $producer_args = $translator->producer_args;
275 22 ▶ my $mysql_version = parse_mysql_version($producer_args->{mysql_version}, 'perl') || 0;
Condition100%2/2
parse_mysql_version($producer_args->{'mysql_version'}, 'perl') || 0
l!l
220
MC/DC100%1/1
parse_mysql_version($producer_args->{'mysql_version'}, 'perl') || 0
parse_mysql_version($producer_args->{'mysql_version'}, 'perl')
Truth table100%2/2
A
A= parse_mysql_version($producer_args->{'mysql_version'}, 'perl')
Aresult
11
00
276 22 ▶ my $max_id_length = $producer_args->{mysql_max_id_length} || $DEFAULT_MAX_ID_LENGTH;
Condition33%1/3
$producer_args->{'mysql_max_id_length'} || $DEFAULT_MAX_ID_LENGTH
l!l&&r!l&&!r
0220
MC/DC0%0/2
$producer_args->{'mysql_max_id_length'} || $DEFAULT_MAX_ID_LENGTH
$producer_args->{'mysql_max_id_length'} $DEFAULT_MAX_ID_LENGTH
Truth table33%1/3
A || B
A= $producer_args->{'mysql_max_id_length'}B= $DEFAULT_MAX_ID_LENGTH
ABresult
1X1
011
000
277  
278 22 my $generator = _generator({ quote_identifiers => $translator->quote_identifiers });
279  
280 22 debug("PKG: Beginning production\n");
281 22 %used_names = ();
282 22 my $create = '';
283 22 ▶ $create .= header_comment unless ($no_comments);
Branch: 1 branch
BranchTrueFalseTotal
unless $no_comments 3 19 22
284  
285 # \todo Don't set if MySQL 3.x is set on command line
286 22 my @create = "SET foreign_key_checks=0";
287  
288 22 preprocess_schema($schema);
289  
290 #
291 # Generate sql
292 #
293 22 my @table_defs = ();
294  
295 22 for my $table ($schema->get_tables) {
296  
297 # print $table->name, "\n";
298 60 push @table_defs,
299 create_table(
300 $table,
301 {
302 add_drop_table => $add_drop_table,
303 show_warnings => $show_warnings,
304 no_comments => $no_comments,
305 generator => $generator,
306 max_id_length => $max_id_length,
307 mysql_version => $mysql_version
308 }
309 );
310 }
311  
312 22 ▶ if ($mysql_version >= 5.000001) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mysql_version >= "5.000001") 2 20 22
313 2 for my $view ($schema->get_views) {
314 2 push @table_defs,
315 create_view(
316 $view,
317 {
318 add_replace_view => $add_drop_table,
319 show_warnings => $show_warnings,
320 no_comments => $no_comments,
321 generator => $generator,
322 max_id_length => $max_id_length,
323 mysql_version => $mysql_version
324 }
325 );
326 }
327 }
328  
329 22 ▶ if ($mysql_version >= 5.000002) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mysql_version >= "5.000002") 2 20 22
330 2 for my $trigger ($schema->get_triggers) {
331 4 push @table_defs,
332 create_trigger(
333 $trigger,
334 {
335 add_drop_trigger => $add_drop_table,
336 show_warnings => $show_warnings,
337 no_comments => $no_comments,
338 generator => $generator,
339 max_id_length => $max_id_length,
340 mysql_version => $mysql_version
341 }
342 );
343 }
344 }
345  
346 # print "@table_defs\n";
347 22 push @table_defs, "SET foreign_key_checks=1";
348  
349 return wantarray
350 ? ($create ? $create : (), @create, @table_defs)
351 22 ▶ : ($create . join('', map { $_ ? "$_;\n\n" : () } (@create, @table_defs)));
Branch: 3 branches
BranchTrueFalseTotal
wantarray ? : 10 12 22
$create ? : 1 9 10
$_ ? : 105 0 105
352 }
353  
354 6 ▶sub create_trigger {
Subroutine create_trigger: called CC=8 SCAR=20.8
Pod documented
355 6 my ($trigger, $options) = @_;
356 6 my $generator = _generator($options);
357  
358 6 my $trigger_name = $trigger->name;
359 6 debug("PKG: Looking at trigger '${trigger_name}'\n");
360  
361 6 my @statements;
362  
363 6 my $events = $trigger->database_events;
364 6 for my $event (@$events) {
365 8 my $name = $trigger_name;
366 8 ▶ if (@$events > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$events > 1) 4 4 8
367 4 $name .= "_$event";
368  
369 warn
370 "Multiple database events supplied for trigger '${trigger_name}', ",
371 "creating trigger '${name}' for the '${event}' event\n"
372 4 ▶ if $options->{show_warnings};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"show_warnings"} 0 4 4
373 }
374  
375 8 my $action = $trigger->action;
376 8 ▶ if ($action !~ /^ \s* BEGIN [\s\;] .*? [\s\;] END [\s\;]* $/six) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($action =~ /^ \s* BEGIN [\s\;] .*? [\s\;] END [\s\;]* $/six) 7 1 8
377 7 ▶ $action .= ";" unless $action =~ /;\s*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $action =~ /;\s*\z/ 1 6 7
378 7 $action = "BEGIN $action END";
379 }
380  
381 push @statements, "DROP TRIGGER IF EXISTS " . $generator->quote($name)
382 8 ▶ if $options->{add_drop_trigger};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"add_drop_trigger"} 6 2 8
383 8 push @statements,
384 sprintf(
385 "CREATE TRIGGER %s %s %s ON %s\n FOR EACH ROW %s",
386 $generator->quote($name),
387 $trigger->perform_action_when,
388 $event, $generator->quote($trigger->on_table), $action,
389 );
390  
391 }
392  
393 # Tack the comment onto the first statement
394 $statements[0] = "--\n-- Trigger " . $generator->quote($trigger_name) . "\n--\n" . $statements[0]
395 6 ▶ unless $options->{no_comments};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"no_comments"} 2 4 6
396 6 return @statements;
397 }
398  
399 6 ▶sub create_view {
Subroutine create_view: called CC=13 SCAR=26.1
Pod documented
400 6 my ($view, $options) = @_;
401 6 my $generator = _generator($options);
402  
403 6 my $view_name = $view->name;
404 6 my $view_name_qt = $generator->quote($view_name);
405  
406 6 debug("PKG: Looking at view '${view_name}'\n");
407  
408 # Header. Should this look like what mysqldump produces?
409 6 my $create = '';
410 $create .= "--\n-- View: $view_name_qt\n--\n"
411 6 ▶ unless $options->{no_comments};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"no_comments"} 0 6 6
412 6 $create .= 'CREATE';
413 6 ▶ $create .= ' OR REPLACE' if $options->{add_replace_view};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"add_replace_view"} 4 2 6
414 6 $create .= "\n";
415  
416 6 my $extra = $view->extra;
417  
418 # ALGORITHM
419 6 ▶ if (exists($extra->{mysql_algorithm})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $extra->{"mysql_algorithm"} and defined(my $algorithm = $extra->{"mysql_algorithm"})) 2 4 6
Condition66%2/3
exists $extra->{"mysql_algorithm"} and defined(my $algorithm = $extra->{"mysql_algorithm"})
!ll&&!rl&&r
402
MC/DC50%1/2
exists $extra->{"mysql_algorithm"} and defined(my $algorithm = $extra->{"mysql_algorithm"})
exists $extra->{"mysql_algorithm"} defined(my $algorithm = $extra->{"mysql_algorithm"})
Truth table66%2/3
A and B
A= exists $extra->{"mysql_algorithm"}B= defined(my $algorithm = $extra->{"mysql_algorithm"})
ABresult
0X0
100
111
420 && defined(my $algorithm = $extra->{mysql_algorithm})) {
421 2 ▶ $create .= " ALGORITHM = ${algorithm}\n"
Branch: 1 branch
BranchTrueFalseTotal
if $algorithm =~ /(?:UNDEFINED|MERGE|TEMPTABLE)/i 2 0 2
422 if $algorithm =~ /(?:UNDEFINED|MERGE|TEMPTABLE)/i;
423 }
424  
425 # DEFINER
426 6 ▶ if (exists($extra->{mysql_definer})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $extra->{"mysql_definer"} and defined(my $user = $extra->{"mysql_definer"})) 2 4 6
Condition66%2/3
exists $extra->{"mysql_definer"} and defined(my $user = $extra->{"mysql_definer"})
!ll&&!rl&&r
402
MC/DC50%1/2
exists $extra->{"mysql_definer"} and defined(my $user = $extra->{"mysql_definer"})
exists $extra->{"mysql_definer"} defined(my $user = $extra->{"mysql_definer"})
Truth table66%2/3
A and B
A= exists $extra->{"mysql_definer"}B= defined(my $user = $extra->{"mysql_definer"})
ABresult
0X0
100
111
427 && defined(my $user = $extra->{mysql_definer})) {
428 2 $create .= " DEFINER = ${user}\n";
429 }
430  
431 # SECURITY
432 6 ▶ if (exists($extra->{mysql_security})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $extra->{"mysql_security"} and defined(my $security = $extra->{"mysql_security"})) 2 4 6
Condition66%2/3
exists $extra->{"mysql_security"} and defined(my $security = $extra->{"mysql_security"})
!ll&&!rl&&r
402
MC/DC50%1/2
exists $extra->{"mysql_security"} and defined(my $security = $extra->{"mysql_security"})
exists $extra->{"mysql_security"} defined(my $security = $extra->{"mysql_security"})
Truth table66%2/3
A and B
A= exists $extra->{"mysql_security"}B= defined(my $security = $extra->{"mysql_security"})
ABresult
0X0
100
111
433 && defined(my $security = $extra->{mysql_security})) {
434 2 ▶ $create .= " SQL SECURITY ${security}\n"
Branch: 1 branch
BranchTrueFalseTotal
if $security =~ /(?:DEFINER|INVOKER)/i 2 0 2
435 if $security =~ /(?:DEFINER|INVOKER)/i;
436 }
437  
438 #Header, cont.
439 6 $create .= " VIEW $view_name_qt";
440  
441 6 ▶ if (my @fields = $view->fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@fields) = $view->fields) 6 0 6
442 6 my $list = join ', ', map { $generator->quote($_) } @fields;
443 6 $create .= " ( ${list} )";
444 }
445 6 ▶ if (my $sql = $view->sql) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sql = $view->sql) 6 0 6
446  
447 # do not wrap parenthesis around the selector, mysql doesn't like this
448 # http://bugs.mysql.com/bug.php?id=9198
449 6 $create .= " AS\n ${sql}\n";
450 }
451  
452 # $create .= "";
453 6 return $create;
454 }
455  
456 60 ▶sub create_table {
Subroutine create_table: called CC=12 SCAR=24.8
Pod documented
457 60 my ($table, $options) = @_;
458 60 my $generator = _generator($options);
459  
460 60 my $table_name = $generator->quote($table->name);
461 60 debug("PKG: Looking at table '$table_name'\n");
462  
463 #
464 # Header. Should this look like what mysqldump produces?
465 #
466 60 my $create = '';
467 60 my $drop;
468 60 ▶ $create .= "--\n-- Table: $table_name\n--\n" unless $options->{no_comments};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"no_comments"} 9 51 60
469 60 ▶ $drop = qq[DROP TABLE IF EXISTS $table_name] if $options->{add_drop_table};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"add_drop_table"} 36 24 60
470 60 $create .= "CREATE TABLE $table_name (\n";
471  
472 #
473 # Fields
474 #
475 60 my @field_defs;
476 60 for my $field ($table->get_fields) {
477 224 push @field_defs, create_field($field, $options);
478 }
479  
480 #
481 # Indices
482 #
483 60 my @index_defs;
484 my %indexed_fields;
485 60 for my $index ($table->get_indices) {
486 46 push @index_defs, create_index($index, $options);
487 46 $indexed_fields{$_} = 1 for $index->fields;
488 }
489  
490 #
491 # Constraints -- need to handle more than just FK. -ky
492 #
493 60 my @constraint_defs;
494 60 my @constraints = $table->get_constraints;
495 60 for my $c (@constraints) {
496 134 my $constr = create_constraint($c, $options);
497 134 ▶ push @constraint_defs, $constr if ($constr);
Branch: 1 branch
BranchTrueFalseTotal
if $constr 116 18 134
498  
499 134 ▶ unless ($indexed_fields{ ($c->fields())[0] }
Branch: 1 branch
BranchTrueFalseTotal
unless ($indexed_fields{($c->fields)[0]} or $c->type ne "FOREIGN KEY") 32 102 134
Condition100%3/3
$indexed_fields{($c->fields)[0]} or $c->type ne "FOREIGN KEY"
l!l&&r!l&&!r
416132
MC/DC100%2/2
$indexed_fields{($c->fields)[0]} or $c->type ne "FOREIGN KEY"
$indexed_fields{($c->fields)[0]} $c->type ne "FOREIGN KEY"
Truth table100%3/3
A or B
A= $indexed_fields{($c->fields)[0]}B= $c->type ne "FOREIGN KEY"
ABresult
1X1
011
000
500 || $c->type ne FOREIGN_KEY) {
501 32 push @index_defs, "INDEX (" . $generator->quote(($c->fields())[0]) . ")";
502 32 $indexed_fields{ ($c->fields())[0] } = 1;
503 }
504 }
505  
506 60 $create .= join(",\n", map {" $_"} @field_defs, @index_defs, @constraint_defs);
507  
508 #
509 # Footer
510 #
511 60 $create .= "\n)";
512 60 ▶ $create .= generate_table_options($table, $options) || '';
Condition100%2/2
generate_table_options($table, $options) || ''
l!l
4218
MC/DC100%1/1
generate_table_options($table, $options) || ''
generate_table_options($table, $options)
Truth table100%2/2
A
A= generate_table_options($table, $options)
Aresult
11
00
513  
514 # $create .= ";\n\n";
515  
516 60 ▶ return $drop ? ($drop, $create) : $create;
Branch: 1 branch
BranchTrueFalseTotal
$drop ? : 36 24 60
517 }
518  
519 65 ▶sub generate_table_options {
Subroutine generate_table_options: called CC=13 SCAR=26.3
Pod undocumented
520 65 my ($table, $options) = @_;
521 65 my $create;
522  
523 65 my $table_type_defined = 0;
524 65 my $generator = _generator($options);
525 65 my $charset = $table->extra('mysql_charset');
526 65 my $collate = $table->extra('mysql_collate');
527 65 my $union = undef;
528 65 for my $t1_option_ref ($table->options) {
529 59 my ($key, $value) = %{$t1_option_ref};
530 59 ▶ $table_type_defined = 1
Branch: 1 branch
BranchTrueFalseTotal
if uc $key eq "ENGINE" or uc $key eq "TYPE" 47 12 59
Condition66%2/3
uc $key eq "ENGINE" or uc $key eq "TYPE"
l!l&&r!l&&!r
47012
MC/DC50%1/2
uc $key eq "ENGINE" or uc $key eq "TYPE"
uc $key eq "ENGINE" uc $key eq "TYPE"
Truth table66%2/3
A or B
A= uc $key eq "ENGINE"B= uc $key eq "TYPE"
ABresult
1X1
011
000
531 if uc $key eq 'ENGINE'
532 or uc $key eq 'TYPE';
533 59 ▶ if (uc $key eq 'CHARACTER SET') {
Branch: 3 branches
BranchTrueFalseTotal
if (uc $key eq 'CHARACTER SET') { } 6 53 59
elsif (uc $key eq 'COLLATE') { } 6 47 53
elsif (uc $key eq 'UNION') { } 0 47 47
534 6 $charset = $value;
535 6 next;
536 } elsif (uc $key eq 'COLLATE') {
537 6 $collate = $value;
538 6 next;
539 } elsif (uc $key eq 'UNION') {
540 0 $union = '(' . join(', ', map { $generator->quote($_) } @$value) . ')';
541 0 next;
542 }
543 47 $create .= " $key=$value";
544 }
545  
546 65 my $mysql_table_type = $table->extra('mysql_table_type');
547 65 ▶ $create .= " ENGINE=$mysql_table_type"
Branch: 1 branch
BranchTrueFalseTotal
if $mysql_table_type and not $table_type_defined 0 65 65
Condition33%1/3
$mysql_table_type and not $table_type_defined
!ll&&!rl&&r
6500
MC/DC0%0/2
$mysql_table_type and not $table_type_defined
$mysql_table_type not $table_type_defined
Truth table33%1/3
A and not(B)
A= $mysql_table_typeB= not $table_type_defined
ABresult
0X0
100
111
548 if $mysql_table_type && !$table_type_defined;
549 65 my $comments = $table->comments;
550  
551 65 ▶ $create .= " DEFAULT CHARACTER SET $charset" if $charset;
Branch: 1 branch
BranchTrueFalseTotal
if $charset 6 59 65
552 65 ▶ $create .= " COLLATE $collate" if $collate;
Branch: 1 branch
BranchTrueFalseTotal
if $collate 6 59 65
553 65 ▶ $create .= " UNION=$union" if $union;
Branch: 1 branch
BranchTrueFalseTotal
if $union 0 65 65
554 65 ▶ $create .= qq[ comment='$comments'] if $comments;
Branch: 1 branch
BranchTrueFalseTotal
if $comments 6 59 65
555 65 return $create;
556 }
557  
558 311 ▶sub create_field {
Subroutine create_field: called CC=36 SCAR=35.9
Pod documented
559 311 my ($field, $options) = @_;
560  
561 311 my $generator = _generator($options);
562  
563 311 my $field_name = $field->name;
564 311 debug("PKG: Looking at field '$field_name'\n");
565 311 my $field_def = $generator->quote($field_name);
566  
567 # data type and size
568 311 my $data_type = $field->data_type;
569 311 my @size = $field->size;
570 311 my %extra = $field->extra;
571 311 ▶ my $list = $extra{'list'} || [];
Condition100%2/2
$extra{'list'} || []
l!l
12299
MC/DC100%1/1
$extra{'list'} || []
$extra{'list'}
Truth table100%2/2
A
A= $extra{'list'}
Aresult
11
00
572 311 my $commalist = join(', ', map { __PACKAGE__->_quote_string($_) } @$list);
573 311 my $charset = $extra{'mysql_charset'};
574 311 my $collate = $extra{'mysql_collate'};
575  
576 311 ▶ my $mysql_version = $options->{mysql_version} || 0;
Condition100%2/2
$options->{'mysql_version'} || 0
l!l
42269
MC/DC100%1/1
$options->{'mysql_version'} || 0
$options->{'mysql_version'}
Truth table100%2/2
A
A= $options->{'mysql_version'}
Aresult
11
00
577 #
578 # Oracle "number" type -- figure best MySQL type
579 #
580 311 ▶ if (lc $data_type eq 'number') {
Branch: 4 branches
BranchTrueFalseTotal
if (lc $data_type eq 'number') { } 8 303 311
elsif ($data_type =~ /char/i and $size[0] > 255) { } 24 279 303
elsif ($data_type =~ /boolean/i) { } 6 273 279
elsif (exists $translate{lc $data_type}) { } 114 159 273
581  
582 # not an integer
583 8 ▶ if (scalar @size > 1) {
Branch: 3 branches
BranchTrueFalseTotal
if (scalar @size > 1) { } 2 6 8
elsif ($size[0] and $size[0] >= 12) { } 2 4 6
elsif ($size[0] and $size[0] <= 1) { } 2 2 4
584 2 $data_type = 'double';
585 ▶ } elsif ($size[0] && $size[0] >= 12) {
Condition66%2/3
$size[0] and $size[0] >= 12
!ll&&!rl&&r
042
MC/DC50%1/2
$size[0] and $size[0] >= 12
$size[0] $size[0] >= 12
Truth table66%2/3
A and B
A= $size[0]B= $size[0] >= 12
ABresult
0X0
100
111
586 2 $data_type = 'bigint';
587 ▶ } elsif ($size[0] && $size[0] <= 1) {
Condition66%2/3
$size[0] and $size[0] <= 1
!ll&&!rl&&r
022
MC/DC50%1/2
$size[0] and $size[0] <= 1
$size[0] $size[0] <= 1
Truth table66%2/3
A and B
A= $size[0]B= $size[0] <= 1
ABresult
0X0
100
111
588 2 $data_type = 'tinyint';
589 } else {
590 2 $data_type = 'int';
591 }
592 }
593 #
594 # Convert a large Oracle varchar to "text"
595 # (not necessary as of 5.0.3 http://dev.mysql.com/doc/refman/5.0/en/char.html)
596 #
597 ▶ elsif ($data_type =~ /char/i && $size[0] > 255) {
Condition100%3/3
$data_type =~ /char/i and $size[0] > 255
!ll&&!rl&&r
2126724
MC/DC100%2/2
$data_type =~ /char/i and $size[0] > 255
$data_type =~ /char/i $size[0] > 255
Truth table100%3/3
A and B
A= $data_type =~ /char/iB= $size[0] > 255
ABresult
0X0
100
111
598 24 ▶ unless ($size[0] <= 65535 && $mysql_version >= 5.000003) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($size[0] <= 65535 and $mysql_version >= "5.000003") 18 6 24
Condition100%3/3
$size[0] <= 65535 and $mysql_version >= "5.000003"
!ll&&!rl&&r
6126
MC/DC100%2/2
$size[0] <= 65535 and $mysql_version >= "5.000003"
$size[0] <= 65535 $mysql_version >= "5.000003"
Truth table100%3/3
A and B
A= $size[0] <= 65535B= $mysql_version >= "5.000003"
ABresult
0X0
100
111
599 18 $data_type = 'text';
600 18 @size = ();
601 }
602 } elsif ($data_type =~ /boolean/i) {
603 6 ▶ if ($mysql_version >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mysql_version >= 4) { } 2 4 6
604 2 $data_type = 'boolean';
605 } else {
606 4 $data_type = 'enum';
607 4 $commalist = "'0','1'";
608 }
609 } elsif (exists $translate{ lc $data_type }) {
610 114 $data_type = $translate{ lc $data_type };
611 }
612  
613 311 ▶ @size = () if $data_type =~ /(text|blob)/i;
Branch: 1 branch
BranchTrueFalseTotal
if $data_type =~ /(text|blob)/i 49 262 311
614  
615 311 ▶ if ($data_type =~ /(double|float)/ && scalar @size == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_type =~ /(double|float)/ and scalar @size == 1) 0 311 311
Condition66%2/3
$data_type =~ /(double|float)/ and scalar @size == 1
!ll&&!rl&&r
30830
MC/DC0%0/2
$data_type =~ /(double|float)/ and scalar @size == 1
$data_type =~ /(double|float)/ scalar @size == 1
Truth table66%2/3
A and B
A= $data_type =~ /(double|float)/B= scalar @size == 1
ABresult
0X0
100
111
616 0 push @size, '0';
617 }
618  
619 311 $field_def .= " $data_type";
620  
621 311 ▶ if (lc($data_type) eq 'enum' || lc($data_type) eq 'set') {
Branch: 2 branches
BranchTrueFalseTotal
if (lc $data_type eq 'enum' or lc $data_type eq 'set') { } 16 295 311
elsif (defined $size[0] and $size[0] > 0 and not grep((lc $data_type eq $_), @no_length_attr)) { } 174 232 406
Condition100%3/3
lc $data_type eq "enum" or lc $data_type eq "set"
l!l&&r!l&&!r
412295
MC/DC100%2/2
lc $data_type eq "enum" or lc $data_type eq "set"
lc $data_type eq "enum" lc $data_type eq "set"
Truth table100%3/3
A or B
A= lc $data_type eq "enum"B= lc $data_type eq "set"
ABresult
1X1
011
000
622 16 $field_def .= '(' . $commalist . ')';
623 ▶ } elsif (defined $size[0] && $size[0] > 0 && !grep lc($data_type) eq $_, @no_length_attr) {
Condition100%6/6
defined $size[0] and $size[0] > 0 and not grep((lc $data_type eq $_), @no_length_attr)
!ll&&!rl&&r
11110174
defined $size[0] and $size[0] > 0
!ll&&!rl&&r
4962184
MC/DC100%3/3
defined $size[0] and $size[0] > 0 and not grep((lc $data_type eq $_), @no_length_attr)
defined $size[0] $size[0] > 0 not grep((lc $data_type eq $_), @no_length_attr)
Truth table100%4/4
A and B and not(C)
A= defined $size[0]B= $size[0] > 0C= not grep((lc $data_type eq $_), @no_length_attr)
ABCresult
0XX0
10X0
1100
1111
624 174 $field_def .= '(' . join(', ', @size) . ')';
625 }
626  
627 # char sets
628 311 ▶ $field_def .= " CHARACTER SET $charset" if $charset;
Branch: 1 branch
BranchTrueFalseTotal
if $charset 0 311 311
629 311 ▶ $field_def .= " COLLATE $collate" if $collate;
Branch: 1 branch
BranchTrueFalseTotal
if $collate 0 311 311
630  
631 # MySQL qualifiers
632 311 for my $qual (qw[ binary unsigned zerofill ]) {
633 933 ▶ my $val = $extra{$qual} || $extra{ uc $qual } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $val = $extra{$qual} || $extra{uc $qual} or next 9 924 933
Condition100%3/3
$extra{$qual} || $extra{uc $qual}
l!l&&r!l&&!r
36924
MC/DC100%2/2
$extra{$qual} || $extra{uc $qual}
$extra{$qual} $extra{uc $qual}
Truth table100%3/3
A || B
A= $extra{$qual}B= $extra{uc $qual}
ABresult
1X1
011
000
634 9 $field_def .= " $qual";
635 }
636 311 for my $qual ('character set', 'collate', 'on update') {
637 933 ▶ my $val = $extra{$qual} || $extra{ uc $qual } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $val = $extra{$qual} || $extra{uc $qual} or next 24 909 933
Condition66%2/3
$extra{$qual} || $extra{uc $qual}
l!l&&r!l&&!r
240909
MC/DC50%1/2
$extra{$qual} || $extra{uc $qual}
$extra{$qual} $extra{uc $qual}
Truth table66%2/3
A || B
A= $extra{$qual}B= $extra{uc $qual}
ABresult
1X1
011
000
638 24 ▶ if (ref $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val) { } 6 18 24
639 6 $field_def .= " $qual ${$val}";
640 } else {
641 18 $field_def .= " $qual $val";
642 }
643 }
644  
645 # Null?
646 311 ▶ if ($field->is_nullable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->is_nullable) { } 206 105 311
647 206 $field_def .= ' NULL';
648 } else {
649 105 $field_def .= ' NOT NULL';
650 }
651  
652 # Default?
653 311 __PACKAGE__->_apply_default_value(
654 $field,
655 \$field_def,
656 [
657 'NULL' => \'NULL',
658 ],
659 );
660  
661 311 ▶ if (my $comments = $field->comments) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $comments = $field->comments) 13 298 311
662 13 $comments = __PACKAGE__->_quote_string($comments);
663 13 $field_def .= qq[ comment $comments];
664 }
665  
666 # auto_increment?
667 311 ▶ $field_def .= " auto_increment" if $field->is_auto_increment;
Branch: 1 branch
BranchTrueFalseTotal
if $field->is_auto_increment 40 271 311
668  
669 311 return $field_def;
670 }
671  
672 91 ▶sub _quote_string {
Subroutine _quote_string: called CC=1 SCAR=0.0
673 91 my ($self, $string) = @_;
674  
675 91 $string =~ s/([\\'])/$1$1/g;
676 91 return qq{'$string'};
677 }
678  
679 4 ▶sub alter_create_index {
Subroutine alter_create_index: called CC=1 SCAR=0.0
Pod undocumented
680 4 my ($index, $options) = @_;
681  
682 4 my $table_name = _generator($options)->quote($index->table->name);
683 4 return join(' ', 'ALTER TABLE', $table_name, 'ADD', create_index(@_));
684 }
685  
686 54 ▶sub create_index {
Subroutine create_index: called CC=7 SCAR=19.5
Pod documented
687 54 my ($index, $options) = @_;
688 54 my $generator = _generator($options);
689  
690 54 my @fields;
691 54 for my $field ($index->fields) {
692 56 my $name = $generator->quote($field->name);
693 56 ▶ if (my $len = $field->extra->{prefix_length}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $len = $field->extra->{"prefix_length"}) 3 53 56
694 3 $name .= "($len)";
695 }
696 56 push @fields, $name;
697  
698 }
699 return join(' ',
700 162 ▶ map { $_ || () } lc $index->type eq 'normal' ? 'INDEX' : $index->type . ' INDEX',
Branch: 2 branches
BranchTrueFalseTotal
lc $index->type eq 'normal' ? : 49 5 54
$index->name ? : 49 5 54
Condition66%4/6
$_ || ()
l!l&&r!l&&!r
15750
$options->{'max_id_length'} || $DEFAULT_MAX_ID_LENGTH
l!l&&r!l&&!r
4360
MC/DC0%0/2
$_ || ()
$_ ()
MC/DC0%0/2
$options->{'max_id_length'} || $DEFAULT_MAX_ID_LENGTH
$options->{'max_id_length'} $DEFAULT_MAX_ID_LENGTH
Truth table66%2/3
A || B
A= $_B= ()
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $options->{'max_id_length'}B= $DEFAULT_MAX_ID_LENGTH
ABresult
1X1
011
000
701 $index->name
702 54 ? $generator->quote(truncate_id_uniquely($index->name, $options->{max_id_length} || $DEFAULT_MAX_ID_LENGTH))
703 : '',
704 '(' . join(', ', @fields) . ')');
705 }
706  
707 4 ▶sub alter_drop_index {
Subroutine alter_drop_index: called CC=2 SCAR=7.6
Pod undocumented
708 4 my ($index, $options) = @_;
709  
710 4 my $table_name = _generator($options)->quote($index->table->name);
711  
712 4 ▶ return join(' ', 'ALTER TABLE', $table_name, 'DROP', 'INDEX', $index->name || $index->fields);
Condition33%1/3
$index->name || $index->fields
l!l&&r!l&&!r
400
MC/DC0%0/2
$index->name || $index->fields
$index->name $index->fields
Truth table33%1/3
A || B
A= $index->nameB= $index->fields
ABresult
1X1
011
000
713  
714 }
715  
716 20 ▶sub alter_drop_constraint {
Subroutine alter_drop_constraint: called CC=3 SCAR=11.0
Pod undocumented
717 20 my ($c, $options) = @_;
718  
719 20 my $generator = _generator($options);
720 20 my $table_name = $generator->quote($c->table->name);
721  
722 20 my @out = ('ALTER', 'TABLE', $table_name, 'DROP');
723 20 ▶ if ($c->type eq PRIMARY_KEY) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->type eq 'PRIMARY KEY') { } 1 19 20
724 1 push @out, $c->type;
725 } else {
726 19 ▶ push @out, ($c->type eq FOREIGN_KEY ? $c->type : "CONSTRAINT"), $generator->quote($c->name);
Branch: 1 branch
BranchTrueFalseTotal
$c->type eq 'FOREIGN KEY' ? : 11 8 19
727 }
728 20 return join(' ', @out);
729 }
730  
731 20 ▶sub alter_create_constraint {
Subroutine alter_create_constraint: called CC=1 SCAR=0.0
Pod undocumented
732 20 my ($index, $options) = @_;
733  
734 20 my $table_name = _generator($options)->quote($index->table->name);
735 20 return join(' ', 'ALTER TABLE', $table_name, 'ADD', create_constraint(@_));
736 }
737  
738 154 ▶sub create_constraint {
Subroutine create_constraint: called CC=26 SCAR=39.0
Pod documented
739 154 my ($c, $options) = @_;
740  
741 154 my $generator = _generator($options);
742 154 ▶ my $leave_name = $options->{leave_name} || undef;
Condition50%1/2
$options->{'leave_name'} || undef
l!l
0154
MC/DC0%0/1
$options->{'leave_name'} || undef
$options->{'leave_name'}
Truth table50%1/2
A
A= $options->{'leave_name'}
Aresult
11
00
743  
744 154 my $reference_table_name = $generator->quote($c->reference_table);
745  
746 154 my @fields = $c->fields;
747  
748 154 ▶ if ($c->type eq PRIMARY_KEY) {
Branch: 4 branches
BranchTrueFalseTotal
if ($c->type eq 'PRIMARY KEY') { } 47 107 154
elsif ($c->type eq 'UNIQUE') { } 34 73 107
elsif ($c->type eq 'FOREIGN KEY') { } 53 20 73
elsif ($c->type eq 'CHECK') { } 2 18 20
749 47 ▶ return unless @fields;
Branch: 1 branch
BranchTrueFalseTotal
unless @fields 0 47 47
750 47 return 'PRIMARY KEY (' . join(", ", map { $generator->quote($_) } @fields) . ')';
751 } elsif ($c->type eq UNIQUE) {
752 34 ▶ return unless @fields;
Branch: 1 branch
BranchTrueFalseTotal
unless @fields 0 34 34
753 return sprintf 'UNIQUE %s(%s)',
754 (
755 (defined $c->name && $c->name)
756 ? $generator->quote(truncate_id_uniquely($c->name, $options->{max_id_length} || $DEFAULT_MAX_ID_LENGTH),)
757 . ' '
758 : ''
759 ),
760 34 ▶ (join ', ', map { $generator->quote($_) } @fields),
Branch: 1 branch
BranchTrueFalseTotal
defined $c->name && $c->name ? : 28 6 34
Condition66%4/6
defined $c->name and $c->name
!ll&&!rl&&r
0628
$options->{'max_id_length'} || $DEFAULT_MAX_ID_LENGTH
l!l&&r!l&&!r
18100
MC/DC50%1/2
defined $c->name and $c->name
defined $c->name $c->name
MC/DC0%0/2
$options->{'max_id_length'} || $DEFAULT_MAX_ID_LENGTH
$options->{'max_id_length'} $DEFAULT_MAX_ID_LENGTH
Truth table66%2/3
A and B
A= defined $c->nameB= $c->name
ABresult
0X0
100
111
Truth table66%2/3
A || B
A= $options->{'max_id_length'}B= $DEFAULT_MAX_ID_LENGTH
ABresult
1X1
011
000
761 ;
762 } elsif ($c->type eq FOREIGN_KEY) {
763 53 ▶ return unless @fields;
Branch: 1 branch
BranchTrueFalseTotal
unless @fields 0 53 53
764 #
765 # Make sure FK field is indexed or MySQL complains.
766 #
767  
768 53 my $table = $c->table;
769 53 ▶ my $c_name = truncate_id_uniquely($c->name, $options->{max_id_length} || $DEFAULT_MAX_ID_LENGTH);
Condition66%2/3
$options->{'max_id_length'} || $DEFAULT_MAX_ID_LENGTH
l!l&&r!l&&!r
4580
MC/DC0%0/2
$options->{'max_id_length'} || $DEFAULT_MAX_ID_LENGTH
$options->{'max_id_length'} $DEFAULT_MAX_ID_LENGTH
Truth table66%2/3
A || B
A= $options->{'max_id_length'}B= $DEFAULT_MAX_ID_LENGTH
ABresult
1X1
011
000
770  
771 53 ▶ my $def = join(' ', 'CONSTRAINT', ($c_name ? $generator->quote($c_name) : ()), 'FOREIGN KEY');
Branch: 1 branch
BranchTrueFalseTotal
$c_name ? : 53 0 53
772  
773 53 $def .= ' (' . join(', ', map { $generator->quote($_) } @fields) . ')';
774  
775 53 $def .= ' REFERENCES ' . $reference_table_name;
776  
777 53 ▶ my @rfields = map { $_ || () } $c->reference_fields;
Condition33%1/3
$_ || ()
l!l&&r!l&&!r
6500
MC/DC0%0/2
$_ || ()
$_ ()
Truth table33%1/3
A || B
A= $_B= ()
ABresult
1X1
011
000
778 53 ▶ unless (@rfields) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@rfields) 0 53 53
779 0 my $rtable_name = $c->reference_table;
780 0 ▶ if (my $ref_table = $table->schema->get_table($rtable_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref_table = $table->schema->get_table($rtable_name)) { } 0 0 0
781 0 push @rfields, $ref_table->primary_key;
782 } else {
783 warn "Can't find reference table '$rtable_name' " . "in schema\n"
784 0 ▶ if $options->{show_warnings};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"show_warnings"} 0 0 0
785 }
786 }
787  
788 53 ▶ if (@rfields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rfields) { } 53 0 53
789 53 $def .= ' (' . join(', ', map { $generator->quote($_) } @rfields) . ')';
790 } else {
791 warn "FK constraint on " . $table->name . '.' . join('', @fields) . " has no reference fields\n"
792 0 ▶ if $options->{show_warnings};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"show_warnings"} 0 0 0
793 }
794  
795 53 ▶ if ($c->match_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->match_type) 0 53 53
796 0 ▶ $def .= ' MATCH ' . ($c->match_type =~ /full/i) ? 'FULL' : 'PARTIAL';
Branch: 1 branch
BranchTrueFalseTotal
' MATCH ' . $c->match_type =~ /full/i ? : 0 0 0
797 }
798  
799 53 ▶ if ($c->on_delete) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->on_delete) 1 52 53
800 1 $def .= ' ON DELETE ' . $c->on_delete;
801 }
802  
803 53 ▶ if ($c->on_update) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->on_update) 0 53 53
804 0 $def .= ' ON UPDATE ' . $c->on_update;
805 }
806 53 return $def;
807 } elsif ($c->type eq CHECK_C) {
808 2 my $table = $c->table;
809 2 ▶ my $c_name = truncate_id_uniquely($c->name, $options->{max_id_length} || $DEFAULT_MAX_ID_LENGTH);
Condition33%1/3
$options->{'max_id_length'} || $DEFAULT_MAX_ID_LENGTH
l!l&&r!l&&!r
020
MC/DC0%0/2
$options->{'max_id_length'} || $DEFAULT_MAX_ID_LENGTH
$options->{'max_id_length'} $DEFAULT_MAX_ID_LENGTH
Truth table33%1/3
A || B
A= $options->{'max_id_length'}B= $DEFAULT_MAX_ID_LENGTH
ABresult
1X1
011
000
810  
811 2 ▶ my $def = join(' ', 'CONSTRAINT', ($c_name ? $generator->quote($c_name) : ()), 'CHECK');
Branch: 1 branch
BranchTrueFalseTotal
$c_name ? : 2 0 2
812  
813 2 $def .= ' (' . $c->expression . ')';
814 2 return $def;
815 }
816  
817 18 return undef;
818 }
819  
820 5 ▶sub alter_table {
Subroutine alter_table: called CC=2 SCAR=7.0
Pod undocumented
821 5 my ($to_table, $options) = @_;
822  
823 5 ▶ my $table_options = generate_table_options($to_table, $options) || '';
Condition50%1/2
generate_table_options($to_table, $options) || ''
l!l
50
MC/DC0%0/1
generate_table_options($to_table, $options) || ''
generate_table_options($to_table, $options)
Truth table50%1/2
A
A= generate_table_options($to_table, $options)
Aresult
11
00
824 5 my $table_name = _generator($options)->quote($to_table->name);
825 5 my $out = sprintf('ALTER TABLE %s%s', $table_name, $table_options);
826  
827 5 return $out;
828 }
829  
830 3 ▶sub rename_field { alter_field(@_) }
Subroutine rename_field: called CC=1 SCAR=0.0
Pod undocumented
831  
832 25 ▶sub alter_field {
Subroutine alter_field: called CC=1 SCAR=0.0
Pod documented
833 25 my ($from_field, $to_field, $options) = @_;
834  
835 25 my $generator = _generator($options);
836 25 my $table_name = $generator->quote($to_field->table->name);
837  
838 25 my $out = sprintf(
839 'ALTER TABLE %s CHANGE COLUMN %s %s',
840 $table_name,
841 $generator->quote($from_field->name),
842 create_field($to_field, $options)
843 );
844  
845 25 return $out;
846 }
847  
848 13 ▶sub add_field {
Subroutine add_field: called CC=1 SCAR=0.0
Pod documented
849 13 my ($new_field, $options) = @_;
850  
851 13 my $table_name = _generator($options)->quote($new_field->table->name);
852  
853 13 my $out = sprintf('ALTER TABLE %s ADD COLUMN %s', $table_name, create_field($new_field, $options));
854  
855 13 return $out;
856  
857 }
858  
859 9 ▶sub drop_field {
Subroutine drop_field: called CC=1 SCAR=0.0
Pod documented
860 9 my ($old_field, $options) = @_;
861  
862 9 my $generator = _generator($options);
863 9 my $table_name = $generator->quote($old_field->table->name);
864  
865 9 my $out = sprintf('ALTER TABLE %s DROP COLUMN %s', $table_name, $generator->quote($old_field->name));
866  
867 9 return $out;
868  
869 }
870  
871 35 ▶sub batch_alter_table {
Subroutine batch_alter_table: called CC=10 SCAR=23.0
Pod undocumented
872 35 my ($table, $diff_hash, $options) = @_;
873  
874 # InnoDB has an issue with dropping and re-adding a FK constraint under the
875 # name in a single alter statement, see: http://bugs.mysql.com/bug.php?id=13741
876 #
877 # We have to work round this.
878  
879 35 my %fks_to_alter;
880 35 ▶ my %fks_to_drop = map { $_->type eq FOREIGN_KEY ? ($_->name => $_) : () } @{ $diff_hash->{alter_drop_constraint} };
Branch: 1 branch
BranchTrueFalseTotal
$_->type eq 'FOREIGN KEY' ? : 11 7 18
881  
882 my %fks_to_create = map {
883 17 ▶ if ($_->type eq FOREIGN_KEY) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->type eq 'FOREIGN KEY') { } 7 10 17
884 $fks_to_alter{ $_->name } = $fks_to_drop{ $_->name }
885 7 ▶ if $fks_to_drop{ $_->name };
Branch: 1 branch
BranchTrueFalseTotal
if $fks_to_drop{$_->name} 1 6 7
886 7 ($_->name => $_);
887 } else {
888 ()
889 10 }
890 35 } @{ $diff_hash->{alter_create_constraint} };
891  
892 35 my @drop_stmt;
893 35 ▶ if (scalar keys %fks_to_alter) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %fks_to_alter) 1 34 35
894 $diff_hash->{alter_drop_constraint}
895 1 = [ grep { !$fks_to_alter{ $_->name } } @{ $diff_hash->{alter_drop_constraint} } ];
896  
897 1 @drop_stmt = batch_alter_table($table, { alter_drop_constraint => [ values %fks_to_alter ] }, $options);
898  
899 }
900  
901 35 my @stmts = batch_alter_table_statements($diff_hash, $options);
902  
903 #quote
904 35 my $generator = _generator($options);
905  
906 # rename_table makes things a bit more complex
907 35 my $renamed_from = "";
908 $renamed_from = $generator->quote($diff_hash->{rename_table}[0][0]->name)
909 35 ▶ if $diff_hash->{rename_table} && @{ $diff_hash->{rename_table} };
Branch: 1 branch
BranchTrueFalseTotal
if $diff_hash->{"rename_table"} and @{$diff_hash->{"rename_table"};} 3 32 35
Condition100%3/3
$diff_hash->{"rename_table"} and @{$diff_hash->{"rename_table"};}
!ll&&!rl&&r
8243
MC/DC100%2/2
$diff_hash->{"rename_table"} and @{$diff_hash->{"rename_table"};}
$diff_hash->{"rename_table"} @{$diff_hash->{"rename_table"};}
Truth table100%3/3
A and B
A= $diff_hash->{"rename_table"}B= @{$diff_hash->{"rename_table"};}
ABresult
0X0
100
111
910  
911 35 ▶ return unless @stmts;
Branch: 1 branch
BranchTrueFalseTotal
unless @stmts 18 17 35
912  
913 # Just zero or one stmts. return now
914 17 ▶ return (@drop_stmt, @stmts) unless @stmts > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @stmts > 1 6 11 17
915  
916 # Now strip off the 'ALTER TABLE xyz' of all but the first one
917  
918 11 my $table_name = $generator->quote($table->name);
919  
920 11 ▶ my $re
Branch: 1 branch
BranchTrueFalseTotal
$renamed_from ? : 3 8 11
921 = $renamed_from
922 ? qr/^ALTER TABLE (?:\Q$table_name\E|\Q$renamed_from\E) /
923 : qr/^ALTER TABLE \Q$table_name\E /;
924  
925 11 my $first = shift @stmts;
926 11 my ($alter_table) = $first =~ /($re)/;
927  
928 11 my $padd = " " x length($alter_table);
929  
930 11 return @drop_stmt, join(",\n", $first, map { s/$re//; $padd . $_ } @stmts);
931  
932 }
933  
934 7 ▶sub drop_table {
Subroutine drop_table: called CC=1 SCAR=0.0
Pod undocumented
935 7 my ($table, $options) = @_;
936  
937 return (
938 # Drop (foreign key) constraints so table drops cleanly
939 batch_alter_table(
940 $table,
941 {
942 7 alter_drop_constraint => [ grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints ]
943 },
944 $options
945 ),
946 'DROP TABLE ' . _generator($options)->quote($table),
947 );
948 }
949  
950 4 ▶sub rename_table {
Subroutine rename_table: called CC=1 SCAR=0.0
Pod undocumented
951 4 my ($old_table, $new_table, $options) = @_;
952  
953 4 my $generator = _generator($options);
954 4 my $old_table_name = $generator->quote($old_table);
955 4 my $new_table_name = $generator->quote($new_table);
956  
957 4 return "ALTER TABLE $old_table_name RENAME TO $new_table_name";
958 }
959  
960 90 ▶sub next_unused_name {
Subroutine next_unused_name: called CC=4 SCAR=13.9
Pod undocumented
961 90 ▶ my $name = shift || '';
Condition50%1/2
shift() || ''
l!l
900
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
962 90 ▶ if (!defined($used_names{$name})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $SQL::Translator::Producer::MySQL::used_names{$name}) 80 10 90
963 80 $used_names{$name} = $name;
964 80 return $name;
965 }
966  
967 10 my $i = 1;
968 10 ▶ while (defined($used_names{ $name . '_' . $i })) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $SQL::Translator::Producer::MySQL::used_names{$name . '_' . $i}) 7 10 17
969 7 ++$i;
970 }
971 10 $name .= '_' . $i;
972 10 $used_names{$name} = $name;
973 10 return $name;
974 }
975  
976 12 1;
977  
978 =pod
979  
980 =head1 SEE ALSO
981  
982 SQL::Translator, http://www.mysql.com/.
983  
984 =head1 AUTHORS
985  
986 darren chamberlain E<lt>darren@cpan.orgE<gt>,
987 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
988  
989 =cut
« blib/lib/SQL/Translator.pm ↑ Summary blib/lib/SQL/Translator/Parser/DBI.pm »