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 auto_commit { |
|
474 |
my $self = shift; |
|
475 |
|
|
select, insert, update, upda...
|
476 |
# Not connected |
477 |
croak "Not connected" unless $self->dbh; |
|
478 |
|
|
removed reconnect method
|
479 |
if (@_) { |
480 |
|
|
481 |
# Set AutoCommit |
|
482 |
$self->dbh->{AutoCommit} = $_[0]; |
|
483 |
|
|
484 |
return $self; |
|
485 |
} |
|
486 |
return $self->dbh->{AutoCommit}; |
|
487 |
} |
|
488 | ||
select, insert, update, upda...
|
489 |
sub commit { |
490 |
my $ret = eval { shift->dbh->commit }; |
|
491 |
croak $@ if $@; |
|
492 |
return $ret; |
|
493 |
} |
|
494 |
sub rollback { |
|
495 |
my $ret = eval { shift->dbh->rollback }; |
|
496 |
croak $@ if $@; |
|
497 |
return $ret; |
|
498 |
} |
|
removed reconnect method
|
499 | |
500 |
sub DESTROY { |
|
501 |
my $self = shift; |
|
502 |
|
|
503 |
# Disconnect |
|
504 |
$self->disconnect if $self->dbh; |
|
505 |
} |
|
506 | ||
507 |
=head1 NAME |
|
508 | ||
509 |
DBIx::Custom - DBI with hash parameter binding and filtering system |
|
510 | ||
511 |
=head1 VERSION |
|
512 | ||
added cache_method attribute
|
513 |
Version 0.1602 |
removed reconnect method
|
514 | |
515 |
=cut |
|
516 | ||
added cache_method attribute
|
517 |
our $VERSION = '0.1602'; |
removed reconnect method
|
518 |
$VERSION = eval $VERSION; |
519 | ||
520 |
=head1 STABILITY |
|
521 | ||
522 |
This module is not stable. Method name and functionality will be change. |
|
523 | ||
524 |
=head1 SYNOPSYS |
|
525 |
|
|
526 |
# Connect |
|
527 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=books", |
|
528 |
user => 'ken', password => '!LFKD%$&'); |
|
529 |
|
|
530 |
# Disconnect |
|
531 |
$dbi->disconnect |
|
532 | ||
533 |
# Insert |
|
534 |
$dbi->insert(table => 'books', |
|
535 |
param => {title => 'perl', author => 'Ken'}, |
|
536 |
filter => {title => 'encode_utf8'}); |
|
537 |
|
|
538 |
# Update |
|
539 |
$dbi->update(table => 'books', |
|
540 |
param => {title => 'aaa', author => 'Ken'}, |
|
541 |
where => {id => 5}, |
|
542 |
filter => {title => 'encode_utf8'}); |
|
543 |
|
|
544 |
# Update all |
|
545 |
$dbi->update_all(table => 'books', |
|
546 |
param => {title => 'aaa'}, |
|
547 |
filter => {title => 'encode_utf8'}); |
|
548 |
|
|
549 |
# Delete |
|
550 |
$dbi->delete(table => 'books', |
|
551 |
where => {author => 'Ken'}, |
|
552 |
filter => {title => 'encode_utf8'}); |
|
553 |
|
|
554 |
# Delete all |
|
555 |
$dbi->delete_all(table => 'books'); |
|
556 |
|
|
557 |
# Select |
|
558 |
my $result = $dbi->select(table => 'books'); |
|
renamed fetch_rows to fetch_...
|
559 |
|
removed register_format()
|
560 |
# Select(more complex) |
renamed fetch_rows to fetch_...
|
561 |
my $result = $dbi->select( |
update document
|
562 |
table => 'books', |
563 |
column => [qw/author title/], |
|
564 |
where => {author => 'Ken'}, |
|
565 |
append => 'order by id limit 1', |
|
566 |
filter => {tilte => 'encode_utf8'} |
|
renamed fetch_rows to fetch_...
|
567 |
); |
added commit method
|
568 |
|
569 |
# Select(Join table) |
|
570 |
my $result = $dbi->select( |
|
571 |
table => ['books', 'rental'], |
|
572 |
column => ['books.name as book_name'] |
|
573 |
relation => {'books.id' => 'rental.book_id'} |
|
574 |
); |
|
removed reconnect method
|
575 |
|
removed register_format()
|
576 |
# Execute SQL |
577 |
$dbi->execute("select title from books"); |
|
578 |
|
|
579 |
# Execute SQL with parameters and filter |
|
580 |
$dbi->execute("select id from books where {= author} && {like title}", |
|
581 |
param => {author => 'ken', title => '%Perl%'}, |
|
582 |
filter => {tilte => 'encode_utf8'}); |
|
removed reconnect method
|
583 | |
584 |
# Create query and execute it |
|
585 |
my $query = $dbi->create_query( |
|
586 |
"select id from books where {= author} && {like title}" |
|
587 |
); |
|
588 |
$dbi->execute($query, param => {author => 'ken', title => '%Perl%'}) |
|
removed register_format()
|
589 |
|
590 |
# Default filter |
|
591 |
$dbi->default_query_filter('encode_utf8'); |
|
592 |
$dbi->default_fetch_filter('decode_utf8'); |
|
593 |
|
|
594 |
# Fetch |
|
595 |
while (my $row = $result->fetch) { |
|
596 |
# ... |
|
597 |
} |
|
598 |
|
|
599 |
# Fetch hash |
|
600 |
while (my $row = $result->fetch_hash) { |
|
601 |
|
|
602 |
} |
|
603 |
|
|
update document
|
604 |
# DBI instance |
605 |
my $dbh = $dbi->dbh; |
|
removed reconnect method
|
606 | |
607 |
=head1 DESCRIPTION |
|
608 | ||
609 |
L<DBIx::Custom> is useful L<DBI> extention. |
|
610 |
This module have hash parameter binding and filtering system. |
|
611 | ||
612 |
Normally, binding parameter is array. |
|
613 |
L<DBIx::Custom> enable you to pass binding parameter as hash. |
|
614 | ||
615 |
This module also provide filtering system. |
|
616 |
You can filter the binding parameter |
|
617 |
or the value of fetching row. |
|
618 | ||
619 |
And have useful method such as insert(), update(), delete(), and select(). |
|
620 | ||
621 |
=head2 Features |
|
622 | ||
623 |
=over 4 |
|
624 | ||
625 |
=item 1. Hash parameter binding. |
|
626 | ||
627 |
=item 2. Value filtering. |
|
628 | ||
629 |
=item 3. Useful methos such as insert(), update(), delete(), and select(). |
|
630 | ||
631 |
=back |
|
632 | ||
update document
|
633 |
=head1 ATTRIBUTES |
packaging one directory
|
634 | |
635 |
=head2 user |
|
636 | ||
update document
|
637 |
Database user name. |
bind_filter argument is chan...
|
638 |
|
version 0.0901
|
639 |
$dbi = $dbi->user('Ken'); |
640 |
$user = $dbi->user; |
|
bind_filter argument is chan...
|
641 |
|
packaging one directory
|
642 |
=head2 password |
643 | ||
update document
|
644 |
Database password. |
bind_filter argument is chan...
|
645 |
|
version 0.0901
|
646 |
$dbi = $dbi->password('lkj&le`@s'); |
647 |
$password = $dbi->password; |
|
packaging one directory
|
648 | |
649 |
=head2 data_source |
|
650 | ||
update document
|
651 |
Database data source. |
bind_filter argument is chan...
|
652 |
|
version 0.0901
|
653 |
$dbi = $dbi->data_source("dbi:mysql:dbname=$database"); |
654 |
$data_source = $dbi->data_source; |
|
packaging one directory
|
655 |
|
removed reconnect method
|
656 |
=head2 dbh |
packaging one directory
|
657 | |
removed reconnect method
|
658 |
Database handle. This is the innstance of L<DBI> |
659 |
|
|
660 |
$dbi = $dbi->dbh($dbh); |
|
661 |
$dbh = $dbi->dbh; |
|
packaging one directory
|
662 | |
removed reconnect method
|
663 |
You can use all methods of L<DBI> |
packaging one directory
|
664 | |
removed reconnect method
|
665 |
my $sth = $dbi->dbh->prepare("..."); |
666 |
my $errstr = $dbi->dbh->errstr; |
|
667 |
|
|
bind_filter argument is chan...
|
668 |
=head2 filters |
packaging one directory
|
669 | |
update document
|
670 |
Filters |
packaging one directory
|
671 | |
update document
|
672 |
$dbi = $dbi->filters({%filters}); |
version 0.0901
|
673 |
$filters = $dbi->filters; |
674 | ||
update document
|
675 |
encode_utf8 and decode_utf8 is set to this attribute by default. |
version 0.0901
|
676 | |
update document
|
677 |
$encode_utf8 = $dbi->filters->{encode_utf8}; |
678 |
$decode_utf8 = $dbi->filters->{decode_utf8}; |
|
packaging one directory
|
679 | |
rename bind_filter to query_...
|
680 |
=head2 default_query_filter |
packaging one directory
|
681 | |
update document
|
682 |
Default query filter. |
packaging one directory
|
683 | |
update document
|
684 |
$dbi = $dbi->default_query_filter('encode_utf8'); |
rename bind_filter to query_...
|
685 |
$default_query_filter = $dbi->default_query_filter |
packaging one directory
|
686 | |
cleanup
|
687 |
=head2 default_fetch_filter |
packaging one directory
|
688 | |
update document
|
689 |
Fetching filter. |
bind_filter argument is chan...
|
690 | |
update document
|
691 |
$dbi = $dbi->default_fetch_filter('decode_utf8'); |
cleanup
|
692 |
$default_fetch_filter = $dbi->default_fetch_filter; |
bind_filter argument is chan...
|
693 | |
packaging one directory
|
694 |
=head2 result_class |
695 | ||
update document
|
696 |
Result class. |
bind_filter argument is chan...
|
697 | |
version 0.0901
|
698 |
$dbi = $dbi->result_class('DBIx::Custom::Result'); |
packaging one directory
|
699 |
$result_class = $dbi->result_class; |
700 | ||
update document
|
701 |
L<DBIx::Custom::Result> is set to this attribute by default. |
update document
|
702 | |
removed reconnect method
|
703 |
=head2 sql_template |
added commit method
|
704 | |
removed reconnect method
|
705 |
SQLTemplate instance. sql_template attribute must be |
706 |
the instance of L<DBIx::Cutom::SQLTemplate> subclass. |
|
added commit method
|
707 | |
removed reconnect method
|
708 |
$dbi = $dbi->sql_template(DBIx::Cutom::SQLTemplate->new); |
709 |
$sql_template = $dbi->sql_template; |
|
added commit method
|
710 | |
removed reconnect method
|
711 |
the instance of DBIx::Cutom::SQLTemplate is set to |
712 |
this attribute by default. |
|
added commit method
|
713 | |
removed reconnect method
|
714 |
=head1 METHODS |
added commit method
|
715 | |
removed reconnect method
|
716 |
This class is L<Object::Simple> subclass. |
717 |
You can use all methods of L<Object::Simple> |
|
added commit method
|
718 | |
packaging one directory
|
719 |
=head2 connect |
720 | ||
update document
|
721 |
Connect to database. |
722 |
|
|
723 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=books", |
|
724 |
user => 'ken', password => '!LFKD%$&'); |
|
bind_filter argument is chan...
|
725 | |
update document
|
726 |
"AutoCommit" and "RaiseError" option is true, |
727 |
and "PrintError" option is false by dfault. |
|
packaging one directory
|
728 | |
729 |
=head2 disconnect |
|
730 | ||
update document
|
731 |
Disconnect database. |
bind_filter argument is chan...
|
732 | |
packaging one directory
|
733 |
$dbi->disconnect; |
734 | ||
version 0.0901
|
735 |
If database is already disconnected, this method do nothing. |
packaging one directory
|
736 | |
737 |
=head2 insert |
|
738 | ||
added commit method
|
739 |
Insert row. |
bind_filter argument is chan...
|
740 | |
removed register_format()
|
741 |
$affected = $dbi->insert(table => $table, |
742 |
param => {%param}, |
|
743 |
append => $append, |
|
744 |
filter => {%filter}); |
|
update document
|
745 | |
added commit method
|
746 |
Retruned value is affected rows count. |
packaging one directory
|
747 |
|
removed reconnect method
|
748 |
Example: |
version 0.0901
|
749 | |
removed register_format()
|
750 |
# insert |
751 |
$dbi->insert(table => 'books', |
|
752 |
param => {title => 'Perl', author => 'Taro'}, |
|
753 |
append => "some statement", |
|
754 |
filter => {title => 'encode_utf8'}) |
|
version 0.0901
|
755 | |
packaging one directory
|
756 |
=head2 update |
757 | ||
added commit method
|
758 |
Update rows. |
update document
|
759 | |
removed register_format()
|
760 |
$affected = $dbi->update(table => $table, |
761 |
param => {%params}, |
|
762 |
where => {%where}, |
|
763 |
append => $append, |
|
764 |
filter => {%filter}) |
|
version 0.0901
|
765 | |
added commit method
|
766 |
Retruned value is affected rows count |
update document
|
767 | |
removed reconnect method
|
768 |
Example: |
bind_filter argument is chan...
|
769 | |
removed register_format()
|
770 |
#update |
771 |
$dbi->update(table => 'books', |
|
772 |
param => {title => 'Perl', author => 'Taro'}, |
|
773 |
where => {id => 5}, |
|
774 |
append => "some statement", |
|
added commit method
|
775 |
filter => {title => 'encode_utf8'}); |
version 0.0901
|
776 | |
packaging one directory
|
777 |
=head2 update_all |
778 | ||
added commit method
|
779 |
Update all rows. |
bind_filter argument is chan...
|
780 | |
removed register_format()
|
781 |
$affected = $dbi->update_all(table => $table, |
782 |
param => {%params}, |
|
783 |
filter => {%filter}, |
|
784 |
append => $append); |
|
update document
|
785 | |
added commit method
|
786 |
Retruned value is affected rows count. |
version 0.0901
|
787 | |
removed reconnect method
|
788 |
Example: |
update document
|
789 | |
removed register_format()
|
790 |
# update_all |
791 |
$dbi->update_all(table => 'books', |
|
792 |
param => {author => 'taro'}, |
|
793 |
filter => {author => 'encode_utf8'}); |
|
packaging one directory
|
794 | |
795 |
=head2 delete |
|
796 | ||
added commit method
|
797 |
Delete rows. |
update document
|
798 | |
removed register_format()
|
799 |
$affected = $dbi->delete(table => $table, |
800 |
where => {%where}, |
|
added commit method
|
801 |
append => $append, |
removed register_format()
|
802 |
filter => {%filter}); |
bind_filter argument is chan...
|
803 | |
version 0.0901
|
804 |
Retrun value is affected rows count |
packaging one directory
|
805 |
|
removed reconnect method
|
806 |
Example: |
packaging one directory
|
807 | |
removed register_format()
|
808 |
# delete |
809 |
$dbi->delete(table => 'books', |
|
810 |
where => {id => 5}, |
|
811 |
append => 'some statement', |
|
removed reconnect method
|
812 |
filter => {id => 'encode_utf8'}); |
version 0.0901
|
813 | |
packaging one directory
|
814 |
=head2 delete_all |
815 | ||
added commit method
|
816 |
Delete all rows. |
bind_filter argument is chan...
|
817 | |
removed register_format()
|
818 |
$affected = $dbi->delete_all(table => $table); |
packaging one directory
|
819 | |
added commit method
|
820 |
Retruned value is affected rows count. |
bind_filter argument is chan...
|
821 | |
removed reconnect method
|
822 |
Example: |
removed register_format()
|
823 |
|
824 |
# delete_all |
|
removed reconnect method
|
825 |
$dbi->delete_all(table => 'books'); |
packaging one directory
|
826 | |
827 |
=head2 select |
|
828 |
|
|
added commit method
|
829 |
Select rows. |
bind_filter argument is chan...
|
830 | |
added commit method
|
831 |
$result = $dbi->select(table => $table, |
832 |
column => [@column], |
|
833 |
where => {%where}, |
|
834 |
append => $append, |
|
removed reconnect method
|
835 |
relation => {%relation}, |
added commit method
|
836 |
filter => {%filter}); |
update document
|
837 | |
removed reconnect method
|
838 |
Return value is the instance of L<DBIx::Custom::Result>. |
update document
|
839 | |
removed reconnect method
|
840 |
Example: |
update document
|
841 | |
added commit method
|
842 |
# select * from books; |
removed reconnect method
|
843 |
$result = $dbi->select(table => 'books'); |
packaging one directory
|
844 |
|
update document
|
845 |
# select * from books where title = 'Perl'; |
removed reconnect method
|
846 |
$result = $dbi->select(table => 'books', where => {title => 1}); |
update document
|
847 |
|
848 |
# select title, author from books where id = 1 for update; |
|
849 |
$result = $dbi->select( |
|
removed register_format()
|
850 |
table => 'books', |
removed reconnect method
|
851 |
column => ['title', 'author'], |
removed register_format()
|
852 |
where => {id => 1}, |
853 |
appned => 'for update' |
|
update document
|
854 |
); |
855 |
|
|
added commit method
|
856 |
# select books.name as book_name from books, rental |
857 |
# where books.id = rental.book_id; |
|
858 |
my $result = $dbi->select( |
|
removed reconnect method
|
859 |
table => ['books', 'rental'], |
860 |
column => ['books.name as book_name'] |
|
added commit method
|
861 |
relation => {'books.id' => 'rental.book_id'} |
update document
|
862 |
); |
863 | ||
removed reconnect method
|
864 |
=head2 create_query |
865 |
|
|
866 |
Create the instance of L<DBIx::Custom::Query>. |
|
867 |
This receive the string written by SQL template. |
|
packaging one directory
|
868 | |
removed reconnect method
|
869 |
my $query = $dbi->create_query("select * from authors where {= name} and {= age}"); |
870 | ||
871 |
=head2 execute |
|
872 | ||
873 |
Execute the instace of L<DBIx::Custom::Query> or |
|
874 |
the string written by SQL template. |
|
875 |
Return value is the instance of L<DBIx::Custom::Result>. |
|
876 | ||
877 |
$result = $dbi->execute($query, param => $params, filter => {%filter}); |
|
878 |
$result = $dbi->execute($template, param => $params, filter => {%filter}); |
|
879 | ||
880 |
Example: |
|
881 | ||
882 |
$result = $dbi->execute("select * from authors where {= name} and {= age}", |
|
883 |
param => {name => 'taro', age => 19}); |
|
884 |
|
|
885 |
while (my $row = $result->fetch) { |
|
886 |
# do something |
|
887 |
} |
|
888 | ||
889 |
See also L<DBIx::Custom::SQLTemplate> to know how to write SQL template. |
|
890 | ||
891 |
=head2 register_filter |
|
892 | ||
893 |
Resister filter. |
|
894 |
|
|
895 |
$dbi->register_filter(%filters); |
|
896 |
|
|
897 |
Example: |
|
packaging one directory
|
898 | |
removed reconnect method
|
899 |
$dbi->register_filter( |
900 |
encode_utf8 => sub { |
|
901 |
my $value = shift; |
|
902 |
|
|
903 |
require Encode; |
|
904 |
|
|
905 |
return Encode::encode('UTF-8', $value); |
|
906 |
}, |
|
907 |
decode_utf8 => sub { |
|
908 |
my $value = shift; |
|
909 |
|
|
910 |
require Encode; |
|
911 |
|
|
912 |
return Encode::decode('UTF-8', $value) |
|
913 |
} |
|
914 |
); |
|
915 | ||
916 |
=head2 auto_commit |
|
917 | ||
918 |
Auto commit. |
|
packaging one directory
|
919 | |
removed reconnect method
|
920 |
$self = $dbi->auto_commit(1); |
921 |
$auto_commit = $dbi->auto_commit; |
|
922 | ||
923 |
This is equal to |
|
924 | ||
925 |
$dbi->dbh->{AutoCommit} = 1; |
|
926 |
$auto_commit = $dbi->dbh->{AutoCommit}; |
|
927 | ||
928 |
=head2 commit |
|
929 | ||
930 |
Commit. |
|
931 | ||
932 |
$dbi->commit; |
|
933 | ||
934 |
This is equal to |
|
935 | ||
936 |
$dbi->dbh->commit; |
|
937 | ||
938 |
=head2 rollback |
|
939 | ||
940 |
Rollback. |
|
941 | ||
942 |
$dbi->rollback |
|
943 | ||
944 |
This is equal to |
|
945 | ||
946 |
$dbi->dbh->rollback; |
|
947 | ||
added cache_method attribute
|
948 |
=head2 cache |
949 | ||
950 |
Cache the result of parsing SQL template. |
|
951 | ||
952 |
$dbi = $dbi->cache(1); |
|
953 |
$cache = $dbi->cache; |
|
954 | ||
955 |
Default to 1. |
|
956 | ||
957 |
=head2 cache_method |
|
958 | ||
959 |
Method for cache. |
|
960 | ||
961 |
$dbi = $dbi->cache_method(sub { ... }); |
|
962 |
$cache_method = $dbi->cache_method |
|
963 | ||
964 |
Example: |
|
965 |
|
|
966 |
$dbi->cache_method( |
|
967 |
sub { |
|
968 |
my $self = shift; |
|
969 |
|
|
970 |
$self->{_cached} ||= {}; |
|
971 |
|
|
972 |
if (@_ > 1) { |
|
973 |
$self->{_cached}{$_[0]} = $_[1] |
|
974 |
} |
|
975 |
else { |
|
976 |
return $self->{_cached}{$_[0]} |
|
977 |
} |
|
978 |
} |
|
979 |
); |
|
980 | ||
removed reconnect method
|
981 |
=head1 AUTHOR |
982 | ||
983 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
|
version 0.0901
|
984 | |
packaging one directory
|
985 |
=head1 COPYRIGHT & LICENSE |
986 | ||
987 |
Copyright 2009 Yuki Kimoto, all rights reserved. |
|
988 | ||
989 |
This program is free software; you can redistribute it and/or modify it |
|
990 |
under the same terms as Perl itself. |
|
991 | ||
992 |
=cut |
|
added cache_method attribute
|
993 | |
994 |