cleanup
|
1 |
package DBIx::Custom; |
2 | ||
check arguments of connect m...
|
3 |
our $VERSION = '0.1622'; |
fixed DBIx::Custom::QueryBui...
|
4 | |
5 |
use 5.008001; |
|
cleanup
|
6 |
use strict; |
7 |
use warnings; |
|
8 | ||
remove run_transaction().
|
9 |
use base 'Object::Simple'; |
many change
|
10 | |
packaging one directory
|
11 |
use Carp 'croak'; |
12 |
use DBI; |
|
13 |
use DBIx::Custom::Result; |
|
cleanup
|
14 |
use DBIx::Custom::Query; |
cleanup
|
15 |
use DBIx::Custom::QueryBuilder; |
update document
|
16 |
use Encode qw/encode_utf8 decode_utf8/; |
packaging one directory
|
17 | |
cleanup
|
18 |
__PACKAGE__->attr([qw/data_source dbh default_bind_filter |
check arguments of connect m...
|
19 |
dbi_options default_fetch_filter password user/]); |
added cache_method attribute
|
20 | |
add cache attribute
|
21 |
__PACKAGE__->attr(cache => 1); |
added cache_method attribute
|
22 |
__PACKAGE__->attr(cache_method => sub { |
23 |
sub { |
|
24 |
my $self = shift; |
|
25 |
|
|
26 |
$self->{_cached} ||= {}; |
|
27 |
|
|
28 |
if (@_ > 1) { |
|
29 |
$self->{_cached}{$_[0]} = $_[1] |
|
30 |
} |
|
31 |
else { |
|
32 |
return $self->{_cached}{$_[0]} |
|
33 |
} |
|
34 |
} |
|
35 |
}); |
|
removed register_format()
|
36 | |
cleanup (removed undocumente...
|
37 |
__PACKAGE__->attr(filters => sub { |
38 |
{ |
|
39 |
encode_utf8 => sub { encode_utf8($_[0]) }, |
|
40 |
decode_utf8 => sub { decode_utf8($_[0]) } |
|
41 |
} |
|
42 |
}); |
|
added check_filter attribute
|
43 |
__PACKAGE__->attr(filter_check => 1); |
cleanup
|
44 |
__PACKAGE__->attr(query_builder => sub {DBIx::Custom::QueryBuilder->new}); |
45 |
__PACKAGE__->attr(result_class => 'DBIx::Custom::Result'); |
|
46 | ||
47 |
# DBI methods |
|
48 |
foreach my $method (qw/begin_work commit rollback/) { |
|
49 |
my $code = sub { |
|
50 |
my $self = shift; |
|
51 |
my $ret = eval {$self->dbh->$method}; |
|
52 |
croak $@ if $@; |
|
53 |
return $ret; |
|
54 |
}; |
|
55 |
no strict 'refs'; |
|
56 |
my $pkg = __PACKAGE__; |
|
57 |
*{"${pkg}::$method"} = $code; |
|
58 |
}; |
|
59 | ||
added helper method
|
60 |
our $AUTOLOAD; |
61 | ||
62 |
sub AUTOLOAD { |
|
63 |
my $self = shift; |
|
64 | ||
65 |
# Method |
|
66 |
my ($package, $method) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/; |
|
67 | ||
68 |
# Helper |
|
69 |
$self->{_helpers} ||= {}; |
|
70 |
croak qq/Can't locate object method "$method" via "$package"/ |
|
71 |
unless my $helper = $self->{_helpers}->{$method}; |
|
72 | ||
73 |
# Run |
|
74 |
return $self->$helper(@_); |
|
75 |
} |
|
76 | ||
77 |
sub helper { |
|
78 |
my $self = shift; |
|
79 |
|
|
80 |
# Merge |
|
81 |
my $helpers = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
82 |
$self->{_helpers} = {%{$self->{_helpers} || {}}, %$helpers}; |
|
83 |
|
|
84 |
return $self; |
|
85 |
} |
|
86 | ||
check arguments of connect m...
|
87 |
#sub new { |
88 |
# my $self = shift->SUPER::new(@_); |
|
89 |
# |
|
90 |
# # Check attribute names |
|
91 |
# my @attrs = keys %$self; |
|
92 |
# foreach my $attr (@attrs) { |
|
93 |
# croak qq{"$attr" is invalid attribute name"} |
|
94 |
# unless $self->can($attr); |
|
95 |
# } |
|
96 |
# |
|
97 |
# return $self; |
|
98 |
#} |
|
99 | ||
packaging one directory
|
100 |
sub connect { |
removed register_format()
|
101 |
my $proto = shift; |
102 |
|
|
103 |
# Create |
|
104 |
my $self = ref $proto ? $proto : $proto->new(@_); |
|
update document
|
105 |
|
106 |
# Information |
|
packaging one directory
|
107 |
my $data_source = $self->data_source; |
check arguments of connect m...
|
108 |
|
109 |
croak qq{"data_source" must be specfied to connect method"} |
|
110 |
unless $data_source; |
|
111 |
|
|
packaging one directory
|
112 |
my $user = $self->user; |
113 |
my $password = $self->password; |
|
114 |
|
|
update document
|
115 |
# Connect |
select, insert, update, upda...
|
116 |
my $dbh = eval {DBI->connect( |
packaging one directory
|
117 |
$data_source, |
118 |
$user, |
|
119 |
$password, |
|
120 |
{ |
|
121 |
RaiseError => 1, |
|
122 |
PrintError => 0, |
|
123 |
AutoCommit => 1, |
|
124 |
} |
|
125 |
)}; |
|
126 |
|
|
update document
|
127 |
# Connect error |
packaging one directory
|
128 |
croak $@ if $@; |
129 |
|
|
update document
|
130 |
# Database handle |
packaging one directory
|
131 |
$self->dbh($dbh); |
update document
|
132 |
|
packaging one directory
|
133 |
return $self; |
134 |
} |
|
135 | ||
cleanup
|
136 |
sub create_query { |
137 |
my ($self, $source) = @_; |
|
update document
|
138 |
|
cleanup
|
139 |
# Cache |
140 |
my $cache = $self->cache; |
|
update document
|
141 |
|
cleanup
|
142 |
# Create query |
143 |
my $query; |
|
144 |
if ($cache) { |
|
145 |
|
|
146 |
# Get query |
|
147 |
my $q = $self->cache_method->($self, $source); |
|
148 |
|
|
149 |
# Create query |
|
150 |
$query = DBIx::Custom::Query->new($q) if $q; |
|
151 |
} |
|
152 |
|
|
153 |
unless ($query) { |
|
cleanup insert
|
154 | |
cleanup
|
155 |
# Create SQL object |
156 |
my $builder = $self->query_builder; |
|
157 |
|
|
158 |
# Create query |
|
159 |
$query = $builder->build_query($source); |
|
removed register_format()
|
160 | |
cleanup
|
161 |
# Cache query |
162 |
$self->cache_method->($self, $source, |
|
163 |
{sql => $query->sql, |
|
164 |
columns => $query->columns}) |
|
165 |
if $cache; |
|
cleanup insert
|
166 |
} |
167 |
|
|
cleanup
|
168 |
# Prepare statement handle |
169 |
my $sth; |
|
170 |
eval { $sth = $self->dbh->prepare($query->{sql})}; |
|
171 |
$self->_croak($@, qq{. SQL: "$query->{sql}"}) if $@; |
|
packaging one directory
|
172 |
|
cleanup
|
173 |
# Set statement handle |
174 |
$query->sth($sth); |
|
packaging one directory
|
175 |
|
cleanup
|
176 |
return $query; |
packaging one directory
|
177 |
} |
178 | ||
cleanup
|
179 |
our %VALID_DELETE_ARGS |
180 |
= map { $_ => 1 } qw/table where append filter allow_delete_all/; |
|
cleanup update and update_al...
|
181 | |
cleanup
|
182 |
sub delete { |
select, insert, update, upda...
|
183 |
my ($self, %args) = @_; |
cleanup update and update_al...
|
184 |
|
185 |
# Check arguments |
|
select, insert, update, upda...
|
186 |
foreach my $name (keys %args) { |
add tests
|
187 |
croak qq{"$name" is invalid argument} |
cleanup
|
188 |
unless $VALID_DELETE_ARGS{$name}; |
cleanup update and update_al...
|
189 |
} |
190 |
|
|
191 |
# Arguments |
|
select, insert, update, upda...
|
192 |
my $table = $args{table} || ''; |
193 |
my $where = $args{where} || {}; |
|
cleanup
|
194 |
my $append = $args{append}; |
select, insert, update, upda...
|
195 |
my $filter = $args{filter}; |
cleanup
|
196 |
my $allow_delete_all = $args{allow_delete_all}; |
packaging one directory
|
197 |
|
198 |
# Where keys |
|
removed register_format()
|
199 |
my @where_keys = keys %$where; |
packaging one directory
|
200 |
|
201 |
# Not exists where keys |
|
add tests
|
202 |
croak qq{"where" argument must be specified and } . |
203 |
qq{contains the pairs of column name and value} |
|
cleanup
|
204 |
if !@where_keys && !$allow_delete_all; |
packaging one directory
|
205 |
|
206 |
# Where clause |
|
207 |
my $where_clause = ''; |
|
208 |
if (@where_keys) { |
|
209 |
$where_clause = 'where '; |
|
add tests
|
210 |
$where_clause .= "{= $_} and " for @where_keys; |
packaging one directory
|
211 |
$where_clause =~ s/ and $//; |
212 |
} |
|
213 |
|
|
add tests
|
214 |
# Source of SQL |
cleanup
|
215 |
my $source = "delete from $table $where_clause"; |
add tests
|
216 |
$source .= " $append" if $append; |
packaging one directory
|
217 |
|
218 |
# Execute query |
|
cleanup
|
219 |
my $ret_val = $self->execute($source, param => $where, |
add tests
|
220 |
filter => $filter); |
packaging one directory
|
221 |
|
222 |
return $ret_val; |
|
223 |
} |
|
224 | ||
cleanup
|
225 |
sub delete_all { shift->delete(allow_delete_all => 1, @_) } |
packaging one directory
|
226 | |
added helper method
|
227 |
sub DESTROY { } |
228 | ||
cleanup
|
229 |
our %VALID_EXECUTE_ARGS = map { $_ => 1 } qw/param filter/; |
refactoring delete and delet...
|
230 | |
cleanup
|
231 |
sub execute{ |
232 |
my ($self, $query, %args) = @_; |
|
refactoring delete and delet...
|
233 |
|
234 |
# Check arguments |
|
select, insert, update, upda...
|
235 |
foreach my $name (keys %args) { |
add tests
|
236 |
croak qq{"$name" is invalid argument} |
cleanup
|
237 |
unless $VALID_EXECUTE_ARGS{$name}; |
refactoring delete and delet...
|
238 |
} |
239 |
|
|
cleanup
|
240 |
my $params = $args{param} || {}; |
packaging one directory
|
241 |
|
cleanup
|
242 |
# First argument is the soruce of SQL |
243 |
$query = $self->create_query($query) |
|
244 |
unless ref $query; |
|
packaging one directory
|
245 |
|
cleanup
|
246 |
my $filter = $args{filter} || $query->filter || {}; |
packaging one directory
|
247 |
|
cleanup
|
248 |
# Create bind value |
249 |
my $bind_values = $self->_build_bind_values($query, $params, $filter); |
|
250 |
|
|
251 |
# Execute |
|
252 |
my $sth = $query->sth; |
|
253 |
my $affected; |
|
254 |
eval {$affected = $sth->execute(@$bind_values)}; |
|
255 |
$self->_croak($@) if $@; |
|
256 |
|
|
257 |
# Return resultset if select statement is executed |
|
258 |
if ($sth->{NUM_OF_FIELDS}) { |
|
259 |
|
|
260 |
# Create result |
|
261 |
my $result = $self->result_class->new( |
|
262 |
sth => $sth, |
|
263 |
default_filter => $self->default_fetch_filter, |
|
264 |
filters => $self->filters, |
|
265 |
filter_check => $self->filter_check |
|
266 |
); |
|
267 | ||
268 |
return $result; |
|
269 |
} |
|
270 |
return $affected; |
|
271 |
} |
|
272 | ||
added experimental expand me...
|
273 |
sub expand { |
274 |
my $self = shift; |
|
275 |
my $source = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
276 |
my $table = (keys %$source)[0]; |
|
277 |
my $param = $source->{$table}; |
|
278 |
|
|
279 |
# Expand table name |
|
280 |
my $expand = {}; |
|
281 |
foreach my $column (keys %$param) { |
|
282 |
$expand->{"$table.$column"} = $param->{$column}; |
|
283 |
} |
|
284 |
|
|
285 |
return %$expand; |
|
286 |
} |
|
287 | ||
cleanup
|
288 |
our %VALID_INSERT_ARGS = map { $_ => 1 } qw/table param append filter/; |
289 | ||
290 |
sub insert { |
|
291 |
my ($self, %args) = @_; |
|
292 | ||
293 |
# Check arguments |
|
294 |
foreach my $name (keys %args) { |
|
295 |
croak qq{"$name" is invalid argument} |
|
296 |
unless $VALID_INSERT_ARGS{$name}; |
|
packaging one directory
|
297 |
} |
298 |
|
|
cleanup
|
299 |
# Arguments |
300 |
my $table = $args{table} || ''; |
|
301 |
my $param = $args{param} || {}; |
|
302 |
my $append = $args{append} || ''; |
|
303 |
my $filter = $args{filter}; |
|
304 |
|
|
305 |
# Insert keys |
|
306 |
my @insert_keys = keys %$param; |
|
307 |
|
|
308 |
# Templte for insert |
|
309 |
my $source = "insert into $table {insert_param " |
|
310 |
. join(' ', @insert_keys) . '}'; |
|
add tests
|
311 |
$source .= " $append" if $append; |
packaging one directory
|
312 |
|
313 |
# Execute query |
|
cleanup
|
314 |
my $ret_val = $self->execute($source, param => $param, |
315 |
filter => $filter); |
|
packaging one directory
|
316 |
|
317 |
return $ret_val; |
|
318 |
} |
|
319 | ||
cleanup
|
320 |
sub register_filter { |
321 |
my $invocant = shift; |
|
322 |
|
|
323 |
# Register filter |
|
324 |
my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
325 |
$invocant->filters({%{$invocant->filters}, %$filters}); |
|
326 |
|
|
327 |
return $invocant; |
|
328 |
} |
|
packaging one directory
|
329 | |
refactoring select
|
330 |
our %VALID_SELECT_ARGS |
added commit method
|
331 |
= map { $_ => 1 } qw/table column where append relation filter param/; |
refactoring select
|
332 | |
packaging one directory
|
333 |
sub select { |
select, insert, update, upda...
|
334 |
my ($self, %args) = @_; |
packaging one directory
|
335 |
|
refactoring select
|
336 |
# Check arguments |
select, insert, update, upda...
|
337 |
foreach my $name (keys %args) { |
add tests
|
338 |
croak qq{"$name" is invalid argument} |
refactoring select
|
339 |
unless $VALID_SELECT_ARGS{$name}; |
340 |
} |
|
packaging one directory
|
341 |
|
refactoring select
|
342 |
# Arguments |
select, insert, update, upda...
|
343 |
my $tables = $args{table} || []; |
removed register_format()
|
344 |
$tables = [$tables] unless ref $tables eq 'ARRAY'; |
select, insert, update, upda...
|
345 |
my $columns = $args{column} || []; |
update document
|
346 |
my $where = $args{where}; |
select, insert, update, upda...
|
347 |
my $relation = $args{relation}; |
348 |
my $append = $args{append}; |
|
349 |
my $filter = $args{filter}; |
|
packaging one directory
|
350 |
|
add tests
|
351 |
# Source of SQL |
renamed default_query_filter...
|
352 |
my $source = 'select '; |
packaging one directory
|
353 |
|
added commit method
|
354 |
# Column clause |
packaging one directory
|
355 |
if (@$columns) { |
356 |
foreach my $column (@$columns) { |
|
renamed default_query_filter...
|
357 |
$source .= "$column, "; |
packaging one directory
|
358 |
} |
renamed default_query_filter...
|
359 |
$source =~ s/, $/ /; |
packaging one directory
|
360 |
} |
361 |
else { |
|
renamed default_query_filter...
|
362 |
$source .= '* '; |
packaging one directory
|
363 |
} |
364 |
|
|
added commit method
|
365 |
# Table |
renamed default_query_filter...
|
366 |
$source .= 'from '; |
packaging one directory
|
367 |
foreach my $table (@$tables) { |
renamed default_query_filter...
|
368 |
$source .= "$table, "; |
packaging one directory
|
369 |
} |
renamed default_query_filter...
|
370 |
$source =~ s/, $/ /; |
packaging one directory
|
371 |
|
added commit method
|
372 |
# Where clause |
update document
|
373 |
my $param; |
374 |
if (ref $where eq 'HASH') { |
|
375 |
$param = $where; |
|
376 |
$source .= 'where ('; |
|
377 |
foreach my $where_key (keys %$where) { |
|
renamed default_query_filter...
|
378 |
$source .= "{= $where_key} and "; |
packaging one directory
|
379 |
} |
update document
|
380 |
$source =~ s/ and $//; |
381 |
$source .= ') '; |
|
382 |
} |
|
383 |
elsif (ref $where eq 'ARRAY') { |
|
384 |
my$where_str = $where->[0] || ''; |
|
385 |
$param = $where->[1]; |
|
386 |
|
|
387 |
$source .= "where ($where_str) "; |
|
packaging one directory
|
388 |
} |
389 |
|
|
added commit method
|
390 |
# Relation |
391 |
if ($relation) { |
|
update document
|
392 |
$source .= $where ? "and " : "where "; |
added commit method
|
393 |
foreach my $rkey (keys %$relation) { |
renamed default_query_filter...
|
394 |
$source .= "$rkey = " . $relation->{$rkey} . " and "; |
packaging one directory
|
395 |
} |
396 |
} |
|
renamed default_query_filter...
|
397 |
$source =~ s/ and $//; |
added commit method
|
398 |
|
399 |
# Append some statement |
|
renamed default_query_filter...
|
400 |
$source .= " $append" if $append; |
packaging one directory
|
401 |
|
402 |
# Execute query |
|
update document
|
403 |
my $result = $self->execute($source, param => $param, |
404 |
filter => $filter); |
|
packaging one directory
|
405 |
|
406 |
return $result; |
|
407 |
} |
|
408 | ||
cleanup
|
409 |
our %VALID_UPDATE_ARGS |
410 |
= map { $_ => 1 } qw/table param where append filter allow_update_all/; |
|
411 | ||
412 |
sub update { |
|
413 |
my ($self, %args) = @_; |
|
version 0.0901
|
414 |
|
cleanup
|
415 |
# Check arguments |
416 |
foreach my $name (keys %args) { |
|
417 |
croak qq{"$name" is invalid argument} |
|
418 |
unless $VALID_UPDATE_ARGS{$name}; |
|
removed reconnect method
|
419 |
} |
added cache_method attribute
|
420 |
|
cleanup
|
421 |
# Arguments |
422 |
my $table = $args{table} || ''; |
|
423 |
my $param = $args{param} || {}; |
|
424 |
my $where = $args{where} || {}; |
|
425 |
my $append = $args{append} || ''; |
|
426 |
my $filter = $args{filter}; |
|
427 |
my $allow_update_all = $args{allow_update_all}; |
|
version 0.0901
|
428 |
|
cleanup
|
429 |
# Update keys |
430 |
my @update_keys = keys %$param; |
|
renamed fetch_rows to fetch_...
|
431 |
|
cleanup
|
432 |
# Where keys |
433 |
my @where_keys = keys %$where; |
|
removed reconnect method
|
434 |
|
cleanup
|
435 |
# Not exists where keys |
436 |
croak qq{"where" argument must be specified and } . |
|
437 |
qq{contains the pairs of column name and value} |
|
438 |
if !@where_keys && !$allow_update_all; |
|
removed experimental registe...
|
439 |
|
cleanup
|
440 |
# Update clause |
441 |
my $update_clause = '{update_param ' . join(' ', @update_keys) . '}'; |
|
removed experimental registe...
|
442 |
|
cleanup
|
443 |
# Where clause |
444 |
my $where_clause = ''; |
|
445 |
my $new_where = {}; |
|
removed reconnect method
|
446 |
|
cleanup
|
447 |
if (@where_keys) { |
448 |
$where_clause = 'where '; |
|
449 |
$where_clause .= "{= $_} and " for @where_keys; |
|
450 |
$where_clause =~ s/ and $//; |
|
removed reconnect method
|
451 |
} |
452 |
|
|
cleanup
|
453 |
# Source of SQL |
454 |
my $source = "update $table $update_clause $where_clause"; |
|
455 |
$source .= " $append" if $append; |
|
removed reconnect method
|
456 |
|
cleanup
|
457 |
# Rearrange parameters |
458 |
foreach my $wkey (@where_keys) { |
|
removed reconnect method
|
459 |
|
cleanup
|
460 |
if (exists $param->{$wkey}) { |
461 |
$param->{$wkey} = [$param->{$wkey}] |
|
462 |
unless ref $param->{$wkey} eq 'ARRAY'; |
|
463 |
|
|
464 |
push @{$param->{$wkey}}, $where->{$wkey}; |
|
465 |
} |
|
466 |
else { |
|
467 |
$param->{$wkey} = $where->{$wkey}; |
|
468 |
} |
|
removed reconnect method
|
469 |
} |
cleanup
|
470 |
|
471 |
# Execute query |
|
472 |
my $ret_val = $self->execute($source, param => $param, |
|
473 |
filter => $filter); |
|
474 |
|
|
475 |
return $ret_val; |
|
removed reconnect method
|
476 |
} |
477 | ||
cleanup
|
478 |
sub update_all { shift->update(allow_update_all => 1, @_) }; |
479 | ||
removed reconnect method
|
480 |
sub _build_bind_values { |
481 |
my ($self, $query, $params, $filter) = @_; |
|
482 |
|
|
483 |
# binding values |
|
484 |
my @bind_values; |
|
add tests
|
485 | |
486 |
# Filter |
|
487 |
$filter ||= {}; |
|
488 |
|
|
489 |
# Parameter |
|
490 |
$params ||= {}; |
|
491 |
|
|
492 |
# Check filter |
|
493 |
$self->_check_filter($self->filters, $filter, |
|
494 |
$self->default_bind_filter, $params) |
|
495 |
if $self->filter_check; |
|
removed reconnect method
|
496 |
|
497 |
# Build bind values |
|
498 |
my $count = {}; |
|
499 |
foreach my $column (@{$query->columns}) { |
|
500 |
|
|
501 |
# Value |
|
502 |
my $value = ref $params->{$column} eq 'ARRAY' |
|
503 |
? $params->{$column}->[$count->{$column} || 0] |
|
504 |
: $params->{$column}; |
|
505 |
|
|
add tests
|
506 |
# Filtering |
renamed default_query_filter...
|
507 |
my $fname = $filter->{$column} || $self->default_bind_filter || ''; |
add tests
|
508 |
my $filter_func = $fname ? $self->filters->{$fname} : undef; |
removed reconnect method
|
509 |
push @bind_values, $filter_func |
510 |
? $filter_func->($value) |
|
511 |
: $value; |
|
512 |
|
|
513 |
# Count up |
|
514 |
$count->{$column}++; |
|
515 |
} |
|
516 |
|
|
517 |
return \@bind_values; |
|
518 |
} |
|
519 | ||
add tests
|
520 |
sub _check_filter { |
521 |
my ($self, $filters, $filter, $default_filter, $params) = @_; |
|
522 |
|
|
523 |
# Filter name not exists |
|
524 |
foreach my $fname (values %$filter) { |
|
525 |
croak qq{Bind filter "$fname" is not registered} |
|
526 |
unless exists $filters->{$fname}; |
|
527 |
} |
|
528 |
|
|
529 |
# Default filter name not exists |
|
530 |
croak qq{Default bind filter "$default_filter" is not registered} |
|
531 |
if $default_filter && ! exists $filters->{$default_filter}; |
|
532 |
|
|
533 |
# Column name not exists |
|
534 |
foreach my $column (keys %$filter) { |
|
535 |
|
|
536 |
croak qq{Column name "$column" in bind filter is not found in paramters} |
|
537 |
unless exists $params->{$column}; |
|
538 |
} |
|
539 |
} |
|
540 | ||
cleanup
|
541 |
sub _croak { |
542 |
my ($self, $error, $append) = @_; |
|
543 |
$append ||= ""; |
|
544 |
|
|
545 |
# Verbose |
|
546 |
if ($Carp::Verbose) { croak $error } |
|
547 |
|
|
548 |
# Not verbose |
|
549 |
else { |
|
550 |
|
|
551 |
# Remove line and module infromation |
|
552 |
my $at_pos = rindex($error, ' at '); |
|
553 |
$error = substr($error, 0, $at_pos); |
|
554 |
$error =~ s/\s+$//; |
|
555 |
|
|
556 |
croak "$error$append"; |
|
557 |
} |
|
558 |
} |
|
559 | ||
fixed DBIx::Custom::QueryBui...
|
560 |
1; |
561 | ||
removed reconnect method
|
562 |
=head1 NAME |
563 | ||
renamed build_query to creat...
|
564 |
DBIx::Custom - DBI interface, having hash parameter binding and filtering system |
removed reconnect method
|
565 | |
566 |
=head1 SYNOPSYS |
|
cleanup
|
567 | |
renamed build_query to creat...
|
568 |
Connect to the database. |
569 |
|
|
570 |
use DBIx::Custom; |
|
renamed update tag to update...
|
571 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname", |
removed reconnect method
|
572 |
user => 'ken', password => '!LFKD%$&'); |
cleanup
|
573 | |
renamed build_query to creat...
|
574 |
Insert, update, and delete |
cleanup
|
575 | |
removed reconnect method
|
576 |
# Insert |
577 |
$dbi->insert(table => 'books', |
|
renamed update tag to update...
|
578 |
param => {title => 'Perl', author => 'Ken'}, |
removed reconnect method
|
579 |
filter => {title => 'encode_utf8'}); |
580 |
|
|
581 |
# Update |
|
582 |
$dbi->update(table => 'books', |
|
renamed update tag to update...
|
583 |
param => {title => 'Perl', author => 'Ken'}, |
removed reconnect method
|
584 |
where => {id => 5}, |
585 |
filter => {title => 'encode_utf8'}); |
|
586 |
|
|
587 |
# Update all |
|
588 |
$dbi->update_all(table => 'books', |
|
renamed update tag to update...
|
589 |
param => {title => 'Perl'}, |
removed reconnect method
|
590 |
filter => {title => 'encode_utf8'}); |
591 |
|
|
592 |
# Delete |
|
593 |
$dbi->delete(table => 'books', |
|
594 |
where => {author => 'Ken'}, |
|
595 |
filter => {title => 'encode_utf8'}); |
|
596 |
|
|
597 |
# Delete all |
|
598 |
$dbi->delete_all(table => 'books'); |
|
cleanup
|
599 | |
renamed build_query to creat...
|
600 |
Select |
cleanup
|
601 | |
removed reconnect method
|
602 |
# Select |
603 |
my $result = $dbi->select(table => 'books'); |
|
renamed fetch_rows to fetch_...
|
604 |
|
renamed build_query to creat...
|
605 |
# Select, more complex |
renamed fetch_rows to fetch_...
|
606 |
my $result = $dbi->select( |
update document
|
607 |
table => 'books', |
608 |
column => [qw/author title/], |
|
609 |
where => {author => 'Ken'}, |
|
updated document
|
610 |
append => 'order by id limit 5', |
renamed build_query to creat...
|
611 |
filter => {title => 'encode_utf8'} |
renamed fetch_rows to fetch_...
|
612 |
); |
added commit method
|
613 |
|
renamed build_query to creat...
|
614 |
# Select, join table |
added commit method
|
615 |
my $result = $dbi->select( |
renamed build_query to creat...
|
616 |
table => ['books', 'rental'], |
617 |
column => ['books.name as book_name'] |
|
added commit method
|
618 |
relation => {'books.id' => 'rental.book_id'} |
619 |
); |
|
updated document
|
620 |
|
621 |
# Select, more flexible where |
|
622 |
my $result = $dbi->select( |
|
623 |
table => 'books', |
|
624 |
where => ['{= author} and {like title}', |
|
625 |
{author => 'Ken', title => '%Perl%'}] |
|
626 |
); |
|
cleanup
|
627 | |
renamed build_query to creat...
|
628 |
Execute SQL |
cleanup
|
629 | |
renamed build_query to creat...
|
630 |
# Execute SQL |
removed register_format()
|
631 |
$dbi->execute("select title from books"); |
632 |
|
|
renamed build_query to creat...
|
633 |
# Execute SQL with hash binding and filtering |
updated document
|
634 |
$dbi->execute("select id from books where {= author} and {like title}", |
removed register_format()
|
635 |
param => {author => 'ken', title => '%Perl%'}, |
renamed build_query to creat...
|
636 |
filter => {title => 'encode_utf8'}); |
removed reconnect method
|
637 | |
638 |
# Create query and execute it |
|
renamed build_query to creat...
|
639 |
my $query = $dbi->create_query( |
updated document
|
640 |
"select id from books where {= author} and {like title}" |
removed reconnect method
|
641 |
); |
updated document
|
642 |
$dbi->execute($query, param => {author => 'Ken', title => '%Perl%'}) |
cleanup
|
643 | |
updated document
|
644 |
Other features. |
cleanup
|
645 | |
removed register_format()
|
646 |
# Default filter |
renamed default_query_filter...
|
647 |
$dbi->default_bind_filter('encode_utf8'); |
removed register_format()
|
648 |
$dbi->default_fetch_filter('decode_utf8'); |
cleanup
|
649 | |
650 |
# Get DBI object |
|
651 |
my $dbh = $dbi->dbh; |
|
652 | ||
653 |
Fetch row. |
|
654 | ||
removed register_format()
|
655 |
# Fetch |
656 |
while (my $row = $result->fetch) { |
|
657 |
# ... |
|
658 |
} |
|
659 |
|
|
660 |
# Fetch hash |
|
661 |
while (my $row = $result->fetch_hash) { |
|
662 |
|
|
663 |
} |
|
664 |
|
|
renamed update tag to update...
|
665 |
=head1 DESCRIPTIONS |
removed reconnect method
|
666 | |
renamed build_query to creat...
|
667 |
L<DBIx::Custom> is one of L<DBI> interface modules, |
668 |
such as L<DBIx::Class>, L<DBIx::Simple>. |
|
removed reconnect method
|
669 | |
renamed build_query to creat...
|
670 |
This module is not O/R mapper. O/R mapper is useful, |
671 |
but you must learn many syntax of the O/R mapper, |
|
updated document
|
672 |
which is almost another language. |
673 |
Created SQL statement is offten not effcient and damage SQL performance. |
|
renamed build_query to creat...
|
674 |
so you have to execute raw SQL in the end. |
removed reconnect method
|
675 | |
renamed build_query to creat...
|
676 |
L<DBIx::Custom> is middle area between L<DBI> and O/R mapper. |
updated document
|
677 |
L<DBIx::Custom> provide flexible hash parameter binding and filtering system, |
added experimental expand me...
|
678 |
and suger methods, such as C<insert()>, C<update()>, C<delete()>, C<select()> |
updated document
|
679 |
to execute SQL easily. |
removed reconnect method
|
680 | |
updated document
|
681 |
L<DBIx::Custom> respects SQL. SQL is very complex and not beautiful, |
682 |
but de-facto standard, |
|
683 |
so all people learing database know it. |
|
renamed update tag to update...
|
684 |
If you already know SQL, |
685 |
you learn a little thing to use L<DBIx::Custom>. |
|
removed reconnect method
|
686 | |
added DBIx::Custom::Guides
|
687 |
See L<DBIx::Custom::Guides> for more details. |
updated document
|
688 | |
update document
|
689 |
=head1 ATTRIBUTES |
packaging one directory
|
690 | |
cleanup
|
691 |
=head2 C<cache> |
packaging one directory
|
692 | |
cleanup
|
693 |
my $cache = $dbi->cache; |
694 |
$dbi = $dbi->cache(1); |
|
removed DESTROY method(not b...
|
695 | |
cleanup
|
696 |
Enable parsed L<DBIx::Custom::Query> object caching. |
697 |
Default to 1. |
|
packaging one directory
|
698 | |
cleanup
|
699 |
=head2 C<cache_method> |
packaging one directory
|
700 | |
cleanup
|
701 |
$dbi = $dbi->cache_method(\&cache_method); |
702 |
$cache_method = $dbi->cache_method |
|
703 | ||
704 |
Method to set and get caches. |
|
705 | ||
706 |
B<Example:> |
|
707 | ||
708 |
$dbi->cache_method( |
|
709 |
sub { |
|
710 |
my $self = shift; |
|
711 |
|
|
712 |
$self->{_cached} ||= {}; |
|
713 |
|
|
714 |
if (@_ > 1) { |
|
715 |
$self->{_cached}{$_[0]} = $_[1] |
|
716 |
} |
|
717 |
else { |
|
718 |
return $self->{_cached}{$_[0]} |
|
719 |
} |
|
720 |
} |
|
721 |
); |
|
removed DESTROY method(not b...
|
722 | |
removed DBIx::Custom commit ...
|
723 |
=head2 C<data_source> |
packaging one directory
|
724 | |
cleanup
|
725 |
my $data_source = $dbi->data_source; |
cleanup
|
726 |
$dbi = $dbi->data_source("DBI:mysql:database=dbname"); |
removed DESTROY method(not b...
|
727 | |
cleanup
|
728 |
Data source. |
729 |
C<connect()> method use this value to connect the database. |
|
removed DESTROY method(not b...
|
730 | |
removed DBIx::Custom commit ...
|
731 |
=head2 C<dbh> |
packaging one directory
|
732 | |
cleanup
|
733 |
my $dbh = $dbi->dbh; |
734 |
$dbi = $dbi->dbh($dbh); |
|
packaging one directory
|
735 | |
cleanup
|
736 |
L<DBI> object. You can call all methods of L<DBI>. |
packaging one directory
|
737 | |
renamed default_query_filter...
|
738 |
=head2 C<default_bind_filter> |
packaging one directory
|
739 | |
cleanup
|
740 |
my $default_bind_filter = $dbi->default_bind_filter |
741 |
$dbi = $dbi->default_bind_filter('encode_utf8'); |
|
packaging one directory
|
742 | |
cleanup
|
743 |
Default filter when parameter binding is executed. |
packaging one directory
|
744 | |
removed DESTROY method(not b...
|
745 |
=head2 C<default_fetch_filter> |
bind_filter argument is chan...
|
746 | |
cleanup
|
747 |
my $default_fetch_filter = $dbi->default_fetch_filter; |
748 |
$dbi = $dbi->default_fetch_filter('decode_utf8'); |
|
bind_filter argument is chan...
|
749 | |
cleanup
|
750 |
Default filter when row is fetched. |
packaging one directory
|
751 | |
cleanup
|
752 |
=head2 C<filters> |
bind_filter argument is chan...
|
753 | |
cleanup
|
754 |
my $filters = $dbi->filters; |
755 |
$dbi = $dbi->filters(\%filters); |
|
packaging one directory
|
756 | |
cleanup
|
757 |
Filter functions. |
758 |
"encode_utf8" and "decode_utf8" is registered by default. |
|
759 | ||
760 |
=head2 C<filter_check> |
|
761 | ||
762 |
my $filter_check = $dbi->filter_check; |
|
763 |
$dbi = $dbi->filter_check(0); |
|
764 | ||
765 |
Enable filter check. |
|
766 |
Default to 1. |
|
767 |
This check maybe damege performance. |
|
768 |
If you require performance, set C<filter_check> attribute to 0. |
|
769 | ||
770 |
=head2 C<password> |
|
771 | ||
772 |
my $password = $dbi->password; |
|
773 |
$dbi = $dbi->password('lkj&le`@s'); |
|
774 | ||
775 |
Password. |
|
776 |
C<connect()> method use this value to connect the database. |
|
update document
|
777 | |
renamed update tag to update...
|
778 |
=head2 C<query_builder> |
added commit method
|
779 | |
renamed update tag to update...
|
780 |
my $sql_class = $dbi->query_builder; |
781 |
$dbi = $dbi->query_builder(DBIx::Custom::QueryBuilder->new); |
|
added commit method
|
782 | |
renamed update tag to update...
|
783 |
SQL builder. C<query_builder()> must be |
renamed build_query to creat...
|
784 |
the instance of L<DBIx::Custom::QueryBuilder> subclass. |
785 |
Default to L<DBIx::Custom::QueryBuilder> object. |
|
cleanup
|
786 | |
cleanup
|
787 |
=head2 C<result_class> |
cleanup
|
788 | |
cleanup
|
789 |
my $result_class = $dbi->result_class; |
790 |
$dbi = $dbi->result_class('DBIx::Custom::Result'); |
|
cleanup
|
791 | |
cleanup
|
792 |
Result class for select statement. |
793 |
Default to L<DBIx::Custom::Result>. |
|
cleanup
|
794 | |
cleanup
|
795 |
=head2 C<user> |
cleanup
|
796 | |
cleanup
|
797 |
my $user = $dbi->user; |
798 |
$dbi = $dbi->user('Ken'); |
|
cleanup
|
799 | |
cleanup
|
800 |
User name. |
801 |
C<connect()> method use this value to connect the database. |
|
802 |
|
|
803 |
=head1 METHODS |
|
added commit method
|
804 | |
cleanup
|
805 |
L<DBIx::Custom> inherits all methods from L<Object::Simple> |
806 |
and implements the following new ones. |
|
added check_filter attribute
|
807 | |
cleanup
|
808 |
=head2 begin_work |
added check_filter attribute
|
809 | |
cleanup
|
810 |
$dbi->begin_work; |
added check_filter attribute
|
811 | |
cleanup
|
812 |
Start transaction. |
813 |
This is same as L<DBI>'s C<begin_work>. |
|
added commit method
|
814 | |
cleanup
|
815 |
L<DBIx::Custom> inherits all methods from L<Object::Simple> |
816 |
and implements the following new ones. |
|
added commit method
|
817 | |
cleanup
|
818 |
=head2 commit |
819 | ||
820 |
$dbi->commit; |
|
821 | ||
822 |
Commit transaction. |
|
823 |
This is same as L<DBI>'s C<commit>. |
|
824 | ||
removed DBIx::Custom commit ...
|
825 |
=head2 C<connect> |
packaging one directory
|
826 | |
cleanup
|
827 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname", |
update document
|
828 |
user => 'ken', password => '!LFKD%$&'); |
bind_filter argument is chan...
|
829 | |
cleanup
|
830 |
Create a new L<DBIx::Custom> object and connect to the database. |
renamed build_query to creat...
|
831 |
L<DBIx::Custom> is a wrapper of L<DBI>. |
cleanup
|
832 |
C<AutoCommit> and C<RaiseError> options are true, |
renamed build_query to creat...
|
833 |
and C<PrintError> option is false by default. |
packaging one directory
|
834 | |
cleanup
|
835 |
=head2 C<create_query> |
836 |
|
|
837 |
my $query = $dbi->create_query( |
|
838 |
"select * from books where {= author} and {like title};" |
|
839 |
); |
|
update document
|
840 | |
cleanup
|
841 |
Create the instance of L<DBIx::Custom::Query> from the source of SQL. |
842 |
If you want to get high performance, |
|
843 |
use C<create_query()> method and execute it by C<execute()> method |
|
844 |
instead of suger methods. |
|
bind_filter argument is chan...
|
845 | |
cleanup
|
846 |
$dbi->execute($query, {author => 'Ken', title => '%Perl%'}); |
version 0.0901
|
847 | |
cleanup
|
848 |
=head2 C<execute> |
packaging one directory
|
849 | |
cleanup
|
850 |
my $result = $dbi->execute($query, param => $params, filter => \%filter); |
851 |
my $result = $dbi->execute($source, param => $params, filter => \%filter); |
|
update document
|
852 | |
cleanup
|
853 |
Execute query or the source of SQL. |
854 |
Query is L<DBIx::Custom::Query> object. |
|
855 |
Return value is L<DBIx::Custom::Result> if select statement is executed, |
|
856 |
or the count of affected rows if insert, update, delete statement is executed. |
|
version 0.0901
|
857 | |
removed DESTROY method(not b...
|
858 |
B<Example:> |
update document
|
859 | |
cleanup
|
860 |
my $result = $dbi->execute( |
861 |
"select * from books where {= author} and {like title}", |
|
862 |
param => {author => 'Ken', title => '%Perl%'} |
|
863 |
); |
|
864 |
|
|
865 |
while (my $row = $result->fetch) { |
|
866 |
my $author = $row->[0]; |
|
867 |
my $title = $row->[1]; |
|
868 |
} |
|
packaging one directory
|
869 | |
added experimental expand me...
|
870 |
=head2 C<(experimental) expand> |
871 | ||
872 |
my %expand = $dbi->expand($source); |
|
873 | ||
874 |
The following hash |
|
875 | ||
876 |
{books => {title => 'Perl', author => 'Ken'}} |
|
877 | ||
878 |
is expanded to |
|
879 | ||
880 |
('books.title' => 'Perl', 'books.author' => 'Ken') |
|
881 | ||
882 |
This is used in C<select()> |
|
883 | ||
884 | ||
885 |
|
|
removed DBIx::Custom commit ...
|
886 |
=head2 C<delete> |
packaging one directory
|
887 | |
cleanup
|
888 |
$dbi->delete(table => $table, |
889 |
where => \%where, |
|
890 |
append => $append, |
|
891 |
filter => \%filter); |
|
bind_filter argument is chan...
|
892 | |
renamed build_query to creat...
|
893 |
Execute delete statement. |
894 |
C<delete> method have C<table>, C<where>, C<append>, and C<filter> arguments. |
|
895 |
C<table> is a table name. |
|
896 |
C<where> is where clause. this must be hash reference. |
|
897 |
C<append> is a string added at the end of the SQL statement. |
|
898 |
C<filter> is filters when parameter binding is executed. |
|
cleanup
|
899 |
Return value of C<delete()> is the count of affected rows. |
renamed build_query to creat...
|
900 | |
removed DESTROY method(not b...
|
901 |
B<Example:> |
packaging one directory
|
902 | |
removed register_format()
|
903 |
$dbi->delete(table => 'books', |
904 |
where => {id => 5}, |
|
905 |
append => 'some statement', |
|
removed reconnect method
|
906 |
filter => {id => 'encode_utf8'}); |
version 0.0901
|
907 | |
removed DBIx::Custom commit ...
|
908 |
=head2 C<delete_all> |
packaging one directory
|
909 | |
cleanup
|
910 |
$dbi->delete_all(table => $table); |
packaging one directory
|
911 | |
renamed build_query to creat...
|
912 |
Execute delete statement to delete all rows. |
913 |
Arguments is same as C<delete> method, |
|
914 |
except that C<delete_all> don't have C<where> argument. |
|
cleanup
|
915 |
Return value of C<delete_all()> is the count of affected rows. |
bind_filter argument is chan...
|
916 | |
removed DESTROY method(not b...
|
917 |
B<Example:> |
removed register_format()
|
918 |
|
removed reconnect method
|
919 |
$dbi->delete_all(table => 'books'); |
packaging one directory
|
920 | |
added helper method
|
921 |
=head2 C<(experimental) helper> |
922 | ||
923 |
$dbi->helper( |
|
924 |
update_or_insert => sub { |
|
925 |
my $self = shift; |
|
926 |
# do something |
|
927 |
}, |
|
928 |
find_or_create => sub { |
|
929 |
my $self = shift; |
|
930 |
# do something |
|
931 |
} |
|
932 |
); |
|
933 | ||
934 |
Register helper methods. These method is called from L<DBIx::Custom> object directory. |
|
935 | ||
936 |
$dbi->update_or_insert; |
|
937 |
$dbi->find_or_create; |
|
938 | ||
cleanup
|
939 |
=head2 C<insert> |
940 | ||
941 |
$dbi->insert(table => $table, |
|
942 |
param => \%param, |
|
943 |
append => $append, |
|
944 |
filter => \%filter); |
|
945 | ||
946 |
Execute insert statement. |
|
947 |
C<insert> method have C<table>, C<param>, C<append> |
|
948 |
and C<filter> arguments. |
|
949 |
C<table> is a table name. |
|
950 |
C<param> is the pairs of column name value. this must be hash reference. |
|
951 |
C<append> is a string added at the end of the SQL statement. |
|
952 |
C<filter> is filters when parameter binding is executed. |
|
953 |
This is overwrites C<default_bind_filter>. |
|
954 |
Return value of C<insert()> is the count of affected rows. |
|
955 | ||
956 |
B<Example:> |
|
957 | ||
958 |
$dbi->insert(table => 'books', |
|
959 |
param => {title => 'Perl', author => 'Taro'}, |
|
960 |
append => "some statement", |
|
961 |
filter => {title => 'encode_utf8'}) |
|
962 | ||
963 |
=head2 C<register_filter> |
|
964 | ||
965 |
$dbi->register_filter(%filters); |
|
966 |
$dbi->register_filter(\%filters); |
|
967 |
|
|
968 |
Register filter. Registered filters is available in the following attributes |
|
969 |
or arguments. |
|
970 | ||
971 |
=over 4 |
|
972 | ||
973 |
=item * |
|
974 | ||
975 |
C<default_bind_filter>, C<default_fetch_filter> |
|
976 | ||
977 |
=item * |
|
978 | ||
979 |
C<filter> argument of C<insert()>, C<update()>, |
|
980 |
C<update_all()>, C<delete()>, C<delete_all()>, C<select()> |
|
981 |
methods |
|
982 | ||
983 |
=item * |
|
984 | ||
985 |
C<execute()> method |
|
986 | ||
987 |
=item * |
|
988 | ||
989 |
C<default_filter> and C<filter> of C<DBIx::Custom::Query> |
|
990 | ||
991 |
=item * |
|
992 | ||
993 |
C<default_filter> and C<filter> of C<DBIx::Custom::Result> |
|
994 | ||
995 |
=back |
|
996 | ||
997 |
B<Example:> |
|
998 | ||
999 |
$dbi->register_filter( |
|
1000 |
encode_utf8 => sub { |
|
1001 |
my $value = shift; |
|
1002 |
|
|
1003 |
require Encode; |
|
1004 |
|
|
1005 |
return Encode::encode('UTF-8', $value); |
|
1006 |
}, |
|
1007 |
decode_utf8 => sub { |
|
1008 |
my $value = shift; |
|
1009 |
|
|
1010 |
require Encode; |
|
1011 |
|
|
1012 |
return Encode::decode('UTF-8', $value) |
|
1013 |
} |
|
1014 |
); |
|
1015 | ||
1016 |
=head2 rollback |
|
1017 | ||
1018 |
$dbi->rollback; |
|
1019 | ||
1020 |
Rollback transaction. |
|
1021 |
This is same as L<DBI>'s C<rollback>. |
|
1022 | ||
removed DBIx::Custom commit ...
|
1023 |
=head2 C<select> |
packaging one directory
|
1024 |
|
cleanup
|
1025 |
my $result = $dbi->select(table => $table, |
1026 |
column => [@column], |
|
1027 |
where => \%where, |
|
1028 |
append => $append, |
|
1029 |
relation => \%relation, |
|
1030 |
filter => \%filter); |
|
update document
|
1031 | |
renamed build_query to creat...
|
1032 |
Execute select statement. |
cleanup
|
1033 |
C<select> method have C<table>, C<column>, C<where>, C<append>, |
renamed build_query to creat...
|
1034 |
C<relation> and C<filter> arguments. |
1035 |
C<table> is a table name. |
|
cleanup
|
1036 |
C<where> is where clause. this is normally hash reference. |
renamed build_query to creat...
|
1037 |
C<append> is a string added at the end of the SQL statement. |
1038 |
C<filter> is filters when parameter binding is executed. |
|
update document
|
1039 | |
removed DESTROY method(not b...
|
1040 |
B<Example:> |
update document
|
1041 | |
added commit method
|
1042 |
# select * from books; |
cleanup
|
1043 |
my $result = $dbi->select(table => 'books'); |
packaging one directory
|
1044 |
|
renamed build_query to creat...
|
1045 |
# select * from books where title = ?; |
1046 |
my $result = $dbi->select(table => 'books', where => {title => 'Perl'}); |
|
update document
|
1047 |
|
renamed build_query to creat...
|
1048 |
# select title, author from books where id = ? for update; |
cleanup
|
1049 |
my $result = $dbi->select( |
removed register_format()
|
1050 |
table => 'books', |
removed reconnect method
|
1051 |
column => ['title', 'author'], |
removed register_format()
|
1052 |
where => {id => 1}, |
1053 |
appned => 'for update' |
|
update document
|
1054 |
); |
1055 |
|
|
renamed update tag to update...
|
1056 |
# select books.name as book_name from books, rental |
added commit method
|
1057 |
# where books.id = rental.book_id; |
1058 |
my $result = $dbi->select( |
|
removed reconnect method
|
1059 |
table => ['books', 'rental'], |
1060 |
column => ['books.name as book_name'] |
|
added commit method
|
1061 |
relation => {'books.id' => 'rental.book_id'} |
update document
|
1062 |
); |
1063 | ||
cleanup
|
1064 |
If you use more complex condition, |
1065 |
you can specify a array reference to C<where> argument. |
|
1066 | ||
1067 |
my $result = $dbi->select( |
|
1068 |
table => 'books', |
|
1069 |
column => ['title', 'author'], |
|
1070 |
where => ['{= title} or {like author}', |
|
1071 |
{title => '%Perl%', author => 'Ken'}] |
|
1072 |
); |
|
1073 | ||
1074 |
First element is a string. it contains tags, |
|
1075 |
such as "{= title} or {like author}". |
|
1076 |
Second element is paramters. |
|
1077 | ||
cleanup
|
1078 |
=head2 C<update> |
removed reconnect method
|
1079 | |
cleanup
|
1080 |
$dbi->update(table => $table, |
1081 |
param => \%params, |
|
1082 |
where => \%where, |
|
1083 |
append => $append, |
|
1084 |
filter => \%filter) |
|
removed reconnect method
|
1085 | |
cleanup
|
1086 |
Execute update statement. |
1087 |
C<update> method have C<table>, C<param>, C<where>, C<append> |
|
1088 |
and C<filter> arguments. |
|
1089 |
C<table> is a table name. |
|
1090 |
C<param> is column-value pairs. this must be hash reference. |
|
1091 |
C<where> is where clause. this must be hash reference. |
|
1092 |
C<append> is a string added at the end of the SQL statement. |
|
1093 |
C<filter> is filters when parameter binding is executed. |
|
1094 |
This is overwrites C<default_bind_filter>. |
|
1095 |
Return value of C<update()> is the count of affected rows. |
|
removed reconnect method
|
1096 | |
removed DBIx::Custom commit ...
|
1097 |
B<Example:> |
removed reconnect method
|
1098 | |
cleanup
|
1099 |
$dbi->update(table => 'books', |
1100 |
param => {title => 'Perl', author => 'Taro'}, |
|
1101 |
where => {id => 5}, |
|
1102 |
append => "some statement", |
|
1103 |
filter => {title => 'encode_utf8'}); |
|
renamed build_query to creat...
|
1104 | |
cleanup
|
1105 |
=head2 C<update_all> |
renamed build_query to creat...
|
1106 | |
cleanup
|
1107 |
$dbi->update_all(table => $table, |
1108 |
param => \%params, |
|
1109 |
filter => \%filter, |
|
1110 |
append => $append); |
|
renamed build_query to creat...
|
1111 | |
cleanup
|
1112 |
Execute update statement to update all rows. |
1113 |
Arguments is same as C<update> method, |
|
1114 |
except that C<update_all> don't have C<where> argument. |
|
1115 |
Return value of C<update_all()> is the count of affected rows. |
|
removed DBIx::Custom commit ...
|
1116 | |
1117 |
B<Example:> |
|
packaging one directory
|
1118 | |
cleanup
|
1119 |
$dbi->update_all(table => 'books', |
1120 |
param => {author => 'taro'}, |
|
1121 |
filter => {author => 'encode_utf8'}); |
|
removed reconnect method
|
1122 | |
DBIx::Custom is now stable
|
1123 |
=head1 STABILITY |
1124 | ||
1125 |
L<DBIx::Custom> is now stable. APIs keep backword compatible in the feature. |
|
1126 | ||
removed DESTROY method(not b...
|
1127 |
=head1 BUGS |
1128 | ||
renamed build_query to creat...
|
1129 |
Please tell me bugs if found. |
removed DESTROY method(not b...
|
1130 | |
1131 |
C<< <kimoto.yuki at gmail.com> >> |
|
1132 | ||
1133 |
L<http://github.com/yuki-kimoto/DBIx-Custom> |
|
1134 | ||
removed reconnect method
|
1135 |
=head1 AUTHOR |
1136 | ||
1137 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
|
version 0.0901
|
1138 | |
packaging one directory
|
1139 |
=head1 COPYRIGHT & LICENSE |
1140 | ||
1141 |
Copyright 2009 Yuki Kimoto, all rights reserved. |
|
1142 | ||
1143 |
This program is free software; you can redistribute it and/or modify it |
|
1144 |
under the same terms as Perl itself. |
|
1145 | ||
1146 |
=cut |
|
added cache_method attribute
|
1147 | |
1148 |