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