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