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