cleanup
|
1 |
package DBIx::Custom; |
2 | ||
3 |
use strict; |
|
4 |
use warnings; |
|
5 | ||
remove run_transaction().
|
6 |
use base 'Object::Simple'; |
many change
|
7 | |
packaging one directory
|
8 |
use Carp 'croak'; |
9 |
use DBI; |
|
10 |
use DBIx::Custom::Result; |
|
many many changes
|
11 |
use DBIx::Custom::SQLTemplate; |
cleanup
|
12 |
use DBIx::Custom::Query; |
packaging one directory
|
13 | |
catch up with Object::Simple...
|
14 |
__PACKAGE__->attr('dbh'); |
version 0.0901
|
15 | |
catch up with Object::Simple...
|
16 |
__PACKAGE__->class_attr(_query_caches => sub { {} }); |
17 |
__PACKAGE__->class_attr(_query_cache_keys => sub { [] }); |
|
packaging one directory
|
18 | |
catch up with Object::Simple...
|
19 |
__PACKAGE__->class_attr('query_cache_max', default => 50, |
20 |
inherit => 'scalar_copy'); |
|
21 | ||
cleanup
|
22 |
__PACKAGE__->attr([qw/user password data_source/]); |
23 |
__PACKAGE__->attr([qw/database host port/]); |
|
rename bind_filter to query_...
|
24 |
__PACKAGE__->attr([qw/default_query_filter default_fetch_filter options/]); |
packaging one directory
|
25 | |
removed register_format()
|
26 |
__PACKAGE__->dual_attr('filters', default => sub { {} }, |
27 |
inherit => 'hash_copy'); |
|
28 |
__PACKAGE__->register_filter( |
|
29 |
encode_utf8 => sub { encode('UTF-8', $_[0]) }, |
|
30 |
decode_utf8 => sub { decode('UTF-8', $_[0]) } |
|
31 |
); |
|
packaging one directory
|
32 | |
cleanup
|
33 |
__PACKAGE__->attr(result_class => 'DBIx::Custom::Result'); |
removed register_format()
|
34 |
__PACKAGE__->attr(sql_template => sub { DBIx::Custom::SQLTemplate->new }); |
35 | ||
36 | ||
packaging one directory
|
37 | |
some changed
|
38 |
sub register_filter { |
packaging one directory
|
39 |
my $invocant = shift; |
40 |
|
|
update document
|
41 |
# Add filter |
catch up with Object::Simple...
|
42 |
my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
43 |
$invocant->filters({%{$invocant->filters}, %$filters}); |
|
44 |
|
|
packaging one directory
|
45 |
return $invocant; |
46 |
} |
|
47 | ||
removed register_format()
|
48 |
sub auto_commit { |
packaging one directory
|
49 |
my $self = shift; |
50 |
|
|
update document
|
51 |
# Not connected |
removed register_format()
|
52 |
croak("Not yet connect to database") unless $self->connected; |
packaging one directory
|
53 |
|
54 |
if (@_) { |
|
update document
|
55 |
|
56 |
# Set AutoCommit |
|
packaging one directory
|
57 |
$self->dbh->{AutoCommit} = $_[0]; |
update document
|
58 |
|
packaging one directory
|
59 |
return $self; |
60 |
} |
|
61 |
return $self->dbh->{AutoCommit}; |
|
62 |
} |
|
63 | ||
64 |
sub connect { |
|
removed register_format()
|
65 |
my $proto = shift; |
66 |
|
|
67 |
# Create |
|
68 |
my $self = ref $proto ? $proto : $proto->new(@_); |
|
update document
|
69 |
|
70 |
# Information |
|
packaging one directory
|
71 |
my $data_source = $self->data_source; |
72 |
my $user = $self->user; |
|
73 |
my $password = $self->password; |
|
version 0.0901
|
74 |
my $options = $self->options; |
packaging one directory
|
75 |
|
update document
|
76 |
# Connect |
packaging one directory
|
77 |
my $dbh = eval{DBI->connect( |
78 |
$data_source, |
|
79 |
$user, |
|
80 |
$password, |
|
81 |
{ |
|
82 |
RaiseError => 1, |
|
83 |
PrintError => 0, |
|
84 |
AutoCommit => 1, |
|
version 0.0901
|
85 |
%{$options || {} } |
packaging one directory
|
86 |
} |
87 |
)}; |
|
88 |
|
|
update document
|
89 |
# Connect error |
packaging one directory
|
90 |
croak $@ if $@; |
91 |
|
|
update document
|
92 |
# Database handle |
packaging one directory
|
93 |
$self->dbh($dbh); |
update document
|
94 |
|
packaging one directory
|
95 |
return $self; |
96 |
} |
|
97 | ||
98 |
sub DESTROY { |
|
99 |
my $self = shift; |
|
update document
|
100 |
|
101 |
# Disconnect |
|
packaging one directory
|
102 |
$self->disconnect if $self->connected; |
103 |
} |
|
104 | ||
update document
|
105 |
sub connected { ref shift->{dbh} eq 'DBI::db' } |
packaging one directory
|
106 | |
107 |
sub disconnect { |
|
108 |
my $self = shift; |
|
update document
|
109 |
|
packaging one directory
|
110 |
if ($self->connected) { |
update document
|
111 |
|
112 |
# Disconnect |
|
packaging one directory
|
113 |
$self->dbh->disconnect; |
114 |
delete $self->{dbh}; |
|
115 |
} |
|
update document
|
116 |
|
117 |
return $self; |
|
packaging one directory
|
118 |
} |
119 | ||
120 |
sub reconnect { |
|
121 |
my $self = shift; |
|
update document
|
122 |
|
123 |
# Reconnect |
|
packaging one directory
|
124 |
$self->disconnect if $self->connected; |
125 |
$self->connect; |
|
update document
|
126 |
|
127 |
return $self; |
|
packaging one directory
|
128 |
} |
129 | ||
130 |
sub create_query { |
|
131 |
my ($self, $template) = @_; |
|
cleanup
|
132 |
|
packaging one directory
|
133 |
my $class = ref $self; |
134 |
|
|
cleanup
|
135 |
if (ref $template eq 'ARRAY') { |
136 |
$template = $template->[1]; |
|
137 |
} |
|
138 |
|
|
packaging one directory
|
139 |
# Create query from SQL template |
removed register_format()
|
140 |
my $sql_template = $self->sql_template; |
packaging one directory
|
141 |
|
142 |
# Try to get cached query |
|
many many changes
|
143 |
my $cached_query = $class->_query_caches->{"$template"}; |
packaging one directory
|
144 |
|
145 |
# Create query |
|
fix timeformat tests
|
146 |
my $query; |
cleanup
|
147 |
if ($cached_query) { |
148 |
$query = DBIx::Custom::Query->new( |
|
149 |
sql => $cached_query->sql, |
|
simplify filtering system
|
150 |
columns => $cached_query->columns |
cleanup
|
151 |
); |
fix timeformat tests
|
152 |
} |
153 |
else { |
|
removed register_format()
|
154 |
$query = eval{$sql_template->create_query($template)}; |
packaging one directory
|
155 |
croak($@) if $@; |
156 |
|
|
many many changes
|
157 |
$class->_add_query_cache("$template", $query); |
packaging one directory
|
158 |
} |
159 |
|
|
160 |
# Connect if not |
|
161 |
$self->connect unless $self->connected; |
|
162 |
|
|
163 |
# Prepare statement handle |
|
add all tests
|
164 |
my $sth = $self->dbh->prepare($query->{sql}); |
packaging one directory
|
165 |
|
166 |
# Set statement handle |
|
167 |
$query->sth($sth); |
|
168 |
|
|
169 |
return $query; |
|
170 |
} |
|
171 | ||
removed register_format()
|
172 |
our %VALID_EXECUTE_ARGS = map { $_ => 1 } qw/param filter/; |
173 | ||
rename query() to execute()
|
174 |
sub execute{ |
removed register_format()
|
175 |
my $self = shift; |
176 |
my $query = shift; |
|
177 |
|
|
178 |
# Arguments |
|
179 |
my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
180 |
|
|
181 |
# Check arguments |
|
182 |
foreach my $name (keys %$args) { |
|
183 |
croak "\"$name\" is invalid name" |
|
184 |
unless $VALID_EXECUTE_ARGS{$name}; |
|
185 |
} |
|
186 |
|
|
187 |
my $params = $args->{param} || {}; |
|
packaging one directory
|
188 |
|
189 |
# First argument is SQL template |
|
Simplify key search
|
190 |
unless (ref $query eq 'DBIx::Custom::Query') { |
191 |
my $template; |
|
192 |
|
|
193 |
if (ref $query eq 'ARRAY') { |
|
many many changes
|
194 |
$template = $query->[0]; |
Simplify key search
|
195 |
} |
196 |
else { $template = $query } |
|
197 |
|
|
many many changes
|
198 |
$query = $self->create_query($template); |
packaging one directory
|
199 |
} |
removed register_format()
|
200 |
|
many change
|
201 |
my $filter = $args->{filter} || $query->filter || {}; |
removed register_format()
|
202 |
|
packaging one directory
|
203 |
# Create bind value |
many many changes
|
204 |
my $bind_values = $self->_build_bind_values($query, $params, $filter); |
packaging one directory
|
205 |
|
206 |
# Execute |
|
version 0.0901
|
207 |
my $sth = $query->sth; |
208 |
my $affected = eval{$sth->execute(@$bind_values)}; |
|
packaging one directory
|
209 |
|
210 |
# Execute error |
|
211 |
if (my $execute_error = $@) { |
|
212 |
require Data::Dumper; |
|
213 |
my $sql = $query->{sql} || ''; |
|
214 |
my $params_dump = Data::Dumper->Dump([$params], ['*params']); |
|
215 |
|
|
216 |
croak("$execute_error" . |
|
217 |
"<Your SQL>\n$sql\n" . |
|
218 |
"<Your parameters>\n$params_dump"); |
|
219 |
} |
|
220 |
|
|
221 |
# Return resultset if select statement is executed |
|
222 |
if ($sth->{NUM_OF_FIELDS}) { |
|
223 |
|
|
224 |
# Get result class |
|
225 |
my $result_class = $self->result_class; |
|
226 |
|
|
227 |
# Create result |
|
228 |
my $result = $result_class->new({ |
|
many many changes
|
229 |
sth => $sth, |
230 |
default_filter => $self->default_fetch_filter, |
|
231 |
filters => $self->filters |
|
packaging one directory
|
232 |
}); |
233 |
return $result; |
|
234 |
} |
|
version 0.0901
|
235 |
return $affected; |
packaging one directory
|
236 |
} |
237 | ||
238 |
sub _build_bind_values { |
|
many many changes
|
239 |
my ($self, $query, $params, $filter) = @_; |
packaging one directory
|
240 |
|
241 |
# binding values |
|
242 |
my @bind_values; |
|
243 |
|
|
Simplify key search
|
244 |
# Build bind values |
simplify filtering system
|
245 |
my $count = {}; |
246 |
foreach my $column (@{$query->columns}) { |
|
packaging one directory
|
247 |
|
add query filter error check
|
248 |
croak "\"$column\" is not exists in params" |
249 |
unless exists $params->{$column}; |
|
250 |
|
|
Simplify key search
|
251 |
# Value |
add query filter error check
|
252 |
my $value = ref $params->{$column} eq 'ARRAY' |
simplify filtering system
|
253 |
? $params->{$column}->[$count->{$column} || 0] |
254 |
: $params->{$column}; |
|
packaging one directory
|
255 |
|
Simplify key search
|
256 |
# Filter |
simplify filtering system
|
257 |
$filter ||= {}; |
many many changes
|
258 |
|
simplify filtering system
|
259 |
# Filter name |
260 |
my $fname = $filter->{$column} || $self->default_query_filter || ''; |
|
261 |
|
|
add query filter error check
|
262 |
my $filter_func; |
263 |
if ($fname) { |
|
264 |
|
|
265 |
if (ref $fname eq 'CODE') { |
|
266 |
$filter_func = $fname; |
|
267 |
} |
|
268 |
else { |
|
269 |
my $filters = $self->filters; |
|
270 |
croak "Not exists filter \"$fname\"" unless exists $filters->{$fname}; |
|
271 |
$filter_func = $filters->{$fname}; |
|
272 |
} |
|
273 |
} |
|
274 |
|
|
275 |
push @bind_values, $filter_func |
|
276 |
? $filter_func->($value) |
|
many change
|
277 |
: $value; |
simplify filtering system
|
278 |
|
279 |
# Count up |
|
280 |
$count->{$column}++; |
|
cleanup
|
281 |
} |
282 |
|
|
Simplify key search
|
283 |
return \@bind_values; |
cleanup
|
284 |
} |
285 | ||
removed register_format()
|
286 |
our %VALID_INSERT_ARGS = map { $_ => 1 } qw/table param append filter/; |
cleanup insert
|
287 | |
packaging one directory
|
288 |
sub insert { |
removed register_format()
|
289 |
my $self = shift; |
cleanup insert
|
290 |
|
291 |
# Arguments |
|
removed register_format()
|
292 |
my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
293 | ||
cleanup insert
|
294 |
# Check arguments |
295 |
foreach my $name (keys %$args) { |
|
296 |
croak "\"$name\" is invalid name" |
|
297 |
unless $VALID_INSERT_ARGS{$name}; |
|
298 |
} |
|
299 |
|
|
removed register_format()
|
300 |
# Arguments |
301 |
my $table = $args->{table} || ''; |
|
302 |
my $param = $args->{param} || {}; |
|
303 |
my $append = $args->{append} || ''; |
|
304 |
my $filter = $args->{filter}; |
|
packaging one directory
|
305 |
|
306 |
# Insert keys |
|
removed register_format()
|
307 |
my @insert_keys = keys %$param; |
packaging one directory
|
308 |
|
309 |
# Not exists insert keys |
|
310 |
croak("Key-value pairs for insert must be specified to 'insert' second argument") |
|
311 |
unless @insert_keys; |
|
312 |
|
|
313 |
# Templte for insert |
|
314 |
my $template = "insert into $table {insert " . join(' ', @insert_keys) . '}'; |
|
removed register_format()
|
315 |
$template .= " $append" if $append; |
packaging one directory
|
316 |
|
317 |
# Execute query |
|
removed register_format()
|
318 |
my $ret_val = $self->execute($template, param => $param, |
319 |
filter => $filter); |
|
packaging one directory
|
320 |
|
321 |
return $ret_val; |
|
322 |
} |
|
323 | ||
cleanup update and update_al...
|
324 |
our %VALID_UPDATE_ARGS |
removed register_format()
|
325 |
= map { $_ => 1 } qw/table param where append filter allow_update_all/; |
cleanup update and update_al...
|
326 | |
packaging one directory
|
327 |
sub update { |
removed register_format()
|
328 |
my $self = shift; |
329 | ||
330 |
my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
cleanup update and update_al...
|
331 |
|
332 |
# Check arguments |
|
333 |
foreach my $name (keys %$args) { |
|
334 |
croak "\"$name\" is invalid name" |
|
335 |
unless $VALID_UPDATE_ARGS{$name}; |
|
336 |
} |
|
337 |
|
|
338 |
# Arguments |
|
removed register_format()
|
339 |
my $table = $args->{table} || ''; |
340 |
my $param = $args->{param} || {}; |
|
341 |
my $where = $args->{where} || {}; |
|
cleanup update and update_al...
|
342 |
my $append_statement = $args->{append} || ''; |
many many changes
|
343 |
my $filter = $args->{filter}; |
cleanup update and update_al...
|
344 |
my $allow_update_all = $args->{allow_update_all}; |
packaging one directory
|
345 |
|
346 |
# Update keys |
|
removed register_format()
|
347 |
my @update_keys = keys %$param; |
packaging one directory
|
348 |
|
349 |
# Not exists update kyes |
|
350 |
croak("Key-value pairs for update must be specified to 'update' second argument") |
|
351 |
unless @update_keys; |
|
352 |
|
|
353 |
# Where keys |
|
removed register_format()
|
354 |
my @where_keys = keys %$where; |
packaging one directory
|
355 |
|
356 |
# Not exists where keys |
|
357 |
croak("Key-value pairs for where clause must be specified to 'update' third argument") |
|
cleanup update and update_al...
|
358 |
if !@where_keys && !$allow_update_all; |
packaging one directory
|
359 |
|
360 |
# Update clause |
|
361 |
my $update_clause = '{update ' . join(' ', @update_keys) . '}'; |
|
362 |
|
|
363 |
# Where clause |
|
364 |
my $where_clause = ''; |
|
simplify filtering system
|
365 |
my $new_where = {}; |
many change
|
366 |
|
packaging one directory
|
367 |
if (@where_keys) { |
368 |
$where_clause = 'where '; |
|
369 |
foreach my $where_key (@where_keys) { |
|
simplify filtering system
|
370 |
|
371 |
$where_clause .= "{= $where_key} and "; |
|
packaging one directory
|
372 |
} |
373 |
$where_clause =~ s/ and $//; |
|
374 |
} |
|
375 |
|
|
376 |
# Template for update |
|
377 |
my $template = "update $table $update_clause $where_clause"; |
|
insert, update, delete appnd...
|
378 |
$template .= " $append_statement" if $append_statement; |
packaging one directory
|
379 |
|
380 |
# Rearrange parammeters |
|
removed register_format()
|
381 |
foreach my $wkey (@where_keys) { |
simplify filtering system
|
382 |
|
removed register_format()
|
383 |
if (exists $param->{$wkey}) { |
384 |
$param->{$wkey} = [$param->{$wkey}] |
|
385 |
unless ref $param->{$wkey} eq 'ARRAY'; |
|
simplify filtering system
|
386 |
|
removed register_format()
|
387 |
push @{$param->{$wkey}}, $where->{$wkey}; |
simplify filtering system
|
388 |
} |
add tests
|
389 |
else { |
removed register_format()
|
390 |
$param->{$wkey} = $where->{$wkey}; |
add tests
|
391 |
} |
simplify filtering system
|
392 |
} |
packaging one directory
|
393 |
|
394 |
# Execute query |
|
removed register_format()
|
395 |
my $ret_val = $self->execute($template, param => $param, |
396 |
filter => $filter); |
|
packaging one directory
|
397 |
|
398 |
return $ret_val; |
|
399 |
} |
|
400 | ||
401 |
sub update_all { |
|
removed register_format()
|
402 |
my $self = shift;; |
cleanup update and update_al...
|
403 |
|
removed register_format()
|
404 |
# Arguments |
405 |
my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
406 |
|
|
refactoring select
|
407 |
# Allow all update |
cleanup update and update_al...
|
408 |
$args->{allow_update_all} = 1; |
insert, update, delete appnd...
|
409 |
|
update document
|
410 |
# Update all rows |
removed register_format()
|
411 |
return $self->update($args); |
packaging one directory
|
412 |
} |
413 | ||
refactoring delete and delet...
|
414 |
our %VALID_DELETE_ARGS |
removed register_format()
|
415 |
= map { $_ => 1 } qw/table where append filter allow_delete_all/; |
refactoring delete and delet...
|
416 | |
packaging one directory
|
417 |
sub delete { |
removed register_format()
|
418 |
my $self = shift; |
419 |
|
|
420 |
# Arguments |
|
421 |
my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
refactoring delete and delet...
|
422 |
|
423 |
# Check arguments |
|
424 |
foreach my $name (keys %$args) { |
|
425 |
croak "\"$name\" is invalid name" |
|
426 |
unless $VALID_DELETE_ARGS{$name}; |
|
427 |
} |
|
428 |
|
|
429 |
# Arguments |
|
removed register_format()
|
430 |
my $table = $args->{table} || ''; |
431 |
my $where = $args->{where} || {}; |
|
refactoring delete and delet...
|
432 |
my $append_statement = $args->{append}; |
removed register_format()
|
433 |
my $filter = $args->{filter}; |
refactoring delete and delet...
|
434 |
my $allow_delete_all = $args->{allow_delete_all}; |
packaging one directory
|
435 |
|
436 |
# Where keys |
|
removed register_format()
|
437 |
my @where_keys = keys %$where; |
packaging one directory
|
438 |
|
439 |
# Not exists where keys |
|
440 |
croak("Key-value pairs for where clause must be specified to 'delete' second argument") |
|
refactoring delete and delet...
|
441 |
if !@where_keys && !$allow_delete_all; |
packaging one directory
|
442 |
|
443 |
# Where clause |
|
444 |
my $where_clause = ''; |
|
445 |
if (@where_keys) { |
|
446 |
$where_clause = 'where '; |
|
removed register_format()
|
447 |
foreach my $wkey (@where_keys) { |
448 |
$where_clause .= "{= $wkey} and "; |
|
packaging one directory
|
449 |
} |
450 |
$where_clause =~ s/ and $//; |
|
451 |
} |
|
452 |
|
|
453 |
# Template for delete |
|
454 |
my $template = "delete from $table $where_clause"; |
|
insert, update, delete appnd...
|
455 |
$template .= " $append_statement" if $append_statement; |
packaging one directory
|
456 |
|
457 |
# Execute query |
|
removed register_format()
|
458 |
my $ret_val = $self->execute($template, param => $where, |
459 |
filter => $filter); |
|
packaging one directory
|
460 |
|
461 |
return $ret_val; |
|
462 |
} |
|
463 | ||
464 |
sub delete_all { |
|
removed register_format()
|
465 |
my $self = shift; |
466 |
|
|
467 |
# Arguments |
|
468 |
my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
refactoring delete and delet...
|
469 |
|
refactoring select
|
470 |
# Allow all delete |
refactoring delete and delet...
|
471 |
$args->{allow_delete_all} = 1; |
insert, update, delete appnd...
|
472 |
|
update document
|
473 |
# Delete all rows |
removed register_format()
|
474 |
return $self->delete($args); |
packaging one directory
|
475 |
} |
476 | ||
refactoring select
|
477 |
our %VALID_SELECT_ARGS |
removed register_format()
|
478 |
= map { $_ => 1 } qw/table column where append filter/; |
refactoring select
|
479 | |
packaging one directory
|
480 |
sub select { |
removed register_format()
|
481 |
my $self = shift;; |
packaging one directory
|
482 |
|
removed register_format()
|
483 |
my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
packaging one directory
|
484 |
|
refactoring select
|
485 |
# Check arguments |
486 |
foreach my $name (keys %$args) { |
|
487 |
croak "\"$name\" is invalid name" |
|
488 |
unless $VALID_SELECT_ARGS{$name}; |
|
489 |
} |
|
packaging one directory
|
490 |
|
refactoring select
|
491 |
# Arguments |
removed register_format()
|
492 |
my $tables = $args->{table} || []; |
493 |
$tables = [$tables] unless ref $tables eq 'ARRAY'; |
|
494 |
my $columns = $args->{column} || []; |
|
refactoring select
|
495 |
my $where_params = $args->{where} || {}; |
496 |
my $append_statement = $args->{append} || ''; |
|
many many changes
|
497 |
my $filter = $args->{filter}; |
packaging one directory
|
498 |
|
499 |
# SQL template for select statement |
|
500 |
my $template = 'select '; |
|
501 |
|
|
502 |
# Join column clause |
|
503 |
if (@$columns) { |
|
504 |
foreach my $column (@$columns) { |
|
505 |
$template .= "$column, "; |
|
506 |
} |
|
507 |
$template =~ s/, $/ /; |
|
508 |
} |
|
509 |
else { |
|
510 |
$template .= '* '; |
|
511 |
} |
|
512 |
|
|
513 |
# Join table |
|
514 |
$template .= 'from '; |
|
515 |
foreach my $table (@$tables) { |
|
516 |
$template .= "$table, "; |
|
517 |
} |
|
518 |
$template =~ s/, $/ /; |
|
519 |
|
|
520 |
# Where clause keys |
|
521 |
my @where_keys = keys %$where_params; |
|
522 |
|
|
523 |
# Join where clause |
|
524 |
if (@where_keys) { |
|
525 |
$template .= 'where '; |
|
526 |
foreach my $where_key (@where_keys) { |
|
compile success
|
527 |
$template .= "{= $where_key} and "; |
packaging one directory
|
528 |
} |
529 |
} |
|
530 |
$template =~ s/ and $//; |
|
531 |
|
|
532 |
# Append something to last of statement |
|
533 |
if ($append_statement =~ s/^where //) { |
|
534 |
if (@where_keys) { |
|
535 |
$template .= " and $append_statement"; |
|
536 |
} |
|
537 |
else { |
|
538 |
$template .= " where $append_statement"; |
|
539 |
} |
|
540 |
} |
|
541 |
else { |
|
542 |
$template .= " $append_statement"; |
|
543 |
} |
|
544 |
|
|
545 |
# Execute query |
|
removed register_format()
|
546 |
my $result = $self->execute($template, param => $where_params, |
547 |
filter => $filter); |
|
packaging one directory
|
548 |
|
549 |
return $result; |
|
550 |
} |
|
551 | ||
552 |
sub _add_query_cache { |
|
553 |
my ($class, $template, $query) = @_; |
|
update document
|
554 |
|
555 |
# Query information |
|
packaging one directory
|
556 |
my $query_cache_keys = $class->_query_cache_keys; |
557 |
my $query_caches = $class->_query_caches; |
|
558 |
|
|
update document
|
559 |
# Already cached |
packaging one directory
|
560 |
return $class if $query_caches->{$template}; |
561 |
|
|
update document
|
562 |
# Cache |
packaging one directory
|
563 |
$query_caches->{$template} = $query; |
564 |
push @$query_cache_keys, $template; |
|
565 |
|
|
update document
|
566 |
# Check cache overflow |
packaging one directory
|
567 |
my $overflow = @$query_cache_keys - $class->query_cache_max; |
568 |
for (my $i = 0; $i < $overflow; $i++) { |
|
569 |
my $template = shift @$query_cache_keys; |
|
570 |
delete $query_caches->{$template}; |
|
571 |
} |
|
572 |
|
|
573 |
return $class; |
|
574 |
} |
|
575 | ||
576 |
=head1 NAME |
|
577 | ||
renamed fetch_rows to fetch_...
|
578 |
DBIx::Custom - DBI with hash bind and filtering system |
packaging one directory
|
579 | |
version 0.0901
|
580 |
=head1 VERSION |
packaging one directory
|
581 | |
removed register_format()
|
582 |
Version 0.1501 |
catch up with Object::Simple...
|
583 | |
584 |
=cut |
|
585 | ||
removed register_format()
|
586 |
our $VERSION = '0.1501'; |
some changed
|
587 |
$VERSION = eval $VERSION; |
packaging one directory
|
588 | |
cleanup
|
589 |
=head1 STATE |
590 | ||
591 |
This module is not stable. Method name and functionality will be change. |
|
592 | ||
version 0.0901
|
593 |
=head1 SYNOPSYS |
594 |
|
|
removed register_format()
|
595 |
# Connect |
596 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=books", |
|
597 |
user => 'ken', password => '!LFKD%$&'); |
|
some changed
|
598 |
|
version 0.0901
|
599 |
# Insert |
removed register_format()
|
600 |
$dbi->insert(table => 'books', |
601 |
param => {title => 'perl', author => 'Ken'} |
|
602 |
filter => {title => 'encode_utf8'}); |
|
version 0.0901
|
603 |
|
604 |
# Update |
|
removed register_format()
|
605 |
$dbi->update(table => 'books', |
606 |
param => {title => 'aaa', author => 'Ken'}, |
|
607 |
where => {id => 5} |
|
608 |
filter => {title => 'encode_utf8'); |
|
609 |
|
|
610 |
# Update all |
|
611 |
$dbi->update_all(table => 'books', |
|
612 |
param => {title => 'aaa'} |
|
613 |
filter => {title => 'encode_utf8'}); |
|
version 0.0901
|
614 |
|
615 |
# Delete |
|
removed register_format()
|
616 |
$dbi->delete(table => 'books', |
617 |
where => {author => 'Ken'} |
|
618 |
filter => {title => 'encode_utf8'}); |
|
version 0.0901
|
619 |
|
removed register_format()
|
620 |
# Delete all |
621 |
$dbi->delete_all(table => 'books'); |
|
renamed fetch_rows to fetch_...
|
622 |
|
removed register_format()
|
623 |
# Select |
624 |
my $result = $dbi->select(table => 'books'); |
|
renamed fetch_rows to fetch_...
|
625 |
|
removed register_format()
|
626 |
# Select(more complex) |
renamed fetch_rows to fetch_...
|
627 |
my $result = $dbi->select( |
628 |
'books', |
|
629 |
{ |
|
630 |
columns => [qw/author title/], |
|
631 |
where => {author => 'Ken'}, |
|
removed register_format()
|
632 |
append => 'order by id limit 1', |
633 |
filter => {tilte => 'encode_utf8'} |
|
renamed fetch_rows to fetch_...
|
634 |
} |
635 |
); |
|
packaging one directory
|
636 | |
removed register_format()
|
637 |
# Execute SQL |
638 |
$dbi->execute("select title from books"); |
|
639 |
|
|
640 |
# Execute SQL with parameters and filter |
|
641 |
$dbi->execute("select id from books where {= author} && {like title}", |
|
642 |
param => {author => 'ken', title => '%Perl%'}, |
|
643 |
filter => {tilte => 'encode_utf8'}); |
|
644 |
|
|
645 |
# Default filter |
|
646 |
$dbi->default_query_filter('encode_utf8'); |
|
647 |
$dbi->default_fetch_filter('decode_utf8'); |
|
648 |
|
|
649 |
# Fetch |
|
650 |
while (my $row = $result->fetch) { |
|
651 |
# ... |
|
652 |
} |
|
653 |
|
|
654 |
# Fetch hash |
|
655 |
while (my $row = $result->fetch_hash) { |
|
656 |
|
|
657 |
} |
|
658 |
|
|
659 |
|
|
update document
|
660 |
=head1 ATTRIBUTES |
packaging one directory
|
661 | |
662 |
=head2 user |
|
663 | ||
update document
|
664 |
Database user name |
bind_filter argument is chan...
|
665 |
|
version 0.0901
|
666 |
$dbi = $dbi->user('Ken'); |
667 |
$user = $dbi->user; |
|
bind_filter argument is chan...
|
668 |
|
packaging one directory
|
669 |
=head2 password |
670 | ||
update document
|
671 |
Database password |
bind_filter argument is chan...
|
672 |
|
version 0.0901
|
673 |
$dbi = $dbi->password('lkj&le`@s'); |
674 |
$password = $dbi->password; |
|
packaging one directory
|
675 | |
676 |
=head2 data_source |
|
677 | ||
update document
|
678 |
Database data source |
bind_filter argument is chan...
|
679 |
|
version 0.0901
|
680 |
$dbi = $dbi->data_source("dbi:mysql:dbname=$database"); |
681 |
$data_source = $dbi->data_source; |
|
packaging one directory
|
682 |
|
version 0.0901
|
683 |
If you know data source more, See also L<DBI>. |
684 | ||
packaging one directory
|
685 |
=head2 database |
686 | ||
update document
|
687 |
Database name |
bind_filter argument is chan...
|
688 | |
version 0.0901
|
689 |
$dbi = $dbi->database('books'); |
690 |
$database = $dbi->database; |
|
packaging one directory
|
691 | |
add port and host method
|
692 |
=head2 host |
693 | ||
update document
|
694 |
Host name |
bind_filter argument is chan...
|
695 | |
version 0.0901
|
696 |
$dbi = $dbi->host('somehost.com'); |
697 |
$host = $dbi->host; |
|
bind_filter argument is chan...
|
698 | |
version 0.0901
|
699 |
You can also set IP address like '127.03.45.12'. |
add port and host method
|
700 | |
701 |
=head2 port |
|
702 | ||
update document
|
703 |
Port number |
bind_filter argument is chan...
|
704 | |
version 0.0901
|
705 |
$dbi = $dbi->port(1198); |
706 |
$port = $dbi->port; |
|
add port and host method
|
707 | |
version 0.0901
|
708 |
=head2 options |
packaging one directory
|
709 | |
update document
|
710 |
DBI options |
packaging one directory
|
711 | |
version 0.0901
|
712 |
$dbi = $dbi->options({PrintError => 0, RaiseError => 1}); |
713 |
$options = $dbi->options; |
|
packaging one directory
|
714 | |
removed register_format()
|
715 |
=head2 sql_template |
packaging one directory
|
716 | |
many many changes
|
717 |
SQLTemplate object |
packaging one directory
|
718 | |
removed register_format()
|
719 |
$dbi = $dbi->sql_template(DBIx::Cutom::SQLTemplate->new); |
720 |
$sql_template = $dbi->sql_template; |
|
packaging one directory
|
721 | |
many many changes
|
722 |
See also L<DBIx::Custom::SQLTemplate>. |
packaging one directory
|
723 | |
bind_filter argument is chan...
|
724 |
=head2 filters |
packaging one directory
|
725 | |
update document
|
726 |
Filters |
packaging one directory
|
727 | |
version 0.0901
|
728 |
$dbi = $dbi->filters({filter1 => sub { }, filter2 => sub {}}); |
729 |
$filters = $dbi->filters; |
|
packaging one directory
|
730 |
|
version 0.0901
|
731 |
This method is generally used to get a filter. |
732 | ||
733 |
$filter = $dbi->filters->{encode_utf8}; |
|
734 | ||
some changed
|
735 |
If you add filter, use register_filter method. |
packaging one directory
|
736 | |
rename bind_filter to query_...
|
737 |
=head2 default_query_filter |
packaging one directory
|
738 | |
removed register_format()
|
739 |
Default query filter |
packaging one directory
|
740 | |
removed register_format()
|
741 |
$dbi = $dbi->default_query_filter($default_query_filter); |
rename bind_filter to query_...
|
742 |
$default_query_filter = $dbi->default_query_filter |
packaging one directory
|
743 | |
removed register_format()
|
744 |
Query filter example |
cleanup
|
745 |
|
some changed
|
746 |
$dbi->register_filter(encode_utf8 => sub { |
cleanup
|
747 |
my $value = shift; |
bind_filter argument is chan...
|
748 |
|
cleanup
|
749 |
require Encode 'encode_utf8'; |
bind_filter argument is chan...
|
750 |
|
cleanup
|
751 |
return encode_utf8($value); |
bind_filter argument is chan...
|
752 |
}); |
cleanup
|
753 |
|
rename bind_filter to query_...
|
754 |
$dbi->default_query_filter('encode_utf8') |
packaging one directory
|
755 | |
version 0.0901
|
756 |
Bind filter arguemts is |
757 | ||
758 |
1. $value : Value |
|
removed register_format()
|
759 |
3. $dbi : DBIx::Custom instance |
version 0.0901
|
760 | |
cleanup
|
761 |
=head2 default_fetch_filter |
packaging one directory
|
762 | |
update document
|
763 |
Fetching filter |
bind_filter argument is chan...
|
764 | |
cleanup
|
765 |
$dbi = $dbi->default_fetch_filter($default_fetch_filter); |
766 |
$default_fetch_filter = $dbi->default_fetch_filter; |
|
bind_filter argument is chan...
|
767 | |
removed register_format()
|
768 |
Fetch filter example |
packaging one directory
|
769 | |
some changed
|
770 |
$dbi->register_filter(decode_utf8 => sub { |
cleanup
|
771 |
my $value = shift; |
bind_filter argument is chan...
|
772 |
|
cleanup
|
773 |
require Encode 'decode_utf8'; |
bind_filter argument is chan...
|
774 |
|
cleanup
|
775 |
return decode_utf8($value); |
bind_filter argument is chan...
|
776 |
}); |
packaging one directory
|
777 | |
cleanup
|
778 |
$dbi->default_fetch_filter('decode_utf8'); |
779 | ||
removed register_format()
|
780 |
Fetching filter arguemts is |
version 0.0901
|
781 | |
removed register_format()
|
782 |
1. Value |
783 |
2. DBIx::Custom instance |
|
version 0.0901
|
784 | |
packaging one directory
|
785 |
=head2 result_class |
786 | ||
update document
|
787 |
Resultset class |
bind_filter argument is chan...
|
788 | |
version 0.0901
|
789 |
$dbi = $dbi->result_class('DBIx::Custom::Result'); |
packaging one directory
|
790 |
$result_class = $dbi->result_class; |
791 | ||
update document
|
792 |
Default is L<DBIx::Custom::Result> |
793 | ||
packaging one directory
|
794 |
=head2 dbh |
795 | ||
update document
|
796 |
Database handle |
bind_filter argument is chan...
|
797 |
|
version 0.0901
|
798 |
$dbi = $dbi->dbh($dbh); |
799 |
$dbh = $dbi->dbh; |
|
bind_filter argument is chan...
|
800 |
|
801 |
=head2 query_cache_max |
|
802 | ||
update document
|
803 |
Query cache max |
bind_filter argument is chan...
|
804 | |
version 0.0901
|
805 |
$class = DBIx::Custom->query_cache_max(50); |
806 |
$query_cache_max = DBIx::Custom->query_cache_max; |
|
807 | ||
808 |
Default value is 50 |
|
809 | ||
update document
|
810 |
=head1 METHODS |
811 | ||
812 |
This class is L<Object::Simple> subclass. |
|
813 |
You can use all methods of L<Object::Simple> |
|
packaging one directory
|
814 | |
removed register_format()
|
815 |
=head2 auto_commit |
816 | ||
817 |
Set and Get auto commit |
|
818 | ||
819 |
$self = $dbi->auto_commit($auto_commit); |
|
820 |
$auto_commit = $dbi->auto_commit; |
|
821 |
|
|
packaging one directory
|
822 |
=head2 connect |
823 | ||
bind_filter argument is chan...
|
824 |
Connect to database |
825 | ||
packaging one directory
|
826 |
$dbi->connect; |
827 | ||
828 |
=head2 disconnect |
|
829 | ||
bind_filter argument is chan...
|
830 |
Disconnect database |
831 | ||
packaging one directory
|
832 |
$dbi->disconnect; |
833 | ||
version 0.0901
|
834 |
If database is already disconnected, this method do nothing. |
packaging one directory
|
835 | |
836 |
=head2 reconnect |
|
837 | ||
bind_filter argument is chan...
|
838 |
Reconnect to database |
839 | ||
packaging one directory
|
840 |
$dbi->reconnect; |
841 | ||
842 |
=head2 connected |
|
843 | ||
version 0.0901
|
844 |
Check if database is connected. |
bind_filter argument is chan...
|
845 |
|
version 0.0901
|
846 |
$is_connected = $dbi->connected; |
packaging one directory
|
847 |
|
some changed
|
848 |
=head2 register_filter |
packaging one directory
|
849 | |
removed register_format()
|
850 |
Resister filter |
packaging one directory
|
851 |
|
some changed
|
852 |
$dbi->register_filter($fname1 => $filter1, $fname => $filter2); |
bind_filter argument is chan...
|
853 |
|
removed register_format()
|
854 |
register_filter example |
version 0.0901
|
855 | |
some changed
|
856 |
$dbi->register_filter( |
packaging one directory
|
857 |
encode_utf8 => sub { |
removed register_format()
|
858 |
my $value = shift; |
859 |
|
|
860 |
require Encode; |
|
861 |
|
|
862 |
return Encode::encode('UTF-8', $value); |
|
packaging one directory
|
863 |
}, |
bind_filter argument is chan...
|
864 |
decode_utf8 => sub { |
removed register_format()
|
865 |
my $value = shift; |
866 |
|
|
867 |
require Encode; |
|
868 |
|
|
869 |
return Encode::decode('UTF-8', $value) |
|
packaging one directory
|
870 |
} |
871 |
); |
|
872 | ||
version 0.0901
|
873 |
=head2 create_query |
bind_filter argument is chan...
|
874 |
|
version 0.0901
|
875 |
Create Query object parsing SQL template |
bind_filter argument is chan...
|
876 | |
version 0.0901
|
877 |
my $query = $dbi->create_query("select * from authors where {= name} and {= age}"); |
bind_filter argument is chan...
|
878 | |
removed register_format()
|
879 |
$query is <DBIx::Query> instance. This is executed by query method as the following |
bind_filter argument is chan...
|
880 | |
rename query() to execute()
|
881 |
$dbi->execute($query, $params); |
bind_filter argument is chan...
|
882 | |
many many changes
|
883 |
If you know SQL template, see also L<DBIx::Custom::SQLTemplate>. |
bind_filter argument is chan...
|
884 | |
rename query() to execute()
|
885 |
=head2 execute |
packaging one directory
|
886 | |
version 0.0901
|
887 |
Query |
bind_filter argument is chan...
|
888 | |
rename query() to execute()
|
889 |
$result = $dbi->execute($template, $params); |
packaging one directory
|
890 | |
rename query() to execute()
|
891 |
The following is query example |
bind_filter argument is chan...
|
892 | |
rename query() to execute()
|
893 |
$result = $dbi->execute("select * from authors where {= name} and {= age}", |
removed register_format()
|
894 |
{name => 'taro', age => 19}); |
packaging one directory
|
895 |
|
896 |
while (my @row = $result->fetch) { |
|
897 |
# do something |
|
898 |
} |
|
899 | ||
many many changes
|
900 |
If you now syntax of template, See also L<DBIx::Custom::SQLTemplate> |
version 0.0901
|
901 | |
removed register_format()
|
902 |
execute() return L<DBIx::Custom::Result> instance |
version 0.0901
|
903 | |
packaging one directory
|
904 |
=head2 insert |
905 | ||
update document
|
906 |
Insert row |
bind_filter argument is chan...
|
907 | |
removed register_format()
|
908 |
$affected = $dbi->insert(table => $table, |
909 |
param => {%param}, |
|
910 |
append => $append, |
|
911 |
filter => {%filter}); |
|
update document
|
912 | |
version 0.0901
|
913 |
Retrun value is affected rows count |
packaging one directory
|
914 |
|
removed register_format()
|
915 |
Example |
version 0.0901
|
916 | |
removed register_format()
|
917 |
# insert |
918 |
$dbi->insert(table => 'books', |
|
919 |
param => {title => 'Perl', author => 'Taro'}, |
|
920 |
append => "some statement", |
|
921 |
filter => {title => 'encode_utf8'}) |
|
version 0.0901
|
922 | |
packaging one directory
|
923 |
=head2 update |
924 | ||
update document
|
925 |
Update rows |
926 | ||
removed register_format()
|
927 |
$affected = $dbi->update(table => $table, |
928 |
param => {%params}, |
|
929 |
where => {%where}, |
|
930 |
append => $append, |
|
931 |
filter => {%filter}) |
|
version 0.0901
|
932 | |
933 |
Retrun value is affected rows count |
|
update document
|
934 | |
removed register_format()
|
935 |
Example |
bind_filter argument is chan...
|
936 | |
removed register_format()
|
937 |
#update |
938 |
$dbi->update(table => 'books', |
|
939 |
param => {title => 'Perl', author => 'Taro'}, |
|
940 |
where => {id => 5}, |
|
941 |
append => "some statement", |
|
942 |
filter => {title => 'encode_utf8'}) |
|
version 0.0901
|
943 | |
packaging one directory
|
944 |
=head2 update_all |
945 | ||
bind_filter argument is chan...
|
946 |
Update all rows |
947 | ||
removed register_format()
|
948 |
$affected = $dbi->update_all(table => $table, |
949 |
param => {%params}, |
|
950 |
filter => {%filter}, |
|
951 |
append => $append); |
|
update document
|
952 | |
version 0.0901
|
953 |
Retrun value is affected rows count |
954 | ||
removed register_format()
|
955 |
Example |
update document
|
956 | |
removed register_format()
|
957 |
# update_all |
958 |
$dbi->update_all(table => 'books', |
|
959 |
param => {author => 'taro'}, |
|
960 |
filter => {author => 'encode_utf8'}); |
|
packaging one directory
|
961 | |
962 |
=head2 delete |
|
963 | ||
update document
|
964 |
Delete rows |
965 | ||
removed register_format()
|
966 |
# delete |
967 |
$affected = $dbi->delete(table => $table, |
|
968 |
where => {%where}, |
|
969 |
append => $append |
|
970 |
filter => {%filter}); |
|
bind_filter argument is chan...
|
971 | |
version 0.0901
|
972 |
Retrun value is affected rows count |
packaging one directory
|
973 |
|
removed register_format()
|
974 |
Example |
packaging one directory
|
975 | |
removed register_format()
|
976 |
# delete |
977 |
$dbi->delete(table => 'books', |
|
978 |
where => {id => 5}, |
|
979 |
append => 'some statement', |
|
980 |
filter => {id => 'encode_utf8'); |
|
version 0.0901
|
981 | |
packaging one directory
|
982 |
=head2 delete_all |
983 | ||
bind_filter argument is chan...
|
984 |
Delete all rows |
985 | ||
removed register_format()
|
986 |
$affected = $dbi->delete_all(table => $table); |
packaging one directory
|
987 | |
version 0.0901
|
988 |
Retrun value is affected rows count |
bind_filter argument is chan...
|
989 | |
removed register_format()
|
990 |
Example |
991 |
|
|
992 |
# delete_all |
|
version 0.0901
|
993 |
$dbi->delete_all('books'); |
packaging one directory
|
994 | |
995 |
=head2 select |
|
996 |
|
|
update document
|
997 |
Select rows |
bind_filter argument is chan...
|
998 | |
removed register_format()
|
999 |
$result = $dbi->select(table => $table, |
1000 |
column => [@column], |
|
1001 |
where => {%where}, |
|
1002 |
append => $append, |
|
1003 |
filter => {%filter}); |
|
update document
|
1004 | |
removed register_format()
|
1005 |
$reslt is L<DBIx::Custom::Result> instance |
update document
|
1006 | |
rename query() to execute()
|
1007 |
The following is some select examples |
update document
|
1008 | |
removed register_format()
|
1009 |
# select |
update document
|
1010 |
$result = $dbi->select('books'); |
packaging one directory
|
1011 |
|
update document
|
1012 |
# select * from books where title = 'Perl'; |
1013 |
$result = $dbi->select('books', {title => 1}); |
|
1014 |
|
|
1015 |
# select title, author from books where id = 1 for update; |
|
1016 |
$result = $dbi->select( |
|
removed register_format()
|
1017 |
table => 'books', |
1018 |
where => ['title', 'author'], |
|
1019 |
where => {id => 1}, |
|
1020 |
appned => 'for update' |
|
update document
|
1021 |
); |
1022 | ||
1023 |
You can join multi tables |
|
1024 |
|
|
1025 |
$result = $dbi->select( |
|
1026 |
['table1', 'table2'], # tables |
|
1027 |
['table1.id as table1_id', 'title'], # columns (alias is ok) |
|
1028 |
{table1.id => 1}, # where clase |
|
1029 |
"where table1.id = table2.id", # join clause (must start 'where') |
|
1030 |
); |
|
1031 | ||
version 0.0901
|
1032 |
=head1 DBIx::Custom default configuration |
packaging one directory
|
1033 | |
removed register_format()
|
1034 |
By default, "AutoCommit" and "RaiseError" is true. |
version 0.0901
|
1035 | |
packaging one directory
|
1036 |
=head1 AUTHOR |
1037 | ||
1038 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
|
1039 | ||
1040 |
Github L<http://github.com/yuki-kimoto> |
|
1041 | ||
version 0.0901
|
1042 |
I develope this module L<http://github.com/yuki-kimoto/DBIx-Custom> |
1043 | ||
packaging one directory
|
1044 |
=head1 COPYRIGHT & LICENSE |
1045 | ||
1046 |
Copyright 2009 Yuki Kimoto, all rights reserved. |
|
1047 | ||
1048 |
This program is free software; you can redistribute it and/or modify it |
|
1049 |
under the same terms as Perl itself. |
|
1050 | ||
1051 |
=cut |