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 |
cleanup
|
10 |
sub prototype : ClassAttr { auto_build => \&_inherit_prototype } |
first commit
|
11 | |
cleanup
|
12 |
# Inherit super class prototype |
13 |
sub _inherit_prototype { |
|
add test
|
14 |
my $class = shift; |
cleanup
|
15 |
my $super = do { |
16 |
no strict 'refs'; |
|
17 |
${"${class}::ISA"}[0]; |
|
18 |
}; |
|
cleanup
|
19 |
my $prototype = eval{$super->can('prototype')} |
20 |
? $super->prototype->clone |
|
cleanup
|
21 |
: $class->Object::Simple::new; |
cleanup
|
22 |
|
cleanup
|
23 |
$class->prototype($prototype); |
first commit
|
24 |
} |
25 | ||
cleanup
|
26 |
# New |
27 |
sub new { |
|
28 |
my $self = shift->Object::Simple::new(@_); |
|
29 |
my $class = ref $self; |
|
cleanup
|
30 |
return bless {%{$class->prototype->clone}, %{$self}}, $class; |
first commit
|
31 |
} |
32 | ||
cleanup
|
33 |
# Initialize class |
34 |
sub initialize_class { |
|
cleanup
|
35 |
my ($class, $callback) = @_; |
first commit
|
36 |
|
cleanup
|
37 |
# Callback to initialize prototype |
38 |
$callback->($class->prototype); |
|
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 {} |
cleanup
|
63 |
sub sql_template : Attr { auto_build => sub { shift->sql_template(DBI::Custom::SQL::Template->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 | ||
cleanup
|
235 |
# Attributes |
add various
|
236 |
sub sth : Attr {} |
237 |
sub fetch_filter : Attr {} |
|
add various things
|
238 | |
cleanup
|
239 | |
cleanup
|
240 |
# Fetch (array) |
add various
|
241 |
sub fetch { |
242 |
my ($self, $type) = @_; |
|
243 |
my $sth = $self->sth; |
|
add various
|
244 |
my $fetch_filter = $self->fetch_filter; |
cleanup
|
245 |
|
246 |
# Fetch |
|
247 |
my $row = $sth->fetchrow_arrayref; |
|
248 |
|
|
249 |
# Cannot fetch |
|
250 |
return unless $row; |
|
251 |
|
|
252 |
# Filter |
|
253 |
if ($fetch_filter) { |
|
add tests
|
254 |
my $keys = $sth->{NAME_lc}; |
255 |
my $types = $sth->{TYPE}; |
|
cleanup
|
256 |
for (my $i = 0; $i < @$keys; $i++) { |
cleanup
|
257 |
$row->[$i]= $fetch_filter->($keys->[$i], $row->[$i], $types->[$i], |
258 |
$sth, $i); |
|
add various
|
259 |
} |
add various
|
260 |
} |
cleanup
|
261 |
return wantarray ? @$row : $row; |
262 |
} |
|
263 | ||
264 |
# Fetch (hash) |
|
265 |
sub fetch_hash { |
|
266 |
my $self = shift; |
|
267 |
my $sth = $self->sth; |
|
268 |
my $fetch_filter = $self->fetch_filter; |
|
269 |
|
|
270 |
# Fetch |
|
add tests
|
271 |
my $row = $sth->fetchrow_arrayref; |
cleanup
|
272 |
|
273 |
# Cannot fetch |
|
274 |
return unless $row; |
|
275 |
|
|
add tests
|
276 |
# Keys |
277 |
my $keys = $sth->{NAME_lc}; |
|
278 |
|
|
cleanup
|
279 |
# Filter |
add tests
|
280 |
my $row_hash = {}; |
cleanup
|
281 |
if ($fetch_filter) { |
add tests
|
282 |
my $types = $sth->{TYPE}; |
283 |
for (my $i = 0; $i < @$keys; $i++) { |
|
cleanup
|
284 |
$row_hash->{$keys->[$i]} = $fetch_filter->($keys->[$i], $row->[$i], |
285 |
$types->[$i], $sth, $i); |
|
add various
|
286 |
} |
add various
|
287 |
} |
cleanup
|
288 |
|
289 |
# No filter |
|
add tests
|
290 |
else { |
291 |
for (my $i = 0; $i < @$keys; $i++) { |
|
292 |
$row_hash->{$keys->[$i]} = $row->[$i]; |
|
293 |
} |
|
294 |
} |
|
295 |
return wantarray ? %$row_hash : $row_hash; |
|
add various
|
296 |
} |
add various things
|
297 | |
cleanup
|
298 |
# Fetch all (array) |
add various
|
299 |
sub fetch_all { |
cleanup
|
300 |
my $self = shift; |
add various
|
301 |
|
cleanup
|
302 |
my $rows = []; |
add tests
|
303 |
while(my @row = $self->fetch) { |
304 |
push @$rows, [@row]; |
|
add various
|
305 |
} |
cleanup
|
306 |
return wantarray ? @$rows : $rows; |
307 |
} |
|
308 | ||
309 |
# Fetch all (hash) |
|
310 |
sub fetch_all_hash { |
|
311 |
my $self = shift; |
|
312 |
|
|
313 |
my $rows = []; |
|
314 |
while(my %row = $self->fetch_hash) { |
|
315 |
push @$rows, {%row}; |
|
add various
|
316 |
} |
cleanup
|
317 |
return wantarray ? @$rows : $rows; |
add various
|
318 |
} |
add various things
|
319 | |
cleanup
|
320 |
# Finish |
add various
|
321 |
sub finish { shift->sth->finish } |
add various things
|
322 | |
cleanup
|
323 |
# Error |
324 |
sub error { |
|
325 |
my $self = shift; |
|
326 |
my $sth = $self->sth; |
|
327 |
wantarray ? ($sth->errstr, $sth->err, $sth->state) : $sth->errstr; |
|
328 |
} |
|
329 | ||
add various things
|
330 |
Object::Simple->build_class; |
331 | ||
332 | ||
cleanup
|
333 |
package DBI::Custom::SQL::Template; |
add test
|
334 |
use Object::Simple; |
try various way
|
335 |
use Carp 'croak'; |
add test
|
336 | |
try varioud way
|
337 |
### Attributes; |
try various way
|
338 |
sub tag_start : Attr { default => '{' } |
339 |
sub tag_end : Attr { default => '}' } |
|
340 |
sub template : Attr {}; |
|
341 |
sub tree : Attr { auto_build => sub { shift->tree([]) } } |
|
342 |
sub bind_filter : Attr {} |
|
343 |
sub values : Attr {} |
|
344 |
sub upper_case : Attr {default => 0} |
|
try varioud way
|
345 | |
add test
|
346 |
sub create_sql { |
try varioud way
|
347 |
my ($self, $template, $values, $filter) = @_; |
348 |
|
|
try various way
|
349 |
$filter ||= $self->bind_filter; |
350 |
|
|
try varioud way
|
351 |
$self->parse($template); |
352 |
|
|
try various way
|
353 |
my ($sql, @bind) = $self->build_sql({bind_filter => $filter, values => $values}); |
try varioud way
|
354 |
|
355 |
return ($sql, @bind); |
|
356 |
} |
|
357 | ||
358 |
our $TAG_SYNTAX = <<'EOS'; |
|
359 |
[tag] [expand] |
|
add test module
|
360 |
{? name} ? |
try varioud way
|
361 |
{= name} name = ? |
try various way
|
362 |
{<> name} name <> ? |
try varioud way
|
363 | |
364 |
{< name} name < ? |
|
365 |
{> name} name > ? |
|
366 |
{>= name} name >= ? |
|
367 |
{<= name} name <= ? |
|
368 | ||
369 |
{like name} name like ? |
|
370 |
{in name} name in [?, ?, ..] |
|
371 | ||
372 |
{insert_values} (key1, key2, key3) values (?, ?, ?) |
|
373 |
{update_values} set key1 = ?, key2 = ?, key3 = ? |
|
374 |
EOS |
|
375 | ||
try various way
|
376 |
our %VALID_TAG_NAMES = map {$_ => 1} qw/= <> < > >= <= like in insert_values update_set/; |
try varioud way
|
377 |
sub parse { |
378 |
my ($self, $template) = @_; |
|
379 |
$self->template($template); |
|
380 |
|
|
381 |
# Clean start; |
|
382 |
delete $self->{tree}; |
|
383 |
|
|
384 |
# Tags |
|
385 |
my $tag_start = quotemeta $self->tag_start; |
|
386 |
my $tag_end = quotemeta $self->tag_end; |
|
first commit
|
387 |
|
try varioud way
|
388 |
# Tokenize |
389 |
my $state = 'text'; |
|
390 |
|
|
391 |
# Save original template |
|
392 |
my $original_template = $template; |
|
393 |
|
|
394 |
# Text |
|
395 |
while ($template =~ s/([^$tag_start]*?)$tag_start([^$tag_end].*?)$tag_end//sm) { |
|
try various way
|
396 |
my $text = $1; |
try varioud way
|
397 |
my $tag = $2; |
398 |
|
|
try various way
|
399 |
push @{$self->tree}, {type => 'text', args => [$text]} if $text; |
try varioud way
|
400 |
|
401 |
if ($tag) { |
|
402 |
|
|
try various way
|
403 |
my ($tag_name, @args) = split /\s+/, $tag; |
try varioud way
|
404 |
|
try various way
|
405 |
$tag ||= ''; |
add test module
|
406 |
croak("Tag '$tag' in SQL template is not exist.\n\n" . |
try various way
|
407 |
"SQL template tag syntax\n$TAG_SYNTAX\n\n" . |
408 |
"Your SQL template is \n$original_template\n\n") |
|
409 |
unless $VALID_TAG_NAMES{$tag_name}; |
|
try varioud way
|
410 |
|
try various way
|
411 |
push @{$self->tree}, {type => 'tag', tag_name => $tag_name, args => [@args]}; |
try varioud way
|
412 |
} |
413 |
} |
|
414 |
|
|
try various way
|
415 |
push @{$self->tree}, {type => 'text', args => [$template]} if $template; |
first commit
|
416 |
} |
417 | ||
try various way
|
418 |
our %EXPAND_PLACE_HOLDER = map {$_ => 1} qw/= <> < > >= <= like/; |
419 |
sub build_sql { |
|
420 |
my ($self, $args) = @_; |
|
421 |
|
|
422 |
my $tree = $args->{tree} || $self->tree; |
|
423 |
my $bind_filter = $args->{bind_filter} || $self->bind_filter; |
|
424 |
my $values = exists $args->{values} ? $args->{values} : $self->values; |
|
425 |
|
|
426 |
my @bind_values; |
|
427 |
my $sql = ''; |
|
428 |
foreach my $node (@$tree) { |
|
429 |
my $type = $node->{type}; |
|
430 |
my $tag_name = $node->{tag_name}; |
|
431 |
my $args = $node->{args}; |
|
432 |
|
|
433 |
if ($type eq 'text') { |
|
434 |
# Join text |
|
435 |
$sql .= $args->[0]; |
|
436 |
} |
|
437 |
elsif ($type eq 'tag') { |
|
438 |
if ($EXPAND_PLACE_HOLDER{$tag_name}) { |
|
439 |
my $key = $args->[0]; |
|
440 |
|
|
441 |
# Filter Value |
|
442 |
if ($bind_filter) { |
|
try various way
|
443 |
push @bind_values, scalar $bind_filter->($key, $values->{$key}); |
try various way
|
444 |
} |
445 |
else { |
|
446 |
push @bind_values, $values->{$key}; |
|
447 |
} |
|
448 |
$tag_name = uc $tag_name if $self->upper_case; |
|
449 |
my $place_holder = "$key $tag_name ?"; |
|
450 |
$sql .= $place_holder; |
|
451 |
} |
|
try various way
|
452 |
elsif ($tag_name eq 'insert_values') { |
453 |
my $statement_keys = '('; |
|
454 |
my $statement_place_holders = '('; |
|
455 |
|
|
456 |
$values = $values->{insert_values}; |
|
457 |
|
|
458 |
foreach my $key (sort keys %$values) { |
|
459 |
if ($bind_filter) { |
|
460 |
push @bind_values, scalar $bind_filter->($key, $values->{$key}); |
|
461 |
} |
|
462 |
else { |
|
463 |
push @bind_values, $values->{$key}; |
|
464 |
} |
|
465 |
|
|
466 |
$statement_keys .= "$key, "; |
|
467 |
$statement_place_holders .= "?, "; |
|
468 |
} |
|
469 |
|
|
470 |
$statement_keys =~ s/, $//; |
|
471 |
$statement_keys .= ')'; |
|
472 |
|
|
473 |
$statement_place_holders =~ s/, $//; |
|
474 |
$statement_place_holders .= ')'; |
|
475 |
|
|
476 |
$sql .= "$statement_keys values $statement_place_holders"; |
|
477 |
} |
|
478 |
elsif ($tag_name eq 'update_set') { |
|
479 |
my $statement = 'set '; |
|
480 |
|
|
481 |
$values = $values->{update_set}; |
|
482 |
|
|
483 |
foreach my $key (sort keys %$values) { |
|
484 |
if ($bind_filter) { |
|
485 |
push @bind_values, scalar $bind_filter->($key, $values->{$key}); |
|
486 |
} |
|
487 |
else { |
|
488 |
push @bind_values, $values->{$key}; |
|
489 |
} |
|
490 |
|
|
491 |
$statement .= "$key = ?, "; |
|
492 |
} |
|
493 |
|
|
494 |
$statement =~ s/, $//; |
|
495 |
|
|
496 |
$sql .= $statement; |
|
497 |
} |
|
try various way
|
498 |
} |
499 |
} |
|
500 |
$sql .= ';' unless $sql =~ /;$/; |
|
501 |
return ($sql, @bind_values); |
|
502 |
} |
|
try varioud way
|
503 | |
cleanup
|
504 |
sub tag_processors : Attr {type => 'hash', deref => 1, auto_build => sub { |
505 |
shift->tag_processors( |
|
506 |
'=' => \&DBI::Custom::SQL::Template::TagProcessor::expand_place_holder, |
|
507 |
'<>' => \&DBI::Custom::SQL::Template::TagProcessor::expand_place_holder, |
|
508 |
'<' => \&DBI::Custom::SQL::Template::TagProcessor::expand_place_holder, |
|
509 |
'>=' => \&DBI::Custom::SQL::Template::TagProcessor::expand_place_holder, |
|
510 |
'<=' => \&DBI::Custom::SQL::Template::TagProcessor::expand_place_holder, |
|
511 |
'like' => \&DBI::Custom::SQL::Template::TagProcessor::expand_place_holder, |
|
512 |
'in' => \&DBI::Custom::SQL::Template::TagProcessor::expand_place_holder |
|
513 |
); |
|
514 |
}} |
|
515 | ||
516 |
sub add_tag_processor { |
|
517 |
|
|
518 |
} |
|
try varioud way
|
519 | |
first commit
|
520 |
Object::Simple->build_class; |
521 | ||
cleanup
|
522 | |
523 |
package DBI::Custom::SQL::Template::TagProcessor; |
|
524 | ||
525 |
sub expand_place_holder { |
|
526 |
my ($tag_name, $args, $values, $bind_filter, $sql_tmpl_obj) = @_; |
|
527 |
|
|
528 |
my $key = $args->[0]; |
|
529 |
|
|
530 |
my @bind_values; |
|
531 |
# Filter Value |
|
532 |
if ($tag_name eq 'in') { |
|
533 |
$values->{$key} = [$values->{$key}] unless ref $values->{$key} eq 'ARRAY'; |
|
534 |
if ($bind_filter) { |
|
535 |
for (my $i = 0; $i < @$values; $i++) { |
|
536 |
push @bind_values, scalar $bind_filter->($key, $values->{$key}->[$i]); |
|
537 |
} |
|
538 |
} |
|
539 |
else { |
|
540 |
for (my $i = 0; $i < @$values; $i++) { |
|
541 |
push @bind_values, $values->{$key}->[$i]; |
|
542 |
} |
|
543 |
} |
|
544 |
} |
|
545 |
else { |
|
546 |
if ($bind_filter) { |
|
547 |
push @bind_values, scalar $bind_filter->($key, $values->{$key}); |
|
548 |
} |
|
549 |
else { |
|
550 |
push @bind_values, $values->{$key}; |
|
551 |
} |
|
552 |
} |
|
553 |
if ($bind_filter) { |
|
554 |
if ($tag_name eq 'in') { |
|
555 |
for (my $i = 0; $i < @$values; $i++) { |
|
556 |
push @bind_values, scalar $bind_filter->($key, $values->{$key}->[$i]); |
|
557 |
} |
|
558 |
} |
|
559 |
else { |
|
560 |
push @bind_values, scalar $bind_filter->($key, $values->{$key}); |
|
561 |
} |
|
562 |
} |
|
563 |
else { |
|
564 |
push @bind_values, $values->{$key}; |
|
565 |
} |
|
566 |
|
|
567 |
$tag_name = uc $tag_name if $sql_tmpl_obj->upper_case; |
|
568 |
|
|
569 |
my $expand; |
|
570 |
if ($tag_name eq '?') { |
|
571 |
$expand = '?'; |
|
572 |
} |
|
573 |
elsif ($tag_name eq 'in') { |
|
574 |
$expand = '('; |
|
575 |
for (my $i = 0; $i < @$values; $i++) { |
|
576 |
$expand .= '?, '; |
|
577 |
} |
|
578 |
$expand =~ s/, $'//; |
|
579 |
$expand .= ')'; |
|
580 |
} |
|
581 |
else { |
|
582 |
$expand = "$key $tag_name ?"; |
|
583 |
} |
|
584 |
|
|
585 |
return ($expand, \@bind_values); |
|
586 |
} |
|
587 | ||
588 | ||
add various things
|
589 |
package DBI::Custom; |
590 |
1; |
|
591 | ||
first commit
|
592 |
=head1 NAME |
593 | ||
add test
|
594 |
DBI::Custom - Customizable simple DBI |
first commit
|
595 | |
596 |
=head1 VERSION |
|
597 | ||
add test
|
598 |
Version 0.0101 |
first commit
|
599 | |
600 |
=cut |
|
601 | ||
602 |
=head1 SYNOPSIS |
|
603 | ||
add test
|
604 |
my $dbi = DBI::Custom->new; |
first commit
|
605 | |
add test
|
606 |
=head1 METHODS |
first commit
|
607 | |
add test
|
608 |
=head2 add_filter |
first commit
|
609 | |
add test module
|
610 |
|
611 | ||
add test
|
612 |
=head2 bind_filter |
first commit
|
613 | |
add test
|
614 |
=head2 clone |
first commit
|
615 | |
add test
|
616 |
=head2 connect |
first commit
|
617 | |
add test
|
618 |
=head2 connect_info |
first commit
|
619 | |
add test
|
620 |
=head2 dbh |
first commit
|
621 | |
add test
|
622 |
=head2 fetch_filter |
first commit
|
623 | |
add test
|
624 |
=head2 filters |
first commit
|
625 | |
cleanup
|
626 |
=head2 initialize_class |
first commit
|
627 | |
cleanup
|
628 |
=head2 prototype |
first commit
|
629 | |
add test
|
630 |
=head2 new |
631 | ||
632 |
=head2 query |
|
first commit
|
633 | |
add test
|
634 |
=head2 create_sql |
635 | ||
636 |
=head2 query_raw_sql |
|
637 | ||
638 |
=head2 sql_template |
|
639 | ||
add tests
|
640 |
=head2 auto_commit |
641 | ||
642 |
=head2 connected |
|
643 | ||
644 |
=head2 dbh_option |
|
645 | ||
646 |
=head2 disconnect |
|
647 | ||
648 |
=head2 reconnect |
|
649 | ||
650 |
=head2 result_class |
|
651 | ||
add various
|
652 |
=head2 commit |
first commit
|
653 | |
add various
|
654 |
=head2 rollback |
first commit
|
655 | |
656 | ||
add various
|
657 |
=head1 AUTHOR |
first commit
|
658 | |
add various
|
659 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
first commit
|
660 | |
661 |
=head1 COPYRIGHT & LICENSE |
|
662 | ||
663 |
Copyright 2009 Yuki Kimoto, all rights reserved. |
|
664 | ||
665 |
This program is free software; you can redistribute it and/or modify it |
|
666 |
under the same terms as Perl itself. |
|
667 | ||
668 | ||
669 |
=cut |
|
670 | ||
671 |
1; # End of DBI::Custom |