Model.pm

filter: Statementstmt 37.9% 120 / 316 Branchbran 23.2% 39 / 168 Conditioncond 19.6% 10 / 51 MC/DCmcdc 0.0% 0 / 33 Subroutinesub 35.4% 17 / 48 Podpod 19.4% 7 / 36 totaltotal 29.6% 193 / 652 CC 104 SCAR 38.7
CC
104
Cov
32%
lookup_multi
156.0
_get_query_args
141.2
update
90.0
CRAP 47.8
« Model/Schema/SQL.pm ↑ Summary Model/SQL.pm »
1 package Data::Model;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 our $VERSION = '0.00008';
6  
7 4 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 $Carp::Internal{(__PACKAGE__)}++;
9  
10 4 ▶use Data::Model::Iterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Data::Model::Transaction;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 our $RUN_VALIDATION;
14 4 ▶if (exists $ENV{DATA_MODE_RUN_VALIDATION}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{'DATA_MODE_RUN_VALIDATION'}) { } 0 4 4
15 0 ▶ $RUN_VALIDATION = $ENV{DATA_MODE_RUN_VALIDATION} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'DATA_MODE_RUN_VALIDATION'} ? : 0 0 0
16 } else {
17 4 $RUN_VALIDATION = 1; # default is any time validation
18 # $RUN_VALIDATION = $ENV{HARNESS_ACTIVE} ? 1 : 0;
19 }
20 4 ▶use Params::Validate ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22  
23 ## for schema methods
24 0 ▶sub driver {};
Subroutine driver: not called CC=1 SCAR=0.0
Pod undocumented
25 0 ▶sub model {};
Subroutine model: not called CC=1 SCAR=0.0
Pod undocumented
26 0 ▶sub schema {};
Subroutine schema: not called CC=1 SCAR=0.0
Pod undocumented
27 0 ▶sub column {};
Subroutine column: not called CC=1 SCAR=0.0
Pod undocumented
28 0 ▶sub columns {};
Subroutine columns: not called CC=1 SCAR=0.0
Pod undocumented
29 0 ▶sub key {};
Subroutine key: not called CC=1 SCAR=0.0
Pod undocumented
30 0 ▶sub index {};
Subroutine index: not called CC=1 SCAR=0.0
Pod undocumented
31 0 ▶sub unique {};
Subroutine unique: not called CC=1 SCAR=0.0
Pod undocumented
32 0 ▶sub schema_options {};
Subroutine schema_options: not called CC=1 SCAR=0.0
Pod undocumented
33 0 ▶sub __properties { +{} }
Subroutine __properties: not called CC=1 SCAR=6.9
34  
35 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
36 1 my $class = shift;
37 1 bless {
38 schema_class => $class,
39 }, $class;
40 }
41  
42 ## data model attributes
43  
44 0 ▶sub get_schema_class {
Subroutine get_schema_class: not called CC=2 SCAR=17.9
Pod undocumented
45 0 my($self, $model) = @_;
46 0 ▶ (ref($self) || $self) . '::' . $model;
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
47 }
48  
49 8 ▶sub get_schema {
Subroutine get_schema: called CC=2 SCAR=7.0
Pod undocumented
50 8 my($self, $model) = @_;
51 8 my $schema = $self->__properties->{schema}->{$model};
52 8 ▶ Carp::croak "not defined schema $model" unless $schema;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema 0 8 8
53 8 $schema;
54 }
55  
56 0 ▶sub clear_all_drivers {
Subroutine clear_all_drivers: not called CC=2 SCAR=17.9
Pod undocumented
57 0 my $self = shift;
58 0 for my $model ($self->schema_names) {
59 0 $self->set_driver($model, undef);
60 }
61 }
62  
63 0 ▶sub get_base_driver {
Subroutine get_base_driver: not called CC=1 SCAR=6.9
Pod undocumented
64 0 shift->__properties->{base_driver};
65 }
66  
67 1 ▶sub set_base_driver {
Subroutine set_base_driver: called CC=3 SCAR=11.1
Pod undocumented
68 1 my($self, $driver) = @_;
69 1 $self->__properties->{base_driver} = $driver;
70 1 for my $model ($self->schema_names) {
71 1 ▶ $self->set_driver($model, $driver) unless $self->get_driver($model);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get_driver($model) 1 0 1
72 }
73 }
74  
75 1 ▶sub get_driver {
Subroutine get_driver: called CC=1 SCAR=0.0
Pod undocumented
76 1 my($self, $model) = @_;
77 1 $self->get_schema($model)->{driver};
78 }
79  
80 1 ▶sub set_driver {
Subroutine set_driver: called CC=4 SCAR=15.2
Pod undocumented
81 1 my($self, $model, $driver) = @_;
82 1 my $schema = $self->get_schema($model);
83 1 ▶ my $old = (exists $schema->{driver} && $schema->{driver});
Condition33%1/3
exists $schema->{'driver'} && $schema->{'driver'}
!ll&&!rl&&r
010
MC/DC0%0/2
exists $schema->{'driver'} && $schema->{'driver'}
exists $schema->{'driver'} $schema->{'driver'}
Truth table33%1/3
A && B
A= exists $schema->{'driver'}B= $schema->{'driver'}
ABresult
0X0
100
111
84 1 ▶ if ($old) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old) 0 1 1
85 0 $old->detach_model($model, $schema);
86 }
87 1 $schema->driver($driver);
88 1 ▶ if ($driver) {
Branch: 1 branch
BranchTrueFalseTotal
if ($driver) 1 0 1
89 1 $driver->attach_model($model, $schema);
90 }
91 }
92  
93  
94 3 ▶sub schema_names {
Subroutine schema_names: called CC=1 SCAR=0.0
Pod undocumented
95 3 my $self = shift;
96 3 keys %{ $self->__properties->{schema} };
97 }
98  
99 1 ▶sub as_sqls {
Subroutine as_sqls: called CC=4 SCAR=14.5
Pod undocumented
100 1 my $self = shift;
101 1 my $target = shift;
102 1 my @sql = ();
103 1 for my $model ($self->schema_names) {
104 1 ▶ next if $target && $model ne $target;
Branch: 1 branch
BranchTrueFalseTotal
if $target and $model ne $target 0 1 1
Condition33%1/3
$target and $model ne $target
!ll&&!rl&&r
010
MC/DC0%0/2
$target and $model ne $target
$target $model ne $target
Truth table33%1/3
A and B
A= $targetB= $model ne $target
ABresult
0X0
100
111
105 1 push @sql, $self->get_schema($model)->sql->as_sql;
106 }
107 1 @sql;
108 }
109  
110 ## get / set / delete
111  
112 3 ▶sub _get_query_args {
Subroutine _get_query_args: called CC=21 SCAR=49.5
113 3 my $self = shift;
114 3 my $schema = shift;
115 3 ▶ return [] unless exists $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_[0] 0 3 3
116  
117 # get key array or query
118 3 my $key_array = undef;
119 3 my $query = undef;
120 3 ▶ if (ref($_[0]) eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 3 3
elsif (ref $_[0] eq 'ARRAY') { } 0 3 3
elsif (not ref $_[0]) { } 3 0 3
121 ## ->get( modelname => { search query } );
122 0 $query = shift;
123 } elsif (ref($_[0]) eq 'ARRAY') {
124 ## ->get( modelname => [ keys ]);
125 0 $key_array = shift;
126 } elsif (!ref($_[0])) {
127 ## ->get( modelname => 'key');
128 3 $key_array = [ shift ];
129 } else {
130 0 return [];
131 }
132  
133 # get query
134 3 ▶ if ($query) {
Branch: 2 branches
BranchTrueFalseTotal
if ($query) { } 0 3 3
elsif (ref $_[0] eq 'HASH') { } 0 3 3
135 ## nop
136 } elsif (ref($_[0]) eq 'HASH') {
137 ## get query
138 0 $query = shift;
139 } else {
140 3 shift;
141 }
142  
143  
144 # for query param validation
145 3 ▶ if ($RUN_VALIDATION && $query) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Data::Model::RUN_VALIDATION and $query) 0 3 3
Condition33%1/3
$Data::Model::RUN_VALIDATION and $query
!ll&&!rl&&r
030
MC/DC0%0/2
$Data::Model::RUN_VALIDATION and $query
$Data::Model::RUN_VALIDATION $query
Truth table33%1/3
A and B
A= $Data::Model::RUN_VALIDATIONB= $query
ABresult
0X0
100
111
146 0 my @p = %{ $query };
147 validate(
148 @p, {
149 index => {
150 type => HASHREF | UNDEF,
151 optional => 1,
152 callbacks => {
153 0 ▶ has_index_name => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
154 0 ▶ return 1 unless $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] 0 0 0
155 0 ▶ return 0 unless scalar(@{ [ %{ $_[0] } ] }) == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{[%{$_[0];}];} == 2 0 0 0
156 0 my($name) = %{ $_[0] };
157 0 $schema->has_index($name);
158 },
159 },
160 },
161 0 where => {
162 type => HASHREF | ARRAYREF | UNDEF,
163 optional => 1,
164 },
165 order => {
166 type => HASHREF | ARRAYREF | UNDEF,
167 optional => 1,
168 },
169 group => {
170 type => HASHREF | ARRAYREF | UNDEF,
171 optional => 1,
172 },
173 limit => {
174 type => SCALAR | UNDEF,
175 optional => 1,
176 },
177 offset => {
178 type => SCALAR | UNDEF,
179 optional => 1,
180 },
181 },
182 );
183 }
184  
185  
186 # if first key is undef then nothing keys
187 3 ▶ $key_array = [] if $key_array && ref($key_array) && !defined $key_array->[0];
Branch: 1 branch
BranchTrueFalseTotal
if $key_array and ref $key_array and not defined $key_array->[0] 0 3 3
Condition33%2/6
$key_array and ref $key_array and not defined $key_array->[0]
!ll&&!rl&&r
030
$key_array and ref $key_array
!ll&&!rl&&r
003
MC/DC0%0/3
$key_array and ref $key_array and not defined $key_array->[0]
$key_array ref $key_array not defined $key_array->[0]
Truth table25%1/4
A and B and not(C)
A= $key_arrayB= ref $key_arrayC= not defined $key_array->[0]
ABCresult
0XX0
10X0
1100
1111
188  
189 # deflate search key
190 3 ▶ if ($schema->has_deflate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->has_deflate) 0 3 3
191 0 ▶ if ($key_array) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_array) 0 0 0
192 0 my $columns = $schema->get_columns_hash_by_key_array_and_hash(+{}, $key_array);
193 0 $schema->deflate($columns);
194 0 $key_array = $schema->get_key_array_by_hash( $columns );
195 }
196  
197 # deflate search index
198 0 ▶ if ($query && ref($query->{index}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($query and ref $query->{"index"} eq "HASH") 0 0 0
Condition0%0/3
$query and ref $query->{"index"} eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$query and ref $query->{"index"} eq "HASH"
$query ref $query->{"index"} eq "HASH"
Truth table0%0/3
A and B
A= $queryB= ref $query->{"index"} eq "HASH"
ABresult
0X0
100
111
199 0 my($name, $key_array) = ( %{ $query->{index} } );
200 0 ▶ $key_array = [ $key_array ] unless ref($key_array) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $key_array eq "ARRAY" 0 0 0
201 0 my $columns = $schema->get_columns_hash_by_key_array_and_hash(+{}, $key_array, $name);
202 0 $schema->deflate($columns);
203 0 $query->{index} = { $name => $schema->get_key_array_by_hash($columns, $name) };
204 }
205 }
206  
207 3 ▶ return [] if ($key_array && !@{ $key_array });
Branch: 1 branch
BranchTrueFalseTotal
if $key_array and not @{$key_array;} 0 3 3
Condition33%1/3
$key_array and not @{$key_array;}
!ll&&!rl&&r
030
MC/DC0%0/2
$key_array and not @{$key_array;}
$key_array not @{$key_array;}
Truth table33%1/3
A and not(B)
A= $key_arrayB= not @{$key_array;}
ABresult
0X0
100
111
208 3 ▶ return [] unless $key_array || $query;
Branch: 1 branch
BranchTrueFalseTotal
unless $key_array or $query 0 3 3
Condition33%1/3
$key_array or $query
l!l&&r!l&&!r
300
MC/DC0%0/2
$key_array or $query
$key_array $query
Truth table33%1/3
A or B
A= $key_arrayB= $query
ABresult
1X1
011
000
209 3 return [ $key_array, $query, @_ ];
210 }
211  
212 0 ▶sub lookup {
Subroutine lookup: not called CC=7 SCAR=40.3
Pod documented
213 0 my($self, $model, $id) = @_;
214 0 ▶ Carp::croak "The 'lookup' method can not be performed during a transaction." if $self->{active_transaction};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"active_transaction"} 0 0 0
215 0 my $schema = $self->get_schema($model);
216 0 ▶ return unless $schema;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema 0 0 0
217  
218 0 ▶ $id = [ $id ] unless ref($id) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $id eq "ARRAY" 0 0 0
219  
220 # deflating
221 0 my $id_hash = $schema->get_columns_hash_by_key_array_and_hash(+{}, $id);
222 0 $schema->deflate($id_hash);
223 0 $id = $schema->get_key_array_by_hash( $id_hash );
224  
225 Carp::confess 'The number of key is wrong'
226 0 ▶ unless scalar(@{ $id }) == scalar(@{ $schema->key });
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$id;} == scalar @{$schema->key;} 0 0 0
227  
228 0 my $data = $schema->{driver}->lookup( $schema, $id );
229 0 ▶ return unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
230  
231 0 my $obj = $data;
232 0 ▶ unless ($schema->{options}->{bare_row}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema->{"options"}{"bare_row"}) 0 0 0
233 0 $obj = $schema->new_obj($self, $data);
234 0 $schema->inflate($obj);
235 0 $schema->call_trigger('post_load', $obj);
236 }
237 0 return $obj;
238 }
239  
240 0 ▶sub lookup_multi {
Subroutine lookup_multi: not called CC=12 SCAR=50.5
Pod documented
241 0 my($self, $model, $ids) = @_;
242 0 ▶ Carp::croak "The 'lookup_multi' method can not be performed during a transaction." if $self->{active_transaction};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"active_transaction"} 0 0 0
243 0 my $schema = $self->get_schema($model);
244 0 ▶ return unless $schema;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema 0 0 0
245  
246 0 ▶ $ids = [ $ids ] unless ref($ids) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ids eq "ARRAY" 0 0 0
247 0 my $id_size = scalar(@{ $schema->key });
248 0 my @id_list;
249 0 for my $id (@{ $ids }) {
250 0 ▶ $id = [ $id ] unless ref($id) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $id eq "ARRAY" 0 0 0
251  
252 Carp::confess 'The number of key is wrong'
253 0 ▶ unless scalar(@{ $id }) == $id_size;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$id;} == $id_size 0 0 0
254  
255 # deflating
256 0 my $id_hash = $schema->get_columns_hash_by_key_array_and_hash(+{}, $id);
257 0 $schema->deflate($id_hash);
258 0 $id = $schema->get_key_array_by_hash( $id_hash );
259  
260 0 push @id_list, $id;
261 }
262  
263 0 my $results = $schema->{driver}->lookup_multi( $schema, \@id_list );
264 0 ▶ return (undef) x scalar(@id_list) unless $results && ref($results) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless $results and ref $results eq "HASH" 0 0 0
Condition0%0/3
$results and ref $results eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$results and ref $results eq "HASH"
$results ref $results eq "HASH"
Truth table0%0/3
A and B
A= $resultsB= ref $results eq "HASH"
ABresult
0X0
100
111
265  
266 0 ▶ while (my($id, $data) = each %{ $results }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$results;}) 0 0 0
267 0 my $obj = $data;
268 0 ▶ unless ($schema->{options}->{bare_row} || !$obj) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema->{"options"}{"bare_row"} or not $obj) 0 0 0
Condition0%0/3
$schema->{"options"}{"bare_row"} or not $obj
l!l&&r!l&&!r
000
MC/DC0%0/2
$schema->{"options"}{"bare_row"} or not $obj
$schema->{"options"}{"bare_row"} not $obj
Truth table0%0/3
A or not(B)
A= $schema->{"options"}{"bare_row"}B= not $obj
ABresult
1X1
011
000
269 0 $obj = $schema->new_obj($self, $data);
270 0 $schema->inflate($obj);
271 0 $schema->call_trigger('post_load', $obj);
272 }
273 0 $results->{$id} = $obj;
274 }
275  
276 0 map { $results->{join("\0", @{ $_ })} } @id_list;
277 }
278  
279  
280 # $model->get( model_name => 'key' );
281 # $model->get( model_name => [qw/ key1 key2 /] );
282 # $model->get( model_name => 'key' => { query options ... });
283 # $model->set( model_name => { search query, ... } );
284 3 ▶sub get {
Subroutine get: called CC=12 SCAR=29.4
Pod documented
285 3 my $self = shift;
286 3 ▶ Carp::croak "The 'get' method can not be performed during a transaction." if $self->{active_transaction};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"active_transaction"} 0 3 3
287 3 my $model = shift;
288 3 my $schema = $self->get_schema($model);
289 3 ▶ return unless $schema;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema 0 3 3
290  
291 3 my $query = $self->_get_query_args($schema, @_);
292 3 ▶ return if @_ && !@{ $query }; # undef key
Branch: 1 branch
BranchTrueFalseTotal
if @_ and not @{$query;} 0 3 3
Condition33%1/3
@_ and not @{$query;}
!ll&&!rl&&r
030
MC/DC0%0/2
@_ and not @{$query;}
@_ not @{$query;}
Truth table33%1/3
A and not(B)
A= @_B= not @{$query;}
ABresult
0X0
100
111
293 3 local $schema->{schema_obj} = $self;
294 3 my($iterator, $iterator_options) = $schema->{driver}->get( $schema, @{ $query } );
295 3 ▶ unless ($iterator) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($iterator) 1 2 3
296 1 ▶ return if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 1 0 1
297 0 return Data::Model::Iterator::Empty->new;
298 }
299  
300 2 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 2 0 2
301 2 my @objs = ();
302 2 ▶ while (my $data = $iterator->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = &$iterator()) 2 2 4
303 2 my $obj = $data;
304 2 ▶ unless ($schema->{options}->{bare_row}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema->{"options"}{"bare_row"}) 2 0 2
305 2 $obj = $schema->new_obj($self, $data);
306 2 $schema->inflate($obj);
307 2 $schema->call_trigger('post_load', $obj);
308 }
309 2 push @objs, $obj;
310 }
311 2 ▶ $iterator_options->{end}->() if exists $iterator_options->{end} && ref($iterator_options->{end}) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if exists $iterator_options->{"end"} and ref $iterator_options->{"end"} eq "CODE" 2 0 2
Condition33%1/3
exists $iterator_options->{"end"} and ref $iterator_options->{"end"} eq "CODE"
!ll&&!rl&&r
002
MC/DC0%0/2
exists $iterator_options->{"end"} and ref $iterator_options->{"end"} eq "CODE"
exists $iterator_options->{"end"} ref $iterator_options->{"end"} eq "CODE"
Truth table33%1/3
A and B
A= exists $iterator_options->{"end"}B= ref $iterator_options->{"end"} eq "CODE"
ABresult
0X0
100
111
312 2 return @objs;
313 }
314 return Data::Model::Iterator->new(
315 $iterator,
316 0 %{ $iterator_options },
317 0 ▶ wrapper => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
318 0 ▶ return shift if $schema->{options}->{bare_row};
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{"options"}{"bare_row"} 0 0 0
319 0 my $obj = $schema->new_obj($self, shift);
320 0 $schema->inflate($obj);
321 0 $schema->call_trigger('post_load', $obj);
322 0 $obj;
323 },
324 0 );
325 }
326  
327 0 ▶sub get_multi {
Subroutine get_multi: not called CC=2 SCAR=17.9
Pod undocumented
328 0 my $self = shift;
329 0 ▶ Carp::croak "The 'get_multi' method can not be performed during a transaction." if $self->{active_transaction};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"active_transaction"} 0 0 0
330 }
331  
332  
333 # $model->set( model_name => 'key' );
334 # $model->set( model_name => [qw/ key1 key2 /] );
335 # $model->set( model_name => 'key' => { column => 'value', ... });
336 # $model->set( model_name => [qw/ key1 key2 /] => { column => 'value', ... } );
337 # $model->set( model_name => { column => 'value', ... } );
338 2 ▶sub set {
Subroutine set: called CC=2 SCAR=7.2
Pod documented
339 2 ▶ Carp::croak "The 'set' method can not be performed during a transaction." if $_[0]->{active_transaction};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"active_transaction"} 0 2 2
340 2 shift->_insert_or_replace(0, @_);
341 }
342  
343 0 ▶sub replace {
Subroutine replace: not called CC=2 SCAR=17.9
Pod undocumented
344 0 ▶ Carp::croak "The 'replace' method can not be performed during a transaction." if $_[0]->{active_transaction};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"active_transaction"} 0 0 0
345 0 shift->_insert_or_replace(1, @_);
346 }
347  
348 2 ▶sub _insert_or_replace {
Subroutine _insert_or_replace: called CC=12 SCAR=28.5
349 2 my $self = shift;
350 2 my $is_replace = shift;
351 2 my $model = shift;
352 2 ▶ return $self->update($model, @_) if ref($model) && $model->isa('Data::Model::Row');
Branch: 1 branch
BranchTrueFalseTotal
if ref $model and $model->isa("Data::Model::Row") 0 2 2
Condition33%1/3
ref $model and $model->isa("Data::Model::Row")
!ll&&!rl&&r
200
MC/DC0%0/2
ref $model and $model->isa("Data::Model::Row")
ref $model $model->isa("Data::Model::Row")
Truth table33%1/3
A and B
A= ref $modelB= $model->isa("Data::Model::Row")
ABresult
0X0
100
111
353 2 my $schema = $self->get_schema($model);
354 2 ▶ return unless $schema;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema 0 2 2
355 # return unless exists $_[0];
356  
357 # get key array
358 2 my $key_array;
359 my $columns;
360 2 ▶ if (ref($_[0]) eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 2 2
elsif (ref $_[0] eq 'ARRAY') { } 0 2 2
elsif (not ref $_[0]) { } 2 0 2
361 ## ->set( modelname => { key => value, ... } );
362 0 $columns = shift;
363 0 $key_array = $schema->get_key_array_by_hash($columns);
364 } elsif (ref($_[0]) eq 'ARRAY') {
365 ## ->set( modelname => [ keys ] => { key => value, ... } );
366 0 $key_array = shift;
367 } elsif (!ref($_[0])) {
368 ## ->set( modelname => 'key' => { key => value, ... } );
369 2 $key_array = [ shift ];
370 } else {
371 # return;
372 }
373  
374 # get columns
375 2 ▶ if ($columns) {
Branch: 2 branches
BranchTrueFalseTotal
if ($columns) { } 0 2 2
elsif (ref $_[0] eq 'HASH') { } 0 2 2
376 ## nop
377 } elsif (ref($_[0]) eq 'HASH') {
378 ## get hash columns data
379 0 my $hash = shift;
380 0 $columns = $schema->get_columns_hash_by_key_array_and_hash($hash, $key_array);
381 } else {
382 2 $columns = $schema->get_columns_hash_by_key_array_and_hash(+{}, $key_array);
383 }
384  
385 # deflate
386 2 $schema->deflate($columns);
387 2 $key_array = $schema->get_key_array_by_hash( $columns );
388  
389 # triggers
390 2 $schema->call_trigger('pre_save', $columns);
391 2 $schema->set_default($columns); # set default
392 2 $schema->call_trigger('pre_insert', $columns);
393  
394 2 local $schema->{schema_obj} = $self;
395 2 ▶ my $method = $is_replace ? 'replace' : 'set';
Branch: 1 branch
BranchTrueFalseTotal
$is_replace ? : 0 2 2
396 2 my $result = $schema->{driver}->$method( $schema, $key_array => $columns, @_ );
397 2 ▶ return unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 2 2
398  
399 2 ▶ unless ($schema->{options}->{bare_row}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema->{"options"}{"bare_row"}) 2 0 2
400 2 my $obj = $schema->new_obj($self, $result);
401 2 $schema->inflate($obj);
402 2 $schema->call_trigger('post_load', $obj);
403 2 return $obj;
404 }
405 0 return $result;
406 }
407  
408 0 ▶sub set_multi {
Subroutine set_multi: not called CC=2 SCAR=17.9
Pod undocumented
409 0 my $self = shift;
410 0 ▶ Carp::croak "The 'set_multi' method can not be performed during a transaction." if $self->{active_transaction};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"active_transaction"} 0 0 0
411 }
412  
413  
414 0 ▶sub _get_schema_by_row {
Subroutine _get_schema_by_row: not called CC=4 SCAR=30.0
415 0 my($self, $row) = @_;
416  
417 0 my $class = ref($row);
418 0 ▶ return unless $class;
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 0 0
419  
420 0 my($klass, $model) = $class =~ /^(.+)::([^:]+)$/;
421 0 ▶ return unless (ref($self) || $self) eq $klass;
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self || $self) eq $klass 0 0 0
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
422 0 return $self->get_schema($model);
423 }
424  
425 0 ▶sub update {
Subroutine update: not called CC=9 SCAR=45.0
Pod documented
426 0 my $self = shift;
427 0 ▶ Carp::croak "The 'update' method can not be performed during a transaction." if $self->{active_transaction};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"active_transaction"} 0 0 0
428 0 my $row = shift;
429 0 ▶ return $self->update_direct($row, @_) unless ref($row) && $row->isa('Data::Model::Row');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $row and $row->isa("Data::Model::Row") 0 0 0
Condition0%0/3
ref $row and $row->isa("Data::Model::Row")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $row and $row->isa("Data::Model::Row")
ref $row $row->isa("Data::Model::Row")
Truth table0%0/3
A and B
A= ref $rowB= $row->isa("Data::Model::Row")
ABresult
0X0
100
111
430  
431 0 my $schema = $self->_get_schema_by_row($row);
432 0 ▶ return unless $schema;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema 0 0 0
433 0 ▶ return unless @{ $schema->{key} } > 0; # not has key
Branch: 1 branch
BranchTrueFalseTotal
unless @{$schema->{"key"};} > 0 0 0 0
434  
435 0 ▶ return unless scalar(%{ $row->get_changed_columns });
Branch: 1 branch
BranchTrueFalseTotal
unless scalar %{$row->get_changed_columns;} 0 0 0
436  
437 0 my $columns = $row->get_columns;
438 0 my $changed_columns = $row->get_changed_columns;
439 0 my $old_columns = +{ %{ $columns }, %{ $changed_columns } };
440  
441 0 ▶ if ($schema->has_deflate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->has_deflate) 0 0 0
442 # deflate
443 0 $schema->deflate($columns);
444 0 $schema->deflate($old_columns);
445 }
446  
447 0 $schema->call_trigger('pre_save', $columns);
448 0 $schema->call_trigger('pre_update', $columns, $old_columns);
449  
450 0 my $key_array = $schema->get_key_array_by_hash($columns);
451 0 my $old_key_array = $schema->get_key_array_by_hash($old_columns);
452  
453 my $result = $schema->{driver}->update(
454 0 $schema, $old_key_array, $key_array, $old_columns, $columns, $changed_columns, @_
455 );
456 0 $row->{changed_cols} = +{};
457 0 ▶ return unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
458  
459 0 $row;
460 }
461  
462  
463 # $model->update_direct( model_name => 'key', +{ querys }, +{ update columns } );
464 # $model->update_direct( model_name => [qw/ key1 key2 /], +{ querys }, +{ update columns } );
465 # $model->update_direct( model_name => +{ querys }, +{ update columns } );
466 # direct_update get しないで直接 updateする where の組み立ては get/delete と同じ
467 0 ▶sub update_direct {
Subroutine update_direct: not called CC=4 SCAR=30.0
Pod undocumented
468 0 my $self = shift;
469 0 ▶ Carp::croak "The 'update_direct' method can not be performed during a transaction." if $self->{active_transaction};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"active_transaction"} 0 0 0
470 0 my $model = shift;
471  
472 0 my $schema = $self->get_schema($model);
473 0 ▶ return unless $schema;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema 0 0 0
474  
475 0 my $query = $self->_get_query_args($schema, @_);
476 0 ▶ return unless @{ $query };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$query;} 0 0 0
477  
478 0 $schema->deflate($query->[2]);
479 0 $schema->call_trigger('pre_save', $query->[2]);
480 0 $schema->call_trigger('pre_update', $query->[2]);
481  
482 0 local $schema->{schema_obj} = $self;
483 0 $schema->{driver}->update_direct( $schema, @{ $query } );
484 }
485  
486  
487 # $model->delete( model_name => 'key' );
488 # $model->delete( model_name => [qw/ key1 key2 /] );
489 0 ▶sub delete {
Subroutine delete: not called CC=6 SCAR=37.4
Pod documented
490 0 my $self = shift;
491 0 ▶ Carp::croak "The 'delete' method can not be performed during a transaction." if $self->{active_transaction};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"active_transaction"} 0 0 0
492 0 my $row = shift;
493 0 ▶ return $self->delete_direct($row, @_) unless ref($row) && $row->isa('Data::Model::Row');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $row and $row->isa("Data::Model::Row") 0 0 0
Condition0%0/3
ref $row and $row->isa("Data::Model::Row")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $row and $row->isa("Data::Model::Row")
ref $row $row->isa("Data::Model::Row")
Truth table0%0/3
A and B
A= ref $rowB= $row->isa("Data::Model::Row")
ABresult
0X0
100
111
494  
495 0 my $schema = $self->_get_schema_by_row($row);
496 0 ▶ return unless $schema;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema 0 0 0
497 0 ▶ return unless @{ $schema->{key} } > 0; # not has key
Branch: 1 branch
BranchTrueFalseTotal
unless @{$schema->{"key"};} > 0 0 0 0
498  
499 0 my $columns = $row->get_columns;
500 0 my $key_array = $schema->get_key_array_by_hash($columns);
501  
502 0 local $schema->{schema_obj} = $self;
503 0 $schema->{driver}->delete( $schema, $key_array, @_ );
504 }
505  
506 0 ▶sub delete_direct {
Subroutine delete_direct: not called CC=4 SCAR=30.0
Pod undocumented
507 0 my $self = shift;
508 0 ▶ Carp::croak "The 'delete_direct' method can not be performed during a transaction." if $self->{active_transaction};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"active_transaction"} 0 0 0
509 0 my $model = shift;
510 0 my $schema = $self->get_schema($model);
511 0 ▶ return unless $schema;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema 0 0 0
512  
513 0 my $query = $self->_get_query_args($schema, @_);
514 0 ▶ return unless @{ $query };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$query;} 0 0 0
515  
516 0 local $schema->{schema_obj} = $self;
517 0 $schema->{driver}->delete( $schema, @{ $query } );
518 }
519  
520 0 ▶sub delete_multi {
Subroutine delete_multi: not called CC=2 SCAR=17.9
Pod undocumented
521 0 my $self = shift;
522 0 ▶ Carp::croak "The 'delete_multi' method can not be performed during a transaction." if $self->{active_transaction};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"active_transaction"} 0 0 0
523 }
524  
525  
526 # for transactions
527 0 ▶sub txn_scope {
Subroutine txn_scope: not called CC=2 SCAR=17.9
Pod undocumented
528 0 ▶ Carp::croak "The 'txn_scope' method can not be performed during a transaction." if $_[0]->{active_transaction};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"active_transaction"} 0 0 0
529 0 Data::Model::Transaction->new( @_ );
530 }
531  
532 0 ▶sub txn_begin {
Subroutine txn_begin: not called CC=3 SCAR=24.8
Pod undocumented
533 0 my $self = shift;
534 0 ▶ Carp::croak "The 'txn_begin' method can not be performed during a transaction." if $self->{active_transaction};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"active_transaction"} 0 0 0
535 0 my $driver = $self->get_base_driver;
536 0 ▶ Carp::croak 'You cannot use transaction, Because base_driver is not set by schema.' unless $driver;
Branch: 1 branch
BranchTrueFalseTotal
unless $driver 0 0 0
537 0 $self->{active_transaction} = 1;
538  
539 0 $driver->txn_begin;
540 }
541  
542 0 ▶sub txn_rollback {
Subroutine txn_rollback: not called CC=1 SCAR=6.9
Pod undocumented
543 0 my $self = shift;
544 0 my $driver = $self->get_base_driver;
545  
546 0 $driver->txn_rollback;
547 0 $self->txn_end;
548 0 1;
549 }
550  
551 0 ▶sub txn_commit {
Subroutine txn_commit: not called CC=1 SCAR=6.9
Pod undocumented
552 0 my $self = shift;
553 0 my $driver = $self->get_base_driver;
554  
555 0 $driver->txn_commit;
556 0 $self->txn_end;
557 0 1;
558 }
559  
560 0 ▶sub txn_end {
Subroutine txn_end: not called CC=1 SCAR=6.9
Pod undocumented
561 0 my $self = shift;
562 0 my $driver = $self->get_base_driver;
563 0 $self->{active_transaction} = 0;
564 0 $driver->txn_end;
565 }
566  
567  
568 4 1;
569 __END__
« Model/Schema/SQL.pm ↑ Summary Model/SQL.pm »