Cake/DBI/Class.pm untested

filter: Statementstmt 0.0% 0 / 151 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 23 totaltotal 0.0% 0 / 253 CC 18 SCAR 33.2
CC
18
Cov
0%
_dbi
110.0
delete
12.0
total
12.0
CRAP 27.8
1 package Cake::DBI::Class;
2 use strict;
3 use warnings;
4 use Cake::DBI::Abstract;
5 use DBI;
6 use POSIX 'ceil';
7 use Carp;
8
9
10 sub new {
11 my $class = shift;
12 my $self = {};
13 $self->{_connect} = \@_;
14 $self->{sql} = Cake::DBI::Abstract->new();
15 return bless ($self,$class);
16 }
17
18 sub _connect {
19 my $self = shift;
20 $self->{dbh} = DBI->connect(@{$self->{_connect}});
21 return $self->{dbh};
22 }
23
24 sub sql { return shift->{sql}; }
25
26 sub dbh {
27 my $self = shift;
28 return $self->{dbh} || $self->_connect();
29 }
30
31 sub commit {
32 return shift->dbh->commit;
33 }
34
35 ##turn auto-commit off while working
36 sub begin_work {
37 my $self = shift;
38 $self->dbh->begin_work;
39 }
40
41 sub _dbi {
42 my ($self,$query,$values,$type) = @_;
43 my (@rows,$rows);
44 my $dbh = $self->dbh;
45
46 ##prepare sql statement
47 my $sth = $dbh->prepare($query);
48
49 ##set error handler
50 $dbh->{HandleError} = sub {
51 my $message = shift;
52 croak $message;
53 };
54
55 if ($type eq 'count'){
56
57 $sth->execute( @{$values} );
58 my ($total_count) = $sth->fetchrow;
59 $sth->finish();
60 return $total_count;
61 }
62
63 elsif ($type eq 'create'){
64 my $num = $sth->execute( @{$values} );
65 my $id = $dbh->{ q{mysql_insertid}};
66
67 if (!$id){
68 $id = $dbh->last_insert_id(undef, undef, undef, undef);
69 }
70
71 $sth->finish();
72 return $id;
73 }
74
75 elsif ($type eq 'update'){
76 my $num = $sth->execute( @{$values} );
77 $sth->finish();
78 return $num;
79 }
80
81 elsif ($type eq 'delete'){
82 my $num = $sth->execute( @{$values} );
83 $sth->finish();
84 return $num;
85 }
86
87 elsif ($type eq 'all'){
88 $rows = $dbh->selectall_arrayref($sth,{ Slice => {} }, @{$values});
89 $self->{cached_results} = $rows;
90 return wantarray ? @{$rows} : $rows;
91 }
92
93 elsif ($type eq 'single') {
94 my $hash_ref = $dbh->selectrow_hashref($sth,undef, @{$values});
95 return $hash_ref;
96 }
97
98 else {
99 $rows = $dbh->selectall_arrayref($sth,{ Slice => {} }, @{$values});
100 $self->{cached_results} = $rows;
101 return wantarray ? @{$rows} : $rows;
102 }
103
104 return @rows;
105 }
106
107
108 sub DESTROY {
109 my $self = shift;
110 if (my $dbh = $self->{dbh}) {
111 %{ $dbh->{CachedKids} } = ();
112 $dbh->disconnect;
113 $self->{_connect} = undef;
114 }
115 }
116
117 *find = \&search;
118 sub search {
119 my $self = shift;
120 $self->sql->where(@_);
121 return $self;
122 }
123
124 *tables = \&table;
125 sub table {
126 my $self = shift;
127 $self->sql->from(@_);
128 return $self;
129 }
130
131 sub columns {
132 my $self = shift;
133 $self->sql->select(@_);
134 return $self;
135 }
136
137 sub all {
138 my $self = shift;
139 my ($sql,@binds) = $self->sql->query;
140 return $self->_dbi($sql,\@binds,'all');
141 }
142
143 sub single {
144 my $self = shift;
145 my ($sql,@binds) = $self->sql->query;
146 return $self->_dbi($sql,\@binds,'single');
147 }
148
149 sub first {
150 my $self = shift;
151 return $self->next;
152 }
153
154 sub next {
155 my $self = shift;
156 if (my $cache = $self->{cached_results}){
157 $self->{cache_position} ||= 0;
158 return $cache->[$self->{cache_position}++];
159 }
160
161 my @results = $self->all;
162 my ($row,@rows) = @results;
163 $self->{cached_results} = \@rows;
164 return $row;
165 }
166
167 sub create {
168 my $self = shift;
169 my ($sql,@binds) = $self->sql->create(@_);
170 return $self->_dbi($sql,\@binds,'create');
171 }
172
173 sub delete {
174 my $self = shift;
175 my $args = shift;
176 if ($args){
177 croak "Delete Only accept HASH ref" if ref $args ne 'HASH';
178 $self->sql->where($args);
179 }
180 my ($sql,@binds) = $self->sql->delete('DELETE');
181 return $self->_dbi($sql,\@binds,'delete');
182 }
183
184 sub update {
185 my $self = shift;
186 my ($sql,@binds) = $self->sql->update(@_);
187 return $self->_dbi($sql,\@binds,'update');
188 }
189
190
191 sub literal {
192 my $self = shift;
193 $self->sql->literal(@_);
194 return $self;
195 #return $self->_dbi($sql,$binds,'all');
196 }
197
198
199 sub total {
200 my $self = shift;
201 my $total;
202 my $limit = $self->sql->limit;
203 return $self->{_total} if $self->{_total};
204
205 ###if no limits then return cached numbers
206 if (!$limit && $self->{cached_results}){
207 $total = scalar @{$self->{cached_results}};
208 } else {
209 my ($sql,@binds) = $self->sql->count();
210 $total = $self->_dbi($sql,\@binds,'count');
211 }
212
213 $self->{_total} = $total;
214 return $total;
215 }
216
217 sub limit {
218 my $self = shift;
219 $self->sql->limit(@_);
220 return $self;
221 }
222
223 sub order {
224 my $self = shift;
225 $self->sql->order(@_);
226 return $self;
227 }
228
229 sub pages {
230 my $self = shift;
231 my $total = $self->total;
232 my $rows = $self->sql->{limit}->{rows} || undef;
233 if ($rows){
234 my $total_pages = $total < $rows ? 1 : POSIX::ceil($total / $rows);
235 return $total_pages;
236 } else {
237 return 1;
238 }
239 }
240
241 1;
242
243
244 __END__