PObject/Driver/DBI.pm untested

filter: Statementstmt 0.0% 0 / 169 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 34 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 293 CC 19 SCAR 23.8
CC
19
Cov
0%
_prepare_create_table
30.0
_prepare_select
20.0
_prepare_where_clause
12.0
CRAP 10.8
1 package Class::PObject::Driver::DBI;
2  
3 # DBI.pm,v 1.13 2003/11/07 04:51:04 sherzodr Exp
4  
5 use strict;
6 #use diagnostics;
7 use Carp;
8 use Log::Agent;
9 use Class::PObject::Driver;
10 use vars ('$VERSION', '@ISA');
11  
12 @ISA = ('Class::PObject::Driver');
13  
14 $VERSION = '2.02';
15  
16  
17  
18 sub _prepare_create_table {
19 my ($self, $object_name, $tablename) = @_;
20  
21 my $props = $object_name->__props();
22 my @cols = ();
23 for my $column ( @{ $props->{columns} } ) {
24 my $type = $props->{tmap}->{$column};
25 if ( $type eq "MD5" ) {
26 $type = "CHAR(32)";
27 } elsif ( $type eq "ENCRYPT" ) {
28 $type = "CHAR(18)";
29 } else {
30 unless ( $type =~ m/^(CHAR|VARCHAR|INTEGER|TEXT|BLOB)(\([^\)]+\))?$/ ) {
31 logtrc 3, "%s is %s", $column, $type;
32 $type = "VARCHAR(255)"
33 }
34 }
35 if ( $column eq 'id' ) {
36 push @cols, "id INTEGER PRIMARY KEY NOT NULL";
37 } else {
38 push @cols, sprintf "%s %s NULL", $column, $type;
39 }
40 }
41 my $sql = sprintf "\nCREATE TABLE %s (\n\t%s\n)", $tablename, join ",\n\t", @cols;
42 logtrc 4, $sql;
43 return $sql
44 }
45  
46  
47  
48 sub _prepare_where_clause {
49 my ($self, $terms) = @_;
50  
51 $terms ||= {};
52 unless ( ref $terms ) {
53 die join ', ', caller(0)
54 }
55 # if no terms present, just return an empty string
56 unless ( keys %$terms ) {
57 return ("", ());
58 }
59 my ($sql, @where, @bind_params);
60 while ( my ($k, $v) = each %$terms ) {
61 push @where, "$k=?";
62 push @bind_params, $v
63 }
64 $sql = "WHERE " . join (" AND ", @where);
65 return ($sql, \@bind_params)
66 }
67  
68  
69  
70 sub _prepare_select {
71 my ($self, $table_name, $terms, $args, $cols) = @_;
72  
73 my ($sql, @where, @bind_params, $selected_cols);
74 my ($where_clause, $bind_params) = $self->_prepare_where_clause($terms);
75 $selected_cols = $cols ? join (", ", @$cols) : "*";
76 $sql = sprintf("SELECT %s FROM %s %s", $selected_cols, $table_name, $where_clause);
77 if ( defined $args ) {
78 $args->{limit} ||= 1000;
79 $args->{offset} ||= 0;
80 if ( $args->{'sort'} ) {
81 $args->{direction} ||= 'asc';
82 $sql .= sprintf(" ORDER BY %s %s", $args->{'sort'}, $args->{direction})
83 }
84 $sql .= sprintf(" LIMIT %d, %d", $args->{offset}, $args->{limit})
85 }
86 return ($sql, $bind_params)
87 }
88  
89  
90  
91 sub _prepare_delete {
92 my ($self, $table_name, $terms) = @_;
93  
94 my ($sql, @where, @bind_params);
95 $sql = "DELETE FROM $table_name ";
96  
97 my ($where_clause, $bind_params) = $self->_prepare_where_clause($terms);
98 $sql .= $where_clause;
99  
100 return ($sql, $bind_params)
101 }
102  
103  
104  
105 sub _prepare_insert {
106 my ($self, $table_name, $columns) = @_;
107  
108 my ($sql, @fields, @values, @bind_params);
109 $sql = "INSERT INTO $table_name";
110  
111 while ( my ($k, $v) = each %$columns ) {
112 push @fields, $k;
113 push @values, '?';
114 push @bind_params, $v
115 }
116  
117 $sql .= sprintf(" (%s) VALUES(%s)", join(", ", @fields), join(", ", @values) );
118 return ($sql, \@bind_params)
119 }
120  
121  
122  
123 sub _prepare_update {
124 my ($self, $table_name, $columns, $terms) = @_;
125  
126 my ($sql, @fields, @bind_params);
127 $sql = "UPDATE $table_name SET ";
128  
129 while ( my ($k, $v) = each %$columns ) {
130 push @fields, "$k=?";
131 push @bind_params, $v
132 }
133  
134 $sql .= join (", ", @fields);
135  
136 my ($where_clause, $where_params) = $self->_prepare_where_clause($terms);
137 $sql .= " " . $where_clause;
138  
139 return ($sql, [@bind_params, @$where_params])
140 }
141  
142  
143  
144 sub _tablename {
145 my ($self, $object_name, $props, $dbh) = @_;
146  
147 if ( defined $props->{datasource}->{Table} ) {
148 return $props->{datasource}->{Table}
149 }
150  
151 my $table_name = lc $object_name;
152 $table_name =~ s/\W+/_/g;
153  
154 return $table_name
155 }
156  
157  
158  
159 sub load {
160 my $self = shift;
161 my ($object_name, $props, $id) = @_;
162  
163 my $dbh = $self->dbh($object_name, $props) or return;
164 my $table = $self->_tablename($object_name, $props, $dbh) or return;
165 my ($sql, $bind_params) = $self->_prepare_select($table, {id=>$id});
166  
167 my $sth = $dbh->prepare( $sql );
168 unless ( $sth->execute( @$bind_params ) ) {
169 $self->errstr($sth->errstr);
170 return undef
171 }
172  
173 return $sth->fetchrow_hashref
174 }
175  
176  
177  
178 sub load_ids {
179 my $self = shift;
180 my ($object_name, $props, $terms, $args) = @_;
181  
182 my $dbh = $self->dbh($object_name, $props) or return;
183 my $table = $self->_tablename($object_name, $props, $dbh) or return;
184 my ($sql, $bind_params) = $self->_prepare_select($table, $terms, $args, ['id']);
185  
186  
187 my $sth = $dbh->prepare( $sql );
188  
189 unless( $sth->execute(@$bind_params) ) {
190 $self->errstr($sth->errstr);
191 return undef
192 }
193  
194 unless ( $sth->rows ) {
195 return []
196 }
197  
198 my @data_set = ();
199 while ( my $row = $sth->fetchrow_hashref() ) {
200 push @data_set, $row->{id}
201 }
202 return \@data_set
203 }
204  
205  
206  
207 sub remove {
208 my $self = shift;
209 my ($object_name, $props, $id) = @_;
210  
211 unless ( defined $id ) {
212 $self->errstr("remove(): don't know what to remove. 'id' is missing");
213 return undef
214 }
215  
216 my $dbh = $self->dbh($object_name, $props) or return;
217 my $table = $self->_tablename($object_name, $props, $dbh) or return;
218 my ($sql, $bind_params) = $self->_prepare_delete($table, {id=>$id});
219  
220 my $sth = $dbh->prepare( $sql );
221 unless ( $sth->execute($id) ) {
222 $self->errstr($sth->errstr);
223 return undef
224 }
225 return $id
226 }
227  
228  
229  
230 sub remove_all {
231 my $self = shift;
232 my ($object_name, $props, $terms) = @_;
233  
234 my $dbh = $self->dbh($object_name, $props) or return;
235 my $table = $self->_tablename($object_name, $props, $dbh) or return;
236 my ($sql, $bind_params) = $self->_prepare_delete($table, $terms);
237  
238 my $sth = $dbh->prepare( $sql );
239 unless ( $sth->execute(@$bind_params) ) {
240 $self->errstr($sth->errstr);
241 return undef
242 }
243 return 1
244 }
245  
246  
247  
248 sub drop_datasource {
249 my $self = shift;
250 my ($object_name, $props) = @_;
251  
252 my $dbh = $self->dbh($object_name, $props) or return;
253 my $table= $self->_tablename($object_name, $props, $dbh) or return;
254 unless ( $dbh->do( "DROP TABLE $table" ) ) {
255 $self->errstr( $dbh->errstr );
256 return undef
257 }
258  
259 return 1
260 }
261  
262  
263  
264 sub count {
265 my $self = shift;
266 my ($object_name, $props, $terms) = @_;
267  
268 my $dbh = $self->dbh($object_name, $props) or return;
269 my $table = $self->_tablename($object_name, $props, $dbh) or return;
270 my ($where_clause, $bind_params)= $self->_prepare_where_clause($terms);
271 my $sql = "SELECT COUNT(*) FROM $table " . $where_clause;
272  
273 my $sth = $dbh->prepare( $sql );
274 unless ( $sth->execute( @$bind_params ) ) {
275 $self->errstr($sth->errstr);
276 return undef
277 }
278  
279 my $count = $sth->fetchrow_array || 0;
280 return $count
281 }
282  
283  
284  
285 sub _read_lock {
286 my ($self, $dbh, $table) = @_;
287  
288  
289  
290  
291  
292  
293 }
294  
295  
296  
297 sub _write_lock {
298 my ($self, $dbh, $table) = @_;
299  
300  
301  
302  
303  
304  
305 }
306  
307  
308  
309 sub _unlock {
310 my ($self, $dbh, $table) = @_;
311  
312  
313  
314  
315  
316  
317 }
318  
319  
320  
321  
322  
323  
324  
325 1;
326 __END__