Karas.pm

filter: Statementstmt 55.3% 144 / 260 Branchbran 30.2% 26 / 86 Conditioncond 30.0% 12 / 40 MC/DCmcdc 4.3% 1 / 23 Subroutinesub 73.6% 28 / 38 Podpod 77.2% 17 / 22 totaltotal 48.6% 228 / 469 CC 62 SCAR 24.5
CC
62
Cov
47%
search_with_pager
42.0
load_plugin
30.0
bulk_insert
30.0
CRAP 11.6
« Karas/Plugin/Timestamp.pm ↑ Summary Karas/Row.pm »
1 package Karas;
2 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 our $VERSION = '0.07';
6 9 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Class::Accessor::Lite 0.05 (
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 rw => [qw/query_builder default_row_class owner_pid connection_manager row_class_map/],
9 9 );
10 9 ▶use Class::Trigger qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
11 BEFORE_INSERT
12  
13 BEFORE_REPLACE
14  
15 BEFORE_BULK_INSERT
16  
17 BEFORE_INSERT_ON_DUPLICATE
18  
19 BEFORE_UPDATE_ROW
20 AFTER_UPDATE_ROW
21 BEFORE_UPDATE_DIRECT
22 AFTER_UPDATE_DIRECT
23  
24 BEFORE_DELETE_ROW
25 BEFORE_DELETE_WHERE
26 AFTER_DELETE_ROW
27 AFTER_DELETE_DIRECT
28 9 );
29  
30 9 ▶use Module::Load ();
Subroutine BEGIN: called CC=1 SCAR=0.0
31 9 ▶use Data::Page::NoTotalEntries;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 9 ▶use DBIx::TransactionManager;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 9 ▶use DBIx::Handler;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 9 ▶use Karas::Row;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 9 ▶use Karas::QueryBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 10 ▶sub new {
Subroutine new: called CC=9 SCAR=25.1
Pod documented
40 10 my $class = shift;
41 0 ▶ my %args = @_==1 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 10 10
42 10 ▶ unless ($args{connect_info}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"connect_info"}) 0 10 10
43 0 Carp::croak("Missing mandatory parameter: connect_info");
44 }
45  
46 # ref. http://blog.nomadscafe.jp/2012/11/dbi-connect.html
47 10 ▶ $args{connect_info}->[3]->{RaiseError} //= 1;
Condition50%1/2
$args{"connect_info"}[3]{"RaiseError"} //= 1
l!l
010
MC/DC0%0/1
$args{"connect_info"}[3]{"RaiseError"} //= 1
$args{"connect_info"}[3]{"RaiseError"}
Truth table50%1/2
A
A= $args{"connect_info"}[3]{"RaiseError"}
Aresult
11
00
48 10 ▶ $args{connect_info}->[3]->{PrintError} //= 0;
Condition50%1/2
$args{"connect_info"}[3]{"PrintError"} //= 0
l!l
010
MC/DC0%0/1
$args{"connect_info"}[3]{"PrintError"} //= 0
$args{"connect_info"}[3]{"PrintError"}
Truth table50%1/2
A
A= $args{"connect_info"}[3]{"PrintError"}
Aresult
11
00
49 10 ▶ $args{connect_info}->[3]->{AutoCommit} //= 1;
Condition50%1/2
$args{"connect_info"}[3]{"AutoCommit"} //= 1
l!l
010
MC/DC0%0/1
$args{"connect_info"}[3]{"AutoCommit"} //= 1
$args{"connect_info"}[3]{"AutoCommit"}
Truth table50%1/2
A
A= $args{"connect_info"}[3]{"AutoCommit"}
Aresult
11
00
50 10 ▶ $args{connect_info}->[3]->{ShowErrorStatement} //= 1;
Condition50%1/2
$args{"connect_info"}[3]{"ShowErrorStatement"} //= 1
l!l
010
MC/DC0%0/1
$args{"connect_info"}[3]{"ShowErrorStatement"} //= 1
$args{"connect_info"}[3]{"ShowErrorStatement"}
Truth table50%1/2
A
A= $args{"connect_info"}[3]{"ShowErrorStatement"}
Aresult
11
00
51 10 ▶ $args{connect_info}->[3]->{AutoInactiveDestroy} //= 1;
Condition50%1/2
$args{"connect_info"}[3]{"AutoInactiveDestroy"} //= 1
l!l
010
MC/DC0%0/1
$args{"connect_info"}[3]{"AutoInactiveDestroy"} //= 1
$args{"connect_info"}[3]{"AutoInactiveDestroy"}
Truth table50%1/2
A
A= $args{"connect_info"}[3]{"AutoInactiveDestroy"}
Aresult
11
00
52  
53 $args{connection_manager} = DBIx::Handler->new(
54 10 @{$args{connect_info}}
55 );
56 10 my $self = bless {
57 row_class_map => {},
58 %args
59 }, $class;
60 10 ▶ $self->{query_builder} ||= Karas::QueryBuilder->new(driver => $self->_driver_name);
Condition33%1/3
$self->{"query_builder"} ||= "Karas::QueryBuilder"->new("driver", $self->_driver_name)
l!l&&r!l&&!r
0100
MC/DC0%0/2
$self->{"query_builder"} ||= "Karas::QueryBuilder"->new("driver", $self->_driver_name)
$self->{"query_builder"} "Karas::QueryBuilder"->new("driver", $self->_driver_name)
Truth table33%1/3
A ||= B
A= $self->{"query_builder"}B= "Karas::QueryBuilder"->new("driver", $self->_driver_name)
ABresult
1X1
011
000
61 10 return $self;
62 }
63  
64 23 ▶sub _driver_name {
Subroutine _driver_name: called CC=2 SCAR=7.1
65 23 my $self = shift;
66 23 ▶ $self->{driver_name} //= $self->dbh->{Driver}->{Name};
Condition66%2/3
$self->{"driver_name"} //= $self->dbh->{"Driver"}{"Name"}
l!l&&r!l&&!r
13100
MC/DC0%0/2
$self->{"driver_name"} //= $self->dbh->{"Driver"}{"Name"}
$self->{"driver_name"} $self->dbh->{"Driver"}{"Name"}
Truth table66%2/3
A //= B
A= $self->{"driver_name"}B= $self->dbh->{"Driver"}{"Name"}
ABresult
1X1
011
000
67 }
68  
69 # -------------------------------------------------------------------------
70 # Plugin
71 #
72 # -------------------------------------------------------------------------
73 0 ▶sub load_plugin {
Subroutine load_plugin: not called CC=5 SCAR=34.0
Pod documented
74 0 my ($class, $name, $args) = @_;
75 0 ▶ Carp::croak("Do not use this plugin to instance") if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 0 0
76 0 ▶ Carp::croak("Do not load this plugin to Karas itself. Please make your own child class from Karas.") if $class eq 'Karas';
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "Karas" 0 0 0
77  
78 0 ▶ $name = ($name =~ s/^\+//) ? $name : "Karas::Plugin::$name";
Branch: 1 branch
BranchTrueFalseTotal
$name =~ s/^\+// ? : 0 0 0
79 0 Module::Load::load($name);
80 0 ▶ my $plugin = $name->new($args || +{});
Condition0%0/2
$args || {}
l!l
00
MC/DC0%0/1
$args || {}
$args
Truth table0%0/2
A
A= $args
Aresult
11
00
81 0 $plugin->init($class);
82 }
83  
84 # -------------------------------------------------------------------------
85 # Connection
86 #
87 # -------------------------------------------------------------------------
88  
89 57 ▶sub dbh {
Subroutine dbh: called CC=2 SCAR=7.1
Pod documented
90 57 my $self = shift @_;
91 57 ▶ Carp::croak("Too many arguments for Karas#dbh") if @_!=0;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 0 0 57 57
92 57 return $self->connection_manager->dbh();
93 }
94  
95 1 ▶sub disconnect {
Subroutine disconnect: called CC=2 SCAR=7.1
Pod undocumented
96 1 my ($self) = @_;
97 1 ▶ Carp::croak("Too many arguments for Karas#disconnect") if @_!=1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 1 1
98 1 $self->connection_manager->disconnect();
99 }
100  
101 # -------------------------------------------------------------------------
102 # schema
103 #
104 # -------------------------------------------------------------------------
105  
106 0 ▶sub load_schema_from_db {
Subroutine load_schema_from_db: not called CC=4 SCAR=30.0
Pod undocumented
107 0 my ($self, %args) = @_;
108 0 require Karas::Loader;
109 0 ▶ my $class = 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
110 0 ▶ $args{namespace} //= do {
Condition0%0/3
$args{"namespace"} //= do { if ($class eq "Karas") { state $i = 0; $class . '::Anon' . $i++; } else { $class; } }
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"namespace"} //= do { if ($class eq "Karas") { state $i = 0; $class . '::Anon' . $i++; } else { $class; } }
$args{"namespace"} do { if ($class eq "Karas") { state $i = 0; $class . '::Anon' . $i++; } else { $class; } }
Truth table0%0/3
A //= B
A= $args{"namespace"}B= do { if ($class eq "Karas") { state $i = 0; $class . '::Anon' . $i++; } else { $class; } }
ABresult
1X1
011
000
111 0 ▶ if ($class eq 'Karas') {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq 'Karas') { } 0 0 0
112 0 state $i=0;
113 0 $class . '::Anon' . $i++;
114 } else {
115 0 $class;
116 }
117 };
118 0 $self->{row_class_map} = Karas::Loader->load(
119 dbh => $self->dbh,
120 %args
121 );
122 0 return undef;
123 }
124  
125 23 ▶sub get_row_class {
Subroutine get_row_class: called CC=3 SCAR=11.4
Pod documented
126 23 my ($self, $table_name) = @_;
127 23 ▶ Carp::croak("Missing mandatory parameter: table_name") unless $table_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $table_name 0 23 23
128 23 ▶ my $row_class = $self->row_class_map->{$table_name}
Branch: 1 branch
BranchTrueFalseTotal
my $row_class = $self->row_class_map->{$table_name} or Carp::croak("Unknown table: $table_name. $table_name is not registered to Karas.") 23 0 23
129 or Carp::croak("Unknown table: $table_name. $table_name is not registered to Karas.");
130 23 return $row_class;
131 }
132  
133 # -------------------------------------------------------------------------
134 # SQL
135 #
136 # -------------------------------------------------------------------------
137  
138 7 ▶sub search {
Subroutine search: called CC=3 SCAR=11.0
Pod documented
139 7 my ($self, $table, $where, $opt) = @_;
140 7 ▶ $opt->{cols} ||= [\'*'];
Condition50%1/2
$opt->{"cols"} ||= [\"*"]
l!l
07
MC/DC0%0/1
$opt->{"cols"} ||= [\"*"]
$opt->{"cols"}
Truth table50%1/2
A
A= $opt->{"cols"}
Aresult
11
00
141 7 my ($sql, @binds) = $self->query_builder->select($table, $opt->{cols}, $where, $opt);
142 7 my $sth = $self->dbh->prepare($sql);
143 7 $sth->execute(@binds);
144 7 my $row_class = $self->get_row_class($table);
145 7 my @rows;
146 7 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 7 7 14
147 7 push @rows, $row_class->new($row);
148 }
149 7 return @rows;
150 }
151  
152 3 ▶sub count {
Subroutine count: called CC=1 SCAR=0.0
Pod documented
153 3 my ($self, $table, $where) = @_;
154 3 my ($sql, @binds) = $self->query_builder->select($table, [\'COUNT(*)'], $where);
155 3 my $sth = $self->dbh->prepare($sql);
156 3 $sth->execute(@binds);
157 3 my ($count) = $sth->fetchrow_array();
158 3 return $count;
159 }
160  
161 0 ▶sub search_with_pager {
Subroutine search_with_pager: not called CC=6 SCAR=37.4
Pod documented
162 0 my ($self, $table, $where, $opt) = @_;
163 0 ▶ $opt->{cols} ||= [\'*'];
Condition0%0/2
$opt->{"cols"} ||= [\"*"]
l!l
00
MC/DC0%0/1
$opt->{"cols"} ||= [\"*"]
$opt->{"cols"}
Truth table0%0/2
A
A= $opt->{"cols"}
Aresult
11
00
164 0 ▶ my $page = delete $opt->{page} // Carp::croak("Missing mandatory parameter: page");
Condition0%0/2
delete $opt->{"page"} // Carp::croak("Missing mandatory parameter: page")
l!l
00
MC/DC0%0/1
delete $opt->{"page"} // Carp::croak("Missing mandatory parameter: page")
delete $opt->{"page"}
Truth table0%0/2
A
A= delete $opt->{"page"}
Aresult
11
00
165 0 ▶ my $rows = delete $opt->{rows} // Carp::croak("Missing mandatory parameter: rows");
Condition0%0/2
delete $opt->{"rows"} // Carp::croak("Missing mandatory parameter: rows")
l!l
00
MC/DC0%0/1
delete $opt->{"rows"} // Carp::croak("Missing mandatory parameter: rows")
delete $opt->{"rows"}
Truth table0%0/2
A
A= delete $opt->{"rows"}
Aresult
11
00
166 0 $opt->{limit} = $rows+1;
167 0 $opt->{offset} = $rows*($page-1);
168 0 my ($sql, @binds) = $self->query_builder->select($table, $opt->{cols}, $where, $opt);
169 0 my $sth = $self->dbh->prepare($sql);
170 0 $sth->execute(@binds);
171 0 my $row_class = $self->get_row_class($table);
172 0 my @rows;
173 0 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
174 0 push @rows, $row_class->new($row);
175 }
176 0 my $has_next = 0;
177 0 ▶ if (@rows == $rows+1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rows == $rows + 1) 0 0 0
178 0 pop @rows;
179 0 $has_next = 1;
180 }
181 0 my $pager = Data::Page::NoTotalEntries->new(
182 has_next => $has_next,
183 entries_per_page => $rows,
184 current_page => $page,
185 entries_on_this_page => 0+@rows,
186 );
187 0 return (\@rows, $pager);
188 }
189  
190 0 ▶sub search_by_sql {
Subroutine search_by_sql: not called CC=4 SCAR=30.0
Pod documented
191 0 my ($self, $sql, $binds, $table_name) = @_;
192 0 my $sth = $self->dbh->prepare($sql);
193 0 $sth->execute(@$binds);
194 0 ▶ $table_name //= $self->guess_table_name($sql);
Condition0%0/3
$table_name //= $self->guess_table_name($sql)
l!l&&r!l&&!r
000
MC/DC0%0/2
$table_name //= $self->guess_table_name($sql)
$table_name $self->guess_table_name($sql)
Truth table0%0/3
A //= B
A= $table_nameB= $self->guess_table_name($sql)
ABresult
1X1
011
000
195 0 ▶ unless ($table_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($table_name) 0 0 0
196 0 Carp::croak("Cannot guess table name from SQL. You need to pass the table name: " . $sql);
197 }
198 0 my $row_class = $self->get_row_class($table_name);
199 0 my @rows;
200 0 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
201 0 push @rows, $row_class->new($row);
202 }
203 0 return @rows;
204 }
205  
206 11 ▶sub insert {
Subroutine insert: called CC=5 SCAR=16.3
Pod documented
207 11 my ($self, $table, $values) = @_;
208 11 ▶ Carp::croak("Missing mandatory parameter: table") unless defined $table;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table 0 11 11
209 11 ▶ Carp::croak("Missing mandatory parameter: values") unless defined $values;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $values 0 11 11
210 11 $self->_insert($table, $values);
211  
212 # and select it.
213 11 my $row_class = $self->get_row_class($table);
214 11 my $last_insert_id = $self->last_insert_id;
215 11 my @pk = $row_class->primary_key;
216 11 ▶ if (@pk == 1 && defined($last_insert_id)) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pk == 1 and defined $last_insert_id) 4 7 11
Condition66%2/3
@pk == 1 and defined $last_insert_id
!ll&&!rl&&r
704
MC/DC50%1/2
@pk == 1 and defined $last_insert_id
@pk == 1 defined $last_insert_id
Truth table66%2/3
A and B
A= @pk == 1B= defined $last_insert_id
ABresult
0X0
100
111
217 4 return(($self->search($table, {$pk[0] => $last_insert_id}))[0]);
218 }
219  
220 # cannot select row. just create new object from arguments.
221 7 return $row_class->new($values);
222 }
223  
224 0 ▶sub fast_insert {
Subroutine fast_insert: not called CC=3 SCAR=24.8
Pod documented
225 0 my ($self, $table, $values) = @_;
226 0 ▶ Carp::croak("Missing mandatory parameter: table") unless defined $table;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table 0 0 0
227 0 ▶ Carp::croak("Missing mandatory parameter: values") unless defined $values;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $values 0 0 0
228 0 $self->_insert($table, $values);
229 0 return $self->last_insert_id;
230 }
231  
232 11 ▶sub _insert {
Subroutine _insert: called CC=1 SCAR=0.0
233 11 my ($self, $table, $values) = @_;
234 11 $self->call_trigger(BEFORE_INSERT => $table, $values);
235 11 my ($sql, @binds) = $self->query_builder->insert($table, $values);
236 11 my $sth = $self->dbh->prepare($sql);
237 11 $sth->execute(@binds);
238 11 return undef;
239 }
240  
241 2 ▶sub replace {
Subroutine replace: called CC=1 SCAR=0.0
Pod documented
242 2 my ($self, $table, $values) = @_;
243 2 $self->call_trigger(BEFORE_REPLACE => $table, $values);
244 2 my ($sql, @binds) = $self->query_builder->insert($table, $values, {prefix => 'REPLACE INTO'});
245 2 my $sth = $self->dbh->prepare($sql);
246 2 $sth->execute(@binds);
247 2 my $last_insert_id = $self->last_insert_id;
248 2 return $last_insert_id;
249 }
250  
251 4 ▶sub retrieve {
Subroutine retrieve: called CC=7 SCAR=20.6
Pod undocumented
252 4 my ($self, $table, $vals) = @_;
253 4 ▶ Carp::croak("Missing mandatory parameter: table") unless defined $table;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table 0 4 4
254 4 ▶ Carp::croak("Too many arguments") if @_ > 3;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 3 0 4 4
255  
256 4 my $row_class = $self->get_row_class($table);
257 4 my %where;
258 4 ▶ if (ref $vals eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $vals eq 'HASH') { } 3 1 4
elsif (ref $vals) { } 0 1 1
259 3 %where = %$vals;
260 } elsif (ref $vals) {
261 0 Carp::croak("Bad arguments for retrieve: $vals");
262 } else {
263 1 my @pk = $row_class->primary_key;
264 1 ▶ if (@pk != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pk != 1) 0 1 1
265 0 Carp::croak(sprintf("%s has %d primary keys, but you passed %d(%s)", $table, 0+@pk, 1, join(', ', @pk)));
266 }
267 1 $where{$pk[0]} = $vals;
268 }
269 4 my ($sql, @binds) = $self->query_builder->select($table, [\'*'], \%where);
270 4 my $sth = $self->dbh->prepare($sql);
271 4 $sth->execute(@binds);
272 4 my $row = $sth->fetchrow_hashref;
273 4 ▶ if ($row) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row) { } 4 0 4
274 4 return $row_class->new($row);
275 } else {
276 0 return undef;
277 }
278 }
279  
280 1 ▶sub update {
Subroutine update: called CC=5 SCAR=20.9
Pod documented
281 1 my $self = shift;
282 1 ▶ if (UNIVERSAL::isa($_[0], 'Karas::Row')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($_[0], 'Karas::Row')) { } 1 0 1
283 1 my ($row, $set) = @_;
284 1 ▶ $set ||= +{};
Condition50%1/2
$set ||= {}
l!l
01
MC/DC0%0/1
$set ||= {}
$set
Truth table50%1/2
A
A= $set
Aresult
11
00
285 1 $set = +{ %{$row->get_dirty_columns()}, %$set };
286 1 my $where = $row->make_where_condition();
287 1 $self->call_trigger(BEFORE_UPDATE_ROW => $row, $set);
288 1 my $rows = $self->_update($row->table_name, $set, $where);
289 1 $self->call_trigger(AFTER_UPDATE_ROW => $row, $set);
290 1 return $rows;
291 } else {
292 0 my ($table_name, $set, $where) = @_;
293 0 ▶ Carp::croak("Usage: \$db->update(\$table_name, \%set, \%where)") if ref $table_name;
Branch: 1 branch
BranchTrueFalseTotal
if ref $table_name 0 0 0
294 0 ▶ Carp::croak("Usage: \$db->update(\$table_name, \%set, \%where)") if @_!=3;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 3 0 0 0
295 0 $self->call_trigger(BEFORE_UPDATE_DIRECT => $table_name, $set, $where);
296 0 my $rows = $self->_update($table_name, $set, $where);
297 0 $self->call_trigger(AFTER_UPDATE_DIRECT => $table_name, $set, $where);
298 0 return $rows;
299 }
300 }
301  
302 1 ▶sub _update {
Subroutine _update: called CC=3 SCAR=11.2
303 1 my ($self, $table, $set, $where) = @_;
304 1 ▶ Carp::croak("Missing mandatory parameter: table") unless defined $table;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table 0 1 1
305 1 ▶ Carp::croak("Missing mandatory parameter: set") unless defined $set;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $set 0 1 1
306 1 my ($sql, @binds) = $self->query_builder->update($table, $set, $where);
307 1 my $sth = $self->dbh->prepare($sql);
308 1 $sth->execute(@binds);
309 1 return $sth->rows;
310 }
311  
312 0 ▶sub delete {
Subroutine delete: not called CC=2 SCAR=17.9
Pod documented
313 0 my $self = shift;
314 0 ▶ if (UNIVERSAL::isa($_[0], 'Karas::Row')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($_[0], 'Karas::Row')) { } 0 0 0
315 0 my ($row) = @_;
316 0 $self->call_trigger(BEFORE_DELETE_ROW => $row);
317 0 my $where = $row->make_where_condition();
318 0 my $retval = $self->_delete($row->table_name, $row->where);
319 0 $self->call_trigger(AFTER_DELETE_ROW => $row);
320 0 return $retval;
321 } else {
322 0 my ($table_name, $where);
323 0 $self->call_trigger(BEFORE_DELETE_DIRECT => $table_name, $where);
324 0 my $rows = $self->_delete($table_name, $where);
325 0 $self->call_trigger(AFTER_DELETE_DIRECT => $table_name, $where);
326 0 return $rows;
327 }
328 }
329  
330 0 ▶sub _delete {
Subroutine _delete: not called CC=3 SCAR=24.8
331 0 my ($self, $table, $where) = @_;
332 0 ▶ Carp::croak("Missing mandatory parameter: table") unless defined $table;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table 0 0 0
333 0 ▶ Carp::croak("Missing mandatory parameter: where") unless defined $where;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $where 0 0 0
334 0 my ($sql, @binds) = $self->query_builder->delete($table, $where);
335 0 my $sth = $self->dbh->prepare($sql);
336 0 $sth->execute(@binds);
337 0 return $sth->rows;
338 }
339  
340 1 ▶sub refetch {
Subroutine refetch: called CC=1 SCAR=0.0
Pod documented
341 1 my ($self, $row) = @_;
342 1 return ($self->search($row->table_name, $row->make_where_condition()))[0];
343 }
344  
345 0 ▶sub bulk_insert {
Subroutine bulk_insert: not called CC=5 SCAR=34.0
Pod documented
346 0 my ($self, $table_name, $rows_data) = @_;
347 0 ▶ Carp::croak("Missing mandatory parameter: table_name") unless defined $table_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table_name 0 0 0
348 0 ▶ Carp::croak("rows_data must be ArrayRef") unless ref $rows_data eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rows_data eq "ARRAY" 0 0 0
349  
350 0 ▶ if ($self->_driver_name eq 'mysql') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_driver_name eq 'mysql') { } 0 0 0
351 0 $self->call_trigger(BEFORE_BULK_INSERT => $table_name, $rows_data);
352 0 my ($sql, @binds) = $self->query_builder->insert_multi($table_name, $rows_data);
353 0 my $sth = $self->dbh->prepare($sql);
354 0 $sth->execute(@binds);
355 0 return $sth->rows;
356 } else {
357 # emulate bulk insert.
358 0 $self->call_trigger(BEFORE_BULK_INSERT => $table_name, $rows_data);
359 0 my $txn = $self->txn_scope();
360 {
361 # Do not run 'BEFORE_INSERT' hook for consistency between mysql.
362 0 for my $row (@$rows_data) {
363 0 my ($sql, @binds) = $self->query_builder->insert($table_name, $row);
364 0 my $sth = $self->dbh->prepare($sql);
365 0 $sth->execute(@binds);
366 }
367 }
368 0 $txn->commit;
369 }
370 }
371  
372 0 ▶sub insert_on_duplicate {
Subroutine insert_on_duplicate: not called CC=2 SCAR=17.9
Pod undocumented
373 0 my ($self, $table_name, $insert_values, $update_values) = @_;
374 0 ▶ if ($self->_driver_name eq 'mysql') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_driver_name eq 'mysql') { } 0 0 0
375 0 $self->call_trigger(BEFORE_INSERT_ON_DUPLICATE => $table_name, $insert_values, $update_values);
376 0 my ($sql, @binds) = $self->query_builder->insert_on_duplicate($table_name, $insert_values, $update_values);
377 0 my $sth = $self->dbh->prepare($sql);
378 0 $sth->execute(@binds);
379 } else {
380 0 Carp::croak("'insert_on_duplicate' method only supports mysql: " . $self->_driver_name);
381 }
382  
383 0 return undef;
384 }
385  
386 # taken from teng.
387 2 ▶sub guess_table_name {
Subroutine guess_table_name: called CC=2 SCAR=7.6
Pod undocumented
388 2 my ( $class, $sql ) = @_;
389  
390 2 ▶ if ( $sql =~ /\sfrom\s+["`]?([\w]+)["`]?\s*/si ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sql =~ /\sfrom\s+["`]?([\w]+)["`]?\s*/is) 2 0 2
391 2 return $1;
392 }
393 0 return undef;
394 }
395  
396 # -------------------------------------------------------------------------
397 # transaction
398 #
399 # -------------------------------------------------------------------------
400  
401 0 ▶sub txn_scope {
Subroutine txn_scope: not called CC=1 SCAR=6.9
Pod documented
402 0 my ($self) = @_;
403 0 return $self->connection_manager->txn_scope;
404 }
405  
406 # taken from Teng
407 13 ▶sub last_insert_id {
Subroutine last_insert_id: called CC=5 SCAR=22.5
Pod documented
408 13 my ( $self, $table_name ) = @_;
409  
410 13 my $driver = $self->_driver_name;
411 13 ▶ if ( $driver eq 'mysql' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($driver eq 'mysql') { } 0 13 13
elsif ($driver eq 'Pg') { } 0 13 13
elsif ($driver eq 'SQLite') { } 13 0 13
elsif ($driver eq 'Oracle') { } 0 0 0
412 0 return $self->dbh->{mysql_insertid};
413 }
414 elsif ( $driver eq 'Pg' ) {
415 0 return $self->dbh->last_insert_id( undef, undef, undef, undef, { sequence => join( '_', $table_name, 'id', 'seq' ) } );
416 }
417 elsif ( $driver eq 'SQLite' ) {
418 13 return $self->dbh->func('last_insert_rowid');
419 }
420 elsif ( $driver eq 'Oracle' ) {
421 0 return undef;
422 }
423 else {
424 0 Carp::croak "Don't know how to get last insert id for $driver";
425 }
426 }
427  
428 9 1;
429 __END__
« Karas/Plugin/Timestamp.pm ↑ Summary Karas/Row.pm »