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 | ||
add various things
|
145 | |
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 | ||
add some method
|
154 |
sub query { |
try varioud way
|
155 |
my ($self, $template, $values, $filter) = @_; |
156 |
|
|
add tests
|
157 |
my $sth_options; |
158 |
|
|
159 |
# Rearrange when argumets is hash referecne |
|
160 |
if (ref $template eq 'HASH') { |
|
161 |
my $args = $template; |
|
162 |
($template, $values, $filter, $sth_options) |
|
163 |
= @{$args}{qw/template values filter sth_options/}; |
|
164 |
} |
|
165 |
|
|
try varioud way
|
166 |
$filter ||= $self->bind_filter; |
167 |
|
|
add various things
|
168 |
my ($sql, @bind) = $self->create_sql($template, $values, $filter); |
add tests
|
169 |
|
170 |
$self->connect unless $self->connected; |
|
171 |
|
|
add various things
|
172 |
my $sth = $self->dbh->prepare($sql); |
add tests
|
173 |
|
174 |
if ($sth_options) { |
|
175 |
foreach my $key (keys %$sth_options) { |
|
176 |
$sth->{$key} = $sth_options->{$key}; |
|
177 |
} |
|
178 |
} |
|
179 |
|
|
add tests
|
180 |
my $ret_val = $sth->execute(@bind); |
add various things
|
181 |
|
182 |
# Select |
|
183 |
if ($sth->{NUM_OF_FIELDS}) { |
|
184 |
my $result_class = $self->result_class; |
|
add various
|
185 |
my $result = $result_class->new({ |
186 |
sth => $sth, |
|
187 |
fetch_filter => $self->fetch_filter |
|
188 |
}); |
|
add various things
|
189 |
return $result; |
190 |
} |
|
add tests
|
191 |
return $ret_val; |
add test
|
192 |
} |
193 | ||
add tests
|
194 | |
add test
|
195 |
sub query_raw_sql { |
196 |
my ($self, $sql, @bind) = @_; |
|
add tests
|
197 |
|
add various
|
198 |
$self->connect unless $self->connected; |
add tests
|
199 |
$sql .= ';' unless $sql =~ /;$/; |
add various things
|
200 |
my $sth = $self->dbh->prepare($sql); |
201 |
$sth->execute(@bind); |
|
202 |
return $sth; |
|
add test
|
203 |
} |
204 | ||
add tests
|
205 | |
add various things
|
206 | |
add test
|
207 |
Object::Simple->build_class; |
208 | ||
add various things
|
209 |
package DBI::Custom::Result; |
210 |
use Object::Simple; |
|
211 | ||
add various
|
212 |
sub sth : Attr {} |
213 |
sub fetch_filter : Attr {} |
|
add various things
|
214 | |
215 |
sub fetchrow_arrayref { |
|
216 |
my $self = shift; |
|
add various
|
217 |
my $sth = $self->{sth}; |
218 |
|
|
add various
|
219 |
$DB::single = 1; |
add various
|
220 |
my $array = $sth->fetchrow_arrayref; |
221 |
|
|
222 |
return $array unless $array; |
|
add various things
|
223 |
|
add various
|
224 |
my $keys = $sth->{NAME_lc}; |
add various
|
225 |
my $fetch_filter = $self->fetch_filter; |
226 |
if ($fetch_filter) { |
|
227 |
for (my $i = 0; $i < @$keys; $i++) { |
|
228 |
$array->[$i] = $fetch_filter->($keys->[$i], $array->[$i]); |
|
229 |
} |
|
add various
|
230 |
} |
231 |
return $array; |
|
add various things
|
232 |
} |
233 | ||
add various
|
234 |
sub fetchrow_array { |
235 |
my $self = shift; |
|
236 |
my $sth = $self->{sth}; |
|
237 |
|
|
238 |
my @array = $sth->fetchrow_array; |
|
239 |
|
|
240 |
return unless @array; |
|
241 |
|
|
242 |
my $keys = $sth->{NAME_lc}; |
|
243 |
|
|
add various
|
244 |
my $fetch_filter = $self->fetch_filter; |
245 |
if ($fetch_filter) { |
|
246 |
for (my $i = 0; $i < @$keys; $i++) { |
|
247 |
$array[$i] = $fetch_filter->($keys->[$i], $array[$i]); |
|
248 |
} |
|
add various
|
249 |
} |
250 |
return @array; |
|
251 |
} |
|
add various things
|
252 | |
add various
|
253 |
sub fetchrow_hashref { |
254 |
my $self = shift; |
|
255 |
my $sth = $self->{sth}; |
|
256 |
|
|
257 |
my $hash = $sth->fetchrow_hashref; |
|
258 |
|
|
259 |
return unless $hash; |
|
add various
|
260 |
my $fetch_filter = $self->fetch_filter; |
add various
|
261 |
|
add various
|
262 |
if ($fetch_filter) { |
263 |
foreach my $key (keys %$hash) { |
|
264 |
$hash->{$key} = $fetch_filter->($key, $hash->{$key}); |
|
265 |
} |
|
add various
|
266 |
} |
267 |
return $hash; |
|
268 |
} |
|
add various things
|
269 | |
270 |
sub err { shift->sth->err } |
|
271 |
sub errstr { shift->sth->errstr } |
|
272 |
sub finish { shift->sth->finish } |
|
273 |
sub rows { shift->sth->rows } |
|
274 |
sub state { shift->sth->state } |
|
275 | ||
276 |
Object::Simple->build_class; |
|
277 | ||
278 | ||
add test
|
279 |
package DBI::Custom::SQLTemplate; |
280 |
use Object::Simple; |
|
try various way
|
281 |
use Carp 'croak'; |
add test
|
282 | |
try varioud way
|
283 |
### Attributes; |
try various way
|
284 |
sub tag_start : Attr { default => '{' } |
285 |
sub tag_end : Attr { default => '}' } |
|
286 |
sub template : Attr {}; |
|
287 |
sub tree : Attr { auto_build => sub { shift->tree([]) } } |
|
288 |
sub bind_filter : Attr {} |
|
289 |
sub values : Attr {} |
|
290 |
sub upper_case : Attr {default => 0} |
|
try varioud way
|
291 | |
add test
|
292 |
sub create_sql { |
try varioud way
|
293 |
my ($self, $template, $values, $filter) = @_; |
294 |
|
|
try various way
|
295 |
$filter ||= $self->bind_filter; |
296 |
|
|
try varioud way
|
297 |
$self->parse($template); |
298 |
|
|
try various way
|
299 |
my ($sql, @bind) = $self->build_sql({bind_filter => $filter, values => $values}); |
try varioud way
|
300 |
|
301 |
return ($sql, @bind); |
|
302 |
} |
|
303 | ||
304 |
our $TAG_SYNTAX = <<'EOS'; |
|
305 |
[tag] [expand] |
|
306 |
{= name} name = ? |
|
try various way
|
307 |
{<> name} name <> ? |
try varioud way
|
308 | |
309 |
{< name} name < ? |
|
310 |
{> name} name > ? |
|
311 |
{>= name} name >= ? |
|
312 |
{<= name} name <= ? |
|
313 | ||
314 |
{like name} name like ? |
|
315 |
{in name} name in [?, ?, ..] |
|
316 | ||
317 |
{insert_values} (key1, key2, key3) values (?, ?, ?) |
|
318 |
{update_values} set key1 = ?, key2 = ?, key3 = ? |
|
319 |
EOS |
|
320 | ||
try various way
|
321 |
our %VALID_TAG_NAMES = map {$_ => 1} qw/= <> < > >= <= like in insert_values update_set/; |
try varioud way
|
322 |
sub parse { |
323 |
my ($self, $template) = @_; |
|
324 |
$self->template($template); |
|
325 |
|
|
326 |
# Clean start; |
|
327 |
delete $self->{tree}; |
|
328 |
|
|
329 |
# Tags |
|
330 |
my $tag_start = quotemeta $self->tag_start; |
|
331 |
my $tag_end = quotemeta $self->tag_end; |
|
first commit
|
332 |
|
try varioud way
|
333 |
# Tokenize |
334 |
my $state = 'text'; |
|
335 |
|
|
336 |
# Save original template |
|
337 |
my $original_template = $template; |
|
338 |
|
|
339 |
# Text |
|
340 |
while ($template =~ s/([^$tag_start]*?)$tag_start([^$tag_end].*?)$tag_end//sm) { |
|
try various way
|
341 |
my $text = $1; |
try varioud way
|
342 |
my $tag = $2; |
343 |
|
|
try various way
|
344 |
push @{$self->tree}, {type => 'text', args => [$text]} if $text; |
try varioud way
|
345 |
|
346 |
if ($tag) { |
|
347 |
|
|
try various way
|
348 |
my ($tag_name, @args) = split /\s+/, $tag; |
try varioud way
|
349 |
|
try various way
|
350 |
$tag ||= ''; |
351 |
croak("Tag '$tag' in SQL template is invalid.\n\n" . |
|
352 |
"SQL template tag syntax\n$TAG_SYNTAX\n\n" . |
|
353 |
"Your SQL template is \n$original_template\n\n") |
|
354 |
unless $VALID_TAG_NAMES{$tag_name}; |
|
try varioud way
|
355 |
|
try various way
|
356 |
push @{$self->tree}, {type => 'tag', tag_name => $tag_name, args => [@args]}; |
try varioud way
|
357 |
} |
358 |
} |
|
359 |
|
|
try various way
|
360 |
push @{$self->tree}, {type => 'text', args => [$template]} if $template; |
first commit
|
361 |
} |
362 | ||
try various way
|
363 |
our %EXPAND_PLACE_HOLDER = map {$_ => 1} qw/= <> < > >= <= like/; |
364 |
sub build_sql { |
|
365 |
my ($self, $args) = @_; |
|
366 |
|
|
367 |
my $tree = $args->{tree} || $self->tree; |
|
368 |
my $bind_filter = $args->{bind_filter} || $self->bind_filter; |
|
369 |
my $values = exists $args->{values} ? $args->{values} : $self->values; |
|
370 |
|
|
371 |
my @bind_values; |
|
372 |
my $sql = ''; |
|
373 |
foreach my $node (@$tree) { |
|
374 |
my $type = $node->{type}; |
|
375 |
my $tag_name = $node->{tag_name}; |
|
376 |
my $args = $node->{args}; |
|
377 |
|
|
378 |
if ($type eq 'text') { |
|
379 |
# Join text |
|
380 |
$sql .= $args->[0]; |
|
381 |
} |
|
382 |
elsif ($type eq 'tag') { |
|
383 |
if ($EXPAND_PLACE_HOLDER{$tag_name}) { |
|
384 |
my $key = $args->[0]; |
|
385 |
|
|
386 |
# Filter Value |
|
387 |
if ($bind_filter) { |
|
try various way
|
388 |
push @bind_values, scalar $bind_filter->($key, $values->{$key}); |
try various way
|
389 |
} |
390 |
else { |
|
391 |
push @bind_values, $values->{$key}; |
|
392 |
} |
|
393 |
$tag_name = uc $tag_name if $self->upper_case; |
|
394 |
my $place_holder = "$key $tag_name ?"; |
|
395 |
$sql .= $place_holder; |
|
396 |
} |
|
try various way
|
397 |
elsif ($tag_name eq 'insert_values') { |
398 |
my $statement_keys = '('; |
|
399 |
my $statement_place_holders = '('; |
|
400 |
|
|
401 |
$values = $values->{insert_values}; |
|
402 |
|
|
403 |
foreach my $key (sort keys %$values) { |
|
404 |
if ($bind_filter) { |
|
405 |
push @bind_values, scalar $bind_filter->($key, $values->{$key}); |
|
406 |
} |
|
407 |
else { |
|
408 |
push @bind_values, $values->{$key}; |
|
409 |
} |
|
410 |
|
|
411 |
$statement_keys .= "$key, "; |
|
412 |
$statement_place_holders .= "?, "; |
|
413 |
} |
|
414 |
|
|
415 |
$statement_keys =~ s/, $//; |
|
416 |
$statement_keys .= ')'; |
|
417 |
|
|
418 |
$statement_place_holders =~ s/, $//; |
|
419 |
$statement_place_holders .= ')'; |
|
420 |
|
|
421 |
$sql .= "$statement_keys values $statement_place_holders"; |
|
422 |
} |
|
423 |
elsif ($tag_name eq 'update_set') { |
|
424 |
my $statement = 'set '; |
|
425 |
|
|
426 |
$values = $values->{update_set}; |
|
427 |
|
|
428 |
foreach my $key (sort keys %$values) { |
|
429 |
if ($bind_filter) { |
|
430 |
push @bind_values, scalar $bind_filter->($key, $values->{$key}); |
|
431 |
} |
|
432 |
else { |
|
433 |
push @bind_values, $values->{$key}; |
|
434 |
} |
|
435 |
|
|
436 |
$statement .= "$key = ?, "; |
|
437 |
} |
|
438 |
|
|
439 |
$statement =~ s/, $//; |
|
440 |
|
|
441 |
$sql .= $statement; |
|
442 |
} |
|
try various way
|
443 |
} |
444 |
} |
|
445 |
$sql .= ';' unless $sql =~ /;$/; |
|
446 |
return ($sql, @bind_values); |
|
447 |
} |
|
try varioud way
|
448 | |
449 | ||
first commit
|
450 |
Object::Simple->build_class; |
451 | ||
add various things
|
452 |
package DBI::Custom; |
453 |
1; |
|
454 | ||
first commit
|
455 |
=head1 NAME |
456 | ||
add test
|
457 |
DBI::Custom - Customizable simple DBI |
first commit
|
458 | |
459 |
=head1 VERSION |
|
460 | ||
add test
|
461 |
Version 0.0101 |
first commit
|
462 | |
463 |
=cut |
|
464 | ||
465 |
=head1 SYNOPSIS |
|
466 | ||
add test
|
467 |
my $dbi = DBI::Custom->new; |
first commit
|
468 | |
add test
|
469 |
=head1 METHODS |
first commit
|
470 | |
add test
|
471 |
=head2 add_filter |
first commit
|
472 | |
add test
|
473 |
=head2 bind_filter |
first commit
|
474 | |
add test
|
475 |
=head2 clone |
first commit
|
476 | |
add test
|
477 |
=head2 connect |
first commit
|
478 | |
add test
|
479 |
=head2 connect_info |
first commit
|
480 | |
add test
|
481 |
=head2 dbh |
first commit
|
482 | |
add test
|
483 |
=head2 fetch_filter |
first commit
|
484 | |
add test
|
485 |
=head2 filters |
first commit
|
486 | |
add test
|
487 |
=head2 initialize_model |
first commit
|
488 | |
add test
|
489 |
=head2 model |
first commit
|
490 | |
add test
|
491 |
=head2 new |
492 | ||
493 |
=head2 query |
|
first commit
|
494 | |
add test
|
495 |
=head2 create_sql |
496 | ||
497 |
=head2 query_raw_sql |
|
498 | ||
499 |
=head2 sql_template |
|
500 | ||
add tests
|
501 |
=head2 auto_commit |
502 | ||
503 |
=head2 connected |
|
504 | ||
505 |
=head2 dbh_option |
|
506 | ||
507 |
=head2 disconnect |
|
508 | ||
509 |
=head2 reconnect |
|
510 | ||
511 |
=head2 result_class |
|
512 | ||
add various
|
513 |
=head2 commit |
first commit
|
514 | |
add various
|
515 |
=head2 rollback |
first commit
|
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 |