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