first commit
|
1 |
package DBI::Custom; |
2 |
use Object::Simple; |
|
add test
|
3 | |
4 |
our $VERSION = '0.0101'; |
|
5 | ||
6 |
use Carp 'croak'; |
|
add some method
|
7 |
use DBI; |
add tests
|
8 |
use DBI::Custom::SQL::Template; |
add tests
|
9 |
use DBI::Custom::Result; |
cleanup
|
10 |
use DBI::Custom::Query; |
add tests
|
11 | |
12 |
### Class-Object Accessors |
|
update document
|
13 |
sub user : ClassObjectAttr { initialize => {clone => 'scalar'} } |
14 |
sub password : ClassObjectAttr { initialize => {clone => 'scalar'} } |
|
15 |
sub data_source : ClassObjectAttr { initialize => {clone => 'scalar'} } |
|
cleanup
|
16 |
sub dbi_options : ClassObjectAttr { initialize => {clone => 'hash', |
add tests
|
17 |
default => sub { {} } } } |
catch up Object::Simple upda...
|
18 | |
update document
|
19 |
sub bind_filter : ClassObjectAttr { initialize => {clone => 'scalar'} } |
20 |
sub fetch_filter : ClassObjectAttr { initialize => {clone => 'scalar'} } |
|
catch up Object::Simple upda...
|
21 | |
add no_bind_filters
|
22 |
sub no_bind_filters : ClassObjectAttr { initialize => {clone => 'array'} } |
23 |
sub no_fetch_filters : ClassObjectAttr { initialize => {clone => 'array'} } |
|
add various thins
|
24 | |
catch up Object::Simple upda...
|
25 |
sub filters : ClassObjectAttr { |
26 |
type => 'hash', |
|
27 |
deref => 1, |
|
28 |
initialize => { |
|
add tests
|
29 |
clone => 'hash', |
30 |
default => sub { {} } |
|
catch up Object::Simple upda...
|
31 |
} |
32 |
} |
|
first commit
|
33 | |
catch up Object::Simple upda...
|
34 |
sub result_class : ClassObjectAttr { |
35 |
initialize => { |
|
add tests
|
36 |
clone => 'scalar', |
37 |
default => 'DBI::Custom::Result' |
|
catch up Object::Simple upda...
|
38 |
} |
39 |
} |
|
cleanup
|
40 | |
catch up Object::Simple upda...
|
41 |
sub sql_template : ClassObjectAttr { |
42 |
initialize => { |
|
update document
|
43 |
clone => sub {$_[0] ? $_[0]->clone : undef}, |
catch up Object::Simple upda...
|
44 |
default => sub {DBI::Custom::SQL::Template->new} |
45 |
} |
|
46 |
} |
|
cleanup
|
47 | |
add tests
|
48 |
### Object Accessor |
add tests
|
49 |
sub dbh : Attr {} |
add tests
|
50 | |
51 | ||
52 |
### Methods |
|
add various thins
|
53 | |
add tests
|
54 |
# Add filter |
55 |
sub add_filter { |
|
56 |
my $invocant = shift; |
|
57 |
|
|
58 |
my %old_filters = $invocant->filters; |
|
59 |
my %new_filters = ref $_[0] eq 'HASH' ? %{$_[0]} : @_; |
|
60 |
$invocant->filters(%old_filters, %new_filters); |
|
update document
|
61 |
return $invocant; |
add tests
|
62 |
} |
add various
|
63 | |
64 |
# Auto commit |
|
update document
|
65 |
sub _auto_commit { |
add various
|
66 |
my $self = shift; |
67 |
|
|
68 |
croak("Cannot change AutoCommit becouse of not connected") |
|
cleanup
|
69 |
unless $self->dbh; |
add various
|
70 |
|
71 |
if (@_) { |
|
72 |
$self->dbh->{AutoCommit} = $_[0]; |
|
73 |
return $self; |
|
74 |
} |
|
75 |
return $self->dbh->{AutoCommit}; |
|
76 |
} |
|
add test
|
77 | |
add various things
|
78 |
# Connect |
add some method
|
79 |
sub connect { |
80 |
my $self = shift; |
|
update document
|
81 |
my $data_source = $self->data_source; |
82 |
my $user = $self->user; |
|
83 |
my $password = $self->password; |
|
cleanup
|
84 |
my $dbi_options = $self->dbi_options; |
add test
|
85 |
|
add some method
|
86 |
my $dbh = DBI->connect( |
update document
|
87 |
$data_source, |
88 |
$user, |
|
89 |
$password, |
|
add some method
|
90 |
{ |
91 |
RaiseError => 1, |
|
92 |
PrintError => 0, |
|
93 |
AutoCommit => 1, |
|
cleanup
|
94 |
%{$dbi_options || {} } |
add some method
|
95 |
} |
96 |
); |
|
97 |
|
|
98 |
$self->dbh($dbh); |
|
add various
|
99 |
return $self; |
add some method
|
100 |
} |
first commit
|
101 | |
add tests
|
102 |
# DESTROY |
add tests
|
103 |
sub DESTROY { |
104 |
my $self = shift; |
|
add tests
|
105 |
$self->disconnect if $self->connected; |
add tests
|
106 |
} |
107 | ||
add various things
|
108 |
# Is connected? |
109 |
sub connected { |
|
110 |
my $self = shift; |
|
cleanup
|
111 |
return ref $self->{dbh} eq 'DBI::db'; |
add various things
|
112 |
} |
113 | ||
114 |
# Disconnect |
|
115 |
sub disconnect { |
|
116 |
my $self = shift; |
|
add tests
|
117 |
if ($self->connected) { |
add various things
|
118 |
$self->dbh->disconnect; |
119 |
delete $self->{dbh}; |
|
120 |
} |
|
121 |
} |
|
122 | ||
123 |
# Reconnect |
|
124 |
sub reconnect { |
|
125 |
my $self = shift; |
|
add tests
|
126 |
$self->disconnect if $self->connected; |
add various things
|
127 |
$self->connect; |
128 |
} |
|
129 | ||
try various
|
130 |
# Run tranzaction |
131 |
sub run_tranzaction { |
|
132 |
my ($self, $tranzaction) = @_; |
|
133 |
|
|
cleanup
|
134 |
# Auto commit off |
update document
|
135 |
$self->_auto_commit(0); |
try various
|
136 |
|
cleanup
|
137 |
# Run tranzaction |
138 |
eval {$tranzaction->()}; |
|
try various
|
139 |
|
cleanup
|
140 |
# Tranzaction error |
141 |
my $tranzaction_error = $@; |
|
142 |
|
|
143 |
# RaiseError on |
|
144 |
my $old_raise_error = $self->dbh->{RaiseError}; |
|
145 |
$self->dbh->{RaiseError} = 1; |
|
146 |
|
|
147 |
# Tranzaction is failed. |
|
148 |
if ($tranzaction_error) { |
|
149 |
# Rollback |
|
150 |
eval{$self->dbh->rollback}; |
|
151 |
|
|
152 |
# Rollback error |
|
153 |
my $rollback_error = $@; |
|
try various
|
154 |
|
cleanup
|
155 |
# Auto commit on |
156 |
$self->_auto_commit(1); |
|
157 |
|
|
158 |
# Restore RaiseError value |
|
159 |
$self->dbh->{RaiseError} = $old_raise_error; |
|
160 |
|
|
161 |
if ($rollback_error) { |
|
162 |
# Rollback is failed |
|
163 |
croak("${tranzaction_error}Rollback is failed : $rollback_error"); |
|
164 |
} |
|
165 |
else { |
|
166 |
# Rollback is success |
|
167 |
croak("${tranzaction_error}Rollback is success"); |
|
168 |
} |
|
169 |
} |
|
170 |
# Tranzaction is success |
|
171 |
else { |
|
172 |
# Commit |
|
173 |
eval{$self->dbh->commit}; |
|
174 |
my $commit_error = $@; |
|
175 |
|
|
176 |
# Auto commit on |
|
177 |
$self->_auto_commit(1); |
|
178 |
|
|
179 |
# Restore RaiseError value |
|
180 |
$self->dbh->{RaiseError} = $old_raise_error; |
|
181 |
|
|
182 |
# Commit is failed |
|
183 |
croak($commit_error) if $commit_error; |
|
try various
|
184 |
} |
add tests
|
185 |
} |
186 | ||
cleanup
|
187 |
# Prepare statement handle |
add prepare
|
188 |
sub prepare { |
189 |
my ($self, $sql) = @_; |
|
cleanup
|
190 |
|
191 |
# Connect if not |
|
add prepare
|
192 |
eval{$self->connect unless $self->connected}; |
193 |
croak($@) if $@; |
|
194 |
|
|
cleanup
|
195 |
# Prepare |
add prepare
|
196 |
my $sth = eval{$self->dbh->prepare($sql)}; |
197 |
croak($@) if $@; |
|
198 |
return $sth; |
|
199 |
} |
|
200 | ||
cleanup
|
201 |
# Execute SQL directly |
add prepare
|
202 |
sub do{ |
203 |
my ($self, $sql, @bind_values) = @_; |
|
cleanup
|
204 |
|
205 |
# Connect if not |
|
add prepare
|
206 |
eval{$self->connect unless $self->connected}; |
207 |
croak($@) if $@; |
|
208 |
|
|
cleanup
|
209 |
# Do |
add prepare
|
210 |
eval{$self->dbh->do($sql, @bind_values)}; |
211 |
croak($@) if $@; |
|
212 |
} |
|
213 | ||
cleanup
|
214 |
# Create query |
add various thins
|
215 |
sub create_query { |
216 |
my ($self, $template) = @_; |
|
add test
|
217 |
|
add various thins
|
218 |
# Create query from SQL template |
add prepare
|
219 |
my $sql_template = $self->sql_template; |
220 |
my $query = eval{$sql_template->create_query($template)}; |
|
221 |
croak($@) if $@; |
|
add test
|
222 |
|
add various thins
|
223 |
# Create Query object; |
cleanup
|
224 |
$query = DBI::Custom::Query->new($query); |
add various thins
|
225 |
|
cleanup
|
226 |
# Connect if not |
227 |
eval{$self->connect unless $self->connected}; |
|
228 |
croak($@) if $@; |
|
try varioud way
|
229 |
|
add various thins
|
230 |
# Prepare statement handle |
add prepare
|
231 |
my $sth = eval{$self->dbh->prepare($query->{sql})}; |
add tests
|
232 |
if ($@) { |
233 |
my $sql = $query->{sql} || ''; |
|
234 |
my $message = "<Created SQL>\n$sql\n"; |
|
235 |
croak("$@$message"); |
|
236 |
} |
|
add tests
|
237 |
|
add no_bind_filters
|
238 |
# Set statement handle |
add various thins
|
239 |
$query->sth($sth); |
add tests
|
240 |
|
add no_bind_filters
|
241 |
# Set bind filter |
242 |
$query->bind_filter($self->bind_filter); |
|
243 |
|
|
244 |
# Set no filter keys when binding |
|
245 |
$query->no_bind_filters($self->no_bind_filters); |
|
add tests
|
246 |
|
add no_bind_filters
|
247 |
# Set fetch filter |
248 |
$query->fetch_filter($self->fetch_filter); |
|
249 |
|
|
250 |
# Set no filter keys when fetching |
|
251 |
$query->no_fetch_filters($self->no_fetch_filters); |
|
252 |
|
|
add various thins
|
253 |
return $query; |
254 |
} |
|
255 | ||
cleanup
|
256 |
# Execute query |
add various thins
|
257 |
sub execute { |
258 |
my ($self, $query, $params) = @_; |
|
add tests
|
259 |
$params ||= {}; |
try varioud way
|
260 |
|
add tests
|
261 |
# First argument is SQL template |
262 |
if (!ref $query) { |
|
263 |
my $template = $query; |
|
264 |
$query = $self->create_query($template); |
|
265 |
my $query_edit_cb = $_[3]; |
|
266 |
$query_edit_cb->($query) if ref $query_edit_cb eq 'CODE'; |
|
267 |
} |
|
268 |
|
|
add various thins
|
269 |
# Create bind value |
270 |
my $bind_values = $self->_build_bind_values($query, $params); |
|
add tests
|
271 |
|
cleanup
|
272 |
# Execute |
cleanup
|
273 |
my $sth = $query->sth; |
add tests
|
274 |
my $ret_val = eval{$sth->execute(@$bind_values)}; |
cleanup
|
275 |
|
276 |
# Execute error |
|
add tests
|
277 |
if ($@) { |
278 |
require Data::Dumper; |
|
279 |
my $sql = $query->{sql} || ''; |
|
280 |
my $params_dump = Data::Dumper->Dump([$params], ['*params']); |
|
281 |
|
|
282 |
my $message = "<Created SQL>\n$sql\n<Your parameters>$params_dump"; |
|
283 |
croak("$@$message"); |
|
284 |
} |
|
add various things
|
285 |
|
cleanup
|
286 |
# Return resultset if select statement is executed |
add various things
|
287 |
if ($sth->{NUM_OF_FIELDS}) { |
cleanup
|
288 |
|
289 |
# Get result class |
|
add various things
|
290 |
my $result_class = $self->result_class; |
cleanup
|
291 |
|
292 |
# Create result |
|
add various
|
293 |
my $result = $result_class->new({ |
add no_bind_filters
|
294 |
sth => $sth, |
295 |
fetch_filter => $query->fetch_filter, |
|
296 |
no_fetch_filters => $query->no_fetch_filters |
|
add various
|
297 |
}); |
add various things
|
298 |
return $result; |
299 |
} |
|
add tests
|
300 |
return $ret_val; |
add test
|
301 |
} |
302 | ||
cleanup
|
303 |
# Build binding values |
add various thins
|
304 |
sub _build_bind_values { |
305 |
my ($self, $query, $params) = @_; |
|
add no_bind_filters
|
306 |
my $key_infos = $query->key_infos; |
307 |
my $bind_filter = $query->bind_filter; |
|
308 |
my $no_bind_filters_map = $query->_no_bind_filters_map || {}; |
|
cleanup
|
309 |
|
add various thins
|
310 |
# binding values |
311 |
my @bind_values; |
|
cleanup
|
312 |
|
add tests
|
313 |
# Create bind values |
cleanup
|
314 |
foreach my $key_info (@$key_infos) { |
cleanup
|
315 |
# Set variable |
316 |
my $access_keys = $key_info->{access_keys}; |
|
cleanup
|
317 |
my $original_key = $key_info->{original_key} || ''; |
318 |
my $table = $key_info->{table} || ''; |
|
319 |
my $column = $key_info->{column} || ''; |
|
add various thins
|
320 |
|
cleanup
|
321 |
# Key is found? |
add tests
|
322 |
my $found; |
cleanup
|
323 |
|
324 |
# Build bind values |
|
add various thins
|
325 |
ACCESS_KEYS : |
326 |
foreach my $access_key (@$access_keys) { |
|
cleanup
|
327 |
# Root parameter |
add various thins
|
328 |
my $root_params = $params; |
cleanup
|
329 |
|
330 |
# Search corresponding value |
|
add various thins
|
331 |
for (my $i = 0; $i < @$access_key; $i++) { |
cleanup
|
332 |
# Current key |
333 |
my $current_key = $access_key->[$i]; |
|
add various thins
|
334 |
|
cleanup
|
335 |
# Each access key must be string or array reference |
add various thins
|
336 |
croak("'access_keys' each value must be string or array reference") |
cleanup
|
337 |
unless (ref $current_key eq 'ARRAY' || |
338 |
($current_key && !ref $current_key)); |
|
add various thins
|
339 |
|
cleanup
|
340 |
# Last key |
add various thins
|
341 |
if ($i == @$access_key - 1) { |
cleanup
|
342 |
# Key is array reference |
343 |
if (ref $current_key eq 'ARRAY') { |
|
344 |
# Filtering |
|
345 |
if ($bind_filter && |
|
346 |
!$no_bind_filters_map->{$original_key}) |
|
347 |
{ |
|
add no_bind_filters
|
348 |
push @bind_values, |
cleanup
|
349 |
$bind_filter->($original_key, |
350 |
$root_params->[$current_key->[0]], |
|
cleanup
|
351 |
$table, $column); |
add various thins
|
352 |
} |
cleanup
|
353 |
# Not filtering |
add various thins
|
354 |
else { |
cleanup
|
355 |
push @bind_values, |
356 |
scalar $root_params->[$current_key->[0]]; |
|
add various thins
|
357 |
} |
358 |
} |
|
cleanup
|
359 |
# Key is string |
add various thins
|
360 |
else { |
cleanup
|
361 |
# Key is not found |
362 |
next ACCESS_KEYS |
|
363 |
unless exists $root_params->{$current_key}; |
|
364 |
|
|
365 |
# Filtering |
|
366 |
if ($bind_filter && |
|
367 |
!$no_bind_filters_map->{$original_key}) |
|
368 |
{ |
|
add no_bind_filters
|
369 |
push @bind_values, |
cleanup
|
370 |
$bind_filter->($original_key, |
371 |
$root_params->{$current_key}, |
|
cleanup
|
372 |
$table, $column); |
add various thins
|
373 |
} |
cleanup
|
374 |
# Not filtering |
add various thins
|
375 |
else { |
cleanup
|
376 |
push @bind_values, |
377 |
scalar $root_params->{$current_key}; |
|
add various thins
|
378 |
} |
379 |
} |
|
cleanup
|
380 |
|
381 |
# Key is found |
|
add tests
|
382 |
$found = 1; |
add various thins
|
383 |
} |
cleanup
|
384 |
# First or middle key |
add various thins
|
385 |
else { |
cleanup
|
386 |
# Key is array reference |
387 |
if (ref $current_key eq 'ARRAY') { |
|
388 |
# Go next key |
|
389 |
$root_params = $root_params->[$current_key->[0]]; |
|
390 |
} |
|
391 |
# Key is string |
|
392 |
else { |
|
393 |
# Not found |
|
394 |
next ACCESS_KEYS |
|
395 |
unless exists $root_params->{$current_key}; |
|
396 |
|
|
397 |
# Go next key |
|
398 |
$root_params = $root_params->{$current_key}; |
|
399 |
} |
|
add various thins
|
400 |
} |
401 |
} |
|
402 |
} |
|
add tests
|
403 |
|
cleanup
|
404 |
# Key is not found |
add tests
|
405 |
unless ($found) { |
406 |
require Data::Dumper; |
|
add tests
|
407 |
my $key_info_dump = Data::Dumper->Dump([$key_info], ['*key_info']); |
add tests
|
408 |
my $params_dump = Data::Dumper->Dump([$params], ['*params']); |
add tests
|
409 |
croak("Key not found in your parameters\n" . |
add tests
|
410 |
"<Key information>\n$key_info_dump\n\n" . |
411 |
"<Your parameters>\n$params_dump\n"); |
|
412 |
} |
|
update document
|
413 |
} |
add tests
|
414 |
return \@bind_values; |
add test
|
415 |
} |
416 | ||
add various thins
|
417 | |
add test
|
418 |
Object::Simple->build_class; |
419 | ||
first commit
|
420 |
=head1 NAME |
421 | ||
add test
|
422 |
DBI::Custom - Customizable simple DBI |
first commit
|
423 | |
424 |
=head1 VERSION |
|
425 | ||
add test
|
426 |
Version 0.0101 |
first commit
|
427 | |
cleanup
|
428 |
=head1 CAUTION |
429 | ||
430 |
This module is now experimental stage. |
|
431 | ||
432 |
I want you to try this module |
|
433 |
because I want this module stable, and not to damage your DB data by this module bug. |
|
434 | ||
435 |
Please tell me bug if you find |
|
first commit
|
436 | |
437 |
=head1 SYNOPSIS |
|
438 | ||
add test
|
439 |
my $dbi = DBI::Custom->new; |
add various thins
|
440 |
|
441 |
my $query = $dbi->create_query($template); |
|
442 |
$dbi->execute($query); |
|
first commit
|
443 | |
update document
|
444 |
=head1 CLASS-OBJECT ACCESSORS |
first commit
|
445 | |
update document
|
446 |
=head2 user |
447 | ||
448 |
# Set and get database user name |
|
449 |
$self = $dbi->user($user); |
|
450 |
$user = $dbi->user; |
|
451 |
|
|
452 |
# Sample |
|
453 |
$dbi->user('taro'); |
|
454 | ||
455 |
=head2 password |
|
456 | ||
457 |
# Set and get database password |
|
458 |
$self = $dbi->password($password); |
|
459 |
$password = $dbi->password; |
|
460 |
|
|
461 |
# Sample |
|
462 |
$dbi->password('lkj&le`@s'); |
|
463 | ||
464 |
=head2 data_source |
|
465 | ||
466 |
# Set and get database data source |
|
467 |
$self = $dbi->data_source($data_soruce); |
|
468 |
$data_source = $dbi->data_source; |
|
469 |
|
|
470 |
# Sample(SQLite) |
|
471 |
$dbi->data_source(dbi:SQLite:dbname=$database); |
|
472 |
|
|
473 |
# Sample(MySQL); |
|
474 |
$dbi->data_source("dbi:mysql:dbname=$database"); |
|
475 |
|
|
476 |
# Sample(PostgreSQL) |
|
477 |
$dbi->data_source("dbi:Pg:dbname=$database"); |
|
cleanup
|
478 |
|
479 |
=head2 database |
|
480 | ||
481 |
# Set and get database name |
|
482 |
$self = $dbi->database($database); |
|
483 |
$database = $dbi->database; |
|
update document
|
484 | |
cleanup
|
485 |
=head2 dbi_options |
update document
|
486 | |
487 |
# Set and get DBI option |
|
cleanup
|
488 |
$self = $dbi->dbi_options({$options => $value, ...}); |
489 |
$dbi_options = $dbi->dbi_options; |
|
update document
|
490 | |
491 |
# Sample |
|
cleanup
|
492 |
$dbi->dbi_options({PrintError => 0, RaiseError => 1}); |
update document
|
493 | |
cleanup
|
494 |
dbi_options is used when you connect database by using connect. |
update document
|
495 | |
add prepare
|
496 |
=head2 prepare |
497 | ||
498 |
$sth = $dbi->prepare($sql); |
|
499 | ||
500 |
This method is same as DBI::prepare |
|
501 | ||
502 |
=head2 do |
|
503 | ||
504 |
$dbi->do($sql, @bind_values); |
|
505 | ||
506 |
This method is same as DBI::do |
|
507 | ||
update document
|
508 |
=head2 sql_template |
509 | ||
510 |
# Set and get SQL::Template object |
|
511 |
$self = $dbi->sql_template($sql_template); |
|
512 |
$sql_template = $dbi->sql_template; |
|
513 |
|
|
514 |
# Sample |
|
515 |
$dbi->sql_template(DBI::Cutom::SQL::Template->new); |
|
516 | ||
517 |
=head2 filters |
|
518 | ||
519 |
# Set and get filters |
|
520 |
$self = $dbi->filters($filters); |
|
521 |
$filters = $dbi->filters; |
|
first commit
|
522 | |
add test
|
523 |
=head2 bind_filter |
first commit
|
524 | |
update document
|
525 |
# Set and get binding filter |
526 |
$self = $dbi->bind_filter($bind_filter); |
|
527 |
$bind_filter = $dbi->bind_filter |
|
first commit
|
528 | |
update document
|
529 |
# Sample |
530 |
$dbi->bind_filter($self->filters->{default_bind_filter}); |
|
531 |
|
|
first commit
|
532 | |
update document
|
533 |
you can get DBI database handle if you need. |
first commit
|
534 | |
add test
|
535 |
=head2 fetch_filter |
first commit
|
536 | |
update document
|
537 |
# Set and get Fetch filter |
538 |
$self = $dbi->fetch_filter($fetch_filter); |
|
539 |
$fetch_filter = $dbi->fetch_filter; |
|
first commit
|
540 | |
update document
|
541 |
# Sample |
542 |
$dbi->fetch_filter($self->filters->{default_fetch_filter}); |
|
add test
|
543 | |
add no_bind_filters
|
544 |
=head2 no_bind_filters |
545 | ||
546 |
# Set and get no filter keys when binding |
|
547 |
$self = $dbi->no_bind_filters($no_bind_filters); |
|
548 |
$no_bind_filters = $dbi->no_bind_filters; |
|
549 | ||
550 |
=head2 no_fetch_filters |
|
cleanup
|
551 | |
add no_bind_filters
|
552 |
# Set and get no filter keys when fetching |
553 |
$self = $dbi->no_fetch_filters($no_fetch_filters); |
|
554 |
$no_fetch_filters = $dbi->no_fetch_filters; |
|
cleanup
|
555 | |
update document
|
556 |
=head2 result_class |
first commit
|
557 | |
update document
|
558 |
# Set and get resultset class |
559 |
$self = $dbi->result_class($result_class); |
|
560 |
$result_class = $dbi->result_class; |
|
561 |
|
|
562 |
# Sample |
|
563 |
$dbi->result_class('DBI::Custom::Result'); |
|
add test
|
564 | |
update document
|
565 |
=head2 dbh |
add test
|
566 | |
update document
|
567 |
# Get database handle |
568 |
$dbh = $self->dbh; |
|
add test
|
569 | |
update document
|
570 |
=head1 METHODS |
add tests
|
571 | |
update document
|
572 |
=head2 connect |
573 | ||
574 |
# Connect to database |
|
575 |
$self = $dbi->connect; |
|
576 |
|
|
577 |
# Sample |
|
578 |
$dbi = DBI::Custom->new(user => 'taro', password => 'lji8(', |
|
579 |
data_soruce => "dbi:mysql:dbname=$database"); |
|
580 |
$dbi->connect; |
|
add tests
|
581 | |
582 |
=head2 disconnect |
|
583 | ||
update document
|
584 |
# Disconnect database |
585 |
$dbi->disconnect; |
|
586 | ||
587 |
If database is already disconnected, this method do noting. |
|
588 | ||
add tests
|
589 |
=head2 reconnect |
590 | ||
update document
|
591 |
# Reconnect |
592 |
$dbi->reconnect; |
|
593 | ||
594 |
=head2 connected |
|
595 | ||
596 |
# Check connected |
|
597 |
$dbi->connected |
|
598 | ||
599 |
=head2 add_filter |
|
600 | ||
601 |
# Add filter (hash ref or hash can be recieve) |
|
602 |
$self = $dbi->add_filter({$filter_name => $filter, ...}); |
|
603 |
$self = $dbi->add_filter($filetr_name => $filter, ...); |
|
604 |
|
|
605 |
# Sample |
|
606 |
$dbi->add_filter( |
|
607 |
decode_utf8 => sub { |
|
cleanup
|
608 |
my ($key, $value, $table, $column) = @_; |
update document
|
609 |
return Encode::decode('UTF-8', $value); |
610 |
}, |
|
611 |
datetime_to_string => sub { |
|
cleanup
|
612 |
my ($key, $value, $table, $column) = @_; |
update document
|
613 |
return $value->strftime('%Y-%m-%d %H:%M:%S') |
614 |
}, |
|
615 |
default_bind_filter => sub { |
|
cleanup
|
616 |
my ($key, $value, $table, $column) = @_; |
update document
|
617 |
if (ref $value eq 'Time::Piece') { |
cleanup
|
618 |
return $dbi->filters->{datetime_to_string}->($value); |
update document
|
619 |
} |
620 |
else { |
|
cleanup
|
621 |
return $dbi->filters->{decode_utf8}->($value); |
update document
|
622 |
} |
623 |
}, |
|
624 |
|
|
625 |
encode_utf8 => sub { |
|
cleanup
|
626 |
my ($key, $value) = @_; |
update document
|
627 |
return Encode::encode('UTF-8', $value); |
628 |
}, |
|
629 |
string_to_datetime => sub { |
|
cleanup
|
630 |
my ($key, $value) = @_; |
update document
|
631 |
return DateTime::Format::MySQL->parse_datetime($value); |
632 |
}, |
|
633 |
default_fetch_filter => sub { |
|
cleanup
|
634 |
my ($key, $value, $type, $sth, $i) = @_; |
update document
|
635 |
if ($type eq 'DATETIME') { |
cleanup
|
636 |
return $dbi->filters->{string_to_datetime}->($value); |
update document
|
637 |
} |
638 |
else { |
|
cleanup
|
639 |
return $dbi->filters->{encode_utf8}->($value); |
update document
|
640 |
} |
641 |
} |
|
642 |
); |
|
643 | ||
644 |
add_filter add filter to filters |
|
add tests
|
645 | |
cleanup
|
646 |
=head2 create_query |
647 |
|
|
648 |
# Create Query object from SQL template |
|
649 |
my $query = $dbi->create_query($template); |
|
650 |
|
|
651 |
=head2 execute |
|
update document
|
652 | |
653 |
# Parse SQL template and execute SQL |
|
cleanup
|
654 |
$result = $dbi->query($query, $params); |
655 |
$result = $dbi->query($template, $params); # Shorcut |
|
update document
|
656 |
|
657 |
# Sample |
|
658 |
$result = $dbi->query("select * from authors where {= name} && {= age}", |
|
659 |
{author => 'taro', age => 19}); |
|
660 |
|
|
661 |
while (my @row = $result->fetch) { |
|
662 |
# do something |
|
663 |
} |
|
664 | ||
665 |
See also L<DBI::Custom::SQL::Template> |
|
666 | ||
cleanup
|
667 |
=head2 run_tranzaction |
first commit
|
668 | |
update document
|
669 |
# Run tranzaction |
670 |
$dbi->run_tranzaction(sub { |
|
671 |
# do something |
|
672 |
}); |
|
first commit
|
673 | |
update document
|
674 |
If tranzaction is success, commit is execute. |
675 |
If tranzation is died, rollback is execute. |
|
first commit
|
676 | |
add various
|
677 |
=head1 AUTHOR |
first commit
|
678 | |
add various
|
679 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
first commit
|
680 | |
add tests
|
681 |
Github L<http://github.com/yuki-kimoto> |
682 | ||
first commit
|
683 |
=head1 COPYRIGHT & LICENSE |
684 | ||
685 |
Copyright 2009 Yuki Kimoto, all rights reserved. |
|
686 | ||
687 |
This program is free software; you can redistribute it and/or modify it |
|
688 |
under the same terms as Perl itself. |
|
689 | ||
690 |
=cut |