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