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

filter: Statementstmt 78.6% 144 / 183 Branchbran 55.4% 51 / 92 Conditioncond 58.1% 42 / 74 (1 uncoverable) MC/DCmcdc 34.8% 14 / 43 (1 uncoverable) Subroutinesub 86.6% 26 / 30 Podpod 0.0% 0 / 1 totaltotal 65.9% 277 / 423 (2 uncoverable) CC 74 SCAR 26.7
CC
74
Cov
68%
_field_type
31.3
_fk_type
29.8
_field_options
27.8
CRAP 14.5
↑ Summary
1 package SQL::Translator::Producer::DBIxSchemaDSL;
2 4 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 our $VERSION = "0.04";
7  
8 4 ▶use DBI qw/:sql_types/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Text::MicroTemplate;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Scalar::Util qw/looks_like_number/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use SQL::Translator::Schema::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Data::Dumper ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 my $_RENDER = Text::MicroTemplate->new(
16 4 template => do { local $/; <DATA> },
17 package_name => __PACKAGE__,
18 escape_func => undef,
19 )->build;
20 4 my %_NAMEMAP = map { $_ => *{$DBI::{$_}}{CODE}->() } @{ $DBI::EXPORT_TAGS{sql_types} };
21  
22 4 our %TYPEMAP = (
23 SQL_GUID() => 'varchar',
24 SQL_WLONGVARCHAR() => 'text',
25 SQL_WVARCHAR() => 'varchar',
26 SQL_WCHAR() => 'char',
27 SQL_BIGINT() => 'bigint',
28 SQL_BIT() => 'bit',
29 SQL_TINYINT() => 'tinyint',
30 SQL_LONGVARBINARY() => 'blob',
31 SQL_VARBINARY() => 'varbinary',
32 SQL_BINARY() => 'binary',
33 SQL_LONGVARCHAR() => 'text',
34 SQL_UNKNOWN_TYPE() => 'blob',
35 SQL_ALL_TYPES() => 'blob',
36 SQL_CHAR() => 'char',
37 SQL_NUMERIC() => 'numeric',
38 SQL_DECIMAL() => 'decimal',
39 SQL_INTEGER() => 'integer',
40 SQL_SMALLINT() => 'smallint',
41 SQL_FLOAT() => 'float',
42 SQL_REAL() => 'real',
43 SQL_DOUBLE() => 'double',
44 SQL_DATETIME() => 'datetime',
45 SQL_DATE() => 'date',
46 SQL_INTERVAL() => 'integer',
47 SQL_TIME() => 'time',
48 SQL_TIMESTAMP() => 'timestamp',
49 SQL_VARCHAR() => 'varchar',
50 SQL_BOOLEAN() => 'tinyint',
51 SQL_UDT() => 'string',
52 SQL_UDT_LOCATOR() => 'string',
53 SQL_ROW() => 'string',
54 SQL_REF() => 'string',
55 SQL_BLOB() => 'blob',
56 SQL_BLOB_LOCATOR() => 'blob',
57 SQL_CLOB() => 'blob',
58 SQL_CLOB_LOCATOR() => 'blob',
59 SQL_ARRAY() => 'blob',
60 SQL_ARRAY_LOCATOR() => 'blob',
61 SQL_MULTISET() => 'blob',
62 SQL_MULTISET_LOCATOR() => 'blob',
63 SQL_TYPE_DATE() => 'date',
64 SQL_TYPE_TIME() => 'time',
65 SQL_TYPE_TIMESTAMP() => 'timestamp',
66 SQL_TYPE_TIME_WITH_TIMEZONE() => 'timestamp',
67 SQL_TYPE_TIMESTAMP_WITH_TIMEZONE() => 'timestamp',
68 SQL_INTERVAL_YEAR() => 'tinyint',
69 SQL_INTERVAL_MONTH() => 'tinyint',
70 SQL_INTERVAL_DAY() => 'tinyint',
71 SQL_INTERVAL_HOUR() => 'tinyint',
72 SQL_INTERVAL_MINUTE() => 'tinyint',
73 SQL_INTERVAL_SECOND() => 'tinyint',
74 SQL_INTERVAL_YEAR_TO_MONTH() => 'tinyint',
75 SQL_INTERVAL_DAY_TO_HOUR() => 'tinyint',
76 SQL_INTERVAL_DAY_TO_MINUTE() => 'tinyint',
77 SQL_INTERVAL_DAY_TO_SECOND() => 'tinyint',
78 SQL_INTERVAL_HOUR_TO_MINUTE() => 'tinyint',
79 SQL_INTERVAL_HOUR_TO_SECOND() => 'tinyint',
80 SQL_INTERVAL_MINUTE_TO_SECOND() => 'tinyint',
81 );
82  
83 4 my %NUMERIC_TYPEMAP = (
84 SQL_INTEGER() => 1,
85 SQL_TINYINT() => 1,
86 SQL_SMALLINT() => 1,
87 SQL_BIGINT() => 1,
88 SQL_DOUBLE() => 1,
89 SQL_NUMERIC() => 1,
90 SQL_DECIMAL() => 1,
91 SQL_FLOAT() => 1,
92 SQL_REAL() => 1,
93 );
94  
95 4 our $DEFAULT_UNISIGNED;
96 4 our $DEFAULT_NOT_NULL;
97  
98 10 ▶sub produce {
Subroutine produce: called CC=4 SCAR=13.9
Pod undocumented
99 10 my $translator = shift;
100 10 my $schema = $translator->schema;
101 10 my $args = $translator->producer_args;
102  
103 10 ▶ my $typemap = $args->{typemap} || {};
Condition100%2/2
$args->{'typemap'} || {}
l!l
28
MC/DC100%1/1
$args->{'typemap'} || {}
$args->{'typemap'}
Truth table100%2/2
A
A= $args->{'typemap'}
Aresult
11
00
104 10 local %TYPEMAP = (%TYPEMAP, %$typemap);
105 10 ▶ local $DEFAULT_NOT_NULL = $args->{default_not_null} || 0;
Condition100%2/2
$args->{'default_not_null'} || 0
l!l
46
MC/DC100%1/1
$args->{'default_not_null'} || 0
$args->{'default_not_null'}
Truth table100%2/2
A
A= $args->{'default_not_null'}
Aresult
11
00
106 10 ▶ local $DEFAULT_UNISIGNED = $args->{default_unsigned} || 0;
Condition100%2/2
$args->{'default_unsigned'} || 0
l!l
46
MC/DC100%1/1
$args->{'default_unsigned'} || 0
$args->{'default_unsigned'}
Truth table100%2/2
A
A= $args->{'default_unsigned'}
Aresult
11
00
107 10 my $src = $_RENDER->($schema, $args);
108 10 $src =~ s/'(\w+)'(?=\s+=>)/$1/msg;
109 10 return $src;
110 }
111  
112 140 ▶sub _field_type {
Subroutine _field_type: called CC=8 SCAR=34.4
113 140 my $field = shift;
114  
115 140 ▶ if ($field->sql_data_type == SQL_UNKNOWN_TYPE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->sql_data_type == &SQL_UNKNOWN_TYPE()) 0 140 140
116 0 ▶ my $list = exists $field->extra->{list} && $field->extra->{list};
Condition0%0/3
exists $field->extra->{'list'} && $field->extra->{'list'}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $field->extra->{'list'} && $field->extra->{'list'}
exists $field->extra->{'list'} $field->extra->{'list'}
Truth table0%0/3
A && B
A= exists $field->extra->{'list'}B= $field->extra->{'list'}
ABresult
0X0
100
111
117 0 ▶ if ($list) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list) 0 0 0
118 0 ▶ return 'enum' if $field->data_type eq 'enum';
Branch: 1 branch
BranchTrueFalseTotal
if $field->data_type eq "enum" 0 0 0
119 0 ▶ return 'set' if $field->data_type eq 'set';
Branch: 1 branch
BranchTrueFalseTotal
if $field->data_type eq "set" 0 0 0
120 }
121 0 return $field->data_type;
122 }
123  
124 140 my $type = $TYPEMAP{$field->sql_data_type};
125 140 ▶ unless (defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 0 140 140
126 0 ▶ my $name = $_NAMEMAP{$field->sql_data_type} || "(unknown)"; # uncoverable condition left
Condition50%0/2
$_NAMEMAP{$field->sql_data_type} || '(unknown)'
l!l
00
MC/DC100%0/1
$_NAMEMAP{$field->sql_data_type} || '(unknown)'
$_NAMEMAP{$field->sql_data_type}
Truth table50%0/2
A
A= $_NAMEMAP{$field->sql_data_type}
Aresult
11
00
127 0 die "Unknown type: $name (sql_data_type: @{[ $field->sql_data_type ]})";
128 }
129  
130 140 return $type;
131 }
132  
133 70 ▶sub _is_numeric_data_type {
Subroutine _is_numeric_data_type: called CC=1 SCAR=0.0
134 70 my $field = shift;
135 70 return $NUMERIC_TYPEMAP{$field->sql_data_type};
136 }
137  
138 70 ▶sub _field_options {
Subroutine _field_options: called CC=24 SCAR=33.2
139 70 my $field = shift;
140 70 ▶ my $unsigned = exists $field->extra->{unsigned} && $field->extra->{unsigned};
Condition33%1/3
exists $field->extra->{'unsigned'} && $field->extra->{'unsigned'}
!ll&&!rl&&r
7000
MC/DC0%0/2
exists $field->extra->{'unsigned'} && $field->extra->{'unsigned'}
exists $field->extra->{'unsigned'} $field->extra->{'unsigned'}
Truth table33%1/3
A && B
A= exists $field->extra->{'unsigned'}B= $field->extra->{'unsigned'}
ABresult
0X0
100
111
141 70 ▶ my $on_update = exists $field->extra->{'on update'} && $field->extra->{'on update'};
Condition33%1/3
exists $field->extra->{'on update'} && $field->extra->{'on update'}
!ll&&!rl&&r
7000
MC/DC0%0/2
exists $field->extra->{'on update'} && $field->extra->{'on update'}
exists $field->extra->{'on update'} $field->extra->{'on update'}
Truth table33%1/3
A && B
A= exists $field->extra->{'on update'}B= $field->extra->{'on update'}
ABresult
0X0
100
111
142 70 ▶ my $list = exists $field->extra->{list} && $field->extra->{list};
Condition33%1/3
exists $field->extra->{'list'} && $field->extra->{'list'}
!ll&&!rl&&r
7000
MC/DC0%0/2
exists $field->extra->{'list'} && $field->extra->{'list'}
exists $field->extra->{'list'} $field->extra->{'list'}
Truth table33%1/3
A && B
A= exists $field->extra->{'list'}B= $field->extra->{'list'}
ABresult
0X0
100
111
143 70 my $numeric = _is_numeric_data_type($field);
144  
145 70 my $type = _field_type($field);
146 70 my $is_char = $type =~ /char$/;
147 70 my $is_decimal = $type eq 'decimal';
148  
149 70 my @options;
150 70 ▶ push @options => _list($field) if $list;
Branch: 1 branch
BranchTrueFalseTotal
if $list 0 70 70
151 70 ▶ push @options => 'signed' if $numeric && !$unsigned && $DEFAULT_UNISIGNED;
Branch: 1 branch
BranchTrueFalseTotal
if $numeric and not $unsigned and $SQL::Translator::Producer::DBIxSchemaDSL::DEFAULT_UNISIGNED 16 54 70
Condition83%5/6
$numeric and not $unsigned and $SQL::Translator::Producer::DBIxSchemaDSL::DEFAULT_UNISIGNED
!ll&&!rl&&r
302416
$numeric and not $unsigned
!ll&&!rl&&r
30040
MC/DC66%2/3
$numeric and not $unsigned and $SQL::Translator::Producer::DBIxSchemaDSL::DEFAULT_UNISIGNED
$numeric not $unsigned $SQL::Translator::Producer::DBIxSchemaDSL::DEFAULT_UNISIGNED
Truth table75%3/4
A and not(B) and C
A= $numericB= not $unsignedC= $SQL::Translator::Producer::DBIxSchemaDSL::DEFAULT_UNISIGNED
ABCresult
0XX0
10X0
1100
1111
152 70 ▶ push @options => 'unsigned' if $numeric && $unsigned && !$DEFAULT_UNISIGNED;
Branch: 1 branch
BranchTrueFalseTotal
if $numeric and $unsigned and not $SQL::Translator::Producer::DBIxSchemaDSL::DEFAULT_UNISIGNED 0 70 70
Condition50%3/6
$numeric and $unsigned and not $SQL::Translator::Producer::DBIxSchemaDSL::DEFAULT_UNISIGNED
!ll&&!rl&&r
7000
$numeric and $unsigned
!ll&&!rl&&r
30400
MC/DC0%0/3
$numeric and $unsigned and not $SQL::Translator::Producer::DBIxSchemaDSL::DEFAULT_UNISIGNED
$numeric $unsigned not $SQL::Translator::Producer::DBIxSchemaDSL::DEFAULT_UNISIGNED
Truth table50%2/4
A and B and not(C)
A= $numericB= $unsignedC= not $SQL::Translator::Producer::DBIxSchemaDSL::DEFAULT_UNISIGNED
ABCresult
0XX0
10X0
1100
1111
153 70 ▶ push @options => _size($field) if ($is_char || $is_decimal) && $field->size;
Branch: 1 branch
BranchTrueFalseTotal
if $is_char || $is_decimal and $field->size 20 50 70
Condition66%4/6
$is_char || $is_decimal and $field->size
!ll&&!rl&&r
50020
$is_char or $is_decimal
l!l&&r!l&&!r
20050
MC/DC33%1/3
$is_char || $is_decimal and $field->size
$is_char $is_decimal $field->size
Truth table40%2/5
A or B and C
A= $is_charB= $is_decimalC= $field->size
ABCresult
00X0
1X00
0100
1X11
0111
154 70 ▶ push @options => 'null' if $field->is_nullable && $DEFAULT_NOT_NULL;
Branch: 1 branch
BranchTrueFalseTotal
if $field->is_nullable and $SQL::Translator::Producer::DBIxSchemaDSL::DEFAULT_NOT_NULL 12 58 70
Condition100%3/3
$field->is_nullable and $SQL::Translator::Producer::DBIxSchemaDSL::DEFAULT_NOT_NULL
!ll&&!rl&&r
401812
MC/DC100%2/2
$field->is_nullable and $SQL::Translator::Producer::DBIxSchemaDSL::DEFAULT_NOT_NULL
$field->is_nullable $SQL::Translator::Producer::DBIxSchemaDSL::DEFAULT_NOT_NULL
Truth table100%3/3
A and B
A= $field->is_nullableB= $SQL::Translator::Producer::DBIxSchemaDSL::DEFAULT_NOT_NULL
ABresult
0X0
100
111
155 70 ▶ push @options => 'not_null' if !$field->is_nullable && !$DEFAULT_NOT_NULL;
Branch: 1 branch
BranchTrueFalseTotal
unless $field->is_nullable or $SQL::Translator::Producer::DBIxSchemaDSL::DEFAULT_NOT_NULL 24 46 70
Condition100%3/3
$field->is_nullable or $SQL::Translator::Producer::DBIxSchemaDSL::DEFAULT_NOT_NULL
l!l&&r!l&&!r
301624
MC/DC100%2/2
$field->is_nullable or $SQL::Translator::Producer::DBIxSchemaDSL::DEFAULT_NOT_NULL
$field->is_nullable $SQL::Translator::Producer::DBIxSchemaDSL::DEFAULT_NOT_NULL
Truth table100%3/3
A or B
A= $field->is_nullableB= $SQL::Translator::Producer::DBIxSchemaDSL::DEFAULT_NOT_NULL
ABresult
1X1
011
000
156 70 ▶ push @options => _default($field) if defined $field->default_value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $field->default_value 10 60 70
157 70 ▶ push @options => _on_update($field) if $on_update;
Branch: 1 branch
BranchTrueFalseTotal
if $on_update 0 70 70
158 70 ▶ push @options => 'primary_key' if _field_is_single_primary_key($field);
Branch: 1 branch
BranchTrueFalseTotal
if _field_is_single_primary_key($field) 20 50 70
159 70 ▶ push @options => 'unique' if _field_is_single_unique_key($field);
Branch: 1 branch
BranchTrueFalseTotal
if _field_is_single_unique_key($field) 10 60 70
160 70 ▶ push @options => 'auto_increment' if $field->is_auto_increment;
Branch: 1 branch
BranchTrueFalseTotal
if $field->is_auto_increment 20 50 70
161 70 push @options => _extra($field);
162  
163 70 ▶ return ', ' . join ', ', @options if @options;
Branch: 1 branch
BranchTrueFalseTotal
if @options 62 8 70
164 8 return '';
165 }
166  
167 70 ▶sub _field_is_single_primary_key {
Subroutine _field_is_single_primary_key: called CC=2 SCAR=7.0
168 70 my $field = shift;
169 70 my @primary_key = $field->table->primary_key->fields;
170 70 ▶ return $field->is_primary_key && @primary_key == 1;
Condition66%2/3
$field->is_primary_key && @primary_key == 1
!ll&&!rl&&r
50020
MC/DC50%1/2
$field->is_primary_key && @primary_key == 1
$field->is_primary_key @primary_key == 1
Truth table66%2/3
A && B
A= $field->is_primary_keyB= @primary_key == 1
ABresult
0X0
100
111
171 }
172  
173 70 ▶sub _field_is_single_unique_key {
Subroutine _field_is_single_unique_key: called CC=5 SCAR=16.2
174 70 my $field = shift;
175 70 for my $unique (grep { $_->type eq UNIQUE } $field->table->get_constraints) {
176 40 my %field = map { $_ => 1 } $unique->field_names;
177 40 ▶ return 1 if $field{$field->name} && keys %field == 1 && $unique->name eq "@{[$field->name]}_uniq";
Branch: 1 branch
BranchTrueFalseTotal
if $field{$field->name} and keys %field == 1 and $unique->name eq "@{[$field->name];}_uniq" 10 30 40
Condition66%4/6
$field{$field->name} and keys %field == 1 and $unique->name eq "@{[$field->name];}_uniq"
!ll&&!rl&&r
30010
$field{$field->name} and keys %field == 1
!ll&&!rl&&r
30010
MC/DC33%1/3
$field{$field->name} and keys %field == 1 and $unique->name eq "@{[$field->name];}_uniq"
$field{$field->name} keys %field == 1 $unique->name eq "@{[$field->name];}_uniq"
Truth table50%2/4
A and B and C
A= $field{$field->name}B= keys %field == 1C= $unique->name eq "@{[$field->name];}_uniq"
ABCresult
0XX0
10X0
1100
1111
178 }
179 60 return 0;
180 }
181  
182 0 ▶sub _list {
Subroutine _list: not called CC=1 SCAR=6.9
183 0 my $field = shift;
184 0 my $values = join ' ', @{ $field->extra->{list} };
185 0 return "[qw/$values/]";
186 }
187  
188 20 ▶sub _size {
Subroutine _size: called CC=2 SCAR=7.6
189 20 my $field = shift;
190  
191 20 my @size = $field->size;
192 20 ▶ return sprintf 'size => %d', $size[0] if @size == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @size == 1 20 0 20
193 0 return sprintf 'size => [%s]', join ', ', @size;
194 }
195  
196 10 ▶sub _default {
Subroutine _default: called CC=4 SCAR=19.7
197 10 my $field = shift;
198 0 ▶ return sprintf q{default => \q{%s}}, ${$field->default_value} if ref $field->default_value eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $field->default_value eq "SCALAR" 0 10 10
199 10 ▶ return sprintf 'default => %s', $field->default_value if looks_like_number($field->default_value);
Branch: 1 branch
BranchTrueFalseTotal
if &looks_like_number(scalar $field->default_value) 10 0 10
200 0 ▶ return 'default => \q{NULL}' if $field->default_value eq 'NULL';
Branch: 1 branch
BranchTrueFalseTotal
if $field->default_value eq "NULL" 0 0 0
201 0 return sprintf q{default => '%s'}, $field->default_value;
202 }
203  
204 0 ▶sub _on_update {
Subroutine _on_update: not called CC=2 SCAR=17.9
205 0 my $field = shift;
206 0 my $on_update = $field->extra->{'on update'};
207 0 ▶ return sprintf q{on_update => \q{%s}}, $$on_update if ref $on_update eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $on_update eq "SCALAR" 0 0 0
208 0 return sprintf q{on_update => '%s'}, $on_update;
209 }
210  
211 70 ▶sub _extra {
Subroutine _extra: called CC=2 SCAR=6.9
212 70 my $field = shift;
213 70 my %extra = $field->extra;
214 70 delete $extra{list};
215 70 delete $extra{'on update'};
216 70 delete $extra{unsigned};
217 70 ▶ return unless %extra;
Branch: 1 branch
BranchTrueFalseTotal
unless %extra 60 10 70
218  
219 10 local $Data::Dumper::Terse = 1;
220 10 local $Data::Dumper::Indent = 0;
221 10 local $Data::Dumper::Sortkeys = 1;
222 10 return sprintf q{extra => %s}, Data::Dumper::Dumper(\%extra);
223 }
224  
225 10 ▶sub _index_fields {
Subroutine _index_fields: called CC=1 SCAR=0.0
226 10 my $index = shift;
227 10 return join ' ', $index->fields;
228 }
229  
230 10 ▶sub _index_type {
Subroutine _index_type: called CC=2 SCAR=7.1
231 10 my $index = shift;
232 10 ▶ return 'unique_index' if $index->type eq 'UNIQUE';
Branch: 1 branch
BranchTrueFalseTotal
if $index->type eq "UNIQUE" 0 10 10
233 10 return 'index';
234 }
235  
236 10 ▶sub _index_options {
Subroutine _index_options: called CC=4 SCAR=14.6
237 10 my $index = shift;
238 10 ▶ return '' if not $index->type;
Branch: 1 branch
BranchTrueFalseTotal
unless $index->type 5 5 10
239 5 ▶ return '' if $index->type eq 'UNIQUE';
Branch: 1 branch
BranchTrueFalseTotal
if $index->type eq "UNIQUE" 0 5 5
240 5 ▶ return '' if $index->type eq 'NORMAL';
Branch: 1 branch
BranchTrueFalseTotal
if $index->type eq "NORMAL" 5 0 5
241 0 return sprintf q{, '%s'}, $index->type;
242 }
243  
244 80 ▶sub _filter_constraint(;$) {# no critic
Subroutine _filter_constraint: called CC=8 SCAR=21.4
245 80 ▶ $_ = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 80 80
246 80 ▶ return if $_->type eq NOT_NULL;
Branch: 1 branch
BranchTrueFalseTotal
if $_->type eq "NOT NULL" 0 80 80
247 80 ▶ return if $_->type eq PRIMARY_KEY && @{ $_->fields } == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $_->type eq "PRIMARY KEY" and @{$_->fields;} == 1 40 40 80
Condition66%2/3
$_->type eq "PRIMARY KEY" and @{$_->fields;} == 1
!ll&&!rl&&r
40040
MC/DC50%1/2
$_->type eq "PRIMARY KEY" and @{$_->fields;} == 1
$_->type eq "PRIMARY KEY" @{$_->fields;} == 1
Truth table66%2/3
A and B
A= $_->type eq "PRIMARY KEY"B= @{$_->fields;} == 1
ABresult
0X0
100
111
248 40 ▶ return if $_->type eq UNIQUE && @{ $_->fields } == 1 && $_->name eq "@{[ $_->fields->[0] ]}_uniq";
Branch: 1 branch
BranchTrueFalseTotal
if $_->type eq "UNIQUE" and @{$_->fields;} == 1 and $_->name eq "@{[$_->fields->[0]];}_uniq" 20 20 40
Condition66%4/6
$_->type eq "UNIQUE" and @{$_->fields;} == 1 and $_->name eq "@{[$_->fields->[0]];}_uniq"
!ll&&!rl&&r
20020
$_->type eq "UNIQUE" and @{$_->fields;} == 1
!ll&&!rl&&r
20020
MC/DC33%1/3
$_->type eq "UNIQUE" and @{$_->fields;} == 1 and $_->name eq "@{[$_->fields->[0]];}_uniq"
$_->type eq "UNIQUE" @{$_->fields;} == 1 $_->name eq "@{[$_->fields->[0]];}_uniq"
Truth table50%2/4
A and B and C
A= $_->type eq "UNIQUE"B= @{$_->fields;} == 1C= $_->name eq "@{[$_->fields->[0]];}_uniq"
ABCresult
0XX0
10X0
1100
1111
249 20 return 1;
250 }
251  
252 10 ▶sub _constraint_type {
Subroutine _constraint_type: called CC=4 SCAR=15.6
253 10 my $constraint = shift;
254  
255 10 ▶ return 'set_primary_key' if $constraint->type eq PRIMARY_KEY;
Branch: 1 branch
BranchTrueFalseTotal
if $constraint->type eq "PRIMARY KEY" 0 10 10
256 10 ▶ return 'add_unique_index' if $constraint->type eq UNIQUE;
Branch: 1 branch
BranchTrueFalseTotal
if $constraint->type eq "UNIQUE" 0 10 10
257 10 ▶ return _fk_type($constraint) if $constraint->type eq FOREIGN_KEY;
Branch: 1 branch
BranchTrueFalseTotal
if $constraint->type eq "FOREIGN KEY" 10 0 10
258 0 die "Unknown type: ", $constraint->type;
259 }
260  
261 20 ▶sub _fk_type {
Subroutine _fk_type: called CC=10 SCAR=34.0
262 20 my $constraint = shift;
263 20 my $table_name = $constraint->table->name;
264 20 my @fields = $constraint->field_names;
265 20 my $reference_table = $constraint->reference_table;
266 20 my @reference_fields = $constraint->reference_fields;
267  
268 20 ▶ if (@fields == 1 && $fields[0] eq 'id') {
Branch: 2 branches
BranchTrueFalseTotal
if (@fields == 1 and $fields[0] eq 'id') { } 0 20 20
elsif (@reference_fields == 1 and $reference_fields[0] eq 'id') { } 20 0 20
Condition33%1/3
@fields == 1 and $fields[0] eq "id"
!ll&&!rl&&r
0200
MC/DC0%0/2
@fields == 1 and $fields[0] eq "id"
@fields == 1 $fields[0] eq "id"
Truth table33%1/3
A and B
A= @fields == 1B= $fields[0] eq "id"
ABresult
0X0
100
111
269 0 my $expected_field = sprintf '%s_id', $table_name;
270 0 ▶ if (@reference_fields == 1 && $reference_fields[0] eq $expected_field) {
Branch: 1 branch
BranchTrueFalseTotal
if (@reference_fields == 1 and $reference_fields[0] eq $expected_field) 0 0 0
Condition0%0/3
@reference_fields == 1 and $reference_fields[0] eq $expected_field
!ll&&!rl&&r
000
MC/DC0%0/2
@reference_fields == 1 and $reference_fields[0] eq $expected_field
@reference_fields == 1 $reference_fields[0] eq $expected_field
Truth table0%0/3
A and B
A= @reference_fields == 1B= $reference_fields[0] eq $expected_field
ABresult
0X0
100
111
271 0 my $unique = $constraint->table->schema->get_table($reference_table)->get_field($expected_field)->is_unique;
272 0 ▶ return $unique ? 'has_one' : 'has_many';
Branch: 1 branch
BranchTrueFalseTotal
$unique ? : 0 0 0
273 }
274 }
275 ▶ elsif (@reference_fields == 1 && $reference_fields[0] eq 'id') {
Condition33%1/3
@reference_fields == 1 and $reference_fields[0] eq "id"
!ll&&!rl&&r
0020
MC/DC0%0/2
@reference_fields == 1 and $reference_fields[0] eq "id"
@reference_fields == 1 $reference_fields[0] eq "id"
Truth table33%1/3
A and B
A= @reference_fields == 1B= $reference_fields[0] eq "id"
ABresult
0X0
100
111
276 20 my $expected_field = sprintf '%s_id', $reference_table;
277 20 ▶ if (@fields == 1 && $fields[0] eq $expected_field) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields == 1 and $fields[0] eq $expected_field) 20 0 20
Condition33%1/3
@fields == 1 and $fields[0] eq $expected_field
!ll&&!rl&&r
0020
MC/DC0%0/2
@fields == 1 and $fields[0] eq $expected_field
@fields == 1 $fields[0] eq $expected_field
Truth table33%1/3
A and B
A= @fields == 1B= $fields[0] eq $expected_field
ABresult
0X0
100
111
278 20 return 'belongs_to';
279 }
280 }
281  
282 0 return 'fk';
283 }
284  
285 10 ▶sub _constraint_options {
Subroutine _constraint_options: called CC=4 SCAR=15.6
286 10 my $constraint = shift;
287 10 ▶ return _primary_key_options($constraint) if $constraint->type eq PRIMARY_KEY;
Branch: 1 branch
BranchTrueFalseTotal
if $constraint->type eq "PRIMARY KEY" 0 10 10
288 10 ▶ return _unique_key_options($constraint) if $constraint->type eq UNIQUE;
Branch: 1 branch
BranchTrueFalseTotal
if $constraint->type eq "UNIQUE" 0 10 10
289 10 ▶ return _fk_options($constraint) if $constraint->type eq FOREIGN_KEY;
Branch: 1 branch
BranchTrueFalseTotal
if $constraint->type eq "FOREIGN KEY" 10 0 10
290 0 die "Unknown type: ", $constraint->type;
291 }
292  
293 0 ▶sub _primary_key_options {
Subroutine _primary_key_options: not called CC=1 SCAR=6.9
294 0 my $constraint = shift;
295 0 my $fields = join ' ', $constraint->field_names;
296 0 return sprintf 'qw/%s/', $fields;
297 }
298  
299 0 ▶sub _unique_key_options {
Subroutine _unique_key_options: not called CC=1 SCAR=6.9
300 0 my $constraint = shift;
301 0 my $fields = join ' ', $constraint->field_names;
302 0 return sprintf q{'%s' => [qw/%s/]}, $constraint->name, $fields;
303 }
304  
305 10 ▶sub _fk_options {
Subroutine _fk_options: called CC=4 SCAR=21.7
306 10 my $constraint = shift;
307  
308 10 my $type = _fk_type($constraint);
309 10 ▶ if ($type eq 'fk') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "fk") 0 10 10
310 0 my @fields = $constraint->field_names;
311 0 ▶ my $fields = @fields > 1 ? sprintf '[qw/%s/]', join ' ', @fields : "'$fields[0]'";
Branch: 1 branch
BranchTrueFalseTotal
@fields > 1 ? : 0 0 0
312 0 my @reference_fields = $constraint->reference_fields;
313 0 ▶ my $reference_fields = @reference_fields > 1 ? sprintf '[qw/%s/]', join ' ', @reference_fields : "'$reference_fields[0]'";
Branch: 1 branch
BranchTrueFalseTotal
@reference_fields > 1 ? : 0 0 0
314 0 return sprintf q{%s, '%s' => %s},
315 $fields, $constraint->reference_table, $reference_fields;
316 }
317  
318 10 return sprintf q{'%s'}, $constraint->reference_table;
319 }
320  
321 4 1;
322  
323 =encoding utf-8
324  
325 =head1 NAME
326  
327 SQL::Translator::Producer::DBIxSchemaDSL - DBIX::Schema::DSL specific producer for SQL::Translator
328  
329 =head1 SYNOPSIS
330  
331 use SQL::Translator;
332 use SQL::Translator::Producer::DBIxSchemaDSL;
333  
334 my $t = SQL::Translator->new( parser => '...' );
335 $t->producer('DBIxSchemaDSL');
336 $t->translate;
337  
338 =head1 DESCRIPTION
339  
340 This module will produce text output of the schema suitable for DBIx::Schema::DSL.
341  
342 =head1 ARGUMENTS
343  
344 =over 4
345  
346 =item C<default_not_null>
347  
348 Enables C<default_not_null> in DSL.
349  
350 =item C<default_unsigned>
351  
352 Enables C<default_unsigned> in DSL.
353  
354 =item C<typemap>
355  
356 Override type mapping from DBI type to DBIx::Schema::DSL type.
357  
358 Example:
359  
360 use DBI qw/:sql_types/;
361 use SQL::Translator;
362 use SQL::Translator::Producer::DBIx::Schema::DSL;
363  
364 my $t = SQL::Translator->new( parser => '...' );
365 $t->producer('GoogleBigQuery', { typemap => { SQL_TINYINT() => 'integer' } });
366 $t->translate;
367  
368 =back
369  
370 =head1 LICENSE
371  
372 Copyright (C) karupanerura.
373  
374 This library is free software; you can redistribute it and/or modify
375 it under the same terms as Perl itself.
376  
377 =head1 AUTHOR
378  
379 karupanerura E<lt>karupa@cpan.orgE<gt>
380  
381 =cut
382  
383 __DATA__
↑ Summary