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