| 1 | package SQL::Translator::Producer::SQLite; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | SQL::Translator::Producer::SQLite - SQLite producer for SQL::Translator | ||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | use SQL::Translator; | ||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | my $t = SQL::Translator->new( parser => '...', producer => 'SQLite' ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 12 | $t->translate; | ||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||
| 16 | This module will produce text output of the schema suitable for SQLite. | ||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||
| 18 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 9 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 9 | ▶ | use SQL::Translator::Schema::Constants; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 9 | ▶ | use SQL::Translator::Utils qw(debug header_comment parse_dbms_version batch_alter_table_statements); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 9 | ▶ | use SQL::Translator::Generator::DDL::SQLite; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 9 | our ( $DEBUG, $WARN ); | |||||||||||||||||||||||||||||||||||||||||||||
| 28 | 9 | our $VERSION = '1.6_3'; | |||||||||||||||||||||||||||||||||||||||||||||
| 29 | 9 | ▶ | $DEBUG = 0 unless defined $DEBUG; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 9 | ▶ | $WARN = 0 unless defined $WARN; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 9 | our $max_id_length = 30; | |||||||||||||||||||||||||||||||||||||||||||||
| 33 | 9 | my %global_names; | |||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | # HIDEOUS TEMPORARY DEFAULT WITHOUT QUOTING! | ||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 9 | our $NO_QUOTES = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 37 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 9 | my ($quoting_generator, $nonquoting_generator); | |||||||||||||||||||||||||||||||||||||||||||||
| 40 | sub _generator { | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 368 | ▶ | $NO_QUOTES | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_generator: called CC=4 SCAR=14.3
Branch: 1 branch
Condition66%4/6
$SQL::Translator::Producer::SQLite::NO_QUOTES ? $nonquoting_generator ||= "SQL::Translator::Generator::DDL::SQLite"->new("quote_chars", []) : $quoting_generator ||= "SQL::Translator::Generator::DDL::SQLite"->new
$nonquoting_generator ||= "SQL::Translator::Generator::DDL::SQLite"->new("quote_chars", [])
MC/DC0%0/2
$SQL::Translator::Producer::SQLite::NO_QUOTES ? $nonquoting_generator ||= "SQL::Translator::Generator::DDL::SQLite"->new("quote_chars", []) : $quoting_generator ||= "SQL::Translator::Generator::DDL::SQLite"->new
$SQL::Translator::Producer::SQLite::NO_QUOTES ? $nonquoting_generator ||= "SQL::Translator::Generator::DDL::SQLite"->new("quote_chars", []) : $quoting_generator
"SQL::Translator::Generator::DDL::SQLite"->new
MC/DC0%0/2
$nonquoting_generator ||= "SQL::Translator::Generator::DDL::SQLite"->new("quote_chars", [])
$nonquoting_generator
"SQL::Translator::Generator::DDL::SQLite"->new("quote_chars", [])
Truth table66%2/3
A ||= B
A= $SQL::Translator::Producer::SQLite::NO_QUOTES ? $nonquoting_generator ||= "SQL::Translator::Generator::DDL::SQLite"->new("quote_chars", []) : $quoting_generatorB= "SQL::Translator::Generator::DDL::SQLite"->new
Truth table66%2/3
A ||= B
A= $nonquoting_generatorB= "SQL::Translator::Generator::DDL::SQLite"->new("quote_chars", [])
| |||||||||||||||||||||||||||||||||||||||||||||||
| 42 | ? $nonquoting_generator ||= SQL::Translator::Generator::DDL::SQLite->new(quote_chars => []) | ||||||||||||||||||||||||||||||||||||||||||||||
| 43 | : $quoting_generator ||= SQL::Translator::Generator::DDL::SQLite->new | ||||||||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 9 | sub produce { | |||||||||||||||||||||||||||||||||||||||||||||
| 48 | 10 | ▶ | my $translator = shift; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
produce: called CC=12 SCAR=24.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 10 | local $DEBUG = $translator->debug; | |||||||||||||||||||||||||||||||||||||||||||||
| 50 | 10 | local $WARN = $translator->show_warnings; | |||||||||||||||||||||||||||||||||||||||||||||
| 51 | 10 | my $no_comments = $translator->no_comments; | |||||||||||||||||||||||||||||||||||||||||||||
| 52 | 10 | my $add_drop_table = $translator->add_drop_table; | |||||||||||||||||||||||||||||||||||||||||||||
| 53 | 10 | my $schema = $translator->schema; | |||||||||||||||||||||||||||||||||||||||||||||
| 54 | 10 | my $producer_args = $translator->producer_args; | |||||||||||||||||||||||||||||||||||||||||||||
| 55 | my $sqlite_version = parse_dbms_version( | ||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 10 | $producer_args->{sqlite_version}, 'perl' | |||||||||||||||||||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 10 | my $no_txn = $producer_args->{no_transaction}; | |||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 10 | debug("PKG: Beginning production\n"); | |||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 10 | %global_names = (); #reset | |||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||
| 64 | # only quote if quotes were requested for real | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | # 0E0 indicates "the default of true" was assumed | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 10 | ▶ | local $NO_QUOTES = 0 | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$translator->quote_identifiers and $translator->quote_identifiers ne "0E0"
MC/DC50%1/2
$translator->quote_identifiers and $translator->quote_identifiers ne "0E0"
$translator->quote_identifiers
$translator->quote_identifiers ne "0E0"
Truth table66%2/3
A and B
A= $translator->quote_identifiersB= $translator->quote_identifiers ne "0E0"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 67 | if $translator->quote_identifiers and $translator->quote_identifiers ne '0E0'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 10 | my $head; | |||||||||||||||||||||||||||||||||||||||||||||
| 70 | 10 | ▶ | $head = (header_comment() . "\n") unless $no_comments; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 10 | my @create = (); | |||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 10 | ▶ | push @create, "BEGIN TRANSACTION" unless $no_txn; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 10 | for my $table ( $schema->get_tables ) { | |||||||||||||||||||||||||||||||||||||||||||||
| 77 | 17 | push @create, create_table($table, { no_comments => $no_comments, | |||||||||||||||||||||||||||||||||||||||||||||
| 78 | sqlite_version => $sqlite_version, | ||||||||||||||||||||||||||||||||||||||||||||||
| 79 | add_drop_table => $add_drop_table,}); | ||||||||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 10 | for my $view ( $schema->get_views ) { | |||||||||||||||||||||||||||||||||||||||||||||
| 83 | 4 | push @create, create_view($view, { | |||||||||||||||||||||||||||||||||||||||||||||
| 84 | add_drop_view => $add_drop_table, | ||||||||||||||||||||||||||||||||||||||||||||||
| 85 | no_comments => $no_comments, | ||||||||||||||||||||||||||||||||||||||||||||||
| 86 | }); | ||||||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 10 | for my $trigger ( $schema->get_triggers ) { | |||||||||||||||||||||||||||||||||||||||||||||
| 90 | 11 | push @create, create_trigger($trigger, { | |||||||||||||||||||||||||||||||||||||||||||||
| 91 | add_drop_trigger => $add_drop_table, | ||||||||||||||||||||||||||||||||||||||||||||||
| 92 | no_comments => $no_comments, | ||||||||||||||||||||||||||||||||||||||||||||||
| 93 | }); | ||||||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 10 | ▶ | push @create, "COMMIT" unless $no_txn; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 10 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 3 | ▶ | return ($head||(), @create); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$head || ()
MC/DC0%0/2
$head || ()
$head
()
Truth table33%1/3
A || B
A= $headB= ()
| |||||||||||||||||||||||||||||||||||||||||||||||
| 100 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 7 | ▶ | return join ('', | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$head || ()
MC/DC50%1/2
$head || ()
$head
()
Truth table66%2/3
A || B
A= $headB= ()
| |||||||||||||||||||||||||||||||||||||||||||||||
| 102 | $head||(), | ||||||||||||||||||||||||||||||||||||||||||||||
| 103 | join(";\n\n", @create ), | ||||||||||||||||||||||||||||||||||||||||||||||
| 104 | ";\n", | ||||||||||||||||||||||||||||||||||||||||||||||
| 105 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||
| 109 | sub mk_name { | ||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 28 | ▶ | my ($name, $scope, $critical) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mk_name: called CC=5 SCAR=16.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 28 | ▶ | $scope ||= \%global_names; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$scope ||= \%global_names
MC/DC0%0/1
$scope ||= \%global_names
$scope
Truth table50%1/2
A
A= $scope
| |||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 28 | ▶ | if ( my $prev = $scope->{ $name } ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 5 | my $name_orig = $name; | |||||||||||||||||||||||||||||||||||||||||||||
| 115 | 5 | $name .= sprintf( "%02d", ++$prev ); | |||||||||||||||||||||||||||||||||||||||||||||
| 116 | 5 | ▶ | substr($name, $max_id_length - 3) = "00" | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 117 | if length( $name ) > $max_id_length; | ||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 5 | ▶ | warn "The name '$name_orig' has been changed to ", | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 120 | "'$name' to make it unique.\n" if $WARN; | ||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 5 | $scope->{ $name_orig }++; | |||||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 28 | $scope->{ $name }++; | |||||||||||||||||||||||||||||||||||||||||||||
| 126 | 28 | return _generator()->quote($name); | |||||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||
| 129 | sub create_view { | ||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 8 | ▶ | my ($view, $options) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
create_view: called CC=8 SCAR=21.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 8 | my $add_drop_view = $options->{add_drop_view}; | |||||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 8 | my $view_name = _generator()->quote($view->name); | |||||||||||||||||||||||||||||||||||||||||||||
| 134 | 8 | $global_names{$view->name} = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 8 | debug("PKG: Looking at view '${view_name}'\n"); | |||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||
| 138 | # Header. Should this look like what mysqldump produces? | ||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 8 | my $extra = $view->extra; | |||||||||||||||||||||||||||||||||||||||||||||
| 140 | 8 | my @create; | |||||||||||||||||||||||||||||||||||||||||||||
| 141 | 8 | ▶ | push @create, "DROP VIEW IF EXISTS $view_name" if $add_drop_view; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 8 | my $create_view = 'CREATE'; | |||||||||||||||||||||||||||||||||||||||||||||
| 144 | 8 | ▶ | $create_view .= " TEMPORARY" if exists($extra->{temporary}) && $extra->{temporary}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $extra->{"temporary"} and $extra->{"temporary"}
MC/DC50%1/2
exists $extra->{"temporary"} and $extra->{"temporary"}
exists $extra->{"temporary"}
$extra->{"temporary"}
Truth table66%2/3
A and B
A= exists $extra->{"temporary"}B= $extra->{"temporary"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 8 | $create_view .= ' VIEW'; | |||||||||||||||||||||||||||||||||||||||||||||
| 146 | 8 | ▶ | $create_view .= " IF NOT EXISTS" if exists($extra->{if_not_exists}) && $extra->{if_not_exists}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $extra->{"if_not_exists"} and $extra->{"if_not_exists"}
MC/DC50%1/2
exists $extra->{"if_not_exists"} and $extra->{"if_not_exists"}
exists $extra->{"if_not_exists"}
$extra->{"if_not_exists"}
Truth table66%2/3
A and B
A= exists $extra->{"if_not_exists"}B= $extra->{"if_not_exists"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 8 | $create_view .= " ${view_name}"; | |||||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 8 | ▶ | if( my $sql = $view->sql ){ | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 8 | $create_view .= " AS\n ${sql}"; | |||||||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 8 | push @create, $create_view; | |||||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||||||
| 154 | # Tack the comment onto the first statement. | ||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 8 | ▶ | unless ($options->{no_comments}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | $create[0] = "--\n-- View: ${view_name}\n--\n" . $create[0]; | |||||||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 8 | return @create; | |||||||||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||
| 163 | sub create_table | ||||||||||||||||||||||||||||||||||||||||||||||
| 164 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 29 | ▶ | my ($table, $options) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
create_table: called CC=19 SCAR=30.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 29 | my $table_name = _generator()->quote($table->name); | |||||||||||||||||||||||||||||||||||||||||||||
| 168 | 29 | $global_names{$table->name} = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 29 | my $no_comments = $options->{no_comments}; | |||||||||||||||||||||||||||||||||||||||||||||
| 171 | 29 | my $add_drop_table = $options->{add_drop_table}; | |||||||||||||||||||||||||||||||||||||||||||||
| 172 | 29 | ▶ | my $sqlite_version = $options->{sqlite_version} || 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$options->{'sqlite_version'} || 0
MC/DC0%0/1
$options->{'sqlite_version'} || 0
$options->{'sqlite_version'}
Truth table50%1/2
A
A= $options->{'sqlite_version'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 29 | debug("PKG: Looking at table '$table_name'\n"); | |||||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 29 | my ( @index_defs, @constraint_defs ); | |||||||||||||||||||||||||||||||||||||||||||||
| 177 | 29 | ▶ | my @fields = $table->get_fields or die "No fields in $table_name"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 29 | ▶ | my $temp = $options->{temporary_table} ? 'TEMPORARY ' : ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 180 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 181 | # Header. | ||||||||||||||||||||||||||||||||||||||||||||||
| 182 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 29 | ▶ | my $exists = ($sqlite_version >= 3.003) ? ' IF EXISTS' : ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 29 | my @create; | |||||||||||||||||||||||||||||||||||||||||||||
| 185 | 29 | my ($comment, $create_table) = ""; | |||||||||||||||||||||||||||||||||||||||||||||
| 186 | 29 | ▶ | $comment = "--\n-- Table: $table_name\n--\n" unless $no_comments; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 29 | ▶ | if ($add_drop_table) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 12 | push @create, $comment . qq[DROP TABLE$exists $table_name]; | |||||||||||||||||||||||||||||||||||||||||||||
| 189 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 17 | $create_table = $comment; | |||||||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 29 | $create_table .= "CREATE ${temp}TABLE $table_name (\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||||||
| 195 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 196 | # Comments | ||||||||||||||||||||||||||||||||||||||||||||||
| 197 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 29 | ▶ | if ( $table->comments and !$no_comments ){ | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$table->comments and not $no_comments
MC/DC0%0/2
$table->comments and not $no_comments
$table->comments
not $no_comments
Truth table33%1/3
A and not(B)
A= $table->commentsB= not $no_comments
| |||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 0 | $create_table .= "-- Comments: \n-- "; | |||||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | $create_table .= join "\n-- ", $table->comments; | |||||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | $create_table .= "\n--\n\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||||||
| 204 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 205 | # How many fields in PK? | ||||||||||||||||||||||||||||||||||||||||||||||
| 206 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 29 | my $pk = $table->primary_key; | |||||||||||||||||||||||||||||||||||||||||||||
| 208 | 29 | ▶ | my @pk_fields = $pk ? $pk->fields : (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||||||
| 210 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 211 | # Fields | ||||||||||||||||||||||||||||||||||||||||||||||
| 212 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 29 | my ( @field_defs, $pk_set ); | |||||||||||||||||||||||||||||||||||||||||||||
| 214 | 29 | for my $field ( @fields ) { | |||||||||||||||||||||||||||||||||||||||||||||
| 215 | 105 | push @field_defs, create_field($field); | |||||||||||||||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 29 | ▶ | if ( | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
scalar @pk_fields > 1 or @pk_fields and not grep(/INTEGER PRIMARY KEY/, @field_defs)
@pk_fields and not grep(/INTEGER PRIMARY KEY/, @field_defs)
MC/DC33%1/3
scalar @pk_fields > 1 or @pk_fields and not grep(/INTEGER PRIMARY KEY/, @field_defs)
scalar @pk_fields > 1
@pk_fields
not grep(/INTEGER PRIMARY KEY/, @field_defs)
Truth table50%2/4
A or B and not(C)
A= scalar @pk_fields > 1B= @pk_fieldsC= not grep(/INTEGER PRIMARY KEY/, @field_defs)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 219 | scalar @pk_fields > 1 | ||||||||||||||||||||||||||||||||||||||||||||||
| 220 | || | ||||||||||||||||||||||||||||||||||||||||||||||
| 221 | ( @pk_fields && !grep /INTEGER PRIMARY KEY/, @field_defs ) | ||||||||||||||||||||||||||||||||||||||||||||||
| 222 | ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 2 | push @field_defs, 'PRIMARY KEY (' . join(', ', map _generator()->quote($_), @pk_fields ) . ')'; | |||||||||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||||||
| 226 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 227 | # Indices | ||||||||||||||||||||||||||||||||||||||||||||||
| 228 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 29 | for my $index ( $table->get_indices ) { | |||||||||||||||||||||||||||||||||||||||||||||
| 230 | 10 | push @index_defs, create_index($index); | |||||||||||||||||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||||||
| 233 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 234 | # Constraints | ||||||||||||||||||||||||||||||||||||||||||||||
| 235 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 29 | for my $c ( $table->get_constraints ) { | |||||||||||||||||||||||||||||||||||||||||||||
| 237 | 41 | ▶ | if ($c->type eq "FOREIGN KEY") { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 10 | push @field_defs, create_foreignkey($c); | |||||||||||||||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 240 | elsif ($c->type eq "CHECK") { | ||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | push @field_defs, create_check_constraint($c); | |||||||||||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 41 | ▶ | next unless $c->type eq UNIQUE; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 11 | push @constraint_defs, create_constraint($c); | |||||||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 29 | $create_table .= join(",\n", map { " $_" } @field_defs ) . "\n)"; | |||||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 29 | return (@create, $create_table, @index_defs, @constraint_defs ); | |||||||||||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||||||
| 252 | sub create_check_constraint { | ||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 1 | ▶ | my $c = shift; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
create_check_constraint: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 1 | my $check = ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 255 | 1 | ▶ | $check .= 'CONSTRAINT ' . _generator->quote( $c->name ) . ' ' if $c->name; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 1 | $check .= 'CHECK(' . $c->expression . ')'; | |||||||||||||||||||||||||||||||||||||||||||||
| 257 | 1 | return $check; | |||||||||||||||||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||||||
| 260 | sub create_foreignkey { | ||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 11 | ▶ | my $c = shift; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
create_foreignkey: called CC=7 SCAR=23.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 11 | my @fields = $c->fields; | |||||||||||||||||||||||||||||||||||||||||||||
| 264 | 11 | ▶ | my @rfields = map { $_ || () } $c->reference_fields; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 11 | ▶ | unless ( @rfields ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | my $rtable_name = $c->reference_table; | |||||||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | ▶ | if ( my $ref_table = $c->schema->get_table( $rtable_name ) ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | push @rfields, $ref_table->primary_key; | |||||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | ▶ | die "FK constraint on " . $rtable_name . '.' . join('', @fields) . " has no reference fields\n" | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 271 | unless @rfields; | ||||||||||||||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 273 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 0 | die "Can't find reference table '$rtable_name' in schema\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||||||
| 278 | my $fk_sql = sprintf 'FOREIGN KEY (%s) REFERENCES %s(%s)', | ||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 11 | join (', ', map { _generator()->quote($_) } @fields ), | |||||||||||||||||||||||||||||||||||||||||||||
| 280 | _generator()->quote($c->reference_table), | ||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 11 | join (', ', map { _generator()->quote($_) } @rfields ) | |||||||||||||||||||||||||||||||||||||||||||||
| 282 | ; | ||||||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 11 | ▶ | $fk_sql .= " ON DELETE " . $c->{on_delete} if $c->{on_delete}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 11 | ▶ | $fk_sql .= " ON UPDATE " . $c->{on_update} if $c->{on_update}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 11 | return $fk_sql; | |||||||||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 108 | ▶ | sub create_field { return _generator()->field($_[0]) } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
create_field: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||||||
| 292 | sub create_index | ||||||||||||||||||||||||||||||||||||||||||||||
| 293 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 13 | ▶ | my ($index, $options) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
create_index: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 13 | (my $index_table_name = $index->table->name) =~ s/^.+?\.//; # table name may not specify schema | |||||||||||||||||||||||||||||||||||||||||||||
| 297 | 13 | ▶ | my $name = mk_name($index->name || "${index_table_name}_idx"); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$index->name || "${index_table_name}_idx"
MC/DC100%1/1
$index->name || "${index_table_name}_idx"
$index->name
Truth table100%2/2
A
A= $index->name
| |||||||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 13 | ▶ | my $type = $index->type eq 'UNIQUE' ? "UNIQUE " : ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||||||
| 301 | # strip any field size qualifiers as SQLite doesn't like these | ||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 13 | my @fields = map { s/\(\d+\)$//; _generator()->quote($_) } $index->fields; | |||||||||||||||||||||||||||||||||||||||||||||
| 303 | 13 | $index_table_name = _generator()->quote($index_table_name); | |||||||||||||||||||||||||||||||||||||||||||||
| 304 | 13 | ▶ | warn "removing schema name from '" . $index->table->name . "' to make '$index_table_name'\n" if $WARN; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 13 | my $index_def = | |||||||||||||||||||||||||||||||||||||||||||||
| 306 | "CREATE ${type}INDEX $name ON " . $index_table_name . | ||||||||||||||||||||||||||||||||||||||||||||||
| 307 | ' (' . join( ', ', @fields ) . ')'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 13 | return $index_def; | |||||||||||||||||||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||||||
| 312 | sub create_constraint | ||||||||||||||||||||||||||||||||||||||||||||||
| 313 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 15 | ▶ | my ($c, $options) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
create_constraint: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 15 | (my $index_table_name = $c->table->name) =~ s/^.+?\.//; # table name may not specify schema | |||||||||||||||||||||||||||||||||||||||||||||
| 317 | 15 | ▶ | my $name = mk_name($c->name || "${index_table_name}_idx"); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$c->name || "${index_table_name}_idx"
MC/DC100%1/1
$c->name || "${index_table_name}_idx"
$c->name
Truth table100%2/2
A
A= $c->name
| |||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 15 | my @fields = map _generator()->quote($_), $c->fields; | |||||||||||||||||||||||||||||||||||||||||||||
| 319 | 15 | $index_table_name = _generator()->quote($index_table_name); | |||||||||||||||||||||||||||||||||||||||||||||
| 320 | 15 | ▶ | warn "removing schema name from '" . $c->table->name . "' to make '$index_table_name'\n" if $WARN; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 15 | my $c_def = | |||||||||||||||||||||||||||||||||||||||||||||
| 323 | "CREATE UNIQUE INDEX $name ON " . $index_table_name . | ||||||||||||||||||||||||||||||||||||||||||||||
| 324 | ' (' . join( ', ', @fields ) . ')'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 15 | return $c_def; | |||||||||||||||||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||||||
| 329 | sub create_trigger { | ||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 13 | ▶ | my ($trigger, $options) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
create_trigger: called CC=11 SCAR=24.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 13 | my $add_drop = $options->{add_drop_trigger}; | |||||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||||||
| 333 | 13 | my @statements; | |||||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||||||
| 335 | 13 | my $trigger_name = $trigger->name; | |||||||||||||||||||||||||||||||||||||||||||||
| 336 | 13 | $global_names{$trigger_name} = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 13 | my $events = $trigger->database_events; | |||||||||||||||||||||||||||||||||||||||||||||
| 339 | 13 | for my $evt ( @$events ) { | |||||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 16 | my $trig_name = $trigger_name; | |||||||||||||||||||||||||||||||||||||||||||||
| 342 | 16 | ▶ | if (@$events > 1) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 343 | 6 | $trig_name .= "_$evt"; | |||||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||||||
| 345 | 6 | ▶ | warn "Multiple database events supplied for trigger '$trigger_name', ", | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 346 | "creating trigger '$trig_name' for the '$evt' event.\n" if $WARN; | ||||||||||||||||||||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||||||
| 349 | 16 | $trig_name = _generator()->quote($trig_name); | |||||||||||||||||||||||||||||||||||||||||||||
| 350 | 16 | ▶ | push @statements, "DROP TRIGGER IF EXISTS $trig_name" if $add_drop; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||||||
| 353 | 16 | $DB::single = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 354 | 16 | my $action = ""; | |||||||||||||||||||||||||||||||||||||||||||||
| 355 | 16 | ▶ | if (not ref $trigger->action) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 12 | $action = $trigger->action; | |||||||||||||||||||||||||||||||||||||||||||||
| 357 | 12 | ▶ | $action = "BEGIN " . $action . " END" | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 358 | unless $action =~ /^ \s* BEGIN [\s\;] .*? [\s\;] END [\s\;]* $/six; | ||||||||||||||||||||||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 360 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 361 | $action = $trigger->action->{for_each} . " " | ||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 4 | ▶ | if $trigger->action->{for_each}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||||||
| 364 | $action = $trigger->action->{when} . " " | ||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 4 | ▶ | if $trigger->action->{when}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 4 | ▶ | my $steps = $trigger->action->{steps} || []; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$trigger->action->{'steps'} || []
MC/DC0%0/1
$trigger->action->{'steps'} || []
$trigger->action->{'steps'}
Truth table50%1/2
A
A= $trigger->action->{'steps'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||||||
| 369 | 4 | $action .= "BEGIN "; | |||||||||||||||||||||||||||||||||||||||||||||
| 370 | 4 | $action .= $_ . "; " for (@$steps); | |||||||||||||||||||||||||||||||||||||||||||||
| 371 | 4 | $action .= "END"; | |||||||||||||||||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||||||
| 374 | 16 | push @statements, sprintf ( | |||||||||||||||||||||||||||||||||||||||||||||
| 375 | 'CREATE TRIGGER %s %s %s on %s %s', | ||||||||||||||||||||||||||||||||||||||||||||||
| 376 | $trig_name, | ||||||||||||||||||||||||||||||||||||||||||||||
| 377 | $trigger->perform_action_when, | ||||||||||||||||||||||||||||||||||||||||||||||
| 378 | $evt, | ||||||||||||||||||||||||||||||||||||||||||||||
| 379 | _generator()->quote($trigger->on_table), | ||||||||||||||||||||||||||||||||||||||||||||||
| 380 | $action | ||||||||||||||||||||||||||||||||||||||||||||||
| 381 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||||||
| 384 | 13 | return @statements; | |||||||||||||||||||||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||||||
| 387 | 1 | ▶ | sub alter_table { () } # Noop | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
alter_table: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||||||
| 389 | sub add_field { | ||||||||||||||||||||||||||||||||||||||||||||||
| 390 | 3 | ▶ | my ($field) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_field: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||||||
| 392 | 3 | return sprintf("ALTER TABLE %s ADD COLUMN %s", | |||||||||||||||||||||||||||||||||||||||||||||
| 393 | _generator()->quote($field->table->name), create_field($field)) | ||||||||||||||||||||||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||||||||||
| 396 | sub alter_create_index { | ||||||||||||||||||||||||||||||||||||||||||||||
| 397 | 1 | ▶ | my ($index) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
alter_create_index: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||||||
| 399 | # This might cause name collisions | ||||||||||||||||||||||||||||||||||||||||||||||
| 400 | 1 | return create_index($index); | |||||||||||||||||||||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||||||||||
| 403 | sub alter_create_constraint { | ||||||||||||||||||||||||||||||||||||||||||||||
| 404 | 3 | ▶ | my ($constraint) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
alter_create_constraint: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||||||
| 406 | 3 | ▶ | return create_constraint($constraint) if $constraint->type eq 'UNIQUE'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||||||||||
| 409 | 2 | ▶ | sub alter_drop_constraint { alter_drop_index(@_) } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
alter_drop_constraint: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||||||||||
| 411 | sub alter_drop_index { | ||||||||||||||||||||||||||||||||||||||||||||||
| 412 | 3 | ▶ | my ($constraint) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
alter_drop_index: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||||||||||
| 414 | 3 | return sprintf("DROP INDEX %s", | |||||||||||||||||||||||||||||||||||||||||||||
| 415 | _generator()->quote($constraint->name)); | ||||||||||||||||||||||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||||||
| 418 | sub batch_alter_table { | ||||||||||||||||||||||||||||||||||||||||||||||
| 419 | 5 | ▶ | my ($table, $diffs, $options) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
batch_alter_table: called CC=8 SCAR=20.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||||||
| 421 | # If we have any of the following | ||||||||||||||||||||||||||||||||||||||||||||||
| 422 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 423 | # rename_field | ||||||||||||||||||||||||||||||||||||||||||||||
| 424 | # alter_field | ||||||||||||||||||||||||||||||||||||||||||||||
| 425 | # drop_field | ||||||||||||||||||||||||||||||||||||||||||||||
| 426 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 427 | # we need to do the following <http://www.sqlite.org/faq.html#q11> | ||||||||||||||||||||||||||||||||||||||||||||||
| 428 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 429 | # BEGIN TRANSACTION; | ||||||||||||||||||||||||||||||||||||||||||||||
| 430 | # CREATE TEMPORARY TABLE t1_backup(a,b); | ||||||||||||||||||||||||||||||||||||||||||||||
| 431 | # INSERT INTO t1_backup SELECT a,b FROM t1; | ||||||||||||||||||||||||||||||||||||||||||||||
| 432 | # DROP TABLE t1; | ||||||||||||||||||||||||||||||||||||||||||||||
| 433 | # CREATE TABLE t1(a,b); | ||||||||||||||||||||||||||||||||||||||||||||||
| 434 | # INSERT INTO t1 SELECT a,b FROM t1_backup; | ||||||||||||||||||||||||||||||||||||||||||||||
| 435 | # DROP TABLE t1_backup; | ||||||||||||||||||||||||||||||||||||||||||||||
| 436 | # COMMIT; | ||||||||||||||||||||||||||||||||||||||||||||||
| 437 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 438 | # Fun, eh? | ||||||||||||||||||||||||||||||||||||||||||||||
| 439 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 440 | # If we have rename_field we do similarly. | ||||||||||||||||||||||||||||||||||||||||||||||
| 441 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 442 | # We create the temporary table as a copy of the new table, copy all data | ||||||||||||||||||||||||||||||||||||||||||||||
| 443 | # to temp table, create new table and then copy as appropriate taking note | ||||||||||||||||||||||||||||||||||||||||||||||
| 444 | # of renamed fields. | ||||||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||||||
| 446 | 5 | my $table_name = $table->name; | |||||||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||||||||
| 448 | 5 | ▶ | if ( @{$diffs->{rename_field}} == 0 && | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
@{$diffs->{"rename_field"};} == 0 and @{$diffs->{"alter_field"};} == 0 and @{$diffs->{"drop_field"};} == 0
@{$diffs->{"rename_field"};} == 0 and @{$diffs->{"alter_field"};} == 0
MC/DC66%2/3
@{$diffs->{"rename_field"};} == 0 and @{$diffs->{"alter_field"};} == 0 and @{$diffs->{"drop_field"};} == 0
@{$diffs->{"rename_field"};} == 0
@{$diffs->{"alter_field"};} == 0
@{$diffs->{"drop_field"};} == 0
Truth table75%3/4
A and B and C
A= @{$diffs->{"rename_field"};} == 0B= @{$diffs->{"alter_field"};} == 0C= @{$diffs->{"drop_field"};} == 0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 449 | 4 | @{$diffs->{alter_field}} == 0 && | |||||||||||||||||||||||||||||||||||||||||||||
| 450 | 4 | @{$diffs->{drop_field}} == 0 | |||||||||||||||||||||||||||||||||||||||||||||
| 451 | ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 452 | 2 | return batch_alter_table_statements($diffs, $options); | |||||||||||||||||||||||||||||||||||||||||||||
| 453 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||||||
| 455 | 3 | my @sql; | |||||||||||||||||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||||||||||||||||
| 457 | # $table is the new table but we may need an old one | ||||||||||||||||||||||||||||||||||||||||||||||
| 458 | # TODO: this is NOT very well tested at the moment so add more tests | ||||||||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||||||
| 460 | 3 | my $old_table = $table; | |||||||||||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||||||||||
| 462 | 3 | ▶ | if ( $diffs->{rename_table} && @{$diffs->{rename_table}} ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$diffs->{"rename_table"} and @{$diffs->{"rename_table"};}
MC/DC0%0/2
$diffs->{"rename_table"} and @{$diffs->{"rename_table"};}
$diffs->{"rename_table"}
@{$diffs->{"rename_table"};}
Truth table33%1/3
A and B
A= $diffs->{"rename_table"}B= @{$diffs->{"rename_table"};}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 463 | 0 | $old_table = $diffs->{rename_table}[0][0]; | |||||||||||||||||||||||||||||||||||||||||||||
| 464 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||||||
| 466 | 3 | my $temp_table_name = $table_name . '_temp_alter'; | |||||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||||||
| 468 | # CREATE TEMPORARY TABLE t1_backup(a,b); | ||||||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||||||
| 470 | 3 | my %temp_table_fields; | |||||||||||||||||||||||||||||||||||||||||||||
| 471 | 3 | do { | |||||||||||||||||||||||||||||||||||||||||||||
| 472 | 3 | local $table->{name} = $temp_table_name; | |||||||||||||||||||||||||||||||||||||||||||||
| 473 | # We only want the table - don't care about indexes on tmp table | ||||||||||||||||||||||||||||||||||||||||||||||
| 474 | 3 | my ($table_sql) = create_table($table, {no_comments => 1, temporary_table => 1}); | |||||||||||||||||||||||||||||||||||||||||||||
| 475 | 3 | push @sql,$table_sql; | |||||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||||||
| 477 | 3 | %temp_table_fields = map { $_ => 1} $table->get_fields; | |||||||||||||||||||||||||||||||||||||||||||||
| 478 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||||||
| 480 | # record renamed fields for later | ||||||||||||||||||||||||||||||||||||||||||||||
| 481 | 3 | my %rename_field = map { $_->[1]->name => $_->[0]->name } @{$diffs->{rename_field}}; | |||||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||||||
| 483 | # drop added fields from %temp_table_fields | ||||||||||||||||||||||||||||||||||||||||||||||
| 484 | 3 | delete @temp_table_fields{@{$diffs->{add_field}}}; | |||||||||||||||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||||||||||||||||
| 486 | # INSERT INTO t1_backup SELECT a,b FROM t1; | ||||||||||||||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||||||||||||||
| 488 | push @sql, sprintf( 'INSERT INTO %s( %s) SELECT %s FROM %s', | ||||||||||||||||||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||||||||||||||||
| 490 | _generator()->quote( $temp_table_name ), | ||||||||||||||||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||||||||||||||
| 492 | join( ', ', | ||||||||||||||||||||||||||||||||||||||||||||||
| 493 | map _generator()->quote($_), | ||||||||||||||||||||||||||||||||||||||||||||||
| 494 | 14 | grep { $temp_table_fields{$_} } $table->get_fields ), | |||||||||||||||||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||||||||||||||||
| 496 | join( ', ', | ||||||||||||||||||||||||||||||||||||||||||||||
| 497 | map _generator()->quote($_), | ||||||||||||||||||||||||||||||||||||||||||||||
| 498 | 11 | ▶ | map { $rename_field{$_} ? $rename_field{$_} : $_ } | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 499 | 3 | grep { $temp_table_fields{$_} } $table->get_fields ), | |||||||||||||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||||||||||||||
| 501 | _generator()->quote( $old_table->name ) | ||||||||||||||||||||||||||||||||||||||||||||||
| 502 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||||||||||||||||||
| 504 | # DROP TABLE t1; | ||||||||||||||||||||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||||||||||||||||||||
| 506 | 3 | push @sql, sprintf('DROP TABLE %s', _generator()->quote($old_table->name)); | |||||||||||||||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||||||||||||||||
| 508 | # CREATE TABLE t1(a,b); | ||||||||||||||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||||||||||||
| 510 | 3 | push @sql, create_table($table, { no_comments => 1 }); | |||||||||||||||||||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||||||||||||||||||||
| 512 | # INSERT INTO t1 SELECT a,b FROM t1_backup; | ||||||||||||||||||||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||||||||||||||||||||
| 514 | 3 | push @sql, sprintf('INSERT INTO %s SELECT %s FROM %s', | |||||||||||||||||||||||||||||||||||||||||||||
| 515 | _generator()->quote($table_name), | ||||||||||||||||||||||||||||||||||||||||||||||
| 516 | join(', ', map _generator()->quote($_), $table->get_fields), | ||||||||||||||||||||||||||||||||||||||||||||||
| 517 | _generator()->quote($temp_table_name) | ||||||||||||||||||||||||||||||||||||||||||||||
| 518 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||||||||||||||||||||
| 520 | # DROP TABLE t1_backup; | ||||||||||||||||||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||||||||||||||||
| 522 | 3 | push @sql, sprintf('DROP TABLE %s', _generator()->quote($temp_table_name)); | |||||||||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||||||||||
| 524 | 3 | ▶ | return wantarray ? @sql : join(";\n", @sql); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 525 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||||||||||||||||||||
| 527 | sub drop_table { | ||||||||||||||||||||||||||||||||||||||||||||||
| 528 | 2 | ▶ | my ($table) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
drop_table: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 529 | 2 | $table = _generator()->quote($table); | |||||||||||||||||||||||||||||||||||||||||||||
| 530 | 2 | return "DROP TABLE $table"; | |||||||||||||||||||||||||||||||||||||||||||||
| 531 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||||||||||||||||
| 533 | sub rename_table { | ||||||||||||||||||||||||||||||||||||||||||||||
| 534 | 2 | ▶ | my ($old_table, $new_table, $options) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rename_table: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||||||||||||||||
| 536 | 2 | $old_table = _generator()->quote($old_table); | |||||||||||||||||||||||||||||||||||||||||||||
| 537 | 2 | $new_table = _generator()->quote($new_table); | |||||||||||||||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||||||||||||||||
| 539 | 2 | return "ALTER TABLE $old_table RENAME TO $new_table"; | |||||||||||||||||||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||||||||||||||||||||
| 541 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||||||||||||||
| 543 | # No-op. Just here to signify that we are a new style parser. | ||||||||||||||||||||||||||||||||||||||||||||||
| 544 | ▶ | sub preproces_schema { } | |||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
preproces_schema: not called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||||||||||||||||
| 546 | 9 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||||||||||||||||
| 548 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||||||||||||
| 550 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||||||||||||||||||||
| 552 | SQL::Translator, http://www.sqlite.org/. | ||||||||||||||||||||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||||||||||||||||||||
| 554 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||||||||||||||||||||
| 556 | Ken Youens-Clark C<< <kclark@cpan.orgE> >>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||||||||||||||||||||
| 558 | Diff code added by Ash Berlin C<< <ash@cpan.org> >>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||||||||||||||||||||
| 560 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||