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