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