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