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; |
|
185 |
my $result = $result_class->new({sth => $sth}); |
|
186 |
return $result; |
|
187 |
} |
|
add tests
|
188 |
return $ret_val; |
add test
|
189 |
} |
190 | ||
add tests
|
191 | |
add test
|
192 |
sub query_raw_sql { |
193 |
my ($self, $sql, @bind) = @_; |
|
add tests
|
194 |
|
195 |
$sefl->connect unless $self->connected; |
|
add various things
|
196 |
my $sth = $self->dbh->prepare($sql); |
197 |
$sth->execute(@bind); |
|
198 |
return $sth; |
|
add test
|
199 |
} |
200 | ||
add tests
|
201 | |
add various things
|
202 | |
add test
|
203 |
Object::Simple->build_class; |
204 | ||
add various things
|
205 |
package DBI::Custom::Result; |
206 |
use Object::Simple; |
|
207 | ||
208 |
sub sth : Attr {}; |
|
209 | ||
210 |
sub fetchrow_arrayref { |
|
211 |
my $self = shift; |
|
212 |
$self->sth; |
|
213 |
|
|
214 |
|
|
215 |
} |
|
216 | ||
217 | ||
218 |
*fetch = \&fetchrow_arrayref; |
|
219 | ||
220 |
sub err { shift->sth->err } |
|
221 |
sub errstr { shift->sth->errstr } |
|
222 |
sub finish { shift->sth->finish } |
|
223 |
sub rows { shift->sth->rows } |
|
224 |
sub state { shift->sth->state } |
|
225 | ||
226 |
Object::Simple->build_class; |
|
227 | ||
228 | ||
add test
|
229 |
package DBI::Custom::SQLTemplate; |
230 |
use Object::Simple; |
|
try various way
|
231 |
use Carp 'croak'; |
add test
|
232 | |
try varioud way
|
233 |
### Attributes; |
try various way
|
234 |
sub tag_start : Attr { default => '{' } |
235 |
sub tag_end : Attr { default => '}' } |
|
236 |
sub template : Attr {}; |
|
237 |
sub tree : Attr { auto_build => sub { shift->tree([]) } } |
|
238 |
sub bind_filter : Attr {} |
|
239 |
sub values : Attr {} |
|
240 |
sub upper_case : Attr {default => 0} |
|
try varioud way
|
241 | |
add test
|
242 |
sub create_sql { |
try varioud way
|
243 |
my ($self, $template, $values, $filter) = @_; |
244 |
|
|
try various way
|
245 |
$filter ||= $self->bind_filter; |
246 |
|
|
try varioud way
|
247 |
$self->parse($template); |
248 |
|
|
try various way
|
249 |
my ($sql, @bind) = $self->build_sql({bind_filter => $filter, values => $values}); |
try varioud way
|
250 |
|
251 |
return ($sql, @bind); |
|
252 |
} |
|
253 | ||
254 |
our $TAG_SYNTAX = <<'EOS'; |
|
255 |
[tag] [expand] |
|
256 |
{= name} name = ? |
|
try various way
|
257 |
{<> name} name <> ? |
try varioud way
|
258 | |
259 |
{< name} name < ? |
|
260 |
{> name} name > ? |
|
261 |
{>= name} name >= ? |
|
262 |
{<= name} name <= ? |
|
263 | ||
264 |
{like name} name like ? |
|
265 |
{in name} name in [?, ?, ..] |
|
266 | ||
267 |
{insert_values} (key1, key2, key3) values (?, ?, ?) |
|
268 |
{update_values} set key1 = ?, key2 = ?, key3 = ? |
|
269 |
EOS |
|
270 | ||
try various way
|
271 |
our %VALID_TAG_NAMES = map {$_ => 1} qw/= <> < > >= <= like in insert_values update_set/; |
try varioud way
|
272 |
sub parse { |
273 |
my ($self, $template) = @_; |
|
274 |
$self->template($template); |
|
275 |
|
|
276 |
# Clean start; |
|
277 |
delete $self->{tree}; |
|
278 |
|
|
279 |
# Tags |
|
280 |
my $tag_start = quotemeta $self->tag_start; |
|
281 |
my $tag_end = quotemeta $self->tag_end; |
|
first commit
|
282 |
|
try varioud way
|
283 |
# Tokenize |
284 |
my $state = 'text'; |
|
285 |
|
|
286 |
# Save original template |
|
287 |
my $original_template = $template; |
|
288 |
|
|
289 |
# Text |
|
290 |
while ($template =~ s/([^$tag_start]*?)$tag_start([^$tag_end].*?)$tag_end//sm) { |
|
try various way
|
291 |
my $text = $1; |
try varioud way
|
292 |
my $tag = $2; |
293 |
|
|
try various way
|
294 |
push @{$self->tree}, {type => 'text', args => [$text]} if $text; |
try varioud way
|
295 |
|
296 |
if ($tag) { |
|
297 |
|
|
try various way
|
298 |
my ($tag_name, @args) = split /\s+/, $tag; |
try varioud way
|
299 |
|
try various way
|
300 |
$tag ||= ''; |
301 |
croak("Tag '$tag' in SQL template is invalid.\n\n" . |
|
302 |
"SQL template tag syntax\n$TAG_SYNTAX\n\n" . |
|
303 |
"Your SQL template is \n$original_template\n\n") |
|
304 |
unless $VALID_TAG_NAMES{$tag_name}; |
|
try varioud way
|
305 |
|
try various way
|
306 |
push @{$self->tree}, {type => 'tag', tag_name => $tag_name, args => [@args]}; |
try varioud way
|
307 |
} |
308 |
} |
|
309 |
|
|
try various way
|
310 |
push @{$self->tree}, {type => 'text', args => [$template]} if $template; |
first commit
|
311 |
} |
312 | ||
try various way
|
313 |
our %EXPAND_PLACE_HOLDER = map {$_ => 1} qw/= <> < > >= <= like/; |
314 |
sub build_sql { |
|
315 |
my ($self, $args) = @_; |
|
316 |
|
|
317 |
my $tree = $args->{tree} || $self->tree; |
|
318 |
my $bind_filter = $args->{bind_filter} || $self->bind_filter; |
|
319 |
my $values = exists $args->{values} ? $args->{values} : $self->values; |
|
320 |
|
|
321 |
my @bind_values; |
|
322 |
my $sql = ''; |
|
323 |
foreach my $node (@$tree) { |
|
324 |
my $type = $node->{type}; |
|
325 |
my $tag_name = $node->{tag_name}; |
|
326 |
my $args = $node->{args}; |
|
327 |
|
|
328 |
if ($type eq 'text') { |
|
329 |
# Join text |
|
330 |
$sql .= $args->[0]; |
|
331 |
} |
|
332 |
elsif ($type eq 'tag') { |
|
333 |
if ($EXPAND_PLACE_HOLDER{$tag_name}) { |
|
334 |
my $key = $args->[0]; |
|
335 |
|
|
336 |
# Filter Value |
|
337 |
if ($bind_filter) { |
|
try various way
|
338 |
push @bind_values, scalar $bind_filter->($key, $values->{$key}); |
try various way
|
339 |
} |
340 |
else { |
|
341 |
push @bind_values, $values->{$key}; |
|
342 |
} |
|
343 |
$tag_name = uc $tag_name if $self->upper_case; |
|
344 |
my $place_holder = "$key $tag_name ?"; |
|
345 |
$sql .= $place_holder; |
|
346 |
} |
|
try various way
|
347 |
elsif ($tag_name eq 'insert_values') { |
348 |
my $statement_keys = '('; |
|
349 |
my $statement_place_holders = '('; |
|
350 |
|
|
351 |
$values = $values->{insert_values}; |
|
352 |
|
|
353 |
foreach my $key (sort keys %$values) { |
|
354 |
if ($bind_filter) { |
|
355 |
push @bind_values, scalar $bind_filter->($key, $values->{$key}); |
|
356 |
} |
|
357 |
else { |
|
358 |
push @bind_values, $values->{$key}; |
|
359 |
} |
|
360 |
|
|
361 |
$statement_keys .= "$key, "; |
|
362 |
$statement_place_holders .= "?, "; |
|
363 |
} |
|
364 |
|
|
365 |
$statement_keys =~ s/, $//; |
|
366 |
$statement_keys .= ')'; |
|
367 |
|
|
368 |
$statement_place_holders =~ s/, $//; |
|
369 |
$statement_place_holders .= ')'; |
|
370 |
|
|
371 |
$sql .= "$statement_keys values $statement_place_holders"; |
|
372 |
} |
|
373 |
elsif ($tag_name eq 'update_set') { |
|
374 |
my $statement = 'set '; |
|
375 |
|
|
376 |
$values = $values->{update_set}; |
|
377 |
|
|
378 |
foreach my $key (sort keys %$values) { |
|
379 |
if ($bind_filter) { |
|
380 |
push @bind_values, scalar $bind_filter->($key, $values->{$key}); |
|
381 |
} |
|
382 |
else { |
|
383 |
push @bind_values, $values->{$key}; |
|
384 |
} |
|
385 |
|
|
386 |
$statement .= "$key = ?, "; |
|
387 |
} |
|
388 |
|
|
389 |
$statement =~ s/, $//; |
|
390 |
|
|
391 |
$sql .= $statement; |
|
392 |
} |
|
try various way
|
393 |
} |
394 |
} |
|
395 |
$sql .= ';' unless $sql =~ /;$/; |
|
396 |
return ($sql, @bind_values); |
|
397 |
} |
|
try varioud way
|
398 | |
399 | ||
first commit
|
400 |
Object::Simple->build_class; |
401 | ||
add various things
|
402 |
package DBI::Custom; |
403 |
1; |
|
404 | ||
first commit
|
405 |
=head1 NAME |
406 | ||
add test
|
407 |
DBI::Custom - Customizable simple DBI |
first commit
|
408 | |
409 |
=head1 VERSION |
|
410 | ||
add test
|
411 |
Version 0.0101 |
first commit
|
412 | |
413 |
=cut |
|
414 | ||
415 |
=head1 SYNOPSIS |
|
416 | ||
add test
|
417 |
my $dbi = DBI::Custom->new; |
first commit
|
418 | |
add test
|
419 |
=head1 METHODS |
first commit
|
420 | |
add test
|
421 |
=head2 add_filter |
first commit
|
422 | |
add test
|
423 |
=head2 bind_filter |
first commit
|
424 | |
add test
|
425 |
=head2 clone |
first commit
|
426 | |
add test
|
427 |
=head2 connect |
first commit
|
428 | |
add test
|
429 |
=head2 connect_info |
first commit
|
430 | |
add test
|
431 |
=head2 dbh |
first commit
|
432 | |
add test
|
433 |
=head2 fetch_filter |
first commit
|
434 | |
add test
|
435 |
=head2 filters |
first commit
|
436 | |
add test
|
437 |
=head2 initialize_model |
first commit
|
438 | |
add test
|
439 |
=head2 model |
first commit
|
440 | |
add test
|
441 |
=head2 new |
442 | ||
443 |
=head2 query |
|
first commit
|
444 | |
add test
|
445 |
=head2 create_sql |
446 | ||
447 |
=head2 query_raw_sql |
|
448 | ||
449 |
=head2 sql_template |
|
450 | ||
add tests
|
451 |
=head2 auto_commit |
452 | ||
453 |
=head2 connected |
|
454 | ||
455 |
=head2 dbh_option |
|
456 | ||
457 |
=head2 disconnect |
|
458 | ||
459 |
=head2 reconnect |
|
460 | ||
461 |
=head2 result_class |
|
462 | ||
first commit
|
463 |
=head1 AUTHOR |
464 | ||
465 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
|
466 | ||
467 |
=head1 BUGS |
|
468 | ||
469 |
Please report any bugs or feature requests to C<bug-dbi-custom at rt.cpan.org>, or through |
|
470 |
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DBI-Custom>. I will be notified, and then you'll |
|
471 |
automatically be notified of progress on your bug as I make changes. |
|
472 | ||
473 | ||
474 | ||
475 | ||
476 |
=head1 SUPPORT |
|
477 | ||
478 |
You can find documentation for this module with the perldoc command. |
|
479 | ||
480 |
perldoc DBI::Custom |
|
481 | ||
482 | ||
483 |
You can also look for information at: |
|
484 | ||
485 |
=over 4 |
|
486 | ||
487 |
=item * RT: CPAN's request tracker |
|
488 | ||
489 |
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=DBI-Custom> |
|
490 | ||
491 |
=item * AnnoCPAN: Annotated CPAN documentation |
|
492 | ||
493 |
L<http://annocpan.org/dist/DBI-Custom> |
|
494 | ||
495 |
=item * CPAN Ratings |
|
496 | ||
497 |
L<http://cpanratings.perl.org/d/DBI-Custom> |
|
498 | ||
499 |
=item * Search CPAN |
|
500 | ||
501 |
L<http://search.cpan.org/dist/DBI-Custom/> |
|
502 | ||
503 |
=back |
|
504 | ||
505 | ||
506 |
=head1 ACKNOWLEDGEMENTS |
|
507 | ||
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 |