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 |
try various
|
10 |
sub prototype : ClassAttr { auto_build => sub { |
add test
|
11 |
my $class = shift; |
cleanup
|
12 |
my $super = do { |
13 |
no strict 'refs'; |
|
14 |
${"${class}::ISA"}[0]; |
|
15 |
}; |
|
cleanup
|
16 |
my $prototype = eval{$super->can('prototype')} |
17 |
? $super->prototype->clone |
|
cleanup
|
18 |
: $class->Object::Simple::new; |
cleanup
|
19 |
|
cleanup
|
20 |
$class->prototype(bless $prototype, $class); |
try various
|
21 |
}} |
first commit
|
22 | |
cleanup
|
23 |
# New |
24 |
sub new { |
|
cleanup
|
25 |
my $invocant = shift; |
26 |
my $class = ref $invocant || $invocant; |
|
27 |
my $prototype = $class->prototype; |
|
28 |
my $self = $class->Object::Simple::new(%{$prototype->clone}, @_); |
|
29 |
return bless $self, $class; |
|
first commit
|
30 |
} |
31 | ||
cleanup
|
32 |
# Clone |
33 |
sub clone { |
|
cleanup
|
34 |
my $self = shift; |
cleanup
|
35 |
my $new = $self->Object::Simple::new; |
try various
|
36 |
|
cleanup
|
37 |
# Scalar copy |
38 |
foreach my $attr (qw/bind_filter fetch_filter result_class/) { |
|
39 |
$new->$attr($self->$attr); |
|
40 |
} |
|
try various
|
41 |
|
cleanup
|
42 |
# Hash ref copy |
43 |
foreach my $attr (qw/connect_info filters valid_connect_info/) { |
|
44 |
$new->$attr(\%{$self->$attr || {}}); |
|
45 |
} |
|
try various
|
46 |
|
cleanup
|
47 |
# Other |
48 |
$new->connect_info->{options} = \%{$self->connect_info->{options}}; |
|
try various
|
49 |
$new->sql_template($self->sql_template->clone); |
cleanup
|
50 |
} |
51 | ||
cleanup
|
52 |
# Attribute |
cleanup
|
53 |
sub connect_info : Attr { type => 'hash', default => sub { {} } } |
add tests
|
54 |
sub bind_filter : Attr {} |
add test
|
55 |
sub fetch_filter : Attr {} |
cleanup
|
56 | |
cleanup
|
57 |
sub filters : Attr { type => 'hash', deref => 1, default => sub { {} } } |
cleanup
|
58 |
sub add_filter { shift->filters(@_) } |
59 | ||
cleanup
|
60 |
sub result_class : Attr { default => 'DBI::Custom::Result' } |
add tests
|
61 |
sub dbh : Attr {} |
cleanup
|
62 |
sub sql_template : Attr { default => sub { DBI::Custom::SQL::Template->new } } |
63 |
sub valid_connect_info : Attr { type => 'hash', deref => 1, default => sub { |
|
64 |
return {map {$_ => 1} qw/data_source user password options/} |
|
65 |
}} |
|
add various
|
66 | |
67 |
# Auto commit |
|
68 |
sub auto_commit { |
|
69 |
my $self = shift; |
|
70 |
|
|
71 |
croak("Cannot change AutoCommit becouse of not connected") |
|
72 |
unless $self->dbh; |
|
73 |
|
|
74 |
if (@_) { |
|
75 |
$self->dbh->{AutoCommit} = $_[0]; |
|
76 |
return $self; |
|
77 |
} |
|
78 |
return $self->dbh->{AutoCommit}; |
|
79 |
} |
|
add test
|
80 | |
add various things
|
81 |
# Connect |
add some method
|
82 |
sub connect { |
83 |
my $self = shift; |
|
84 |
my $connect_info = $self->connect_info; |
|
85 |
|
|
add test
|
86 |
foreach my $key (keys %{$self->connect_info}) { |
add test module
|
87 |
croak("connect_info '$key' is wrong name") |
try various
|
88 |
unless $self->valid_connect_info->{$key}; |
add test
|
89 |
} |
90 |
|
|
add some method
|
91 |
my $dbh = DBI->connect( |
add test
|
92 |
$connect_info->{data_source}, |
add some method
|
93 |
$connect_info->{user}, |
94 |
$connect_info->{password}, |
|
95 |
{ |
|
96 |
RaiseError => 1, |
|
97 |
PrintError => 0, |
|
98 |
AutoCommit => 1, |
|
99 |
%{$connect_info->{options} || {} } |
|
100 |
} |
|
101 |
); |
|
102 |
|
|
103 |
$self->dbh($dbh); |
|
add various
|
104 |
return $self; |
add some method
|
105 |
} |
first commit
|
106 | |
add tests
|
107 |
sub DESTROY { |
108 |
my $self = shift; |
|
add tests
|
109 |
$self->disconnect if $self->connected; |
add tests
|
110 |
} |
111 | ||
add various things
|
112 |
# Is connected? |
113 |
sub connected { |
|
114 |
my $self = shift; |
|
add tests
|
115 |
return exists $self->{dbh} && eval {$self->{dbh}->can('prepare')}; |
add various things
|
116 |
} |
117 | ||
118 |
# Disconnect |
|
119 |
sub disconnect { |
|
120 |
my $self = shift; |
|
add tests
|
121 |
if ($self->connected) { |
add various things
|
122 |
$self->dbh->disconnect; |
123 |
delete $self->{dbh}; |
|
124 |
} |
|
125 |
} |
|
126 | ||
127 |
# Reconnect |
|
128 |
sub reconnect { |
|
129 |
my $self = shift; |
|
add tests
|
130 |
$self->disconnect if $self->connected; |
add various things
|
131 |
$self->connect; |
132 |
} |
|
133 | ||
try various
|
134 |
# Run tranzaction |
135 |
sub run_tranzaction { |
|
136 |
my ($self, $tranzaction) = @_; |
|
137 |
|
|
138 |
$self->auto_commit(0); |
|
139 |
|
|
140 |
eval { |
|
141 |
$tranzaction->(); |
|
142 |
$self->dbh->commit; |
|
143 |
}; |
|
144 |
|
|
145 |
if ($@) { |
|
146 |
my $tranzaction_error = $@; |
|
147 |
|
|
148 |
$self->dbh->rollback or croak("$@ and rollback also failed"); |
|
149 |
croak("$tranzaction_error"); |
|
150 |
} |
|
151 |
$self->auto_commit(1); |
|
add tests
|
152 |
} |
153 | ||
cleanup
|
154 |
# Create SQL from SQL template |
add test
|
155 |
sub create_sql { |
156 |
my $self = shift; |
|
157 |
|
|
158 |
my ($sql, @bind) = $self->sql_template->create_sql(@_); |
|
159 |
|
|
160 |
return ($sql, @bind); |
|
161 |
} |
|
162 | ||
cleanup
|
163 |
# Prepare and execute SQL |
add some method
|
164 |
sub query { |
try varioud way
|
165 |
my ($self, $template, $values, $filter) = @_; |
166 |
|
|
add tests
|
167 |
my $sth_options; |
168 |
|
|
169 |
# Rearrange when argumets is hash referecne |
|
170 |
if (ref $template eq 'HASH') { |
|
171 |
my $args = $template; |
|
172 |
($template, $values, $filter, $sth_options) |
|
173 |
= @{$args}{qw/template values filter sth_options/}; |
|
174 |
} |
|
175 |
|
|
try varioud way
|
176 |
$filter ||= $self->bind_filter; |
177 |
|
|
add various things
|
178 |
my ($sql, @bind) = $self->create_sql($template, $values, $filter); |
add tests
|
179 |
|
180 |
$self->connect unless $self->connected; |
|
181 |
|
|
add various things
|
182 |
my $sth = $self->dbh->prepare($sql); |
add tests
|
183 |
|
184 |
if ($sth_options) { |
|
185 |
foreach my $key (keys %$sth_options) { |
|
186 |
$sth->{$key} = $sth_options->{$key}; |
|
187 |
} |
|
188 |
} |
|
189 |
|
|
cleanup
|
190 |
# Execute |
add tests
|
191 |
my $ret_val = $sth->execute(@bind); |
add various things
|
192 |
|
cleanup
|
193 |
# Return resultset if select statement is executed |
add various things
|
194 |
if ($sth->{NUM_OF_FIELDS}) { |
195 |
my $result_class = $self->result_class; |
|
add various
|
196 |
my $result = $result_class->new({ |
197 |
sth => $sth, |
|
198 |
fetch_filter => $self->fetch_filter |
|
199 |
}); |
|
add various things
|
200 |
return $result; |
201 |
} |
|
add tests
|
202 |
return $ret_val; |
add test
|
203 |
} |
204 | ||
cleanup
|
205 |
# Prepare and execute raw SQL |
add test
|
206 |
sub query_raw_sql { |
cleanup
|
207 |
my ($self, $sql, @bind_values) = @_; |
add tests
|
208 |
|
cleanup
|
209 |
# Connect |
add various
|
210 |
$self->connect unless $self->connected; |
cleanup
|
211 |
|
212 |
# Add semicolon if not exist; |
|
add tests
|
213 |
$sql .= ';' unless $sql =~ /;$/; |
cleanup
|
214 |
|
215 |
# Prepare |
|
add various things
|
216 |
my $sth = $self->dbh->prepare($sql); |
cleanup
|
217 |
|
218 |
# Execute |
|
219 |
$sth->execute(@bind_values); |
|
220 |
|
|
add various things
|
221 |
return $sth; |
add test
|
222 |
} |
223 | ||
224 |
Object::Simple->build_class; |
|
225 | ||
cleanup
|
226 | |
add various things
|
227 |
package DBI::Custom::Result; |
228 |
use Object::Simple; |
|
229 | ||
cleanup
|
230 |
# Attributes |
add various
|
231 |
sub sth : Attr {} |
232 |
sub fetch_filter : Attr {} |
|
add various things
|
233 | |
cleanup
|
234 | |
cleanup
|
235 |
# Fetch (array) |
add various
|
236 |
sub fetch { |
237 |
my ($self, $type) = @_; |
|
238 |
my $sth = $self->sth; |
|
add various
|
239 |
my $fetch_filter = $self->fetch_filter; |
cleanup
|
240 |
|
241 |
# Fetch |
|
242 |
my $row = $sth->fetchrow_arrayref; |
|
243 |
|
|
244 |
# Cannot fetch |
|
245 |
return unless $row; |
|
246 |
|
|
247 |
# Filter |
|
248 |
if ($fetch_filter) { |
|
add tests
|
249 |
my $keys = $sth->{NAME_lc}; |
250 |
my $types = $sth->{TYPE}; |
|
cleanup
|
251 |
for (my $i = 0; $i < @$keys; $i++) { |
cleanup
|
252 |
$row->[$i]= $fetch_filter->($keys->[$i], $row->[$i], $types->[$i], |
253 |
$sth, $i); |
|
add various
|
254 |
} |
add various
|
255 |
} |
cleanup
|
256 |
return wantarray ? @$row : $row; |
257 |
} |
|
258 | ||
259 |
# Fetch (hash) |
|
260 |
sub fetch_hash { |
|
261 |
my $self = shift; |
|
262 |
my $sth = $self->sth; |
|
263 |
my $fetch_filter = $self->fetch_filter; |
|
264 |
|
|
265 |
# Fetch |
|
add tests
|
266 |
my $row = $sth->fetchrow_arrayref; |
cleanup
|
267 |
|
268 |
# Cannot fetch |
|
269 |
return unless $row; |
|
270 |
|
|
add tests
|
271 |
# Keys |
272 |
my $keys = $sth->{NAME_lc}; |
|
273 |
|
|
cleanup
|
274 |
# Filter |
add tests
|
275 |
my $row_hash = {}; |
cleanup
|
276 |
if ($fetch_filter) { |
add tests
|
277 |
my $types = $sth->{TYPE}; |
278 |
for (my $i = 0; $i < @$keys; $i++) { |
|
cleanup
|
279 |
$row_hash->{$keys->[$i]} = $fetch_filter->($keys->[$i], $row->[$i], |
280 |
$types->[$i], $sth, $i); |
|
add various
|
281 |
} |
add various
|
282 |
} |
cleanup
|
283 |
|
284 |
# No filter |
|
add tests
|
285 |
else { |
286 |
for (my $i = 0; $i < @$keys; $i++) { |
|
287 |
$row_hash->{$keys->[$i]} = $row->[$i]; |
|
288 |
} |
|
289 |
} |
|
290 |
return wantarray ? %$row_hash : $row_hash; |
|
add various
|
291 |
} |
add various things
|
292 | |
cleanup
|
293 |
# Fetch all (array) |
add various
|
294 |
sub fetch_all { |
cleanup
|
295 |
my $self = shift; |
add various
|
296 |
|
cleanup
|
297 |
my $rows = []; |
add tests
|
298 |
while(my @row = $self->fetch) { |
299 |
push @$rows, [@row]; |
|
add various
|
300 |
} |
cleanup
|
301 |
return wantarray ? @$rows : $rows; |
302 |
} |
|
303 | ||
304 |
# Fetch all (hash) |
|
305 |
sub fetch_all_hash { |
|
306 |
my $self = shift; |
|
307 |
|
|
308 |
my $rows = []; |
|
309 |
while(my %row = $self->fetch_hash) { |
|
310 |
push @$rows, {%row}; |
|
add various
|
311 |
} |
cleanup
|
312 |
return wantarray ? @$rows : $rows; |
add various
|
313 |
} |
add various things
|
314 | |
cleanup
|
315 |
# Finish |
add various
|
316 |
sub finish { shift->sth->finish } |
add various things
|
317 | |
cleanup
|
318 |
# Error |
319 |
sub error { |
|
320 |
my $self = shift; |
|
321 |
my $sth = $self->sth; |
|
cleanup
|
322 |
return wantarray ? ($sth->errstr, $sth->err, $sth->state) : $sth->errstr; |
cleanup
|
323 |
} |
324 | ||
add various things
|
325 |
Object::Simple->build_class; |
326 | ||
327 | ||
cleanup
|
328 |
package DBI::Custom::SQL::Template; |
add test
|
329 |
use Object::Simple; |
try various way
|
330 |
use Carp 'croak'; |
add test
|
331 | |
cleanup
|
332 |
# Clone |
try various
|
333 |
sub clone { |
334 |
my $self = shift; |
|
335 |
my $new = $self->Object::Simple::new; |
|
336 |
|
|
cleanup
|
337 |
# Scalar copy |
338 |
foreach my $attr (qw/tag_start tag_end bind_filter upper_case tag_syntax template/) { |
|
339 |
$new->$attr($self->$attr); |
|
340 |
} |
|
341 |
|
|
342 |
# Hash ref copy |
|
343 |
foreach my $attr (qw/tag_processors/) { |
|
344 |
$new->$attr(\%{$self->$attr || {}}); |
|
345 |
} |
|
346 |
|
|
347 |
# Other |
|
348 |
$new->tree([]); |
|
349 |
|
|
350 |
return $new; |
|
try various
|
351 |
} |
352 | ||
cleanup
|
353 | |
try varioud way
|
354 |
### Attributes; |
try various way
|
355 |
sub tag_start : Attr { default => '{' } |
356 |
sub tag_end : Attr { default => '}' } |
|
357 |
sub template : Attr {}; |
|
cleanup
|
358 |
sub tree : Attr { default => sub { [] } } |
try various way
|
359 |
sub bind_filter : Attr {} |
360 |
sub upper_case : Attr {default => 0} |
|
try varioud way
|
361 | |
try various
|
362 |
sub tag_syntax : Attr { default => <<'EOS' }; |
add test module
|
363 |
{? name} ? |
try varioud way
|
364 |
{= name} name = ? |
try various way
|
365 |
{<> name} name <> ? |
try varioud way
|
366 | |
367 |
{< name} name < ? |
|
368 |
{> name} name > ? |
|
369 |
{>= name} name >= ? |
|
370 |
{<= name} name <= ? |
|
371 | ||
372 |
{like name} name like ? |
|
373 |
{in name} name in [?, ?, ..] |
|
374 | ||
375 |
{insert_values} (key1, key2, key3) values (?, ?, ?) |
|
376 |
{update_values} set key1 = ?, key2 = ?, key3 = ? |
|
377 |
EOS |
|
378 | ||
cleanup
|
379 |
sub tag_processors : Attr {type => 'hash', deref => 1, auto_build => sub { |
380 |
shift->tag_processors( |
|
381 |
'?' => \&DBI::Custom::SQL::Template::TagProcessor::expand_place_holder, |
|
382 |
'=' => \&DBI::Custom::SQL::Template::TagProcessor::expand_place_holder, |
|
383 |
'<>' => \&DBI::Custom::SQL::Template::TagProcessor::expand_place_holder, |
|
384 |
'>' => \&DBI::Custom::SQL::Template::TagProcessor::expand_place_holder, |
|
385 |
'<' => \&DBI::Custom::SQL::Template::TagProcessor::expand_place_holder, |
|
386 |
'>=' => \&DBI::Custom::SQL::Template::TagProcessor::expand_place_holder, |
|
387 |
'<=' => \&DBI::Custom::SQL::Template::TagProcessor::expand_place_holder, |
|
388 |
'like' => \&DBI::Custom::SQL::Template::TagProcessor::expand_place_holder, |
|
389 |
'in' => \&DBI::Custom::SQL::Template::TagProcessor::expand_place_holder, |
|
390 |
'insert_values' => \&DBI::Custom::SQL::Template::TagProcessor::expand_insert_values, |
|
391 |
'update_set' => \&DBI::Custom::SQL::Template::TagProcessor::expand_update_set |
|
392 |
); |
|
393 |
}} |
|
394 | ||
395 |
sub add_tag_processor { |
|
396 |
my $class = shift; |
|
397 |
my $tag_processors = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
398 |
$class->tag_processor(%{$class->tag_processor}, %{$tag_processors}); |
|
399 |
} |
|
400 | ||
401 |
sub create_sql { |
|
402 |
my ($self, $template, $values, $filter) = @_; |
|
403 |
|
|
404 |
$filter ||= $self->bind_filter; |
|
405 |
|
|
406 |
$self->parse($template); |
|
407 |
|
|
408 |
my ($sql, @bind) = $self->build_sql({bind_filter => $filter, values => $values}); |
|
409 |
|
|
410 |
return ($sql, @bind); |
|
411 |
} |
|
412 | ||
try varioud way
|
413 |
sub parse { |
414 |
my ($self, $template) = @_; |
|
415 |
$self->template($template); |
|
416 |
|
|
417 |
# Clean start; |
|
cleanup
|
418 |
$self->tree([]); |
try varioud way
|
419 |
|
420 |
# Tags |
|
421 |
my $tag_start = quotemeta $self->tag_start; |
|
422 |
my $tag_end = quotemeta $self->tag_end; |
|
first commit
|
423 |
|
try varioud way
|
424 |
# Tokenize |
425 |
my $state = 'text'; |
|
426 |
|
|
427 |
# Save original template |
|
428 |
my $original_template = $template; |
|
429 |
|
|
430 |
# Text |
|
431 |
while ($template =~ s/([^$tag_start]*?)$tag_start([^$tag_end].*?)$tag_end//sm) { |
|
try various way
|
432 |
my $text = $1; |
try varioud way
|
433 |
my $tag = $2; |
434 |
|
|
try various way
|
435 |
push @{$self->tree}, {type => 'text', args => [$text]} if $text; |
try varioud way
|
436 |
|
437 |
if ($tag) { |
|
438 |
|
|
try various way
|
439 |
my ($tag_name, @args) = split /\s+/, $tag; |
try varioud way
|
440 |
|
try various way
|
441 |
$tag ||= ''; |
try various
|
442 |
unless ($self->tag_processors->{$tag_name}) { |
443 |
my $tag_syntax = $self->tag_syntax; |
|
cleanup
|
444 |
croak("Tag '{$tag}' in SQL template is not exist.\n\n" . |
try various
|
445 |
"SQL template tag syntax\n" . |
446 |
"$tag_syntax\n\n" . |
|
447 |
"Your SQL template is \n$original_template\n\n"); |
|
448 |
} |
|
try varioud way
|
449 |
|
try various way
|
450 |
push @{$self->tree}, {type => 'tag', tag_name => $tag_name, args => [@args]}; |
try varioud way
|
451 |
} |
452 |
} |
|
453 |
|
|
try various way
|
454 |
push @{$self->tree}, {type => 'text', args => [$template]} if $template; |
first commit
|
455 |
} |
456 | ||
try various way
|
457 |
sub build_sql { |
458 |
my ($self, $args) = @_; |
|
459 |
|
|
460 |
my $tree = $args->{tree} || $self->tree; |
|
461 |
my $bind_filter = $args->{bind_filter} || $self->bind_filter; |
|
cleanup
|
462 |
my $values = $args->{values} || {}; |
try various way
|
463 |
|
try various
|
464 |
my @bind_values_all; |
try various way
|
465 |
my $sql = ''; |
466 |
foreach my $node (@$tree) { |
|
467 |
my $type = $node->{type}; |
|
468 |
my $tag_name = $node->{tag_name}; |
|
469 |
my $args = $node->{args}; |
|
470 |
|
|
471 |
if ($type eq 'text') { |
|
472 |
# Join text |
|
473 |
$sql .= $args->[0]; |
|
474 |
} |
|
475 |
elsif ($type eq 'tag') { |
|
cleanup
|
476 |
my $tag_processor = $self->tag_processors->{$tag_name}; |
try various
|
477 |
|
478 |
croak("Tag processor '$type' must be code reference") |
|
479 |
unless ref $tag_processor eq 'CODE'; |
|
480 |
|
|
481 |
my ($expand, @bind_values) |
|
cleanup
|
482 |
= $tag_processor->($tag_name, $args, $values, |
483 |
$bind_filter, $self); |
|
try various
|
484 |
|
cleanup
|
485 |
$DB::single = 1; |
486 |
unless ($self->_placeholder_count($expand) == @bind_values) { |
|
try various
|
487 |
require Data::Dumper; |
try various way
|
488 |
|
try various
|
489 |
my $bind_values_dump |
cleanup
|
490 |
= Data::Dumper->Dump([\@bind_values], ['*bind_values']); |
try various way
|
491 |
|
try various
|
492 |
croak("Place holder count must be same as bind value count\n" . |
493 |
"Tag : $tag_name\n" . |
|
494 |
"Expand : $expand\n" . |
|
495 |
"Bind values: $bind_values_dump\n"); |
|
try various way
|
496 |
} |
try various
|
497 |
push @bind_values_all, @bind_values; |
498 |
$sql .= $expand; |
|
try various way
|
499 |
} |
500 |
} |
|
501 |
$sql .= ';' unless $sql =~ /;$/; |
|
try various
|
502 |
return ($sql, @bind_values_all); |
503 |
} |
|
504 | ||
505 |
sub _placeholder_count { |
|
506 |
my ($self, $expand) = @_; |
|
507 |
$expand ||= ''; |
|
508 |
|
|
509 |
my $count = 0; |
|
cleanup
|
510 |
my $pos = -1; |
511 |
while (($pos = index($expand, '?', $pos + 1)) != -1) { |
|
try various
|
512 |
$count++; |
513 |
} |
|
514 |
return $count; |
|
try various way
|
515 |
} |
try varioud way
|
516 | |
first commit
|
517 |
Object::Simple->build_class; |
518 | ||
cleanup
|
519 | |
520 |
package DBI::Custom::SQL::Template::TagProcessor; |
|
cleanup
|
521 |
use strict; |
522 |
use warnings; |
|
cleanup
|
523 | |
524 |
sub expand_place_holder { |
|
525 |
my ($tag_name, $args, $values, $bind_filter, $sql_tmpl_obj) = @_; |
|
526 |
|
|
527 |
my $key = $args->[0]; |
|
528 |
|
|
529 |
my @bind_values; |
|
530 |
# Filter Value |
|
531 |
if ($tag_name eq 'in') { |
|
532 |
$values->{$key} = [$values->{$key}] unless ref $values->{$key} eq 'ARRAY'; |
|
533 |
if ($bind_filter) { |
|
534 |
for (my $i = 0; $i < @$values; $i++) { |
|
535 |
push @bind_values, scalar $bind_filter->($key, $values->{$key}->[$i]); |
|
536 |
} |
|
537 |
} |
|
538 |
else { |
|
539 |
for (my $i = 0; $i < @$values; $i++) { |
|
540 |
push @bind_values, $values->{$key}->[$i]; |
|
541 |
} |
|
542 |
} |
|
543 |
} |
|
544 |
else { |
|
545 |
if ($bind_filter) { |
|
546 |
push @bind_values, scalar $bind_filter->($key, $values->{$key}); |
|
547 |
} |
|
548 |
else { |
|
549 |
push @bind_values, $values->{$key}; |
|
550 |
} |
|
551 |
} |
|
try various
|
552 |
|
cleanup
|
553 |
$tag_name = uc $tag_name if $sql_tmpl_obj->upper_case; |
554 |
|
|
555 |
my $expand; |
|
556 |
if ($tag_name eq '?') { |
|
557 |
$expand = '?'; |
|
558 |
} |
|
559 |
elsif ($tag_name eq 'in') { |
|
560 |
$expand = '('; |
|
561 |
for (my $i = 0; $i < @$values; $i++) { |
|
562 |
$expand .= '?, '; |
|
563 |
} |
|
564 |
$expand =~ s/, $'//; |
|
565 |
$expand .= ')'; |
|
566 |
} |
|
567 |
else { |
|
568 |
$expand = "$key $tag_name ?"; |
|
569 |
} |
|
570 |
|
|
try various
|
571 |
return ($expand, @bind_values); |
572 |
} |
|
573 | ||
574 |
sub expand_insert_values { |
|
575 |
my ($tag_name, $args, $values, $bind_filter, $sql_tmpl_obj) = @_; |
|
576 |
|
|
577 |
my $insert_keys = '('; |
|
578 |
my $place_holders = '('; |
|
579 |
|
|
580 |
$values = $args->[0] ? $values->{$args->[0]} : $values->{insert_values}; |
|
581 |
|
|
582 |
my @bind_values; |
|
583 |
foreach my $key (sort keys %$values) { |
|
584 |
$bind_filter ? push @bind_values, scalar $bind_filter->($key, $values->{$key}) |
|
585 |
: push @bind_values, $values->{$key}; |
|
586 |
|
|
587 |
$insert_keys .= "$key, "; |
|
588 |
$place_holders .= "?, "; |
|
589 |
} |
|
590 |
|
|
591 |
$insert_keys =~ s/, $//; |
|
592 |
$insert_keys .= ')'; |
|
593 |
|
|
594 |
$place_holders =~ s/, $//; |
|
595 |
$place_holders .= ')'; |
|
596 |
|
|
cleanup
|
597 |
my $expand = $sql_tmpl_obj->upper_case ? "$insert_keys VALUES $place_holders" |
598 |
: "$insert_keys values $place_holders"; |
|
try various
|
599 |
|
600 |
return ($expand, @bind_values); |
|
601 |
} |
|
602 | ||
603 |
sub expand_update_set { |
|
604 |
my ($tag_name, $args, $values, $bind_filter, $sql_tmpl_obj) = @_; |
|
605 |
|
|
cleanup
|
606 |
my $expand = $sql_tmpl_obj->upper_case ? 'SET ' : 'set '; |
try various
|
607 |
$values = $args->[0] ? $values->{$args->[0]} : $values->{update_set}; |
608 |
|
|
609 |
my @bind_values; |
|
610 |
foreach my $key (sort keys %$values) { |
|
611 |
$bind_filter ? push @bind_values, scalar $bind_filter->($key, $values->{$key}) |
|
612 |
: push @bind_values, $values->{$key}; |
|
613 |
|
|
614 |
$expand .= "$key = ?, "; |
|
615 |
} |
|
616 |
$expand =~ s/, $//; |
|
617 |
return ($expand, @bind_values); |
|
cleanup
|
618 |
} |
619 | ||
620 | ||
add various things
|
621 |
package DBI::Custom; |
622 |
1; |
|
623 | ||
first commit
|
624 |
=head1 NAME |
625 | ||
add test
|
626 |
DBI::Custom - Customizable simple DBI |
first commit
|
627 | |
628 |
=head1 VERSION |
|
629 | ||
add test
|
630 |
Version 0.0101 |
first commit
|
631 | |
632 |
=cut |
|
633 | ||
634 |
=head1 SYNOPSIS |
|
635 | ||
add test
|
636 |
my $dbi = DBI::Custom->new; |
first commit
|
637 | |
add test
|
638 |
=head1 METHODS |
first commit
|
639 | |
add test
|
640 |
=head2 add_filter |
first commit
|
641 | |
add test
|
642 |
=head2 bind_filter |
first commit
|
643 | |
add test
|
644 |
=head2 clone |
first commit
|
645 | |
add test
|
646 |
=head2 connect |
first commit
|
647 | |
add test
|
648 |
=head2 connect_info |
first commit
|
649 | |
add test
|
650 |
=head2 dbh |
first commit
|
651 | |
add test
|
652 |
=head2 fetch_filter |
first commit
|
653 | |
add test
|
654 |
=head2 filters |
first commit
|
655 | |
cleanup
|
656 |
=head2 prototype |
first commit
|
657 | |
add test
|
658 |
=head2 new |
659 | ||
660 |
=head2 query |
|
first commit
|
661 | |
add test
|
662 |
=head2 create_sql |
663 | ||
664 |
=head2 query_raw_sql |
|
665 | ||
666 |
=head2 sql_template |
|
667 | ||
add tests
|
668 |
=head2 auto_commit |
669 | ||
670 |
=head2 connected |
|
671 | ||
672 |
=head2 disconnect |
|
673 | ||
674 |
=head2 reconnect |
|
675 | ||
676 |
=head2 result_class |
|
677 | ||
cleanup
|
678 |
=head2 run_tranzaction |
first commit
|
679 | |
cleanup
|
680 |
=head2 valid_connect_info |
first commit
|
681 | |
682 | ||
add various
|
683 |
=head1 AUTHOR |
first commit
|
684 | |
add various
|
685 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
first commit
|
686 | |
687 |
=head1 COPYRIGHT & LICENSE |
|
688 | ||
689 |
Copyright 2009 Yuki Kimoto, all rights reserved. |
|
690 | ||
691 |
This program is free software; you can redistribute it and/or modify it |
|
692 |
under the same terms as Perl itself. |
|
693 | ||
694 | ||
695 |
=cut |
|
696 | ||
697 |
1; # End of DBI::Custom |