| 1 | package DBIO::SQLite::DDL; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Generate SQLite DDL from DBIO Result classes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 194 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 194 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 194 | ▶ | use DBIO::SQL::Util qw(_quote_ident); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 194 | ▶ | use DBIO::Schema::Type (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 194 | ▶ | use DBIO::SQLite::Adapter (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 194 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 194 | my $ADAPTER = DBIO::SQLite::Adapter->new; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 352 | ▶ | sub install_ddl { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
install_ddl: called CC=53 SCAR=43.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 352 | my ($class, $schema) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 352 | my @stmts; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | my @view_stmts; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 352 | my %seen_table; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 352 | my %seen_view; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 352 | for my $source_name (sort $schema->sources) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 15096 | my $source = $schema->source($source_name); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 15096 | my $result_class = $source->result_class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 15096 | my $table_name = $source->name; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | # Views: emit CREATE VIEW from the stored definition, after all | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | # tables (they may reference any table). Virtual views are ORM-only | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | # and have no database object to create. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 15096 | ▶ | if ($source->isa('DBIO::ResultSource::View')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 670 | ▶ | next if $source->can('is_virtual') && $source->is_virtual; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$source->can("is_virtual") and $source->is_virtual
MC/DC50%1/2
$source->can("is_virtual") and $source->is_virtual
$source->can("is_virtual")
$source->is_virtual
Truth table66%2/3
A and B
A= $source->can("is_virtual")B= $source->is_virtual
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 335 | ▶ | my $vname = ref $table_name eq 'SCALAR' ? $$table_name : $table_name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 335 | ▶ | next if ref $vname || $seen_view{$vname}++; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $vname or $seen_view{$vname}++
MC/DC0%0/2
ref $vname or $seen_view{$vname}++
ref $vname
$seen_view{$vname}++
Truth table33%1/3
A or B
A= ref $vnameB= $seen_view{$vname}++
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 335 | ▶ | my $def = $source->can('view_definition') ? $source->view_definition : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 335 | ▶ | next unless defined $def && length $def; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $def and length $def
MC/DC0%0/2
defined $def and length $def
defined $def
length $def
Truth table33%1/3
A and B
A= defined $defB= length $def
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 335 | push @view_stmts, sprintf 'CREATE VIEW %s AS %s;', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | _quote_ident($vname), $def; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 335 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | # A scalar-ref name is either a literal table name (\'cd', emitted | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | # unquoted) or an inline subquery source (\'(SELECT ...)'). Deref the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | # former; the latter has no table to create. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 14426 | ▶ | if (ref $table_name eq 'SCALAR') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 669 | $table_name = $$table_name; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 669 | ▶ | next if $table_name =~ /\s|\(/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 14091 | ▶ | next if ref $table_name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | # Several result sources can map to the same physical table (e.g. a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | # Result subclass or a source_name alias). Emit each table once. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 14091 | ▶ | next if $seen_table{$table_name}++; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 13421 | my @col_defs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | my %is_pk; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 13421 | my @pk_cols = $source->primary_columns; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 13421 | @is_pk{@pk_cols} = (1) x @pk_cols; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | # Columns that participate in a UNIQUE constraint are implicitly NOT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | # NULL in well-formed schemas (a unique constraint permits any number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | # of NULLs, so NOT NULL is what makes the constraint meaningful). DBIO | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | # schema definitions sometimes omit is_nullable on such columns and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | # rely on the unique declaration to convey it -- promote them to NOT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | # NULL so the storage layer reports the right is_nullable. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 13421 | my %unique_col; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 13421 | ▶ | if ($source->can('unique_constraints')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 13421 | my %uniques = $source->unique_constraints; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 13421 | for my $uname (sort keys %uniques) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 18781 | ▶ | next if $uname eq 'primary'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 5695 | my $cols = $uniques{$uname}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 5695 | $unique_col{$_} = 1 for @$cols; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 13421 | for my $col_name ($source->columns) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 43615 | my $info = $source->column_info($col_name); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 43615 | my $type = _sqlite_column_type($info); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 43615 | my $def = sprintf ' %s %s', _quote_ident($col_name), $type; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | # A plain INTEGER PRIMARY KEY column is already a SQLite rowid alias -- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | # no AUTOINCREMENT keyword needed. AUTOINCREMENT only adds the extra | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | # guarantee that rowids are never reused after DELETE (tracked via | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | # sqlite_sequence), which breaks tests that delete rows then re-insert | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | # and expect the rowid to start back at 1. Emit plain PRIMARY KEY, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | # matching DBIx::Class / SQL::Translator's SQLite producer. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 43615 | ▶ | if ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%9/9
@pk_cols == 1 and $is_pk{$col_name} and uc $type eq "INTEGER" and $info->{"is_auto_increment"}
@pk_cols == 1 and $is_pk{$col_name} and uc $type eq "INTEGER"
@pk_cols == 1 and $is_pk{$col_name}
MC/DC100%4/4
@pk_cols == 1 and $is_pk{$col_name} and uc $type eq "INTEGER" and $info->{"is_auto_increment"}
@pk_cols == 1
$is_pk{$col_name}
uc $type eq "INTEGER"
$info->{"is_auto_increment"}
Truth table100%5/5
A and B and C and D
A= @pk_cols == 1B= $is_pk{$col_name}C= uc $type eq "INTEGER"D= $info->{"is_auto_increment"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | @pk_cols == 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | && $is_pk{$col_name} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | && uc($type) eq 'INTEGER' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | && $info->{is_auto_increment} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 8061 | $def .= ' PRIMARY KEY'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | my $is_nullable = exists $info->{is_nullable} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | ? $info->{is_nullable} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 43615 | ▶ | : ($unique_col{$col_name} ? 0 : undef); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 43615 | ▶ | $def .= ' NOT NULL' if defined $is_nullable && !$is_nullable; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $is_nullable and not $is_nullable
MC/DC100%2/2
defined $is_nullable and not $is_nullable
defined $is_nullable
not $is_nullable
Truth table100%3/3
A and not(B)
A= defined $is_nullableB= not $is_nullable
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 43615 | ▶ | if (defined $info->{default_value}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 1005 | my $dv = $info->{default_value}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 1005 | ▶ | if (ref $dv eq 'SCALAR') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 335 | $def .= " DEFAULT $$dv"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | } elsif ($dv =~ /\A-?\d+(?:\.\d+)?\z/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | # Numeric literals: emit unquoted so that the storage layer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | # reports the default as the bare number, not a string. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 670 | $def .= " DEFAULT $dv"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | $def .= " DEFAULT '$dv'"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 43615 | push @col_defs, $def; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | # Multi-column or non-INTEGER primary key: emit as a table-level constraint | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | my $had_inline_pk = (@pk_cols == 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | && exists $is_pk{ $pk_cols[0] } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 13421 | ▶ | && do { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition83%5/6
@pk_cols == 1 && exists $is_pk{$pk_cols[0]} && do {
@pk_cols == 1 and exists $is_pk{$pk_cols[0]}
MC/DC66%2/3
@pk_cols == 1 && exists $is_pk{$pk_cols[0]} && do {
my $info = $source->column_info($pk_cols[0]);
uc _sqlite_column_type($info) eq 'INTEGER' and $info->{'is_auto_increment'}
}
@pk_cols == 1
exists $is_pk{$pk_cols[0]}
do {
my $info = $source->column_info($pk_cols[0]);
uc _sqlite_column_type($info) eq 'INTEGER' and $info->{'is_auto_increment'}
}
Truth table75%3/4
A and B && C
A= @pk_cols == 1B= exists $is_pk{$pk_cols[0]}C= do {
my $info = $source->column_info($pk_cols[0]);
uc _sqlite_column_type($info) eq 'INTEGER' and $info->{'is_auto_increment'}
}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 10071 | my $info = $source->column_info($pk_cols[0]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 10071 | ▶ | uc(_sqlite_column_type($info)) eq 'INTEGER' && $info->{is_auto_increment}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
uc _sqlite_column_type($info) eq 'INTEGER' && $info->{'is_auto_increment'}
MC/DC100%2/2
uc _sqlite_column_type($info) eq 'INTEGER' && $info->{'is_auto_increment'}
uc _sqlite_column_type($info) eq 'INTEGER'
$info->{'is_auto_increment'}
Truth table100%3/3
A && B
A= uc _sqlite_column_type($info) eq 'INTEGER'B= $info->{'is_auto_increment'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 13421 | ▶ | if (@pk_cols && !$had_inline_pk) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@pk_cols and not $had_inline_pk
MC/DC100%2/2
@pk_cols and not $had_inline_pk
@pk_cols
not $had_inline_pk
Truth table100%3/3
A and not(B)
A= @pk_colsB= not $had_inline_pk
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | push @col_defs, sprintf ' PRIMARY KEY (%s)', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 5025 | join(', ', map { _quote_ident($_) } @pk_cols); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | # Unique constraints declared via add_unique_constraint | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 13421 | ▶ | if ($source->can('unique_constraints')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 13421 | my %uniques = $source->unique_constraints; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 13421 | for my $uname (sort keys %uniques) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 18781 | ▶ | next if $uname eq 'primary'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 5695 | my $cols = $uniques{$uname}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | push @col_defs, sprintf ' UNIQUE (%s)', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 5695 | join(', ', map { _quote_ident($_) } @$cols); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | # Foreign keys derived from belongs_to relationships | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 13421 | for my $rel ($source->relationships) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 28145 | my $info = $source->relationship_info($rel); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 28145 | ▶ | next unless $info && $info->{attrs} && $info->{attrs}{is_foreign_key_constraint}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$info and $info->{"attrs"} and $info->{"attrs"}{"is_foreign_key_constraint"}
$info and $info->{"attrs"}
MC/DC33%1/3
$info and $info->{"attrs"} and $info->{"attrs"}{"is_foreign_key_constraint"}
$info
$info->{"attrs"}
$info->{"attrs"}{"is_foreign_key_constraint"}
Truth table50%2/4
A and B and C
A= $infoB= $info->{"attrs"}C= $info->{"attrs"}{"is_foreign_key_constraint"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 13404 | my $foreign = $info->{class}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 13404 | my $foreign_source = eval { $schema->source($foreign) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 13404 | ▶ | // eval { $schema->source($foreign =~ s/.*:://r) }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
eval { // eval {
MC/DC0%0/2
eval {
do {
$schema->source($foreign)
}
} // eval {
do {
$schema->source($foreign =~ s/.*:://r)
}
}
eval {
do {
$schema->source($foreign)
}
}
eval {
do {
$schema->source($foreign =~ s/.*:://r)
}
}
Truth table33%1/3
A // B
A= eval {
do {
$schema->source($foreign)
}
}B= eval {
do {
$schema->source($foreign =~ s/.*:://r)
}
}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 13404 | ▶ | next unless $foreign_source; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 13404 | my $cond = $info->{cond}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 13404 | ▶ | next unless ref $cond eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 12064 | my (@from, @to); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 12064 | for my $foreign_col (sort keys %$cond) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 13739 | my $fcol = $foreign_col; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 13739 | $fcol =~ s/^foreign\.//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 13739 | my $self_col = $cond->{$foreign_col}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 13739 | $self_col =~ s/^self\.//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 13739 | push @to, $fcol; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 13739 | push @from, $self_col; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | # A literal table name (\'cd') arrives as a scalar ref; deref it so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | # the FK references the table, not "SCALAR(0x...)". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 12064 | my $foreign_name = $foreign_source->name; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 12064 | ▶ | $foreign_name = $$foreign_name if ref $foreign_name eq 'SCALAR'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | push @col_defs, sprintf ' FOREIGN KEY (%s) REFERENCES %s(%s)', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 13739 | join(', ', map { _quote_ident($_) } @from), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | _quote_ident($foreign_name), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 12064 | join(', ', map { _quote_ident($_) } @to); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 13421 | push @stmts, sprintf "CREATE TABLE %s (\n%s\n);", | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | _quote_ident($table_name), join(",\n", @col_defs); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | # Standalone (non-unique-constraint) indexes -- only if the result | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | # class declares them via sqlite_indexes (parallel to pg_indexes) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 13421 | ▶ | if ($result_class->can('sqlite_indexes')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | my $indexes = $result_class->sqlite_indexes; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | for my $idx_name (sort keys %$indexes) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | my $idx = $indexes->{$idx_name}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | ▶ | my $unique = $idx->{unique} ? 'UNIQUE ' : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | my $columns = join ', ', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 0 | ▶ | map { _quote_ident($_) } @{ $idx->{columns} // [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$idx->{"columns"} // []
MC/DC0%0/1
$idx->{"columns"} // []
$idx->{"columns"}
Truth table0%0/2
A
A= $idx->{"columns"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | my $sql = sprintf 'CREATE %sINDEX %s ON %s (%s)', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | $unique, _quote_ident($idx_name), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | _quote_ident($table_name), $columns; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | ▶ | $sql .= " WHERE $idx->{where}" if $idx->{where}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | push @stmts, "$sql;"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 352 | return join "\n\n", @stmts, @view_stmts; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 53686 | ▶ | sub _sqlite_column_type { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_sqlite_column_type: called CC=8 SCAR=21.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 53686 | my ($info) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 53686 | ▶ | my $type = $info->{data_type} // 'TEXT'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$info->{"data_type"} // "TEXT"
MC/DC0%0/1
$info->{"data_type"} // "TEXT"
$info->{"data_type"}
Truth table50%1/2
A
A= $info->{"data_type"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | # Pre-parameterized types pass through unchanged | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 53686 | ▶ | return $type if $type =~ /\(.+\)$/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | # Portable base types: delegate to the adapter for the single source of truth | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 53686 | ▶ | if (DBIO::Schema::Type::is_base_type(lc $type)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 39269 | my $canon = eval { DBIO::Schema::Type::canonical_column('_', $info) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 39269 | ▶ | return $ADAPTER->to_native($canon) if $canon; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | # Legacy / dialect aliases: keep familiar SQLite-dialect names so that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | # introspection round-trips cleanly (e.g. VARCHAR stays VARCHAR, not TEXT). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 14417 | my %type_map = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | bigint => 'INTEGER', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | smallint => 'INTEGER', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | int => 'INTEGER', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | serial => 'INTEGER', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | bigserial => 'INTEGER', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | varchar => 'VARCHAR', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | char => 'CHAR', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | string => 'TEXT', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | real => 'REAL', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | float => 'REAL', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 'double precision' => 'REAL', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | decimal => 'NUMERIC', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | bytea => 'BLOB', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | date => 'DATE', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | datetime => 'DATETIME', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | time => 'TIME', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 14417 | ▶ | my $native = $type_map{ lc $type } // uc $type; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$type_map{lc $type} // uc $type
MC/DC0%0/2
$type_map{lc $type} // uc $type
$type_map{lc $type}
uc $type
Truth table66%2/3
A // B
A= $type_map{lc $type}B= uc $type
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | # Parametrize character types when a size is declared -- DBD::SQLite's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | # column_info reports COLUMN_SIZE from the declared VARCHAR(N)/CHAR(N), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | # and downstream consumers (e.g. tests, schema diffs) expect it back. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 14417 | ▶ | if (defined $info->{size} && $native =~ /^(?:VARCHAR|CHAR)$/i) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $info->{"size"} and $native =~ /^(?:VARCHAR|CHAR)$/i
MC/DC100%2/2
defined $info->{"size"} and $native =~ /^(?:VARCHAR|CHAR)$/i
defined $info->{"size"}
$native =~ /^(?:VARCHAR|CHAR)$/i
Truth table100%3/3
A and B
A= defined $info->{"size"}B= $native =~ /^(?:VARCHAR|CHAR)$/i
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 9057 | $native .= "($info->{size})"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 14417 | return $native; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 194 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | DBIO::SQLite::DDL - Generate SQLite DDL from DBIO Result classes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | version 0.900001 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | C<DBIO::SQLite::DDL> generates a SQLite DDL script from the L<DBIO::Schema> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | class hierarchy. It is the desired-state side of the test-deploy-and-compare | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | strategy used by L<DBIO::SQLite::Deploy>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | my $ddl = DBIO::SQLite::DDL->install_ddl($schema_class_or_instance); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | # CREATE TABLE ...; CREATE INDEX ...; ... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | The output is plain SQL, suitable for executing one statement at a time | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | against a fresh SQLite database. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | SQLite has neither schemas nor sequences nor functions/triggers/RLS, so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | the generated DDL is much smaller than the PostgreSQL equivalent. The | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | only constructs emitted are C<CREATE TABLE> (with inline columns, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | primary key, unique constraints, foreign keys) and C<CREATE INDEX>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | =head2 install_ddl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | my $ddl = DBIO::SQLite::DDL->install_ddl($schema); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | Returns the full installation DDL as a single string. C<$schema> may be | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | a connected schema instance or a schema class name. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | DBIO & DBIx::Class Authors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | Copyright (C) 2026 DBIO Authors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | Based on DBIx::Class, heavily modified. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||