Version 0.0101 release
|
1 |
package DBIx::Custom; |
first commit
|
2 |
use Object::Simple; |
add test
|
3 | |
4 |
our $VERSION = '0.0101'; |
|
5 | ||
6 |
use Carp 'croak'; |
|
add some method
|
7 |
use DBI; |
Version 0.0101 release
|
8 |
use DBIx::Custom::Query; |
9 |
use DBIx::Custom::Result; |
|
10 |
use DBIx::Custom::SQL::Template; |
|
add tests
|
11 | |
add tests
|
12 | |
13 |
### Class-Object Accessors |
|
update document
|
14 |
sub user : ClassObjectAttr { initialize => {clone => 'scalar'} } |
15 |
sub password : ClassObjectAttr { initialize => {clone => 'scalar'} } |
|
16 |
sub data_source : ClassObjectAttr { initialize => {clone => 'scalar'} } |
|
cleanup
|
17 |
sub dbi_options : ClassObjectAttr { initialize => {clone => 'hash', |
add tests
|
18 |
default => sub { {} } } } |
add database method
|
19 |
sub database : ClassObjectAttr { initialize => {clone => 'scalar'} } |
catch up Object::Simple upda...
|
20 | |
update document
|
21 |
sub bind_filter : ClassObjectAttr { initialize => {clone => 'scalar'} } |
22 |
sub fetch_filter : ClassObjectAttr { initialize => {clone => 'scalar'} } |
|
catch up Object::Simple upda...
|
23 | |
add no_bind_filters
|
24 |
sub no_bind_filters : ClassObjectAttr { initialize => {clone => 'array'} } |
25 |
sub no_fetch_filters : ClassObjectAttr { initialize => {clone => 'array'} } |
|
add various thins
|
26 | |
catch up Object::Simple upda...
|
27 |
sub filters : ClassObjectAttr { |
28 |
type => 'hash', |
|
29 |
deref => 1, |
|
30 |
initialize => { |
|
add tests
|
31 |
clone => 'hash', |
32 |
default => sub { {} } |
|
catch up Object::Simple upda...
|
33 |
} |
34 |
} |
|
first commit
|
35 | |
add formats add_format
|
36 |
sub formats : ClassObjectAttr { |
37 |
type => 'hash', |
|
38 |
deref => 1, |
|
39 |
initialize => { |
|
40 |
clone => 'hash', |
|
41 |
default => sub { {} } |
|
42 |
} |
|
43 |
} |
|
44 | ||
catch up Object::Simple upda...
|
45 |
sub result_class : ClassObjectAttr { |
46 |
initialize => { |
|
add tests
|
47 |
clone => 'scalar', |
Version 0.0101 release
|
48 |
default => 'DBIx::Custom::Result' |
catch up Object::Simple upda...
|
49 |
} |
50 |
} |
|
cleanup
|
51 | |
catch up Object::Simple upda...
|
52 |
sub sql_template : ClassObjectAttr { |
53 |
initialize => { |
|
update document
|
54 |
clone => sub {$_[0] ? $_[0]->clone : undef}, |
Version 0.0101 release
|
55 |
default => sub {DBIx::Custom::SQL::Template->new} |
catch up Object::Simple upda...
|
56 |
} |
57 |
} |
|
cleanup
|
58 | |
add tests
|
59 |
### Object Accessor |
add tests
|
60 |
sub dbh : Attr {} |
add tests
|
61 | |
62 | ||
63 |
### Methods |
|
add various thins
|
64 | |
add tests
|
65 |
# Add filter |
66 |
sub add_filter { |
|
67 |
my $invocant = shift; |
|
68 |
|
|
69 |
my %old_filters = $invocant->filters; |
|
70 |
my %new_filters = ref $_[0] eq 'HASH' ? %{$_[0]} : @_; |
|
71 |
$invocant->filters(%old_filters, %new_filters); |
|
update document
|
72 |
return $invocant; |
add tests
|
73 |
} |
add various
|
74 | |
add formats add_format
|
75 |
# Add format |
76 |
sub add_format{ |
|
77 |
my $invocant = shift; |
|
78 |
|
|
79 |
my %old_formats = $invocant->formats; |
|
80 |
my %new_formats = ref $_[0] eq 'HASH' ? %{$_[0]} : @_; |
|
81 |
$invocant->formats(%old_formats, %new_formats); |
|
82 |
return $invocant; |
|
83 |
} |
|
84 | ||
add various
|
85 |
# Auto commit |
update document
|
86 |
sub _auto_commit { |
add various
|
87 |
my $self = shift; |
88 |
|
|
add tests
|
89 |
croak("Not yet connect to database") unless $self->dbh; |
add various
|
90 |
|
91 |
if (@_) { |
|
92 |
$self->dbh->{AutoCommit} = $_[0]; |
|
93 |
return $self; |
|
94 |
} |
|
95 |
return $self->dbh->{AutoCommit}; |
|
96 |
} |
|
add test
|
97 | |
add various things
|
98 |
# Connect |
add some method
|
99 |
sub connect { |
100 |
my $self = shift; |
|
update document
|
101 |
my $data_source = $self->data_source; |
102 |
my $user = $self->user; |
|
103 |
my $password = $self->password; |
|
cleanup
|
104 |
my $dbi_options = $self->dbi_options; |
add test
|
105 |
|
add tests
|
106 |
my $dbh = eval{DBI->connect( |
update document
|
107 |
$data_source, |
108 |
$user, |
|
109 |
$password, |
|
add some method
|
110 |
{ |
111 |
RaiseError => 1, |
|
112 |
PrintError => 0, |
|
113 |
AutoCommit => 1, |
|
cleanup
|
114 |
%{$dbi_options || {} } |
add some method
|
115 |
} |
add tests
|
116 |
)}; |
117 |
|
|
118 |
croak $@ if $@; |
|
add some method
|
119 |
|
120 |
$self->dbh($dbh); |
|
add various
|
121 |
return $self; |
add some method
|
122 |
} |
first commit
|
123 | |
add tests
|
124 |
# DESTROY |
add tests
|
125 |
sub DESTROY { |
126 |
my $self = shift; |
|
add tests
|
127 |
$self->disconnect if $self->connected; |
add tests
|
128 |
} |
129 | ||
add various things
|
130 |
# Is connected? |
131 |
sub connected { |
|
132 |
my $self = shift; |
|
cleanup
|
133 |
return ref $self->{dbh} eq 'DBI::db'; |
add various things
|
134 |
} |
135 | ||
136 |
# Disconnect |
|
137 |
sub disconnect { |
|
138 |
my $self = shift; |
|
add tests
|
139 |
if ($self->connected) { |
add various things
|
140 |
$self->dbh->disconnect; |
141 |
delete $self->{dbh}; |
|
142 |
} |
|
143 |
} |
|
144 | ||
145 |
# Reconnect |
|
146 |
sub reconnect { |
|
147 |
my $self = shift; |
|
add tests
|
148 |
$self->disconnect if $self->connected; |
add various things
|
149 |
$self->connect; |
150 |
} |
|
151 | ||
cleanup
|
152 |
# Prepare statement handle |
add prepare
|
153 |
sub prepare { |
154 |
my ($self, $sql) = @_; |
|
cleanup
|
155 |
|
156 |
# Connect if not |
|
add tests
|
157 |
$self->connect unless $self->connected; |
add prepare
|
158 |
|
cleanup
|
159 |
# Prepare |
add prepare
|
160 |
my $sth = eval{$self->dbh->prepare($sql)}; |
add tests
|
161 |
|
162 |
# Error |
|
163 |
croak("$@<Your SQL>\n$sql") if $@; |
|
164 |
|
|
add prepare
|
165 |
return $sth; |
166 |
} |
|
167 | ||
cleanup
|
168 |
# Execute SQL directly |
add prepare
|
169 |
sub do{ |
170 |
my ($self, $sql, @bind_values) = @_; |
|
cleanup
|
171 |
|
172 |
# Connect if not |
|
add tests
|
173 |
$self->connect unless $self->connected; |
add prepare
|
174 |
|
cleanup
|
175 |
# Do |
add tests
|
176 |
my $ret_val = eval{$self->dbh->do($sql, @bind_values)}; |
177 |
|
|
178 |
# Error |
|
179 |
if ($@) { |
|
180 |
my $error = $@; |
|
181 |
require Data::Dumper; |
|
182 |
|
|
183 |
my $bind_value_dump |
|
184 |
= Data::Dumper->Dump([\@bind_values], ['*bind_valuds']); |
|
185 |
|
|
186 |
croak("$error<Your SQL>\n$sql\n<Your bind values>\n$bind_value_dump\n"); |
|
187 |
} |
|
add prepare
|
188 |
} |
189 | ||
cleanup
|
190 |
# Create query |
add various thins
|
191 |
sub create_query { |
192 |
my ($self, $template) = @_; |
|
add query cache system
|
193 |
my $class = ref $self; |
add test
|
194 |
|
add various thins
|
195 |
# Create query from SQL template |
add prepare
|
196 |
my $sql_template = $self->sql_template; |
add test
|
197 |
|
add query cache system
|
198 |
# Try to get cached query |
199 |
my $query = $class->_query_caches->{$template}; |
|
200 |
|
|
201 |
# Create query |
|
202 |
unless ($query) { |
|
203 |
$query = eval{$sql_template->create_query($template)}; |
|
204 |
croak($@) if $@; |
|
205 |
|
|
Version 0.0101 release
|
206 |
$query = DBIx::Custom::Query->new($query); |
add query cache system
|
207 |
|
208 |
$class->_add_query_cache($template, $query); |
|
209 |
} |
|
add various thins
|
210 |
|
cleanup
|
211 |
# Connect if not |
add tests
|
212 |
$self->connect unless $self->connected; |
try varioud way
|
213 |
|
add various thins
|
214 |
# Prepare statement handle |
add tests
|
215 |
my $sth = $self->prepare($query->{sql}); |
add tests
|
216 |
|
add no_bind_filters
|
217 |
# Set statement handle |
add various thins
|
218 |
$query->sth($sth); |
add tests
|
219 |
|
add no_bind_filters
|
220 |
# Set bind filter |
221 |
$query->bind_filter($self->bind_filter); |
|
222 |
|
|
223 |
# Set no filter keys when binding |
|
224 |
$query->no_bind_filters($self->no_bind_filters); |
|
add tests
|
225 |
|
add no_bind_filters
|
226 |
# Set fetch filter |
227 |
$query->fetch_filter($self->fetch_filter); |
|
228 |
|
|
229 |
# Set no filter keys when fetching |
|
230 |
$query->no_fetch_filters($self->no_fetch_filters); |
|
231 |
|
|
add various thins
|
232 |
return $query; |
233 |
} |
|
234 | ||
cleanup
|
235 |
# Execute query |
add various thins
|
236 |
sub execute { |
237 |
my ($self, $query, $params) = @_; |
|
add tests
|
238 |
$params ||= {}; |
try varioud way
|
239 |
|
add tests
|
240 |
# First argument is SQL template |
241 |
if (!ref $query) { |
|
242 |
my $template = $query; |
|
243 |
$query = $self->create_query($template); |
|
244 |
my $query_edit_cb = $_[3]; |
|
245 |
$query_edit_cb->($query) if ref $query_edit_cb eq 'CODE'; |
|
246 |
} |
|
247 |
|
|
add various thins
|
248 |
# Create bind value |
249 |
my $bind_values = $self->_build_bind_values($query, $params); |
|
add tests
|
250 |
|
cleanup
|
251 |
# Execute |
cleanup
|
252 |
my $sth = $query->sth; |
add tests
|
253 |
my $ret_val = eval{$sth->execute(@$bind_values)}; |
cleanup
|
254 |
|
255 |
# Execute error |
|
add tests
|
256 |
if (my $execute_error = $@) { |
add tests
|
257 |
require Data::Dumper; |
add tests
|
258 |
my $sql = $query->{sql} || ''; |
259 |
my $key_infos_dump = Data::Dumper->Dump([$query->key_infos], ['*key_infos']); |
|
260 |
my $params_dump = Data::Dumper->Dump([$params], ['*params']); |
|
add tests
|
261 |
|
add tests
|
262 |
croak("$execute_error" . |
263 |
"<Your SQL>\n$sql\n" . |
|
264 |
"<Your parameters>\n$params_dump"); |
|
add tests
|
265 |
} |
add various things
|
266 |
|
cleanup
|
267 |
# Return resultset if select statement is executed |
add various things
|
268 |
if ($sth->{NUM_OF_FIELDS}) { |
cleanup
|
269 |
|
270 |
# Get result class |
|
add various things
|
271 |
my $result_class = $self->result_class; |
cleanup
|
272 |
|
273 |
# Create result |
|
add various
|
274 |
my $result = $result_class->new({ |
add no_bind_filters
|
275 |
sth => $sth, |
276 |
fetch_filter => $query->fetch_filter, |
|
277 |
no_fetch_filters => $query->no_fetch_filters |
|
add various
|
278 |
}); |
add various things
|
279 |
return $result; |
280 |
} |
|
add tests
|
281 |
return $ret_val; |
add test
|
282 |
} |
283 | ||
cleanup
|
284 |
# Build binding values |
add various thins
|
285 |
sub _build_bind_values { |
286 |
my ($self, $query, $params) = @_; |
|
add no_bind_filters
|
287 |
my $key_infos = $query->key_infos; |
288 |
my $bind_filter = $query->bind_filter; |
|
289 |
my $no_bind_filters_map = $query->_no_bind_filters_map || {}; |
|
cleanup
|
290 |
|
add various thins
|
291 |
# binding values |
292 |
my @bind_values; |
|
cleanup
|
293 |
|
add tests
|
294 |
# Create bind values |
add tests
|
295 |
KEY_INFOS : |
cleanup
|
296 |
foreach my $key_info (@$key_infos) { |
cleanup
|
297 |
# Set variable |
298 |
my $access_keys = $key_info->{access_keys}; |
|
cleanup
|
299 |
my $original_key = $key_info->{original_key} || ''; |
300 |
my $table = $key_info->{table} || ''; |
|
301 |
my $column = $key_info->{column} || ''; |
|
add various thins
|
302 |
|
cleanup
|
303 |
# Key is found? |
add tests
|
304 |
my $found; |
cleanup
|
305 |
|
306 |
# Build bind values |
|
add various thins
|
307 |
ACCESS_KEYS : |
308 |
foreach my $access_key (@$access_keys) { |
|
cleanup
|
309 |
# Root parameter |
add various thins
|
310 |
my $root_params = $params; |
cleanup
|
311 |
|
312 |
# Search corresponding value |
|
add various thins
|
313 |
for (my $i = 0; $i < @$access_key; $i++) { |
cleanup
|
314 |
# Current key |
315 |
my $current_key = $access_key->[$i]; |
|
add various thins
|
316 |
|
cleanup
|
317 |
# Last key |
add various thins
|
318 |
if ($i == @$access_key - 1) { |
cleanup
|
319 |
# Key is array reference |
320 |
if (ref $current_key eq 'ARRAY') { |
|
321 |
# Filtering |
|
322 |
if ($bind_filter && |
|
323 |
!$no_bind_filters_map->{$original_key}) |
|
324 |
{ |
|
add no_bind_filters
|
325 |
push @bind_values, |
cleanup
|
326 |
$bind_filter->($original_key, |
327 |
$root_params->[$current_key->[0]], |
|
cleanup
|
328 |
$table, $column); |
add various thins
|
329 |
} |
cleanup
|
330 |
# Not filtering |
add various thins
|
331 |
else { |
cleanup
|
332 |
push @bind_values, |
333 |
scalar $root_params->[$current_key->[0]]; |
|
add various thins
|
334 |
} |
335 |
} |
|
cleanup
|
336 |
# Key is string |
add various thins
|
337 |
else { |
cleanup
|
338 |
# Key is not found |
339 |
next ACCESS_KEYS |
|
340 |
unless exists $root_params->{$current_key}; |
|
341 |
|
|
342 |
# Filtering |
|
343 |
if ($bind_filter && |
|
344 |
!$no_bind_filters_map->{$original_key}) |
|
345 |
{ |
|
add no_bind_filters
|
346 |
push @bind_values, |
cleanup
|
347 |
$bind_filter->($original_key, |
348 |
$root_params->{$current_key}, |
|
cleanup
|
349 |
$table, $column); |
add various thins
|
350 |
} |
cleanup
|
351 |
# Not filtering |
add various thins
|
352 |
else { |
cleanup
|
353 |
push @bind_values, |
354 |
scalar $root_params->{$current_key}; |
|
add various thins
|
355 |
} |
356 |
} |
|
cleanup
|
357 |
|
358 |
# Key is found |
|
add tests
|
359 |
$found = 1; |
add tests
|
360 |
next KEY_INFOS; |
add various thins
|
361 |
} |
cleanup
|
362 |
# First or middle key |
add various thins
|
363 |
else { |
cleanup
|
364 |
# Key is array reference |
365 |
if (ref $current_key eq 'ARRAY') { |
|
366 |
# Go next key |
|
367 |
$root_params = $root_params->[$current_key->[0]]; |
|
368 |
} |
|
369 |
# Key is string |
|
370 |
else { |
|
371 |
# Not found |
|
372 |
next ACCESS_KEYS |
|
373 |
unless exists $root_params->{$current_key}; |
|
374 |
|
|
375 |
# Go next key |
|
376 |
$root_params = $root_params->{$current_key}; |
|
377 |
} |
|
add various thins
|
378 |
} |
379 |
} |
|
380 |
} |
|
add tests
|
381 |
|
cleanup
|
382 |
# Key is not found |
add tests
|
383 |
unless ($found) { |
384 |
require Data::Dumper; |
|
add tests
|
385 |
my $key_info_dump = Data::Dumper->Dump([$key_info], ['*key_info']); |
add tests
|
386 |
my $params_dump = Data::Dumper->Dump([$params], ['*params']); |
add tests
|
387 |
croak("Corresponding key is not found in your parameters\n" . |
add tests
|
388 |
"<Key information>\n$key_info_dump\n\n" . |
389 |
"<Your parameters>\n$params_dump\n"); |
|
390 |
} |
|
update document
|
391 |
} |
add tests
|
392 |
return \@bind_values; |
add test
|
393 |
} |
394 | ||
rename tranzaction to transa...
|
395 |
# Run transaction |
396 |
sub run_transaction { |
|
397 |
my ($self, $transaction) = @_; |
|
add tests
|
398 |
|
399 |
# Check auto commit |
|
rename tranzaction to transa...
|
400 |
croak("AutoCommit must be true before transaction start") |
add tests
|
401 |
unless $self->_auto_commit; |
402 |
|
|
403 |
# Auto commit off |
|
404 |
$self->_auto_commit(0); |
|
405 |
|
|
rename tranzaction to transa...
|
406 |
# Run transaction |
407 |
eval {$transaction->()}; |
|
add tests
|
408 |
|
409 |
# Tranzaction error |
|
rename tranzaction to transa...
|
410 |
my $transaction_error = $@; |
add tests
|
411 |
|
412 |
# Tranzaction is failed. |
|
rename tranzaction to transa...
|
413 |
if ($transaction_error) { |
add tests
|
414 |
# Rollback |
415 |
eval{$self->dbh->rollback}; |
|
416 |
|
|
417 |
# Rollback error |
|
418 |
my $rollback_error = $@; |
|
419 |
|
|
420 |
# Auto commit on |
|
421 |
$self->_auto_commit(1); |
|
422 |
|
|
423 |
if ($rollback_error) { |
|
424 |
# Rollback is failed |
|
rename tranzaction to transa...
|
425 |
croak("${transaction_error}Rollback is failed : $rollback_error"); |
add tests
|
426 |
} |
427 |
else { |
|
428 |
# Rollback is success |
|
rename tranzaction to transa...
|
429 |
croak("${transaction_error}Rollback is success"); |
add tests
|
430 |
} |
431 |
} |
|
432 |
# Tranzaction is success |
|
433 |
else { |
|
434 |
# Commit |
|
435 |
eval{$self->dbh->commit}; |
|
436 |
my $commit_error = $@; |
|
437 |
|
|
438 |
# Auto commit on |
|
439 |
$self->_auto_commit(1); |
|
440 |
|
|
441 |
# Commit is failed |
|
442 |
croak($commit_error) if $commit_error; |
|
443 |
} |
|
444 |
} |
|
add various thins
|
445 | |
add methods
|
446 |
# Get last insert id |
447 |
sub last_insert_id { |
|
448 |
my $self = shift; |
|
449 |
|
|
450 |
# Not connected |
|
451 |
croak("Not yet connect to database") |
|
452 |
unless $self->connected; |
|
453 |
|
|
454 |
return $self->dbh->last_insert_id(@_); |
|
455 |
} |
|
456 | ||
457 |
# Insert |
|
458 |
sub insert { |
|
add tests
|
459 |
my ($self, $table, $insert_params, $query_edit_cb) = @_; |
add tests
|
460 |
$table ||= ''; |
add methods
|
461 |
$insert_params ||= {}; |
462 |
|
|
463 |
# Insert keys |
|
464 |
my @insert_keys = keys %$insert_params; |
|
465 |
|
|
466 |
# Not exists insert keys |
|
add tests
|
467 |
croak("Key-value pairs for insert must be specified to 'insert' second argument") |
add methods
|
468 |
unless @insert_keys; |
469 |
|
|
470 |
# Templte for insert |
|
471 |
my $template = "insert into $table {insert " . join(' ', @insert_keys) . '}'; |
|
472 |
|
|
473 |
# Create query |
|
474 |
my $query = $self->create_query($template); |
|
475 |
|
|
add tests
|
476 |
# Query edit callback must be code reference |
477 |
croak("Query edit callback must be code reference") |
|
478 |
if $query_edit_cb && ref $query_edit_cb ne 'CODE'; |
|
add methods
|
479 |
|
add tests
|
480 |
# Query edit if need |
481 |
$query_edit_cb->($query) if $query_edit_cb; |
|
add methods
|
482 |
|
483 |
# Execute query |
|
484 |
my $ret_val = $self->execute($query, $insert_params); |
|
485 |
|
|
486 |
return $ret_val; |
|
487 |
} |
|
488 | ||
add tests
|
489 |
# Update |
add methods
|
490 |
sub update { |
491 |
my ($self, $table, $update_params, |
|
add tests
|
492 |
$where_params, $query_edit_cb, $options) = @_; |
add methods
|
493 |
|
add tests
|
494 |
$table ||= ''; |
add methods
|
495 |
$update_params ||= {}; |
496 |
$where_params ||= {}; |
|
497 |
|
|
498 |
# Update keys |
|
add tests
|
499 |
my @update_keys = keys %$update_params; |
add methods
|
500 |
|
501 |
# Not exists update kyes |
|
add tests
|
502 |
croak("Key-value pairs for update must be specified to 'update' second argument") |
add methods
|
503 |
unless @update_keys; |
504 |
|
|
505 |
# Where keys |
|
506 |
my @where_keys = keys %$where_params; |
|
507 |
|
|
508 |
# Not exists where keys |
|
add tests
|
509 |
croak("Key-value pairs for where clause must be specified to 'update' third argument") |
add methods
|
510 |
if !@where_keys && !$options->{allow_update_all}; |
511 |
|
|
512 |
# Update clause |
|
513 |
my $update_clause = '{update ' . join(' ', @update_keys) . '}'; |
|
514 |
|
|
515 |
# Where clause |
|
add tests
|
516 |
my $where_clause = ''; |
517 |
if (@where_keys) { |
|
518 |
$where_clause = 'where '; |
|
519 |
foreach my $where_key (@where_keys) { |
|
add tests
|
520 |
$where_clause .= "{= $where_key} and "; |
add tests
|
521 |
} |
add tests
|
522 |
$where_clause =~ s/ and $//; |
add methods
|
523 |
} |
524 |
|
|
525 |
# Template for update |
|
526 |
my $template = "update $table $update_clause $where_clause"; |
|
527 |
|
|
528 |
# Create query |
|
529 |
my $query = $self->create_query($template); |
|
530 |
|
|
add tests
|
531 |
# Query edit callback must be code reference |
532 |
croak("Query edit callback must be code reference") |
|
533 |
if $query_edit_cb && ref $query_edit_cb ne 'CODE'; |
|
add methods
|
534 |
|
add tests
|
535 |
# Query edit if need |
536 |
$query_edit_cb->($query) if $query_edit_cb; |
|
add methods
|
537 |
|
538 |
# Rearrange parammeters |
|
539 |
my $params = {'#update' => $update_params, %$where_params}; |
|
540 |
|
|
541 |
# Execute query |
|
542 |
my $ret_val = $self->execute($query, $params); |
|
543 |
|
|
544 |
return $ret_val; |
|
545 |
} |
|
546 | ||
547 |
# Update all rows |
|
548 |
sub update_all { |
|
add tests
|
549 |
my ($self, $table, $update_params, $query_edit_cb) = @_; |
add methods
|
550 |
|
add tests
|
551 |
return $self->update($table, $update_params, {}, $query_edit_cb, |
add methods
|
552 |
{allow_update_all => 1}); |
553 |
} |
|
554 | ||
555 |
# Delete |
|
556 |
sub delete { |
|
add tests
|
557 |
my ($self, $table, $where_params, $query_edit_cb, $options) = @_; |
add tests
|
558 |
$table ||= ''; |
add methods
|
559 |
$where_params ||= {}; |
560 |
|
|
561 |
# Where keys |
|
562 |
my @where_keys = keys %$where_params; |
|
563 |
|
|
564 |
# Not exists where keys |
|
add tests
|
565 |
croak("Key-value pairs for where clause must be specified to 'delete' second argument") |
566 |
if !@where_keys && !$options->{allow_delete_all}; |
|
add methods
|
567 |
|
568 |
# Where clause |
|
add tests
|
569 |
my $where_clause = ''; |
570 |
if (@where_keys) { |
|
571 |
$where_clause = 'where '; |
|
572 |
foreach my $where_key (@where_keys) { |
|
add tests
|
573 |
$where_clause .= "{= $where_key} and "; |
add tests
|
574 |
} |
add tests
|
575 |
$where_clause =~ s/ and $//; |
add methods
|
576 |
} |
577 |
|
|
578 |
# Template for delete |
|
579 |
my $template = "delete from $table $where_clause"; |
|
580 |
|
|
581 |
# Create query |
|
582 |
my $query = $self->create_query($template); |
|
583 |
|
|
add tests
|
584 |
# Query edit callback must be code reference |
585 |
croak("Query edit callback must be code reference") |
|
586 |
if $query_edit_cb && ref $query_edit_cb ne 'CODE'; |
|
add methods
|
587 |
|
add tests
|
588 |
# Query edit if need |
589 |
$query_edit_cb->($query) if $query_edit_cb; |
|
add methods
|
590 |
|
591 |
# Execute query |
|
592 |
my $ret_val = $self->execute($query, $where_params); |
|
593 |
|
|
594 |
return $ret_val; |
|
595 |
} |
|
596 | ||
597 |
# Delete all rows |
|
598 |
sub delete_all { |
|
add tests
|
599 |
my ($self, $table) = @_; |
600 |
return $self->delete($table, {}, undef, {allow_delete_all => 1}); |
|
add methods
|
601 |
} |
602 | ||
add select
|
603 |
sub _select_usage { return << 'EOS' } |
add select
|
604 |
Your select arguments is wrong. |
605 |
select usage: |
|
add select
|
606 |
$dbi->select( |
add select
|
607 |
$table, # must be string or array ref |
add select
|
608 |
[@$columns], # must be array reference. this is optional |
609 |
{%$where_params}, # must be hash reference. this is optional |
|
610 |
$append_statement, # must be string. this is optional |
|
611 |
$query_edit_callback # must be code reference. this is optional |
|
612 |
); |
|
613 |
EOS |
|
614 | ||
615 |
sub select { |
|
616 |
my $self = shift; |
|
617 |
|
|
618 |
# Check argument |
|
619 |
croak($self->_select_usage) unless @_; |
|
620 |
|
|
621 |
# Arguments |
|
622 |
my $tables = shift || ''; |
|
623 |
$tables = [$tables] unless ref $tables; |
|
624 |
|
|
625 |
my $columns = ref $_[0] eq 'ARRAY' ? shift : []; |
|
626 |
my $where_params = ref $_[0] eq 'HASH' ? shift : {}; |
|
add tests
|
627 |
my $append_statement = $_[0] && !ref $_[0] ? shift : ''; |
add select
|
628 |
my $query_edit_cb = shift if ref $_[0] eq 'CODE'; |
629 |
|
|
630 |
# Check rest argument |
|
add tests
|
631 |
croak($self->_select_usage) if @_; |
add select
|
632 |
|
633 |
# SQL template for select statement |
|
634 |
my $template = 'select '; |
|
635 |
|
|
636 |
# Join column clause |
|
637 |
if (@$columns) { |
|
638 |
foreach my $column (@$columns) { |
|
639 |
$template .= "$column, "; |
|
640 |
} |
|
add tests
|
641 |
$template =~ s/, $/ /; |
add select
|
642 |
} |
643 |
else { |
|
644 |
$template .= '* '; |
|
645 |
} |
|
646 |
|
|
647 |
# Join table |
|
add tests
|
648 |
$template .= 'from '; |
add select
|
649 |
foreach my $table (@$tables) { |
650 |
$template .= "$table, "; |
|
651 |
} |
|
add tests
|
652 |
$template =~ s/, $/ /; |
add select
|
653 |
|
654 |
# Where clause keys |
|
655 |
my @where_keys = keys %$where_params; |
|
656 |
|
|
657 |
# Join where clause |
|
658 |
if (@where_keys) { |
|
659 |
$template .= 'where '; |
|
660 |
foreach my $where_key (@where_keys) { |
|
add tests
|
661 |
$template .= "{= $where_key} and "; |
add select
|
662 |
} |
663 |
} |
|
add tests
|
664 |
$template =~ s/ and $//; |
add select
|
665 |
|
666 |
# Append something to last of statement |
|
667 |
if ($append_statement =~ s/^where //) { |
|
668 |
if (@where_keys) { |
|
add tests
|
669 |
$template .= " and $append_statement"; |
add select
|
670 |
} |
671 |
else { |
|
672 |
$template .= " where $append_statement"; |
|
673 |
} |
|
674 |
} |
|
675 |
else { |
|
676 |
$template .= " $append_statement"; |
|
677 |
} |
|
678 |
|
|
679 |
# Create query |
|
680 |
my $query = $self->create_query($template); |
|
681 |
|
|
682 |
# Query edit |
|
683 |
$query_edit_cb->($query) if $query_edit_cb; |
|
684 |
|
|
685 |
# Execute query |
|
686 |
my $result = $self->execute($query, $where_params); |
|
687 |
|
|
688 |
return $result; |
|
689 |
} |
|
690 | ||
add query cache system
|
691 |
sub _query_caches : ClassAttr { type => 'hash', |
692 |
auto_build => sub {shift->_query_caches({}) } } |
|
693 |
|
|
694 |
sub _query_cache_keys : ClassAttr { type => 'array', |
|
695 |
auto_build => sub {shift->_query_cache_keys([])} } |
|
696 |
|
|
697 |
sub query_cache_max : ClassAttr { auto_build => sub {shift->query_cache_max(50)} } |
|
698 | ||
699 |
# Add query cahce |
|
700 |
sub _add_query_cache { |
|
701 |
my ($class, $template, $query) = @_; |
|
702 |
my $query_cache_keys = $class->_query_cache_keys; |
|
703 |
my $query_caches = $class->_query_caches; |
|
704 |
|
|
705 |
return $class if $query_caches->{$template}; |
|
706 |
|
|
707 |
$query_caches->{$template} = $query; |
|
708 |
push @$query_cache_keys, $template; |
|
709 |
|
|
710 |
my $overflow = @$query_cache_keys - $class->query_cache_max; |
|
711 |
|
|
712 |
for (my $i = 0; $i < $overflow; $i++) { |
|
713 |
my $template = shift @$query_cache_keys; |
|
714 |
delete $query_caches->{$template}; |
|
715 |
} |
|
716 |
|
|
717 |
return $class; |
|
718 |
} |
|
add methods
|
719 | |
rename tranzaction to transa...
|
720 |
# Both bind_filter and fetch_filter off |
721 |
sub filter_off { |
|
722 |
my $self = shift; |
|
723 |
|
|
724 |
# filter off |
|
725 |
$self->bind_filter(undef); |
|
726 |
$self->fetch_filter(undef); |
|
727 |
|
|
728 |
return $self; |
|
729 |
} |
|
730 | ||
add test
|
731 |
Object::Simple->build_class; |
732 | ||
first commit
|
733 |
=head1 NAME |
734 | ||
Version 0.0101 release
|
735 |
DBIx::Custom - Customizable simple DBI |
first commit
|
736 | |
737 |
=head1 VERSION |
|
738 | ||
add test
|
739 |
Version 0.0101 |
first commit
|
740 | |
cleanup
|
741 |
=head1 CAUTION |
742 | ||
743 |
This module is now experimental stage. |
|
744 | ||
745 |
I want you to try this module |
|
746 |
because I want this module stable, and not to damage your DB data by this module bug. |
|
747 | ||
748 |
Please tell me bug if you find |
|
first commit
|
749 | |
750 |
=head1 SYNOPSIS |
|
751 | ||
Version 0.0101 release
|
752 |
my $dbi = DBIx::Custom->new; |
add various thins
|
753 |
|
754 |
my $query = $dbi->create_query($template); |
|
755 |
$dbi->execute($query); |
|
first commit
|
756 | |
update document
|
757 |
=head1 CLASS-OBJECT ACCESSORS |
first commit
|
758 | |
update document
|
759 |
=head2 user |
760 | ||
761 |
# Set and get database user name |
|
762 |
$self = $dbi->user($user); |
|
763 |
$user = $dbi->user; |
|
764 |
|
|
765 |
# Sample |
|
766 |
$dbi->user('taro'); |
|
767 | ||
768 |
=head2 password |
|
769 | ||
770 |
# Set and get database password |
|
771 |
$self = $dbi->password($password); |
|
772 |
$password = $dbi->password; |
|
773 |
|
|
774 |
# Sample |
|
775 |
$dbi->password('lkj&le`@s'); |
|
776 | ||
777 |
=head2 data_source |
|
778 | ||
779 |
# Set and get database data source |
|
780 |
$self = $dbi->data_source($data_soruce); |
|
781 |
$data_source = $dbi->data_source; |
|
782 |
|
|
783 |
# Sample(SQLite) |
|
784 |
$dbi->data_source(dbi:SQLite:dbname=$database); |
|
785 |
|
|
786 |
# Sample(MySQL); |
|
787 |
$dbi->data_source("dbi:mysql:dbname=$database"); |
|
788 |
|
|
789 |
# Sample(PostgreSQL) |
|
790 |
$dbi->data_source("dbi:Pg:dbname=$database"); |
|
cleanup
|
791 |
|
792 |
=head2 database |
|
793 | ||
794 |
# Set and get database name |
|
795 |
$self = $dbi->database($database); |
|
796 |
$database = $dbi->database; |
|
update document
|
797 | |
add database method
|
798 |
This method will be used in subclass connect method. |
799 | ||
cleanup
|
800 |
=head2 dbi_options |
update document
|
801 | |
802 |
# Set and get DBI option |
|
cleanup
|
803 |
$self = $dbi->dbi_options({$options => $value, ...}); |
804 |
$dbi_options = $dbi->dbi_options; |
|
update document
|
805 | |
806 |
# Sample |
|
cleanup
|
807 |
$dbi->dbi_options({PrintError => 0, RaiseError => 1}); |
update document
|
808 | |
cleanup
|
809 |
dbi_options is used when you connect database by using connect. |
update document
|
810 | |
add prepare
|
811 |
=head2 prepare |
812 | ||
813 |
$sth = $dbi->prepare($sql); |
|
814 | ||
815 |
This method is same as DBI::prepare |
|
816 | ||
817 |
=head2 do |
|
818 | ||
819 |
$dbi->do($sql, @bind_values); |
|
820 | ||
821 |
This method is same as DBI::do |
|
822 | ||
update document
|
823 |
=head2 sql_template |
824 | ||
825 |
# Set and get SQL::Template object |
|
826 |
$self = $dbi->sql_template($sql_template); |
|
827 |
$sql_template = $dbi->sql_template; |
|
828 |
|
|
829 |
# Sample |
|
830 |
$dbi->sql_template(DBI::Cutom::SQL::Template->new); |
|
831 | ||
832 |
=head2 filters |
|
833 | ||
834 |
# Set and get filters |
|
835 |
$self = $dbi->filters($filters); |
|
836 |
$filters = $dbi->filters; |
|
first commit
|
837 | |
add formats add_format
|
838 |
=head2 formats |
839 | ||
840 |
# Set and get formats |
|
841 |
$self = $dbi->formats($formats); |
|
842 |
$formats = $dbi->formats; |
|
843 |
|
|
add test
|
844 |
=head2 bind_filter |
first commit
|
845 | |
update document
|
846 |
# Set and get binding filter |
847 |
$self = $dbi->bind_filter($bind_filter); |
|
848 |
$bind_filter = $dbi->bind_filter |
|
first commit
|
849 | |
update document
|
850 |
# Sample |
851 |
$dbi->bind_filter($self->filters->{default_bind_filter}); |
|
852 |
|
|
first commit
|
853 | |
update document
|
854 |
you can get DBI database handle if you need. |
first commit
|
855 | |
add test
|
856 |
=head2 fetch_filter |
first commit
|
857 | |
update document
|
858 |
# Set and get Fetch filter |
859 |
$self = $dbi->fetch_filter($fetch_filter); |
|
860 |
$fetch_filter = $dbi->fetch_filter; |
|
first commit
|
861 | |
update document
|
862 |
# Sample |
863 |
$dbi->fetch_filter($self->filters->{default_fetch_filter}); |
|
add test
|
864 | |
add no_bind_filters
|
865 |
=head2 no_bind_filters |
866 | ||
867 |
# Set and get no filter keys when binding |
|
868 |
$self = $dbi->no_bind_filters($no_bind_filters); |
|
869 |
$no_bind_filters = $dbi->no_bind_filters; |
|
870 | ||
871 |
=head2 no_fetch_filters |
|
cleanup
|
872 | |
add no_bind_filters
|
873 |
# Set and get no filter keys when fetching |
874 |
$self = $dbi->no_fetch_filters($no_fetch_filters); |
|
875 |
$no_fetch_filters = $dbi->no_fetch_filters; |
|
cleanup
|
876 | |
update document
|
877 |
=head2 result_class |
first commit
|
878 | |
update document
|
879 |
# Set and get resultset class |
880 |
$self = $dbi->result_class($result_class); |
|
881 |
$result_class = $dbi->result_class; |
|
882 |
|
|
883 |
# Sample |
|
Version 0.0101 release
|
884 |
$dbi->result_class('DBIx::Custom::Result'); |
add test
|
885 | |
update document
|
886 |
=head2 dbh |
add test
|
887 | |
update document
|
888 |
# Get database handle |
889 |
$dbh = $self->dbh; |
|
add test
|
890 | |
update document
|
891 |
=head1 METHODS |
add tests
|
892 | |
update document
|
893 |
=head2 connect |
894 | ||
895 |
# Connect to database |
|
896 |
$self = $dbi->connect; |
|
897 |
|
|
898 |
# Sample |
|
Version 0.0101 release
|
899 |
$dbi = DBIx::Custom->new(user => 'taro', password => 'lji8(', |
update document
|
900 |
data_soruce => "dbi:mysql:dbname=$database"); |
901 |
$dbi->connect; |
|
add tests
|
902 | |
903 |
=head2 disconnect |
|
904 | ||
update document
|
905 |
# Disconnect database |
906 |
$dbi->disconnect; |
|
907 | ||
908 |
If database is already disconnected, this method do noting. |
|
909 | ||
add tests
|
910 |
=head2 reconnect |
911 | ||
update document
|
912 |
# Reconnect |
913 |
$dbi->reconnect; |
|
914 | ||
915 |
=head2 connected |
|
916 | ||
917 |
# Check connected |
|
918 |
$dbi->connected |
|
rename tranzaction to transa...
|
919 |
|
920 |
=head2 filter_off |
|
921 | ||
922 |
# bind_filter and fitch_filter off |
|
923 |
$self->filter_off; |
|
924 |
|
|
925 |
This is equeal to |
|
926 |
|
|
927 |
$self->bind_filter(undef); |
|
928 |
$self->fetch_filter(undef); |
|
update document
|
929 | |
930 |
=head2 add_filter |
|
931 | ||
932 |
# Add filter (hash ref or hash can be recieve) |
|
933 |
$self = $dbi->add_filter({$filter_name => $filter, ...}); |
|
934 |
$self = $dbi->add_filter($filetr_name => $filter, ...); |
|
935 |
|
|
936 |
# Sample |
|
937 |
$dbi->add_filter( |
|
938 |
decode_utf8 => sub { |
|
cleanup
|
939 |
my ($key, $value, $table, $column) = @_; |
update document
|
940 |
return Encode::decode('UTF-8', $value); |
941 |
}, |
|
942 |
datetime_to_string => sub { |
|
cleanup
|
943 |
my ($key, $value, $table, $column) = @_; |
update document
|
944 |
return $value->strftime('%Y-%m-%d %H:%M:%S') |
945 |
}, |
|
946 |
default_bind_filter => sub { |
|
cleanup
|
947 |
my ($key, $value, $table, $column) = @_; |
update document
|
948 |
if (ref $value eq 'Time::Piece') { |
cleanup
|
949 |
return $dbi->filters->{datetime_to_string}->($value); |
update document
|
950 |
} |
951 |
else { |
|
cleanup
|
952 |
return $dbi->filters->{decode_utf8}->($value); |
update document
|
953 |
} |
954 |
}, |
|
955 |
|
|
956 |
encode_utf8 => sub { |
|
cleanup
|
957 |
my ($key, $value) = @_; |
update document
|
958 |
return Encode::encode('UTF-8', $value); |
959 |
}, |
|
960 |
string_to_datetime => sub { |
|
cleanup
|
961 |
my ($key, $value) = @_; |
update document
|
962 |
return DateTime::Format::MySQL->parse_datetime($value); |
963 |
}, |
|
964 |
default_fetch_filter => sub { |
|
cleanup
|
965 |
my ($key, $value, $type, $sth, $i) = @_; |
update document
|
966 |
if ($type eq 'DATETIME') { |
cleanup
|
967 |
return $dbi->filters->{string_to_datetime}->($value); |
update document
|
968 |
} |
969 |
else { |
|
cleanup
|
970 |
return $dbi->filters->{encode_utf8}->($value); |
update document
|
971 |
} |
972 |
} |
|
973 |
); |
|
974 | ||
975 |
add_filter add filter to filters |
|
add tests
|
976 | |
add formats add_format
|
977 |
=head2 add_format |
978 | ||
979 |
$dbi->add_format(date => '%Y:%m:%d'); |
|
980 | ||
cleanup
|
981 |
=head2 create_query |
982 |
|
|
983 |
# Create Query object from SQL template |
|
984 |
my $query = $dbi->create_query($template); |
|
985 |
|
|
986 |
=head2 execute |
|
update document
|
987 | |
988 |
# Parse SQL template and execute SQL |
|
cleanup
|
989 |
$result = $dbi->query($query, $params); |
990 |
$result = $dbi->query($template, $params); # Shorcut |
|
update document
|
991 |
|
992 |
# Sample |
|
add tests
|
993 |
$result = $dbi->query("select * from authors where {= name} and {= age}", |
update document
|
994 |
{author => 'taro', age => 19}); |
995 |
|
|
996 |
while (my @row = $result->fetch) { |
|
997 |
# do something |
|
998 |
} |
|
999 | ||
Version 0.0101 release
|
1000 |
See also L<DBIx::Custom::SQL::Template> |
update document
|
1001 | |
rename tranzaction to transa...
|
1002 |
=head2 run_transaction |
first commit
|
1003 | |
rename tranzaction to transa...
|
1004 |
# Run transaction |
1005 |
$dbi->run_transaction(sub { |
|
update document
|
1006 |
# do something |
1007 |
}); |
|
first commit
|
1008 | |
rename tranzaction to transa...
|
1009 |
If transaction is success, commit is execute. |
update document
|
1010 |
If tranzation is died, rollback is execute. |
first commit
|
1011 | |
add methods
|
1012 |
=head2 insert |
1013 | ||
1014 |
# Insert |
|
1015 |
$dbi->insert($table, $insert_values); |
|
1016 |
|
|
1017 |
# Sample |
|
1018 |
$dbi->insert('books', {title => 'Perl', author => 'Taro'}); |
|
1019 | ||
1020 |
=head2 update |
|
1021 | ||
1022 |
# Update |
|
1023 |
$dbi->update($table, $update_values, $where); |
|
1024 |
|
|
1025 |
# Sample |
|
1026 |
$dbi->update('books', {title => 'Perl', author => 'Taro'}, {id => 5}); |
|
1027 | ||
1028 |
=head2 update_all |
|
1029 | ||
1030 |
# Update all rows |
|
1031 |
$dbi->update($table, $updat_values); |
|
1032 | ||
1033 |
=head2 delete |
|
1034 | ||
1035 |
# Delete |
|
1036 |
$dbi->delete($table, $where); |
|
1037 |
|
|
1038 |
# Sample |
|
1039 |
$dbi->delete('Books', {id => 5}); |
|
1040 | ||
1041 |
=head2 delete_all |
|
1042 | ||
1043 |
# Delete all rows |
|
1044 |
$dbi->delete_all($table); |
|
1045 | ||
1046 |
=head2 last_insert_id |
|
1047 | ||
1048 |
# Get last insert id |
|
1049 |
$last_insert_id = $dbi->last_insert_id; |
|
1050 |
|
|
1051 |
This method is same as DBI last_insert_id; |
|
1052 | ||
add select
|
1053 |
=head2 select |
1054 |
|
|
1055 |
# Select |
|
1056 |
$dbi->select( |
|
1057 |
$table, # must be string or array; |
|
1058 |
[@$columns], # must be array reference. this is optional |
|
1059 |
{%$where_params}, # must be hash reference. this is optional |
|
1060 |
$append_statement, # must be string. this is optional |
|
1061 |
$query_edit_callback # must be code reference. this is optional |
|
1062 |
); |
|
1063 |
|
|
1064 |
# Sample |
|
1065 |
$dbi->select( |
|
1066 |
'Books', |
|
1067 |
['title', 'author'], |
|
1068 |
{id => 1}, |
|
1069 |
"for update", |
|
1070 |
sub { |
|
1071 |
my $query = shift; |
|
1072 |
$query->bind_filter(sub { |
|
1073 |
# ... |
|
1074 |
}); |
|
1075 |
} |
|
1076 |
); |
|
1077 |
|
|
1078 |
# The way to join multi tables |
|
1079 |
$dbi->select( |
|
1080 |
['table1', 'table2'], |
|
1081 |
['table1.id as table1_id', 'title'], |
|
1082 |
{table1.id => 1}, |
|
1083 |
"where table1.id = table2.id", |
|
1084 |
); |
|
1085 | ||
add query cache system
|
1086 |
=head1 Class Accessors |
1087 | ||
1088 |
=head2 query_cache_max |
|
1089 | ||
1090 |
# Max query cache count |
|
1091 |
$class = $class->query_cache_max($query_cache_max); |
|
1092 |
$query_cache_max = $class->query_cache_max; |
|
1093 |
|
|
1094 |
# Sample |
|
Version 0.0101 release
|
1095 |
DBIx::Custom->query_cache_max(50); |
add query cache system
|
1096 | |
add tests
|
1097 |
=head1 CAUTION |
1098 | ||
Version 0.0101 release
|
1099 |
DBIx::Custom have DIB object internal. |
add tests
|
1100 |
This module is work well in the following DBI condition. |
1101 | ||
1102 |
1. AutoCommit is true |
|
1103 |
2. RaiseError is true |
|
1104 | ||
1105 |
By default, Both AutoCommit and RaiseError is true. |
|
1106 |
You must not change these mode not to damage your data. |
|
1107 | ||
1108 |
If you change these mode, |
|
1109 |
you cannot get correct error message, |
|
rename tranzaction to transa...
|
1110 |
or run_transaction may fail. |
add tests
|
1111 | |
add various
|
1112 |
=head1 AUTHOR |
first commit
|
1113 | |
add various
|
1114 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
first commit
|
1115 | |
add tests
|
1116 |
Github L<http://github.com/yuki-kimoto> |
1117 | ||
first commit
|
1118 |
=head1 COPYRIGHT & LICENSE |
1119 | ||
1120 |
Copyright 2009 Yuki Kimoto, all rights reserved. |
|
1121 | ||
1122 |
This program is free software; you can redistribute it and/or modify it |
|
1123 |
under the same terms as Perl itself. |
|
1124 | ||
1125 |
=cut |