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; |
|
renamed default_query_filter...
|
11 |
use DBIx::Custom::QueryBuilder; |
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/]); |
renamed default_query_filter...
|
17 |
__PACKAGE__->attr([qw/default_bind_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'); |
renamed default_query_filter...
|
27 |
__PACKAGE__->attr(sql_builder => sub {DBIx::Custom::QueryBuilder->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 |
|
renamed default_query_filter...
|
112 |
my $source = "insert into $table {insert " |
removed DESTROY method(not b...
|
113 |
. join(' ', @insert_keys) . '}'; |
renamed default_query_filter...
|
114 |
$source .= " $append" if $append; |
packaging one directory
|
115 |
|
116 |
# Execute query |
|
renamed default_query_filter...
|
117 |
my $ret_val = $self->execute($source, param => $param, |
removed register_format()
|
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 |
|
renamed default_query_filter...
|
174 |
my $source = "update $table $update_clause $where_clause"; |
175 |
$source .= " $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 |
|
renamed default_query_filter...
|
192 |
my $ret_val = $self->execute($source, param => $param, |
removed register_format()
|
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 |
|
renamed default_query_filter...
|
237 |
my $source = "delete from $table $where_clause"; |
238 |
$source .= " $append_statement" if $append_statement; |
|
packaging one directory
|
239 |
|
240 |
# Execute query |
|
renamed default_query_filter...
|
241 |
my $ret_val = $self->execute($source, param => $where, |
removed register_format()
|
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 |
|
renamed default_query_filter...
|
272 |
my $source = 'select '; |
packaging one directory
|
273 |
|
added commit method
|
274 |
# Column clause |
packaging one directory
|
275 |
if (@$columns) { |
276 |
foreach my $column (@$columns) { |
|
renamed default_query_filter...
|
277 |
$source .= "$column, "; |
packaging one directory
|
278 |
} |
renamed default_query_filter...
|
279 |
$source =~ s/, $/ /; |
packaging one directory
|
280 |
} |
281 |
else { |
|
renamed default_query_filter...
|
282 |
$source .= '* '; |
packaging one directory
|
283 |
} |
284 |
|
|
added commit method
|
285 |
# Table |
renamed default_query_filter...
|
286 |
$source .= 'from '; |
packaging one directory
|
287 |
foreach my $table (@$tables) { |
renamed default_query_filter...
|
288 |
$source .= "$table, "; |
packaging one directory
|
289 |
} |
renamed default_query_filter...
|
290 |
$source =~ s/, $/ /; |
packaging one directory
|
291 |
|
added commit method
|
292 |
# Where clause |
293 |
my @where_keys = keys %$where; |
|
packaging one directory
|
294 |
if (@where_keys) { |
renamed default_query_filter...
|
295 |
$source .= 'where '; |
packaging one directory
|
296 |
foreach my $where_key (@where_keys) { |
renamed default_query_filter...
|
297 |
$source .= "{= $where_key} and "; |
packaging one directory
|
298 |
} |
299 |
} |
|
renamed default_query_filter...
|
300 |
$source =~ s/ and $//; |
packaging one directory
|
301 |
|
added commit method
|
302 |
# Relation |
303 |
if ($relation) { |
|
renamed default_query_filter...
|
304 |
$source .= @where_keys ? "and " : "where "; |
added commit method
|
305 |
foreach my $rkey (keys %$relation) { |
renamed default_query_filter...
|
306 |
$source .= "$rkey = " . $relation->{$rkey} . " and "; |
packaging one directory
|
307 |
} |
308 |
} |
|
renamed default_query_filter...
|
309 |
$source =~ s/ and $//; |
added commit method
|
310 |
|
311 |
# Append some statement |
|
renamed default_query_filter...
|
312 |
$source .= " $append" if $append; |
packaging one directory
|
313 |
|
314 |
# Execute query |
|
renamed default_query_filter...
|
315 |
my $result = $self->execute($source, param => $where, |
removed register_format()
|
316 |
filter => $filter); |
packaging one directory
|
317 |
|
318 |
return $result; |
|
319 |
} |
|
320 | ||
renamed default_query_filter...
|
321 |
sub build_query { |
322 |
my ($self, $source) = @_; |
|
removed register_format()
|
323 |
|
renamed default_query_filter...
|
324 |
# Cache |
added cache_method attribute
|
325 |
my $cache = $self->cache; |
version 0.0901
|
326 |
|
removed reconnect method
|
327 |
# Create query |
328 |
my $query; |
|
added cache_method attribute
|
329 |
if ($cache) { |
330 |
|
|
renamed default_query_filter...
|
331 |
# Get query |
332 |
my $q = $self->cache_method->($self, $source); |
|
added cache_method attribute
|
333 |
|
334 |
# Create query |
|
335 |
$query = DBIx::Custom::Query->new($q) if $q; |
|
removed reconnect method
|
336 |
} |
added cache_method attribute
|
337 |
|
338 |
unless ($query) { |
|
renamed default_query_filter...
|
339 | |
340 |
# Create SQL object |
|
341 |
my $builder = $self->sql_builder; |
|
added cache_method attribute
|
342 |
|
343 |
# Create query |
|
renamed default_query_filter...
|
344 |
$query = eval{$builder->build_query($source)}; |
removed reconnect method
|
345 |
croak($@) if $@; |
346 |
|
|
added cache_method attribute
|
347 |
# Cache query |
renamed default_query_filter...
|
348 |
$self->cache_method->($self, $source, |
added cache_method attribute
|
349 |
{sql => $query->sql, |
350 |
columns => $query->columns}) |
|
351 |
if $cache; |
|
removed reconnect method
|
352 |
} |
version 0.0901
|
353 |
|
removed reconnect method
|
354 |
# Prepare statement handle |
select, insert, update, upda...
|
355 |
my $sth = eval {$self->dbh->prepare($query->{sql})}; |
356 |
croak $@ if $@; |
|
renamed fetch_rows to fetch_...
|
357 |
|
removed reconnect method
|
358 |
# Set statement handle |
359 |
$query->sth($sth); |
|
360 |
|
|
361 |
return $query; |
|
362 |
} |
|
363 | ||
364 |
our %VALID_EXECUTE_ARGS = map { $_ => 1 } qw/param filter/; |
|
365 | ||
366 |
sub execute{ |
|
select, insert, update, upda...
|
367 |
my ($self, $query, %args) = @_; |
removed reconnect method
|
368 |
|
369 |
# Check arguments |
|
select, insert, update, upda...
|
370 |
foreach my $name (keys %args) { |
removed reconnect method
|
371 |
croak "\"$name\" is invalid name" |
372 |
unless $VALID_EXECUTE_ARGS{$name}; |
|
373 |
} |
|
374 |
|
|
select, insert, update, upda...
|
375 |
my $params = $args{param} || {}; |
removed reconnect method
|
376 |
|
377 |
# First argument is SQL template |
|
378 |
unless (ref $query eq 'DBIx::Custom::Query') { |
|
renamed default_query_filter...
|
379 |
my $source; |
removed reconnect method
|
380 |
|
381 |
if (ref $query eq 'ARRAY') { |
|
renamed default_query_filter...
|
382 |
$source = $query->[0]; |
removed reconnect method
|
383 |
} |
renamed default_query_filter...
|
384 |
else { $source = $query } |
removed reconnect method
|
385 |
|
renamed default_query_filter...
|
386 |
$query = $self->build_query($source); |
removed reconnect method
|
387 |
} |
388 |
|
|
select, insert, update, upda...
|
389 |
my $filter = $args{filter} || $query->filter || {}; |
removed reconnect method
|
390 |
|
391 |
# Create bind value |
|
392 |
my $bind_values = $self->_build_bind_values($query, $params, $filter); |
|
393 |
|
|
394 |
# Execute |
|
395 |
my $sth = $query->sth; |
|
select, insert, update, upda...
|
396 |
my $affected = eval {$sth->execute(@$bind_values)}; |
397 |
croak $@ if $@; |
|
removed reconnect method
|
398 |
|
399 |
# Return resultset if select statement is executed |
|
400 |
if ($sth->{NUM_OF_FIELDS}) { |
|
401 |
|
|
402 |
# Create result |
|
renamed default_query_filter...
|
403 |
my $result = $self->result_class->new( |
404 |
sth => $sth, |
|
405 |
default_filter => $self->default_fetch_filter, |
|
406 |
filters => $self->filters |
|
407 |
); |
|
408 | ||
removed reconnect method
|
409 |
return $result; |
410 |
} |
|
411 |
return $affected; |
|
412 |
} |
|
413 | ||
414 |
sub _build_bind_values { |
|
415 |
my ($self, $query, $params, $filter) = @_; |
|
416 |
|
|
417 |
# binding values |
|
418 |
my @bind_values; |
|
419 |
|
|
420 |
# Build bind values |
|
421 |
my $count = {}; |
|
422 |
foreach my $column (@{$query->columns}) { |
|
423 |
|
|
424 |
croak "\"$column\" is not exists in params" |
|
425 |
unless exists $params->{$column}; |
|
426 |
|
|
427 |
# Value |
|
428 |
my $value = ref $params->{$column} eq 'ARRAY' |
|
429 |
? $params->{$column}->[$count->{$column} || 0] |
|
430 |
: $params->{$column}; |
|
431 |
|
|
432 |
# Filter |
|
433 |
$filter ||= {}; |
|
434 |
|
|
435 |
# Filter name |
|
renamed default_query_filter...
|
436 |
my $fname = $filter->{$column} || $self->default_bind_filter || ''; |
removed reconnect method
|
437 |
|
438 |
my $filter_func; |
|
439 |
if ($fname) { |
|
440 |
|
|
441 |
if (ref $fname eq 'CODE') { |
|
442 |
$filter_func = $fname; |
|
443 |
} |
|
444 |
else { |
|
445 |
my $filters = $self->filters; |
|
removed DESTROY method(not b...
|
446 |
croak "Not exists filter \"$fname\"" |
447 |
unless exists $filters->{$fname}; |
|
removed reconnect method
|
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 |
=head1 NAME |
|
464 | ||
465 |
DBIx::Custom - DBI with hash parameter binding and filtering system |
|
466 | ||
467 |
=cut |
|
468 | ||
removed DESTROY method(not b...
|
469 |
our $VERSION = '0.1604'; |
removed reconnect method
|
470 | |
471 |
=head1 STABILITY |
|
472 | ||
removed DESTROY method(not b...
|
473 |
This module is not stable. Method name and implementations will be changed. |
removed reconnect method
|
474 | |
475 |
=head1 SYNOPSYS |
|
476 |
|
|
477 |
# Connect |
|
478 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=books", |
|
479 |
user => 'ken', password => '!LFKD%$&'); |
|
480 |
# Insert |
|
481 |
$dbi->insert(table => 'books', |
|
482 |
param => {title => 'perl', author => 'Ken'}, |
|
483 |
filter => {title => 'encode_utf8'}); |
|
484 |
|
|
485 |
# Update |
|
486 |
$dbi->update(table => 'books', |
|
487 |
param => {title => 'aaa', author => 'Ken'}, |
|
488 |
where => {id => 5}, |
|
489 |
filter => {title => 'encode_utf8'}); |
|
490 |
|
|
491 |
# Update all |
|
492 |
$dbi->update_all(table => 'books', |
|
493 |
param => {title => 'aaa'}, |
|
494 |
filter => {title => 'encode_utf8'}); |
|
495 |
|
|
496 |
# Delete |
|
497 |
$dbi->delete(table => 'books', |
|
498 |
where => {author => 'Ken'}, |
|
499 |
filter => {title => 'encode_utf8'}); |
|
500 |
|
|
501 |
# Delete all |
|
502 |
$dbi->delete_all(table => 'books'); |
|
503 |
|
|
504 |
# Select |
|
505 |
my $result = $dbi->select(table => 'books'); |
|
renamed fetch_rows to fetch_...
|
506 |
|
removed register_format()
|
507 |
# Select(more complex) |
renamed fetch_rows to fetch_...
|
508 |
my $result = $dbi->select( |
update document
|
509 |
table => 'books', |
510 |
column => [qw/author title/], |
|
511 |
where => {author => 'Ken'}, |
|
512 |
append => 'order by id limit 1', |
|
513 |
filter => {tilte => 'encode_utf8'} |
|
renamed fetch_rows to fetch_...
|
514 |
); |
added commit method
|
515 |
|
516 |
# Select(Join table) |
|
517 |
my $result = $dbi->select( |
|
518 |
table => ['books', 'rental'], |
|
519 |
column => ['books.name as book_name'] |
|
520 |
relation => {'books.id' => 'rental.book_id'} |
|
521 |
); |
|
removed reconnect method
|
522 |
|
removed register_format()
|
523 |
# Execute SQL |
524 |
$dbi->execute("select title from books"); |
|
525 |
|
|
526 |
# Execute SQL with parameters and filter |
|
527 |
$dbi->execute("select id from books where {= author} && {like title}", |
|
528 |
param => {author => 'ken', title => '%Perl%'}, |
|
529 |
filter => {tilte => 'encode_utf8'}); |
|
removed reconnect method
|
530 | |
531 |
# Create query and execute it |
|
renamed default_query_filter...
|
532 |
my $query = $dbi->build_query( |
removed reconnect method
|
533 |
"select id from books where {= author} && {like title}" |
534 |
); |
|
535 |
$dbi->execute($query, param => {author => 'ken', title => '%Perl%'}) |
|
removed register_format()
|
536 |
|
537 |
# Default filter |
|
renamed default_query_filter...
|
538 |
$dbi->default_bind_filter('encode_utf8'); |
removed register_format()
|
539 |
$dbi->default_fetch_filter('decode_utf8'); |
540 |
|
|
541 |
# Fetch |
|
542 |
while (my $row = $result->fetch) { |
|
543 |
# ... |
|
544 |
} |
|
545 |
|
|
546 |
# Fetch hash |
|
547 |
while (my $row = $result->fetch_hash) { |
|
548 |
|
|
549 |
} |
|
550 |
|
|
removed DESTROY method(not b...
|
551 |
# Get DBI object |
update document
|
552 |
my $dbh = $dbi->dbh; |
removed reconnect method
|
553 | |
554 |
=head1 DESCRIPTION |
|
555 | ||
556 |
L<DBIx::Custom> is useful L<DBI> extention. |
|
557 |
This module have hash parameter binding and filtering system. |
|
558 | ||
559 |
Normally, binding parameter is array. |
|
560 |
L<DBIx::Custom> enable you to pass binding parameter as hash. |
|
561 | ||
562 |
This module also provide filtering system. |
|
563 |
You can filter the binding parameter |
|
564 |
or the value of fetching row. |
|
565 | ||
566 |
And have useful method such as insert(), update(), delete(), and select(). |
|
567 | ||
568 |
=head2 Features |
|
569 | ||
570 |
=over 4 |
|
571 | ||
removed DESTROY method(not b...
|
572 |
=item * |
removed reconnect method
|
573 | |
removed DESTROY method(not b...
|
574 |
Hash parameter binding. |
removed reconnect method
|
575 | |
removed DESTROY method(not b...
|
576 |
=item * |
577 | ||
578 |
Value filtering. |
|
579 | ||
580 |
=item * |
|
581 | ||
582 |
Provide suger methods, such as insert(), update(), delete(), and select(). |
|
removed reconnect method
|
583 | |
584 |
=back |
|
585 | ||
update document
|
586 |
=head1 ATTRIBUTES |
packaging one directory
|
587 | |
removed DBIx::Custom commit ...
|
588 |
=head2 C<user> |
packaging one directory
|
589 | |
cleanup
|
590 |
my $user = $dbi->user; |
591 |
$dbi = $dbi->user('Ken'); |
|
removed DESTROY method(not b...
|
592 | |
593 |
Database user name. |
|
594 |
This is used for connect(). |
|
bind_filter argument is chan...
|
595 |
|
removed DBIx::Custom commit ...
|
596 |
=head2 C<password> |
packaging one directory
|
597 | |
cleanup
|
598 |
my $password = $dbi->password; |
599 |
$dbi = $dbi->password('lkj&le`@s'); |
|
packaging one directory
|
600 | |
removed DESTROY method(not b...
|
601 |
Database password. |
602 |
This is used for connect(). |
|
603 | ||
removed DBIx::Custom commit ...
|
604 |
=head2 C<data_source> |
packaging one directory
|
605 | |
cleanup
|
606 |
my $data_source = $dbi->data_source; |
607 |
$dbi = $dbi->data_source("dbi:mysql:dbname=$database"); |
|
removed DESTROY method(not b...
|
608 | |
609 |
Database data source. |
|
610 |
This is used for connect(). |
|
611 | ||
removed DBIx::Custom commit ...
|
612 |
=head2 C<dbh> |
packaging one directory
|
613 | |
cleanup
|
614 |
my $dbh = $dbi->dbh; |
615 |
$dbi = $dbi->dbh($dbh); |
|
packaging one directory
|
616 | |
removed DESTROY method(not b...
|
617 |
Database handle. This is a L<DBI> object. |
618 |
You can call all methods of L<DBI> |
|
packaging one directory
|
619 | |
removed reconnect method
|
620 |
my $sth = $dbi->dbh->prepare("..."); |
621 |
my $errstr = $dbi->dbh->errstr; |
|
removed DESTROY method(not b...
|
622 |
$dbi->dbh->begin_work; |
623 |
$dbi->dbh->commit; |
|
624 |
$dbi->dbh->rollback; |
|
removed reconnect method
|
625 |
|
removed DBIx::Custom commit ...
|
626 |
=head2 C<filters> |
packaging one directory
|
627 | |
cleanup
|
628 |
my $filters = $dbi->filters; |
629 |
$dbi = $dbi->filters({%filters}); |
|
version 0.0901
|
630 | |
removed DESTROY method(not b...
|
631 |
Filter functions. |
632 |
By default, "encode_utf8" and "decode_utf8" is registered. |
|
version 0.0901
|
633 | |
update document
|
634 |
$encode_utf8 = $dbi->filters->{encode_utf8}; |
635 |
$decode_utf8 = $dbi->filters->{decode_utf8}; |
|
packaging one directory
|
636 | |
renamed default_query_filter...
|
637 |
=head2 C<default_bind_filter> |
packaging one directory
|
638 | |
cleanup
|
639 |
my $default_bind_filter = $dbi->default_bind_filter |
640 |
$dbi = $dbi->default_bind_filter('encode_utf8'); |
|
packaging one directory
|
641 | |
removed DESTROY method(not b...
|
642 |
Default filter for value binding |
packaging one directory
|
643 | |
removed DESTROY method(not b...
|
644 |
=head2 C<default_fetch_filter> |
bind_filter argument is chan...
|
645 | |
cleanup
|
646 |
my $default_fetch_filter = $dbi->default_fetch_filter; |
647 |
$dbi = $dbi->default_fetch_filter('decode_utf8'); |
|
bind_filter argument is chan...
|
648 | |
removed DESTROY method(not b...
|
649 |
Default filter for fetching. |
packaging one directory
|
650 | |
removed DESTROY method(not b...
|
651 |
=head2 C<result_class> |
bind_filter argument is chan...
|
652 | |
cleanup
|
653 |
my $result_class = $dbi->result_class; |
654 |
$dbi = $dbi->result_class('DBIx::Custom::Result'); |
|
packaging one directory
|
655 | |
removed DESTROY method(not b...
|
656 |
Result class for select statement. |
renamed default_query_filter...
|
657 |
Default to L<DBIx::Custom::Result>. |
update document
|
658 | |
renamed default_query_filter...
|
659 |
=head2 C<sql_builder> |
added commit method
|
660 | |
cleanup
|
661 |
my $sql_class = $dbi->sql_builder; |
662 |
$dbi = $dbi->sql_builder(DBIx::Custom::QueryBuilder->new); |
|
added commit method
|
663 | |
cleanup
|
664 |
SQL builder. sql_builder must be |
665 |
the instance of L<DBIx::Custom::QueryBuilder> subclass |
|
renamed default_query_filter...
|
666 |
Default to DBIx::Custom::QueryBuilder. |
added commit method
|
667 | |
removed reconnect method
|
668 |
=head1 METHODS |
added commit method
|
669 | |
removed reconnect method
|
670 |
This class is L<Object::Simple> subclass. |
671 |
You can use all methods of L<Object::Simple> |
|
added commit method
|
672 | |
removed DBIx::Custom commit ...
|
673 |
=head2 C<connect> |
packaging one directory
|
674 | |
update document
|
675 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=books", |
676 |
user => 'ken', password => '!LFKD%$&'); |
|
bind_filter argument is chan...
|
677 | |
removed DESTROY method(not b...
|
678 |
Connect to database. |
update document
|
679 |
"AutoCommit" and "RaiseError" option is true, |
removed DESTROY method(not b...
|
680 |
and "PrintError" option is false by default. |
packaging one directory
|
681 | |
removed DBIx::Custom commit ...
|
682 |
=head2 C<insert> |
packaging one directory
|
683 | |
removed register_format()
|
684 |
$affected = $dbi->insert(table => $table, |
685 |
param => {%param}, |
|
686 |
append => $append, |
|
687 |
filter => {%filter}); |
|
update document
|
688 | |
removed DESTROY method(not b...
|
689 |
Insert row. |
690 |
Retrun value is the count of affected rows. |
|
packaging one directory
|
691 |
|
removed DESTROY method(not b...
|
692 |
B<Example:> |
version 0.0901
|
693 | |
removed register_format()
|
694 |
$dbi->insert(table => 'books', |
695 |
param => {title => 'Perl', author => 'Taro'}, |
|
696 |
append => "some statement", |
|
697 |
filter => {title => 'encode_utf8'}) |
|
version 0.0901
|
698 | |
removed DBIx::Custom commit ...
|
699 |
=head2 C<update> |
packaging one directory
|
700 | |
removed register_format()
|
701 |
$affected = $dbi->update(table => $table, |
702 |
param => {%params}, |
|
703 |
where => {%where}, |
|
704 |
append => $append, |
|
705 |
filter => {%filter}) |
|
version 0.0901
|
706 | |
removed DESTROY method(not b...
|
707 |
Update rows. |
708 |
Retrun value is the count of affected rows. |
|
update document
|
709 | |
removed DESTROY method(not b...
|
710 |
B<Example:> |
bind_filter argument is chan...
|
711 | |
removed register_format()
|
712 |
$dbi->update(table => 'books', |
713 |
param => {title => 'Perl', author => 'Taro'}, |
|
714 |
where => {id => 5}, |
|
715 |
append => "some statement", |
|
added commit method
|
716 |
filter => {title => 'encode_utf8'}); |
version 0.0901
|
717 | |
removed DBIx::Custom commit ...
|
718 |
=head2 C<update_all> |
packaging one directory
|
719 | |
removed register_format()
|
720 |
$affected = $dbi->update_all(table => $table, |
721 |
param => {%params}, |
|
722 |
filter => {%filter}, |
|
723 |
append => $append); |
|
update document
|
724 | |
removed DESTROY method(not b...
|
725 |
Update all rows. |
726 |
Retrun value is the count of affected rows. |
|
version 0.0901
|
727 | |
removed DESTROY method(not b...
|
728 |
B<Example:> |
update document
|
729 | |
removed register_format()
|
730 |
$dbi->update_all(table => 'books', |
731 |
param => {author => 'taro'}, |
|
732 |
filter => {author => 'encode_utf8'}); |
|
packaging one directory
|
733 | |
removed DBIx::Custom commit ...
|
734 |
=head2 C<delete> |
packaging one directory
|
735 | |
removed register_format()
|
736 |
$affected = $dbi->delete(table => $table, |
737 |
where => {%where}, |
|
added commit method
|
738 |
append => $append, |
removed register_format()
|
739 |
filter => {%filter}); |
bind_filter argument is chan...
|
740 | |
removed DESTROY method(not b...
|
741 |
Delete rows. |
742 |
Retrun value is the count of affected rows. |
|
packaging one directory
|
743 |
|
removed DESTROY method(not b...
|
744 |
B<Example:> |
packaging one directory
|
745 | |
removed register_format()
|
746 |
$dbi->delete(table => 'books', |
747 |
where => {id => 5}, |
|
748 |
append => 'some statement', |
|
removed reconnect method
|
749 |
filter => {id => 'encode_utf8'}); |
version 0.0901
|
750 | |
removed DBIx::Custom commit ...
|
751 |
=head2 C<delete_all> |
packaging one directory
|
752 | |
removed register_format()
|
753 |
$affected = $dbi->delete_all(table => $table); |
packaging one directory
|
754 | |
removed DESTROY method(not b...
|
755 |
Delete all rows. |
756 |
Retrun value is the count of affected rows. |
|
bind_filter argument is chan...
|
757 | |
removed DESTROY method(not b...
|
758 |
B<Example:> |
removed register_format()
|
759 |
|
removed reconnect method
|
760 |
$dbi->delete_all(table => 'books'); |
packaging one directory
|
761 | |
removed DBIx::Custom commit ...
|
762 |
=head2 C<select> |
packaging one directory
|
763 |
|
added commit method
|
764 |
$result = $dbi->select(table => $table, |
765 |
column => [@column], |
|
766 |
where => {%where}, |
|
767 |
append => $append, |
|
removed reconnect method
|
768 |
relation => {%relation}, |
added commit method
|
769 |
filter => {%filter}); |
update document
|
770 | |
removed DESTROY method(not b...
|
771 |
Select rows. |
removed reconnect method
|
772 |
Return value is the instance of L<DBIx::Custom::Result>. |
update document
|
773 | |
removed DESTROY method(not b...
|
774 |
B<Example:> |
update document
|
775 | |
added commit method
|
776 |
# select * from books; |
removed reconnect method
|
777 |
$result = $dbi->select(table => 'books'); |
packaging one directory
|
778 |
|
update document
|
779 |
# select * from books where title = 'Perl'; |
removed reconnect method
|
780 |
$result = $dbi->select(table => 'books', where => {title => 1}); |
update document
|
781 |
|
782 |
# select title, author from books where id = 1 for update; |
|
783 |
$result = $dbi->select( |
|
removed register_format()
|
784 |
table => 'books', |
removed reconnect method
|
785 |
column => ['title', 'author'], |
removed register_format()
|
786 |
where => {id => 1}, |
787 |
appned => 'for update' |
|
update document
|
788 |
); |
789 |
|
|
added commit method
|
790 |
# select books.name as book_name from books, rental |
791 |
# where books.id = rental.book_id; |
|
792 |
my $result = $dbi->select( |
|
removed reconnect method
|
793 |
table => ['books', 'rental'], |
794 |
column => ['books.name as book_name'] |
|
added commit method
|
795 |
relation => {'books.id' => 'rental.book_id'} |
update document
|
796 |
); |
797 | ||
renamed default_query_filter...
|
798 |
=head2 C<build_query> |
removed reconnect method
|
799 |
|
renamed default_query_filter...
|
800 |
my $query = $dbi->build_query( |
removed DBIx::Custom commit ...
|
801 |
"select * from authors where {= name} and {= age};" |
802 |
); |
|
803 | ||
renamed default_query_filter...
|
804 |
Build the instance of L<DBIx::Custom::Query> |
805 |
using L<DBIx::Custom::QueryBuilder>. |
|
packaging one directory
|
806 | |
removed DBIx::Custom commit ...
|
807 |
=head2 C<execute> |
removed reconnect method
|
808 | |
removed DBIx::Custom commit ...
|
809 |
$result = $dbi->execute($query, param => $params, filter => {%filter}); |
renamed default_query_filter...
|
810 |
$result = $dbi->execute($source, param => $params, filter => {%filter}); |
removed reconnect method
|
811 | |
812 |
Execute the instace of L<DBIx::Custom::Query> or |
|
813 |
the string written by SQL template. |
|
814 |
Return value is the instance of L<DBIx::Custom::Result>. |
|
815 | ||
removed DBIx::Custom commit ...
|
816 |
B<Example:> |
removed reconnect method
|
817 | |
818 |
$result = $dbi->execute("select * from authors where {= name} and {= age}", |
|
819 |
param => {name => 'taro', age => 19}); |
|
820 |
|
|
821 |
while (my $row = $result->fetch) { |
|
822 |
# do something |
|
823 |
} |
|
824 | ||
removed DBIx::Custom commit ...
|
825 |
=head2 C<register_filter> |
removed reconnect method
|
826 | |
827 |
$dbi->register_filter(%filters); |
|
removed DESTROY method(not b...
|
828 |
$dbi->register_filter(\%filters); |
removed reconnect method
|
829 |
|
removed DBIx::Custom commit ...
|
830 |
Resister filter. |
831 | ||
832 |
B<Example:> |
|
packaging one directory
|
833 | |
removed reconnect method
|
834 |
$dbi->register_filter( |
835 |
encode_utf8 => sub { |
|
836 |
my $value = shift; |
|
837 |
|
|
838 |
require Encode; |
|
839 |
|
|
840 |
return Encode::encode('UTF-8', $value); |
|
841 |
}, |
|
842 |
decode_utf8 => sub { |
|
843 |
my $value = shift; |
|
844 |
|
|
845 |
require Encode; |
|
846 |
|
|
847 |
return Encode::decode('UTF-8', $value) |
|
848 |
} |
|
849 |
); |
|
850 | ||
removed DBIx::Custom commit ...
|
851 |
=head2 C<cache> |
added cache_method attribute
|
852 | |
853 |
$dbi = $dbi->cache(1); |
|
854 |
$cache = $dbi->cache; |
|
855 | ||
removed DBIx::Custom commit ...
|
856 |
Cache the result of parsing SQL template. |
added cache_method attribute
|
857 |
Default to 1. |
858 | ||
removed DBIx::Custom commit ...
|
859 |
=head2 C<cache_method> |
860 | ||
861 |
$dbi = $dbi->cache_method(\&cache_method); |
|
862 |
$cache_method = $dbi->cache_method |
|
added cache_method attribute
|
863 | |
864 |
Method for cache. |
|
865 | ||
removed DBIx::Custom commit ...
|
866 |
B<Example:> |
added cache_method attribute
|
867 | |
868 |
$dbi->cache_method( |
|
869 |
sub { |
|
870 |
my $self = shift; |
|
871 |
|
|
872 |
$self->{_cached} ||= {}; |
|
873 |
|
|
874 |
if (@_ > 1) { |
|
875 |
$self->{_cached}{$_[0]} = $_[1] |
|
876 |
} |
|
877 |
else { |
|
878 |
return $self->{_cached}{$_[0]} |
|
879 |
} |
|
880 |
} |
|
881 |
); |
|
882 | ||
removed DESTROY method(not b...
|
883 |
=head1 BUGS |
884 | ||
885 |
Please tell me bugs. |
|
886 | ||
887 |
C<< <kimoto.yuki at gmail.com> >> |
|
888 | ||
889 |
L<http://github.com/yuki-kimoto/DBIx-Custom> |
|
890 | ||
removed reconnect method
|
891 |
=head1 AUTHOR |
892 | ||
893 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
|
version 0.0901
|
894 | |
packaging one directory
|
895 |
=head1 COPYRIGHT & LICENSE |
896 | ||
897 |
Copyright 2009 Yuki Kimoto, all rights reserved. |
|
898 | ||
899 |
This program is free software; you can redistribute it and/or modify it |
|
900 |
under the same terms as Perl itself. |
|
901 | ||
902 |
=cut |
|
added cache_method attribute
|
903 | |
904 |