cleanup
|
1 |
package DBIx::Custom; |
2 | ||
add examples
|
3 |
our $VERSION = '0.1628'; |
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 | ||
added insert, update, update...
|
609 |
sub txn_scope { |
610 |
my $self = shift; |
|
611 |
|
|
612 |
require DBIx::TransactionManager; |
|
613 |
|
|
614 |
$self->{_transaction_manager} |
|
615 |
||= DBIx::TransactionManager->new($self->dbh); |
|
616 |
|
|
617 |
return $self->{_transaction_manager}->txn_scope; |
|
618 |
} |
|
619 | ||
cleanup
|
620 |
our %VALID_UPDATE_ARGS |
added auto_filter method
|
621 |
= map { $_ => 1 } qw/auto_filter_table table param |
622 |
where append filter allow_update_all/; |
|
cleanup
|
623 | |
624 |
sub update { |
|
625 |
my ($self, %args) = @_; |
|
version 0.0901
|
626 |
|
cleanup
|
627 |
# Check arguments |
628 |
foreach my $name (keys %args) { |
|
629 |
croak qq{"$name" is invalid argument} |
|
630 |
unless $VALID_UPDATE_ARGS{$name}; |
|
removed reconnect method
|
631 |
} |
added cache_method attribute
|
632 |
|
cleanup
|
633 |
# Arguments |
634 |
my $table = $args{table} || ''; |
|
635 |
my $param = $args{param} || {}; |
|
636 |
my $where = $args{where} || {}; |
|
637 |
my $append = $args{append} || ''; |
|
638 |
my $filter = $args{filter}; |
|
639 |
my $allow_update_all = $args{allow_update_all}; |
|
version 0.0901
|
640 |
|
added auto_filter method
|
641 |
my $auto_filter_table = exists $args{auto_filter_table} |
642 |
? $args{auto_filter_table} |
|
643 |
: [$table]; |
|
644 |
$auto_filter_table ||= []; |
|
645 |
|
|
cleanup
|
646 |
# Update keys |
647 |
my @update_keys = keys %$param; |
|
renamed fetch_rows to fetch_...
|
648 |
|
cleanup
|
649 |
# Where keys |
650 |
my @where_keys = keys %$where; |
|
removed reconnect method
|
651 |
|
cleanup
|
652 |
# Not exists where keys |
653 |
croak qq{"where" argument must be specified and } . |
|
654 |
qq{contains the pairs of column name and value} |
|
655 |
if !@where_keys && !$allow_update_all; |
|
removed experimental registe...
|
656 |
|
cleanup
|
657 |
# Update clause |
658 |
my $update_clause = '{update_param ' . join(' ', @update_keys) . '}'; |
|
removed experimental registe...
|
659 |
|
cleanup
|
660 |
# Where clause |
661 |
my $where_clause = ''; |
|
662 |
my $new_where = {}; |
|
removed reconnect method
|
663 |
|
cleanup
|
664 |
if (@where_keys) { |
665 |
$where_clause = 'where '; |
|
666 |
$where_clause .= "{= $_} and " for @where_keys; |
|
667 |
$where_clause =~ s/ and $//; |
|
removed reconnect method
|
668 |
} |
669 |
|
|
cleanup
|
670 |
# Source of SQL |
671 |
my $source = "update $table $update_clause $where_clause"; |
|
672 |
$source .= " $append" if $append; |
|
removed reconnect method
|
673 |
|
cleanup
|
674 |
# Rearrange parameters |
675 |
foreach my $wkey (@where_keys) { |
|
removed reconnect method
|
676 |
|
cleanup
|
677 |
if (exists $param->{$wkey}) { |
678 |
$param->{$wkey} = [$param->{$wkey}] |
|
679 |
unless ref $param->{$wkey} eq 'ARRAY'; |
|
680 |
|
|
681 |
push @{$param->{$wkey}}, $where->{$wkey}; |
|
682 |
} |
|
683 |
else { |
|
684 |
$param->{$wkey} = $where->{$wkey}; |
|
685 |
} |
|
removed reconnect method
|
686 |
} |
cleanup
|
687 |
|
688 |
# Execute query |
|
689 |
my $ret_val = $self->execute($source, param => $param, |
|
added auto_filter method
|
690 |
filter => $filter, |
691 |
auto_filter_table => $auto_filter_table); |
|
cleanup
|
692 |
|
693 |
return $ret_val; |
|
removed reconnect method
|
694 |
} |
695 | ||
cleanup
|
696 |
sub update_all { shift->update(allow_update_all => 1, @_) }; |
697 | ||
removed reconnect method
|
698 |
sub _build_bind_values { |
699 |
my ($self, $query, $params, $filter) = @_; |
|
700 |
|
|
701 |
# binding values |
|
702 |
my @bind_values; |
|
add tests
|
703 | |
704 |
# Filter |
|
705 |
$filter ||= {}; |
|
706 |
|
|
707 |
# Parameter |
|
708 |
$params ||= {}; |
|
709 |
|
|
removed reconnect method
|
710 |
# Build bind values |
711 |
my $count = {}; |
|
712 |
foreach my $column (@{$query->columns}) { |
|
713 |
|
|
714 |
# Value |
|
715 |
my $value = ref $params->{$column} eq 'ARRAY' |
|
716 |
? $params->{$column}->[$count->{$column} || 0] |
|
717 |
: $params->{$column}; |
|
718 |
|
|
add tests
|
719 |
# Filtering |
cleanup
|
720 |
my $f = $filter->{$column} || $self->{default_bind_filter} || ''; |
cleanup
|
721 |
|
cleanup
|
722 |
push @bind_values, $f ? $f->($value) : $value; |
removed reconnect method
|
723 |
|
724 |
# Count up |
|
725 |
$count->{$column}++; |
|
726 |
} |
|
727 |
|
|
728 |
return \@bind_values; |
|
729 |
} |
|
730 | ||
cleanup
|
731 |
sub _croak { |
732 |
my ($self, $error, $append) = @_; |
|
733 |
$append ||= ""; |
|
734 |
|
|
735 |
# Verbose |
|
736 |
if ($Carp::Verbose) { croak $error } |
|
737 |
|
|
738 |
# Not verbose |
|
739 |
else { |
|
740 |
|
|
741 |
# Remove line and module infromation |
|
742 |
my $at_pos = rindex($error, ' at '); |
|
743 |
$error = substr($error, 0, $at_pos); |
|
744 |
$error =~ s/\s+$//; |
|
745 |
|
|
746 |
croak "$error$append"; |
|
747 |
} |
|
748 |
} |
|
749 | ||
fixed DBIx::Custom::QueryBui...
|
750 |
1; |
751 | ||
removed reconnect method
|
752 |
=head1 NAME |
753 | ||
renamed build_query to creat...
|
754 |
DBIx::Custom - DBI interface, having hash parameter binding and filtering system |
removed reconnect method
|
755 | |
756 |
=head1 SYNOPSYS |
|
cleanup
|
757 | |
renamed build_query to creat...
|
758 |
Connect to the database. |
759 |
|
|
760 |
use DBIx::Custom; |
|
renamed update tag to update...
|
761 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname", |
removed reconnect method
|
762 |
user => 'ken', password => '!LFKD%$&'); |
cleanup
|
763 | |
renamed build_query to creat...
|
764 |
Insert, update, and delete |
cleanup
|
765 | |
removed reconnect method
|
766 |
# Insert |
added insert, update, update...
|
767 |
$dbi->insert(table => 'book', |
renamed update tag to update...
|
768 |
param => {title => 'Perl', author => 'Ken'}, |
removed reconnect method
|
769 |
filter => {title => 'encode_utf8'}); |
770 |
|
|
771 |
# Update |
|
added insert, update, update...
|
772 |
$dbi->update(table => 'book', |
renamed update tag to update...
|
773 |
param => {title => 'Perl', author => 'Ken'}, |
removed reconnect method
|
774 |
where => {id => 5}, |
775 |
filter => {title => 'encode_utf8'}); |
|
776 |
|
|
777 |
# Update all |
|
added insert, update, update...
|
778 |
$dbi->update_all(table => 'book', |
renamed update tag to update...
|
779 |
param => {title => 'Perl'}, |
removed reconnect method
|
780 |
filter => {title => 'encode_utf8'}); |
781 |
|
|
782 |
# Delete |
|
added insert, update, update...
|
783 |
$dbi->delete(table => 'book', |
removed reconnect method
|
784 |
where => {author => 'Ken'}, |
785 |
filter => {title => 'encode_utf8'}); |
|
786 |
|
|
787 |
# Delete all |
|
added insert, update, update...
|
788 |
$dbi->delete_all(table => 'book'); |
cleanup
|
789 | |
renamed build_query to creat...
|
790 |
Select |
cleanup
|
791 | |
removed reconnect method
|
792 |
# Select |
added insert, update, update...
|
793 |
my $result = $dbi->select(table => 'book'); |
renamed fetch_rows to fetch_...
|
794 |
|
renamed build_query to creat...
|
795 |
# Select, more complex |
renamed fetch_rows to fetch_...
|
796 |
my $result = $dbi->select( |
added insert, update, update...
|
797 |
table => 'book', |
update document
|
798 |
column => [qw/author title/], |
799 |
where => {author => 'Ken'}, |
|
updated document
|
800 |
append => 'order by id limit 5', |
renamed build_query to creat...
|
801 |
filter => {title => 'encode_utf8'} |
renamed fetch_rows to fetch_...
|
802 |
); |
added commit method
|
803 |
|
renamed build_query to creat...
|
804 |
# Select, join table |
added commit method
|
805 |
my $result = $dbi->select( |
added insert, update, update...
|
806 |
table => ['book', 'rental'], |
807 |
column => ['book.name as book_name'] |
|
808 |
relation => {'book.id' => 'rental.book_id'} |
|
added commit method
|
809 |
); |
updated document
|
810 |
|
811 |
# Select, more flexible where |
|
812 |
my $result = $dbi->select( |
|
added insert, update, update...
|
813 |
table => 'book', |
updated document
|
814 |
where => ['{= author} and {like title}', |
815 |
{author => 'Ken', title => '%Perl%'}] |
|
816 |
); |
|
cleanup
|
817 | |
renamed build_query to creat...
|
818 |
Execute SQL |
cleanup
|
819 | |
renamed build_query to creat...
|
820 |
# Execute SQL |
added insert, update, update...
|
821 |
$dbi->execute("select title from book"); |
removed register_format()
|
822 |
|
renamed build_query to creat...
|
823 |
# Execute SQL with hash binding and filtering |
added insert, update, update...
|
824 |
$dbi->execute("select id from book where {= author} and {like title}", |
removed register_format()
|
825 |
param => {author => 'ken', title => '%Perl%'}, |
renamed build_query to creat...
|
826 |
filter => {title => 'encode_utf8'}); |
removed reconnect method
|
827 | |
828 |
# Create query and execute it |
|
renamed build_query to creat...
|
829 |
my $query = $dbi->create_query( |
added insert, update, update...
|
830 |
"select id from book where {= author} and {like title}" |
removed reconnect method
|
831 |
); |
updated document
|
832 |
$dbi->execute($query, param => {author => 'Ken', title => '%Perl%'}) |
cleanup
|
833 | |
updated document
|
834 |
Other features. |
cleanup
|
835 | |
836 |
# Get DBI object |
|
837 |
my $dbh = $dbi->dbh; |
|
838 | ||
839 |
Fetch row. |
|
840 | ||
removed register_format()
|
841 |
# Fetch |
842 |
while (my $row = $result->fetch) { |
|
843 |
# ... |
|
844 |
} |
|
845 |
|
|
846 |
# Fetch hash |
|
847 |
while (my $row = $result->fetch_hash) { |
|
848 |
|
|
849 |
} |
|
850 |
|
|
renamed update tag to update...
|
851 |
=head1 DESCRIPTIONS |
removed reconnect method
|
852 | |
renamed build_query to creat...
|
853 |
L<DBIx::Custom> is one of L<DBI> interface modules, |
854 |
such as L<DBIx::Class>, L<DBIx::Simple>. |
|
removed reconnect method
|
855 | |
renamed build_query to creat...
|
856 |
This module is not O/R mapper. O/R mapper is useful, |
857 |
but you must learn many syntax of the O/R mapper, |
|
updated document
|
858 |
which is almost another language. |
859 |
Created SQL statement is offten not effcient and damage SQL performance. |
|
renamed build_query to creat...
|
860 |
so you have to execute raw SQL in the end. |
removed reconnect method
|
861 | |
renamed build_query to creat...
|
862 |
L<DBIx::Custom> is middle area between L<DBI> and O/R mapper. |
updated document
|
863 |
L<DBIx::Custom> provide flexible hash parameter binding and filtering system, |
added experimental expand me...
|
864 |
and suger methods, such as C<insert()>, C<update()>, C<delete()>, C<select()> |
updated document
|
865 |
to execute SQL easily. |
removed reconnect method
|
866 | |
updated document
|
867 |
L<DBIx::Custom> respects SQL. SQL is very complex and not beautiful, |
868 |
but de-facto standard, |
|
869 |
so all people learing database know it. |
|
renamed update tag to update...
|
870 |
If you already know SQL, |
871 |
you learn a little thing to use L<DBIx::Custom>. |
|
removed reconnect method
|
872 | |
added DBIx::Custom::Guides
|
873 |
See L<DBIx::Custom::Guides> for more details. |
updated document
|
874 | |
update document
|
875 |
=head1 ATTRIBUTES |
packaging one directory
|
876 | |
cleanup
|
877 |
=head2 C<cache> |
packaging one directory
|
878 | |
cleanup
|
879 |
my $cache = $dbi->cache; |
880 |
$dbi = $dbi->cache(1); |
|
removed DESTROY method(not b...
|
881 | |
cleanup
|
882 |
Enable parsed L<DBIx::Custom::Query> object caching. |
883 |
Default to 1. |
|
packaging one directory
|
884 | |
cleanup
|
885 |
=head2 C<cache_method> |
packaging one directory
|
886 | |
cleanup
|
887 |
$dbi = $dbi->cache_method(\&cache_method); |
888 |
$cache_method = $dbi->cache_method |
|
889 | ||
890 |
Method to set and get caches. |
|
891 | ||
892 |
B<Example:> |
|
893 | ||
894 |
$dbi->cache_method( |
|
895 |
sub { |
|
896 |
my $self = shift; |
|
897 |
|
|
898 |
$self->{_cached} ||= {}; |
|
899 |
|
|
900 |
if (@_ > 1) { |
|
901 |
$self->{_cached}{$_[0]} = $_[1] |
|
902 |
} |
|
903 |
else { |
|
904 |
return $self->{_cached}{$_[0]} |
|
905 |
} |
|
906 |
} |
|
907 |
); |
|
removed DESTROY method(not b...
|
908 | |
removed DBIx::Custom commit ...
|
909 |
=head2 C<data_source> |
packaging one directory
|
910 | |
cleanup
|
911 |
my $data_source = $dbi->data_source; |
cleanup
|
912 |
$dbi = $dbi->data_source("DBI:mysql:database=dbname"); |
removed DESTROY method(not b...
|
913 | |
cleanup
|
914 |
Data source. |
915 |
C<connect()> method use this value to connect the database. |
|
removed DESTROY method(not b...
|
916 | |
removed DBIx::Custom commit ...
|
917 |
=head2 C<dbh> |
packaging one directory
|
918 | |
cleanup
|
919 |
my $dbh = $dbi->dbh; |
920 |
$dbi = $dbi->dbh($dbh); |
|
packaging one directory
|
921 | |
cleanup
|
922 |
L<DBI> object. You can call all methods of L<DBI>. |
packaging one directory
|
923 | |
added dbi_options attribute
|
924 |
=head2 C<dbi_options> |
925 | ||
926 |
my $dbi_options = $dbi->dbi_options; |
|
927 |
$dbi = $dbi->dbi_options($dbi_options); |
|
928 | ||
929 |
DBI options. |
|
930 |
C<connect()> method use this value to connect the database. |
|
931 | ||
cleanup
|
932 |
Default filter when row is fetched. |
packaging one directory
|
933 | |
cleanup
|
934 |
=head2 C<filters> |
bind_filter argument is chan...
|
935 | |
cleanup
|
936 |
my $filters = $dbi->filters; |
937 |
$dbi = $dbi->filters(\%filters); |
|
packaging one directory
|
938 | |
cleanup
|
939 |
Filter functions. |
940 |
"encode_utf8" and "decode_utf8" is registered by default. |
|
941 | ||
942 |
=head2 C<filter_check> |
|
943 | ||
944 |
my $filter_check = $dbi->filter_check; |
|
945 |
$dbi = $dbi->filter_check(0); |
|
946 | ||
cleanup
|
947 |
B<this attribute is now deprecated and has no mean |
948 |
because check is always done>. |
|
cleanup
|
949 | |
950 |
=head2 C<password> |
|
951 | ||
952 |
my $password = $dbi->password; |
|
953 |
$dbi = $dbi->password('lkj&le`@s'); |
|
954 | ||
955 |
Password. |
|
956 |
C<connect()> method use this value to connect the database. |
|
update document
|
957 | |
renamed update tag to update...
|
958 |
=head2 C<query_builder> |
added commit method
|
959 | |
renamed update tag to update...
|
960 |
my $sql_class = $dbi->query_builder; |
961 |
$dbi = $dbi->query_builder(DBIx::Custom::QueryBuilder->new); |
|
added commit method
|
962 | |
renamed update tag to update...
|
963 |
SQL builder. C<query_builder()> must be |
renamed build_query to creat...
|
964 |
the instance of L<DBIx::Custom::QueryBuilder> subclass. |
965 |
Default to L<DBIx::Custom::QueryBuilder> object. |
|
cleanup
|
966 | |
cleanup
|
967 |
=head2 C<result_class> |
cleanup
|
968 | |
cleanup
|
969 |
my $result_class = $dbi->result_class; |
970 |
$dbi = $dbi->result_class('DBIx::Custom::Result'); |
|
cleanup
|
971 | |
cleanup
|
972 |
Result class for select statement. |
973 |
Default to L<DBIx::Custom::Result>. |
|
cleanup
|
974 | |
cleanup
|
975 |
=head2 C<user> |
cleanup
|
976 | |
cleanup
|
977 |
my $user = $dbi->user; |
978 |
$dbi = $dbi->user('Ken'); |
|
cleanup
|
979 | |
cleanup
|
980 |
User name. |
981 |
C<connect()> method use this value to connect the database. |
|
982 |
|
|
983 |
=head1 METHODS |
|
added commit method
|
984 | |
cleanup
|
985 |
L<DBIx::Custom> inherits all methods from L<Object::Simple> |
986 |
and implements the following new ones. |
|
added check_filter attribute
|
987 | |
added auto_filter method
|
988 |
=head2 C<(experimental) auto_filter > |
989 | ||
990 |
$dbi->auto_filter( |
|
991 |
$table, |
|
992 |
[$column1, $bind_filter1, $fetch_filter1], |
|
993 |
[$column2, $bind_filter2, $fetch_filter2], |
|
994 |
[...], |
|
995 |
); |
|
996 | ||
997 |
C<auto_filter> is automatically filter for columns of table. |
|
998 |
This have effect C<insert>, C<update>, C<delete>. C<select> |
|
cleanup
|
999 |
and L<DBIx::Custom::Result> object. but this has'nt C<execute> method. |
1000 | ||
1001 |
If you want to have effect <execute< method, use C<auto_filter_table> |
|
1002 |
arguments. |
|
added auto_filter method
|
1003 | |
cleanup
|
1004 |
$result = $dbi->execute( |
1005 |
"select * from table1 where {= key1} and {= key2};", |
|
1006 |
param => {key1 => 1, key2 => 2}, |
|
1007 |
auto_filter_table => ['table1'] |
|
1008 |
); |
|
1009 |
|
|
added auto_filter method
|
1010 |
B<Example:> |
1011 | ||
added insert, update, update...
|
1012 |
$dbi->auto_filter('book', 'sale_date', 'to_date', 'date_to'); |
added auto_filter method
|
1013 | |
1014 |
=head2 C<begin_work> |
|
added check_filter attribute
|
1015 | |
cleanup
|
1016 |
$dbi->begin_work; |
added check_filter attribute
|
1017 | |
cleanup
|
1018 |
Start transaction. |
1019 |
This is same as L<DBI>'s C<begin_work>. |
|
added commit method
|
1020 | |
cleanup
|
1021 |
L<DBIx::Custom> inherits all methods from L<Object::Simple> |
1022 |
and implements the following new ones. |
|
added commit method
|
1023 | |
added auto_filter method
|
1024 |
=head2 C<commit> |
cleanup
|
1025 | |
1026 |
$dbi->commit; |
|
1027 | ||
1028 |
Commit transaction. |
|
1029 |
This is same as L<DBI>'s C<commit>. |
|
1030 | ||
removed DBIx::Custom commit ...
|
1031 |
=head2 C<connect> |
packaging one directory
|
1032 | |
cleanup
|
1033 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname", |
update document
|
1034 |
user => 'ken', password => '!LFKD%$&'); |
bind_filter argument is chan...
|
1035 | |
cleanup
|
1036 |
Create a new L<DBIx::Custom> object and connect to the database. |
renamed build_query to creat...
|
1037 |
L<DBIx::Custom> is a wrapper of L<DBI>. |
cleanup
|
1038 |
C<AutoCommit> and C<RaiseError> options are true, |
renamed build_query to creat...
|
1039 |
and C<PrintError> option is false by default. |
packaging one directory
|
1040 | |
cleanup
|
1041 |
=head2 C<create_query> |
1042 |
|
|
1043 |
my $query = $dbi->create_query( |
|
added insert, update, update...
|
1044 |
"select * from book where {= author} and {like title};" |
cleanup
|
1045 |
); |
update document
|
1046 | |
cleanup
|
1047 |
Create the instance of L<DBIx::Custom::Query> from the source of SQL. |
1048 |
If you want to get high performance, |
|
1049 |
use C<create_query()> method and execute it by C<execute()> method |
|
1050 |
instead of suger methods. |
|
bind_filter argument is chan...
|
1051 | |
cleanup
|
1052 |
$dbi->execute($query, {author => 'Ken', title => '%Perl%'}); |
version 0.0901
|
1053 | |
cleanup
|
1054 |
=head2 C<(deprecated) default_bind_filter> |
1055 | ||
cleanup
|
1056 |
my $default_bind_filter = $dbi->default_bind_filter; |
1057 |
$dbi = $dbi->default_bind_filter($fname); |
|
cleanup
|
1058 | |
1059 |
Default filter when parameter binding is executed. |
|
1060 | ||
1061 |
=head2 C<(deprecated) default_fetch_filter> |
|
1062 | ||
cleanup
|
1063 |
my $default_fetch_filter = $dbi->default_fetch_filter; |
cleanup
|
1064 |
$dbi = $dbi->default_fetch_filter($fname); |
1065 | ||
cleanup
|
1066 |
=head2 C<execute> |
packaging one directory
|
1067 | |
cleanup
|
1068 |
my $result = $dbi->execute($query, param => $params, filter => \%filter); |
1069 |
my $result = $dbi->execute($source, param => $params, filter => \%filter); |
|
update document
|
1070 | |
cleanup
|
1071 |
Execute query or the source of SQL. |
1072 |
Query is L<DBIx::Custom::Query> object. |
|
1073 |
Return value is L<DBIx::Custom::Result> if select statement is executed, |
|
1074 |
or the count of affected rows if insert, update, delete statement is executed. |
|
version 0.0901
|
1075 | |
removed DESTROY method(not b...
|
1076 |
B<Example:> |
update document
|
1077 | |
cleanup
|
1078 |
my $result = $dbi->execute( |
added insert, update, update...
|
1079 |
"select * from book where {= author} and {like title}", |
cleanup
|
1080 |
param => {author => 'Ken', title => '%Perl%'} |
1081 |
); |
|
1082 |
|
|
1083 |
while (my $row = $result->fetch) { |
|
1084 |
my $author = $row->[0]; |
|
1085 |
my $title = $row->[1]; |
|
1086 |
} |
|
packaging one directory
|
1087 | |
added experimental expand me...
|
1088 |
=head2 C<(experimental) expand> |
1089 | ||
1090 |
my %expand = $dbi->expand($source); |
|
1091 | ||
1092 |
The following hash |
|
1093 | ||
added insert, update, update...
|
1094 |
{book => {title => 'Perl', author => 'Ken'}} |
added experimental expand me...
|
1095 | |
1096 |
is expanded to |
|
1097 | ||
added insert, update, update...
|
1098 |
('book.title' => 'Perl', 'book.author' => 'Ken') |
added experimental expand me...
|
1099 | |
1100 |
This is used in C<select()> |
|
1101 | ||
1102 | ||
1103 |
|
|
removed DBIx::Custom commit ...
|
1104 |
=head2 C<delete> |
packaging one directory
|
1105 | |
cleanup
|
1106 |
$dbi->delete(table => $table, |
1107 |
where => \%where, |
|
1108 |
append => $append, |
|
1109 |
filter => \%filter); |
|
bind_filter argument is chan...
|
1110 | |
renamed build_query to creat...
|
1111 |
Execute delete statement. |
1112 |
C<delete> method have C<table>, C<where>, C<append>, and C<filter> arguments. |
|
1113 |
C<table> is a table name. |
|
1114 |
C<where> is where clause. this must be hash reference. |
|
1115 |
C<append> is a string added at the end of the SQL statement. |
|
1116 |
C<filter> is filters when parameter binding is executed. |
|
cleanup
|
1117 |
Return value of C<delete()> is the count of affected rows. |
renamed build_query to creat...
|
1118 | |
removed DESTROY method(not b...
|
1119 |
B<Example:> |
packaging one directory
|
1120 | |
added insert, update, update...
|
1121 |
$dbi->delete(table => 'book', |
removed register_format()
|
1122 |
where => {id => 5}, |
1123 |
append => 'some statement', |
|
removed reconnect method
|
1124 |
filter => {id => 'encode_utf8'}); |
version 0.0901
|
1125 | |
removed DBIx::Custom commit ...
|
1126 |
=head2 C<delete_all> |
packaging one directory
|
1127 | |
cleanup
|
1128 |
$dbi->delete_all(table => $table); |
packaging one directory
|
1129 | |
renamed build_query to creat...
|
1130 |
Execute delete statement to delete all rows. |
1131 |
Arguments is same as C<delete> method, |
|
1132 |
except that C<delete_all> don't have C<where> argument. |
|
cleanup
|
1133 |
Return value of C<delete_all()> is the count of affected rows. |
bind_filter argument is chan...
|
1134 | |
removed DESTROY method(not b...
|
1135 |
B<Example:> |
removed register_format()
|
1136 |
|
added insert, update, update...
|
1137 |
$dbi->delete_all(table => 'book'); |
packaging one directory
|
1138 | |
added helper method
|
1139 |
=head2 C<(experimental) helper> |
1140 | ||
1141 |
$dbi->helper( |
|
1142 |
update_or_insert => sub { |
|
1143 |
my $self = shift; |
|
1144 |
# do something |
|
1145 |
}, |
|
1146 |
find_or_create => sub { |
|
1147 |
my $self = shift; |
|
1148 |
# do something |
|
1149 |
} |
|
1150 |
); |
|
1151 | ||
1152 |
Register helper methods. These method is called from L<DBIx::Custom> object directory. |
|
1153 | ||
1154 |
$dbi->update_or_insert; |
|
1155 |
$dbi->find_or_create; |
|
1156 | ||
cleanup
|
1157 |
=head2 C<insert> |
1158 | ||
1159 |
$dbi->insert(table => $table, |
|
1160 |
param => \%param, |
|
1161 |
append => $append, |
|
1162 |
filter => \%filter); |
|
1163 | ||
1164 |
Execute insert statement. |
|
1165 |
C<insert> method have C<table>, C<param>, C<append> |
|
1166 |
and C<filter> arguments. |
|
1167 |
C<table> is a table name. |
|
1168 |
C<param> is the pairs of column name value. this must be hash reference. |
|
1169 |
C<append> is a string added at the end of the SQL statement. |
|
1170 |
C<filter> is filters when parameter binding is executed. |
|
1171 |
This is overwrites C<default_bind_filter>. |
|
1172 |
Return value of C<insert()> is the count of affected rows. |
|
1173 | ||
1174 |
B<Example:> |
|
1175 | ||
added insert, update, update...
|
1176 |
$dbi->insert(table => 'book', |
cleanup
|
1177 |
param => {title => 'Perl', author => 'Taro'}, |
1178 |
append => "some statement", |
|
1179 |
filter => {title => 'encode_utf8'}) |
|
1180 | ||
added dbi_options attribute
|
1181 |
=head2 C<new> |
1182 | ||
1183 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname", |
|
1184 |
user => 'ken', password => '!LFKD%$&'); |
|
1185 | ||
1186 |
Create a new L<DBIx::Custom> object. |
|
1187 | ||
added experimental iterate_a...
|
1188 |
=head2 C<(experimental) iterate_all_columns> |
1189 | ||
1190 |
$dbi->iterate_all_columns( |
|
1191 |
sub { |
|
1192 |
my ($table, $column, $column_info) = @_; |
|
1193 |
|
|
1194 |
# do something; |
|
1195 |
} |
|
1196 |
); |
|
1197 | ||
1198 |
Iterate all columns of all tables. Argument is callback. |
|
1199 |
You can do anything by callback. |
|
1200 | ||
cleanup
|
1201 |
=head2 C<register_filter> |
1202 | ||
1203 |
$dbi->register_filter(%filters); |
|
1204 |
$dbi->register_filter(\%filters); |
|
1205 |
|
|
1206 |
Register filter. Registered filters is available in the following attributes |
|
1207 |
or arguments. |
|
1208 | ||
1209 |
=over 4 |
|
1210 | ||
1211 |
=item * |
|
1212 | ||
1213 |
C<filter> argument of C<insert()>, C<update()>, |
|
1214 |
C<update_all()>, C<delete()>, C<delete_all()>, C<select()> |
|
1215 |
methods |
|
1216 | ||
1217 |
=item * |
|
1218 | ||
1219 |
C<execute()> method |
|
1220 | ||
1221 |
=item * |
|
1222 | ||
1223 |
C<default_filter> and C<filter> of C<DBIx::Custom::Query> |
|
1224 | ||
1225 |
=item * |
|
1226 | ||
1227 |
C<default_filter> and C<filter> of C<DBIx::Custom::Result> |
|
1228 | ||
1229 |
=back |
|
1230 | ||
1231 |
B<Example:> |
|
1232 | ||
1233 |
$dbi->register_filter( |
|
1234 |
encode_utf8 => sub { |
|
1235 |
my $value = shift; |
|
1236 |
|
|
1237 |
require Encode; |
|
1238 |
|
|
1239 |
return Encode::encode('UTF-8', $value); |
|
1240 |
}, |
|
1241 |
decode_utf8 => sub { |
|
1242 |
my $value = shift; |
|
1243 |
|
|
1244 |
require Encode; |
|
1245 |
|
|
1246 |
return Encode::decode('UTF-8', $value) |
|
1247 |
} |
|
1248 |
); |
|
1249 | ||
added auto_filter method
|
1250 |
=head2 C<rollback> |
cleanup
|
1251 | |
1252 |
$dbi->rollback; |
|
1253 | ||
1254 |
Rollback transaction. |
|
1255 |
This is same as L<DBI>'s C<rollback>. |
|
1256 | ||
removed DBIx::Custom commit ...
|
1257 |
=head2 C<select> |
packaging one directory
|
1258 |
|
cleanup
|
1259 |
my $result = $dbi->select(table => $table, |
1260 |
column => [@column], |
|
1261 |
where => \%where, |
|
1262 |
append => $append, |
|
1263 |
relation => \%relation, |
|
1264 |
filter => \%filter); |
|
update document
|
1265 | |
renamed build_query to creat...
|
1266 |
Execute select statement. |
cleanup
|
1267 |
C<select> method have C<table>, C<column>, C<where>, C<append>, |
renamed build_query to creat...
|
1268 |
C<relation> and C<filter> arguments. |
1269 |
C<table> is a table name. |
|
cleanup
|
1270 |
C<where> is where clause. this is normally hash reference. |
renamed build_query to creat...
|
1271 |
C<append> is a string added at the end of the SQL statement. |
1272 |
C<filter> is filters when parameter binding is executed. |
|
update document
|
1273 | |
removed DESTROY method(not b...
|
1274 |
B<Example:> |
update document
|
1275 | |
added insert, update, update...
|
1276 |
# select * from book; |
1277 |
my $result = $dbi->select(table => 'book'); |
|
packaging one directory
|
1278 |
|
added insert, update, update...
|
1279 |
# select * from book where title = ?; |
1280 |
my $result = $dbi->select(table => 'book', where => {title => 'Perl'}); |
|
update document
|
1281 |
|
added insert, update, update...
|
1282 |
# select title, author from book where id = ? for update; |
cleanup
|
1283 |
my $result = $dbi->select( |
added insert, update, update...
|
1284 |
table => 'book', |
removed reconnect method
|
1285 |
column => ['title', 'author'], |
removed register_format()
|
1286 |
where => {id => 1}, |
1287 |
appned => 'for update' |
|
update document
|
1288 |
); |
1289 |
|
|
added insert, update, update...
|
1290 |
# select book.name as book_name from book, rental |
1291 |
# where book.id = rental.book_id; |
|
added commit method
|
1292 |
my $result = $dbi->select( |
added insert, update, update...
|
1293 |
table => ['book', 'rental'], |
1294 |
column => ['book.name as book_name'] |
|
1295 |
relation => {'book.id' => 'rental.book_id'} |
|
update document
|
1296 |
); |
1297 | ||
cleanup
|
1298 |
If you use more complex condition, |
1299 |
you can specify a array reference to C<where> argument. |
|
1300 | ||
1301 |
my $result = $dbi->select( |
|
added insert, update, update...
|
1302 |
table => 'book', |
cleanup
|
1303 |
column => ['title', 'author'], |
1304 |
where => ['{= title} or {like author}', |
|
1305 |
{title => '%Perl%', author => 'Ken'}] |
|
1306 |
); |
|
1307 | ||
1308 |
First element is a string. it contains tags, |
|
1309 |
such as "{= title} or {like author}". |
|
1310 |
Second element is paramters. |
|
1311 | ||
cleanup
|
1312 |
=head2 C<update> |
removed reconnect method
|
1313 | |
cleanup
|
1314 |
$dbi->update(table => $table, |
1315 |
param => \%params, |
|
1316 |
where => \%where, |
|
1317 |
append => $append, |
|
1318 |
filter => \%filter) |
|
removed reconnect method
|
1319 | |
cleanup
|
1320 |
Execute update statement. |
1321 |
C<update> method have C<table>, C<param>, C<where>, C<append> |
|
1322 |
and C<filter> arguments. |
|
1323 |
C<table> is a table name. |
|
1324 |
C<param> is column-value pairs. this must be hash reference. |
|
1325 |
C<where> is where clause. this must be hash reference. |
|
1326 |
C<append> is a string added at the end of the SQL statement. |
|
1327 |
C<filter> is filters when parameter binding is executed. |
|
1328 |
This is overwrites C<default_bind_filter>. |
|
1329 |
Return value of C<update()> is the count of affected rows. |
|
removed reconnect method
|
1330 | |
removed DBIx::Custom commit ...
|
1331 |
B<Example:> |
removed reconnect method
|
1332 | |
added insert, update, update...
|
1333 |
$dbi->update(table => 'book', |
cleanup
|
1334 |
param => {title => 'Perl', author => 'Taro'}, |
1335 |
where => {id => 5}, |
|
1336 |
append => "some statement", |
|
1337 |
filter => {title => 'encode_utf8'}); |
|
renamed build_query to creat...
|
1338 | |
added insert, update, update...
|
1339 |
=head2 C<(experimental) txn_scope> |
1340 | ||
1341 |
{ |
|
1342 |
my $txn = $dbi->txn_scope; |
|
1343 |
$dbi->insert(table => 'book', param => {title => 'Perl'}); |
|
1344 |
$dbi->insert(table => 'book', param => {title => 'Good days'}); |
|
1345 |
$txn->commit; |
|
1346 |
} |
|
1347 | ||
1348 |
Create transaction scope. If you escape scope(that is { .. }) and commited, |
|
1349 |
Rollback is automatically done. |
|
1350 | ||
1351 |
Note that this is feature of L<DBIx::TransactionManager> |
|
1352 |
L<DBIx::TransactionManager> is required. |
|
1353 | ||
cleanup
|
1354 |
=head2 C<update_all> |
renamed build_query to creat...
|
1355 | |
cleanup
|
1356 |
$dbi->update_all(table => $table, |
1357 |
param => \%params, |
|
1358 |
filter => \%filter, |
|
1359 |
append => $append); |
|
renamed build_query to creat...
|
1360 | |
cleanup
|
1361 |
Execute update statement to update all rows. |
1362 |
Arguments is same as C<update> method, |
|
1363 |
except that C<update_all> don't have C<where> argument. |
|
1364 |
Return value of C<update_all()> is the count of affected rows. |
|
removed DBIx::Custom commit ...
|
1365 | |
1366 |
B<Example:> |
|
packaging one directory
|
1367 | |
added insert, update, update...
|
1368 |
$dbi->update_all(table => 'book', |
cleanup
|
1369 |
param => {author => 'taro'}, |
1370 |
filter => {author => 'encode_utf8'}); |
|
removed reconnect method
|
1371 | |
DBIx::Custom is now stable
|
1372 |
=head1 STABILITY |
1373 | ||
1374 |
L<DBIx::Custom> is now stable. APIs keep backword compatible in the feature. |
|
1375 | ||
removed DESTROY method(not b...
|
1376 |
=head1 BUGS |
1377 | ||
renamed build_query to creat...
|
1378 |
Please tell me bugs if found. |
removed DESTROY method(not b...
|
1379 | |
1380 |
C<< <kimoto.yuki at gmail.com> >> |
|
1381 | ||
1382 |
L<http://github.com/yuki-kimoto/DBIx-Custom> |
|
1383 | ||
removed reconnect method
|
1384 |
=head1 AUTHOR |
1385 | ||
1386 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
|
version 0.0901
|
1387 | |
packaging one directory
|
1388 |
=head1 COPYRIGHT & LICENSE |
1389 | ||
1390 |
Copyright 2009 Yuki Kimoto, all rights reserved. |
|
1391 | ||
1392 |
This program is free software; you can redistribute it and/or modify it |
|
1393 |
under the same terms as Perl itself. |
|
1394 | ||
1395 |
=cut |
|
added cache_method attribute
|
1396 | |
1397 |