web/cgi-bin/yatt.lib/YATT/DBSchema.pm

filter: Statementstmt 8.6% 38 / 439 Branchbran 0.0% 0 / 208 Conditioncond 0.0% 0 / 71 MC/DCmcdc 0.0% 0 / 45 Subroutinesub 17.6% 12 / 68 Podpod 0.0% 0 / 47 totaltotal 5.6% 50 / 878 CC 145 SCAR 38.9
CC
145
Cov
5%
run
156.0
sql_join
132.0
sql_create_table
110.0
CRAP 48.8
« web/cgi-bin/yatt.lib/YATT/Toplevel/CGI.pm ↑ Summary blib/lib/YATT/Toplevel/Server.pm »
1 package YATT::DBSchema;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings qw(FATAL all NONFATAL misc);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use YATT::Util::CmdLine;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use base qw(YATT::Class::Configurable);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use YATT::Fields (qw(schemas tables cf_DBH
Subroutine BEGIN: called CC=1 SCAR=0.0
11 cf_user
12 cf_auth
13 ^cf_connection_spec
14 ^cf_verbose
15 )
16 , ['^cf_dbtype' => 'sqlite']
17 , ['^cf_NULL' => '']
18 , ['^cf_name' => 'DBSchema']
19 , qw(
20 cf_no_header
21 cf_auto_create
22 cf_as_base
23 )
24 1 );
25  
26 1 ▶use YATT::Types [Item => [qw(cf_name)]];
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 1 ▶use YATT::Types -base => Item
Subroutine BEGIN: called CC=1 SCAR=0.0
29 , [Table => [qw(pk raw_create chk_unique chk_index chk_check colNames)]
30 , [Column => [qw(colnum
31 cf_type
32 cf_hidden
33 cf_unique
34 cf_indexed
35 cf_decode_depth
36 cf_encoded_by
37 cf_updated
38 cf_primary_key
39 cf_auto_increment
40 1 )]]];
41 1 ▶use YATT::Util::Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use YATT::Util qw(coalesce);
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 require YATT::Inc;
44  
45 #----------------------------------------
46  
47 0 ▶sub YATT::DBSchema::Table::rowid_spec {
Subroutine rowid_spec: not called CC=2 SCAR=17.9
Pod undocumented
48 0 (my Table $tab, my $schema) = @_;
49 0 ▶ if (my Column $pk = $tab->{pk}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my YATT::DBSchema::Column $pk = $tab->{'pk'}) { } 0 0 0
50 $pk->{cf_name}
51 0 } else {
52 0 $schema->rowid_col;
53 }
54 }
55  
56 0 ▶sub rowid_col { 'rowid' }
Subroutine rowid_col: not called CC=1 SCAR=6.9
Pod undocumented
57  
58 #========================================
59 # Class Hierarchy in case of 'package YourSchema; use YATT::DBSchema':
60 #
61 # YATT::DBSchema (or its subclass)
62 # ↑
63 # YourSchema::DBSchema (holds singleton $SCHEMA and &SCHEMA)
64 # ↑
65 # YourSchema
66 #
67  
68 0 ▶sub import {
Subroutine import: not called CC=2 SCAR=17.9
69 0 my ($pack) = shift;
70 0 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
71 0 my MY $schema = $pack->define(@_);
72  
73 0 $schema->export_and_rebless_with(caller);
74 }
75  
76 0 ▶sub export_and_rebless_with {
Subroutine export_and_rebless_with: not called CC=3 SCAR=24.8
Pod undocumented
77 0 (my MY $schema, my ($callpack)) = @_;
78  
79 # Allocate new class.
80 0 my $classFullName = join("::", $callpack, $schema->name);
81 0 YATT::Inc->add_inc($classFullName);
82 0 eval sprintf q{use strict; package %s; use base qw(%s)}
83 , $classFullName, ref $schema;
84 # MY->add_isa($classFullName, $pack);
85 eval qq{use strict; package $callpack; use base qw($classFullName)}
86 0 ▶ if $schema->{cf_as_base};
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{"cf_as_base"} 0 0 0
87 # MY->add_isa($callpack, $classFullName);
88  
89 0 my $glob = globref($classFullName, "SCHEMA");
90 0 *{$glob} = \ $schema;
91 0 define_const($glob, $schema);
92  
93 0 $schema->export_to($callpack);
94  
95 $schema->rebless_with($callpack)
96 0 ▶ if $schema->{cf_as_base};
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{"cf_as_base"} 0 0 0
97 }
98  
99 0 ▶sub export_to {
Subroutine export_to: not called CC=1 SCAR=6.9
Pod undocumented
100 0 (my MY $schema, my ($callpack)) = @_;
101 # Install to caller
102 0 define_const(globref($callpack, $schema->name), $schema);
103 # XXX: special new for singleton. (for schema->new->run)
104 0 ▶ *{globref($callpack, 'new')} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
105 0 shift;
106 0 ▶ $schema->configure(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
107 0 $schema;
108 0 };
109 }
110  
111 #========================================
112 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
113 0 my MY $schema = shift;
114 0 ▶ if ($schema->{cf_DBH}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"cf_DBH"}) 0 0 0
115 # XXX: sqlite specific commit.
116 0 $schema->{cf_DBH}->commit;
117 }
118 }
119  
120 #========================================
121  
122 0 ▶sub define {
Subroutine define: not called CC=3 SCAR=24.8
Pod undocumented
123 0 my ($pack) = shift;
124 0 $pack->parse_import(\@_, \ my %opts);
125 0 my MY $self = $pack->new(%opts);
126 0 foreach my $item (@_) {
127 0 ▶ if (ref $item) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item) { } 0 0 0
128 0 $self->add_table(@$item);
129 } else {
130 0 croak "Invalid schema item: $item";
131 }
132 }
133 0 $self;
134 }
135  
136 0 ▶sub parse_import {
Subroutine parse_import: not called CC=5 SCAR=34.0
Pod undocumented
137 0 my ($pack, $list, $opts) = @_;
138 # -bool_flag
139 # key => value
140 0 ▶ for (; @$list; shift @$list) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$list) 0 0 0
141 0 ▶ last if ref $list->[0];
Branch: 1 branch
BranchTrueFalseTotal
if ref $list->[0] 0 0 0
142 0 ▶ if ($list->[0] =~ /^-(\w+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list->[0] =~ /^-(\w+)/) { } 0 0 0
143 0 $opts->{$1} = 1;
144 } else {
145 0 ▶ croak "Option value is missing for $list->[0]"
Branch: 1 branch
BranchTrueFalseTotal
unless @$list >= 2 0 0 0
146 unless @$list >= 2;
147 0 $opts->{$list->[0]} = $list->[1];
148 0 shift @$list;
149 }
150 }
151 }
152  
153 #========================================
154  
155 0 ▶sub has_connection {
Subroutine has_connection: not called CC=1 SCAR=6.9
Pod undocumented
156 0 my MY $schema = shift;
157 $schema->{cf_DBH}
158 0 }
159  
160 0 ▶sub dbh {
Subroutine dbh: not called CC=6 SCAR=37.4
Pod undocumented
161 0 (my MY $schema, my $spec) = @_;
162 0 ▶ unless ($schema->{cf_DBH}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema->{"cf_DBH"}) 0 0 0
163 0 ▶ unless (defined ($spec ||= $schema->connection_spec)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($spec ||= $schema->connection_spec)) 0 0 0
Condition0%0/3
$spec ||= $schema->connection_spec
l!l&&r!l&&!r
000
MC/DC0%0/2
$spec ||= $schema->connection_spec
$spec $schema->connection_spec
Truth table0%0/3
A ||= B
A= $specB= $schema->connection_spec
ABresult
1X1
011
000
164 0 croak "connection_spec is empty";
165 }
166 0 ▶ if (ref $spec eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $spec eq 'ARRAY') { } 0 0 0
elsif (ref $spec eq 'CODE') { } 0 0 0
167 0 $schema->connect_to(@$spec);
168 } elsif (ref $spec eq 'CODE') {
169 0 $schema->{cf_DBH} = $spec->($schema);
170 } else {
171 0 croak "Unknown connection spec obj: $spec";
172 }
173 };
174  
175 $schema->{cf_DBH}
176 0 }
177  
178 0 ▶sub connect_to {
Subroutine connect_to: not called CC=2 SCAR=17.9
Pod undocumented
179 0 (my MY $schema, my ($dbtype)) = splice @_, 0, 2;
180 0 ▶ if (my $sub = $schema->can("connect_to_$dbtype")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = $schema->can("connect_to_$dbtype")) { } 0 0 0
181 0 $sub->($schema, @_);
182 } else {
183 0 croak sprintf("%s: Unknown dbtype: %s", MY, $dbtype);
184 }
185 }
186  
187 0 ▶sub connect_to_sqlite {
Subroutine connect_to_sqlite: not called CC=2 SCAR=17.9
Pod undocumented
188 0 (my MY $schema, my ($dbname, $rwflag)) = @_;
189 0 ▶ my $ro = defined $rwflag && $rwflag =~ /ro/i;
Condition0%0/3
defined $rwflag && $rwflag =~ /ro/i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $rwflag && $rwflag =~ /ro/i
defined $rwflag $rwflag =~ /ro/i
Truth table0%0/3
A && B
A= defined $rwflagB= $rwflag =~ /ro/i
ABresult
0X0
100
111
190 0 my $dbi_dsn = "dbi:SQLite:dbname=$dbname";
191 0 $schema->{cf_auto_create} = 1;
192 0 $schema->connect_to_dbi
193 ($dbi_dsn, undef, undef
194 , RaiseError => 1, PrintError => 0, AutoCommit => $ro);
195 }
196  
197 0 ▶sub connect_to_dbi {
Subroutine connect_to_dbi: not called CC=4 SCAR=30.0
Pod undocumented
198 0 (my MY $schema, my ($dbi_dsn, $user, $auth, %param)) = @_;
199 0 ▶ map {$param{$$_[0]} = $$_[1] unless defined $param{$$_[0]}}
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param{$_->[0]} 0 0 0
200 ([RaiseError => 1], [PrintError => 0], [AutoCommit => 0]);
201 0 require DBI;
202 0 ▶ if ($dbi_dsn =~ m{^dbi:(\w+):}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbi_dsn =~ /^dbi:(\w+):/) 0 0 0
203 0 $schema->configure(dbtype => lc($1));
204 }
205 0 my $dbh = $schema->{cf_DBH} = DBI->connect($dbi_dsn, $user, $auth, \%param);
206 0 ▶ $schema->create if $schema->{cf_auto_create};
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{"cf_auto_create"} 0 0 0
207 0 $dbh;
208 }
209  
210 #
211 # ./lib/MyApp.pm create sqlite data/myapp.db3
212 #
213 0 ▶sub create {
Subroutine create: not called CC=6 SCAR=37.4
Pod undocumented
214 0 (my MY $schema, my @spec) = @_;
215 0 ▶ my $dbh = $schema->dbh(@spec ? \@spec : ());
Branch: 1 branch
BranchTrueFalseTotal
@spec ? : 0 0 0
216 0 foreach my Table $table (@{$schema->{schemas}}) {
217 0 ▶ next if $schema->has_table($table->{cf_name}, $dbh);
Branch: 1 branch
BranchTrueFalseTotal
if $schema->has_table($table->{"cf_name"}, $dbh) 0 0 0
218 0 foreach my $create ($schema->sql_create_table($table)) {
219 0 ▶ print STDERR "$create\n" if $schema->{cf_verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{"cf_verbose"} 0 0 0
220 0 $dbh->do($create);
221 }
222 }
223 }
224  
225 0 ▶sub has_table {
Subroutine has_table: not called CC=2 SCAR=17.9
Pod undocumented
226 0 (my MY $schema, my ($table, $dbh)) = @_;
227 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
228 0 $dbh->tables("", "", $table, 'TABLE');
229 }
230  
231 0 ▶sub tables {
Subroutine tables: not called CC=1 SCAR=6.9
Pod undocumented
232 0 my MY $schema = shift;
233 0 keys %{$schema->{tables}};
234 }
235  
236 0 ▶sub has_column {
Subroutine has_column: not called CC=2 SCAR=17.9
Pod undocumented
237 0 (my MY $schema, my ($table, $column, $dbh)) = @_;
238 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
239 0 exists $hash->{$column};
240 }
241  
242 0 ▶sub columns_hash {
Subroutine columns_hash: not called CC=2 SCAR=17.9
Pod undocumented
243 0 (my MY $schema, my ($table, $dbh)) = @_;
244 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
245 0 my $sth = $dbh->prepare("select * from $table limit 0");
246 0 $sth->execute;
247 0 my %hash = %{$sth->{NAME_hash}};
248 0 \%hash;
249 }
250  
251 0 ▶sub drop {
Subroutine drop: not called CC=2 SCAR=17.9
Pod undocumented
252 0 (my MY $schema) = @_;
253 0 foreach my $sql ($schema->sql_drop) {
254 0 $schema->dbh->do($sql);
255 }
256 }
257  
258 #========================================
259  
260 0 ▶sub add_table {
Subroutine add_table: not called CC=8 SCAR=42.8
Pod undocumented
261 0 (my MY $self, my ($name, $opts, @columns)) = @_;
262 0 ▶ $self->{tables}{$name} ||= do {
Condition0%0/3
$self->{"tables"}{$name} ||= do { push @{$$self{"schemas"};}, my YATT::DBSchema::Table $tab = $self->Table->new; $tab->{"cf_name"} = $name; if (@columns) { $tab->{"raw_create"} = $opts; my $fields = $tab->fields_hash; foreach my $desc (@columns) { if (ref(my $kw = $desc->[0])) { if ($$fields{my $fname = "chk_$$kw"}) { push @{$$tab{$fname};}, [@{$desc;}[1 .. $desc->$#*]]; } else { croak("Invalid column constraint $kw for table $name"); } ; } else { () = @$desc; $self->add_table_column($tab, $col, $type, map({if (/^-(\w+)/) { $1, 1; } else { $_; } ;} @desc)); } ; } ; } elsif (not ref $opts) { $self->add_table_column($tab, $name . 'no', "integer", "primary_key", 1); $self->add_table_column($tab, $name, $opts, "unique", 1); } else { die "Unknown table desc $name $opts"; } ; $tab }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"tables"}{$name} ||= do { push @{$$self{"schemas"};}, my YATT::DBSchema::Table $tab = $self->Table->new; $tab->{"cf_name"} = $name; if (@columns) { $tab->{"raw_create"} = $opts; my $fields = $tab->fields_hash; foreach my $desc (@columns) { if (ref(my $kw = $desc->[0])) { if ($$fields{my $fname = "chk_$$kw"}) { push @{$$tab{$fname};}, [@{$desc;}[1 .. $desc->$#*]]; } else { croak("Invalid column constraint $kw for table $name"); } ; } else { () = @$desc; $self->add_table_column($tab, $col, $type, map({if (/^-(\w+)/) { $1, 1; } else { $_; } ;} @desc)); } ; } ; } elsif (not ref $opts) { $self->add_table_column($tab, $name . 'no', "integer", "primary_key", 1); $self->add_table_column($tab, $name, $opts, "unique", 1); } else { die "Unknown table desc $name $opts"; } ; $tab }
$self->{"tables"}{$name} do { push @{$$self{"schemas"};}, my YATT::DBSchema::Table $tab = $self->Table->new; $tab->{"cf_name"} = $name; if (@columns) { $tab->{"raw_create"} = $opts; my $fields = $tab->fields_hash; foreach my $desc (@columns) { if (ref(my $kw = $desc->[0])) { if ($$fields{my $fname = "chk_$$kw"}) { push @{$$tab{$fname};}, [@{$desc;}[1 .. $desc->$#*]]; } else { croak("Invalid column constraint $kw for table $name"); } ; } else { () = @$desc; $self->add_table_column($tab, $col, $type, map({if (/^-(\w+)/) { $1, 1; } else { $_; } ;} @desc)); } ; } ; } elsif (not ref $opts) { $self->add_table_column($tab, $name . 'no', "integer", "primary_key", 1); $self->add_table_column($tab, $name, $opts, "unique", 1); } else { die "Unknown table desc $name $opts"; } ; $tab }
Truth table0%0/3
A ||= B
A= $self->{"tables"}{$name}B= do { push @{$$self{"schemas"};}, my YATT::DBSchema::Table $tab = $self->Table->new; $tab->{"cf_name"} = $name; if (@columns) { $tab->{"raw_create"} = $opts; my $fields = $tab->fields_hash; foreach my $desc (@columns) { if (ref(my $kw = $desc->[0])) { if ($$fields{my $fname = "chk_$$kw"}) { push @{$$tab{$fname};}, [@{$desc;}[1 .. $desc->$#*]]; } else { croak("Invalid column constraint $kw for table $name"); } ; } else { () = @$desc; $self->add_table_column($tab, $col, $type, map({if (/^-(\w+)/) { $1, 1; } else { $_; } ;} @desc)); } ; } ; } elsif (not ref $opts) { $self->add_table_column($tab, $name . 'no', "integer", "primary_key", 1); $self->add_table_column($tab, $name, $opts, "unique", 1); } else { die "Unknown table desc $name $opts"; } ; $tab }
ABresult
1X1
011
000
263 0 push @{$self->{schemas}}
264 , my Table $tab = $self->Table->new;
265  
266 0 $tab->{cf_name} = $name;
267 0 ▶ if (@columns) {
Branch: 2 branches
BranchTrueFalseTotal
if (@columns) { } 0 0 0
elsif (not ref $opts) { } 0 0 0
268 # XXX: 拡張の余地あり
269 0 $tab->{raw_create} = $opts;
270 0 my $fields = $tab->fields_hash;
271 0 foreach my $desc (@columns) {
272 0 ▶ if (ref (my $kw = $desc->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref(my $kw = $desc->[0])) { } 0 0 0
273 0 ▶ unless ($fields->{my $fname = "chk_$$kw"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$fields{my $fname = "chk_$$kw"}) { } 0 0 0
274 0 croak "Invalid column constraint $kw for table $name";
275 } else {
276 0 push @{$tab->{$fname}}, [@{$desc}[1 .. $#$desc]];
277 }
278 } else {
279 0 my ($col, $type, @desc) = @$desc;
280 $self->add_table_column($tab, $col, $type, map {
281 0 ▶ if (/^-(\w+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^-(\w+)/) { } 0 0 0
282 0 $1 => 1
283 } else {
284 0 $_
285 }
286 } @desc);
287 }
288 }
289 } elsif (not ref $opts) {
290 # $opts is used as column type.
291 # XXX: SQLite specific.
292 0 $self->add_table_column($tab, $name . 'no', 'integer'
293 , primary_key => 1);
294 0 $self->add_table_column($tab, $name, $opts
295 , unique => 1);
296 } else {
297 0 die "Unknown table desc $name $opts";
298 }
299 0 $tab;
300 };
301 }
302  
303 0 ▶sub add_table_column {
Subroutine add_table_column: not called CC=5 SCAR=34.0
Pod undocumented
304 0 (my MY $self, my Table $tab, my ($colName, $type, @opts)) = @_;
305 0 ▶ if ($tab->{colNames}{$colName}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tab->{"colNames"}{$colName}) 0 0 0
306 0 croak "Conflicting column name $colName for table $tab->{cf_name}";
307 }
308 0 push @{$tab->{Column}}, my Column $col = $self->Column->new(@opts);
309 0 $tab->{colNames}{$colName} = $col->{colnum} = @{$tab->{Column}};
310  
311 $col->{cf_hidden} = ($colName =~ s/^-//
312 0 ▶ || $col->{cf_auto_increment});
Condition0%0/3
$colName =~ s/^-// || $col->{'cf_auto_increment'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$colName =~ s/^-// || $col->{'cf_auto_increment'}
$colName =~ s/^-// $col->{'cf_auto_increment'}
Truth table0%0/3
A || B
A= $colName =~ s/^-//B= $col->{'cf_auto_increment'}
ABresult
1X1
011
000
313 0 $col->{cf_name} = $colName;
314 # if ref $type, else
315 0 $col->{cf_type} = do {
316 0 ▶ if (ref $type) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $type) { } 0 0 0
317 0 $col->{cf_encoded_by} = $self->add_table(@$type);
318 # XXX: SQLite specific.
319 0 'int'
320 } else {
321 0 $type
322 }
323 };
324 0 ▶ if ($col->{cf_primary_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col->{"cf_primary_key"}) 0 0 0
325 0 $tab->{pk} = $col;
326 }
327 # XXX: Validation: name/option conflicts and others.
328 0 $col;
329 }
330  
331 #========================================
332  
333 0 ▶sub sql_create {
Subroutine sql_create: not called CC=1 SCAR=6.9
Pod undocumented
334 0 (my MY $schema, my %opts) = @_;
335 0 $schema->foreach_tables_do('sql_create_table', \%opts)
336 }
337  
338 0 ▶sub sql_create_table {
Subroutine sql_create_table: not called CC=10 SCAR=47.0
Pod undocumented
339 0 (my MY $schema, my Table $tab, my $opts) = @_;
340 0 my (@cols, @indices);
341 0 ▶ my $dbtype = $opts->{dbtype} || $schema->dbtype;
Condition0%0/3
$opts->{'dbtype'} || $schema->dbtype
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{'dbtype'} || $schema->dbtype
$opts->{'dbtype'} $schema->dbtype
Truth table0%0/3
A || B
A= $opts->{'dbtype'}B= $schema->dbtype
ABresult
1X1
011
000
342 0 ▶ my $sub = $schema->can($dbtype.'_sql_create_column')
Condition0%0/3
$schema->can($dbtype . '_sql_create_column') || $schema->can('sql_create_column')
l!l&&r!l&&!r
000
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 table0%0/3
A || B
A= $schema->can($dbtype . '_sql_create_column')B= $schema->can('sql_create_column')
ABresult
1X1
011
000
343 || $schema->can('sql_create_column');
344 0 foreach my Column $col (@{$tab->{Column}}) {
345 0 push @cols, $sub->($schema, $tab, $col, $opts);
346 0 ▶ push @indices, $col if $col->{cf_indexed};
Branch: 1 branch
BranchTrueFalseTotal
if $col->{"cf_indexed"} 0 0 0
347 }
348 0 ▶ foreach my $constraint (map {$_ ? @$_ : ()} $tab->{chk_unique}) {
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 0 0 0
349 0 push @cols, sprintf q{unique(%s)}, join(", ", @$constraint);
350 }
351  
352 # XXX: SQLite specific.
353 push my @create
354 , sprintf qq{CREATE TABLE %s\n(%s)}, $tab->{cf_name}
355 0 , join "\n, ", @cols;
356  
357 0 foreach my Column $ix (@indices) {
358 push @create
359 , sprintf q{CREATE INDEX %1$s_%2$s on %1$s(%2$s)}
360 0 , $tab->{cf_name}, $ix->{cf_name};
361 }
362  
363 # insert が有っても、構わない。
364 0 ▶ push @create, map {$_ ? @$_ : ()} $tab->{raw_create};
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 0 0 0
365  
366 0 ▶ wantarray ? @create : join(";\n", @create);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
367 }
368  
369 # XXX: text => varchar(80)
370 0 ▶sub map_coltype {
Subroutine map_coltype: not called CC=1 SCAR=6.9
Pod undocumented
371 0 (my MY $schema, my $typeName) = @_;
372 }
373  
374 0 ▶sub sql_create_column {
Subroutine sql_create_column: not called CC=4 SCAR=30.0
Pod undocumented
375 0 (my MY $schema, my Table $tab, my Column $col, my $opts) = @_;
376 join(" ", $col->{cf_name}
377 , $col->{cf_type}
378 , ($col->{cf_primary_key} ? "primary key" : ())
379 , ($col->{cf_unique} ? "unique" : ())
380 0 ▶ , ($col->{cf_auto_increment} ? "auto_increment" : ()));
Branch: 3 branches
BranchTrueFalseTotal
$col->{'cf_primary_key'} ? : 0 0 0
$col->{'cf_unique'} ? : 0 0 0
$col->{'cf_auto_increment'} ? : 0 0 0
381 }
382  
383 0 ▶sub sqlite_sql_create_column {
Subroutine sqlite_sql_create_column: not called CC=3 SCAR=24.8
Pod undocumented
384 0 (my MY $schema, my Table $tab, my Column $col, my $opts) = @_;
385 0 ▶ if ($col->{cf_type} =~ /^int/i && $col->{cf_primary_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col->{'cf_type'} =~ /^int/i and $col->{'cf_primary_key'}) { } 0 0 0
Condition0%0/3
$col->{"cf_type"} =~ /^int/i and $col->{"cf_primary_key"}
!ll&&!rl&&r
000
MC/DC0%0/2
$col->{"cf_type"} =~ /^int/i and $col->{"cf_primary_key"}
$col->{"cf_type"} =~ /^int/i $col->{"cf_primary_key"}
Truth table0%0/3
A and B
A= $col->{"cf_type"} =~ /^int/iB= $col->{"cf_primary_key"}
ABresult
0X0
100
111
386 0 "$col->{cf_name} integer primary key"
387 } else {
388 0 $schema->sql_create_column($tab, $col, $opts);
389 }
390 }
391  
392 0 ▶sub sql_drop {
Subroutine sql_drop: not called CC=1 SCAR=6.9
Pod undocumented
393 shift->foreach_tables_do
394 0 ▶ (sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
395 0 (my Table $tab) = @_;
396 0 qq{drop table $tab->{cf_name}};
397 })
398 0 }
399  
400 0 ▶sub foreach_tables_do {
Subroutine foreach_tables_do: not called CC=5 SCAR=34.0
Pod undocumented
401 0 (my MY $self, my $method, my $opts) = @_;
402 0 ▶ my $code = ref $method ? $method : sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
403 0 $self->$method(@_);
404 0 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
ref $method ? : 0 0 0
405 0 my @result;
406 0 my $wantarray = wantarray;
407 0 foreach my Table $tab (@{$self->{schemas}}) {
408 push @result, map {
409 0 ▶ $wantarray ? $_ . "\n" : $_
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
410 } $code->($tab, $opts);
411 }
412 0 ▶ wantarray ? @result : join(";\n", @result);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
413 }
414  
415 #========================================
416  
417 0 ▶sub sql_insert {
Subroutine sql_insert: not called CC=8 SCAR=42.8
Pod undocumented
418 0 (my MY $schema, my ($tabName, @fields)) = @_;
419 0 ▶ my Table $tab = $schema->{tables}{$tabName}
Branch: 1 branch
BranchTrueFalseTotal
my YATT::DBSchema::Table $tab = $schema->{"tables"}{$tabName} or croak("No such table: $tabName") 0 0 0
420 or croak "No such table: $tabName";
421 0 my (@insNames, @insEncs);
422 0 ▶ foreach my Column $col (@fields ? (map {
Branch: 1 branch
BranchTrueFalseTotal
@fields ? : 0 0 0
423 0 ▶ unless (my $colno = $tab->{colNames}{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $colno = $tab->{'colNames'}{$_}) { } 0 0 0
424 0 die "No such column $_ in $tabName\n";
425 } else {
426 0 $tab->{Column}[$colno - 1];
427 }
428 0 } @fields) : @{$tab->{Column}}) {
429 0 ▶ push @insNames, $col->{cf_name} unless $col->{cf_hidden};
Branch: 1 branch
BranchTrueFalseTotal
unless $col->{"cf_hidden"} 0 0 0
430 0 ▶ if (my Table $encTab = $col->{cf_encoded_by}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my YATT::DBSchema::Table $encTab = $col->{"cf_encoded_by"}) 0 0 0
431 0 push @insEncs, [$#insNames => $encTab->{cf_name}];
432 }
433 }
434  
435 0 my $sql = <<END;
436 0 INSERT INTO $tabName(@{[join ", ", @insNames]})
437 0 values(@{[join ", ", map {q|?|} @insNames]})
438 END
439  
440 0 ▶ wantarray ? ($sql, @insEncs) : $sql;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
441 }
442  
443 0 ▶sub to_insert {
Subroutine to_insert: not called CC=5 SCAR=34.0
Pod undocumented
444 0 (my MY $schema, my ($tabName, $fields)) = @_;
445 0 my $dbh = $schema->dbh;
446 my ($sql, @insEncs) = $schema->sql_insert
447 0 ($tabName, do {
448 0 ▶ unless ($fields) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fields) { } 0 0 0
if (do { ref $fields }) { } 0 0 0
449 ()
450 0 } elsif (ref $fields) {
451 0 @$fields
452 } else {
453 0 $fields
454 }
455 });
456 0 ▶ print STDERR "$sql\n" if $schema->{cf_verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{"cf_verbose"} 0 0 0
457 0 my $sth = $dbh->prepare($sql);
458 # ここで encode 用の sql/sth も生成せよと?
459 0 my @encoder;
460 0 foreach my $item (@insEncs) {
461 0 my ($i, $table) = @$item;
462 0 push @encoder, [$schema->to_encode($table, $dbh), $i];
463 }
464 0 my $rowid = $schema->{tables}{$tabName}->rowid_spec($schema);
465 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
466 0 my (@values) = @_;
467 0 foreach my $enc (@encoder) {
468 0 $enc->[0]->(\@values, $enc->[1]);
469 }
470 0 $sth->execute(@values);
471 0 $dbh->last_insert_id(undef, undef, $tabName, $rowid);
472 }
473 0 }
474  
475 0 ▶sub to_encode {
Subroutine to_encode: not called CC=5 SCAR=34.0
Pod undocumented
476 0 (my MY $schema, my ($encDesc, $dbh)) = @_;
477 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
478 0 ▶ my ($table, $column) = ref $encDesc ? @$encDesc : ($encDesc, $encDesc);
Branch: 1 branch
BranchTrueFalseTotal
ref $encDesc ? : 0 0 0
479 0 my Table $tab = $schema->{tables}{$table};
480 0 my $rowid = $tab->rowid_spec($schema);
481 0 my $check_sql = <<END;
482 select $rowid from $table where $column = ?
483 END
484 0 ▶ print STDERR "$check_sql\n" if $schema->{cf_verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{"cf_verbose"} 0 0 0
485  
486 0 my $ins_sql = <<END;
487 INSERT INTO $table($column) values(?)
488 END
489 0 ▶ print STDERR "$ins_sql\n" if $schema->{cf_verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{"cf_verbose"} 0 0 0
490  
491 # XXX: sth にまでするべきか。prepare_cached 廃止案。
492 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
493 0 my ($list, $nth) = @_;
494 0 my ($rowid) = do {
495 0 my $check = $dbh->prepare_cached($check_sql);
496 0 $dbh->selectrow_array($check, {}, $list->[$nth]);
497 };
498 0 ▶ unless (defined $rowid) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rowid) 0 0 0
499 0 my $ins = $dbh->prepare_cached($ins_sql, undef, 1);
500 0 $ins->execute($list->[$nth]);
501 0 $rowid = $dbh->last_insert_id(undef, undef, $table, $rowid);
502 }
503 0 $list->[$nth] = $rowid;
504 }
505 0 }
506  
507 #========================================
508  
509 0 ▶sub sql {
Subroutine sql: not called CC=1 SCAR=6.9
Pod undocumented
510 0 (my MY $self, my ($mode, $table)) = splice @_, 0, 3;
511 0 unshift @_, $self->parse_params(\@_);
512 0 $self->can("sql_${mode}")->($self, $table, @_);
513 }
514  
515 # XXX: explain を。 cf_explain で?
516 0 ▶sub cmd_select {
Subroutine cmd_select: not called CC=7 SCAR=40.3
Pod undocumented
517 0 my MY $self = shift;
518 0 $self->parse_opts(\@_, \ my %opts);
519 0 my $table = shift;
520 0 $self->parse_opts(\@_, \ %opts);
521 0 ▶ $self->configure(%opts) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 0 0 0
522 0 $self->parse_params(\@_, \ my %param);
523 0 my ($sth, $bind) = do {
524 0 ▶ if (my $sub = $self->can("select_$table")) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $sub = $self->can("select_$table")) { } 0 0 0
elsif ($sub = $self->can("sql_select_$table")) { } 0 0 0
525 # XXX: select_zzz は execute してはいけない
526 0 $sub->($self, \%param, @_);
527 } elsif ($sub = $self->can("sql_select_$table")) {
528 0 $self->dbh->prepare($sub->($self, \%param));
529 } else {
530 0 $self->prepare_select($table, \@_, %param);
531 }
532 };
533 0 ▶ $sth->execute($bind ? @$bind : @_);
Branch: 1 branch
BranchTrueFalseTotal
$bind ? : 0 0 0
534 0 my $null = $self->NULL;
535 0 my $format = $self->can('tsv_with_null');
536 0 ▶ print $format->($null, @{$sth->{NAME}}) unless $self->{cf_no_header};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cf_no_header"} 0 0 0
537 0 ▶ while (my (@res) = $sth->fetchrow_array) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@res) = $sth->fetchrow_array) 0 0 0
538 0 print $format->($null, @res);
539 }
540 }
541  
542 0 ▶sub select {
Subroutine select: not called CC=10 SCAR=47.0
Pod undocumented
543 0 (my MY $schema, my ($tabName, $columns, %param)) = @_;
544  
545 0 my $is_text = delete $param{text};
546 0 ▶ my $separator = delete $param{separator} || "\t";
Condition0%0/2
delete $param{'separator'} || "\t"
l!l
00
MC/DC0%0/1
delete $param{'separator'} || "\t"
delete $param{'separator'}
Truth table0%0/2
A
A= delete $param{'separator'}
Aresult
11
00
547 0 ▶ ($is_text, $separator) = (1, "\t") if delete $param{tsv};
Branch: 1 branch
BranchTrueFalseTotal
if delete $param{"tsv"} 0 0 0
548  
549 0 my (@fetch) = grep {delete $param{$_}} qw(hashref arrayref array);
550 0 ▶ die "Conflict! @fetch" if @fetch > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @fetch > 1 0 0 0
551  
552 0 my ($sth, $bind) = $schema->prepare_select($tabName, $columns, %param);
553  
554 0 ▶ if ($is_text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_text) { } 0 0 0
555 # Debugging aid.
556 0 my $null = $schema->NULL;
557 0 my $header; $header = $schema->format_line($sth->{NAME}, $separator, $null)
558 0 ▶ if $schema->{cf_no_header};
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{"cf_no_header"} 0 0 0
559 0 ▶ my $res = $sth->fetchall_arrayref
Branch: 1 branch
BranchTrueFalseTotal
my $res = $sth->fetchall_arrayref or return 0 0 0
560 or return;
561 join("", defined $header ? $header : ()
562 0 ▶ , map { $schema->format_line($_, $separator, $null) } @$res)
Branch: 1 branch
BranchTrueFalseTotal
defined $header ? : 0 0 0
563 } else {
564 0 ▶ my $method = $fetch[0] || 'arrayref';
Condition0%0/2
$fetch[0] || 'arrayref'
l!l
00
MC/DC0%0/1
$fetch[0] || 'arrayref'
$fetch[0]
Truth table0%0/2
A
A= $fetch[0]
Aresult
11
00
565 0 ▶ $sth->execute($bind ? @$bind : ());
Branch: 1 branch
BranchTrueFalseTotal
$bind ? : 0 0 0
566 0 $sth->can("fetchrow_$method")->($sth);
567 }
568 }
569  
570 # XXX: to_selectrow/selectall に分ければいいかも?
571 0 ▶sub to_select {
Subroutine to_select: not called CC=5 SCAR=34.0
Pod undocumented
572 0 (my MY $schema, my ($tabName, $columns, %params)) = @_;
573 0 my $type = do {
574 0 my (@fetch) = grep {delete $params{$_}} qw(hashref arrayref array);
575 0 ▶ die "Conflict! @fetch" if @fetch > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @fetch > 1 0 0 0
576 0 ▶ $fetch[0] || 'arrayref';
Condition0%0/2
$fetch[0] || 'arrayref'
l!l
00
MC/DC0%0/1
$fetch[0] || 'arrayref'
$fetch[0]
Truth table0%0/2
A
A= $fetch[0]
Aresult
11
00
577 };
578 0 my ($sth, $bind) = $schema->prepare_select($tabName, $columns, %params);
579 0 ▶ my $sub = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
580 0 ▶ $sth->execute($bind ? @$bind : @_);
Branch: 1 branch
BranchTrueFalseTotal
$bind ? : 0 0 0
581 0 ▶ my $method = wantarray ? "fetchall_$type" : "fetchrow_$type";
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
582 0 $sth->$method;
583 0 };
584 0 ▶ wantarray ? ($sub, ($bind ? $bind : ())) : $sub;
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 0 0 0
$bind ? : 0 0 0
585 }
586  
587 # 後は fetchrow するだけ、の sth を返す。
588 0 ▶sub to_fetch {
Subroutine to_fetch: not called CC=2 SCAR=17.9
Pod undocumented
589 0 (my MY $schema, my ($tabName, $columns, %params)) = @_;
590 0 my ($sth, $bind) = $schema->prepare_select($tabName, $columns, %params);
591 0 ▶ $sth->execute($bind ? @$bind : ());
Branch: 1 branch
BranchTrueFalseTotal
$bind ? : 0 0 0
592 0 $sth;
593 }
594  
595 # $sth 返しなのは、$sth->{NAME} を取りたいから。でも、単純なケースでは不便よね。
596 0 ▶sub prepare_select {
Subroutine prepare_select: not called CC=4 SCAR=30.0
Pod undocumented
597 0 (my MY $schema, my ($tabName, $columns, %params)) = @_;
598 0 ▶ my ($sql, $bind) = $schema->sql_select($tabName, \%params
Branch: 1 branch
BranchTrueFalseTotal
ref $columns ? : 0 0 0
599 , ref $columns ? @$columns : $columns);
600 0 my $sth = $schema->dbh->prepare($sql);
601 0 ▶ wantarray ? ($sth, ($bind ? $bind : ())) : $sth;
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 0 0 0
$bind ? : 0 0 0
602 }
603  
604 0 ▶sub sql_decode {
Subroutine sql_decode: not called CC=9 SCAR=45.0
Pod undocumented
605 0 (my MY $schema, my Table $tab
606 , my ($selJoins, $depth, $alias, $until)) = @_;
607 0 ▶ $depth = 0 unless defined $depth;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $depth 0 0 0
608 0 ▶ $alias ||= $tab->{cf_name};
Condition0%0/3
$alias ||= $tab->{"cf_name"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$alias ||= $tab->{"cf_name"}
$alias $tab->{"cf_name"}
Truth table0%0/3
A ||= B
A= $aliasB= $tab->{"cf_name"}
ABresult
1X1
011
000
609 0 my @selCols;
610 0 foreach my Column $col (@{$tab->{Column}}) {
611 0 my Table $enc = $col->{cf_encoded_by};
612 0 ▶ if ($depth || $enc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth or $enc) { } 0 0 0
Condition0%0/3
$depth or $enc
l!l&&r!l&&!r
000
MC/DC0%0/2
$depth or $enc
$depth $enc
Truth table0%0/3
A or B
A= $depthB= $enc
ABresult
1X1
011
000
613 # primary key は既に積まれている。
614 push @selCols, "$alias.$col->{cf_name}"
615 0 ▶ unless $col->{cf_primary_key};
Branch: 1 branch
BranchTrueFalseTotal
unless $col->{"cf_primary_key"} 0 0 0
616 } else {
617 0 push @selCols, $col->{cf_name};
618 }
619  
620 0 ▶ if ($enc && $depth < coalesce($until, 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enc and $depth < coalesce($until, 1)) 0 0 0
Condition0%0/3
$enc and $depth < coalesce($until, 1)
!ll&&!rl&&r
000
MC/DC0%0/2
$enc and $depth < coalesce($until, 1)
$enc $depth < coalesce($until, 1)
Truth table0%0/3
A and B
A= $encB= $depth < coalesce($until, 1)
ABresult
0X0
100
111
621 # alias と rowid と…
622 0 my $enc_alias = $col->{cf_name};
623 0 push @$selJoins, "\nLEFT JOIN $enc->{cf_name} $enc_alias"
624 . " on $alias.$col->{cf_name}"
625 . " = $enc_alias." . $enc->rowid_spec($schema);
626  
627 push @selCols, $schema->sql_decode
628 ($enc, $selJoins, $depth + 1, $col->{cf_name}
629 0 , $col->{cf_decode_depth});
630 }
631 }
632 0 @selCols;
633 }
634  
635 0 ▶sub sql_join {
Subroutine sql_join: not called CC=11 SCAR=48.8
Pod undocumented
636 0 (my MY $schema, my ($tabName, $params)) = @_;
637  
638 0 ▶ if (my $sub = $schema->can("sql_select_$tabName")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = $schema->can("sql_select_$tabName")) 0 0 0
639 0 return $sub->($schema, $params);
640 }
641  
642 0 ▶ my Table $tab = $schema->{tables}{$tabName}
Branch: 1 branch
BranchTrueFalseTotal
my YATT::DBSchema::Table $tab = $schema->{"tables"}{$tabName} or croak("No such table: $tabName") 0 0 0
643 or croak "No such table: $tabName";
644  
645 0 my @selJoins = $tab->{cf_name};
646 0 my @selCols = $schema->sql_decode($tab, \@selJoins);
647  
648 0 my (@appendix, @bind);
649 0 ▶ if (my $where = delete $params->{where}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $where = delete $params->{"where"}) 0 0 0
650 0 push @appendix, do {
651 0 ▶ if (ref $where) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $where) { } 0 0 0
652 0 require SQL::Abstract;
653 0 (my $stmt, @bind) = SQL::Abstract->new->where($where);
654 0 $stmt;
655 } else {
656 0 $where;
657 }
658 };
659 }
660  
661 {
662 0 ▶ if ($params->{offset} and not $params->{limit}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"offset"} and not $params->{"limit"}) 0 0 0
Condition0%0/3
$params->{"offset"} and not $params->{"limit"}
!ll&&!rl&&r
000
MC/DC0%0/2
$params->{"offset"} and not $params->{"limit"}
$params->{"offset"} not $params->{"limit"}
Truth table0%0/3
A and not(B)
A= $params->{"offset"}B= not $params->{"limit"}
ABresult
0X0
100
111
663 0 die "offset needs limit!";
664 }
665  
666 0 foreach my $kw (qw(group_by order_by limit offset)) {
667 0 ▶ if (my $val = delete $params->{$kw}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $val = delete $params->{$kw}) 0 0 0
668 0 push @appendix, join(" ", map(do {s/_/ /; $_}, uc($kw)), $val);
669 }
670 }
671  
672 die "Unknown param(s) for select $tabName: "
673 0 ▶ , join(", ", map {"$_=" . $params->{$_}} keys %$params) if %$params;
Branch: 1 branch
BranchTrueFalseTotal
if %$params 0 0 0
674 }
675  
676 0 ▶ (\@selCols, \@selJoins, \@appendix, @bind ? \@bind : ());
Branch: 1 branch
BranchTrueFalseTotal
@bind ? : 0 0 0
677 }
678  
679 0 ▶sub sql_select {
Subroutine sql_select: not called CC=7 SCAR=40.3
Pod undocumented
680 0 (my MY $schema, my ($tabName, $params)) = splice @_, 0, 3;
681  
682 0 my $raw = delete $params->{raw};
683 0 my $colExpr = do {
684 0 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 0 0 0
elsif ($raw) { } 0 0 0
685 0 join(", ", @_);
686 } elsif ($raw) {
687 0 '*';
688 }
689 };
690  
691 0 my ($selCols, $selJoins, $where, $bind)
692 = $schema->sql_join($tabName, $params);
693  
694 0 ▶ my $sql = join("\n", sprintf(q{SELECT %s FROM %s}
Branch: 1 branch
BranchTrueFalseTotal
$raw ? : 0 0 0
Condition0%0/3
$colExpr || join(', ', @$selCols)
l!l&&r!l&&!r
000
MC/DC0%0/2
$colExpr || join(', ', @$selCols)
$colExpr join(', ', @$selCols)
Truth table0%0/3
A || B
A= $colExprB= join(', ', @$selCols)
ABresult
1X1
011
000
695 , $colExpr || join(", ", @$selCols)
696 , $raw ? $tabName : join("", @$selJoins))
697 , @$where);
698  
699 0 ▶ wantarray ? ($sql, (defined $bind ? $bind : ())) : $sql;
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 0 0 0
defined $bind ? : 0 0 0
700 }
701  
702 #----------------------------------------
703  
704 0 ▶sub indexed {
Subroutine indexed: not called CC=2 SCAR=17.9
Pod undocumented
705 0 (my MY $schema, my ($tabName, $colName, $value, $params)) = @_;
706 0 ▶ my $dbh = delete $params->{dbh} || $schema->dbh;
Condition0%0/3
delete $params->{'dbh'} || $schema->dbh
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $params->{'dbh'} || $schema->dbh
delete $params->{'dbh'} $schema->dbh
Truth table0%0/3
A || B
A= delete $params->{'dbh'}B= $schema->dbh
ABresult
1X1
011
000
707 0 my $sql = $schema->sql_indexed($tabName, $colName);
708 0 $dbh->selectrow_hashref($sql, undef, $value);
709 }
710  
711 0 ▶sub sql_indexed {
Subroutine sql_indexed: not called CC=1 SCAR=6.9
Pod undocumented
712 0 (my MY $schema, my ($tabName, $colName)) = @_;
713 0 <<"END";
714 select _rowid_, * from $tabName where $colName = ?
715 END
716 }
717  
718 0 ▶sub format_line {
Subroutine format_line: not called CC=3 SCAR=24.8
Pod undocumented
719 0 (my MY $schema, my ($rec, $separator, $null)) = @_;
720 join($separator, map {
721 0 ▶ unless (defined $_) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $_) { } 0 0 0
if (do { (my $val = $_) =~ s/[\t\n]/ /g }) { } 0 0 0
722 0 $null
723 } elsif ((my $val = $_) =~ s/[\t\n]/ /g) {
724 0 $val
725 } else {
726 0 $_
727 }
728 } @$rec). "\n";
729 }
730  
731 #========================================
732  
733 0 ▶sub to_update {
Subroutine to_update: not called CC=2 SCAR=17.9
Pod undocumented
734 0 (my MY $schema, my ($tabName, $colName)) = @_;
735 0 my $sql = $schema->sql_update($tabName, $colName);
736 0 ▶ print STDERR "$sql\n" if $schema->{cf_verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{"cf_verbose"} 0 0 0
737 0 my $sth = $schema->dbh->prepare($sql);
738 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
739 0 my ($colValue, $rowId) = @_;
740 0 $sth->execute($colValue, $rowId);
741 }
742 0 }
743  
744 0 ▶sub sql_update {
Subroutine sql_update: not called CC=1 SCAR=6.9
Pod undocumented
745 0 (my MY $schema, my ($tabName, $colName)) = @_;
746 0 my $rowid = $schema->{tables}{$tabName}->rowid_spec($schema);
747 0 "UPDATE $tabName SET $colName = ? WHERE $rowid = ?";
748 }
749  
750 ########################################
751  
752 0 ▶sub tsv_with_null {
Subroutine tsv_with_null: not called CC=2 SCAR=17.9
Pod undocumented
753 0 my $null = shift;
754 0 ▶ join("\t", map {defined $_ ? $_ : $null} @_). "\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
755 }
756  
757  
758 ########################################
759  
760 0 ▶sub run {
Subroutine run: not called CC=12 SCAR=50.5
Pod undocumented
761 0 my $pack = shift;
762 0 ▶ $pack->cmd_help unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
763 0 my MY $obj = $pack->new(MY->parse_opts(\@_));
764 0 ▶ my $cmd = shift || "help";
Condition0%0/2
shift() || 'help'
l!l
00
MC/DC0%0/1
shift() || 'help'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
765 0 $obj->configure(MY->parse_opts(\@_));
766 0 my $method = "cmd_$cmd";
767 0 ▶ if (my $sub = $obj->can("cmd_$cmd")) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $sub = $obj->can("cmd_$cmd")) { } 0 0 0
elsif ($sub = $obj->can($cmd)) { } 0 0 0
768 0 $sub->($obj, @_);
769 } elsif ($sub = $obj->can($cmd)) {
770 0 my @res = $sub->($obj, @_);
771 0 ▶ exit 1 unless @res;
Branch: 1 branch
BranchTrueFalseTotal
unless @res 0 0 0
772 0 ▶ unless (@res == 1 and defined $res[0] and $res[0] eq "1") {
Branch: 1 branch
BranchTrueFalseTotal
unless (@res == 1 and defined $res[0] and $res[0] eq 1) 0 0 0
Condition0%0/6
@res == 1 and defined $res[0] and $res[0] eq 1
!ll&&!rl&&r
000
@res == 1 and defined $res[0]
!ll&&!rl&&r
000
MC/DC0%0/3
@res == 1 and defined $res[0] and $res[0] eq 1
@res == 1 defined $res[0] $res[0] eq 1
Truth table0%0/4
A and B and C
A= @res == 1B= defined $res[0]C= $res[0] eq 1
ABCresult
0XX0
10X0
1100
1111
773 0 ▶ if (grep {defined $_ && ref $_} @res) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {defined $_ and ref $_;} @res) { } 0 0 0
Condition0%0/3
defined $_ && ref $_
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && ref $_
defined $_ ref $_
Truth table0%0/3
A && B
A= defined $_B= ref $_
ABresult
0X0
100
111
774 0 require Data::Dumper;
775 print Data::Dumper->new([$_])->Indent(0)->Terse(1)->Dump
776 0 , "\n" for @res;
777 } else {
778 0 print join("\n", @res), "\n";
779 }
780 }
781 } else {
782 0 die "No such method $cmd for $pack\n";
783 }
784 0 $obj->DESTROY; # To make sure committed.
785 }
786  
787 0 ▶sub cmd_help {
Subroutine cmd_help: not called CC=2 SCAR=14.0
Pod undocumented
788 0 my ($self) = @_;
789 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
790 0 my $stash = do {
791 0 my $pkg = $pack . '::';
792 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
793 0 \%{$pkg};
794 };
795 0 my @methods = sort grep s/^cmd_//, keys %$stash;
796 0 die "Usage: @{[basename($0)]} method args..\n "
797 . join("\n ", @methods) . "\n";
798 }
799  
800 #========================================
801  
802 0 ▶sub ymd_hms {
Subroutine ymd_hms: not called CC=2 SCAR=17.9
Pod undocumented
803 0 my ($pack, $time, $as_utc) = @_;
804 my ($S, $M, $H, $d, $m, $y) = map {
805 0 ▶ $as_utc ? gmtime($_) : localtime($_)
Branch: 1 branch
BranchTrueFalseTotal
$as_utc ? : 0 0 0
806 } $time;
807 0 sprintf q{%04d-%02d-%02d %02d:%02d:%02d}, 1900+$y, $m+1, $d, $H, $M, $S;
808 }
809  
810 1 1;
811 # -for_dbic
812 # -for_sqlengine
813 # -for_sqlt
814  
« web/cgi-bin/yatt.lib/YATT/Toplevel/CGI.pm ↑ Summary blib/lib/YATT/Toplevel/Server.pm »