| 1 | package DBIx::Schema::DSL; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 8 | ▶ | use 5.008_001; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 8 | our $VERSION = '0.11'; | |||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 8 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 8 | ▶ | use Array::Diff; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 8 | ▶ | use DBIx::Schema::DSL::Context; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 8 | ▶ | use SQL::Translator::Schema::Constants; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 8 | ▶ | use SQL::Translator::Schema::Field; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | sub context { | ||||||||||||||||||||||||||||||||||||||||||
| 15 | 105 | ▶ | my $pkg = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
context: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 105 | ▶ | die 'something wrong when calling context method.' if $pkg eq __PACKAGE__; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 8 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 105 | ▶ | ${"$pkg\::CONTEXT"} ||= DBIx::Schema::DSL::Context->new; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
${"${pkg}::CONTEXT";} ||= "DBIx::Schema::DSL::Context"->new
MC/DC0%0/2
${"${pkg}::CONTEXT";} ||= "DBIx::Schema::DSL::Context"->new
${"${pkg}::CONTEXT";}
"DBIx::Schema::DSL::Context"->new
Truth table66%2/3
A ||= B
A= ${"${pkg}::CONTEXT";}B= "DBIx::Schema::DSL::Context"->new
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | # don't override CORE::int | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 8 | ▶ | use Pod::Functions (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | my @column_methods = | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 8 | grep {!$Pod::Functions::Type{$_}} grep { /^[a-zA-Z_][0-9a-zA-Z_]*$/ } keys(%SQL::Translator::Schema::Field::type_mapping), qw/string number enum set/; | |||||||||||||||||||||||||||||||||||||||||
| 25 | 8 | my @column_sugars = qw/unique auto_increment unsigned null/; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 8 | my @rev_column_sugars = qw/not_null signed/; | |||||||||||||||||||||||||||||||||||||||||
| 27 | 8 | my @export_dsls = qw/ | |||||||||||||||||||||||||||||||||||||||||
| 28 | create_database database create_table column primary_key set_primary_key add_index add_unique_index | ||||||||||||||||||||||||||||||||||||||||||
| 29 | foreign_key has_many has_one belongs_to add_table_options default_unsigned columns pk fk | ||||||||||||||||||||||||||||||||||||||||||
| 30 | default_not_null | ||||||||||||||||||||||||||||||||||||||||||
| 31 | /; | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 8 | my @class_methods = qw/context output no_fk_output translate_to translator/; | |||||||||||||||||||||||||||||||||||||||||
| 33 | sub import { | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 13 | ▶ | my $caller = caller; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 8 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 13 | for my $func (@export_dsls, @column_methods, @column_sugars, @class_methods, @rev_column_sugars) { | |||||||||||||||||||||||||||||||||||||||||
| 38 | 702 | *{"$caller\::$func"} = \&$func; | |||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 3 | ▶ | sub create_database($) { caller->context->name(shift) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
create_database: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 3 | ▶ | sub database($) { caller->context->db(shift) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
database: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | sub add_table_options { | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 3 | ▶ | my $c = caller->context; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_table_options: called CC=3 SCAR=11.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 3 | my %opt = @_; | |||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | $c->set_table_extra({ | ||||||||||||||||||||||||||||||||||||||||||
| 50 | 3 | %{$c->table_extra}, | |||||||||||||||||||||||||||||||||||||||||
| 51 | %opt, | ||||||||||||||||||||||||||||||||||||||||||
| 52 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 3 | ▶ | if ($opt{mysql_charset} && $opt{mysql_charset} eq 'utf8mb4') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$opt{"mysql_charset"} and $opt{"mysql_charset"} eq "utf8mb4"
MC/DC0%0/2
$opt{"mysql_charset"} and $opt{"mysql_charset"} eq "utf8mb4"
$opt{"mysql_charset"}
$opt{"mysql_charset"} eq "utf8mb4"
Truth table33%1/3
A and B
A= $opt{"mysql_charset"}B= $opt{"mysql_charset"} eq "utf8mb4"
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 3 | $c->default_varchar_size(191); | |||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | sub default_unsigned() { | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 2 | ▶ | caller->context->default_unsigned(1); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_unsigned: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | sub default_not_null() { | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 1 | ▶ | caller->context->default_not_null(1); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_not_null: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | sub create_table($$) { | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 17 | ▶ | my ($table_name, $code) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
create_table: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 17 | my $kls = caller; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 17 | my $c = $kls->context; | |||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 17 | $c->_creating_table({ | |||||||||||||||||||||||||||||||||||||||||
| 74 | table_name => $table_name, | ||||||||||||||||||||||||||||||||||||||||||
| 75 | columns => [], | ||||||||||||||||||||||||||||||||||||||||||
| 76 | indices => [], | ||||||||||||||||||||||||||||||||||||||||||
| 77 | constraints => [], | ||||||||||||||||||||||||||||||||||||||||||
| 78 | primary_key => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 79 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 17 | $code->(); | |||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 15 | my $data = $c->_creating_table; | |||||||||||||||||||||||||||||||||||||||||
| 84 | my $table = $c->schema->add_table( | ||||||||||||||||||||||||||||||||||||||||||
| 85 | name => $table_name, | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 15 | extra => {%{$c->table_extra}}, | |||||||||||||||||||||||||||||||||||||||||
| 87 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 15 | for my $column (@{ $data->{columns} }) { | |||||||||||||||||||||||||||||||||||||||||
| 89 | 52 | $table->add_field(%{ $column } ); | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 15 | my @columns = map {$_->{name}} @{$data->{columns}}; | |||||||||||||||||||||||||||||||||||||||||
| 93 | 15 | for my $index (@{ $data->{indices} }) { | |||||||||||||||||||||||||||||||||||||||||
| 94 | 3 | ▶ | if (my @undefined_columns = _detect_undefined_columns(\@columns, $index->{fields})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 1 | croak "Index error: Key column [@{[join ', ', @undefined_columns]}] doesn't exist in table]"; | |||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | 2 | $table->add_index(%{ $index } ); | |||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | 14 | for my $constraint (@{ $data->{constraints} }) { | |||||||||||||||||||||||||||||||||||||||||
| 100 | 8 | my $cols = $constraint->{fields}; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 8 | ▶ | $cols = [$cols] unless ref $cols; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 8 | ▶ | if (my @undefined_columns = _detect_undefined_columns(\@columns, $cols)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | croak "Constraint error: Key column [@{[join ', ', @undefined_columns]}] doesn't exist in table]"; | |||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 8 | $table->add_constraint(%{ $constraint } ); | |||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 14 | ▶ | if (my $pk = $data->{primary_key}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 12 | ▶ | $pk = [$pk] unless ref $pk; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 12 | ▶ | if (my @undefined_columns = _detect_undefined_columns(\@columns, $pk)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 1 | croak "Primary key error: Key column [@{[join ', ', @undefined_columns]}] doesn't exist in table]"; | |||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 11 | $table->primary_key($data->{primary_key}); | |||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 13 | $c->_clear_creating_table; | |||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 17 | ▶ | sub columns(&) {shift} | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
columns: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | sub _detect_undefined_columns { | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 23 | ▶ | my ($set, $subset) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_detect_undefined_columns: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | 23 | my $diff = Array::Diff->diff([sort @$set], [sort @$subset]); | |||||||||||||||||||||||||||||||||||||||||
| 124 | 23 | @{$diff->added}; | |||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | sub column($$;%) { | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 56 | ▶ | my ($column_name, $data_type, @opt) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
column: called CC=24 SCAR=31.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 56 | ▶ | croak '`column` function called in non void context' if defined wantarray; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 55 | ▶ | if (ref $opt[0] eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | # enum or set | ||||||||||||||||||||||||||||||||||||||||||
| 133 | 1 | unshift @opt, 'list'; | |||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | 55 | ▶ | if (@opt % 2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 1 | croak "odd number elements are assined to options. arguments: [@{[join ', ', @_]}]"; | |||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 54 | my %opt = @opt; | |||||||||||||||||||||||||||||||||||||||||
| 140 | 54 | ▶ | $data_type = 'varchar' if $data_type eq 'string'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 54 | my $c = caller->context; | |||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | 54 | ▶ | my $creating_data = $c->_creating_table | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | or croak q{can't call `column` method outside `create_table` method}; | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | 54 | my %args = ( | |||||||||||||||||||||||||||||||||||||||||
| 148 | name => $column_name, | ||||||||||||||||||||||||||||||||||||||||||
| 149 | data_type => uc $data_type, | ||||||||||||||||||||||||||||||||||||||||||
| 150 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | 54 | my %map = ( | |||||||||||||||||||||||||||||||||||||||||
| 153 | null => 'is_nullable', | ||||||||||||||||||||||||||||||||||||||||||
| 154 | limit => 'size', | ||||||||||||||||||||||||||||||||||||||||||
| 155 | default => 'default_value', | ||||||||||||||||||||||||||||||||||||||||||
| 156 | unique => 'is_unique', | ||||||||||||||||||||||||||||||||||||||||||
| 157 | primary_key => 'is_primary_key', | ||||||||||||||||||||||||||||||||||||||||||
| 158 | auto_increment => 'is_auto_increment', | ||||||||||||||||||||||||||||||||||||||||||
| 159 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 54 | for my $key (keys %map) { | |||||||||||||||||||||||||||||||||||||||||
| 161 | 324 | ▶ | $args{$map{$key}} = delete $opt{$key} if exists $opt{$key}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||||||||
| 163 | %args = ( | ||||||||||||||||||||||||||||||||||||||||||
| 164 | 54 | %args, | |||||||||||||||||||||||||||||||||||||||||
| 165 | %opt | ||||||||||||||||||||||||||||||||||||||||||
| 166 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | 54 | ▶ | if (exists $args{unsigned}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | 3 | $args{extra}{unsigned} = delete $args{unsigned}; | |||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||
| 171 | ▶ | elsif ($c->default_unsigned && $data_type =~ /int(?:eger)?$/) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$c->default_unsigned and $data_type =~ /int(?:eger)?$/
MC/DC100%2/2
$c->default_unsigned and $data_type =~ /int(?:eger)?$/
$c->default_unsigned
$data_type =~ /int(?:eger)?$/
Truth table100%3/3
A and B
A= $c->default_unsignedB= $data_type =~ /int(?:eger)?$/
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | 9 | $args{extra}{unsigned} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | 54 | ▶ | if (exists $args{on_update}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | 1 | $args{extra}{'on update'} = delete $args{on_update}; | |||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | 54 | ▶ | if (exists $args{list}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | 1 | $args{extra}{list} = delete $args{list}; | |||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | 54 | ▶ | if ( !exists $args{is_nullable} && $c->default_not_null ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not exists $args{"is_nullable"} and $c->default_not_null
MC/DC100%2/2
not exists $args{"is_nullable"} and $c->default_not_null
not exists $args{"is_nullable"}
$c->default_not_null
Truth table100%3/3
not(A) and B
A= not exists $args{"is_nullable"}B= $c->default_not_null
| |||||||||||||||||||||||||||||||||||||||||||
| 185 | 2 | $args{is_nullable} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | 54 | ▶ | if ($args{data_type} eq 'VARCHAR' && !$args{size}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$args{"data_type"} eq "VARCHAR" and not $args{"size"}
MC/DC50%1/2
$args{"data_type"} eq "VARCHAR" and not $args{"size"}
$args{"data_type"} eq "VARCHAR"
not $args{"size"}
Truth table66%2/3
A and not(B)
A= $args{"data_type"} eq "VARCHAR"B= not $args{"size"}
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 15 | $args{size} = $c->default_varchar_size; | |||||||||||||||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | 54 | ▶ | if ($args{precision}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | 2 | my $precision = delete $args{precision}; | |||||||||||||||||||||||||||||||||||||||||
| 194 | 2 | ▶ | my $scale = delete $args{scale} || 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $args{'scale'} || 0
MC/DC0%0/1
delete $args{'scale'} || 0
delete $args{'scale'}
Truth table50%1/2
A
A= delete $args{'scale'}
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | 2 | $args{size} = [$precision, $scale]; | |||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | 54 | ▶ | if ($args{is_primary_key}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | 12 | $creating_data->{primary_key} = $column_name; | |||||||||||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||||||||||
| 201 | elsif ($args{is_unique}) { | ||||||||||||||||||||||||||||||||||||||||||
| 202 | 5 | push @{$creating_data->{constraints}}, { | |||||||||||||||||||||||||||||||||||||||||
| 203 | name => "${column_name}_uniq", | ||||||||||||||||||||||||||||||||||||||||||
| 204 | fields => [$column_name], | ||||||||||||||||||||||||||||||||||||||||||
| 205 | type => UNIQUE, | ||||||||||||||||||||||||||||||||||||||||||
| 206 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | # explicitly add `DEFAULT NULL` if is_nullable and not specified default_value | ||||||||||||||||||||||||||||||||||||||||||
| 210 | 54 | ▶ | if ($args{is_nullable} && !exists $args{default_value} && $args{data_type} !~ /^(?:TINY|MEDIUM|LONG)?(?:TEXT|BLOB)$/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$args{"is_nullable"} and not exists $args{"default_value"} and not $args{"data_type"} =~ /^(?:TINY|MEDIUM|LONG)?(?:TEXT|BLOB)$/
$args{"is_nullable"} and not exists $args{"default_value"}
MC/DC33%1/3
$args{"is_nullable"} and not exists $args{"default_value"} and not $args{"data_type"} =~ /^(?:TINY|MEDIUM|LONG)?(?:TEXT|BLOB)$/
$args{"is_nullable"}
not exists $args{"default_value"}
not $args{"data_type"} =~ /^(?:TINY|MEDIUM|LONG)?(?:TEXT|BLOB)$/
Truth table50%2/4
A and not(B) and not(C)
A= $args{"is_nullable"}B= not exists $args{"default_value"}C= not $args{"data_type"} =~ /^(?:TINY|MEDIUM|LONG)?(?:TEXT|BLOB)$/
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | 10 | $args{default_value} = \'NULL'; | |||||||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | 54 | push @{$creating_data->{columns}}, \%args; | |||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||
| 217 | sub primary_key { | ||||||||||||||||||||||||||||||||||||||||||
| 218 | 9 | ▶ | if (defined wantarray) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
primary_key: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | 7 | (primary_key => 1); | |||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||
| 221 | else { # void context | ||||||||||||||||||||||||||||||||||||||||||
| 222 | 2 | my $column_name = shift; | |||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | 2 | @_ = ($column_name, 'integer', primary_key(), auto_increment(), @_); | |||||||||||||||||||||||||||||||||||||||||
| 225 | 2 | goto \&column; | |||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||
| 228 | 8 | *pk = \&primary_key; | |||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | 8 | for my $method (@column_methods) { | |||||||||||||||||||||||||||||||||||||||||
| 231 | 8 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 232 | 200 | *{__PACKAGE__."::$method"} = sub { | |||||||||||||||||||||||||||||||||||||||||
| 233 | 8 | ▶ | use strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 234 | 51 | ▶ | my $column_name = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | 51 | @_ = ($column_name, $method, @_); | |||||||||||||||||||||||||||||||||||||||||
| 237 | 51 | goto \&column; | |||||||||||||||||||||||||||||||||||||||||
| 238 | 200 | }; | |||||||||||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | 8 | for my $method (@column_sugars) { | |||||||||||||||||||||||||||||||||||||||||
| 242 | 8 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 243 | 32 | *{__PACKAGE__."::$method"} = sub() { | |||||||||||||||||||||||||||||||||||||||||
| 244 | 8 | ▶ | use strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 245 | 20 | ▶ | ($method => 1); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | 32 | }; | |||||||||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||||||||
| 248 | 0 | ▶ | sub not_null() { (null => 0) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
not_null: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | ▶ | sub signed() { (unsigned => 0) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
signed: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | sub set_primary_key(@) { | ||||||||||||||||||||||||||||||||||||||||||
| 252 | 3 | ▶ | my @keys = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_primary_key: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | 3 | my $c = caller->context; | |||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | 3 | ▶ | my $creating_data = $c->_creating_table | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | or die q{can't call `set_primary_key` method outside `create_table` method}; | ||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | 3 | $creating_data->{primary_key} = \@keys; | |||||||||||||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | sub add_index { | ||||||||||||||||||||||||||||||||||||||||||
| 263 | 2 | ▶ | my $c = caller->context; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_index: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||
| 265 | 2 | ▶ | my $creating_data = $c->_creating_table | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 266 | or die q{can't call `add_index` method outside `create_table` method}; | ||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||
| 268 | 2 | my ($idx_name, $fields, $type) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||
| 270 | 2 | ▶ | push @{$creating_data->{indices}}, { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 271 | name => $idx_name, | ||||||||||||||||||||||||||||||||||||||||||
| 272 | fields => $fields, | ||||||||||||||||||||||||||||||||||||||||||
| 273 | ($type ? (type => $type) : ()), | ||||||||||||||||||||||||||||||||||||||||||
| 274 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | sub add_unique_index { | ||||||||||||||||||||||||||||||||||||||||||
| 278 | 1 | ▶ | my $c = caller->context; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_unique_index: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||
| 280 | 1 | ▶ | my $creating_data = $c->_creating_table | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 281 | or die q{can't call `add_unique_index` method outside `create_table` method}; | ||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | 1 | my ($idx_name, $fields) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | 1 | push @{$creating_data->{indices}}, { | |||||||||||||||||||||||||||||||||||||||||
| 286 | name => $idx_name, | ||||||||||||||||||||||||||||||||||||||||||
| 287 | fields => $fields, | ||||||||||||||||||||||||||||||||||||||||||
| 288 | type => UNIQUE, | ||||||||||||||||||||||||||||||||||||||||||
| 289 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||
| 292 | sub foreign_key { | ||||||||||||||||||||||||||||||||||||||||||
| 293 | 4 | ▶ | my $c = caller->context; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
foreign_key: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | 4 | ▶ | my $creating_data = $c->_creating_table | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 296 | or die q{can't call `foreign` method outside `create_table` method}; | ||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | 4 | my ($columns, $table, $foreign_columns, %opt) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||
| 300 | 4 | push @{$creating_data->{constraints}}, { | |||||||||||||||||||||||||||||||||||||||||
| 301 | type => FOREIGN_KEY, | ||||||||||||||||||||||||||||||||||||||||||
| 302 | fields => $columns, | ||||||||||||||||||||||||||||||||||||||||||
| 303 | reference_table => $table, | ||||||||||||||||||||||||||||||||||||||||||
| 304 | reference_fields => $foreign_columns, | ||||||||||||||||||||||||||||||||||||||||||
| 305 | %opt, | ||||||||||||||||||||||||||||||||||||||||||
| 306 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||||||||||
| 308 | 8 | *fk = \&foreign_key; | |||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||
| 310 | sub has_many { | ||||||||||||||||||||||||||||||||||||||||||
| 311 | 2 | ▶ | my $c = caller->context; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
has_many: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||
| 313 | 2 | my ($table, %opt) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | 2 | ▶ | my $columns = delete $opt{column} || 'id'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $opt{'column'} || 'id'
MC/DC0%0/1
delete $opt{'column'} || 'id'
delete $opt{'column'}
Truth table50%1/2
A
A= delete $opt{'column'}
| |||||||||||||||||||||||||||||||||||||||||||
| 316 | 2 | ▶ | my $foreign_columns = delete $opt{foreign_column} || $c->_creating_table_name .'_id'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $opt{'foreign_column'} || $c->_creating_table_name . '_id'
MC/DC0%0/1
delete $opt{'foreign_column'} || $c->_creating_table_name . '_id'
delete $opt{'foreign_column'}
Truth table50%1/2
A
A= delete $opt{'foreign_column'}
| |||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||
| 318 | 2 | @_ = ($columns, $table, $foreign_columns, %opt); | |||||||||||||||||||||||||||||||||||||||||
| 319 | 2 | goto \&foreign_key; | |||||||||||||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||
| 322 | sub has_one { | ||||||||||||||||||||||||||||||||||||||||||
| 323 | 0 | ▶ | my $c = caller->context; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
has_one: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||
| 325 | 0 | my ($table, %opt) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | 0 | ▶ | my $columns = delete $opt{column} || 'id'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
delete $opt{'column'} || 'id'
MC/DC0%0/1
delete $opt{'column'} || 'id'
delete $opt{'column'}
Truth table0%0/2
A
A= delete $opt{'column'}
| |||||||||||||||||||||||||||||||||||||||||||
| 328 | 0 | ▶ | my $foreign_columns = delete $opt{foreign_column} || $c->_creating_table_name .'_id'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
delete $opt{'foreign_column'} || $c->_creating_table_name . '_id'
MC/DC0%0/1
delete $opt{'foreign_column'} || $c->_creating_table_name . '_id'
delete $opt{'foreign_column'}
Truth table0%0/2
A
A= delete $opt{'foreign_column'}
| |||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||
| 330 | 0 | @_ = ($columns, $table, $foreign_columns, %opt); | |||||||||||||||||||||||||||||||||||||||||
| 331 | 0 | goto \&foreign_key; | |||||||||||||||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||
| 334 | sub belongs_to { | ||||||||||||||||||||||||||||||||||||||||||
| 335 | 2 | ▶ | my ($table, %opt) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
belongs_to: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | 2 | ▶ | my $columns = delete $opt{column} || "${table}_id"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $opt{'column'} || "${table}_id"
MC/DC0%0/1
delete $opt{'column'} || "${table}_id"
delete $opt{'column'}
Truth table50%1/2
A
A= delete $opt{'column'}
| |||||||||||||||||||||||||||||||||||||||||||
| 338 | 2 | ▶ | my $foreign_columns = delete $opt{foreign_column} || 'id'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $opt{'foreign_column'} || 'id'
MC/DC0%0/1
delete $opt{'foreign_column'} || 'id'
delete $opt{'foreign_column'}
Truth table50%1/2
A
A= delete $opt{'foreign_column'}
| |||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||
| 340 | 2 | @_ = ($columns, $table, $foreign_columns, %opt); | |||||||||||||||||||||||||||||||||||||||||
| 341 | 2 | goto \&foreign_key; | |||||||||||||||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||
| 344 | sub output { | ||||||||||||||||||||||||||||||||||||||||||
| 345 | 5 | ▶ | shift->context->translate; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
output: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||
| 348 | sub no_fk_output { | ||||||||||||||||||||||||||||||||||||||||||
| 349 | 1 | ▶ | shift->context->no_fk_translate; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
no_fk_output: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||
| 352 | sub translator { | ||||||||||||||||||||||||||||||||||||||||||
| 353 | 1 | ▶ | shift->context->translator; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
translator: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||
| 356 | sub translate_to { | ||||||||||||||||||||||||||||||||||||||||||
| 357 | 1 | ▶ | my ($kls, $db_type) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
translate_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||
| 359 | 1 | $kls->translator->translate(to => $db_type); | |||||||||||||||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||
| 362 | 8 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 363 | __END__ | ||||||||||||||||||||||||||||||||||||||||||