cleanup
|
1 |
package DBIx::Custom; |
2 | ||
3 |
use strict; |
|
4 |
use warnings; |
|
5 | ||
remove run_transaction().
|
6 |
use base 'Object::Simple'; |
many change
|
7 | |
packaging one directory
|
8 |
use Carp 'croak'; |
9 |
use DBI; |
|
10 |
use DBIx::Custom::Result; |
|
many many changes
|
11 |
use DBIx::Custom::SQLTemplate; |
cleanup
|
12 |
use DBIx::Custom::Query; |
update document
|
13 |
use Encode qw/encode_utf8 decode_utf8/; |
packaging one directory
|
14 | |
catch up with Object::Simple...
|
15 |
__PACKAGE__->attr('dbh'); |
cleanup
|
16 |
__PACKAGE__->attr([qw/user password data_source/]); |
update document
|
17 |
__PACKAGE__->attr([qw/default_query_filter default_fetch_filter/]); |
packaging one directory
|
18 | |
removed register_format()
|
19 |
__PACKAGE__->dual_attr('filters', default => sub { {} }, |
20 |
inherit => 'hash_copy'); |
|
21 |
__PACKAGE__->register_filter( |
|
update document
|
22 |
encode_utf8 => sub { encode_utf8($_[0]) }, |
23 |
decode_utf8 => sub { decode_utf8($_[0]) } |
|
removed register_format()
|
24 |
); |
packaging one directory
|
25 | |
cleanup
|
26 |
__PACKAGE__->attr(result_class => 'DBIx::Custom::Result'); |
removed register_format()
|
27 |
__PACKAGE__->attr(sql_template => sub { DBIx::Custom::SQLTemplate->new }); |
added cache_method attribute
|
28 | |
add cache attribute
|
29 |
__PACKAGE__->attr(cache => 1); |
added cache_method attribute
|
30 |
__PACKAGE__->attr(cache_method => sub { |
31 |
sub { |
|
32 |
my $self = shift; |
|
33 |
|
|
34 |
$self->{_cached} ||= {}; |
|
35 |
|
|
36 |
if (@_ > 1) { |
|
37 |
$self->{_cached}{$_[0]} = $_[1] |
|
38 |
} |
|
39 |
else { |
|
40 |
return $self->{_cached}{$_[0]} |
|
41 |
} |
|
42 |
} |
|
43 |
}); |
|
removed register_format()
|
44 | |
packaging one directory
|
45 |
sub connect { |
removed register_format()
|
46 |
my $proto = shift; |
47 |
|
|
48 |
# Create |
|
49 |
my $self = ref $proto ? $proto : $proto->new(@_); |
|
update document
|
50 |
|
51 |
# Information |
|
packaging one directory
|
52 |
my $data_source = $self->data_source; |
53 |
my $user = $self->user; |
|
54 |
my $password = $self->password; |
|
55 |
|
|
update document
|
56 |
# Connect |
select, insert, update, upda...
|
57 |
my $dbh = eval {DBI->connect( |
packaging one directory
|
58 |
$data_source, |
59 |
$user, |
|
60 |
$password, |
|
61 |
{ |
|
62 |
RaiseError => 1, |
|
63 |
PrintError => 0, |
|
64 |
AutoCommit => 1, |
|
65 |
} |
|
66 |
)}; |
|
67 |
|
|
update document
|
68 |
# Connect error |
packaging one directory
|
69 |
croak $@ if $@; |
70 |
|
|
update document
|
71 |
# Database handle |
packaging one directory
|
72 |
$self->dbh($dbh); |
update document
|
73 |
|
packaging one directory
|
74 |
return $self; |
75 |
} |
|
76 | ||
77 |
sub disconnect { |
|
78 |
my $self = shift; |
|
update document
|
79 |
|
removed reconnect method
|
80 |
# Disconnect |
select, insert, update, upda...
|
81 |
my $ret = eval { $self->dbh->disconnect }; |
82 |
croak $@ if $@; |
|
removed reconnect method
|
83 |
$self->dbh(undef); |
update document
|
84 |
|
select, insert, update, upda...
|
85 |
return $ret; |
packaging one directory
|
86 |
} |
87 | ||
removed register_format()
|
88 |
our %VALID_INSERT_ARGS = map { $_ => 1 } qw/table param append filter/; |
cleanup insert
|
89 | |
packaging one directory
|
90 |
sub insert { |
select, insert, update, upda...
|
91 |
my ($self, %args) = @_; |
removed register_format()
|
92 | |
cleanup insert
|
93 |
# Check arguments |
select, insert, update, upda...
|
94 |
foreach my $name (keys %args) { |
cleanup insert
|
95 |
croak "\"$name\" is invalid name" |
96 |
unless $VALID_INSERT_ARGS{$name}; |
|
97 |
} |
|
98 |
|
|
removed register_format()
|
99 |
# Arguments |
select, insert, update, upda...
|
100 |
my $table = $args{table} || ''; |
101 |
my $param = $args{param} || {}; |
|
102 |
my $append = $args{append} || ''; |
|
103 |
my $filter = $args{filter}; |
|
packaging one directory
|
104 |
|
105 |
# Insert keys |
|
removed register_format()
|
106 |
my @insert_keys = keys %$param; |
packaging one directory
|
107 |
|
108 |
# Not exists insert keys |
|
109 |
croak("Key-value pairs for insert must be specified to 'insert' second argument") |
|
110 |
unless @insert_keys; |
|
111 |
|
|
112 |
# Templte for insert |
|
113 |
my $template = "insert into $table {insert " . join(' ', @insert_keys) . '}'; |
|
removed register_format()
|
114 |
$template .= " $append" if $append; |
packaging one directory
|
115 |
|
116 |
# Execute query |
|
removed register_format()
|
117 |
my $ret_val = $self->execute($template, param => $param, |
118 |
filter => $filter); |
|
packaging one directory
|
119 |
|
120 |
return $ret_val; |
|
121 |
} |
|
122 | ||
cleanup update and update_al...
|
123 |
our %VALID_UPDATE_ARGS |
removed register_format()
|
124 |
= map { $_ => 1 } qw/table param where append filter allow_update_all/; |
cleanup update and update_al...
|
125 | |
packaging one directory
|
126 |
sub update { |
select, insert, update, upda...
|
127 |
my ($self, %args) = @_; |
cleanup update and update_al...
|
128 |
|
129 |
# Check arguments |
|
select, insert, update, upda...
|
130 |
foreach my $name (keys %args) { |
cleanup update and update_al...
|
131 |
croak "\"$name\" is invalid name" |
132 |
unless $VALID_UPDATE_ARGS{$name}; |
|
133 |
} |
|
134 |
|
|
135 |
# Arguments |
|
select, insert, update, upda...
|
136 |
my $table = $args{table} || ''; |
137 |
my $param = $args{param} || {}; |
|
138 |
my $where = $args{where} || {}; |
|
139 |
my $append_statement = $args{append} || ''; |
|
140 |
my $filter = $args{filter}; |
|
141 |
my $allow_update_all = $args{allow_update_all}; |
|
packaging one directory
|
142 |
|
143 |
# Update keys |
|
removed register_format()
|
144 |
my @update_keys = keys %$param; |
packaging one directory
|
145 |
|
146 |
# Not exists update kyes |
|
147 |
croak("Key-value pairs for update must be specified to 'update' second argument") |
|
148 |
unless @update_keys; |
|
149 |
|
|
150 |
# Where keys |
|
removed register_format()
|
151 |
my @where_keys = keys %$where; |
packaging one directory
|
152 |
|
153 |
# Not exists where keys |
|
154 |
croak("Key-value pairs for where clause must be specified to 'update' third argument") |
|
cleanup update and update_al...
|
155 |
if !@where_keys && !$allow_update_all; |
packaging one directory
|
156 |
|
157 |
# Update clause |
|
158 |
my $update_clause = '{update ' . join(' ', @update_keys) . '}'; |
|
159 |
|
|
160 |
# Where clause |
|
161 |
my $where_clause = ''; |
|
simplify filtering system
|
162 |
my $new_where = {}; |
many change
|
163 |
|
packaging one directory
|
164 |
if (@where_keys) { |
165 |
$where_clause = 'where '; |
|
166 |
foreach my $where_key (@where_keys) { |
|
simplify filtering system
|
167 |
|
168 |
$where_clause .= "{= $where_key} and "; |
|
packaging one directory
|
169 |
} |
170 |
$where_clause =~ s/ and $//; |
|
171 |
} |
|
172 |
|
|
173 |
# Template for update |
|
174 |
my $template = "update $table $update_clause $where_clause"; |
|
insert, update, delete appnd...
|
175 |
$template .= " $append_statement" if $append_statement; |
packaging one directory
|
176 |
|
177 |
# Rearrange parammeters |
|
removed register_format()
|
178 |
foreach my $wkey (@where_keys) { |
simplify filtering system
|
179 |
|
removed register_format()
|
180 |
if (exists $param->{$wkey}) { |
181 |
$param->{$wkey} = [$param->{$wkey}] |
|
182 |
unless ref $param->{$wkey} eq 'ARRAY'; |
|
simplify filtering system
|
183 |
|
removed register_format()
|
184 |
push @{$param->{$wkey}}, $where->{$wkey}; |
simplify filtering system
|
185 |
} |
add tests
|
186 |
else { |
removed register_format()
|
187 |
$param->{$wkey} = $where->{$wkey}; |
add tests
|
188 |
} |
simplify filtering system
|
189 |
} |
packaging one directory
|
190 |
|
191 |
# Execute query |
|
removed register_format()
|
192 |
my $ret_val = $self->execute($template, param => $param, |
193 |
filter => $filter); |
|
packaging one directory
|
194 |
|
195 |
return $ret_val; |
|
196 |
} |
|
197 | ||
select, insert, update, upda...
|
198 |
sub update_all { shift->update(allow_update_all => 1, @_) }; |
packaging one directory
|
199 | |
refactoring delete and delet...
|
200 |
our %VALID_DELETE_ARGS |
removed register_format()
|
201 |
= map { $_ => 1 } qw/table where append filter allow_delete_all/; |
refactoring delete and delet...
|
202 | |
packaging one directory
|
203 |
sub delete { |
select, insert, update, upda...
|
204 |
my ($self, %args) = @_; |
refactoring delete and delet...
|
205 |
|
206 |
# Check arguments |
|
select, insert, update, upda...
|
207 |
foreach my $name (keys %args) { |
refactoring delete and delet...
|
208 |
croak "\"$name\" is invalid name" |
209 |
unless $VALID_DELETE_ARGS{$name}; |
|
210 |
} |
|
211 |
|
|
212 |
# Arguments |
|
select, insert, update, upda...
|
213 |
my $table = $args{table} || ''; |
214 |
my $where = $args{where} || {}; |
|
215 |
my $append_statement = $args{append}; |
|
216 |
my $filter = $args{filter}; |
|
217 |
my $allow_delete_all = $args{allow_delete_all}; |
|
packaging one directory
|
218 |
|
219 |
# Where keys |
|
removed register_format()
|
220 |
my @where_keys = keys %$where; |
packaging one directory
|
221 |
|
222 |
# Not exists where keys |
|
223 |
croak("Key-value pairs for where clause must be specified to 'delete' second argument") |
|
refactoring delete and delet...
|
224 |
if !@where_keys && !$allow_delete_all; |
packaging one directory
|
225 |
|
226 |
# Where clause |
|
227 |
my $where_clause = ''; |
|
228 |
if (@where_keys) { |
|
229 |
$where_clause = 'where '; |
|
removed register_format()
|
230 |
foreach my $wkey (@where_keys) { |
231 |
$where_clause .= "{= $wkey} and "; |
|
packaging one directory
|
232 |
} |
233 |
$where_clause =~ s/ and $//; |
|
234 |
} |
|
235 |
|
|
236 |
# Template for delete |
|
237 |
my $template = "delete from $table $where_clause"; |
|
insert, update, delete appnd...
|
238 |
$template .= " $append_statement" if $append_statement; |
packaging one directory
|
239 |
|
240 |
# Execute query |
|
removed register_format()
|
241 |
my $ret_val = $self->execute($template, param => $where, |
242 |
filter => $filter); |
|
packaging one directory
|
243 |
|
244 |
return $ret_val; |
|
245 |
} |
|
246 | ||
select, insert, update, upda...
|
247 |
sub delete_all { shift->delete(allow_delete_all => 1, @_) } |
packaging one directory
|
248 | |
refactoring select
|
249 |
our %VALID_SELECT_ARGS |
added commit method
|
250 |
= map { $_ => 1 } qw/table column where append relation filter param/; |
refactoring select
|
251 | |
packaging one directory
|
252 |
sub select { |
select, insert, update, upda...
|
253 |
my ($self, %args) = @_; |
packaging one directory
|
254 |
|
refactoring select
|
255 |
# Check arguments |
select, insert, update, upda...
|
256 |
foreach my $name (keys %args) { |
refactoring select
|
257 |
croak "\"$name\" is invalid name" |
258 |
unless $VALID_SELECT_ARGS{$name}; |
|
259 |
} |
|
packaging one directory
|
260 |
|
refactoring select
|
261 |
# Arguments |
select, insert, update, upda...
|
262 |
my $tables = $args{table} || []; |
removed register_format()
|
263 |
$tables = [$tables] unless ref $tables eq 'ARRAY'; |
select, insert, update, upda...
|
264 |
my $columns = $args{column} || []; |
265 |
my $where = $args{where} || {}; |
|
266 |
my $relation = $args{relation}; |
|
267 |
my $append = $args{append}; |
|
268 |
my $filter = $args{filter}; |
|
269 |
my $param = $args{param} || {}; |
|
packaging one directory
|
270 |
|
271 |
# SQL template for select statement |
|
272 |
my $template = 'select '; |
|
273 |
|
|
added commit method
|
274 |
# Column clause |
packaging one directory
|
275 |
if (@$columns) { |
276 |
foreach my $column (@$columns) { |
|
277 |
$template .= "$column, "; |
|
278 |
} |
|
279 |
$template =~ s/, $/ /; |
|
280 |
} |
|
281 |
else { |
|
282 |
$template .= '* '; |
|
283 |
} |
|
284 |
|
|
added commit method
|
285 |
# Table |
packaging one directory
|
286 |
$template .= 'from '; |
287 |
foreach my $table (@$tables) { |
|
288 |
$template .= "$table, "; |
|
289 |
} |
|
290 |
$template =~ s/, $/ /; |
|
291 |
|
|
added commit method
|
292 |
# Where clause |
293 |
my @where_keys = keys %$where; |
|
packaging one directory
|
294 |
if (@where_keys) { |
295 |
$template .= 'where '; |
|
296 |
foreach my $where_key (@where_keys) { |
|
compile success
|
297 |
$template .= "{= $where_key} and "; |
packaging one directory
|
298 |
} |
299 |
} |
|
300 |
$template =~ s/ and $//; |
|
301 |
|
|
added commit method
|
302 |
# Relation |
303 |
if ($relation) { |
|
304 |
$template .= @where_keys ? "and " : "where "; |
|
305 |
foreach my $rkey (keys %$relation) { |
|
306 |
$template .= "$rkey = " . $relation->{$rkey} . " and "; |
|
packaging one directory
|
307 |
} |
308 |
} |
|
added commit method
|
309 |
$template =~ s/ and $//; |
310 |
|
|
311 |
# Append some statement |
|
312 |
$template .= " $append" if $append; |
|
packaging one directory
|
313 |
|
314 |
# Execute query |
|
added commit method
|
315 |
my $result = $self->execute($template, param => $where, |
removed register_format()
|
316 |
filter => $filter); |
packaging one directory
|
317 |
|
318 |
return $result; |
|
319 |
} |
|
320 | ||
removed reconnect method
|
321 |
sub create_query { |
322 |
my ($self, $template) = @_; |
|
version 0.0901
|
323 |
|
removed reconnect method
|
324 |
# Create query from SQL template |
325 |
my $sql_template = $self->sql_template; |
|
removed register_format()
|
326 |
|
added cache_method attribute
|
327 |
my $cache = $self->cache; |
version 0.0901
|
328 |
|
removed reconnect method
|
329 |
# Create query |
330 |
my $query; |
|
added cache_method attribute
|
331 |
if ($cache) { |
332 |
|
|
333 |
# Cached query |
|
334 |
my $q = $self->cache_method->($self, $template); |
|
335 |
|
|
336 |
# Create query |
|
337 |
$query = DBIx::Custom::Query->new($q) if $q; |
|
removed reconnect method
|
338 |
} |
added cache_method attribute
|
339 |
|
340 |
unless ($query) { |
|
341 |
|
|
342 |
# Create query |
|
removed reconnect method
|
343 |
$query = eval{$sql_template->create_query($template)}; |
344 |
croak($@) if $@; |
|
345 |
|
|
added cache_method attribute
|
346 |
# Cache query |
347 |
$self->cache_method->($self, $template, |
|
348 |
{sql => $query->sql, |
|
349 |
columns => $query->columns}) |
|
350 |
if $cache; |
|
removed reconnect method
|
351 |
} |
version 0.0901
|
352 |
|
removed reconnect method
|
353 |
# Prepare statement handle |
select, insert, update, upda...
|
354 |
my $sth = eval {$self->dbh->prepare($query->{sql})}; |
355 |
croak $@ if $@; |
|
renamed fetch_rows to fetch_...
|
356 |
|
removed reconnect method
|
357 |
# Set statement handle |
358 |
$query->sth($sth); |
|
359 |
|
|
360 |
return $query; |
|
361 |
} |
|
362 | ||
363 |
our %VALID_EXECUTE_ARGS = map { $_ => 1 } qw/param filter/; |
|
364 | ||
365 |
sub execute{ |
|
select, insert, update, upda...
|
366 |
my ($self, $query, %args) = @_; |
removed reconnect method
|
367 |
|
368 |
# Check arguments |
|
select, insert, update, upda...
|
369 |
foreach my $name (keys %args) { |
removed reconnect method
|
370 |
croak "\"$name\" is invalid name" |
371 |
unless $VALID_EXECUTE_ARGS{$name}; |
|
372 |
} |
|
373 |
|
|
select, insert, update, upda...
|
374 |
my $params = $args{param} || {}; |
removed reconnect method
|
375 |
|
376 |
# First argument is SQL template |
|
377 |
unless (ref $query eq 'DBIx::Custom::Query') { |
|
378 |
my $template; |
|
379 |
|
|
380 |
if (ref $query eq 'ARRAY') { |
|
381 |
$template = $query->[0]; |
|
382 |
} |
|
383 |
else { $template = $query } |
|
384 |
|
|
385 |
$query = $self->create_query($template); |
|
386 |
} |
|
387 |
|
|
select, insert, update, upda...
|
388 |
my $filter = $args{filter} || $query->filter || {}; |
removed reconnect method
|
389 |
|
390 |
# Create bind value |
|
391 |
my $bind_values = $self->_build_bind_values($query, $params, $filter); |
|
392 |
|
|
393 |
# Execute |
|
394 |
my $sth = $query->sth; |
|
select, insert, update, upda...
|
395 |
my $affected = eval {$sth->execute(@$bind_values)}; |
396 |
croak $@ if $@; |
|
removed reconnect method
|
397 |
|
398 |
# Return resultset if select statement is executed |
|
399 |
if ($sth->{NUM_OF_FIELDS}) { |
|
400 |
|
|
401 |
# Get result class |
|
402 |
my $result_class = $self->result_class; |
|
403 |
|
|
404 |
# Create result |
|
405 |
my $result = $result_class->new({ |
|
406 |
sth => $sth, |
|
407 |
default_filter => $self->default_fetch_filter, |
|
408 |
filters => $self->filters |
|
409 |
}); |
|
410 |
return $result; |
|
411 |
} |
|
412 |
return $affected; |
|
413 |
} |
|
414 | ||
415 |
sub _build_bind_values { |
|
416 |
my ($self, $query, $params, $filter) = @_; |
|
417 |
|
|
418 |
# binding values |
|
419 |
my @bind_values; |
|
420 |
|
|
421 |
# Build bind values |
|
422 |
my $count = {}; |
|
423 |
foreach my $column (@{$query->columns}) { |
|
424 |
|
|
425 |
croak "\"$column\" is not exists in params" |
|
426 |
unless exists $params->{$column}; |
|
427 |
|
|
428 |
# Value |
|
429 |
my $value = ref $params->{$column} eq 'ARRAY' |
|
430 |
? $params->{$column}->[$count->{$column} || 0] |
|
431 |
: $params->{$column}; |
|
432 |
|
|
433 |
# Filter |
|
434 |
$filter ||= {}; |
|
435 |
|
|
436 |
# Filter name |
|
437 |
my $fname = $filter->{$column} || $self->default_query_filter || ''; |
|
438 |
|
|
439 |
my $filter_func; |
|
440 |
if ($fname) { |
|
441 |
|
|
442 |
if (ref $fname eq 'CODE') { |
|
443 |
$filter_func = $fname; |
|
444 |
} |
|
445 |
else { |
|
446 |
my $filters = $self->filters; |
|
447 |
croak "Not exists filter \"$fname\"" unless exists $filters->{$fname}; |
|
448 |
$filter_func = $filters->{$fname}; |
|
449 |
} |
|
450 |
} |
|
451 |
|
|
452 |
push @bind_values, $filter_func |
|
453 |
? $filter_func->($value) |
|
454 |
: $value; |
|
455 |
|
|
456 |
# Count up |
|
457 |
$count->{$column}++; |
|
458 |
} |
|
459 |
|
|
460 |
return \@bind_values; |
|
461 |
} |
|
462 | ||
463 |
sub register_filter { |
|
464 |
my $invocant = shift; |
|
465 |
|
|
466 |
# Add filter |
|
467 |
my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
468 |
$invocant->filters({%{$invocant->filters}, %$filters}); |
|
469 |
|
|
470 |
return $invocant; |
|
471 |
} |
|
472 | ||
473 |
sub DESTROY { |
|
474 |
my $self = shift; |
|
475 |
|
|
476 |
# Disconnect |
|
477 |
$self->disconnect if $self->dbh; |
|
478 |
} |
|
479 | ||
480 |
=head1 NAME |
|
481 | ||
482 |
DBIx::Custom - DBI with hash parameter binding and filtering system |
|
483 | ||
484 |
=cut |
|
485 | ||
removed DBIx::Custom commit ...
|
486 |
our $VERSION = '0.1603'; |
removed reconnect method
|
487 | |
488 |
=head1 STABILITY |
|
489 | ||
removed DBIx::Custom commit ...
|
490 |
This module is not stable. Method name and implementations will be change. |
removed reconnect method
|
491 | |
492 |
=head1 SYNOPSYS |
|
493 |
|
|
494 |
# Connect |
|
495 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=books", |
|
496 |
user => 'ken', password => '!LFKD%$&'); |
|
497 |
|
|
498 |
# Disconnect |
|
499 |
$dbi->disconnect |
|
500 | ||
501 |
# Insert |
|
502 |
$dbi->insert(table => 'books', |
|
503 |
param => {title => 'perl', author => 'Ken'}, |
|
504 |
filter => {title => 'encode_utf8'}); |
|
505 |
|
|
506 |
# Update |
|
507 |
$dbi->update(table => 'books', |
|
508 |
param => {title => 'aaa', author => 'Ken'}, |
|
509 |
where => {id => 5}, |
|
510 |
filter => {title => 'encode_utf8'}); |
|
511 |
|
|
512 |
# Update all |
|
513 |
$dbi->update_all(table => 'books', |
|
514 |
param => {title => 'aaa'}, |
|
515 |
filter => {title => 'encode_utf8'}); |
|
516 |
|
|
517 |
# Delete |
|
518 |
$dbi->delete(table => 'books', |
|
519 |
where => {author => 'Ken'}, |
|
520 |
filter => {title => 'encode_utf8'}); |
|
521 |
|
|
522 |
# Delete all |
|
523 |
$dbi->delete_all(table => 'books'); |
|
524 |
|
|
525 |
# Select |
|
526 |
my $result = $dbi->select(table => 'books'); |
|
renamed fetch_rows to fetch_...
|
527 |
|
removed register_format()
|
528 |
# Select(more complex) |
renamed fetch_rows to fetch_...
|
529 |
my $result = $dbi->select( |
update document
|
530 |
table => 'books', |
531 |
column => [qw/author title/], |
|
532 |
where => {author => 'Ken'}, |
|
533 |
append => 'order by id limit 1', |
|
534 |
filter => {tilte => 'encode_utf8'} |
|
renamed fetch_rows to fetch_...
|
535 |
); |
added commit method
|
536 |
|
537 |
# Select(Join table) |
|
538 |
my $result = $dbi->select( |
|
539 |
table => ['books', 'rental'], |
|
540 |
column => ['books.name as book_name'] |
|
541 |
relation => {'books.id' => 'rental.book_id'} |
|
542 |
); |
|
removed reconnect method
|
543 |
|
removed register_format()
|
544 |
# Execute SQL |
545 |
$dbi->execute("select title from books"); |
|
546 |
|
|
547 |
# Execute SQL with parameters and filter |
|
548 |
$dbi->execute("select id from books where {= author} && {like title}", |
|
549 |
param => {author => 'ken', title => '%Perl%'}, |
|
550 |
filter => {tilte => 'encode_utf8'}); |
|
removed reconnect method
|
551 | |
552 |
# Create query and execute it |
|
553 |
my $query = $dbi->create_query( |
|
554 |
"select id from books where {= author} && {like title}" |
|
555 |
); |
|
556 |
$dbi->execute($query, param => {author => 'ken', title => '%Perl%'}) |
|
removed register_format()
|
557 |
|
558 |
# Default filter |
|
559 |
$dbi->default_query_filter('encode_utf8'); |
|
560 |
$dbi->default_fetch_filter('decode_utf8'); |
|
561 |
|
|
562 |
# Fetch |
|
563 |
while (my $row = $result->fetch) { |
|
564 |
# ... |
|
565 |
} |
|
566 |
|
|
567 |
# Fetch hash |
|
568 |
while (my $row = $result->fetch_hash) { |
|
569 |
|
|
570 |
} |
|
571 |
|
|
update document
|
572 |
# DBI instance |
573 |
my $dbh = $dbi->dbh; |
|
removed reconnect method
|
574 | |
575 |
=head1 DESCRIPTION |
|
576 | ||
577 |
L<DBIx::Custom> is useful L<DBI> extention. |
|
578 |
This module have hash parameter binding and filtering system. |
|
579 | ||
580 |
Normally, binding parameter is array. |
|
581 |
L<DBIx::Custom> enable you to pass binding parameter as hash. |
|
582 | ||
583 |
This module also provide filtering system. |
|
584 |
You can filter the binding parameter |
|
585 |
or the value of fetching row. |
|
586 | ||
587 |
And have useful method such as insert(), update(), delete(), and select(). |
|
588 | ||
589 |
=head2 Features |
|
590 | ||
591 |
=over 4 |
|
592 | ||
593 |
=item 1. Hash parameter binding. |
|
594 | ||
595 |
=item 2. Value filtering. |
|
596 | ||
597 |
=item 3. Useful methos such as insert(), update(), delete(), and select(). |
|
598 | ||
599 |
=back |
|
600 | ||
update document
|
601 |
=head1 ATTRIBUTES |
packaging one directory
|
602 | |
removed DBIx::Custom commit ...
|
603 |
=head2 C<user> |
packaging one directory
|
604 | |
update document
|
605 |
Database user name. |
bind_filter argument is chan...
|
606 |
|
version 0.0901
|
607 |
$dbi = $dbi->user('Ken'); |
608 |
$user = $dbi->user; |
|
bind_filter argument is chan...
|
609 |
|
removed DBIx::Custom commit ...
|
610 |
=head2 C<password> |
packaging one directory
|
611 | |
update document
|
612 |
Database password. |
bind_filter argument is chan...
|
613 |
|
version 0.0901
|
614 |
$dbi = $dbi->password('lkj&le`@s'); |
615 |
$password = $dbi->password; |
|
packaging one directory
|
616 | |
removed DBIx::Custom commit ...
|
617 |
=head2 C<data_source> |
packaging one directory
|
618 | |
update document
|
619 |
Database data source. |
bind_filter argument is chan...
|
620 |
|
version 0.0901
|
621 |
$dbi = $dbi->data_source("dbi:mysql:dbname=$database"); |
622 |
$data_source = $dbi->data_source; |
|
packaging one directory
|
623 |
|
removed DBIx::Custom commit ...
|
624 |
=head2 C<dbh> |
packaging one directory
|
625 | |
removed reconnect method
|
626 |
Database handle. This is the innstance of L<DBI> |
627 |
|
|
628 |
$dbi = $dbi->dbh($dbh); |
|
629 |
$dbh = $dbi->dbh; |
|
packaging one directory
|
630 | |
removed reconnect method
|
631 |
You can use all methods of L<DBI> |
packaging one directory
|
632 | |
removed reconnect method
|
633 |
my $sth = $dbi->dbh->prepare("..."); |
634 |
my $errstr = $dbi->dbh->errstr; |
|
635 |
|
|
removed DBIx::Custom commit ...
|
636 |
=head2 C<filters> |
packaging one directory
|
637 | |
update document
|
638 |
Filters |
packaging one directory
|
639 | |
update document
|
640 |
$dbi = $dbi->filters({%filters}); |
version 0.0901
|
641 |
$filters = $dbi->filters; |
642 | ||
update document
|
643 |
encode_utf8 and decode_utf8 is set to this attribute by default. |
version 0.0901
|
644 | |
update document
|
645 |
$encode_utf8 = $dbi->filters->{encode_utf8}; |
646 |
$decode_utf8 = $dbi->filters->{decode_utf8}; |
|
packaging one directory
|
647 | |
removed DBIx::Custom commit ...
|
648 |
=head2 C<default_query_filter> |
packaging one directory
|
649 | |
update document
|
650 |
Default query filter. |
packaging one directory
|
651 | |
update document
|
652 |
$dbi = $dbi->default_query_filter('encode_utf8'); |
rename bind_filter to query_...
|
653 |
$default_query_filter = $dbi->default_query_filter |
packaging one directory
|
654 | |
removed DBIx::Custom commit ...
|
655 |
=head2 C<default_fetch_filter> |
packaging one directory
|
656 | |
update document
|
657 |
Fetching filter. |
bind_filter argument is chan...
|
658 | |
update document
|
659 |
$dbi = $dbi->default_fetch_filter('decode_utf8'); |
cleanup
|
660 |
$default_fetch_filter = $dbi->default_fetch_filter; |
bind_filter argument is chan...
|
661 | |
removed DBIx::Custom commit ...
|
662 |
=head2 C<result_class> |
packaging one directory
|
663 | |
update document
|
664 |
Result class. |
bind_filter argument is chan...
|
665 | |
version 0.0901
|
666 |
$dbi = $dbi->result_class('DBIx::Custom::Result'); |
packaging one directory
|
667 |
$result_class = $dbi->result_class; |
668 | ||
update document
|
669 |
L<DBIx::Custom::Result> is set to this attribute by default. |
update document
|
670 | |
removed DBIx::Custom commit ...
|
671 |
=head2 C<sql_template> |
added commit method
|
672 | |
removed reconnect method
|
673 |
SQLTemplate instance. sql_template attribute must be |
674 |
the instance of L<DBIx::Cutom::SQLTemplate> subclass. |
|
added commit method
|
675 | |
removed reconnect method
|
676 |
$dbi = $dbi->sql_template(DBIx::Cutom::SQLTemplate->new); |
677 |
$sql_template = $dbi->sql_template; |
|
added commit method
|
678 | |
removed reconnect method
|
679 |
the instance of DBIx::Cutom::SQLTemplate is set to |
680 |
this attribute by default. |
|
added commit method
|
681 | |
removed reconnect method
|
682 |
=head1 METHODS |
added commit method
|
683 | |
removed reconnect method
|
684 |
This class is L<Object::Simple> subclass. |
685 |
You can use all methods of L<Object::Simple> |
|
added commit method
|
686 | |
removed DBIx::Custom commit ...
|
687 |
=head2 C<connect> |
packaging one directory
|
688 | |
update document
|
689 |
Connect to database. |
690 |
|
|
691 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=books", |
|
692 |
user => 'ken', password => '!LFKD%$&'); |
|
bind_filter argument is chan...
|
693 | |
update document
|
694 |
"AutoCommit" and "RaiseError" option is true, |
695 |
and "PrintError" option is false by dfault. |
|
packaging one directory
|
696 | |
removed DBIx::Custom commit ...
|
697 |
=head2 C<disconnect> |
packaging one directory
|
698 | |
update document
|
699 |
Disconnect database. |
bind_filter argument is chan...
|
700 | |
packaging one directory
|
701 |
$dbi->disconnect; |
702 | ||
version 0.0901
|
703 |
If database is already disconnected, this method do nothing. |
packaging one directory
|
704 | |
removed DBIx::Custom commit ...
|
705 |
=head2 C<insert> |
packaging one directory
|
706 | |
added commit method
|
707 |
Insert row. |
bind_filter argument is chan...
|
708 | |
removed register_format()
|
709 |
$affected = $dbi->insert(table => $table, |
710 |
param => {%param}, |
|
711 |
append => $append, |
|
712 |
filter => {%filter}); |
|
update document
|
713 | |
added commit method
|
714 |
Retruned value is affected rows count. |
packaging one directory
|
715 |
|
removed reconnect method
|
716 |
Example: |
version 0.0901
|
717 | |
removed register_format()
|
718 |
# insert |
719 |
$dbi->insert(table => 'books', |
|
720 |
param => {title => 'Perl', author => 'Taro'}, |
|
721 |
append => "some statement", |
|
722 |
filter => {title => 'encode_utf8'}) |
|
version 0.0901
|
723 | |
removed DBIx::Custom commit ...
|
724 |
=head2 C<update> |
packaging one directory
|
725 | |
added commit method
|
726 |
Update rows. |
update document
|
727 | |
removed register_format()
|
728 |
$affected = $dbi->update(table => $table, |
729 |
param => {%params}, |
|
730 |
where => {%where}, |
|
731 |
append => $append, |
|
732 |
filter => {%filter}) |
|
version 0.0901
|
733 | |
added commit method
|
734 |
Retruned value is affected rows count |
update document
|
735 | |
removed reconnect method
|
736 |
Example: |
bind_filter argument is chan...
|
737 | |
removed register_format()
|
738 |
#update |
739 |
$dbi->update(table => 'books', |
|
740 |
param => {title => 'Perl', author => 'Taro'}, |
|
741 |
where => {id => 5}, |
|
742 |
append => "some statement", |
|
added commit method
|
743 |
filter => {title => 'encode_utf8'}); |
version 0.0901
|
744 | |
removed DBIx::Custom commit ...
|
745 |
=head2 C<update_all> |
packaging one directory
|
746 | |
added commit method
|
747 |
Update all rows. |
bind_filter argument is chan...
|
748 | |
removed register_format()
|
749 |
$affected = $dbi->update_all(table => $table, |
750 |
param => {%params}, |
|
751 |
filter => {%filter}, |
|
752 |
append => $append); |
|
update document
|
753 | |
added commit method
|
754 |
Retruned value is affected rows count. |
version 0.0901
|
755 | |
removed reconnect method
|
756 |
Example: |
update document
|
757 | |
removed register_format()
|
758 |
# update_all |
759 |
$dbi->update_all(table => 'books', |
|
760 |
param => {author => 'taro'}, |
|
761 |
filter => {author => 'encode_utf8'}); |
|
packaging one directory
|
762 | |
removed DBIx::Custom commit ...
|
763 |
=head2 C<delete> |
packaging one directory
|
764 | |
added commit method
|
765 |
Delete rows. |
update document
|
766 | |
removed register_format()
|
767 |
$affected = $dbi->delete(table => $table, |
768 |
where => {%where}, |
|
added commit method
|
769 |
append => $append, |
removed register_format()
|
770 |
filter => {%filter}); |
bind_filter argument is chan...
|
771 | |
version 0.0901
|
772 |
Retrun value is affected rows count |
packaging one directory
|
773 |
|
removed reconnect method
|
774 |
Example: |
packaging one directory
|
775 | |
removed register_format()
|
776 |
# delete |
777 |
$dbi->delete(table => 'books', |
|
778 |
where => {id => 5}, |
|
779 |
append => 'some statement', |
|
removed reconnect method
|
780 |
filter => {id => 'encode_utf8'}); |
version 0.0901
|
781 | |
removed DBIx::Custom commit ...
|
782 |
=head2 C<delete_all> |
packaging one directory
|
783 | |
added commit method
|
784 |
Delete all rows. |
bind_filter argument is chan...
|
785 | |
removed register_format()
|
786 |
$affected = $dbi->delete_all(table => $table); |
packaging one directory
|
787 | |
added commit method
|
788 |
Retruned value is affected rows count. |
bind_filter argument is chan...
|
789 | |
removed reconnect method
|
790 |
Example: |
removed register_format()
|
791 |
|
792 |
# delete_all |
|
removed reconnect method
|
793 |
$dbi->delete_all(table => 'books'); |
packaging one directory
|
794 | |
removed DBIx::Custom commit ...
|
795 |
=head2 C<select> |
packaging one directory
|
796 |
|
added commit method
|
797 |
Select rows. |
bind_filter argument is chan...
|
798 | |
added commit method
|
799 |
$result = $dbi->select(table => $table, |
800 |
column => [@column], |
|
801 |
where => {%where}, |
|
802 |
append => $append, |
|
removed reconnect method
|
803 |
relation => {%relation}, |
added commit method
|
804 |
filter => {%filter}); |
update document
|
805 | |
removed reconnect method
|
806 |
Return value is the instance of L<DBIx::Custom::Result>. |
update document
|
807 | |
removed reconnect method
|
808 |
Example: |
update document
|
809 | |
added commit method
|
810 |
# select * from books; |
removed reconnect method
|
811 |
$result = $dbi->select(table => 'books'); |
packaging one directory
|
812 |
|
update document
|
813 |
# select * from books where title = 'Perl'; |
removed reconnect method
|
814 |
$result = $dbi->select(table => 'books', where => {title => 1}); |
update document
|
815 |
|
816 |
# select title, author from books where id = 1 for update; |
|
817 |
$result = $dbi->select( |
|
removed register_format()
|
818 |
table => 'books', |
removed reconnect method
|
819 |
column => ['title', 'author'], |
removed register_format()
|
820 |
where => {id => 1}, |
821 |
appned => 'for update' |
|
update document
|
822 |
); |
823 |
|
|
added commit method
|
824 |
# select books.name as book_name from books, rental |
825 |
# where books.id = rental.book_id; |
|
826 |
my $result = $dbi->select( |
|
removed reconnect method
|
827 |
table => ['books', 'rental'], |
828 |
column => ['books.name as book_name'] |
|
added commit method
|
829 |
relation => {'books.id' => 'rental.book_id'} |
update document
|
830 |
); |
831 | ||
removed DBIx::Custom commit ...
|
832 |
=head2 C<create_query> |
removed reconnect method
|
833 |
|
removed DBIx::Custom commit ...
|
834 |
my $query = $dbi->create_query( |
835 |
"select * from authors where {= name} and {= age};" |
|
836 |
); |
|
837 | ||
removed reconnect method
|
838 |
Create the instance of L<DBIx::Custom::Query>. |
839 |
This receive the string written by SQL template. |
|
packaging one directory
|
840 | |
removed DBIx::Custom commit ...
|
841 |
=head2 C<execute> |
removed reconnect method
|
842 | |
removed DBIx::Custom commit ...
|
843 |
$result = $dbi->execute($query, param => $params, filter => {%filter}); |
844 |
$result = $dbi->execute($template, param => $params, filter => {%filter}); |
|
removed reconnect method
|
845 | |
846 |
Execute the instace of L<DBIx::Custom::Query> or |
|
847 |
the string written by SQL template. |
|
848 |
Return value is the instance of L<DBIx::Custom::Result>. |
|
849 | ||
removed DBIx::Custom commit ...
|
850 |
B<Example:> |
removed reconnect method
|
851 | |
852 |
$result = $dbi->execute("select * from authors where {= name} and {= age}", |
|
853 |
param => {name => 'taro', age => 19}); |
|
854 |
|
|
855 |
while (my $row = $result->fetch) { |
|
856 |
# do something |
|
857 |
} |
|
858 | ||
859 |
See also L<DBIx::Custom::SQLTemplate> to know how to write SQL template. |
|
860 | ||
removed DBIx::Custom commit ...
|
861 |
=head2 C<register_filter> |
removed reconnect method
|
862 | |
863 |
$dbi->register_filter(%filters); |
|
864 |
|
|
removed DBIx::Custom commit ...
|
865 |
Resister filter. |
866 | ||
867 |
B<Example:> |
|
packaging one directory
|
868 | |
removed reconnect method
|
869 |
$dbi->register_filter( |
870 |
encode_utf8 => sub { |
|
871 |
my $value = shift; |
|
872 |
|
|
873 |
require Encode; |
|
874 |
|
|
875 |
return Encode::encode('UTF-8', $value); |
|
876 |
}, |
|
877 |
decode_utf8 => sub { |
|
878 |
my $value = shift; |
|
879 |
|
|
880 |
require Encode; |
|
881 |
|
|
882 |
return Encode::decode('UTF-8', $value) |
|
883 |
} |
|
884 |
); |
|
885 | ||
removed DBIx::Custom commit ...
|
886 |
=head2 C<cache> |
added cache_method attribute
|
887 | |
888 |
$dbi = $dbi->cache(1); |
|
889 |
$cache = $dbi->cache; |
|
890 | ||
removed DBIx::Custom commit ...
|
891 |
Cache the result of parsing SQL template. |
added cache_method attribute
|
892 |
Default to 1. |
893 | ||
removed DBIx::Custom commit ...
|
894 |
=head2 C<cache_method> |
895 | ||
896 |
$dbi = $dbi->cache_method(\&cache_method); |
|
897 |
$cache_method = $dbi->cache_method |
|
added cache_method attribute
|
898 | |
899 |
Method for cache. |
|
900 | ||
removed DBIx::Custom commit ...
|
901 |
B<Example:> |
added cache_method attribute
|
902 | |
903 |
$dbi->cache_method( |
|
904 |
sub { |
|
905 |
my $self = shift; |
|
906 |
|
|
907 |
$self->{_cached} ||= {}; |
|
908 |
|
|
909 |
if (@_ > 1) { |
|
910 |
$self->{_cached}{$_[0]} = $_[1] |
|
911 |
} |
|
912 |
else { |
|
913 |
return $self->{_cached}{$_[0]} |
|
914 |
} |
|
915 |
} |
|
916 |
); |
|
917 | ||
removed reconnect method
|
918 |
=head1 AUTHOR |
919 | ||
920 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
|
version 0.0901
|
921 | |
packaging one directory
|
922 |
=head1 COPYRIGHT & LICENSE |
923 | ||
924 |
Copyright 2009 Yuki Kimoto, all rights reserved. |
|
925 | ||
926 |
This program is free software; you can redistribute it and/or modify it |
|
927 |
under the same terms as Perl itself. |
|
928 | ||
929 |
=cut |
|
added cache_method attribute
|
930 | |
931 |