blib/lib/YATT/Lite/WebMVC0/DBSchema.pm

filter: Statementstmt 71.5% 392 / 548 Branchbran 56.0% 148 / 264 Conditioncond 48.0% 48 / 100 MC/DCmcdc 8.0% 5 / 62 Subroutinesub 73.7% 76 / 103 Podpod 1.2% 1 / 83 totaltotal 57.7% 670 / 1160 CC 190 SCAR 21.0
CC
190
Cov
64%
ensure_created_on
30.7
startup
20.0
sql_schema
20.0
CRAP 8.2
« blib/lib/YATT/Lite/PageConsts.pm ↑ Summary blib/lib/YATT/Lite/CLI/Lint.pm »
1 package YATT::Lite::WebMVC0::DBSchema; sub MY () {__PACKAGE__}
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings qw(FATAL all NONFATAL misc);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use base qw/YATT::Lite::Object
Subroutine BEGIN: called CC=1 SCAR=0.0
9 YATT::Lite::Util::CmdLine
10 3 /;
11 use YATT::Lite::MFields
12 3 ▶ (qw/_table_list _table_dict _dbtype
Subroutine BEGIN: called CC=1 SCAR=0.0
13 DBH
14 user
15 auth
16 connection_spec
17 connect_atstart
18 verbose
19 NULL
20 name
21 no_header
22 auto_create
23 coltype_map
24  
25 after_dbinit
26 group_writable
27  
28 is_clone
29 debug
30 on_destroy
31  
32 _role_dict
33 3 /);
34  
35 use YATT::Lite::Types
36 3 ▶ ([Item => fields => [qw/_not_configured
Subroutine BEGIN: called CC=1 SCAR=0.0
37 name/]
38 , [Table => fields => [qw/_pk _chk_unique
39 _chk_index _chk_check
40 _col_list _col_dict
41 _relation_list _relation_dict
42 _reference_dict
43 _initializer
44 view virtual
45 trigger_after_delete
46 /]]
47 , [Column => fields => [qw/type
48 hidden
49 unique
50 indexed
51 primary_key
52 autoincrement
53  
54 default
55 null
56  
57 usage
58 label
59 max_length
60 /]]]
61 3 );
62  
63 3 ▶use YATT::Lite::Util qw/coalesce globref ckeval terse_dump lexpand
Subroutine BEGIN: called CC=1 SCAR=0.0
64 shallow_copy
65 3 /;
66  
67 #========================================
68 11 ▶DESTROY {
Subroutine DESTROY: called CC=2 SCAR=6.9
69 11 my MY $self = shift;
70 11 ▶ if (my $sub = $self->{on_destroy}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = $self->{"on_destroy"}) 1 10 11
71 1 $sub->($self);
72 }
73 11 $self->disconnect("from DBSchema->DESTROY");
74 }
75 11 ▶sub disconnect {
Subroutine disconnect: called CC=6 SCAR=18.2
Pod undocumented
76 11 (my MY $schema, my $msg) = @_;
77 11 ▶ $msg ||= "";
Condition50%1/2
$msg ||= ""
l!l
110
MC/DC0%0/1
$msg ||= ""
$msg
Truth table50%1/2
A
A= $msg
Aresult
11
00
78 11 ▶ if (my $dbh = delete $schema->{DBH}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dbh = delete $schema->{'DBH'}) { } 5 6 11
79 # XXX: is_clone
80 5 ▶ $dbh->commit unless $dbh->{AutoCommit};
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->{"AutoCommit"} 1 4 5
81 5 $dbh->disconnect;
82 print STDERR "DEBUG: DBSchema->disconnect $msg $schema, had dbh $dbh\n"
83 5 ▶ if $schema->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{"debug"} 0 5 5
84 } else {
85 print STDERR "DEBUG: DBSchema->disconnect $msg $schema, without dbh\n"
86 6 ▶ if $schema->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{"debug"} 0 6 6
87 }
88 }
89  
90 #========================================
91  
92 14 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
93 14 my $pack = shift;
94 14 $pack->parse_import(\@_, \ my %opts);
95 14 my MY $self = $pack->SUPER::new(%opts);
96 14 $self->init_schema;
97 14 ▶ $self->add_schema(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 14 0 14
98 14 $self->verify_schema;
99 14 $self;
100 }
101  
102 1 ▶sub clone {
Subroutine clone: called CC=6 SCAR=18.2
Pod undocumented
103 1 my MY $orig = shift;
104 1 ▶ croak "Can't clone non-object: $orig" unless ref $orig;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $orig 0 1 1
105 1 my MY $new = bless {}, ref($orig);
106 1 foreach my $k (keys %$orig) {
107 6 my $v = $orig->{$k};
108 # shallow_copy with pass-thru flag.
109 6 ▶ $new->{$k} = ref $v ? shallow_copy($v, 1) : $v;
Branch: 1 branch
BranchTrueFalseTotal
ref $v ? : 4 2 6
110 }
111 1 $new->reset;
112 1 $new->{is_clone} = 1;
113 1 ▶ $new->configure(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 0 1
114 1 ▶ print STDERR "DEBUG: dbschema clone, now=$new\n" if $new->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $new->{"debug"} 0 1 1
115 1 $new;
116 }
117  
118 2 ▶sub reset {
Subroutine reset: called CC=3 SCAR=11.1
Pod undocumented
119 2 (my MY $self) = @_;
120 2 ▶ if (my $dbh = delete $self->{DBH}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dbh = delete $self->{"DBH"}) 1 1 2
121 1 ▶ $dbh->disconnect if $self->{is_clone};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"is_clone"} 1 0 1
122 }
123 }
124  
125 0 ▶sub is_known_role {
Subroutine is_known_role: not called CC=2 SCAR=17.9
Pod undocumented
126 0 (my MY $self, my $class) = @_;
127 0 ▶ $class //= caller;
Condition0%0/3
$class //= caller
l!l&&r!l&&!r
000
MC/DC0%0/2
$class //= caller
$class caller
Truth table0%0/3
A //= B
A= $classB= caller
ABresult
1X1
011
000
128 0 $self->{_role_dict}{$class}++;
129 }
130  
131 # Extension hook.
132 14 ▶sub init_schema {}
Subroutine init_schema: called CC=1 SCAR=0.0
Pod undocumented
133  
134 14 ▶sub add_schema {
Subroutine add_schema: called CC=3 SCAR=11.7
Pod undocumented
135 14 (my MY $self) = shift;
136 14 foreach my $item (@_) {
137 20 ▶ if (ref $item) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item) { } 20 0 20
138 20 $self->add_table(@$item);
139 } else {
140 0 croak "Invalid schema item: $item";
141 }
142 }
143 }
144  
145 14 ▶sub parse_import {
Subroutine parse_import: called CC=5 SCAR=16.8
Pod undocumented
146 14 my ($pack, $list, $opts) = @_;
147 # -bool_flag
148 # key => value
149 14 ▶ for (; @$list; shift @$list) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$list) 20 0 20
150 20 ▶ last if ref $list->[0];
Branch: 1 branch
BranchTrueFalseTotal
if ref $list->[0] 14 6 20
151 6 ▶ if ($list->[0] =~ /^-(\w+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list->[0] =~ /^-(\w+)/) { } 0 6 6
152 0 $opts->{$1} = 1;
153 } else {
154 6 ▶ croak "Option value is missing for $list->[0]"
Branch: 1 branch
BranchTrueFalseTotal
unless @$list >= 2 0 6 6
155 unless @$list >= 2;
156 6 $opts->{$list->[0]} = $list->[1];
157 6 shift @$list;
158 }
159 }
160 }
161  
162 #########################################
163 5 ▶sub after_connect {
Subroutine after_connect: called CC=2 SCAR=7.2
Pod undocumented
164 5 my MY $self = shift;
165 5 ▶ $self->ensure_created_on($self->{DBH}) if $self->{auto_create};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"auto_create"} 5 0 5
166 }
167  
168 5 ▶sub dbinit_sqlite {
Subroutine dbinit_sqlite: called CC=3 SCAR=12.0
Pod undocumented
169 5 (my MY $self, my $sqlite_fn) = @_;
170 5 ▶ chmod 0664, $sqlite_fn if $self->{group_writable} // 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"group_writable"} // 1 5 0 5
Condition50%1/2
$self->{"group_writable"} // 1
l!l
05
MC/DC0%0/1
$self->{"group_writable"} // 1
$self->{"group_writable"}
Truth table50%1/2
A
A= $self->{"group_writable"}
Aresult
11
00
171 }
172  
173 #========================================
174  
175 0 ▶sub startup {
Subroutine startup: not called CC=4 SCAR=30.0
Pod undocumented
176 0 (my MY $schema, my (@apps)) = @_;
177 0 foreach my $app (@apps) {
178 # XXX: logging?
179 0 ▶ my $sub = $app->can("backend_startup")
Branch: 1 branch
BranchTrueFalseTotal
my $sub = $app->can("backend_startup") or next 0 0 0
180 or next;
181 0 $sub->($app, $schema);
182 }
183  
184 0 ▶ if ($schema->{connect_atstart}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"connect_atstart"}) 0 0 0
185 0 $schema->make_connection;
186 }
187 }
188  
189 #========================================
190  
191 0 ▶sub has_connection { my MY $schema = shift; $schema->{DBH} }
Subroutine has_connection: not called CC=1 SCAR=6.9
Pod undocumented
192  
193 21 ▶sub dbh {
Subroutine dbh: called CC=2 SCAR=7.1
Pod undocumented
194 21 (my MY $schema) = @_;
195 21 ▶ $schema->{DBH} // $schema->make_connection;
Condition66%2/3
$schema->{"DBH"} // $schema->make_connection
l!l&&r!l&&!r
2010
MC/DC0%0/2
$schema->{"DBH"} // $schema->make_connection
$schema->{"DBH"} $schema->make_connection
Truth table66%2/3
A // B
A= $schema->{"DBH"}B= $schema->make_connection
ABresult
1X1
011
000
196 }
197  
198 #
199 # Quasi-option to configure $when and @spec at once.
200 #
201 0 ▶sub configure_connect {
Subroutine configure_connect: not called CC=1 SCAR=6.9
Pod undocumented
202 0 (my MY $schema, my $config) = @_;
203 0 my ($when, @spec) = @$config;
204 0 $schema->{connection_spec} = \@spec;
205 0 $schema->{connect_atstart} = $schema->parse_connect_when($when);
206 }
207  
208 0 ▶sub parse_connect_when {
Subroutine parse_connect_when: not called CC=3 SCAR=24.8
Pod undocumented
209 0 (my MY $schema, my $when) = @_;
210 0 ▶ if ($when =~ /^at_?start$/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($when =~ /^at_?start$/i) { } 0 0 0
elsif ($when =~ /^on_?demand$/i) { } 0 0 0
211 0 1;
212 } elsif ($when =~ /^on_?demand$/i) {
213 0 0;
214 } else {
215 0 croak "Unknown connection timing: '$when'";
216 }
217 }
218  
219 0 ▶sub configure_dbtype {
Subroutine configure_dbtype: not called CC=1 SCAR=6.9
Pod undocumented
220 0 (my MY $schema, my $value) = @_;
221 0 $schema->{_dbtype} = $value;
222 }
223  
224 #
225 # This must fill DBH.
226 #
227 5 ▶sub make_connection {
Subroutine make_connection: called CC=8 SCAR=27.1
Pod undocumented
228 5 (my MY $schema) = shift;
229 5 ▶ my ($spec) = @_ ? @_ : $schema->{connection_spec};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 3 2 5
230 5 ▶ unless (defined $spec) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $spec) 0 5 5
231 0 croak "connection_spec is empty";
232 }
233 5 ▶ if (ref $spec eq 'ARRAY' or not ref $spec) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $spec eq 'ARRAY' or not ref $spec) { } 5 0 5
elsif (ref $spec eq 'CODE') { } 0 0 0
Condition33%1/3
ref $spec eq "ARRAY" or not ref $spec
l!l&&r!l&&!r
500
MC/DC0%0/2
ref $spec eq "ARRAY" or not ref $spec
ref $spec eq "ARRAY" not ref $spec
Truth table33%1/3
A or not(B)
A= ref $spec eq "ARRAY"B= not ref $spec
ABresult
1X1
011
000
234 5 $schema->connect_to(lexpand($spec));
235 } elsif (ref $spec eq 'CODE') {
236 0 $spec->($schema);
237 } else {
238 0 croak "Unknown connection spec obj: $spec";
239 }
240 print STDERR "DEBUG: dbh for $schema=$schema->{DBH}"
241 , ($schema->{debug} >= 2 ? Carp::longmess() : ()), "\n\n"
242 5 ▶ if $schema->{debug};
Branch: 2 branches
BranchTrueFalseTotal
if $schema->{"debug"} 0 5 5
$schema->{'debug'} >= 2 ? : 0 0 0
243 5 $schema->{DBH};
244 }
245  
246 #----------------------------------------
247 5 ▶sub connect_to {
Subroutine connect_to: called CC=3 SCAR=12.7
Pod undocumented
248 5 (my MY $schema, my ($dbtype, @args)) = @_;
249 5 ▶ if ($dbtype =~ /^dbi:/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dbtype =~ /^dbi:/i) { } 0 5 5
elsif (my $sub = $schema->can("connect_to_\L$dbtype\E")) { } 5 0 5
250 0 $schema->connect_to_dbi($dbtype, @args);
251 } elsif (my $sub = $schema->can("connect_to_\L$dbtype")) {
252 5 $schema->{_dbtype} = lc($dbtype);
253 5 $sub->($schema, @args);
254 } else {
255 0 croak sprintf("%s: Unknown dbtype: %s", MY, $dbtype);
256 }
257 }
258  
259 0 ▶sub dbtype_of_dbi_dsn {
Subroutine dbtype_of_dbi_dsn: not called CC=2 SCAR=17.9
Pod undocumented
260 0 (my MY $schema, my $dbi) = @_;
261 0 ▶ my ($driver) = $dbi =~ m{^dbi:([^:]+):}i
Branch: 1 branch
BranchTrueFalseTotal
my($driver) = $dbi =~ /^dbi:([^:]+):/i or croak("Unknown driver spec in DBI DSN! $dbi") 0 0 0
262 or croak "Unknown driver spec in DBI DSN! $dbi";
263 0 $driver;
264 }
265  
266 0 ▶sub connect_to_dbi {
Subroutine connect_to_dbi: not called CC=2 SCAR=17.9
Pod undocumented
267 0 (my MY $schema, my ($dbi, @args)) = @_;
268 0 my $driver = $schema->dbtype_of_dbi_dsn($dbi);
269 0 $schema->{_dbtype} = lc($driver);
270 0 ▶ if (my $sub = $schema->can("connect_to_\L$driver")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = $schema->can("connect_to_\L$driver\E")) { } 0 0 0
271 0 $sub->($schema, $dbi, @args);
272 } else {
273 0 $schema->dbi_connect($dbi, @args);
274 }
275 }
276  
277 #----------------------------------------
278  
279 5 ▶sub connect_to_sqlite {
Subroutine connect_to_sqlite: called CC=7 SCAR=20.6
Pod undocumented
280 5 (my MY $schema, my ($dsn_or_sqlite_fn, %opts)) = @_;
281 5 require DBD::SQLite; my $minver = version->parse("1.30_02");
282  
283 5 my ($sqlite_fn, $dbi_dsn) = do {
284 5 ▶ if ($dsn_or_sqlite_fn =~ /^dbi:SQLite:(?:dbname=)?(.*)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsn_or_sqlite_fn =~ /^dbi:SQLite:(?:dbname=)?(.*)$/i) { } 0 5 5
285 0 ($1, $dsn_or_sqlite_fn);
286 } else {
287 5 ($dsn_or_sqlite_fn, "dbi:SQLite:dbname=$dsn_or_sqlite_fn");
288 }
289 };
290 5 ▶ unless (delete $opts{RO}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (delete $opts{"RO"}) 5 0 5
291 5 ▶ $opts{sqlite_use_immediate_transaction} = 1
Branch: 1 branch
BranchTrueFalseTotal
if "version"->parse($DBD::SQLite::VERSION) >= $minver 5 0 5
292 if version->parse($DBD::SQLite::VERSION) >= $minver;
293 }
294 5 ▶ $schema->{_dbtype} //= 'sqlite';
Condition50%1/2
$schema->{"_dbtype"} //= "sqlite"
l!l
50
MC/DC0%0/1
$schema->{"_dbtype"} //= "sqlite"
$schema->{"_dbtype"}
Truth table50%1/2
A
A= $schema->{"_dbtype"}
Aresult
11
00
295 5 my $first_time = not -e $sqlite_fn;
296 5 ▶ $schema->{auto_create} //= 1;
Condition50%1/2
$schema->{"auto_create"} //= 1
l!l
05
MC/DC0%0/1
$schema->{"auto_create"} //= 1
$schema->{"auto_create"}
Truth table50%1/2
A
A= $schema->{"auto_create"}
Aresult
11
00
297 5 $schema->dbi_connect($dbi_dsn, undef, undef, %opts);
298 5 ▶ $schema->dbinit_sqlite($sqlite_fn) if $first_time;
Branch: 1 branch
BranchTrueFalseTotal
if $first_time 5 0 5
299 5 $schema;
300 }
301  
302 5 ▶sub dbi_connect {
Subroutine dbi_connect: called CC=3 SCAR=11.2
Pod undocumented
303 5 (my MY $schema, my ($dbi_dsn, $user, $auth, %attr)) = @_;
304 5 my %default = $schema->default_dbi_attr;
305 5 ▶ $attr{$_} //= $default{$_} for keys %default;
Condition33%1/3
$attr{$_} //= $default{$_}
l!l&&r!l&&!r
0150
MC/DC0%0/2
$attr{$_} //= $default{$_}
$attr{$_} $default{$_}
Truth table33%1/3
A //= B
A= $attr{$_}B= $default{$_}
ABresult
1X1
011
000
306 5 require DBI;
307 5 my $dbh = $schema->{DBH} = DBI->connect($dbi_dsn, $user, $auth, \%attr);
308 5 $schema->after_connect;
309 5 $schema;
310 }
311  
312 5 ▶sub default_dbi_attr {
Subroutine default_dbi_attr: called CC=1 SCAR=0.0
Pod undocumented
313 5 (RaiseError => 1, PrintError => 0, AutoCommit => 1);
314 }
315  
316 #----------------------------------------
317  
318 #
319 # ./lib/MyModel.pm create sqlite data/myapp.db3
320 #
321 4 ▶sub create {
Subroutine create: called CC=3 SCAR=11.0
Pod undocumented
322 4 (my MY $schema, my @spec) = @_;
323 # $schema->dbh() will call ensure_created_on when auto_create is on.
324 4 ▶ my $dbh = $schema->{DBH} || $schema->make_connection(\@spec);
Condition66%2/3
$schema->{'DBH'} || $schema->make_connection(\@spec)
l!l&&r!l&&!r
130
MC/DC0%0/2
$schema->{'DBH'} || $schema->make_connection(\@spec)
$schema->{'DBH'} $schema->make_connection(\@spec)
Truth table66%2/3
A || B
A= $schema->{'DBH'}B= $schema->make_connection(\@spec)
ABresult
1X1
011
000
325 #
326 4 ▶ $schema->ensure_created_on($dbh) unless $schema->{auto_create};
Branch: 1 branch
BranchTrueFalseTotal
unless $schema->{"auto_create"} 1 3 4
327 4 $schema;
328 }
329  
330 0 ▶sub sql_schema {
Subroutine sql_schema: not called CC=4 SCAR=30.0
Pod undocumented
331 0 (my MY $schema) = @_;
332 0 my @sql;
333 0 foreach my Table $table ($schema->list_tables(raw => 1)) {
334 0 foreach my $create ($schema->sql_create_table($table)) {
335 0 push @sql, $create;
336 }
337 }
338 0 foreach my Table $view ($schema->list_views(raw => 1)) {
339 0 push @sql, "CREATE VIEW $view->{name}\nAS $view->{view}";
340 }
341 0 @sql;
342 }
343  
344 10 ▶sub ensure_created_on {
Subroutine ensure_created_on: called CC=17 SCAR=34.2
Pod undocumented
345 10 (my MY $schema, my $dbh) = @_;
346 # Carp::cluck("ensure_created is called");
347  
348 10 $schema->dbtype_try_invoke('begin_create');
349  
350 10 my (@sql, @created);
351 10 foreach my Table $table ($schema->list_tables(raw => 1)) {
352 21 ▶ next if $schema->has_table($table->{name}, $dbh);
Branch: 1 branch
BranchTrueFalseTotal
if $schema->has_table($table->{"name"}, $dbh) 0 21 21
353 21 push @created, $table;
354 21 foreach my $create ($schema->sql_create_table($table)) {
355 29 ▶ unless ($schema->{verbose}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($schema->{'verbose'}) { } 0 29 29
if (do { $schema->{'verbose'} >= 2 }) { } 0 0 0
elsif ($schema->{'verbose'} and $create =~ /^create table /i) { } 0 0 0
356 } elsif ($schema->{verbose} >= 2) {
357 0 print STDERR "-- $table->{name} --\n$create\n\n"
358 ▶ } elsif ($schema->{verbose} and $create =~ /^create table /i) {
Condition0%0/3
$schema->{"verbose"} and $create =~ /^create table /i
!ll&&!rl&&r
000
MC/DC0%0/2
$schema->{"verbose"} and $create =~ /^create table /i
$schema->{"verbose"} $create =~ /^create table /i
Truth table0%0/3
A and B
A= $schema->{"verbose"}B= $create =~ /^create table /i
ABresult
0X0
100
111
359 0 print STDERR "CREATE TABLE $table->{name}\n";
360 }
361 29 push @sql, $create;
362 }
363 }
364 10 foreach my Table $view ($schema->list_views(raw => 1)) {
365 2 ▶ next if $schema->has_view($view->{name}, $dbh);
Branch: 1 branch
BranchTrueFalseTotal
if $schema->has_view($view->{"name"}, $dbh) 0 2 2
366 2 ▶ next if $view->{virtual};
Branch: 1 branch
BranchTrueFalseTotal
if $view->{"virtual"} 0 2 2
367 2 ▶ if ($schema->{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"verbose"}) 0 2 2
368 0 print STDERR "CREATE VIEW $view->{name}\n";
369 }
370 2 push @sql, "CREATE VIEW $view->{name}\nAS $view->{view}";
371 }
372 10 $dbh->do($_) for @sql;
373 10 ▶ if (@created) {
Branch: 1 branch
BranchTrueFalseTotal
if (@created) 10 0 10
374 10 foreach my Table $tab (@created) {
375 21 $schema->ensure_table_populated($dbh, $tab);
376 }
377 }
378 10 ▶ if (@sql) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sql) 10 0 10
379 10 ▶ $dbh->commit unless $dbh->{AutoCommit};
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->{"AutoCommit"} 1 9 10
380 }
381 10 @created;
382 }
383  
384 21 ▶sub ensure_table_populated {
Subroutine ensure_table_populated: called CC=6 SCAR=20.8
Pod undocumented
385 21 (my MY $schema, my $dbh, my Table $tab) = @_;
386 21 foreach my $init (lexpand($tab->{_initializer})) {
387 4 my ($colSpec, @values) = @$init;
388 4 my $sql = $schema->sql_to_insert($tab->{name}, @$colSpec);
389 4 my $ins = $dbh->prepare($sql);
390 4 foreach my $record (@values) {
391 8 ▶ if (grep {ref $_ eq 'SCALAR'} @$record) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {ref $_ eq 'SCALAR';} @$record) { } 0 8 8
392 my ($sql, $values) = $schema->sql_and_values_to_insert_expr
393 0 ($tab->{name}, $colSpec, $record);
394 0 my @vals = $schema->expand_codevalue($tab, $values);
395 print STDERR $sql, "\n -- (", join(",", @vals), ")\n"
396 0 ▶ if $schema->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{"verbose"} 0 0 0
397 0 $dbh->do($sql, undef, @vals);
398 } else {
399 8 my @vals = $schema->expand_codevalue($tab, $record);
400 print STDERR $sql, "\n -- (", join(",", @vals), ")\n"
401 8 ▶ if $schema->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{"verbose"} 0 8 8
402 8 $ins->execute(@vals);
403 }
404 }
405 }
406 }
407  
408 5 ▶sub sqlite_begin_create {
Subroutine sqlite_begin_create: called CC=2 SCAR=7.0
Pod undocumented
409 5 (my MY $schema) = @_;
410 # To speedup create statements.
411 5 my $dbh = $schema->dbh;
412 5 ▶ if ($dbh->{AutoCommit}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh->{"AutoCommit"}) 5 0 5
413 5 $dbh->do("PRAGMA synchronous = OFF");
414 }
415 }
416  
417 8 ▶sub expand_codevalue {
Subroutine expand_codevalue: called CC=2 SCAR=7.1
Pod undocumented
418 8 (my MY $schema, my $tab, my $record) = @_;
419 8 ▶ map {ref $_ ? $_->($schema, $tab) : $_} @$record;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 16 16
420 }
421  
422 21 ▶sub has_table { shift->has_type(table => @_); }
Subroutine has_table: called CC=1 SCAR=0.0
Pod undocumented
423 2 ▶sub has_view { shift->has_type(view => @_); }
Subroutine has_view: called CC=1 SCAR=0.0
Pod undocumented
424  
425 23 ▶sub has_type {
Subroutine has_type: called CC=4 SCAR=14.3
Pod undocumented
426 23 (my MY $schema, my ($type, $table, $dbh)) = @_;
427 23 ▶ if ($$schema{_dbtype}
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{'_dbtype'} and my $sub = $schema->can("$schema->{'_dbtype'}_has_type")) { } 10 13 23
Condition66%2/3
$schema->{"_dbtype"} and my $sub = $schema->can(qq[$schema->{"_dbtype"}_has_type])
!ll&&!rl&&r
13010
MC/DC50%1/2
$schema->{"_dbtype"} and my $sub = $schema->can(qq[$schema->{"_dbtype"}_has_type])
$schema->{"_dbtype"} my $sub = $schema->can(qq[$schema->{"_dbtype"}_has_type])
Truth table66%2/3
A and B
A= $schema->{"_dbtype"}B= my $sub = $schema->can(qq[$schema->{"_dbtype"}_has_type])
ABresult
0X0
100
111
428 and my $sub = $schema->can("$$schema{_dbtype}_has_type")) {
429 10 $sub->($schema, $type, $table, $dbh);
430 } else {
431 13 ▶ $dbh ||= $schema->dbh;
Condition33%1/3
$dbh ||= $schema->dbh
l!l&&r!l&&!r
1300
MC/DC0%0/2
$dbh ||= $schema->dbh
$dbh $schema->dbh
Truth table33%1/3
A ||= B
A= $dbhB= $schema->dbh
ABresult
1X1
011
000
432 13 $dbh->tables("", "", $table, uc($type));
433 }
434 }
435  
436 10 ▶sub dbtype_try_invoke {
Subroutine dbtype_try_invoke: called CC=3 SCAR=11.0
Pod undocumented
437 10 (my MY $schema, my ($method, @args)) = @_;
438 10 ▶ return unless $schema->{_dbtype};
Branch: 1 branch
BranchTrueFalseTotal
unless $schema->{"_dbtype"} 5 5 10
439 5 ▶ my $sub = $schema->can("$schema->{_dbtype}_$method")
Branch: 1 branch
BranchTrueFalseTotal
my $sub = $schema->can(qq[$schema->{"_dbtype"}_$method]) or return 5 0 5
440 or return;
441 5 $sub->($schema, @args);
442 }
443  
444 10 ▶sub sqlite_has_type {
Subroutine sqlite_has_type: called CC=2 SCAR=8.1
Pod undocumented
445 10 (my MY $schema, my ($type, $name, $dbh)) = @_;
446 10 ▶ my ($found) = $dbh->selectrow_array(<<'END', undef, $type, $name)
Branch: 1 branch
BranchTrueFalseTotal
my($found) = $dbh->selectrow_array("select name from sqlite_master where type = ? and name = ?\n", undef, $type, $name) or return undef 0 10 10
447 select name from sqlite_master where type = ? and name = ?
448 END
449  
450 or return undef;
451 0 $found;
452 }
453  
454 0 ▶sub tables {
Subroutine tables: not called CC=1 SCAR=6.9
Pod undocumented
455 0 my MY $schema = shift;
456 0 keys %{$schema->{_table_dict}};
457 }
458  
459 0 ▶sub has_column {
Subroutine has_column: not called CC=2 SCAR=17.9
Pod undocumented
460 0 (my MY $schema, my ($table, $column, $dbh)) = @_;
461 0 ▶ my $hash = $schema->columns_hash($table, $dbh || $schema->dbh);
Condition0%0/3
$dbh || $schema->dbh
l!l&&r!l&&!r
000
MC/DC0%0/2
$dbh || $schema->dbh
$dbh $schema->dbh
Truth table0%0/3
A || B
A= $dbhB= $schema->dbh
ABresult
1X1
011
000
462 0 exists $hash->{$column};
463 }
464  
465 0 ▶sub columns_hash {
Subroutine columns_hash: not called CC=2 SCAR=17.9
Pod undocumented
466 0 (my MY $schema, my ($table, $dbh)) = @_;
467 0 ▶ $dbh ||= $schema->dbh;
Condition0%0/3
$dbh ||= $schema->dbh
l!l&&r!l&&!r
000
MC/DC0%0/2
$dbh ||= $schema->dbh
$dbh $schema->dbh
Truth table0%0/3
A ||= B
A= $dbhB= $schema->dbh
ABresult
1X1
011
000
468 0 my $sth = $dbh->prepare("select * from $table limit 0");
469 0 $sth->execute;
470 0 my %hash = %{$sth->{NAME_hash}};
471 0 \%hash;
472 }
473  
474 0 ▶sub drop {
Subroutine drop: not called CC=2 SCAR=17.9
Pod undocumented
475 0 (my MY $schema) = @_;
476 0 foreach my $sql ($schema->sql_drop) {
477 0 $schema->dbh->do($sql);
478 }
479 }
480  
481 #========================================
482  
483 48 ▶sub _list_items {
Subroutine _list_items: called CC=2 SCAR=6.9
484 48 (my MY $self, my $opts) = splice @_, 0, 2;
485 $opts->{raw} ? @_ : map {
486 48 ▶ my Item $item = $_;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'raw'} ? : 23 25 48
487 $item->{name}
488 46 } @_;
489 }
490  
491 20 ▶sub list_tables {
Subroutine list_tables: called CC=1 SCAR=0.0
Pod undocumented
492 20 (my MY $self, my %opts) = @_;
493 $self->_list_items(\%opts, grep {
494 44 my Table $tab = $_;
495 not $tab->{view}
496 20 } @{$self->{_table_list}});
497 }
498  
499 11 ▶sub list_views {
Subroutine list_views: called CC=1 SCAR=0.0
Pod undocumented
500 11 (my MY $self, my %opts) = @_;
501 $self->_list_items(\%opts, grep {
502 26 my Table $tab = $_;
503 $tab->{view}
504 11 } @{$self->{_table_list}});
505 }
506  
507 17 ▶sub list_relations {
Subroutine list_relations: called CC=6 SCAR=19.9
Pod undocumented
508 17 (my MY $self, my ($tabName, %opts)) = @_;
509 17 ▶ my Table $tab = $self->{_table_dict}{$tabName}
Branch: 1 branch
BranchTrueFalseTotal
my YATT::Lite::WebMVC0::DBSchema::Table $tab = $self->{"_table_dict"}{$tabName} or return 17 0 17
510 or return;
511 17 ▶ if ($opts{raw}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'raw'}) { } 0 17 17
512 0 @{$tab->{_relation_list}}
513 } else {
514 map {
515 18 (my ($relType, $relName, $fkName), my Table $subTab) = @$_;
516 18 ▶ $fkName //= do {
Condition66%2/3
$fkName //= do { if (my YATT::Lite::WebMVC0::DBSchema::Column $pk = $self->get_table_pk($subTab) || $self->get_table_pk($tab)) { $pk->{"name"}; } }
l!l&&r!l&&!r
1530
MC/DC0%0/2
$fkName //= do { if (my YATT::Lite::WebMVC0::DBSchema::Column $pk = $self->get_table_pk($subTab) || $self->get_table_pk($tab)) { $pk->{"name"}; } }
$fkName do { if (my YATT::Lite::WebMVC0::DBSchema::Column $pk = $self->get_table_pk($subTab) || $self->get_table_pk($tab)) { $pk->{"name"}; } }
Truth table66%2/3
A //= B
A= $fkNameB= do { if (my YATT::Lite::WebMVC0::DBSchema::Column $pk = $self->get_table_pk($subTab) || $self->get_table_pk($tab)) { $pk->{"name"}; } }
ABresult
1X1
011
000
517 3 ▶ if (my Column $pk = $self->get_table_pk($subTab)
Branch: 1 branch
BranchTrueFalseTotal
if (my YATT::Lite::WebMVC0::DBSchema::Column $pk = $self->get_table_pk($subTab) || $self->get_table_pk($tab)) 3 0 3
Condition33%1/3
$self->get_table_pk($subTab) || $self->get_table_pk($tab)
l!l&&r!l&&!r
300
MC/DC0%0/2
$self->get_table_pk($subTab) || $self->get_table_pk($tab)
$self->get_table_pk($subTab) $self->get_table_pk($tab)
Truth table33%1/3
A || B
A= $self->get_table_pk($subTab)B= $self->get_table_pk($tab)
ABresult
1X1
011
000
518 || $self->get_table_pk($tab)) {
519 3 $pk->{name};
520 }
521 };
522 18 [$relType, $relName, $fkName, $subTab->{name}];
523 17 } @{$tab->{_relation_list}};
524 }
525 }
526  
527 7 ▶sub list_table_columns {
Subroutine list_table_columns: called CC=2 SCAR=7.0
Pod undocumented
528 7 (my MY $self, my ($tabName, %opts)) = @_;
529 7 ▶ my Table $tab = $self->{_table_dict}{$tabName}
Branch: 1 branch
BranchTrueFalseTotal
my YATT::Lite::WebMVC0::DBSchema::Table $tab = $self->{"_table_dict"}{$tabName} or return 7 0 7
530 or return;
531 7 $self->_list_items(\%opts, @{$tab->{_col_list}});
532 }
533  
534 91 ▶sub get_table {
Subroutine get_table: called CC=2 SCAR=7.0
Pod undocumented
535 91 (my MY $self, my $name) = @_;
536 91 ▶ $self->{_table_dict}{$name} //= do {
Condition66%2/3
$self->{"_table_dict"}{$name} //= do { push @{$$self{"_table_list"};}, my YATT::Lite::WebMVC0::DBSchema::Table $tab = $self->Table->new("name", $name); $tab->{"_not_configured"} = 1; $tab }
l!l&&r!l&&!r
58330
MC/DC0%0/2
$self->{"_table_dict"}{$name} //= do { push @{$$self{"_table_list"};}, my YATT::Lite::WebMVC0::DBSchema::Table $tab = $self->Table->new("name", $name); $tab->{"_not_configured"} = 1; $tab }
$self->{"_table_dict"}{$name} do { push @{$$self{"_table_list"};}, my YATT::Lite::WebMVC0::DBSchema::Table $tab = $self->Table->new("name", $name); $tab->{"_not_configured"} = 1; $tab }
Truth table66%2/3
A //= B
A= $self->{"_table_dict"}{$name}B= do { push @{$$self{"_table_list"};}, my YATT::Lite::WebMVC0::DBSchema::Table $tab = $self->Table->new("name", $name); $tab->{"_not_configured"} = 1; $tab }
ABresult
1X1
011
000
537 33 push @{$self->{_table_list}}
538 , my Table $tab = $self->Table->new(name => $name);
539 33 $tab->{_not_configured} = 1;
540 33 $tab;
541 };
542 }
543  
544 23 ▶sub get_table_pk {
Subroutine get_table_pk: called CC=6 SCAR=18.0
Pod undocumented
545 23 (my MY $self, my ($tabName, %opts)) = @_;
546 23 ▶ my Table $tab = ref $tabName ? $tabName : $self->{_table_dict}{$tabName};
Branch: 1 branch
BranchTrueFalseTotal
ref $tabName ? : 23 0 23
547 23 my $pkinfo = $tab->{_pk};
548 23 ▶ return unless $pkinfo;
Branch: 1 branch
BranchTrueFalseTotal
unless $pkinfo 0 23 23
549 23 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 10 13 23
550 10 ▶ $self->_list_items(\%opts, ref $pkinfo eq 'ARRAY' ? @$pkinfo : $pkinfo);
Branch: 1 branch
BranchTrueFalseTotal
ref $pkinfo eq 'ARRAY' ? : 1 9 10
551 } else {
552 13 ▶ ref $pkinfo eq 'ARRAY' ? $pkinfo->[0] : $pkinfo
Branch: 1 branch
BranchTrueFalseTotal
ref $pkinfo eq 'ARRAY' ? : 2 11 13
553 }
554 }
555  
556 33 ▶sub add_table {
Subroutine add_table: called CC=4 SCAR=15.2
Pod undocumented
557 33 my MY $self = shift;
558 33 my ($name, $opts, @colpairs) = @_;
559 33 my Table $tab = $self->get_table($name);
560 33 ▶ return $tab if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 33 33
561 33 ▶ if ($tab and not $tab->{_not_configured}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tab and not $tab->{"_not_configured"}) 0 33 33
Condition33%1/3
$tab and not $tab->{"_not_configured"}
!ll&&!rl&&r
0330
MC/DC0%0/2
$tab and not $tab->{"_not_configured"}
$tab not $tab->{"_not_configured"}
Truth table33%1/3
A and not(B)
A= $tabB= not $tab->{"_not_configured"}
ABresult
0X0
100
111
562 0 croak "Duplicate definition of table $name";
563 }
564 33 delete $tab->{_not_configured};
565 33 $self->extend_table(@_);
566 }
567  
568 35 ▶sub extend_table {
Subroutine extend_table: called CC=7 SCAR=19.5
Pod undocumented
569 35 my MY $self = shift;
570 35 my ($name, $opts, @colpairs) = @_;
571 35 my Table $tab = $self->get_table($name);
572 35 ▶ $tab->configure(lhexpand($opts)) if $opts;
Branch: 1 branch
BranchTrueFalseTotal
if $opts 3 32 35
573 35 ▶ while (@colpairs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@colpairs) 134 35 169
574 # colName => [colSpec]
575 # [check => args]
576 134 ▶ unless (ref $colpairs[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $colpairs[0]) { } 20 114 134
577 114 my ($col, $desc) = splice @colpairs, 0, 2;
578 114 ▶ $self->add_table_column($tab, $col, ref $desc ? @$desc : $desc);
Branch: 1 branch
BranchTrueFalseTotal
ref $desc ? : 54 60 114
579 } else {
580 20 my ($method, @args) = @{shift @colpairs};
581 20 $method =~ s/^-//;
582 # XXX: [has_many => @tables]
583 20 ▶ if (my ($relType, @relSpec) = $self->known_rels($method, undef, @args)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $self->known_rels($method, undef, @args)) { } 13 7 20
584 13 $self->add_table_relation($tab, undef, $relType => \@relSpec, @args);
585 } else {
586 7 ▶ my $sub = $self->can("add_table_\L$method")
Branch: 1 branch
BranchTrueFalseTotal
my $sub = $self->can("add_table_\L$method\E") or croak("Unknown table option '${method}' for table $name") 7 0 7
587 or croak "Unknown table option '$method' for table $name";
588 7 $sub->($self, $tab, @args);
589 }
590 }
591 }
592  
593 35 $tab;
594 }
595  
596 3 ▶sub add_table_primary_key {
Subroutine add_table_primary_key: called CC=3 SCAR=12.7
Pod undocumented
597 3 (my MY $self, my Table $tab, my @args) = @_;
598 3 ▶ if ($tab->{_pk} and @args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tab->{"_pk"} and @args) 0 3 3
Condition33%1/3
$tab->{"_pk"} and @args
!ll&&!rl&&r
300
MC/DC0%0/2
$tab->{"_pk"} and @args
$tab->{"_pk"} @args
Truth table33%1/3
A and B
A= $tab->{"_pk"}B= @args
ABresult
0X0
100
111
599 0 croak "Duplicate PK definition. old $tab->{_pk}";
600 }
601 3 $tab->{_pk} = [map {$tab->{_col_dict}{$_}} @args];
602 }
603  
604 0 ▶sub add_table_unique {
Subroutine add_table_unique: not called CC=1 SCAR=6.9
Pod undocumented
605 0 (my MY $self, my Table $tab, my @cols) = @_;
606 # XXX: 重複検査, 有無検査
607 0 push @{$tab->{_chk_unique}}, [@cols];
608 }
609  
610 0 ▶sub add_table_index {
Subroutine add_table_index: not called CC=1 SCAR=6.9
Pod undocumented
611 0 (my MY $self, my Table $tab, my @cols) = @_;
612 # XXX: 重複検査, 有無検査
613 0 push @{$tab->{_chk_index}}, [@cols];
614 }
615  
616 # -opt は引数無フラグ、又は [-opt, ...] として可変長オプションに使う
617 33 ▶sub add_table_relation {
Subroutine add_table_relation: called CC=10 SCAR=24.4
Pod undocumented
618 33 (my MY $self, my Table $tab, my Column $fkCol
619 , my ($relType, $relSpec, $item, $fkName, $atts)) = @_;
620 33 ▶ unless (defined $item) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $item) 0 33 33
621 0 croak "Undefined relation spec for table $tab->{name}";
622 }
623  
624 #
625 # [-has_many => 'table.key']
626 #
627 33 ▶ $fkName = $1 if not ref $item and $item =~ s/\.(\w+)$//;
Branch: 1 branch
BranchTrueFalseTotal
if not ref $item and $item =~ s/\.(\w+)$// 0 33 33
Condition66%2/3
not ref $item and $item =~ s/\.(\w+)$//
!ll&&!rl&&r
13200
MC/DC0%0/2
not ref $item and $item =~ s/\.(\w+)$//
not ref $item $item =~ s/\.(\w+)$//
Truth table66%2/3
not(A) and B
A= not ref $itemB= $item =~ s/\.(\w+)$//
ABresult
0X0
100
111
628  
629 33 ▶ my Table $subTab = ref $item ? $self->add_table(@$item)
Branch: 1 branch
BranchTrueFalseTotal
ref $item ? : 13 20 33
630 : $self->get_table($item);
631 33 ▶ my $relName = $relSpec->[0] // lc($subTab->{name});
Condition100%2/2
$relSpec->[0] // lc $subTab->{"name"}
l!l
1419
MC/DC100%1/1
$relSpec->[0] // lc $subTab->{"name"}
$relSpec->[0]
Truth table100%2/2
A
A= $relSpec->[0]
Aresult
11
00
632 $fkName //= $relSpec->[1] // $fkCol->{name}
633 33 ▶ // $subTab->{_reference_dict}{$tab->{name}};
Condition77%7/9
$fkName //= ($relSpec->[1] // $fkCol->{"name"}) // $subTab->{"_reference_dict"}{$tab->{"name"}}
l!l&&r!l&&!r
7206
($relSpec->[1] // $fkCol->{"name"}) // $subTab->{"_reference_dict"}{$tab->{"name"}}
l!l&&r!l&&!r
2006
$relSpec->[1] // $fkCol->{"name"}
l!l&&r!l&&!r
2006
MC/DC0%0/4
$fkName //= ($relSpec->[1] // $fkCol->{"name"}) // $subTab->{"_reference_dict"}{$tab->{"name"}}
$fkName $relSpec->[1] $fkCol->{"name"} $subTab->{"_reference_dict"}{$tab->{"name"}}
Truth table0%0/5
A //= B // C // D
A= $fkNameB= $relSpec->[1]C= $fkCol->{"name"}D= $subTab->{"_reference_dict"}{$tab->{"name"}}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
634 33 ▶ if ($tab->{_relation_dict}{$relName}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tab->{"_relation_dict"}{$relName}) 0 33 33
635 0 croak "Conflicting relation! $tab->{name}.$relName";
636 }
637 33 push @{$tab->{_relation_list}}
638 33 , $tab->{_relation_dict}{$relName}
639 = [$relType => $relName, $fkName, $subTab];
640 }
641  
642 114 ▶sub add_table_column {
Subroutine add_table_column: called CC=11 SCAR=26.6
Pod undocumented
643 114 (my MY $self, my Table $tab, my ($colName, $type, @colSpec)) = @_;
644 114 ▶ if ($tab->{_col_dict}{$colName}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tab->{"_col_dict"}{$colName}) 0 114 114
645 0 croak "Conflicting column name $colName for table $tab->{name}";
646 }
647 # $tab.$colName is encoded by $refTab.pk
648 114 ▶ if (ref $type) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $type) { } 0 114 114
elsif (not defined $type) { } 0 114 114
649 0 croak "Deprecated column spec in $tab->{name}.$colName";
650 } elsif (not defined $type) {
651 0 Carp::cluck "Column type $tab->{name}.$colName is undef";
652 }
653  
654 114 my (@opt, @rels);
655 114 ▶ while (@colSpec) {
Branch: 1 branch
BranchTrueFalseTotal
if (@colSpec) 93 114 207
656 93 ▶ unless (defined (my $key = shift @colSpec)) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined(my $key = shift @colSpec)) { } 93 0 93
if (do { ref $key }) { } 20 73 93
elsif ($key =~ /^-/) { } 73 0 73
657 0 croak "Undefined colum spec for $tab->{name}.$colName";
658 } elsif (ref $key) {
659 20 my ($method, @args) = @$key;
660 20 $method =~ s/^-//;
661 # XXX: [has_many => @tables]
662 # XXX: [unique => k1, k2..]
663 20 ▶ if (my ($relType, @relSpec)
Branch: 1 branch
BranchTrueFalseTotal
if (() = $self->known_rels($method, $colName, @args)) { } 20 0 20
664 = $self->known_rels($method, $colName, @args)) {
665 20 push @rels, [$relType => \@relSpec, @args];
666 } else {
667 0 croak "Unknown method $method";
668 }
669 } elsif ($key =~ /^-/) {
670 73 push @opt, $key => 1;
671 } else {
672 0 push @opt, $key, shift @colSpec;
673 }
674 }
675 114 push @{$tab->{_col_list}}, ($tab->{_col_dict}{$colName})
676 = (my Column $col) = $self->Column->new
677 (@opt, name => $colName, type => $type);
678 114 ▶ $tab->{_pk} = $col if $col->{primary_key};
Branch: 1 branch
BranchTrueFalseTotal
if $col->{"primary_key"} 28 86 114
679  
680 114 $self->add_table_relation($tab, $col, @$_) for @rels;
681  
682 # XXX: Validation: name/option conflicts and others.
683 114 $col;
684 }
685  
686 4 ▶sub add_table_values {
Subroutine add_table_values: called CC=1 SCAR=0.0
Pod undocumented
687 4 (my MY $self, my Table $tab, my ($colspec, @values)) = @_;
688 4 push @{$tab->{_initializer}}, [$colspec, @values];
689 }
690  
691 14 ▶sub verify_schema {
Subroutine verify_schema: called CC=4 SCAR=16.4
Pod undocumented
692 14 (my MY $self) = @_;
693 14 my @not_configured;
694 14 foreach my Table $tab (lexpand($self->{_table_list})) {
695 33 ▶ if ($tab->{_not_configured}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tab->{"_not_configured"}) 0 33 33
696 0 push @not_configured, $tab->{name};
697 0 next;
698 }
699 # foreach my Column $col (lexpand($tab->{_col_list})) { }
700 }
701 14 ▶ if (@not_configured) {
Branch: 1 branch
BranchTrueFalseTotal
if (@not_configured) 0 14 14
702 0 croak "Some tables are not configure, possibly spellmiss!: @not_configured";
703 }
704 }
705  
706 {
707 3 my %known_rels = qw(has_many 1 has_one 1 belongs_to 1
708 many_to_many 1 might_have 1
709 );
710 40 ▶ sub known_rels {
Subroutine known_rels: called CC=3 SCAR=11.0
Pod undocumented
711 40 (my MY $self, my ($desc, $myColName, @args)) = @_;
712 # ['-has_many:rel:fk' => 'table']
713 # has_many ..fk is their_fk
714 # belongs_to ..fk is our_fk
715 40 my ($relType, $relName, $fkName) = split /:/, $desc, 3;
716 40 ▶ return unless $known_rels{$relType};
Branch: 1 branch
BranchTrueFalseTotal
unless $known_rels{$relType} 7 33 40
717 33 ▶ ($relType, $relName, $fkName || $myColName)
Branch: 1 branch
BranchTrueFalseTotal
$fkName or $myColName 3 30 33
718 }
719 }
720  
721 #========================================
722  
723 3 ▶sub sql_create {
Subroutine sql_create: called CC=1 SCAR=0.0
Pod undocumented
724 3 (my MY $schema, my %opts) = @_;
725 3 $schema->foreach_tables_do('sql_create_table', \%opts)
726 }
727  
728 17 ▶sub default_dbtype {'sqlite'}
Subroutine default_dbtype: called CC=1 SCAR=0.0
Pod undocumented
729 26 ▶sub sql_create_table {
Subroutine sql_create_table: called CC=14 SCAR=27.2
Pod undocumented
730 26 (my MY $schema, my Table $tab, my $opts) = @_;
731 26 my (@cols, @indices);
732 26 ▶ my $dbtype = $opts->{_dbtype} || $schema->{_dbtype} || $schema->default_dbtype;
Condition66%4/6
$opts->{'_dbtype'} || $schema->{'_dbtype'} || $schema->default_dbtype
l!l&&r!l&&!r
9170
$opts->{"_dbtype"} or $schema->{"_dbtype"}
l!l&&r!l&&!r
0917
MC/DC0%0/3
$opts->{'_dbtype'} || $schema->{'_dbtype'} || $schema->default_dbtype
$opts->{"_dbtype"} $schema->{"_dbtype"} $schema->default_dbtype
Truth table50%2/4
A or B || C
A= $opts->{"_dbtype"}B= $schema->{"_dbtype"}C= $schema->default_dbtype
ABCresult
1XX1
01X1
0011
0000
733 26 ▶ my $sub = $schema->can($dbtype.'_sql_create_column')
Condition33%1/3
$schema->can($dbtype . '_sql_create_column') || $schema->can('sql_create_column')
l!l&&r!l&&!r
2600
MC/DC0%0/2
$schema->can($dbtype . '_sql_create_column') || $schema->can('sql_create_column')
$schema->can($dbtype . '_sql_create_column') $schema->can('sql_create_column')
Truth table33%1/3
A || B
A= $schema->can($dbtype . '_sql_create_column')B= $schema->can('sql_create_column')
ABresult
1X1
011
000
734 || $schema->can('sql_create_column');
735  
736 26 my $pk_ok;
737 26 foreach my Column $col (@{$tab->{_col_list}}) {
738 87 ▶ $pk_ok = 1 if $col->{primary_key};
Branch: 1 branch
BranchTrueFalseTotal
if $col->{"primary_key"} 23 64 87
739 87 push @cols, $sub->($schema, $tab, $col, $opts);
740 87 ▶ push @indices, $col if $col->{indexed};
Branch: 1 branch
BranchTrueFalseTotal
if $col->{"indexed"} 9 78 87
741 }
742  
743 # Multi column primary key(...)
744 # XXX: conflict clause
745 26 ▶ if (not $pk_ok and $tab->{_pk}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $pk_ok and $tab->{"_pk"}) 3 23 26
Condition66%2/3
not $pk_ok and $tab->{"_pk"}
!ll&&!rl&&r
2303
MC/DC50%1/2
not $pk_ok and $tab->{"_pk"}
not $pk_ok $tab->{"_pk"}
Truth table66%2/3
not(A) and B
A= not $pk_okB= $tab->{"_pk"}
ABresult
0X0
100
111
746 push @cols, "PRIMARY KEY(".join(", ", map {
747 6 my Column $col = $_;
748 $col->{name}
749 3 } @{$tab->{_pk}}).")";
750 }
751  
752 # Other unique(...)
753 26 foreach my $constraint (lexpand($tab->{_chk_unique})) {
754 0 push @cols, sprintf q{unique(%s)}, join(", ", @$constraint);
755 }
756  
757 # XXX: SQLite specific.
758 # XXX: MySQL ENGINE(TYPE) = ...
759 push my @create
760 , sprintf qq{CREATE TABLE %s\n(%s)}, $tab->{name}
761 26 , join "\n, ", @cols;
762  
763 26 foreach my Column $ix (@indices) {
764 push @create
765 , sprintf q{CREATE INDEX %1$s_%2$s on %1$s(%2$s)}
766 9 , $tab->{name}, $ix->{name};
767 }
768  
769 26 foreach my $colnames (lexpand($tab->{_chk_index})) {
770 0 my $ixname = join "_", $tab->{name}, @$colnames;
771 push @create, sprintf(q{CREATE INDEX %s on %s(%s)}
772 , $tab->{name}
773 0 , join("_", $tab->{name}, @$colnames)
774 , join(",", @$colnames));
775 }
776  
777 # after delete on user for each row begin
778 26 ▶ if (my $trigger = $tab->{trigger_after_delete}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $trigger = $tab->{"trigger_after_delete"}) 1 25 26
779 push @create, map {
780 1 qq{CREATE TRIGGER $_ AFTER DELETE ON $tab->{name}}
781 1 . qq{ FOR EACH ROW } . $schema->sql_compound_trigger($trigger->{$_});
782 } keys %$trigger;
783 }
784  
785 26 ▶ wantarray ? @create : join(";\n", @create);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 26 0 26
786 }
787  
788 # XXX: text => varchar(80)
789 64 ▶sub map_coltype {
Subroutine map_coltype: called CC=2 SCAR=8.1
Pod undocumented
790 64 (my MY $schema, my $typeName) = @_;
791 64 ▶ $schema->{coltype_map}{$typeName} // $typeName;
Condition33%1/3
$schema->{"coltype_map"}{$typeName} // $typeName
l!l&&r!l&&!r
0640
MC/DC0%0/2
$schema->{"coltype_map"}{$typeName} // $typeName
$schema->{"coltype_map"}{$typeName} $typeName
Truth table33%1/3
A // B
A= $schema->{"coltype_map"}{$typeName}B= $typeName
ABresult
1X1
011
000
792 }
793  
794 64 ▶sub sql_create_column {
Subroutine sql_create_column: called CC=4 SCAR=14.5
Pod undocumented
795 64 (my MY $schema, my Table $tab, my Column $col, my $opts) = @_;
796 # XXX: primary key ASC/DESC
797 join(" ", $col->{name}
798 , $schema->map_coltype($col->{type})
799 , ($col->{primary_key} ? "primary key" : ())
800 , ($col->{unique} ? "unique" : ())
801 64 ▶ , ($col->{autoincrement} ? "auto_increment" : ()));
Branch: 3 branches
BranchTrueFalseTotal
$col->{'primary_key'} ? : 0 64 64
$col->{'unique'} ? : 5 59 64
$col->{'autoincrement'} ? : 0 64 64
802 }
803  
804 87 ▶sub sqlite_sql_create_column {
Subroutine sqlite_sql_create_column: called CC=4 SCAR=14.0
Pod undocumented
805 87 (my MY $schema, my Table $tab, my Column $col, my $opts) = @_;
806 87 ▶ unless (defined $col->{type}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $col->{'type'}) { } 87 0 87
if (do { $col->{'type'} =~ /^int/i and $col->{'primary_key'} }) { } 23 64 87
807 0 croak "Column type is not yet defined! $tab->{name}.$col->{name}"
808 ▶ } elsif ($col->{type} =~ /^int/i && $col->{primary_key}) {
Condition100%3/3
$col->{"type"} =~ /^int/i and $col->{"primary_key"}
!ll&&!rl&&r
521223
MC/DC100%2/2
$col->{"type"} =~ /^int/i and $col->{"primary_key"}
$col->{"type"} =~ /^int/i $col->{"primary_key"}
Truth table100%3/3
A and B
A= $col->{"type"} =~ /^int/iB= $col->{"primary_key"}
ABresult
0X0
100
111
809 23 "$col->{name} integer primary key"
810 } else {
811 64 $schema->sql_create_column($tab, $col, $opts);
812 }
813 }
814  
815 1 ▶sub sql_compound_trigger {
Subroutine sql_compound_trigger: called CC=2 SCAR=7.1
Pod undocumented
816 1 (my MY $schema, my $item) = @_;
817 1 ▶ my $sub = $schema->can($$schema{_dbtype}.'_sql_compound_trigger')
Branch: 1 branch
BranchTrueFalseTotal
my $sub = $schema->can($schema->{"_dbtype"} . '_sql_compound_trigger') or croak(qq[Compound trigger for $schema->{"_dbtype"} is not yet implemented]) 1 0 1
818 or croak "Compound trigger for $$schema{_dbtype} is not yet implemented";
819 1 $sub->($schema, $item);
820 }
821  
822 0 ▶sub mysql_sql_compound_trigger {
Subroutine mysql_sql_compound_trigger: not called CC=3 SCAR=24.8
Pod undocumented
823 0 (my MY $schema, my $item) = @_;
824 0 ▶ unless (ref $item) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $item) { } 0 0 0
if (do { @$item == 1 }) { } 0 0 0
825 0 $item
826 } elsif (@$item == 1) {
827 0 $item->[0]
828 } else {
829 0 "BEGIN ".join("; ", @$item). "; END";
830 }
831 }
832  
833 1 ▶sub sqlite_sql_compound_trigger {
Subroutine sqlite_sql_compound_trigger: called CC=2 SCAR=7.2
Pod undocumented
834 1 (my MY $schema, my $item) = @_;
835 1 ▶ "BEGIN ".join("; ", ref $item ? @$item : $item). "; END";
Branch: 1 branch
BranchTrueFalseTotal
ref $item ? : 0 1 1
836 }
837  
838 0 ▶sub sql_drop {
Subroutine sql_drop: not called CC=1 SCAR=6.9
Pod undocumented
839 shift->foreach_tables_do
840 0 ▶ (sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
841 0 (my Table $tab) = @_;
842 0 qq{drop table $tab->{name}};
843 })
844 0 }
845  
846 3 ▶sub foreach_tables_do {
Subroutine foreach_tables_do: called CC=5 SCAR=16.4
Pod undocumented
847 3 (my MY $self, my $method, my $opts) = @_;
848 5 ▶ my $code = ref $method ? $method : sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
849 5 $self->$method(@_);
850 3 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
ref $method ? : 0 3 3
851 3 my @result;
852 3 my $wantarray = wantarray;
853 3 foreach my Table $tab (@{$self->{_table_list}}) {
854 push @result, map {
855 5 ▶ $wantarray ? $_ . "\n" : $_
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 7 0 7
856 } $code->($tab, $opts);
857 }
858 3 ▶ wantarray ? @result : join(";\n", @result);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 0 3
859 }
860  
861 ########################################
862 # Below is poorman's CRUD closure generator(instead of ORM).
863  
864  
865 1 ▶sub to_encode {
Subroutine to_encode: called CC=1 SCAR=0.0
Pod undocumented
866 1 (my MY $self, my $tabName, my $keyCol, my @otherCols) = @_;
867  
868 1 my $to_find = $self->to_find($tabName, $keyCol);
869 1 my $to_ins = $self->to_insert($tabName, $keyCol, @otherCols);
870  
871 2 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
872 2 my ($value, @rest) = @_;
873 2 ▶ $to_find->($value) || $to_ins->($value, @rest);
Branch: 1 branch
BranchTrueFalseTotal
&$to_find($value) or &$to_ins($value, @rest) 1 1 2
874 1 };
875 }
876  
877 # to_fetchall は別途用意する
878 2 ▶sub to_find {
Subroutine to_find: called CC=2 SCAR=7.0
Pod undocumented
879 2 (my MY $self, my ($tabName, $keyCol, $rowidCol)) = @_;
880 2 my $sql = $self->sql_to_find($tabName, $keyCol, $rowidCol);
881 2 ▶ print STDERR "-- $sql\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 2 2
882 2 my $sth;
883 3 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
884 3 my ($value) = @_;
885 3 ▶ $sth ||= $self->dbh->prepare($sql);
Condition66%2/3
$sth ||= $self->dbh->prepare($sql)
l!l&&r!l&&!r
120
MC/DC0%0/2
$sth ||= $self->dbh->prepare($sql)
$sth $self->dbh->prepare($sql)
Truth table66%2/3
A ||= B
A= $sthB= $self->dbh->prepare($sql)
ABresult
1X1
011
000
886 3 $sth->execute($value);
887 3 ▶ my ($rowid) = $sth->fetchrow_array
Branch: 1 branch
BranchTrueFalseTotal
my($rowid) = $sth->fetchrow_array or return 2 1 3
888 or return;
889 2 $rowid;
890 2 };
891 }
892  
893 2 ▶sub to_fetch {
Subroutine to_fetch: called CC=2 SCAR=7.1
Pod undocumented
894 2 (my MY $self, my ($tabName, $keyColList, $resColList, @rest)) = @_;
895 2 my $sql = $self->sql_to_fetch($tabName, $keyColList, $resColList, @rest);
896 2 ▶ print STDERR "-- $sql\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 2 2
897 2 my $sth;
898 2 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.4
899 2 my (@value) = @_;
900 2 ▶ $sth ||= $self->dbh->prepare($sql);
Condition33%1/3
$sth ||= $self->dbh->prepare($sql)
l!l&&r!l&&!r
020
MC/DC0%0/2
$sth ||= $self->dbh->prepare($sql)
$sth $self->dbh->prepare($sql)
Truth table33%1/3
A ||= B
A= $sthB= $self->dbh->prepare($sql)
ABresult
1X1
011
000
901 2 $sth->execute(@value);
902 2 $sth;
903 2 };
904 }
905  
906 3 ▶sub to_insert {
Subroutine to_insert: called CC=2 SCAR=7.0
Pod undocumented
907 3 (my MY $self, my ($tabName, @fields)) = @_;
908 3 my $sql = $self->sql_to_insert($tabName, @fields);
909 3 ▶ print STDERR "-- $sql\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 3 3
910 3 my $sth;
911 4 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
912 4 my (@value) = @_;
913 4 ▶ $sth ||= $self->dbh->prepare($sql);
Condition66%2/3
$sth ||= $self->dbh->prepare($sql)
l!l&&r!l&&!r
130
MC/DC0%0/2
$sth ||= $self->dbh->prepare($sql)
$sth $self->dbh->prepare($sql)
Truth table66%2/3
A ||= B
A= $sthB= $self->dbh->prepare($sql)
ABresult
1X1
011
000
914 # print STDERR "-- inserting @value to $sql\n";
915 4 $sth->execute(@value);
916 4 $self->dbh->last_insert_id('', '', '', '');
917 3 };
918 }
919  
920 2 ▶sub sql_to_find {
Subroutine sql_to_find: called CC=3 SCAR=12.0
Pod undocumented
921 2 (my MY $self, my ($tabName, $keyCol, $rowidCol)) = @_;
922 2 ▶ my Table $tab = $self->{_table_dict}{$tabName}
Branch: 1 branch
BranchTrueFalseTotal
my YATT::Lite::WebMVC0::DBSchema::Table $tab = $self->{"_table_dict"}{$tabName} or croak("No such table: $tabName") 2 0 2
923 or croak "No such table: $tabName";
924 # XXX: col name check.
925 2 ▶ $rowidCol ||= $self->rowid_col($tab);
Condition33%1/3
$rowidCol ||= $self->rowid_col($tab)
l!l&&r!l&&!r
020
MC/DC0%0/2
$rowidCol ||= $self->rowid_col($tab)
$rowidCol $self->rowid_col($tab)
Truth table33%1/3
A ||= B
A= $rowidColB= $self->rowid_col($tab)
ABresult
1X1
011
000
926 2 <<END;
927 select $rowidCol from $tabName where $keyCol = ?
928 END
929 }
930  
931 4 ▶sub sql_to_fetch {
Subroutine sql_to_fetch: called CC=10 SCAR=29.3
Pod undocumented
932 4 (my MY $self, my ($tabName, $keyColList, $resColList, %opts)) = @_;
933 4 my $group_by = delete $opts{group_by};
934 4 my $order_by = delete $opts{order_by};
935 4 ▶ my Table $tab = $self->{_table_dict}{$tabName}
Branch: 1 branch
BranchTrueFalseTotal
my YATT::Lite::WebMVC0::DBSchema::Table $tab = $self->{"_table_dict"}{$tabName} or croak("No such table: $tabName") 4 0 4
936 or croak "No such table: $tabName";
937 # XXX: col name check... いや、式かもしれないし。
938 4 ▶ my $cols = $resColList ? join(", ", lexpand $resColList) : '*';
Branch: 1 branch
BranchTrueFalseTotal
$resColList ? : 4 0 4
939 4 my $where = do {
940 4 ▶ unless (defined $keyColList) {
Branch: 4 branches
BranchTrueFalseTotal
if (defined $keyColList) { } 4 0 4
if (do { not ref $keyColList }) { } 4 0 4
elsif (ref $keyColList eq 'ARRAY') { } 0 0 0
elsif (ref $keyColList eq 'SCALAR') { } 0 0 0
941 0 undef;
942 } elsif (not ref $keyColList) {
943 4 "$keyColList = ?"
944 } elsif (ref $keyColList eq 'ARRAY') {
945 0 join " AND ", map {"$_ = ?"} @$keyColList
946 } elsif (ref $keyColList eq 'SCALAR') {
947 # RAW SQL
948 0 $$keyColList;
949 } else {
950 0 die "Not yet implemented!";
951 }
952 };
953 4 ▶ if ($group_by) {
Branch: 1 branch
BranchTrueFalseTotal
if ($group_by) 0 4 4
954 0 $where .= " GROUP BY $group_by";
955 }
956 4 ▶ if ($order_by) {
Branch: 1 branch
BranchTrueFalseTotal
if ($order_by) 2 2 4
957 2 $where .= " ORDER BY $order_by";
958 }
959 4 ▶ qq|select $cols from $tabName| . (defined $where ? " where $where" : "");
Branch: 1 branch
BranchTrueFalseTotal
defined $where ? : 4 0 4
960 }
961  
962  
963 7 ▶sub sql_to_insert {
Subroutine sql_to_insert: called CC=1 SCAR=0.0
Pod undocumented
964 7 (my MY $self, my ($tabName, @fields)) = @_;
965 sprintf qq{INSERT INTO $tabName(%s) VALUES(%s)}
966 , join(", ", @fields)
967 7 , join(", ", map {'?'} @fields);
968 }
969  
970 0 ▶sub sql_and_values_to_insert_expr {
Subroutine sql_and_values_to_insert_expr: not called CC=2 SCAR=17.9
Pod undocumented
971 0 (my MY $self, my ($tabName, $colNames, $valsOrExprs)) = @_;
972 0 my (@values);
973 my @exprs = map {
974 0 ▶ if (ref $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) { } 0 0 0
975 0 $$_;
976 } else {
977 0 push @values, $_;
978 0 '?'
979 }
980 } @$valsOrExprs;
981 0 my $sql = sprintf qq{INSERT INTO $tabName(%s) VALUES(%s)}
982 , join(", ", @$colNames), join(", ", @exprs);
983  
984 0 ($sql, \@values);
985 }
986  
987  
988 0 ▶sub default_rowid_col { 'rowid' }
Subroutine default_rowid_col: not called CC=1 SCAR=6.9
Pod undocumented
989 2 ▶sub rowid_col {
Subroutine rowid_col: called CC=2 SCAR=7.6
Pod undocumented
990 2 (my MY $schema, my Table $tab) = @_;
991 2 ▶ if (my Column $pk = $tab->{_pk}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my YATT::Lite::WebMVC0::DBSchema::Column $pk = $tab->{'_pk'}) { } 2 0 2
992 $pk->{name}
993 2 } else {
994 # XXX: dbtype dispatch
995 0 $schema->default_rowid_col;
996 }
997 }
998  
999 ########################################
1000  
1001 15 ▶sub add_inc {
Subroutine add_inc: called CC=1 SCAR=0.0
Pod undocumented
1002 15 my ($pack, $callpack) = @_;
1003 15 $callpack =~ s{::}{/}g;
1004 15 $INC{$callpack . '.pm'} = 1;
1005 }
1006  
1007 ########################################
1008  
1009 3 ▶use YATT::Lite::XHF::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
1010  
1011 0 ▶sub cmd_deploy {
Subroutine cmd_deploy: not called CC=1 SCAR=6.9
Pod undocumented
1012 0 (my MY $schema) = @_;
1013 0 local $schema->{verbose} = 1;
1014 0 my $dbh = $schema->dbh;
1015 0 local $dbh->{AutoCommit};
1016 0 $schema->ensure_created_on($dbh);
1017 0 $dbh->commit;
1018 }
1019  
1020 0 ▶sub cmd_schema {
Subroutine cmd_schema: not called CC=1 SCAR=6.9
Pod undocumented
1021 0 (my MY $schema) = @_;
1022 print $schema->dump_xhf(map {
1023 0 $schema->info_tableobj($_);
1024 0 } @{$schema->{_table_list}}), "\n";
1025 }
1026  
1027 0 ▶sub info_tableobj {
Subroutine info_tableobj: not called CC=1 SCAR=6.9
Pod undocumented
1028 0 (my MY $schema, my Table $tab) = @_;
1029 [$tab->{name}, undef, map {
1030 0 $schema->info_columnobj($_);
1031 0 } @{$tab->{_col_list}}];
1032 }
1033  
1034 0 ▶sub info_columnobj {
Subroutine info_columnobj: not called CC=1 SCAR=6.9
Pod undocumented
1035 0 (my MY $schema, my Column $col) = @_;
1036 0 ($col->{name}, $col->{type});
1037 }
1038  
1039 0 ▶sub cmd_help {
Subroutine cmd_help: not called CC=3 SCAR=24.8
Pod undocumented
1040 0 my ($self) = @_;
1041 0 ▶ my $pack = ref($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
1042 0 my @opts = do {
1043 0 ▶ if (my $sub = $pack->can('cf_list')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = $pack->can('cf_list')) { } 0 0 0
1044 0 $sub->($pack);
1045 } else {
1046 0 ();
1047 }
1048 };
1049 0 require YATT::Lite::Util::FindMethods;
1050 my @methods = YATT::Lite::Util::FindMethods::FindMethods
1051 0 ▶ ($pack, , sub {s/^cmd_//});
Subroutine __ANON__: not called CC=1 SCAR=6.9
1052 0 die <<END;
1053 0 Usage: @{[basename($0)]} [--opt=value] <command> [--opt=value] [<args>]
1054  
1055 Available commands are:
1056 0 @{[join("\n ", @methods)]}
1057  
1058 All options(might not usefull) are:
1059 0 @{[join "\n ", map {"--$_"} @opts]}
1060 END
1061  
1062 }
1063  
1064 #========================================
1065  
1066 0 ▶sub ymd_hms {
Subroutine ymd_hms: not called CC=2 SCAR=17.9
Pod undocumented
1067 0 my ($pack, $time, $as_utc) = @_;
1068 my ($S, $M, $H, $d, $m, $y) = map {
1069 0 ▶ $as_utc ? gmtime($_) : localtime($_)
Branch: 1 branch
BranchTrueFalseTotal
$as_utc ? : 0 0 0
1070 } $time;
1071 0 sprintf q{%04d-%02d-%02d %02d:%02d:%02d}, 1900+$y, $m+1, $d, $H, $M, $S;
1072 }
1073  
1074 3 ▶sub lhexpand {
Subroutine lhexpand: called CC=4 SCAR=17.9
Pod undocumented
1075 3 ▶ return unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 3 3
1076 3 ▶ ref $_[0] eq 'HASH' ? %{$_[0]}
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 0 0
1077 0 ▶ : ref $_[0] eq 'ARRAY' ? @{$_[0]}
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 3 0 3
1078 : croak "Invalid option: $_[0]";
1079 }
1080  
1081 3 ▶use YATT::Lite::Breakpoint ();
Subroutine BEGIN: called CC=1 SCAR=0.0
1082 3 YATT::Lite::Breakpoint::break_load_dbschema();
1083  
1084 3 1;
« blib/lib/YATT/Lite/PageConsts.pm ↑ Summary blib/lib/YATT/Lite/CLI/Lint.pm »