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