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