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 | ||
removed DESTROY method(not b...
|
77 |
sub register_filter { |
78 |
my $invocant = shift; |
|
update document
|
79 |
|
removed DESTROY method(not b...
|
80 |
# Add filter |
81 |
my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
82 |
$invocant->filters({%{$invocant->filters}, %$filters}); |
|
update document
|
83 |
|
removed DESTROY method(not b...
|
84 |
return $invocant; |
packaging one directory
|
85 |
} |
86 | ||
removed register_format()
|
87 |
our %VALID_INSERT_ARGS = map { $_ => 1 } qw/table param append filter/; |
cleanup insert
|
88 | |
packaging one directory
|
89 |
sub insert { |
select, insert, update, upda...
|
90 |
my ($self, %args) = @_; |
removed register_format()
|
91 | |
cleanup insert
|
92 |
# Check arguments |
select, insert, update, upda...
|
93 |
foreach my $name (keys %args) { |
cleanup insert
|
94 |
croak "\"$name\" is invalid name" |
95 |
unless $VALID_INSERT_ARGS{$name}; |
|
96 |
} |
|
97 |
|
|
removed register_format()
|
98 |
# Arguments |
select, insert, update, upda...
|
99 |
my $table = $args{table} || ''; |
100 |
my $param = $args{param} || {}; |
|
101 |
my $append = $args{append} || ''; |
|
102 |
my $filter = $args{filter}; |
|
packaging one directory
|
103 |
|
104 |
# Insert keys |
|
removed register_format()
|
105 |
my @insert_keys = keys %$param; |
packaging one directory
|
106 |
|
107 |
# Not exists insert keys |
|
108 |
croak("Key-value pairs for insert must be specified to 'insert' second argument") |
|
109 |
unless @insert_keys; |
|
110 |
|
|
111 |
# Templte for insert |
|
removed DESTROY method(not b...
|
112 |
my $template = "insert into $table {insert " |
113 |
. 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; |
|
removed DESTROY method(not b...
|
447 |
croak "Not exists filter \"$fname\"" |
448 |
unless exists $filters->{$fname}; |
|
removed reconnect method
|
449 |
$filter_func = $filters->{$fname}; |
450 |
} |
|
451 |
} |
|
452 |
|
|
453 |
push @bind_values, $filter_func |
|
454 |
? $filter_func->($value) |
|
455 |
: $value; |
|
456 |
|
|
457 |
# Count up |
|
458 |
$count->{$column}++; |
|
459 |
} |
|
460 |
|
|
461 |
return \@bind_values; |
|
462 |
} |
|
463 | ||
464 |
=head1 NAME |
|
465 | ||
466 |
DBIx::Custom - DBI with hash parameter binding and filtering system |
|
467 | ||
468 |
=cut |
|
469 | ||
removed DESTROY method(not b...
|
470 |
our $VERSION = '0.1604'; |
removed reconnect method
|
471 | |
472 |
=head1 STABILITY |
|
473 | ||
removed DESTROY method(not b...
|
474 |
This module is not stable. Method name and implementations will be changed. |
removed reconnect method
|
475 | |
476 |
=head1 SYNOPSYS |
|
477 |
|
|
478 |
# Connect |
|
479 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=books", |
|
480 |
user => 'ken', password => '!LFKD%$&'); |
|
481 |
# Insert |
|
482 |
$dbi->insert(table => 'books', |
|
483 |
param => {title => 'perl', author => 'Ken'}, |
|
484 |
filter => {title => 'encode_utf8'}); |
|
485 |
|
|
486 |
# Update |
|
487 |
$dbi->update(table => 'books', |
|
488 |
param => {title => 'aaa', author => 'Ken'}, |
|
489 |
where => {id => 5}, |
|
490 |
filter => {title => 'encode_utf8'}); |
|
491 |
|
|
492 |
# Update all |
|
493 |
$dbi->update_all(table => 'books', |
|
494 |
param => {title => 'aaa'}, |
|
495 |
filter => {title => 'encode_utf8'}); |
|
496 |
|
|
497 |
# Delete |
|
498 |
$dbi->delete(table => 'books', |
|
499 |
where => {author => 'Ken'}, |
|
500 |
filter => {title => 'encode_utf8'}); |
|
501 |
|
|
502 |
# Delete all |
|
503 |
$dbi->delete_all(table => 'books'); |
|
504 |
|
|
505 |
# Select |
|
506 |
my $result = $dbi->select(table => 'books'); |
|
renamed fetch_rows to fetch_...
|
507 |
|
removed register_format()
|
508 |
# Select(more complex) |
renamed fetch_rows to fetch_...
|
509 |
my $result = $dbi->select( |
update document
|
510 |
table => 'books', |
511 |
column => [qw/author title/], |
|
512 |
where => {author => 'Ken'}, |
|
513 |
append => 'order by id limit 1', |
|
514 |
filter => {tilte => 'encode_utf8'} |
|
renamed fetch_rows to fetch_...
|
515 |
); |
added commit method
|
516 |
|
517 |
# Select(Join table) |
|
518 |
my $result = $dbi->select( |
|
519 |
table => ['books', 'rental'], |
|
520 |
column => ['books.name as book_name'] |
|
521 |
relation => {'books.id' => 'rental.book_id'} |
|
522 |
); |
|
removed reconnect method
|
523 |
|
removed register_format()
|
524 |
# Execute SQL |
525 |
$dbi->execute("select title from books"); |
|
526 |
|
|
527 |
# Execute SQL with parameters and filter |
|
528 |
$dbi->execute("select id from books where {= author} && {like title}", |
|
529 |
param => {author => 'ken', title => '%Perl%'}, |
|
530 |
filter => {tilte => 'encode_utf8'}); |
|
removed reconnect method
|
531 | |
532 |
# Create query and execute it |
|
533 |
my $query = $dbi->create_query( |
|
534 |
"select id from books where {= author} && {like title}" |
|
535 |
); |
|
536 |
$dbi->execute($query, param => {author => 'ken', title => '%Perl%'}) |
|
removed register_format()
|
537 |
|
538 |
# Default filter |
|
539 |
$dbi->default_query_filter('encode_utf8'); |
|
540 |
$dbi->default_fetch_filter('decode_utf8'); |
|
541 |
|
|
542 |
# Fetch |
|
543 |
while (my $row = $result->fetch) { |
|
544 |
# ... |
|
545 |
} |
|
546 |
|
|
547 |
# Fetch hash |
|
548 |
while (my $row = $result->fetch_hash) { |
|
549 |
|
|
550 |
} |
|
551 |
|
|
removed DESTROY method(not b...
|
552 |
# Get DBI object |
update document
|
553 |
my $dbh = $dbi->dbh; |
removed reconnect method
|
554 | |
555 |
=head1 DESCRIPTION |
|
556 | ||
557 |
L<DBIx::Custom> is useful L<DBI> extention. |
|
558 |
This module have hash parameter binding and filtering system. |
|
559 | ||
560 |
Normally, binding parameter is array. |
|
561 |
L<DBIx::Custom> enable you to pass binding parameter as hash. |
|
562 | ||
563 |
This module also provide filtering system. |
|
564 |
You can filter the binding parameter |
|
565 |
or the value of fetching row. |
|
566 | ||
567 |
And have useful method such as insert(), update(), delete(), and select(). |
|
568 | ||
569 |
=head2 Features |
|
570 | ||
571 |
=over 4 |
|
572 | ||
removed DESTROY method(not b...
|
573 |
=item * |
removed reconnect method
|
574 | |
removed DESTROY method(not b...
|
575 |
Hash parameter binding. |
removed reconnect method
|
576 | |
removed DESTROY method(not b...
|
577 |
=item * |
578 | ||
579 |
Value filtering. |
|
580 | ||
581 |
=item * |
|
582 | ||
583 |
Provide suger methods, such as insert(), update(), delete(), and select(). |
|
removed reconnect method
|
584 | |
585 |
=back |
|
586 | ||
update document
|
587 |
=head1 ATTRIBUTES |
packaging one directory
|
588 | |
removed DBIx::Custom commit ...
|
589 |
=head2 C<user> |
packaging one directory
|
590 | |
version 0.0901
|
591 |
$dbi = $dbi->user('Ken'); |
592 |
$user = $dbi->user; |
|
removed DESTROY method(not b...
|
593 | |
594 |
Database user name. |
|
595 |
This is used for connect(). |
|
bind_filter argument is chan...
|
596 |
|
removed DBIx::Custom commit ...
|
597 |
=head2 C<password> |
packaging one directory
|
598 | |
version 0.0901
|
599 |
$dbi = $dbi->password('lkj&le`@s'); |
600 |
$password = $dbi->password; |
|
packaging one directory
|
601 | |
removed DESTROY method(not b...
|
602 |
Database password. |
603 |
This is used for connect(). |
|
604 | ||
removed DBIx::Custom commit ...
|
605 |
=head2 C<data_source> |
packaging one directory
|
606 | |
version 0.0901
|
607 |
$dbi = $dbi->data_source("dbi:mysql:dbname=$database"); |
608 |
$data_source = $dbi->data_source; |
|
removed DESTROY method(not b...
|
609 | |
610 |
Database data source. |
|
611 |
This is used for connect(). |
|
612 | ||
removed DBIx::Custom commit ...
|
613 |
=head2 C<dbh> |
packaging one directory
|
614 | |
removed reconnect method
|
615 |
$dbi = $dbi->dbh($dbh); |
616 |
$dbh = $dbi->dbh; |
|
packaging one directory
|
617 | |
removed DESTROY method(not b...
|
618 |
Database handle. This is a L<DBI> object. |
619 |
You can call all methods of L<DBI> |
|
packaging one directory
|
620 | |
removed reconnect method
|
621 |
my $sth = $dbi->dbh->prepare("..."); |
622 |
my $errstr = $dbi->dbh->errstr; |
|
removed DESTROY method(not b...
|
623 |
$dbi->dbh->begin_work; |
624 |
$dbi->dbh->commit; |
|
625 |
$dbi->dbh->rollback; |
|
removed reconnect method
|
626 |
|
removed DBIx::Custom commit ...
|
627 |
=head2 C<filters> |
packaging one directory
|
628 | |
update document
|
629 |
$dbi = $dbi->filters({%filters}); |
version 0.0901
|
630 |
$filters = $dbi->filters; |
631 | ||
removed DESTROY method(not b...
|
632 |
Filter functions. |
633 |
By default, "encode_utf8" and "decode_utf8" is registered. |
|
version 0.0901
|
634 | |
update document
|
635 |
$encode_utf8 = $dbi->filters->{encode_utf8}; |
636 |
$decode_utf8 = $dbi->filters->{decode_utf8}; |
|
packaging one directory
|
637 | |
removed DBIx::Custom commit ...
|
638 |
=head2 C<default_query_filter> |
packaging one directory
|
639 | |
update document
|
640 |
$dbi = $dbi->default_query_filter('encode_utf8'); |
rename bind_filter to query_...
|
641 |
$default_query_filter = $dbi->default_query_filter |
packaging one directory
|
642 | |
removed DESTROY method(not b...
|
643 |
Default filter for value binding |
packaging one directory
|
644 | |
removed DESTROY method(not b...
|
645 |
=head2 C<default_fetch_filter> |
bind_filter argument is chan...
|
646 | |
update document
|
647 |
$dbi = $dbi->default_fetch_filter('decode_utf8'); |
cleanup
|
648 |
$default_fetch_filter = $dbi->default_fetch_filter; |
bind_filter argument is chan...
|
649 | |
removed DESTROY method(not b...
|
650 |
Default filter for fetching. |
packaging one directory
|
651 | |
removed DESTROY method(not b...
|
652 |
=head2 C<result_class> |
bind_filter argument is chan...
|
653 | |
version 0.0901
|
654 |
$dbi = $dbi->result_class('DBIx::Custom::Result'); |
packaging one directory
|
655 |
$result_class = $dbi->result_class; |
656 | ||
removed DESTROY method(not b...
|
657 |
Result class for select statement. |
658 |
Default to L<DBIx::Custom::Result> |
|
update document
|
659 | |
removed DBIx::Custom commit ...
|
660 |
=head2 C<sql_template> |
added commit method
|
661 | |
removed reconnect method
|
662 |
$dbi = $dbi->sql_template(DBIx::Cutom::SQLTemplate->new); |
663 |
$sql_template = $dbi->sql_template; |
|
added commit method
|
664 | |
removed DESTROY method(not b...
|
665 |
SQLTemplate instance. sql_template attribute must be |
666 |
the instance of L<DBIx::Cutom::SQLTemplate> subclass. |
|
667 |
Default to DBIx::Cutom::SQLTemplate object |
|
added commit method
|
668 | |
removed reconnect method
|
669 |
=head1 METHODS |
added commit method
|
670 | |
removed reconnect method
|
671 |
This class is L<Object::Simple> subclass. |
672 |
You can use all methods of L<Object::Simple> |
|
added commit method
|
673 | |
removed DBIx::Custom commit ...
|
674 |
=head2 C<connect> |
packaging one directory
|
675 | |
update document
|
676 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=books", |
677 |
user => 'ken', password => '!LFKD%$&'); |
|
bind_filter argument is chan...
|
678 | |
removed DESTROY method(not b...
|
679 |
Connect to database. |
update document
|
680 |
"AutoCommit" and "RaiseError" option is true, |
removed DESTROY method(not b...
|
681 |
and "PrintError" option is false by default. |
packaging one directory
|
682 | |
removed DBIx::Custom commit ...
|
683 |
=head2 C<insert> |
packaging one directory
|
684 | |
removed register_format()
|
685 |
$affected = $dbi->insert(table => $table, |
686 |
param => {%param}, |
|
687 |
append => $append, |
|
688 |
filter => {%filter}); |
|
update document
|
689 | |
removed DESTROY method(not b...
|
690 |
Insert row. |
691 |
Retrun value is the count of affected rows. |
|
packaging one directory
|
692 |
|
removed DESTROY method(not b...
|
693 |
B<Example:> |
version 0.0901
|
694 | |
removed register_format()
|
695 |
$dbi->insert(table => 'books', |
696 |
param => {title => 'Perl', author => 'Taro'}, |
|
697 |
append => "some statement", |
|
698 |
filter => {title => 'encode_utf8'}) |
|
version 0.0901
|
699 | |
removed DBIx::Custom commit ...
|
700 |
=head2 C<update> |
packaging one directory
|
701 | |
removed register_format()
|
702 |
$affected = $dbi->update(table => $table, |
703 |
param => {%params}, |
|
704 |
where => {%where}, |
|
705 |
append => $append, |
|
706 |
filter => {%filter}) |
|
version 0.0901
|
707 | |
removed DESTROY method(not b...
|
708 |
Update rows. |
709 |
Retrun value is the count of affected rows. |
|
update document
|
710 | |
removed DESTROY method(not b...
|
711 |
B<Example:> |
bind_filter argument is chan...
|
712 | |
removed register_format()
|
713 |
$dbi->update(table => 'books', |
714 |
param => {title => 'Perl', author => 'Taro'}, |
|
715 |
where => {id => 5}, |
|
716 |
append => "some statement", |
|
added commit method
|
717 |
filter => {title => 'encode_utf8'}); |
version 0.0901
|
718 | |
removed DBIx::Custom commit ...
|
719 |
=head2 C<update_all> |
packaging one directory
|
720 | |
removed register_format()
|
721 |
$affected = $dbi->update_all(table => $table, |
722 |
param => {%params}, |
|
723 |
filter => {%filter}, |
|
724 |
append => $append); |
|
update document
|
725 | |
removed DESTROY method(not b...
|
726 |
Update all rows. |
727 |
Retrun value is the count of affected rows. |
|
version 0.0901
|
728 | |
removed DESTROY method(not b...
|
729 |
B<Example:> |
update document
|
730 | |
removed register_format()
|
731 |
$dbi->update_all(table => 'books', |
732 |
param => {author => 'taro'}, |
|
733 |
filter => {author => 'encode_utf8'}); |
|
packaging one directory
|
734 | |
removed DBIx::Custom commit ...
|
735 |
=head2 C<delete> |
packaging one directory
|
736 | |
removed register_format()
|
737 |
$affected = $dbi->delete(table => $table, |
738 |
where => {%where}, |
|
added commit method
|
739 |
append => $append, |
removed register_format()
|
740 |
filter => {%filter}); |
bind_filter argument is chan...
|
741 | |
removed DESTROY method(not b...
|
742 |
Delete rows. |
743 |
Retrun value is the count of affected rows. |
|
packaging one directory
|
744 |
|
removed DESTROY method(not b...
|
745 |
B<Example:> |
packaging one directory
|
746 | |
removed register_format()
|
747 |
$dbi->delete(table => 'books', |
748 |
where => {id => 5}, |
|
749 |
append => 'some statement', |
|
removed reconnect method
|
750 |
filter => {id => 'encode_utf8'}); |
version 0.0901
|
751 | |
removed DBIx::Custom commit ...
|
752 |
=head2 C<delete_all> |
packaging one directory
|
753 | |
removed register_format()
|
754 |
$affected = $dbi->delete_all(table => $table); |
packaging one directory
|
755 | |
removed DESTROY method(not b...
|
756 |
Delete all rows. |
757 |
Retrun value is the count of affected rows. |
|
bind_filter argument is chan...
|
758 | |
removed DESTROY method(not b...
|
759 |
B<Example:> |
removed register_format()
|
760 |
|
removed reconnect method
|
761 |
$dbi->delete_all(table => 'books'); |
packaging one directory
|
762 | |
removed DBIx::Custom commit ...
|
763 |
=head2 C<select> |
packaging one directory
|
764 |
|
added commit method
|
765 |
$result = $dbi->select(table => $table, |
766 |
column => [@column], |
|
767 |
where => {%where}, |
|
768 |
append => $append, |
|
removed reconnect method
|
769 |
relation => {%relation}, |
added commit method
|
770 |
filter => {%filter}); |
update document
|
771 | |
removed DESTROY method(not b...
|
772 |
Select rows. |
removed reconnect method
|
773 |
Return value is the instance of L<DBIx::Custom::Result>. |
update document
|
774 | |
removed DESTROY method(not b...
|
775 |
B<Example:> |
update document
|
776 | |
added commit method
|
777 |
# select * from books; |
removed reconnect method
|
778 |
$result = $dbi->select(table => 'books'); |
packaging one directory
|
779 |
|
update document
|
780 |
# select * from books where title = 'Perl'; |
removed reconnect method
|
781 |
$result = $dbi->select(table => 'books', where => {title => 1}); |
update document
|
782 |
|
783 |
# select title, author from books where id = 1 for update; |
|
784 |
$result = $dbi->select( |
|
removed register_format()
|
785 |
table => 'books', |
removed reconnect method
|
786 |
column => ['title', 'author'], |
removed register_format()
|
787 |
where => {id => 1}, |
788 |
appned => 'for update' |
|
update document
|
789 |
); |
790 |
|
|
added commit method
|
791 |
# select books.name as book_name from books, rental |
792 |
# where books.id = rental.book_id; |
|
793 |
my $result = $dbi->select( |
|
removed reconnect method
|
794 |
table => ['books', 'rental'], |
795 |
column => ['books.name as book_name'] |
|
added commit method
|
796 |
relation => {'books.id' => 'rental.book_id'} |
update document
|
797 |
); |
798 | ||
removed DBIx::Custom commit ...
|
799 |
=head2 C<create_query> |
removed reconnect method
|
800 |
|
removed DBIx::Custom commit ...
|
801 |
my $query = $dbi->create_query( |
802 |
"select * from authors where {= name} and {= age};" |
|
803 |
); |
|
804 | ||
removed reconnect method
|
805 |
Create the instance of L<DBIx::Custom::Query>. |
806 |
This receive the string written by SQL template. |
|
packaging one directory
|
807 | |
removed DBIx::Custom commit ...
|
808 |
=head2 C<execute> |
removed reconnect method
|
809 | |
removed DBIx::Custom commit ...
|
810 |
$result = $dbi->execute($query, param => $params, filter => {%filter}); |
811 |
$result = $dbi->execute($template, param => $params, filter => {%filter}); |
|
removed reconnect method
|
812 | |
813 |
Execute the instace of L<DBIx::Custom::Query> or |
|
814 |
the string written by SQL template. |
|
815 |
Return value is the instance of L<DBIx::Custom::Result>. |
|
816 | ||
removed DBIx::Custom commit ...
|
817 |
B<Example:> |
removed reconnect method
|
818 | |
819 |
$result = $dbi->execute("select * from authors where {= name} and {= age}", |
|
820 |
param => {name => 'taro', age => 19}); |
|
821 |
|
|
822 |
while (my $row = $result->fetch) { |
|
823 |
# do something |
|
824 |
} |
|
825 | ||
826 |
See also L<DBIx::Custom::SQLTemplate> to know how to write SQL template. |
|
827 | ||
removed DBIx::Custom commit ...
|
828 |
=head2 C<register_filter> |
removed reconnect method
|
829 | |
830 |
$dbi->register_filter(%filters); |
|
removed DESTROY method(not b...
|
831 |
$dbi->register_filter(\%filters); |
removed reconnect method
|
832 |
|
removed DBIx::Custom commit ...
|
833 |
Resister filter. |
834 | ||
835 |
B<Example:> |
|
packaging one directory
|
836 | |
removed reconnect method
|
837 |
$dbi->register_filter( |
838 |
encode_utf8 => sub { |
|
839 |
my $value = shift; |
|
840 |
|
|
841 |
require Encode; |
|
842 |
|
|
843 |
return Encode::encode('UTF-8', $value); |
|
844 |
}, |
|
845 |
decode_utf8 => sub { |
|
846 |
my $value = shift; |
|
847 |
|
|
848 |
require Encode; |
|
849 |
|
|
850 |
return Encode::decode('UTF-8', $value) |
|
851 |
} |
|
852 |
); |
|
853 | ||
removed DBIx::Custom commit ...
|
854 |
=head2 C<cache> |
added cache_method attribute
|
855 | |
856 |
$dbi = $dbi->cache(1); |
|
857 |
$cache = $dbi->cache; |
|
858 | ||
removed DBIx::Custom commit ...
|
859 |
Cache the result of parsing SQL template. |
added cache_method attribute
|
860 |
Default to 1. |
861 | ||
removed DBIx::Custom commit ...
|
862 |
=head2 C<cache_method> |
863 | ||
864 |
$dbi = $dbi->cache_method(\&cache_method); |
|
865 |
$cache_method = $dbi->cache_method |
|
added cache_method attribute
|
866 | |
867 |
Method for cache. |
|
868 | ||
removed DBIx::Custom commit ...
|
869 |
B<Example:> |
added cache_method attribute
|
870 | |
871 |
$dbi->cache_method( |
|
872 |
sub { |
|
873 |
my $self = shift; |
|
874 |
|
|
875 |
$self->{_cached} ||= {}; |
|
876 |
|
|
877 |
if (@_ > 1) { |
|
878 |
$self->{_cached}{$_[0]} = $_[1] |
|
879 |
} |
|
880 |
else { |
|
881 |
return $self->{_cached}{$_[0]} |
|
882 |
} |
|
883 |
} |
|
884 |
); |
|
885 | ||
removed DESTROY method(not b...
|
886 |
=head1 BUGS |
887 | ||
888 |
Please tell me bugs. |
|
889 | ||
890 |
C<< <kimoto.yuki at gmail.com> >> |
|
891 | ||
892 |
L<http://github.com/yuki-kimoto/DBIx-Custom> |
|
893 | ||
removed reconnect method
|
894 |
=head1 AUTHOR |
895 | ||
896 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
|
version 0.0901
|
897 | |
packaging one directory
|
898 |
=head1 COPYRIGHT & LICENSE |
899 | ||
900 |
Copyright 2009 Yuki Kimoto, all rights reserved. |
|
901 | ||
902 |
This program is free software; you can redistribute it and/or modify it |
|
903 |
under the same terms as Perl itself. |
|
904 | ||
905 |
=cut |
|
added cache_method attribute
|
906 | |
907 |