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'} } |
|
add various thins
|
16 |
sub database : ClassObjectAttr { initialize => {clone => 'scalar'} } |
catch up Object::Simple upda...
|
17 | |
cleanup
|
18 |
sub dbi_options : ClassObjectAttr { initialize => {clone => 'hash', |
update document
|
19 |
default => sub { {} } } } |
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 | |
catch up Object::Simple upda...
|
36 |
sub result_class : ClassObjectAttr { |
37 |
initialize => { |
|
add tests
|
38 |
clone => 'scalar', |
39 |
default => 'DBI::Custom::Result' |
|
catch up Object::Simple upda...
|
40 |
} |
41 |
} |
|
cleanup
|
42 | |
catch up Object::Simple upda...
|
43 |
sub sql_template : ClassObjectAttr { |
44 |
initialize => { |
|
update document
|
45 |
clone => sub {$_[0] ? $_[0]->clone : undef}, |
catch up Object::Simple upda...
|
46 |
default => sub {DBI::Custom::SQL::Template->new} |
47 |
} |
|
48 |
} |
|
cleanup
|
49 | |
add tests
|
50 |
### Object Accessor |
add tests
|
51 |
sub dbh : Attr {} |
add tests
|
52 | |
53 | ||
54 |
### Methods |
|
add various thins
|
55 | |
add tests
|
56 |
# Add filter |
57 |
sub add_filter { |
|
58 |
my $invocant = shift; |
|
59 |
|
|
60 |
my %old_filters = $invocant->filters; |
|
61 |
my %new_filters = ref $_[0] eq 'HASH' ? %{$_[0]} : @_; |
|
62 |
$invocant->filters(%old_filters, %new_filters); |
|
update document
|
63 |
return $invocant; |
add tests
|
64 |
} |
add various
|
65 | |
66 |
# Auto commit |
|
update document
|
67 |
sub _auto_commit { |
add various
|
68 |
my $self = shift; |
69 |
|
|
70 |
croak("Cannot change AutoCommit becouse of not connected") |
|
71 |
unless $self->dbh; |
|
72 |
|
|
73 |
if (@_) { |
|
74 |
$self->dbh->{AutoCommit} = $_[0]; |
|
75 |
return $self; |
|
76 |
} |
|
77 |
return $self->dbh->{AutoCommit}; |
|
78 |
} |
|
add test
|
79 | |
add various things
|
80 |
# Connect |
add some method
|
81 |
sub connect { |
82 |
my $self = shift; |
|
update document
|
83 |
my $data_source = $self->data_source; |
84 |
my $user = $self->user; |
|
85 |
my $password = $self->password; |
|
cleanup
|
86 |
my $dbi_options = $self->dbi_options; |
add test
|
87 |
|
add some method
|
88 |
my $dbh = DBI->connect( |
update document
|
89 |
$data_source, |
90 |
$user, |
|
91 |
$password, |
|
add some method
|
92 |
{ |
93 |
RaiseError => 1, |
|
94 |
PrintError => 0, |
|
95 |
AutoCommit => 1, |
|
cleanup
|
96 |
%{$dbi_options || {} } |
add some method
|
97 |
} |
98 |
); |
|
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; |
|
add tests
|
113 |
return exists $self->{dbh} && eval {$self->{dbh}->can('prepare')}; |
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 | ||
try various
|
132 |
# Run tranzaction |
133 |
sub run_tranzaction { |
|
134 |
my ($self, $tranzaction) = @_; |
|
135 |
|
|
update document
|
136 |
$self->_auto_commit(0); |
try various
|
137 |
|
138 |
eval { |
|
139 |
$tranzaction->(); |
|
140 |
$self->dbh->commit; |
|
141 |
}; |
|
142 |
|
|
143 |
if ($@) { |
|
144 |
my $tranzaction_error = $@; |
|
145 |
|
|
146 |
$self->dbh->rollback or croak("$@ and rollback also failed"); |
|
147 |
croak("$tranzaction_error"); |
|
148 |
} |
|
update document
|
149 |
$self->_auto_commit(1); |
add tests
|
150 |
} |
151 | ||
add various thins
|
152 |
sub create_query { |
153 |
my ($self, $template) = @_; |
|
add test
|
154 |
|
add various thins
|
155 |
# Create query from SQL template |
156 |
my $query = $self->sql_template->create_query($template); |
|
add test
|
157 |
|
add various thins
|
158 |
# Create Query object; |
cleanup
|
159 |
$query = DBI::Custom::Query->new($query); |
add various thins
|
160 |
|
161 |
# connect if not |
|
162 |
$self->connect unless $self->connected; |
|
try varioud way
|
163 |
|
add various thins
|
164 |
# Prepare statement handle |
165 |
my $sth = $self->dbh->prepare($query->{sql}); |
|
add tests
|
166 |
|
add no_bind_filters
|
167 |
# Set statement handle |
add various thins
|
168 |
$query->sth($sth); |
add tests
|
169 |
|
add no_bind_filters
|
170 |
# Set bind filter |
171 |
$query->bind_filter($self->bind_filter); |
|
172 |
|
|
173 |
# Set no filter keys when binding |
|
174 |
$query->no_bind_filters($self->no_bind_filters); |
|
175 | ||
176 |
# Set fetch filter |
|
177 |
$query->fetch_filter($self->fetch_filter); |
|
178 |
|
|
179 |
# Set no filter keys when fetching |
|
180 |
$query->no_fetch_filters($self->no_fetch_filters); |
|
181 |
|
|
add various thins
|
182 |
return $query; |
183 |
} |
|
184 | ||
185 |
sub execute { |
|
186 |
my ($self, $query, $params) = @_; |
|
add tests
|
187 |
$params ||= {}; |
try varioud way
|
188 |
|
add various thins
|
189 |
# Create bind value |
190 |
my $bind_values = $self->_build_bind_values($query, $params); |
|
add tests
|
191 |
|
cleanup
|
192 |
# Execute |
cleanup
|
193 |
my $sth = $query->sth; |
194 |
my $ret_val = $sth->execute(@$bind_values); |
|
add various things
|
195 |
|
cleanup
|
196 |
# Return resultset if select statement is executed |
add various things
|
197 |
if ($sth->{NUM_OF_FIELDS}) { |
198 |
my $result_class = $self->result_class; |
|
add various
|
199 |
my $result = $result_class->new({ |
add no_bind_filters
|
200 |
sth => $sth, |
201 |
fetch_filter => $query->fetch_filter, |
|
202 |
no_fetch_filters => $query->no_fetch_filters |
|
add various
|
203 |
}); |
add various things
|
204 |
return $result; |
205 |
} |
|
add tests
|
206 |
return $ret_val; |
add test
|
207 |
} |
208 | ||
add various thins
|
209 |
sub _build_bind_values { |
210 |
my ($self, $query, $params) = @_; |
|
cleanup
|
211 |
|
add no_bind_filters
|
212 |
my $key_infos = $query->key_infos; |
213 |
my $bind_filter = $query->bind_filter; |
|
214 |
my $no_bind_filters_map = $query->_no_bind_filters_map || {}; |
|
cleanup
|
215 |
|
add various thins
|
216 |
# binding values |
217 |
my @bind_values; |
|
cleanup
|
218 |
|
add various thins
|
219 |
# Filter and sdd bind values |
cleanup
|
220 |
foreach my $key_info (@$key_infos) { |
221 |
my $filtering_key = $key_info->{key}; |
|
222 |
my $access_keys = $key_info->{access_keys}; |
|
add various thins
|
223 |
|
cleanup
|
224 |
my $original_key = $key_info->{original_key} || ''; |
225 |
my $table = $key_info->{table} || ''; |
|
226 |
my $column = $key_info->{column} || ''; |
|
add various thins
|
227 |
|
228 |
ACCESS_KEYS : |
|
229 |
foreach my $access_key (@$access_keys) { |
|
230 |
my $root_params = $params; |
|
231 |
for (my $i = 0; $i < @$access_key; $i++) { |
|
232 |
my $key = $access_key->[$i]; |
|
233 |
|
|
234 |
croak("'access_keys' each value must be string or array reference") |
|
235 |
unless (ref $key eq 'ARRAY' || ($key && !ref $key)); |
|
236 |
|
|
237 |
if ($i == @$access_key - 1) { |
|
238 |
if (ref $key eq 'ARRAY') { |
|
add no_bind_filters
|
239 |
if ($bind_filter && !$no_bind_filters_map->{$original_key}) { |
240 |
push @bind_values, |
|
241 |
$bind_filter->($root_params->[$key->[0]], |
|
242 |
$original_key, $table, $column); |
|
add various thins
|
243 |
} |
244 |
else { |
|
245 |
push @bind_values, scalar $root_params->[$key->[0]]; |
|
246 |
} |
|
247 |
} |
|
248 |
else { |
|
249 |
next ACCESS_KEYS unless exists $root_params->{$key}; |
|
add no_bind_filters
|
250 |
if ($bind_filter && !$no_bind_filters_map->{$original_key}) { |
251 |
push @bind_values, |
|
252 |
$bind_filter->($root_params->{$key}, |
|
253 |
$original_key, $table, $column); |
|
add various thins
|
254 |
} |
255 |
else { |
|
256 |
push @bind_values, scalar $root_params->{$key}; |
|
257 |
} |
|
258 |
} |
|
259 |
return @bind_values; |
|
260 |
} |
|
261 |
|
|
262 |
if ($key eq 'ARRAY') { |
|
263 |
$root_params = $root_params->[$key->[0]]; |
|
264 |
} |
|
265 |
else { |
|
266 |
next ACCESS_KEYS unless exists $root_params->{$key}; |
|
267 |
$root_params = $root_params->{$key}; |
|
268 |
} |
|
269 |
} |
|
270 |
} |
|
271 |
croak("Cannot find key"); |
|
update document
|
272 |
} |
add test
|
273 |
} |
274 | ||
add various thins
|
275 | |
add test
|
276 |
Object::Simple->build_class; |
277 | ||
first commit
|
278 |
=head1 NAME |
279 | ||
add test
|
280 |
DBI::Custom - Customizable simple DBI |
first commit
|
281 | |
282 |
=head1 VERSION |
|
283 | ||
add test
|
284 |
Version 0.0101 |
first commit
|
285 | |
286 |
=cut |
|
287 | ||
288 |
=head1 SYNOPSIS |
|
289 | ||
add test
|
290 |
my $dbi = DBI::Custom->new; |
add various thins
|
291 |
|
292 |
my $query = $dbi->create_query($template); |
|
293 |
$dbi->execute($query); |
|
first commit
|
294 | |
update document
|
295 |
=head1 CLASS-OBJECT ACCESSORS |
first commit
|
296 | |
update document
|
297 |
=head2 user |
298 | ||
299 |
# Set and get database user name |
|
300 |
$self = $dbi->user($user); |
|
301 |
$user = $dbi->user; |
|
302 |
|
|
303 |
# Sample |
|
304 |
$dbi->user('taro'); |
|
305 | ||
306 |
=head2 password |
|
307 | ||
308 |
# Set and get database password |
|
309 |
$self = $dbi->password($password); |
|
310 |
$password = $dbi->password; |
|
311 |
|
|
312 |
# Sample |
|
313 |
$dbi->password('lkj&le`@s'); |
|
314 | ||
315 |
=head2 data_source |
|
316 | ||
317 |
# Set and get database data source |
|
318 |
$self = $dbi->data_source($data_soruce); |
|
319 |
$data_source = $dbi->data_source; |
|
320 |
|
|
321 |
# Sample(SQLite) |
|
322 |
$dbi->data_source(dbi:SQLite:dbname=$database); |
|
323 |
|
|
324 |
# Sample(MySQL); |
|
325 |
$dbi->data_source("dbi:mysql:dbname=$database"); |
|
326 |
|
|
327 |
# Sample(PostgreSQL) |
|
328 |
$dbi->data_source("dbi:Pg:dbname=$database"); |
|
cleanup
|
329 |
|
330 |
=head2 database |
|
331 | ||
332 |
# Set and get database name |
|
333 |
$self = $dbi->database($database); |
|
334 |
$database = $dbi->database; |
|
update document
|
335 | |
cleanup
|
336 |
=head2 dbi_options |
update document
|
337 | |
338 |
# Set and get DBI option |
|
cleanup
|
339 |
$self = $dbi->dbi_options({$options => $value, ...}); |
340 |
$dbi_options = $dbi->dbi_options; |
|
update document
|
341 | |
342 |
# Sample |
|
cleanup
|
343 |
$dbi->dbi_options({PrintError => 0, RaiseError => 1}); |
update document
|
344 | |
cleanup
|
345 |
dbi_options is used when you connect database by using connect. |
update document
|
346 | |
347 |
=head2 sql_template |
|
348 | ||
349 |
# Set and get SQL::Template object |
|
350 |
$self = $dbi->sql_template($sql_template); |
|
351 |
$sql_template = $dbi->sql_template; |
|
352 |
|
|
353 |
# Sample |
|
354 |
$dbi->sql_template(DBI::Cutom::SQL::Template->new); |
|
355 | ||
356 |
=head2 filters |
|
357 | ||
358 |
# Set and get filters |
|
359 |
$self = $dbi->filters($filters); |
|
360 |
$filters = $dbi->filters; |
|
first commit
|
361 | |
add test
|
362 |
=head2 bind_filter |
first commit
|
363 | |
update document
|
364 |
# Set and get binding filter |
365 |
$self = $dbi->bind_filter($bind_filter); |
|
366 |
$bind_filter = $dbi->bind_filter |
|
first commit
|
367 | |
update document
|
368 |
# Sample |
369 |
$dbi->bind_filter($self->filters->{default_bind_filter}); |
|
370 |
|
|
first commit
|
371 | |
update document
|
372 |
you can get DBI database handle if you need. |
first commit
|
373 | |
add test
|
374 |
=head2 fetch_filter |
first commit
|
375 | |
update document
|
376 |
# Set and get Fetch filter |
377 |
$self = $dbi->fetch_filter($fetch_filter); |
|
378 |
$fetch_filter = $dbi->fetch_filter; |
|
first commit
|
379 | |
update document
|
380 |
# Sample |
381 |
$dbi->fetch_filter($self->filters->{default_fetch_filter}); |
|
add test
|
382 | |
add no_bind_filters
|
383 |
=head2 no_bind_filters |
384 | ||
385 |
# Set and get no filter keys when binding |
|
386 |
$self = $dbi->no_bind_filters($no_bind_filters); |
|
387 |
$no_bind_filters = $dbi->no_bind_filters; |
|
388 | ||
389 |
=head2 no_fetch_filters |
|
cleanup
|
390 | |
add no_bind_filters
|
391 |
# Set and get no filter keys when fetching |
392 |
$self = $dbi->no_fetch_filters($no_fetch_filters); |
|
393 |
$no_fetch_filters = $dbi->no_fetch_filters; |
|
cleanup
|
394 | |
update document
|
395 |
=head2 result_class |
first commit
|
396 | |
update document
|
397 |
# Set and get resultset class |
398 |
$self = $dbi->result_class($result_class); |
|
399 |
$result_class = $dbi->result_class; |
|
400 |
|
|
401 |
# Sample |
|
402 |
$dbi->result_class('DBI::Custom::Result'); |
|
add test
|
403 | |
update document
|
404 |
=head2 dbh |
add test
|
405 | |
update document
|
406 |
# Get database handle |
407 |
$dbh = $self->dbh; |
|
add test
|
408 | |
update document
|
409 |
=head1 METHODS |
add tests
|
410 | |
update document
|
411 |
=head2 connect |
412 | ||
413 |
# Connect to database |
|
414 |
$self = $dbi->connect; |
|
415 |
|
|
416 |
# Sample |
|
417 |
$dbi = DBI::Custom->new(user => 'taro', password => 'lji8(', |
|
418 |
data_soruce => "dbi:mysql:dbname=$database"); |
|
419 |
$dbi->connect; |
|
add tests
|
420 | |
421 |
=head2 disconnect |
|
422 | ||
update document
|
423 |
# Disconnect database |
424 |
$dbi->disconnect; |
|
425 | ||
426 |
If database is already disconnected, this method do noting. |
|
427 | ||
add tests
|
428 |
=head2 reconnect |
429 | ||
update document
|
430 |
# Reconnect |
431 |
$dbi->reconnect; |
|
432 | ||
433 |
=head2 connected |
|
434 | ||
435 |
# Check connected |
|
436 |
$dbi->connected |
|
437 | ||
438 |
=head2 add_filter |
|
439 | ||
440 |
# Add filter (hash ref or hash can be recieve) |
|
441 |
$self = $dbi->add_filter({$filter_name => $filter, ...}); |
|
442 |
$self = $dbi->add_filter($filetr_name => $filter, ...); |
|
443 |
|
|
444 |
# Sample |
|
445 |
$dbi->add_filter( |
|
446 |
decode_utf8 => sub { |
|
447 |
my $value = shift; |
|
448 |
return Encode::decode('UTF-8', $value); |
|
449 |
}, |
|
450 |
datetime_to_string => sub { |
|
451 |
my $value = shift; |
|
452 |
return $value->strftime('%Y-%m-%d %H:%M:%S') |
|
453 |
}, |
|
454 |
default_bind_filter => sub { |
|
455 |
my ($value, $key, $filters) = @_; |
|
456 |
if (ref $value eq 'Time::Piece') { |
|
457 |
return $filters->{datetime_to_string}->($value); |
|
458 |
} |
|
459 |
else { |
|
460 |
return $filters->{decode_utf8}->($value); |
|
461 |
} |
|
462 |
}, |
|
463 |
|
|
464 |
encode_utf8 => sub { |
|
465 |
my $value = shift; |
|
466 |
return Encode::encode('UTF-8', $value); |
|
467 |
}, |
|
468 |
string_to_datetime => sub { |
|
469 |
my $value = shift; |
|
470 |
return DateTime::Format::MySQL->parse_datetime($value); |
|
471 |
}, |
|
472 |
default_fetch_filter => sub { |
|
473 |
my ($value, $key, $filters, $type, $sth, $i) = @_; |
|
474 |
if ($type eq 'DATETIME') { |
|
475 |
return $self->filters->{string_to_datetime}->($value); |
|
476 |
} |
|
477 |
else { |
|
478 |
return $self->filters->{encode_utf8}->($value); |
|
479 |
} |
|
480 |
} |
|
481 |
); |
|
482 | ||
483 |
add_filter add filter to filters |
|
add tests
|
484 | |
cleanup
|
485 |
=head2 create_query |
486 |
|
|
487 |
# Create Query object from SQL template |
|
488 |
my $query = $dbi->create_query($template); |
|
489 |
|
|
490 |
=head2 execute |
|
update document
|
491 | |
492 |
# Parse SQL template and execute SQL |
|
cleanup
|
493 |
$result = $dbi->query($query, $params); |
494 |
$result = $dbi->query($template, $params); # Shorcut |
|
update document
|
495 |
|
496 |
# Sample |
|
497 |
$result = $dbi->query("select * from authors where {= name} && {= age}", |
|
498 |
{author => 'taro', age => 19}); |
|
499 |
|
|
500 |
while (my @row = $result->fetch) { |
|
501 |
# do something |
|
502 |
} |
|
503 | ||
504 |
See also L<DBI::Custom::SQL::Template> |
|
505 | ||
cleanup
|
506 |
=head2 run_tranzaction |
first commit
|
507 | |
update document
|
508 |
# Run tranzaction |
509 |
$dbi->run_tranzaction(sub { |
|
510 |
# do something |
|
511 |
}); |
|
first commit
|
512 | |
update document
|
513 |
If tranzaction is success, commit is execute. |
514 |
If tranzation is died, rollback is execute. |
|
first commit
|
515 | |
cleanup
|
516 | |
517 | ||
add various
|
518 |
=head1 AUTHOR |
first commit
|
519 | |
add various
|
520 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
first commit
|
521 | |
522 |
=head1 COPYRIGHT & LICENSE |
|
523 | ||
524 |
Copyright 2009 Yuki Kimoto, all rights reserved. |
|
525 | ||
526 |
This program is free software; you can redistribute it and/or modify it |
|
527 |
under the same terms as Perl itself. |
|
528 | ||
529 | ||
530 |
=cut |
|
531 | ||
532 |
1; # End of DBI::Custom |