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