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 test
|
55 |
sub bind_filter : Attr {} |
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(@_) } |
add various things
|
60 |
sub result_class : Attr { default => 'DBI::Custom::Result' } |
cleanup
|
61 | |
cleanup
|
62 |
sub dbh : Attr { auto_build => sub { shift->connect } } |
add test
|
63 |
sub sql_template : Attr { auto_build => sub { shift->sql_template(DBI::Custom::SQLTemplate->new) } } |
add test
|
64 | |
add various things
|
65 | |
add test
|
66 |
our %VALID_CONNECT_INFO = map {$_ => 1} qw/data_source user password options/; |
cleanup
|
67 | |
add various things
|
68 |
# Connect |
add some method
|
69 |
sub connect { |
70 |
my $self = shift; |
|
71 |
my $connect_info = $self->connect_info; |
|
72 |
|
|
add test
|
73 |
foreach my $key (keys %{$self->connect_info}) { |
add test
|
74 |
croak("connect_info '$key' is invald") |
75 |
unless $VALID_CONNECT_INFO{$key}; |
|
add test
|
76 |
} |
77 |
|
|
add some method
|
78 |
my $dbh = DBI->connect( |
add test
|
79 |
$connect_info->{data_source}, |
add some method
|
80 |
$connect_info->{user}, |
81 |
$connect_info->{password}, |
|
82 |
{ |
|
83 |
RaiseError => 1, |
|
84 |
PrintError => 0, |
|
85 |
AutoCommit => 1, |
|
86 |
%{$connect_info->{options} || {} } |
|
87 |
} |
|
88 |
); |
|
89 |
|
|
add various things
|
90 |
$self->auto_commit($self->dbh->{AutoCommit}); |
add some method
|
91 |
$self->dbh($dbh); |
92 |
} |
|
first commit
|
93 | |
add various things
|
94 |
# Is connected? |
95 |
sub connected { |
|
96 |
my $self = shift; |
|
97 |
return exists $sefl->{dbh}; |
|
98 |
} |
|
99 | ||
100 |
# Disconnect |
|
101 |
sub disconnect { |
|
102 |
my $self = shift; |
|
103 |
if ($self->conneced) { |
|
104 |
$self->dbh->disconnect; |
|
105 |
delete $self->{dbh}; |
|
106 |
} |
|
107 |
} |
|
108 | ||
109 |
# Reconnect |
|
110 |
sub reconnect { |
|
111 |
my $self = shift; |
|
112 |
$sefl->disconnect if $self->connected; |
|
113 |
$self->connect; |
|
114 |
} |
|
115 | ||
116 | ||
add test
|
117 |
sub create_sql { |
118 |
my $self = shift; |
|
119 |
|
|
120 |
my ($sql, @bind) = $self->sql_template->create_sql(@_); |
|
121 |
|
|
122 |
return ($sql, @bind); |
|
123 |
} |
|
124 | ||
add some method
|
125 |
sub query { |
try varioud way
|
126 |
my ($self, $template, $values, $filter) = @_; |
127 |
|
|
128 |
$filter ||= $self->bind_filter; |
|
129 |
|
|
add various things
|
130 |
my ($sql, @bind) = $self->create_sql($template, $values, $filter); |
131 |
my ( |
|
132 |
my $sth = $self->dbh->prepare($sql); |
|
133 |
$sth->execute(@bind); |
|
134 |
|
|
135 |
# Select |
|
136 |
if ($sth->{NUM_OF_FIELDS}) { |
|
137 |
my $result_class = $self->result_class; |
|
138 |
my $result = $result_class->new({sth => $sth}); |
|
139 |
return $result; |
|
140 |
} |
|
141 |
return; |
|
add test
|
142 |
} |
143 | ||
144 |
sub query_raw_sql { |
|
145 |
my ($self, $sql, @bind) = @_; |
|
add various things
|
146 |
my $sth = $self->dbh->prepare($sql); |
147 |
$sth->execute(@bind); |
|
148 |
return $sth; |
|
add test
|
149 |
} |
150 | ||
add various things
|
151 |
sub auto_commit : Attr { |
152 | ||
add test
|
153 |
Object::Simple->build_class; |
154 | ||
add various things
|
155 |
package DBI::Custom::Result; |
156 |
use Object::Simple; |
|
157 | ||
158 |
sub sth : Attr {}; |
|
159 | ||
160 |
sub fetchrow_arrayref { |
|
161 |
my $self = shift; |
|
162 |
$self->sth; |
|
163 |
|
|
164 |
|
|
165 |
} |
|
166 | ||
167 | ||
168 |
*fetch = \&fetchrow_arrayref; |
|
169 | ||
170 |
sub err { shift->sth->err } |
|
171 |
sub errstr { shift->sth->errstr } |
|
172 |
sub finish { shift->sth->finish } |
|
173 |
sub rows { shift->sth->rows } |
|
174 |
sub state { shift->sth->state } |
|
175 | ||
176 |
Object::Simple->build_class; |
|
177 | ||
178 | ||
add test
|
179 |
package DBI::Custom::SQLTemplate; |
180 |
use Object::Simple; |
|
try various way
|
181 |
use Carp 'croak'; |
add test
|
182 | |
try varioud way
|
183 |
### Attributes; |
try various way
|
184 |
sub tag_start : Attr { default => '{' } |
185 |
sub tag_end : Attr { default => '}' } |
|
186 |
sub template : Attr {}; |
|
187 |
sub tree : Attr { auto_build => sub { shift->tree([]) } } |
|
188 |
sub bind_filter : Attr {} |
|
189 |
sub values : Attr {} |
|
190 |
sub upper_case : Attr {default => 0} |
|
try varioud way
|
191 | |
add test
|
192 |
sub create_sql { |
try varioud way
|
193 |
my ($self, $template, $values, $filter) = @_; |
194 |
|
|
try various way
|
195 |
$filter ||= $self->bind_filter; |
196 |
|
|
try varioud way
|
197 |
$self->parse($template); |
198 |
|
|
try various way
|
199 |
my ($sql, @bind) = $self->build_sql({bind_filter => $filter, values => $values}); |
try varioud way
|
200 |
|
201 |
return ($sql, @bind); |
|
202 |
} |
|
203 | ||
204 |
our $TAG_SYNTAX = <<'EOS'; |
|
205 |
[tag] [expand] |
|
206 |
{= name} name = ? |
|
try various way
|
207 |
{<> name} name <> ? |
try varioud way
|
208 | |
209 |
{< name} name < ? |
|
210 |
{> name} name > ? |
|
211 |
{>= name} name >= ? |
|
212 |
{<= name} name <= ? |
|
213 | ||
214 |
{like name} name like ? |
|
215 |
{in name} name in [?, ?, ..] |
|
216 | ||
217 |
{insert_values} (key1, key2, key3) values (?, ?, ?) |
|
218 |
{update_values} set key1 = ?, key2 = ?, key3 = ? |
|
219 |
EOS |
|
220 | ||
try various way
|
221 |
our %VALID_TAG_NAMES = map {$_ => 1} qw/= <> < > >= <= like in insert_values update_set/; |
try varioud way
|
222 |
sub parse { |
223 |
my ($self, $template) = @_; |
|
224 |
$self->template($template); |
|
225 |
|
|
226 |
# Clean start; |
|
227 |
delete $self->{tree}; |
|
228 |
|
|
229 |
# Tags |
|
230 |
my $tag_start = quotemeta $self->tag_start; |
|
231 |
my $tag_end = quotemeta $self->tag_end; |
|
first commit
|
232 |
|
try varioud way
|
233 |
# Tokenize |
234 |
my $state = 'text'; |
|
235 |
|
|
236 |
# Save original template |
|
237 |
my $original_template = $template; |
|
238 |
|
|
239 |
# Text |
|
240 |
while ($template =~ s/([^$tag_start]*?)$tag_start([^$tag_end].*?)$tag_end//sm) { |
|
try various way
|
241 |
my $text = $1; |
try varioud way
|
242 |
my $tag = $2; |
243 |
|
|
try various way
|
244 |
push @{$self->tree}, {type => 'text', args => [$text]} if $text; |
try varioud way
|
245 |
|
246 |
if ($tag) { |
|
247 |
|
|
try various way
|
248 |
my ($tag_name, @args) = split /\s+/, $tag; |
try varioud way
|
249 |
|
try various way
|
250 |
$tag ||= ''; |
251 |
croak("Tag '$tag' in SQL template is invalid.\n\n" . |
|
252 |
"SQL template tag syntax\n$TAG_SYNTAX\n\n" . |
|
253 |
"Your SQL template is \n$original_template\n\n") |
|
254 |
unless $VALID_TAG_NAMES{$tag_name}; |
|
try varioud way
|
255 |
|
try various way
|
256 |
push @{$self->tree}, {type => 'tag', tag_name => $tag_name, args => [@args]}; |
try varioud way
|
257 |
} |
258 |
} |
|
259 |
|
|
try various way
|
260 |
push @{$self->tree}, {type => 'text', args => [$template]} if $template; |
first commit
|
261 |
} |
262 | ||
try various way
|
263 |
our %EXPAND_PLACE_HOLDER = map {$_ => 1} qw/= <> < > >= <= like/; |
264 |
sub build_sql { |
|
265 |
my ($self, $args) = @_; |
|
266 |
|
|
267 |
my $tree = $args->{tree} || $self->tree; |
|
268 |
my $bind_filter = $args->{bind_filter} || $self->bind_filter; |
|
269 |
my $values = exists $args->{values} ? $args->{values} : $self->values; |
|
270 |
|
|
271 |
my @bind_values; |
|
272 |
my $sql = ''; |
|
273 |
foreach my $node (@$tree) { |
|
274 |
my $type = $node->{type}; |
|
275 |
my $tag_name = $node->{tag_name}; |
|
276 |
my $args = $node->{args}; |
|
277 |
|
|
278 |
if ($type eq 'text') { |
|
279 |
# Join text |
|
280 |
$sql .= $args->[0]; |
|
281 |
} |
|
282 |
elsif ($type eq 'tag') { |
|
283 |
if ($EXPAND_PLACE_HOLDER{$tag_name}) { |
|
284 |
my $key = $args->[0]; |
|
285 |
|
|
286 |
# Filter Value |
|
287 |
if ($bind_filter) { |
|
try various way
|
288 |
push @bind_values, scalar $bind_filter->($key, $values->{$key}); |
try various way
|
289 |
} |
290 |
else { |
|
291 |
push @bind_values, $values->{$key}; |
|
292 |
} |
|
293 |
$tag_name = uc $tag_name if $self->upper_case; |
|
294 |
my $place_holder = "$key $tag_name ?"; |
|
295 |
$sql .= $place_holder; |
|
296 |
} |
|
try various way
|
297 |
elsif ($tag_name eq 'insert_values') { |
298 |
my $statement_keys = '('; |
|
299 |
my $statement_place_holders = '('; |
|
300 |
|
|
301 |
$values = $values->{insert_values}; |
|
302 |
|
|
303 |
foreach my $key (sort keys %$values) { |
|
304 |
if ($bind_filter) { |
|
305 |
push @bind_values, scalar $bind_filter->($key, $values->{$key}); |
|
306 |
} |
|
307 |
else { |
|
308 |
push @bind_values, $values->{$key}; |
|
309 |
} |
|
310 |
|
|
311 |
$statement_keys .= "$key, "; |
|
312 |
$statement_place_holders .= "?, "; |
|
313 |
} |
|
314 |
|
|
315 |
$statement_keys =~ s/, $//; |
|
316 |
$statement_keys .= ')'; |
|
317 |
|
|
318 |
$statement_place_holders =~ s/, $//; |
|
319 |
$statement_place_holders .= ')'; |
|
320 |
|
|
321 |
$sql .= "$statement_keys values $statement_place_holders"; |
|
322 |
} |
|
323 |
elsif ($tag_name eq 'update_set') { |
|
324 |
my $statement = 'set '; |
|
325 |
|
|
326 |
$values = $values->{update_set}; |
|
327 |
|
|
328 |
foreach my $key (sort keys %$values) { |
|
329 |
if ($bind_filter) { |
|
330 |
push @bind_values, scalar $bind_filter->($key, $values->{$key}); |
|
331 |
} |
|
332 |
else { |
|
333 |
push @bind_values, $values->{$key}; |
|
334 |
} |
|
335 |
|
|
336 |
$statement .= "$key = ?, "; |
|
337 |
} |
|
338 |
|
|
339 |
$statement =~ s/, $//; |
|
340 |
|
|
341 |
$sql .= $statement; |
|
342 |
} |
|
try various way
|
343 |
} |
344 |
} |
|
345 |
$sql .= ';' unless $sql =~ /;$/; |
|
346 |
return ($sql, @bind_values); |
|
347 |
} |
|
try varioud way
|
348 | |
349 | ||
first commit
|
350 |
Object::Simple->build_class; |
351 | ||
add various things
|
352 |
package DBI::Custom; |
353 |
1; |
|
354 | ||
first commit
|
355 |
=head1 NAME |
356 | ||
add test
|
357 |
DBI::Custom - Customizable simple DBI |
first commit
|
358 | |
359 |
=head1 VERSION |
|
360 | ||
add test
|
361 |
Version 0.0101 |
first commit
|
362 | |
363 |
=cut |
|
364 | ||
365 |
=head1 SYNOPSIS |
|
366 | ||
add test
|
367 |
my $dbi = DBI::Custom->new; |
first commit
|
368 | |
add test
|
369 |
=head1 METHODS |
first commit
|
370 | |
add test
|
371 |
=head2 add_filter |
first commit
|
372 | |
add test
|
373 |
=head2 bind_filter |
first commit
|
374 | |
add test
|
375 |
=head2 clone |
first commit
|
376 | |
add test
|
377 |
=head2 connect |
first commit
|
378 | |
add test
|
379 |
=head2 connect_info |
first commit
|
380 | |
add test
|
381 |
=head2 dbh |
first commit
|
382 | |
add test
|
383 |
=head2 fetch_filter |
first commit
|
384 | |
add test
|
385 |
=head2 filters |
first commit
|
386 | |
add test
|
387 |
=head2 initialize_model |
first commit
|
388 | |
add test
|
389 |
=head2 model |
first commit
|
390 | |
add test
|
391 |
=head2 new |
392 | ||
393 |
=head2 query |
|
first commit
|
394 | |
add test
|
395 |
=head2 create_sql |
396 | ||
397 |
=head2 query_raw_sql |
|
398 | ||
399 |
=head2 sql_template |
|
400 | ||
first commit
|
401 |
=head1 AUTHOR |
402 | ||
403 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
|
404 | ||
405 |
=head1 BUGS |
|
406 | ||
407 |
Please report any bugs or feature requests to C<bug-dbi-custom at rt.cpan.org>, or through |
|
408 |
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DBI-Custom>. I will be notified, and then you'll |
|
409 |
automatically be notified of progress on your bug as I make changes. |
|
410 | ||
411 | ||
412 | ||
413 | ||
414 |
=head1 SUPPORT |
|
415 | ||
416 |
You can find documentation for this module with the perldoc command. |
|
417 | ||
418 |
perldoc DBI::Custom |
|
419 | ||
420 | ||
421 |
You can also look for information at: |
|
422 | ||
423 |
=over 4 |
|
424 | ||
425 |
=item * RT: CPAN's request tracker |
|
426 | ||
427 |
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=DBI-Custom> |
|
428 | ||
429 |
=item * AnnoCPAN: Annotated CPAN documentation |
|
430 | ||
431 |
L<http://annocpan.org/dist/DBI-Custom> |
|
432 | ||
433 |
=item * CPAN Ratings |
|
434 | ||
435 |
L<http://cpanratings.perl.org/d/DBI-Custom> |
|
436 | ||
437 |
=item * Search CPAN |
|
438 | ||
439 |
L<http://search.cpan.org/dist/DBI-Custom/> |
|
440 | ||
441 |
=back |
|
442 | ||
443 | ||
444 |
=head1 ACKNOWLEDGEMENTS |
|
445 | ||
446 | ||
447 |
=head1 COPYRIGHT & LICENSE |
|
448 | ||
449 |
Copyright 2009 Yuki Kimoto, all rights reserved. |
|
450 | ||
451 |
This program is free software; you can redistribute it and/or modify it |
|
452 |
under the same terms as Perl itself. |
|
453 | ||
454 | ||
455 |
=cut |
|
456 | ||
457 |
1; # End of DBI::Custom |