NoSQL/Store.pm

filter: Statementstmt 76.9% 117 / 152 Branchbran 50.0% 25 / 50 Conditioncond 58.3% 7 / 12 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 79.4% 27 / 34 Podpod 0.0% 0 / 13 totaltotal 66.5% 179 / 269 CC 34 SCAR 24.9
CC
34
Cov
70%
validate
72.0
model_exists
12.0
_is_likely_file_connection
7.4
CRAP 12.0
« NoSQL/Class.pm ↑ Summary NoSQL/Search.pm »
1 package DBIx::NoSQL::Store;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 eval { require JSON::XS; };
12 8 our $json = JSON->new->pretty;
13 55 ▶sub json { $json }
Subroutine json: called CC=1 SCAR=0.0
Pod undocumented
14  
15 8 ▶use DBIx::NoSQL::Model;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 8 has database => qw/ is ro /;
18 8 has connection => qw/ is ro /;
19 8 has strict => qw/ is rw isa Bool default 0 /;
20  
21 8 has storage => qw/ is ro lazy_build 1 /;
22 sub _build_storage {
23 9 ▶ my $self = shift;
Subroutine _build_storage: called CC=1 SCAR=0.0
24 9 require DBIx::NoSQL::Storage;
25 9 return DBIx::NoSQL::Storage->new( store => $self );
26 }
27  
28 8 has _model => qw/ is ro lazy_build 1 /;
29 9 ▶sub _build__model { {} }
Subroutine _build__model: called CC=1 SCAR=0.0
30  
31 8 has type_map => qw/ is ro lazy_build 1 /;
32 sub _build_type_map {
33 0 ▶ my $self = shift;
Subroutine _build_type_map: not called CC=1 SCAR=6.9
34 0 require DBIx::NoSQL::TypeMap;
35 0 return DBIx::NoSQL::TypeMap->new();
36 }
37  
38 sub model {
39 67 ▶ my $self = shift;
Subroutine model: called CC=6 SCAR=19.9
Pod undocumented
40 67 ▶ die "Missing model name" unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 67 67
41 67 ▶ if ( @_ > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 0 67 67
42 0 $self->model( $_ ) for @_;
43 }
44 else {
45 67 ▶ my $name = shift or die "Missing model name";
Branch: 1 branch
BranchTrueFalseTotal
my $name = shift() or die "Missing model name" 0 67 67
46  
47 67 ▶ return $self->_model->{ $name } ||= DBIx::NoSQL::Model->new( store => $self, name => $name );
Condition66%2/3
$self->_model->{$name} ||= "DBIx::NoSQL::Model"->new("store", $self, "name", $name)
l!l&&r!l&&!r
49180
MC/DC0%0/2
$self->_model->{$name} ||= "DBIx::NoSQL::Model"->new("store", $self, "name", $name)
$self->_model->{$name} "DBIx::NoSQL::Model"->new("store", $self, "name", $name)
Truth table66%2/3
A ||= B
A= $self->_model->{$name}B= "DBIx::NoSQL::Model"->new("store", $self, "name", $name)
ABresult
1X1
011
000
48 }
49 }
50  
51 sub model_exists {
52 0 ▶ my $self = shift;
Subroutine model_exists: not called CC=3 SCAR=24.8
Pod undocumented
53 0 my $name = shift;
54 0 ▶ die "Missing model name" unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 0 0
55 0 ▶ return $self->_model->{ $name } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_model->{$name} ? : 0 0 0
56 }
57  
58 sub validate {
59 0 ▶ my $self = shift;
Subroutine validate: not called CC=8 SCAR=42.8
Pod undocumented
60 0 my %options = @_;
61  
62 0 ▶ exists $options{ $_ } or $options{ $_ } = 1 for qw/ fatal /;
Branch: 1 branch
BranchTrueFalseTotal
exists $options{$_} or $options{$_} = 1 0 0 0
63  
64 0 my $valid = 1;
65 0 for my $model ( values %{ $self->_model } ) {
66 0 ▶ next unless $model->searchable;
Branch: 1 branch
BranchTrueFalseTotal
unless $model->searchable 0 0 0
67 0 my $index = $model->index;
68 0 ▶ next unless $index->exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $index->exists 0 0 0
69 0 $valid = $index->same;
70 0 ▶ if ( ! $valid && $options{ fatal } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $valid and $options{"fatal"}) 0 0 0
Condition0%0/3
not $valid and $options{"fatal"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $valid and $options{"fatal"}
not $valid $options{"fatal"}
Truth table0%0/3
not(A) and B
A= not $validB= $options{"fatal"}
ABresult
0X0
100
111
71 0 my $name = $model->name;
72 0 die "Model \"$model\" has invalid index (schema mismatch)";
73 }
74 }
75 }
76  
77 sub reindex {
78 1 ▶ my $self = shift;
Subroutine reindex: called CC=5 SCAR=16.2
Pod undocumented
79  
80 1 for my $model ( values %{ $self->_model } ) {
81 2 ▶ next unless $model->searchable;
Branch: 1 branch
BranchTrueFalseTotal
unless $model->searchable 1 1 2
82 1 my $index = $model->index;
83 1 $index->reset;
84 1 ▶ next unless $index->exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $index->exists 0 1 1
85 1 ▶ next if $index->same;
Branch: 1 branch
BranchTrueFalseTotal
if $index->same 0 1 1
86 1 $index->reindex;
87 }
88 }
89  
90 sub _model_do {
91 36 ▶ my $self = shift;
Subroutine _model_do: called CC=3 SCAR=11.3
92 36 ▶ my $name = shift or die "Missing model name";
Branch: 1 branch
BranchTrueFalseTotal
my $name = shift() or die "Missing model name" 0 36 36
93 36 ▶ my $operation = shift or die "Missing model operation";
Branch: 1 branch
BranchTrueFalseTotal
my $operation = shift() or die "Missing model operation" 0 36 36
94  
95 36 my $model = $self->model( $name );
96 36 return $model->$operation( @_ );
97 }
98  
99 sub search {
100 16 ▶ return shift->_model_do( shift, 'search', @_ );
Subroutine search: called CC=1 SCAR=0.0
Pod undocumented
101 }
102  
103 sub set {
104 6 ▶ return shift->_model_do( shift, 'set', @_ );
Subroutine set: called CC=1 SCAR=0.0
Pod undocumented
105 }
106  
107 sub get {
108 3 ▶ return shift->_model_do( shift, 'get', @_ );
Subroutine get: called CC=1 SCAR=0.0
Pod undocumented
109 }
110  
111 sub delete {
112 1 ▶ return shift->_model_do( shift, 'delete', @_ );
Subroutine delete: called CC=1 SCAR=0.0
Pod undocumented
113 }
114  
115 sub exists {
116 10 ▶ return shift->_model_do( shift, 'exists', @_ );
Subroutine exists: called CC=1 SCAR=0.0
Pod undocumented
117 }
118  
119 8 has stash => qw/ is ro lazy_build 1 /;
120 sub _build_stash {
121 9 ▶ require DBIx::NoSQL::Stash;
Subroutine _build_stash: called CC=1 SCAR=0.0
122 9 my $self = shift;
123 9 my $stash = DBIx::NoSQL::Stash->new( store => $self );
124 9 return $stash;
125 }
126  
127 8 require DBIx::NoSQL::ClassScaffold;
128  
129 8 has schema_class_scaffold => qw/ is ro lazy_build 1 /;
130 9 ▶sub _build_schema_class_scaffold { return DBIx::NoSQL::ClassScaffold->new->become_Schema }
Subroutine _build_schema_class_scaffold: called CC=1 SCAR=0.0
131 8 has schema_class => qw/ is ro lazy_build 1 /;
132 sub _build_schema_class {
133 9 ▶ my $self = shift;
Subroutine _build_schema_class: called CC=1 SCAR=0.0
134 9 my $class = $self->schema_class_scaffold->package;
135  
136 9 my $store_result_class_scaffold = DBIx::NoSQL::ClassScaffold->new->become_ResultClass_Store;
137 9 my $store_result_class = $store_result_class_scaffold->package;
138 9 $store_result_class->register( $class, $store_result_class->table );
139  
140 9 return $class;
141 }
142  
143 8 has schema => qw/ accessor _schema lazy_build 1 predicate _has_schema /;
144 sub _build_schema {
145 2 ▶ my $self = shift;
Subroutine _build_schema: called CC=3 SCAR=11.3
146  
147 2 my $connection = $self->connection;
148 2 ▶ if ( ! $connection ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($connection) 2 0 2
149 2 my $database = $self->database;
150 2 ▶ if ( ! $database ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($database) 0 2 2
151 0 die "Unable to connect schema to database because no connection or database are defined";
152 }
153 2 $connection = $database;
154 }
155  
156 2 my $schema = $self->_connect( $connection );
157 2 return $schema;
158 }
159  
160 sub schema {
161 109 ▶ my $self = shift;
Subroutine schema: called CC=1 SCAR=0.0
Pod undocumented
162 109 return $self->_schema( @_ );
163 }
164  
165 sub connect {
166 13 ▶ my $self = shift;
Subroutine connect: called CC=3 SCAR=11.0
Pod undocumented
167 13 ▶ if ( ! blessed $self ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($self)) 6 7 13
168 6 return $self->new->connect( @_ );
169 }
170  
171 7 ▶ $self->clear_schema if $self->_has_schema;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_schema 0 7 7
172 7 my $schema = $self->_connect( @_ );
173 7 return $self;
174 }
175  
176 sub _is_likely_file_connection {
177 15 ▶ my $self = shift;
Subroutine _is_likely_file_connection: called CC=7 SCAR=20.1
178 15 my $connection = shift;
179  
180 15 ▶ if ( ref $connection eq 'ARRAY' ) { return 0 }
Branch: 4 branches
BranchTrueFalseTotal
if (ref $connection eq 'ARRAY') { } 1 14 15
elsif (ref $connection eq '' and $connection =~ /^dbi:/i) { } 1 13 14
elsif (builtin::blessed($connection) and $connection->isa('Path::Class::File')) { } 9 4 13
elsif (ref $connection eq '') { } 4 0 4
181 1 ▶ elsif ( ref $connection eq '' && $connection =~ m/^dbi:/i ) { return 0 }
Condition100%3/3
ref $connection eq "" and $connection =~ /^dbi:/i
!ll&&!rl&&r
941
MC/DC100%2/2
ref $connection eq "" and $connection =~ /^dbi:/i
ref $connection eq "" $connection =~ /^dbi:/i
Truth table100%3/3
A and B
A= ref $connection eq ""B= $connection =~ /^dbi:/i
ABresult
0X0
100
111
182 9 ▶ elsif ( blessed $connection && $connection->isa( 'Path::Class::File' ) ) { return 1 }
Condition66%2/3
builtin::blessed($connection) and $connection->isa("Path::Class::File")
!ll&&!rl&&r
409
MC/DC50%1/2
builtin::blessed($connection) and $connection->isa("Path::Class::File")
builtin::blessed($connection) $connection->isa("Path::Class::File")
Truth table66%2/3
A and B
A= builtin::blessed($connection)B= $connection->isa("Path::Class::File")
ABresult
0X0
100
111
183 4 elsif ( ref $connection eq '' ) { return 1 }
184  
185 0 warn ref $connection;
186 0 warn $connection;
187  
188 0 return 0; # Not sure, pass through to DBI I guess
189 }
190  
191 sub _connect {
192 9 ▶ my $self = shift;
Subroutine _connect: called CC=4 SCAR=13.9
193 9 my $connection = shift;
194  
195 9 my $database_file;
196 9 ▶ if ( $self->_is_likely_file_connection( $connection ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_likely_file_connection($connection)) 9 0 9
197 9 $connection = file "$connection";
198 9 $database_file = $connection;
199 9 $database_file->parent->mkpath; # TODO Make this optional?
200 9 $connection = "dbi:SQLite:dbname=$database_file";
201 }
202  
203 9 ▶ $connection = [ $connection ] unless ref $connection eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $connection eq "ARRAY" 9 0 9
204 9 my $schema = $self->schema_class->connect( @$connection );
205 9 $schema->store( $self );
206  
207 # FIXME This kind of sucks, and potentially a little redundant, see _build_schema
208 9 $self->schema( $schema );
209 9 ▶ if ( ! $self->storage->table_exists( '__Store__' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->storage->table_exists("__Store__")) 8 1 9
210 8 $schema->deploy;
211 }
212  
213 9 return $schema;
214 }
215  
216 8 has dbh => qw/ is ro lazy_build 1 weak_ref 1 /;
217 sub _build_dbh {
218 1 ▶ my $self = shift;
Subroutine _build_dbh: called CC=1 SCAR=0.0
219 1 return $self->schema->storage->dbh;
220 }
221  
222 sub transact {
223 0 ▶ my $self = shift;
Subroutine transact: not called CC=1 SCAR=6.9
Pod undocumented
224 0 my $code = shift;
225  
226 0 my $dbh = $self->dbh;
227 try {
228 0 ▶ $dbh->begin_work;
Subroutine __ANON__: not called CC=1 SCAR=6.9
229 0 $code->();
230 0 $dbh->commit;
231 }
232 catch {
233 0 ▶ my $error = $_[0];
Subroutine __ANON__: not called CC=1 SCAR=6.9
234 try {
235 0 ▶ $dbh->rollback;
Subroutine __ANON__: not called CC=1 SCAR=6.9
236 }
237 0 die $error;
238 }
239 0 }
240  
241 8 1;
« NoSQL/Class.pm ↑ Summary NoSQL/Search.pm »