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