KiokuDB/Backend/DBI.pm

filter: Statementstmt 91.9% 444 / 483 Branchbran 69.5% 96 / 138 Conditioncond 48.8% 22 / 45 MC/DCmcdc 22.2% 6 / 27 Subroutinesub 90.2% 92 / 102 Podpod 5.5% 2 / 36 totaltotal 79.6% 662 / 831 CC 106 SCAR 17.4
CC
106
Cov
84%
new_garbage_collector
30.0
get
16.9
__ANON__
12.0
CRAP 5.7
↑ Summary DBIx/Class/Schema/KiokuDB.pm »
1 package KiokuDB::Backend::DBI;
2 6 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 $KiokuDB::Backend::DBI::AUTHORITY = 'cpan:NUFFIN';
4 }
5 6 $KiokuDB::Backend::DBI::VERSION = '1.23';
6 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 # ABSTRACT: DBI backend for KiokuDB
8  
9 6 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 ▶use MooseX::Types 0.08 -declare => [qw(ValidColumnName SchemaProto)];
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 6 ▶use MooseX::Types::Moose qw(ArrayRef HashRef Str Defined);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 6 ▶use Moose::Util::TypeConstraints qw(enum);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 6 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶use Data::Stream::Bulk::DBI 0.07;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 6 ▶use SQL::Abstract;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 6 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 6 ▶use Scalar::Util qw(weaken refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 6 ▶use List::MoreUtils qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 6 ▶use Class::Load qw(load_class);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 6 ▶use Search::GIN 0.07 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 6 ▶use KiokuDB 0.46 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27 6 ▶use KiokuDB::Backend::DBI::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 6 ▶use KiokuDB::TypeMap;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 6 ▶use KiokuDB::TypeMap::Entry::DBIC::Row;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 6 ▶use KiokuDB::TypeMap::Entry::DBIC::ResultSource;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 6 ▶use KiokuDB::TypeMap::Entry::DBIC::ResultSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 6 ▶use KiokuDB::TypeMap::Entry::DBIC::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 6 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 6 with qw(
37 KiokuDB::Backend
38 KiokuDB::Backend::Serialize::Delegate
39 KiokuDB::Backend::Role::Clear
40 KiokuDB::Backend::Role::TXN
41 KiokuDB::Backend::Role::Scan
42 KiokuDB::Backend::Role::Query::Simple
43 KiokuDB::Backend::Role::Query::GIN
44 KiokuDB::Backend::Role::Concurrency::POSIX
45 KiokuDB::Backend::Role::GC
46 Search::GIN::Extract::Delegate
47 );
48 # KiokuDB::Backend::Role::TXN::Nested is not supported by many DBs
49 # we don't really care though
50  
51 6 my @std_cols = qw(id class root tied);
52 6 my @reserved_cols = ( @std_cols, 'data' );
53 6 my %reserved_cols = ( map { $_ => 1 } @reserved_cols );
54  
55 119 ▶subtype ValidColumnName, as Str, where { not exists $reserved_cols{$_} };
Subroutine __ANON__: called CC=1 SCAR=0.0
56 63 ▶subtype SchemaProto, as Defined, where {
Subroutine __ANON__: called CC=4 SCAR=16.9
57 63 ▶ load_class($_) unless ref;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ 63 0 63
58 63 ▶ !ref($_) || blessed($_) and $_->isa("DBIx::Class::Schema::KiokuDB");
Branch: 1 branch
BranchTrueFalseTotal
!ref($_) || builtin::blessed($_) and $_->isa("DBIx::Class::Schema::KiokuDB") 63 0 63
Condition33%1/3
not ref $_ or builtin::blessed($_)
l!l&&r!l&&!r
6300
MC/DC0%0/2
not ref $_ or builtin::blessed($_)
not ref $_ builtin::blessed($_)
Truth table33%1/3
not(A) or B
A= not ref $_B= builtin::blessed($_)
ABresult
1X1
011
000
59 6 };
60  
61 61 ▶sub new_from_dsn {
Subroutine new_from_dsn: called CC=3 SCAR=13.3
Pod undocumented
62 61 my ( $self, $dsn, @args ) = @_;
63 0 ▶ @args = %{ $args[0] } if @args == 1 and ref $args[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if @args == 1 and ref $args[0] eq "HASH" 0 61 61
Condition33%1/3
@args == 1 and ref $args[0] eq "HASH"
!ll&&!rl&&r
6100
MC/DC0%0/2
@args == 1 and ref $args[0] eq "HASH"
@args == 1 ref $args[0] eq "HASH"
Truth table33%1/3
A and B
A= @args == 1B= ref $args[0] eq "HASH"
ABresult
0X0
100
111
64 61 $self->new( dsn => "dbi:$dsn", @args );
65 }
66  
67 63 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
68 63 my $self = shift;
69  
70 63 $self->schema; # connect early
71  
72 63 ▶ if ( $self->create ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->create) 61 2 63
73 61 $self->create_tables;
74 }
75 }
76  
77 6 has '+serializer' => ( default => "json" ); # to make dumps readable
78  
79 has json => (
80 isa => "Object",
81 is => "ro",
82 63 ▶ default => sub { JSON->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
83 6 );
84  
85 6 has create => (
86 isa => "Bool",
87 is => "ro",
88 default => 0,
89 );
90  
91 6 has 'dsn' => (
92 isa => "Str|CodeRef",
93 is => "ro",
94 );
95  
96 6 has [qw(user password)] => (
97 isa => "Str",
98 is => "ro",
99 );
100  
101 6 has dbi_attrs => (
102 isa => HashRef,
103 is => "ro",
104 );
105  
106 6 has mysql_strict => (
107 isa => "Bool",
108 is => "ro",
109 default => 1,
110 );
111  
112 6 has sqlite_sync_mode => (
113 isa => enum([qw(0 1 2 OFF NORMAL FULL off normal full)]),
114 is => "ro",
115 predicate => "has_sqlite_fsync_mode",
116 );
117  
118 6 has on_connect_call => (
119 isa => "ArrayRef",
120 is => "ro",
121 lazy_build => 1,
122 );
123  
124 62 ▶sub _build_on_connect_call {
Subroutine _build_on_connect_call: called CC=3 SCAR=11.2
125 62 my $self = shift;
126  
127 62 my @call;
128  
129 62 ▶ if ( $self->mysql_strict ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mysql_strict) 62 0 62
130 61 ▶ push @call, sub {
Subroutine __ANON__: called CC=2 SCAR=8.1
131 61 my $storage = shift;
132  
133 61 ▶ if ( $storage->can("connect_call_set_strict_mode") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($storage->can("connect_call_set_strict_mode")) 0 61 61
134 0 $storage->connect_call_set_strict_mode;
135 }
136 62 };
137 };
138  
139 62 ▶ if ( $self->has_sqlite_fsync_mode ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_sqlite_fsync_mode) 59 3 62
140 59 ▶ push @call, sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
141 59 my $storage = shift;
142  
143 59 ▶ if ( $storage->sqlt_type eq 'SQLite' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($storage->sqlt_type eq "SQLite") 59 0 59
144 59 ▶ $storage->dbh_do(sub { $_[1]->do("PRAGMA synchronous=" . $self->sqlite_sync_mode) });
Subroutine __ANON__: called CC=1 SCAR=0.0
145 }
146 59 };
147 }
148  
149 62 return \@call;
150 }
151  
152 6 has dbic_attrs => (
153 isa => "HashRef",
154 is => "ro",
155 lazy_build => 1,
156 );
157  
158 62 ▶sub _build_dbic_attrs {
Subroutine _build_dbic_attrs: called CC=1 SCAR=0.0
159 62 my $self = shift;
160  
161 return {
162 62 on_connect_call => $self->on_connect_call,
163 };
164 }
165  
166 6 has connect_info => (
167 isa => ArrayRef,
168 is => "ro",
169 lazy_build => 1,
170 );
171  
172 62 ▶sub _build_connect_info {
Subroutine _build_connect_info: called CC=1 SCAR=0.0
173 62 my $self = shift;
174  
175 62 return [ $self->dsn, $self->user, $self->password, $self->dbi_attrs, $self->dbic_attrs ];
176 }
177  
178 6 has schema => (
179 isa => "DBIx::Class::Schema",
180 is => "ro",
181 lazy_build => 1,
182 init_arg => "connected_schema",
183 handles => [qw(deploy kiokudb_entries_source_name)],
184 );
185  
186 6 has _schema_proto => (
187 isa => SchemaProto,
188 is => "ro",
189 init_arg => "schema",
190 default => "KiokuDB::Backend::DBI::Schema",
191 );
192  
193 6 has schema_hook => (
194 isa => "CodeRef|Str",
195 is => "ro",
196 predicate => "has_schema_hook",
197 );
198  
199 62 ▶sub _build_schema {
Subroutine _build_schema: called CC=3 SCAR=11.8
200 62 my $self = shift;
201  
202 62 my $schema = $self->_schema_proto->clone;
203  
204 62 ▶ unless ( $schema->kiokudb_entries_source_name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema->kiokudb_entries_source_name) 62 0 62
205 62 $schema->define_kiokudb_schema( extra_entries_columns => $self->columns );
206 }
207  
208 62 ▶ if ( $self->has_schema_hook ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_schema_hook) 0 62 62
209 0 my $h = $self->schema_hook;
210 0 $self->$h($schema);
211 }
212  
213 62 $schema->connect(@{ $self->connect_info });
214 }
215  
216 6 has storage => (
217 isa => "DBIx::Class::Storage::DBI",
218 is => "rw",
219 lazy_build => 1,
220 handles => [qw(dbh_do)],
221 );
222  
223 62 ▶sub _build_storage { shift->schema->storage }
Subroutine _build_storage: called CC=1 SCAR=0.0
224  
225 6 has for_update => (
226 isa => "Bool",
227 is => "ro",
228 default => 1,
229 );
230  
231 6 has _for_update => (
232 isa => "Bool",
233 is => "ro",
234 lazy_build => 1,
235 );
236  
237 52 ▶sub _build__for_update {
Subroutine _build__for_update: called CC=2 SCAR=8.1
238 52 my $self = shift;
239  
240 return (
241 52 ▶ $self->for_update
Condition33%1/3
$self->for_update && $self->storage->sqlt_type =~ /^(?:MySQL|Oracle|PostgreSQL)$/
!ll&&!rl&&r
0520
MC/DC0%0/2
$self->for_update && $self->storage->sqlt_type =~ /^(?:MySQL|Oracle|PostgreSQL)$/
$self->for_update $self->storage->sqlt_type =~ /^(?:MySQL|Oracle|PostgreSQL)$/
Truth table33%1/3
A && B
A= $self->for_updateB= $self->storage->sqlt_type =~ /^(?:MySQL|Oracle|PostgreSQL)$/
ABresult
0X0
100
111
242 and
243 $self->storage->sqlt_type =~ /^(?:MySQL|Oracle|PostgreSQL)$/
244 );
245 }
246  
247 has columns => (
248 isa => ArrayRef[ValidColumnName|HashRef],
249 is => "ro",
250 3 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
251 6 );
252  
253 6 has _columns => (
254 isa => HashRef,
255 is => "ro",
256 lazy_build => 1,
257 );
258  
259 53 ▶sub _build__columns {
Subroutine _build__columns: called CC=2 SCAR=7.0
260 53 my $self = shift;
261  
262 53 my $rs = $self->schema->source( $self->kiokudb_entries_source_name );
263  
264 53 my @user_cols = grep { not exists $reserved_cols{$_} } $rs->columns;
265  
266 53 ▶ return { map { $_ => $rs->column_info($_)->{extract} || undef } @user_cols };
Condition50%1/2
$rs->column_info($_)->{'extract'} || undef
l!l
099
MC/DC0%0/1
$rs->column_info($_)->{'extract'} || undef
$rs->column_info($_)->{'extract'}
Truth table50%1/2
A
A= $rs->column_info($_)->{'extract'}
Aresult
11
00
267 }
268  
269 6 has _ordered_columns => (
270 isa => "ArrayRef",
271 is => "ro",
272 lazy_build => 1,
273 );
274  
275 52 ▶sub _build__ordered_columns {
Subroutine _build__ordered_columns: called CC=1 SCAR=0.0
276 52 my $self = shift;
277 52 return [ @reserved_cols, sort keys %{ $self->_columns } ];
278 }
279  
280 6 has _column_order => (
281 isa => "HashRef",
282 is => "ro",
283 lazy_build => 1,
284 );
285  
286 0 ▶sub _build__column_order {
Subroutine _build__column_order: not called CC=1 SCAR=6.9
287 0 my $self = shift;
288  
289 0 my $cols = $self->_ordered_columns;
290 0 return { map { $cols->[$_] => $_ } 0 .. $#$cols }
291 }
292  
293 6 has '+extract' => (
294 required => 0,
295 );
296  
297 6 has sql_abstract => (
298 isa => "SQL::Abstract",
299 is => "ro",
300 lazy_build => 1,
301 );
302  
303 3 ▶sub _build_sql_abstract {
Subroutine _build_sql_abstract: called CC=1 SCAR=0.0
304 3 my $self = shift;
305  
306 3 SQL::Abstract->new;
307 }
308  
309 # use a Maybe so we can force undef in the builder
310 6 has batch_size => (
311 isa => "Maybe[Int]",
312 is => "ro",
313 lazy => 1,
314 builder => '_build_batch_size',
315 );
316  
317 52 ▶sub _build_batch_size {
Subroutine _build_batch_size: called CC=2 SCAR=7.6
318 52 my $self = shift;
319  
320 52 ▶ if ($self->storage->sqlt_type eq 'SQLite') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->storage->sqlt_type eq 'SQLite') { } 52 0 52
321 52 return 999;
322 } else {
323 0 return undef;
324 }
325 }
326  
327 0 ▶sub has_batch_size { defined shift->batch_size }
Subroutine has_batch_size: not called CC=1 SCAR=6.9
Pod undocumented
328  
329 62 ▶sub register_handle {
Subroutine register_handle: called CC=1 SCAR=0.0
Pod undocumented
330 62 my ( $self, $kiokudb ) = @_;
331  
332 62 $self->schema->_kiokudb_handle($kiokudb);
333 }
334  
335 52 ▶sub default_typemap {
Subroutine default_typemap: called CC=1 SCAR=0.0
Pod undocumented
336 52 KiokuDB::TypeMap->new(
337 isa_entries => {
338 # redirect to schema row
339 'DBIx::Class::Row' => KiokuDB::TypeMap::Entry::DBIC::Row->new,
340  
341 # actual serialization
342 'DBIx::Class::ResultSet' => KiokuDB::TypeMap::Entry::DBIC::ResultSet->new,
343  
344 # fake, the entries never get written to the db
345 'DBIx::Class::ResultSource' => KiokuDB::TypeMap::Entry::DBIC::ResultSource->new,
346 'DBIx::Class::Schema' => KiokuDB::TypeMap::Entry::DBIC::Schema->new,
347 },
348 );
349 }
350  
351 121 ▶sub insert {
Subroutine insert: called CC=7 SCAR=19.6
Pod undocumented
352 121 my ( $self, @entries ) = @_;
353  
354 121 ▶ return unless @entries;
Branch: 1 branch
BranchTrueFalseTotal
unless @entries 11 110 121
355  
356 110 my $g = $self->schema->txn_scope_guard;
357  
358 110 $self->insert_rows( $self->entries_to_rows(@entries) );
359  
360 # hopefully we're in a transaction, otherwise this totally sucks
361 108 ▶ if ( $self->extract ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->extract) 100 8 108
362 100 my %gin_index;
363  
364 100 foreach my $entry ( @entries ) {
365 3350 my $id = $entry->id;
366  
367 3350 ▶ if ( $entry->deleted || !$entry->has_object ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->deleted or not $entry->has_object) { } 2 3348 3350
Condition66%2/3
$entry->deleted or not $entry->has_object
l!l&&r!l&&!r
023348
MC/DC50%1/2
$entry->deleted or not $entry->has_object
$entry->deleted not $entry->has_object
Truth table66%2/3
A or not(B)
A= $entry->deletedB= not $entry->has_object
ABresult
1X1
011
000
368 2 $gin_index{$id} = [];
369 } else {
370 3348 ▶ my $d = $entry->backend_data || $entry->backend_data({});
Condition33%1/3
$entry->backend_data || $entry->backend_data({})
l!l&&r!l&&!r
033480
MC/DC0%0/2
$entry->backend_data || $entry->backend_data({})
$entry->backend_data $entry->backend_data({})
Truth table33%1/3
A || B
A= $entry->backend_dataB= $entry->backend_data({})
ABresult
1X1
011
000
371 3348 $gin_index{$id} = [ $self->extract_values( $entry->object, entry => $entry ) ];
372 }
373 }
374  
375 100 $self->update_index(\%gin_index);
376 }
377  
378 108 $g->commit;
379 }
380  
381 110 ▶sub entries_to_rows {
Subroutine entries_to_rows: called CC=7 SCAR=19.5
Pod undocumented
382 110 my ( $self, @entries ) = @_;
383  
384 110 my ( %insert, %update, @dbic );
385  
386 110 foreach my $t ( \%insert, \%update ) {
387 220 foreach my $col ( @{ $self->_ordered_columns } ) {
388 1504 $t->{$col} = [];
389 }
390 }
391  
392 110 foreach my $entry ( @entries ) {
393 3366 my $id = $entry->id;
394  
395 3366 ▶ if ( $id =~ /^dbic:schema/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($id =~ /^dbic:schema/) { } 2 3364 3366
elsif ($id =~ /^dbic:row:/) { } 2 3362 3364
396 2 next;
397 } elsif ( $id =~ /^dbic:row:/ ) {
398 2 push @dbic, $entry->data;
399 } else {
400 3362 ▶ my $targ = $entry->prev ? \%update : \%insert;
Branch: 1 branch
BranchTrueFalseTotal
$entry->prev ? : 58 3304 3362
401  
402 3362 my $row = $self->entry_to_row($entry, $targ);
403 }
404 }
405  
406 110 return \( %insert, %update, @dbic );
407 }
408  
409 3363 ▶sub entry_to_row {
Subroutine entry_to_row: called CC=9 SCAR=22.9
Pod undocumented
410 3363 my ( $self, $entry, $collector ) = @_;
411  
412 3363 for (qw(id class tied)) {
413 10089 push @{ $collector->{$_} }, $entry->$_;
414 }
415  
416 3363 ▶ push @{ $collector->{root} }, $entry->root ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$entry->root ? : 3248 115 3363
417  
418 3363 push @{ $collector->{data} }, $self->serialize($entry);
419  
420 3363 my $cols = $self->_columns;
421  
422 3363 foreach my $column ( keys %$cols ) {
423 6695 my $c = $collector->{$column};
424 6695 ▶ if ( my $extract = $cols->{$column} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $extract = $cols->{$column}) { } 0 6695 6695
elsif (ref(my $data = $entry->data) eq 'HASH') { } 6665 30 6695
425 0 ▶ if ( my $obj = $entry->object ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $obj = $entry->object) 0 0 0
426 0 push @$c, $obj->$extract($column);
427 0 next;
428 }
429 } elsif ( ref( my $data = $entry->data ) eq 'HASH' ) {
430 6665 ▶ if ( exists $data->{$column} and not ref( my $value = $data->{$column} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{$column} and not ref(my $value = $data->{$column})) 3387 3278 6665
Condition66%2/3
exists $data->{$column} and not ref(my $value = $data->{$column})
!ll&&!rl&&r
327803387
MC/DC50%1/2
exists $data->{$column} and not ref(my $value = $data->{$column})
exists $data->{$column} not ref(my $value = $data->{$column})
Truth table66%2/3
A and not(B)
A= exists $data->{$column}B= not ref(my $value = $data->{$column})
ABresult
0X0
100
111
431 3387 push @$c, $value;
432 3387 next;
433 }
434 }
435  
436 3308 push @$c, undef;
437 }
438 }
439  
440 110 ▶sub insert_rows {
Subroutine insert_rows: called CC=1 SCAR=0.0
Pod undocumented
441 110 my ( $self, $insert, $update, $dbic ) = @_;
442  
443 110 my $g = $self->schema->txn_scope_guard;
444  
445 110 ▶ $self->dbh_do(sub {
Subroutine __ANON__: called CC=11 SCAR=24.2
446 110 my ( $storage, $dbh ) = @_;
447  
448 110 ▶ if ( $self->extract ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->extract) 102 8 110
449 102 ▶ if ( my @ids = map { @{ $_->{id} || [] } } $insert, $update ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@ids) = map({@{$_->{"id"} or [];};} $insert, $update)) 102 0 102
Condition50%1/2
$_->{'id'} || []
l!l
2040
MC/DC0%0/1
$_->{'id'} || []
$_->{'id'}
Truth table50%1/2
A
A= $_->{'id'}
Aresult
11
00
450  
451 102 ▶ my $batch_size = $self->batch_size || scalar(@ids);
Condition50%1/2
$self->batch_size || scalar @ids
l!l
1020
MC/DC0%0/1
$self->batch_size || scalar @ids
$self->batch_size
Truth table50%1/2
A
A= $self->batch_size
Aresult
11
00
452  
453 102 my @ids_copy = @ids;
454 102 ▶ while ( my @batch_ids = splice @ids_copy, 0, $batch_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@batch_ids) = splice(@ids_copy, 0, $batch_size)) 105 102 207
455 105 my $del_gin_sth = $dbh->prepare_cached("DELETE FROM gin_index WHERE id IN (" . join(", ", ('?') x @batch_ids) . ")");
456  
457 105 $del_gin_sth->execute(@batch_ids);
458  
459 105 $del_gin_sth->finish;
460 }
461 }
462 }
463  
464 110 my $colinfo = $self->schema->source('entries')->columns_info;
465  
466 110 my %rows = ( insert => $insert, update => $update );
467  
468 110 foreach my $op (qw(insert update)) {
469 218 my $prepare = "prepare_$op";
470 218 my ( $sth, @cols ) = $self->$prepare($dbh);
471  
472 218 my $i = 1;
473  
474 218 foreach my $column_name (@cols) {
475 1490 my $attributes = {};
476  
477 1490 ▶ if ( exists $colinfo->{$column_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $colinfo->{$column_name}) 1490 0 1490
478 1490 my $dt = $colinfo->{$column_name}{data_type};
479 1490 $attributes = $self->storage->bind_attribute_by_data_type($dt);
480 }
481  
482 1490 $sth->bind_param_array( $i, $rows{$op}->{$column_name}, $attributes );
483  
484 1490 $i++;
485 }
486  
487 218 ▶ $sth->execute_array({ArrayTupleStatus => []}) or die;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute_array({"ArrayTupleStatus", []}) or die 216 0 216
488  
489 216 $sth->finish;
490 }
491  
492 108 $_->insert_or_update for @$dbic;
493 110 });
494  
495 108 $g->commit;
496 }
497  
498 350 ▶sub prepare_select {
Subroutine prepare_select: called CC=2 SCAR=7.2
Pod undocumented
499 350 my ( $self, $dbh, $stmt ) = @_;
500  
501 350 ▶ $dbh->prepare_cached($stmt . ( $self->_for_update ? " FOR UPDATE" : "" ), {}, 3); # 3 = don't use if still Active
Branch: 1 branch
BranchTrueFalseTotal
$self->_for_update ? : 0 350 350
502 }
503  
504 110 ▶sub prepare_insert {
Subroutine prepare_insert: called CC=1 SCAR=0.0
Pod undocumented
505 110 my ( $self, $dbh ) = @_;
506  
507 110 my @cols = @{ $self->_ordered_columns };
508  
509 110 my $ins = $dbh->prepare_cached("INSERT INTO entries (" . join(", ", @cols) . ") VALUES (" . join(", ", ('?') x @cols) . ")");
510  
511 110 return ( $ins, @cols );
512 }
513  
514 108 ▶sub prepare_update {
Subroutine prepare_update: called CC=1 SCAR=0.0
Pod undocumented
515 108 my ( $self, $dbh ) = @_;
516  
517 108 my ( $id, @cols ) = @{ $self->_ordered_columns };
518  
519 108 my $upd = $dbh->prepare_cached("UPDATE entries SET " . join(", ", map { "$_ = ?" } @cols) . " WHERE $id = ?");
520  
521 108 return ( $upd, @cols, $id );
522 }
523  
524 100 ▶sub update_index {
Subroutine update_index: called CC=1 SCAR=0.0
Pod undocumented
525 100 my ( $self, $entries ) = @_;
526  
527 100 ▶ $self->dbh_do(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
528 100 my ( $storage, $dbh ) = @_;
529  
530 100 my $i_sth = $dbh->prepare_cached("INSERT INTO gin_index (id, value) VALUES (?, ?)");
531  
532 100 foreach my $id ( keys %$entries ) {
533 my $rv = $i_sth->execute_array(
534 {ArrayTupleStatus => []},
535 $id,
536 3350 $entries->{$id},
537 );
538 }
539  
540 100 $i_sth->finish;
541 100 });
542 }
543  
544 6 ▶sub _parse_dbic_key {
Subroutine _parse_dbic_key: called CC=1 SCAR=0.0
545 6 my ( $self, $key ) = @_;
546  
547 6 @{ $self->json->decode(substr($key,length('dbic:row:'))) };
548 }
549  
550 267 ▶sub _part_rows_and_ids {
Subroutine _part_rows_and_ids: called CC=4 SCAR=13.9
551 267 my ( $self, $rows_and_ids ) = @_;
552  
553 267 my ( @rows, @ids, @special );
554  
555 267 for ( @$rows_and_ids ) {
556 3445 ▶ if ( /^dbic:schema/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^dbic:schema/) { } 2 3443 3445
elsif (/^dbic:row:/) { } 6 3437 3443
557 2 push @special, $_;
558 } elsif ( /^dbic:row:/ ) {
559 6 push @rows, $_;
560 } else {
561 3437 push @ids, $_;
562 }
563 }
564  
565 267 return \( @rows, @ids, @special );
566 }
567  
568 5 ▶sub _group_dbic_keys {
Subroutine _group_dbic_keys: called CC=5 SCAR=16.2
569 5 my ( $self, $keys, $mkey_handler ) = @_;
570  
571 5 my ( %keys, %ids );
572  
573 5 foreach my $id ( @$keys ) {
574 6 my ( $rs_name, @key ) = $self->_parse_dbic_key($id);
575  
576 6 ▶ if ( @key > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@key > 1) { } 0 6 6
577 0 $mkey_handler->($id, $rs_name, @key);
578 } else {
579 # for other objects we queue up IDs for a single SELECT
580 6 ▶ push @{ $keys{$rs_name} ||= [] }, $key[0];
Condition100%2/2
$keys{$rs_name} ||= []
l!l
15
MC/DC100%1/1
$keys{$rs_name} ||= []
$keys{$rs_name}
Truth table100%2/2
A
A= $keys{$rs_name}
Aresult
11
00
581 6 ▶ push @{ $ids{$rs_name} ||= [] }, $id;
Condition100%2/2
$ids{$rs_name} ||= []
l!l
15
MC/DC100%1/1
$ids{$rs_name} ||= []
$ids{$rs_name}
Truth table100%2/2
A
A= $ids{$rs_name}
Aresult
11
00
582 }
583 }
584  
585 5 return \( %keys, %ids );
586 }
587  
588 224 ▶sub get {
Subroutine get: called CC=15 SCAR=28.3
Pod documented
589 224 my ( $self, @rows_and_ids ) = @_;
590  
591 224 ▶ return unless @rows_and_ids;
Branch: 1 branch
BranchTrueFalseTotal
unless @rows_and_ids 0 224 224
592  
593 224 my %entries;
594  
595 224 my ( $rows, $ids, $special ) = $self->_part_rows_and_ids(\@rows_and_ids);
596  
597 224 ▶ if ( @$ids ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$ids) 219 5 224
598 219 ▶ $self->dbh_do(sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
599 219 my ( $storage, $dbh ) = @_;
600  
601 219 my @ids_copy = @$ids;
602  
603 219 ▶ my $batch_size = $self->batch_size || scalar(@$ids);
Condition50%1/2
$self->batch_size || scalar @$ids
l!l
2190
MC/DC0%0/1
$self->batch_size || scalar @$ids
$self->batch_size
Truth table50%1/2
A
A= $self->batch_size
Aresult
11
00
604  
605 219 ▶ while ( my @batch_ids = splice(@ids_copy, 0, $batch_size) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@batch_ids) = splice(@ids_copy, 0, $batch_size)) 222 219 441
606 222 my $sth = $self->prepare_select($dbh, "SELECT id, data FROM entries WHERE id IN (" . join(", ", ('?') x @batch_ids) . ")");
607 222 $sth->execute(@batch_ids);
608  
609 222 $sth->bind_columns( \my ( $id, $data ) );
610  
611 # not actually necessary but i'm keeping it around for reference:
612 #my ( $id, $data );
613 #use DBD::Pg qw(PG_BYTEA);
614 #$sth->bind_col(1, \$id);
615 #$sth->bind_col(2, \$data, { pg_type => PG_BYTEA });
616  
617 222 ▶ while ( $sth->fetch ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->fetch) 3388 222 3610
618 3388 $entries{$id} = $data;
619 }
620 }
621 219 });
622 }
623  
624 224 ▶ if ( @$rows ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$rows) 4 220 224
625 4 my $schema = $self->schema;
626  
627 4 my $err = \"foo";
628 4 ▶ my ( $rs_keys, $rs_ids ) = try {
Subroutine __ANON__: called CC=1 SCAR=4.6
629 0 ▶ $self->_group_dbic_keys( $rows, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
630 0 my ( $id, $rs_name, @key ) = @_;
631  
632 # multi column primary keys need 'find'
633 0 ▶ my $obj = $schema->resultset($rs_name)->find(@key) or die $err; # die to stop search
Branch: 1 branch
BranchTrueFalseTotal
my $obj = $schema->resultset($rs_name)->find(@key) or die $err 0 0 0
634  
635 0 $entries{$id} = KiokuDB::Entry->new(
636 id => $id,
637 class => ref($obj),
638 data => $obj,
639 );
640 4 });
641 0 ▶ } catch {
Subroutine __ANON__: not called CC=3 SCAR=24.8
642 0 ▶ die $_ if ref $_ and refaddr($_) == refaddr($err);
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ and builtin::refaddr($_) == builtin::refaddr($err) 0 0 0
Condition0%0/3
ref $_ and builtin::refaddr($_) == builtin::refaddr($err)
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_ and builtin::refaddr($_) == builtin::refaddr($err)
ref $_ builtin::refaddr($_) == builtin::refaddr($err)
Truth table0%0/3
A and B
A= ref $_B= builtin::refaddr($_) == builtin::refaddr($err)
ABresult
0X0
100
111
643 4 ▶ } or return;
Branch: 1 branch
BranchTrueFalseTotal
() = &try(sub { $self->_group_dbic_keys($rows, sub { my($id, $rs_name, @key) = @_; my $obj = $schema->resultset($rs_name)->find(@key) or die $err; $entries{$id} = "KiokuDB::Entry"->new("id", $id, "class", ref $obj, "data", $obj); } ); } , &catch(sub { die $_ if ref $_ and builtin::refaddr($_) == builtin::refaddr($err); } )) or return 4 0 4
644  
645 4 foreach my $rs_name ( keys %$rs_keys ) {
646 4 my $rs = $schema->resultset($rs_name);
647  
648 4 my $ids = $rs_ids->{$rs_name};
649  
650 4 my @objs;
651  
652 4 ▶ if ( @$ids == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$ids == 1) { } 3 1 4
653 3 my $id = $ids->[0];
654  
655 3 ▶ my $obj = $rs->find($rs_keys->{$rs_name}[0]) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $obj = $rs->find($rs_keys->{$rs_name}[0]) or return 3 0 3
656  
657 3 $entries{$id} = KiokuDB::Entry->new(
658 id => $id,
659 class => ref($obj),
660 data => $obj,
661 );
662 } else {
663 1 my ($pk) = $rs->result_source->primary_columns;
664  
665 1 my $keys = $rs_keys->{$rs_name};
666  
667 1 my @objs = $rs->search({ $pk => $keys })->all;
668  
669 1 ▶ return if @objs != @$ids;
Branch: 1 branch
BranchTrueFalseTotal
if @objs != @$ids 0 1 1
670  
671 # this key lookup is because it's not returned in the same order
672 1 my %pk_to_id;
673 1 @pk_to_id{@$keys} = @$ids;
674  
675 1 foreach my $obj ( @objs ) {
676 2 my $id = $pk_to_id{$obj->id};
677 2 $entries{$id} = KiokuDB::Entry->new(
678 id => $id,
679 class => ref($obj),
680 data => $obj,
681 );
682 }
683 }
684 }
685 }
686  
687 224 for ( @$special ) {
688 2 ▶ $entries{$_} = KiokuDB::Entry->new(
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'dbic:schema' ? : 1 1 2
689 id => $_,
690 $_ eq 'dbic:schema'
691 ? ( data => $self->schema,
692 class => "DBIx::Class::Schema" )
693 : ( data => undef,
694 class => "DBIx::Class::ResultSource" )
695 );
696 }
697  
698 # ->rows only works after we're done
699 224 ▶ return if @rows_and_ids != keys %entries;
Branch: 1 branch
BranchTrueFalseTotal
if @rows_and_ids != keys %entries 4 220 224
700 # case sensitivity differences, possibly?
701 220 ▶ return if any { !defined } @entries{@rows_and_ids};
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { not defined $_; } , @entries{@rows_and_ids}) 0 220 220
702  
703 220 ▶ map { ref($_) ? $_ : $self->deserialize($_) } @entries{@rows_and_ids};
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 7 3388 3395
704 }
705  
706 11 ▶sub delete {
Subroutine delete: called CC=2 SCAR=6.9
Pod undocumented
707 11 my ( $self, @ids_or_entries ) = @_;
708  
709 # FIXME special DBIC rows
710  
711 11 ▶ my @ids = map { ref($_) ? $_->id : $_ } @ids_or_entries;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 4 7 11
712  
713 11 ▶ $self->dbh_do(sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
714 11 my ( $storage, $dbh ) = @_;
715  
716 11 my $g = $self->schema->txn_scope_guard;
717  
718 11 ▶ my $batch_size = $self->batch_size || scalar(@ids);
Condition50%1/2
$self->batch_size || scalar @ids
l!l
110
MC/DC0%0/1
$self->batch_size || scalar @ids
$self->batch_size
Truth table50%1/2
A
A= $self->batch_size
Aresult
11
00
719  
720 11 my @ids_copy = @ids;
721 11 ▶ while ( my @batch_ids = splice @ids_copy, 0, $batch_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@batch_ids) = splice(@ids_copy, 0, $batch_size)) 11 11 22
722 11 ▶ if ( $self->extract ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->extract) 11 0 11
723 # FIXME rely on cascade delete?
724 11 my $sth = $dbh->prepare_cached("DELETE FROM gin_index WHERE id IN (" . join(", ", ('?') x @batch_ids) . ")");
725 11 $sth->execute(@batch_ids);
726 11 $sth->finish;
727 }
728  
729 11 my $sth = $dbh->prepare_cached("DELETE FROM entries WHERE id IN (" . join(", ", ('?') x @batch_ids) . ")");
730 11 $sth->execute(@batch_ids);
731 11 $sth->finish;
732 }
733  
734 11 $g->commit;
735 11 });
736  
737 11 return;
738 }
739  
740 43 ▶sub exists {
Subroutine exists: called CC=8 SCAR=21.8
Pod undocumented
741 43 my ( $self, @rows_and_ids ) = @_;
742  
743 43 ▶ return unless @rows_and_ids;
Branch: 1 branch
BranchTrueFalseTotal
unless @rows_and_ids 0 43 43
744  
745 43 my $schema = $self->schema;
746  
747 43 my %entries;
748  
749 43 my ( $rows, $ids, $special ) = $self->_part_rows_and_ids(\@rows_and_ids);
750  
751 43 ▶ if ( @$ids ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$ids) 42 1 43
752 42 ▶ $self->dbh_do(sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
753 42 my ( $storage, $dbh ) = @_;
754  
755 42 ▶ my $batch_size = $self->batch_size || scalar(@$ids);
Condition50%1/2
$self->batch_size || scalar @$ids
l!l
420
MC/DC0%0/1
$self->batch_size || scalar @$ids
$self->batch_size
Truth table50%1/2
A
A= $self->batch_size
Aresult
11
00
756  
757 42 my @ids_copy = @$ids;
758 42 ▶ while ( my @batch_ids = splice @ids_copy, 0, $batch_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@batch_ids) = splice(@ids_copy, 0, $batch_size)) 42 42 84
759 42 my $sth = $self-> prepare_select ( $dbh, "SELECT id FROM entries WHERE id IN (" . join(", ", ('?') x @batch_ids) . ")");
760 42 $sth->execute(@batch_ids);
761  
762 42 $sth->bind_columns( \( my $id ) );
763  
764 42 ▶ $entries{$id} = 1 while $sth->fetch;
Branch: 1 branch
BranchTrueFalseTotal
$sth->fetch and do { $entries{$id} = 1 } 17 42 59
765 }
766 42 });
767 }
768  
769 43 ▶ if ( @$rows ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$rows) 1 42 43
770 0 ▶ my ( $rs_keys, $rs_ids ) = $self->_group_dbic_keys( $rows, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
771 0 my ( $id, $rs_name, @key ) = @_;
772 0 $entries{$id} = defined $schema->resultset($rs_name)->find(@key); # FIXME slow
773 1 });
774  
775 1 foreach my $rs_name ( keys %$rs_keys ) {
776 1 my $rs = $schema->resultset($rs_name);
777  
778 1 my $ids = $rs_ids->{$rs_name};
779 1 my $keys = $rs_keys->{$rs_name};
780  
781 1 my ( $pk ) = $rs->result_source->primary_columns;
782  
783 1 my @exists = $rs->search({ $pk => $keys })->get_column($pk)->all;
784  
785 1 my %pk_to_id;
786 1 @pk_to_id{@$keys} = @$ids;
787  
788 1 @entries{@pk_to_id{@exists}} = ( (1) x @exists );
789 }
790 }
791  
792 43 for ( @$special ) {
793 0 ▶ if ( $_ eq 'dbic:schema' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_ eq 'dbic:schema') { } 0 0 0
elsif (/^dbic:schema:(.*)/) { } 0 0 0
794 0 $entries{$_} = 1;
795 } elsif ( /^dbic:schema:(.*)/ ) {
796 0 ▶ $entries{$_} = defined try { $schema->source($1) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
797 }
798 }
799  
800 43 return @entries{@rows_and_ids};
801 }
802  
803 220 ▶sub txn_begin { shift->storage->txn_begin(@_) }
Subroutine txn_begin: called CC=1 SCAR=0.0
Pod undocumented
804 170 ▶sub txn_commit { shift->storage->txn_commit(@_) }
Subroutine txn_commit: called CC=1 SCAR=0.0
Pod undocumented
805 50 ▶sub txn_rollback { shift->storage->txn_rollback(@_) }
Subroutine txn_rollback: called CC=1 SCAR=0.0
Pod undocumented
806  
807 26 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod undocumented
808 26 my $self = shift;
809  
810 26 ▶ $self->dbh_do(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
811 26 my ( $storage, $dbh ) = @_;
812  
813 26 $dbh->do("DELETE FROM gin_index");
814 26 $dbh->do("DELETE FROM entries");
815 26 });
816 }
817  
818 86 ▶sub _sth_stream {
Subroutine _sth_stream: called CC=1 SCAR=0.0
819 86 my ( $self, $sql, @bind ) = @_;
820  
821 86 ▶ $self->dbh_do(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
822 86 my ( $storage, $dbh ) = @_;
823 86 my $sth = $self->prepare_select($dbh, $sql);
824  
825 86 $sth->execute(@bind);
826  
827 86 Data::Stream::Bulk::DBI->new( sth => $sth );
828 86 });
829 }
830  
831 64 ▶sub _select_entry_stream {
Subroutine _select_entry_stream: called CC=1 SCAR=0.0
832 64 my ( $self, @args ) = @_;
833  
834 64 my $stream = $self->_sth_stream(@args);
835  
836 64 ▶ return $stream->filter(sub { [ map { $self->deserialize($_->[0]) } @$_ ] });
Subroutine __ANON__: called CC=1 SCAR=0.0
837 }
838  
839 22 ▶sub all_entries {
Subroutine all_entries: called CC=1 SCAR=0.0
Pod undocumented
840 22 my $self = shift;
841 22 $self->_select_entry_stream("SELECT data FROM entries");
842 }
843  
844 26 ▶sub root_entries {
Subroutine root_entries: called CC=1 SCAR=0.0
Pod undocumented
845 26 my $self = shift;
846 26 $self->_select_entry_stream("SELECT data FROM entries WHERE root");
847 }
848  
849 3 ▶sub child_entries {
Subroutine child_entries: called CC=1 SCAR=0.0
Pod undocumented
850 3 my $self = shift;
851 3 $self->_select_entry_stream("SELECT data FROM entries WHERE not root");
852 }
853  
854 22 ▶sub _select_id_stream {
Subroutine _select_id_stream: called CC=1 SCAR=0.0
855 22 my ( $self, @args ) = @_;
856  
857 22 my $stream = $self->_sth_stream(@args);
858  
859 22 ▶ return $stream->filter(sub {[ map { $_->[0] } @$_ ]});
Subroutine __ANON__: called CC=1 SCAR=0.0
860 }
861  
862 6 ▶sub all_entry_ids {
Subroutine all_entry_ids: called CC=1 SCAR=0.0
Pod undocumented
863 6 my $self = shift;
864 6 $self->_select_id_stream("SELECT id FROM entries");
865 }
866  
867 14 ▶sub root_entry_ids {
Subroutine root_entry_ids: called CC=1 SCAR=0.0
Pod undocumented
868 14 my $self = shift;
869 14 $self->_select_id_stream("SELECT id FROM entries WHERE root");
870 }
871  
872 2 ▶sub child_entry_ids {
Subroutine child_entry_ids: called CC=1 SCAR=0.0
Pod undocumented
873 2 my $self = shift;
874 2 $self->_select_id_stream("SELECT id FROM entries WHERE not root");
875 }
876  
877 5 ▶sub simple_search {
Subroutine simple_search: called CC=1 SCAR=0.0
Pod undocumented
878 5 my ( $self, $proto ) = @_;
879  
880 5 my ( $where_clause, @bind ) = $self->sql_abstract->where($proto);
881  
882 5 $self->_select_entry_stream("SELECT data FROM entries $where_clause", @bind);
883 }
884  
885 8 ▶sub search {
Subroutine search: called CC=1 SCAR=0.0
Pod undocumented
886 8 my ( $self, $query, @args ) = @_;
887  
888 8 my %args = (
889 distinct => $self->distinct,
890 @args,
891 );
892  
893 8 my %spec = $query->extract_values($self);
894 8 my @binds;
895  
896 8 my $inner_sql = $self->_search_gin_subquery(\%spec, \@binds);
897 8 return $self->_select_entry_stream("SELECT data FROM entries WHERE id IN (".$inner_sql.")",@binds);
898 }
899  
900 10 ▶sub _search_gin_subquery {
Subroutine _search_gin_subquery: called CC=8 SCAR=20.9
901 10 my ($self, $spec, $binds) = @_;
902  
903 10 ▶ my @v = ref $spec->{values} eq 'ARRAY' ? @{ $spec->{values} } : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $spec->{'values'} eq 'ARRAY' ? : 9 1 10
904 10 ▶ if ( $spec->{method} eq 'set' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($spec->{'method'} eq 'set') { } 1 9 10
elsif ($spec->{'method'} eq 'all' and @v > 1) { } 1 8 9
905 1 my $op = $spec->{operation};
906  
907 1 ▶ die 'gin set query received bad operation'
Branch: 1 branch
BranchTrueFalseTotal
unless $op =~ /^(UNION|INTERSECT|EXCEPT)$/i 0 1 1
908 unless $op =~ /^(UNION|INTERSECT|EXCEPT)$/i;
909  
910 die 'gin set query missing subqueries'
911 unless ref $spec->{subqueries} eq 'ARRAY' &&
912 1 ▶ scalar @{ $spec->{subqueries} };
Branch: 1 branch
BranchTrueFalseTotal
unless ref $spec->{"subqueries"} eq "ARRAY" and scalar @{$spec->{"subqueries"};} 0 1 1
Condition50%1/2
ref $spec->{"subqueries"} eq "ARRAY" and scalar @{$spec->{"subqueries"};}
!ll
01
MC/DC0%0/1
ref $spec->{"subqueries"} eq "ARRAY" and scalar @{$spec->{"subqueries"};}
ref $spec->{"subqueries"} eq "ARRAY"
Truth table50%1/2
A
A= ref $spec->{"subqueries"} eq "ARRAY"
Aresult
00
11
913  
914 return "(".
915 (
916 join ' '.$op.' ',
917 2 map { $self->_search_gin_subquery($_, $binds) }
918 1 @{ $spec->{subqueries} }
919 ).")";
920  
921 ▶ } elsif ( $spec->{method} eq 'all' and @v > 1) {
Condition100%3/3
$spec->{"method"} eq "all" and @v > 1
!ll&&!rl&&r
171
MC/DC100%2/2
$spec->{"method"} eq "all" and @v > 1
$spec->{"method"} eq "all" @v > 1
Truth table100%3/3
A and B
A= $spec->{"method"} eq "all"B= @v > 1
ABresult
0X0
100
111
922 # for some reason count(id) = ? doesn't work
923 1 push @$binds, @v;
924 1 return "SELECT id FROM gin_index WHERE value IN ".
925 "(" . join(", ", ('?') x @v) . ")" .
926 "GROUP BY id HAVING COUNT(id) = " . scalar(@v);
927 } else {
928 8 push @$binds, @v;
929 8 return "SELECT DISTINCT id FROM gin_index WHERE value IN ".
930 "(" . join(", ", ('?') x @v) . ")";
931 }
932 }
933  
934 0 ▶sub fetch_entry { die "TODO" }
Subroutine fetch_entry: not called CC=1 SCAR=6.9
Pod undocumented
935  
936 0 ▶sub remove_ids {
Subroutine remove_ids: not called CC=1 SCAR=6.9
Pod undocumented
937 0 my ( $self, @ids ) = @_;
938  
939 0 die "Deletion the GIN index is handled implicitly";
940 }
941  
942 0 ▶sub insert_entry {
Subroutine insert_entry: not called CC=1 SCAR=6.9
Pod undocumented
943 0 my ( $self, $id, @keys ) = @_;
944  
945 0 die "Insertion to the GIN index is handled implicitly";
946 }
947  
948 61 ▶sub _table_info {
Subroutine _table_info: called CC=1 SCAR=0.0
949 61 my ( $self, $catalog, $schema, $table ) = @_;
950  
951 61 ▶ $self->dbh_do(sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
952 61 my ( $storage, $dbh ) = @_;
953  
954 61 ▶ my $filter = ( $self->storage->sqlt_type eq 'SQLite' ? '%' : '' );
Branch: 1 branch
BranchTrueFalseTotal
$self->storage->sqlt_type eq 'SQLite' ? : 61 0 61
955  
956 61 foreach my $arg ( $catalog, $schema, $table ) {
957 183 ▶ $arg = $filter unless defined $arg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg 122 61 183
958 }
959  
960 61 $dbh->table_info($catalog, $schema, $table, 'TABLE')->fetchall_arrayref;
961 61 });
962 }
963  
964 61 ▶sub tables_exist {
Subroutine tables_exist: called CC=1 SCAR=0.0
Pod undocumented
965 61 my $self = shift;
966  
967 61 return ( @{ $self->_table_info(undef, undef, 'entries') } > 0 );
968 }
969  
970 61 ▶sub create_tables {
Subroutine create_tables: called CC=1 SCAR=0.0
Pod undocumented
971 61 my $self = shift;
972  
973 61 ▶ $self->dbh_do(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
974 61 my ( $storage, $dbh ) = @_;
975  
976 61 ▶ unless ( $self->tables_exist ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->tables_exist) 5 56 61
977 5 $self->deploy({ producer_args => { mysql_version => 4.1 } });
978 }
979 61 });
980 }
981  
982 2 ▶sub drop_tables {
Subroutine drop_tables: called CC=1 SCAR=0.0
Pod documented
983 2 my $self = shift;
984  
985 2 ▶ $self->dbh_do(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
986 2 my ( $storage, $dbh ) = @_;
987  
988 2 $dbh->do("DROP TABLE gin_index");
989 2 $dbh->do("DROP TABLE entries");
990 2 });
991 }
992  
993 4 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=3 SCAR=11.0
Pod undocumented
994 4 my $self = shift;
995 4 ▶ return if $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] 0 4 4
996  
997 4 ▶ if ( $self->has_storage ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_storage) 3 1 4
998 3 $self->storage->disconnect;
999 }
1000 }
1001  
1002 0 ▶sub new_garbage_collector {
Subroutine new_garbage_collector: not called CC=5 SCAR=34.0
Pod undocumented
1003 0 my ( $self, %args ) = @_;
1004  
1005 0 ▶ if ( grep { $_ !~ /^(?:entries|gin_index)/ } map { $_->[2] } @{ $self->_table_info } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {not $_ =~ /^(?:entries|gin_index)/;} map({$_->[2];} @{$self->_table_info;})) { } 0 0 0
1006 0 die "\nRefusing to GC a database with additional tables.\n\nThis is ecause the root set and referencing scheme might be ambiguous (it's not yet clear what garbage collection should actually do on a mixed schema).\n";
1007 } else {
1008 0 my $cmd = $args{command};
1009 0 ▶ my $class = $args{class} || $cmd ? $cmd->class : "KiokuDB::GC::Naive";
Branch: 1 branch
BranchTrueFalseTotal
$args{'class'} || $cmd ? : 0 0 0
Condition0%0/3
$args{"class"} or $cmd
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"class"} or $cmd
$args{"class"} $cmd
Truth table0%0/3
A or B
A= $args{"class"}B= $cmd
ABresult
1X1
011
000
1010  
1011 0 load_class($class);
1012  
1013 0 ▶ return $class->new(
Branch: 1 branch
BranchTrueFalseTotal
$cmd ? : 0 0 0
1014 %args,
1015 backend => $self,
1016 ( $cmd ? ( verbose => $cmd->verbose ) : $cmd ),
1017 );
1018 }
1019 }
1020  
1021 6 __PACKAGE__->meta->make_immutable;
1022  
1023 6 __PACKAGE__
1024  
1025 __END__
↑ Summary DBIx/Class/Schema/KiokuDB.pm »