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; |
first commit
|
8 | |
cleanup
|
9 |
# Model |
10 |
sub model : ClassAttr { auto_build => \&_inherit_model } |
|
first commit
|
11 | |
cleanup
|
12 |
# Inherit super class model |
13 |
sub _inherit_model { |
|
add test
|
14 |
my $class = shift; |
cleanup
|
15 |
my $super = do { |
16 |
no strict 'refs'; |
|
17 |
${"${class}::ISA"}[0]; |
|
18 |
}; |
|
19 |
my $model = eval{$super->can('model')} |
|
20 |
? $super->model->clone |
|
21 |
: $class->Object::Simple::new; |
|
cleanup
|
22 |
|
23 |
$class->model($model); |
|
first commit
|
24 |
} |
25 | ||
cleanup
|
26 |
# New |
27 |
sub new { |
|
28 |
my $self = shift->Object::Simple::new(@_); |
|
29 |
my $class = ref $self; |
|
30 |
return bless {%{$class->model->clone}, %{$self}}, $class; |
|
first commit
|
31 |
} |
32 | ||
cleanup
|
33 |
# Initialize modle |
34 |
sub initialize_model { |
|
35 |
my ($class, $callback) = @_; |
|
first commit
|
36 |
|
cleanup
|
37 |
# Callback to initialize model |
38 |
$callback->($class->model); |
|
first commit
|
39 |
} |
40 | ||
cleanup
|
41 |
# Clone |
42 |
sub clone { |
|
cleanup
|
43 |
my $self = shift; |
cleanup
|
44 |
my $new = $self->Object::Simple::new; |
add test
|
45 |
$new->connect_info(%{$self->connect_info || {}}); |
cleanup
|
46 |
$new->filters(%{$self->filters || {}}); |
add test
|
47 |
$new->bind_filter($self->bind_filter); |
48 |
$new->fetch_filter($self->fetch_filter); |
|
add various things
|
49 |
$new->result_class($self->result_class); |
cleanup
|
50 |
} |
51 | ||
cleanup
|
52 |
# Attribute |
53 |
sub connect_info : Attr { type => 'hash', auto_build => sub { shift->connect_info({}) } } |
|
cleanup
|
54 | |
add tests
|
55 |
sub bind_filter : Attr {} |
add test
|
56 |
sub fetch_filter : Attr {} |
cleanup
|
57 | |
add test
|
58 |
sub filters : Attr { type => 'hash', deref => 1, auto_build => sub { shift->filters({}) } } |
cleanup
|
59 |
sub add_filter { shift->filters(@_) } |
60 | ||
add tests
|
61 |
sub result_class : Attr { auto_build => sub { shift->result_class('DBI::Custom::Result') }} |
add tests
|
62 |
sub dbh : Attr {} |
add test
|
63 |
sub sql_template : Attr { auto_build => sub { shift->sql_template(DBI::Custom::SQLTemplate->new) } } |
add tests
|
64 |
sub auto_commit : Attr {} |
add test
|
65 | |
add various things
|
66 | |
add test
|
67 |
our %VALID_CONNECT_INFO = map {$_ => 1} qw/data_source user password options/; |
cleanup
|
68 | |
add various things
|
69 |
# Connect |
add some method
|
70 |
sub connect { |
71 |
my $self = shift; |
|
72 |
my $connect_info = $self->connect_info; |
|
73 |
|
|
add test
|
74 |
foreach my $key (keys %{$self->connect_info}) { |
add test
|
75 |
croak("connect_info '$key' is invald") |
76 |
unless $VALID_CONNECT_INFO{$key}; |
|
add test
|
77 |
} |
78 |
|
|
add some method
|
79 |
my $dbh = DBI->connect( |
add test
|
80 |
$connect_info->{data_source}, |
add some method
|
81 |
$connect_info->{user}, |
82 |
$connect_info->{password}, |
|
83 |
{ |
|
84 |
RaiseError => 1, |
|
85 |
PrintError => 0, |
|
86 |
AutoCommit => 1, |
|
87 |
%{$connect_info->{options} || {} } |
|
88 |
} |
|
89 |
); |
|
90 |
|
|
add tests
|
91 |
$self->auto_commit($dbh->{AutoCommit}); |
add some method
|
92 |
$self->dbh($dbh); |
93 |
} |
|
first commit
|
94 | |
add tests
|
95 |
sub DESTROY { |
96 |
my $self = shift; |
|
add tests
|
97 |
$self->disconnect if $self->connected; |
add tests
|
98 |
} |
99 | ||
add various things
|
100 |
# Is connected? |
101 |
sub connected { |
|
102 |
my $self = shift; |
|
add tests
|
103 |
return exists $self->{dbh} && eval {$self->{dbh}->can('prepare')}; |
add various things
|
104 |
} |
105 | ||
106 |
# Disconnect |
|
107 |
sub disconnect { |
|
108 |
my $self = shift; |
|
add tests
|
109 |
if ($self->connected) { |
add various things
|
110 |
$self->dbh->disconnect; |
111 |
delete $self->{dbh}; |
|
112 |
} |
|
113 |
} |
|
114 | ||
115 |
# Reconnect |
|
116 |
sub reconnect { |
|
117 |
my $self = shift; |
|
add tests
|
118 |
$self->disconnect if $self->connected; |
add various things
|
119 |
$self->connect; |
120 |
} |
|
121 | ||
add tests
|
122 |
# Commit |
123 |
sub commit { |
|
124 |
my $self = shift; |
|
125 |
return $self->dbh->commit; |
|
126 |
} |
|
127 | ||
128 |
# Rollback |
|
129 |
sub rollback { |
|
130 |
my $self = shift; |
|
131 |
return $self->dbh->rollback; |
|
132 |
} |
|
133 | ||
add tests
|
134 |
sub dbh_option { |
135 |
my $self = shift; |
|
136 |
croak("Not connected") unless $self->connected; |
|
137 |
my $dbh = $self->dbh; |
|
138 |
if (@_ > 1) { |
|
139 |
$dbh->{$_[0]} = $_[1]; |
|
140 |
return $self; |
|
141 |
} |
|
142 |
return $dbh->{$_[0]} |
|
143 |
} |
|
144 | ||
cleanup
|
145 |
# Create SQL from SQL template |
add test
|
146 |
sub create_sql { |
147 |
my $self = shift; |
|
148 |
|
|
149 |
my ($sql, @bind) = $self->sql_template->create_sql(@_); |
|
150 |
|
|
151 |
return ($sql, @bind); |
|
152 |
} |
|
153 | ||
cleanup
|
154 |
# Prepare and execute SQL |
add some method
|
155 |
sub query { |
try varioud way
|
156 |
my ($self, $template, $values, $filter) = @_; |
157 |
|
|
add tests
|
158 |
my $sth_options; |
159 |
|
|
160 |
# Rearrange when argumets is hash referecne |
|
161 |
if (ref $template eq 'HASH') { |
|
162 |
my $args = $template; |
|
163 |
($template, $values, $filter, $sth_options) |
|
164 |
= @{$args}{qw/template values filter sth_options/}; |
|
165 |
} |
|
166 |
|
|
try varioud way
|
167 |
$filter ||= $self->bind_filter; |
168 |
|
|
add various things
|
169 |
my ($sql, @bind) = $self->create_sql($template, $values, $filter); |
add tests
|
170 |
|
171 |
$self->connect unless $self->connected; |
|
172 |
|
|
add various things
|
173 |
my $sth = $self->dbh->prepare($sql); |
add tests
|
174 |
|
175 |
if ($sth_options) { |
|
176 |
foreach my $key (keys %$sth_options) { |
|
177 |
$sth->{$key} = $sth_options->{$key}; |
|
178 |
} |
|
179 |
} |
|
180 |
|
|
cleanup
|
181 |
# Execute |
add tests
|
182 |
my $ret_val = $sth->execute(@bind); |
add various things
|
183 |
|
cleanup
|
184 |
# Return resultset if select statement is executed |
add various things
|
185 |
if ($sth->{NUM_OF_FIELDS}) { |
186 |
my $result_class = $self->result_class; |
|
add various
|
187 |
my $result = $result_class->new({ |
188 |
sth => $sth, |
|
189 |
fetch_filter => $self->fetch_filter |
|
190 |
}); |
|
add various things
|
191 |
return $result; |
192 |
} |
|
add tests
|
193 |
return $ret_val; |
add test
|
194 |
} |
195 | ||
cleanup
|
196 |
# Prepare and execute raw SQL |
add test
|
197 |
sub query_raw_sql { |
cleanup
|
198 |
my ($self, $sql, @bind_values) = @_; |
add tests
|
199 |
|
cleanup
|
200 |
# Connect |
add various
|
201 |
$self->connect unless $self->connected; |
cleanup
|
202 |
|
203 |
# Add semicolon if not exist; |
|
add tests
|
204 |
$sql .= ';' unless $sql =~ /;$/; |
cleanup
|
205 |
|
206 |
# Prepare |
|
add various things
|
207 |
my $sth = $self->dbh->prepare($sql); |
cleanup
|
208 |
|
209 |
# Execute |
|
210 |
$sth->execute(@bind_values); |
|
211 |
|
|
add various things
|
212 |
return $sth; |
add test
|
213 |
} |
214 | ||
215 |
Object::Simple->build_class; |
|
216 | ||
add various things
|
217 |
package DBI::Custom::Result; |
218 |
use Object::Simple; |
|
219 | ||
add various
|
220 |
sub sth : Attr {} |
221 |
sub fetch_filter : Attr {} |
|
add various things
|
222 | |
223 |
sub fetchrow_arrayref { |
|
224 |
my $self = shift; |
|
add various
|
225 |
my $sth = $self->{sth}; |
226 |
|
|
227 |
my $array = $sth->fetchrow_arrayref; |
|
228 |
|
|
229 |
return $array unless $array; |
|
add various things
|
230 |
|
add various
|
231 |
my $keys = $sth->{NAME_lc}; |
add various
|
232 |
my $fetch_filter = $self->fetch_filter; |
233 |
if ($fetch_filter) { |
|
234 |
for (my $i = 0; $i < @$keys; $i++) { |
|
235 |
$array->[$i] = $fetch_filter->($keys->[$i], $array->[$i]); |
|
236 |
} |
|
add various
|
237 |
} |
238 |
return $array; |
|
add various things
|
239 |
} |
240 | ||
add various
|
241 |
sub fetchrow_array { |
242 |
my $self = shift; |
|
243 |
my $sth = $self->{sth}; |
|
244 |
|
|
245 |
my @array = $sth->fetchrow_array; |
|
246 |
|
|
247 |
return unless @array; |
|
248 |
|
|
249 |
my $keys = $sth->{NAME_lc}; |
|
250 |
|
|
add various
|
251 |
my $fetch_filter = $self->fetch_filter; |
252 |
if ($fetch_filter) { |
|
253 |
for (my $i = 0; $i < @$keys; $i++) { |
|
254 |
$array[$i] = $fetch_filter->($keys->[$i], $array[$i]); |
|
255 |
} |
|
add various
|
256 |
} |
257 |
return @array; |
|
258 |
} |
|
add various things
|
259 | |
add various
|
260 |
sub fetchrow_hashref { |
261 |
my $self = shift; |
|
262 |
my $sth = $self->{sth}; |
|
263 |
|
|
264 |
my $hash = $sth->fetchrow_hashref; |
|
265 |
|
|
266 |
return unless $hash; |
|
add various
|
267 |
my $fetch_filter = $self->fetch_filter; |
add various
|
268 |
|
add various
|
269 |
if ($fetch_filter) { |
270 |
foreach my $key (keys %$hash) { |
|
271 |
$hash->{$key} = $fetch_filter->($key, $hash->{$key}); |
|
272 |
} |
|
add various
|
273 |
} |
274 |
return $hash; |
|
275 |
} |
|
add various things
|
276 | |
277 |
sub err { shift->sth->err } |
|
278 |
sub errstr { shift->sth->errstr } |
|
279 |
sub finish { shift->sth->finish } |
|
280 |
sub rows { shift->sth->rows } |
|
281 |
sub state { shift->sth->state } |
|
282 | ||
283 |
Object::Simple->build_class; |
|
284 | ||
285 | ||
add test
|
286 |
package DBI::Custom::SQLTemplate; |
287 |
use Object::Simple; |
|
try various way
|
288 |
use Carp 'croak'; |
add test
|
289 | |
try varioud way
|
290 |
### Attributes; |
try various way
|
291 |
sub tag_start : Attr { default => '{' } |
292 |
sub tag_end : Attr { default => '}' } |
|
293 |
sub template : Attr {}; |
|
294 |
sub tree : Attr { auto_build => sub { shift->tree([]) } } |
|
295 |
sub bind_filter : Attr {} |
|
296 |
sub values : Attr {} |
|
297 |
sub upper_case : Attr {default => 0} |
|
try varioud way
|
298 | |
add test
|
299 |
sub create_sql { |
try varioud way
|
300 |
my ($self, $template, $values, $filter) = @_; |
301 |
|
|
try various way
|
302 |
$filter ||= $self->bind_filter; |
303 |
|
|
try varioud way
|
304 |
$self->parse($template); |
305 |
|
|
try various way
|
306 |
my ($sql, @bind) = $self->build_sql({bind_filter => $filter, values => $values}); |
try varioud way
|
307 |
|
308 |
return ($sql, @bind); |
|
309 |
} |
|
310 | ||
311 |
our $TAG_SYNTAX = <<'EOS'; |
|
312 |
[tag] [expand] |
|
313 |
{= name} name = ? |
|
try various way
|
314 |
{<> name} name <> ? |
try varioud way
|
315 | |
316 |
{< name} name < ? |
|
317 |
{> name} name > ? |
|
318 |
{>= name} name >= ? |
|
319 |
{<= name} name <= ? |
|
320 | ||
321 |
{like name} name like ? |
|
322 |
{in name} name in [?, ?, ..] |
|
323 | ||
324 |
{insert_values} (key1, key2, key3) values (?, ?, ?) |
|
325 |
{update_values} set key1 = ?, key2 = ?, key3 = ? |
|
326 |
EOS |
|
327 | ||
try various way
|
328 |
our %VALID_TAG_NAMES = map {$_ => 1} qw/= <> < > >= <= like in insert_values update_set/; |
try varioud way
|
329 |
sub parse { |
330 |
my ($self, $template) = @_; |
|
331 |
$self->template($template); |
|
332 |
|
|
333 |
# Clean start; |
|
334 |
delete $self->{tree}; |
|
335 |
|
|
336 |
# Tags |
|
337 |
my $tag_start = quotemeta $self->tag_start; |
|
338 |
my $tag_end = quotemeta $self->tag_end; |
|
first commit
|
339 |
|
try varioud way
|
340 |
# Tokenize |
341 |
my $state = 'text'; |
|
342 |
|
|
343 |
# Save original template |
|
344 |
my $original_template = $template; |
|
345 |
|
|
346 |
# Text |
|
347 |
while ($template =~ s/([^$tag_start]*?)$tag_start([^$tag_end].*?)$tag_end//sm) { |
|
try various way
|
348 |
my $text = $1; |
try varioud way
|
349 |
my $tag = $2; |
350 |
|
|
try various way
|
351 |
push @{$self->tree}, {type => 'text', args => [$text]} if $text; |
try varioud way
|
352 |
|
353 |
if ($tag) { |
|
354 |
|
|
try various way
|
355 |
my ($tag_name, @args) = split /\s+/, $tag; |
try varioud way
|
356 |
|
try various way
|
357 |
$tag ||= ''; |
358 |
croak("Tag '$tag' in SQL template is invalid.\n\n" . |
|
359 |
"SQL template tag syntax\n$TAG_SYNTAX\n\n" . |
|
360 |
"Your SQL template is \n$original_template\n\n") |
|
361 |
unless $VALID_TAG_NAMES{$tag_name}; |
|
try varioud way
|
362 |
|
try various way
|
363 |
push @{$self->tree}, {type => 'tag', tag_name => $tag_name, args => [@args]}; |
try varioud way
|
364 |
} |
365 |
} |
|
366 |
|
|
try various way
|
367 |
push @{$self->tree}, {type => 'text', args => [$template]} if $template; |
first commit
|
368 |
} |
369 | ||
try various way
|
370 |
our %EXPAND_PLACE_HOLDER = map {$_ => 1} qw/= <> < > >= <= like/; |
371 |
sub build_sql { |
|
372 |
my ($self, $args) = @_; |
|
373 |
|
|
374 |
my $tree = $args->{tree} || $self->tree; |
|
375 |
my $bind_filter = $args->{bind_filter} || $self->bind_filter; |
|
376 |
my $values = exists $args->{values} ? $args->{values} : $self->values; |
|
377 |
|
|
378 |
my @bind_values; |
|
379 |
my $sql = ''; |
|
380 |
foreach my $node (@$tree) { |
|
381 |
my $type = $node->{type}; |
|
382 |
my $tag_name = $node->{tag_name}; |
|
383 |
my $args = $node->{args}; |
|
384 |
|
|
385 |
if ($type eq 'text') { |
|
386 |
# Join text |
|
387 |
$sql .= $args->[0]; |
|
388 |
} |
|
389 |
elsif ($type eq 'tag') { |
|
390 |
if ($EXPAND_PLACE_HOLDER{$tag_name}) { |
|
391 |
my $key = $args->[0]; |
|
392 |
|
|
393 |
# Filter Value |
|
394 |
if ($bind_filter) { |
|
try various way
|
395 |
push @bind_values, scalar $bind_filter->($key, $values->{$key}); |
try various way
|
396 |
} |
397 |
else { |
|
398 |
push @bind_values, $values->{$key}; |
|
399 |
} |
|
400 |
$tag_name = uc $tag_name if $self->upper_case; |
|
401 |
my $place_holder = "$key $tag_name ?"; |
|
402 |
$sql .= $place_holder; |
|
403 |
} |
|
try various way
|
404 |
elsif ($tag_name eq 'insert_values') { |
405 |
my $statement_keys = '('; |
|
406 |
my $statement_place_holders = '('; |
|
407 |
|
|
408 |
$values = $values->{insert_values}; |
|
409 |
|
|
410 |
foreach my $key (sort keys %$values) { |
|
411 |
if ($bind_filter) { |
|
412 |
push @bind_values, scalar $bind_filter->($key, $values->{$key}); |
|
413 |
} |
|
414 |
else { |
|
415 |
push @bind_values, $values->{$key}; |
|
416 |
} |
|
417 |
|
|
418 |
$statement_keys .= "$key, "; |
|
419 |
$statement_place_holders .= "?, "; |
|
420 |
} |
|
421 |
|
|
422 |
$statement_keys =~ s/, $//; |
|
423 |
$statement_keys .= ')'; |
|
424 |
|
|
425 |
$statement_place_holders =~ s/, $//; |
|
426 |
$statement_place_holders .= ')'; |
|
427 |
|
|
428 |
$sql .= "$statement_keys values $statement_place_holders"; |
|
429 |
} |
|
430 |
elsif ($tag_name eq 'update_set') { |
|
431 |
my $statement = 'set '; |
|
432 |
|
|
433 |
$values = $values->{update_set}; |
|
434 |
|
|
435 |
foreach my $key (sort keys %$values) { |
|
436 |
if ($bind_filter) { |
|
437 |
push @bind_values, scalar $bind_filter->($key, $values->{$key}); |
|
438 |
} |
|
439 |
else { |
|
440 |
push @bind_values, $values->{$key}; |
|
441 |
} |
|
442 |
|
|
443 |
$statement .= "$key = ?, "; |
|
444 |
} |
|
445 |
|
|
446 |
$statement =~ s/, $//; |
|
447 |
|
|
448 |
$sql .= $statement; |
|
449 |
} |
|
try various way
|
450 |
} |
451 |
} |
|
452 |
$sql .= ';' unless $sql =~ /;$/; |
|
453 |
return ($sql, @bind_values); |
|
454 |
} |
|
try varioud way
|
455 | |
456 | ||
first commit
|
457 |
Object::Simple->build_class; |
458 | ||
add various things
|
459 |
package DBI::Custom; |
460 |
1; |
|
461 | ||
first commit
|
462 |
=head1 NAME |
463 | ||
add test
|
464 |
DBI::Custom - Customizable simple DBI |
first commit
|
465 | |
466 |
=head1 VERSION |
|
467 | ||
add test
|
468 |
Version 0.0101 |
first commit
|
469 | |
470 |
=cut |
|
471 | ||
472 |
=head1 SYNOPSIS |
|
473 | ||
add test
|
474 |
my $dbi = DBI::Custom->new; |
first commit
|
475 | |
add test
|
476 |
=head1 METHODS |
first commit
|
477 | |
add test
|
478 |
=head2 add_filter |
first commit
|
479 | |
add test
|
480 |
=head2 bind_filter |
first commit
|
481 | |
add test
|
482 |
=head2 clone |
first commit
|
483 | |
add test
|
484 |
=head2 connect |
first commit
|
485 | |
add test
|
486 |
=head2 connect_info |
first commit
|
487 | |
add test
|
488 |
=head2 dbh |
first commit
|
489 | |
add test
|
490 |
=head2 fetch_filter |
first commit
|
491 | |
add test
|
492 |
=head2 filters |
first commit
|
493 | |
add test
|
494 |
=head2 initialize_model |
first commit
|
495 | |
add test
|
496 |
=head2 model |
first commit
|
497 | |
add test
|
498 |
=head2 new |
499 | ||
500 |
=head2 query |
|
first commit
|
501 | |
add test
|
502 |
=head2 create_sql |
503 | ||
504 |
=head2 query_raw_sql |
|
505 | ||
506 |
=head2 sql_template |
|
507 | ||
add tests
|
508 |
=head2 auto_commit |
509 | ||
510 |
=head2 connected |
|
511 | ||
512 |
=head2 dbh_option |
|
513 | ||
514 |
=head2 disconnect |
|
515 | ||
516 |
=head2 reconnect |
|
517 | ||
518 |
=head2 result_class |
|
519 | ||
add various
|
520 |
=head2 commit |
first commit
|
521 | |
add various
|
522 |
=head2 rollback |
first commit
|
523 | |
524 | ||
add various
|
525 |
=head1 AUTHOR |
first commit
|
526 | |
add various
|
527 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
first commit
|
528 | |
529 |
=head1 COPYRIGHT & LICENSE |
|
530 | ||
531 |
Copyright 2009 Yuki Kimoto, all rights reserved. |
|
532 | ||
533 |
This program is free software; you can redistribute it and/or modify it |
|
534 |
under the same terms as Perl itself. |
|
535 | ||
536 | ||
537 |
=cut |
|
538 | ||
539 |
1; # End of DBI::Custom |