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