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; |
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; |
fix select method empty wher...
|
510 |
my $wexists; |
added experimental DBIx::Cus...
|
511 |
if (ref $where eq 'HASH' && keys %$where) { |
update document
|
512 |
$param = $where; |
fix select method empty wher...
|
513 |
$wexists = keys %$where; |
514 |
|
|
515 |
if ($wexists) { |
|
516 |
$source .= 'where ('; |
|
517 |
foreach my $where_key (keys %$where) { |
|
518 |
$source .= "{= $where_key} and "; |
|
519 |
} |
|
520 |
$source =~ s/ and $//; |
|
521 |
$source .= ') '; |
|
packaging one directory
|
522 |
} |
update document
|
523 |
} |
524 |
elsif (ref $where eq 'ARRAY') { |
|
fix select method empty wher...
|
525 |
my $w = $where->[0] || ''; |
update document
|
526 |
$param = $where->[1]; |
527 |
|
|
fix select method empty wher...
|
528 |
if (ref $w eq 'HASH') { |
529 |
|
|
530 |
} |
|
531 |
else { |
|
532 |
$wexists = $w =~ /\S/; |
|
533 |
$source .= "where ($w) " if $wexists; |
|
534 |
} |
|
packaging one directory
|
535 |
} |
536 |
|
|
added commit method
|
537 |
# Relation |
538 |
if ($relation) { |
|
fix select method empty wher...
|
539 |
$source .= $wexists ? "and " : "where "; |
added commit method
|
540 |
foreach my $rkey (keys %$relation) { |
renamed default_query_filter...
|
541 |
$source .= "$rkey = " . $relation->{$rkey} . " and "; |
packaging one directory
|
542 |
} |
543 |
} |
|
renamed default_query_filter...
|
544 |
$source =~ s/ and $//; |
added commit method
|
545 |
|
546 |
# Append some statement |
|
renamed default_query_filter...
|
547 |
$source .= " $append" if $append; |
packaging one directory
|
548 |
|
549 |
# Execute query |
|
added auto_filter method
|
550 |
my $result = $self->execute( |
551 |
$source, param => $param, filter => $filter, |
|
renamed auto_filter to apply...
|
552 |
table => $tables); |
packaging one directory
|
553 |
|
554 |
return $result; |
|
555 |
} |
|
556 | ||
remove DBIx::Custom::Model
|
557 |
sub table { |
558 |
my $self = shift; |
|
559 |
my $name = shift; |
|
560 |
|
|
561 |
# Table class |
|
562 |
my $table_class = $self->table_class; |
|
563 |
croak qq{Invalid table class name "$table_class"} |
|
564 |
unless $table_class =~ /^[\w:]+$/; |
|
565 |
unless ($table_class->can('isa')) { |
|
566 |
eval "require $table_class"; |
|
567 |
croak $@ if $@; |
|
568 |
} |
|
569 |
# Create table |
|
570 |
$self->{_tables} ||= {}; |
|
571 |
$self->{_tables}->{$name} |
|
572 |
= $table_class->new(name => $name, dbi => $self) |
|
573 |
unless defined $self->{_tables}->{$name}; |
|
574 |
|
|
575 |
# Helper |
|
576 |
$self->{_tables}->{$name}->helper(@_) if @_; |
|
577 |
|
|
578 |
return $self->{_tables}{$name}; |
|
579 |
} |
|
580 | ||
added insert, update, update...
|
581 |
sub txn_scope { |
582 |
my $self = shift; |
|
583 |
|
|
584 |
require DBIx::TransactionManager; |
|
585 |
|
|
586 |
$self->{_transaction_manager} |
|
587 |
||= DBIx::TransactionManager->new($self->dbh); |
|
588 |
|
|
589 |
return $self->{_transaction_manager}->txn_scope; |
|
590 |
} |
|
591 | ||
cleanup
|
592 |
our %VALID_UPDATE_ARGS |
renamed auto_filter to apply...
|
593 |
= map { $_ => 1 } qw/table param |
added auto_filter method
|
594 |
where append filter allow_update_all/; |
cleanup
|
595 | |
596 |
sub update { |
|
597 |
my ($self, %args) = @_; |
|
version 0.0901
|
598 |
|
cleanup
|
599 |
# Check arguments |
600 |
foreach my $name (keys %args) { |
|
601 |
croak qq{"$name" is invalid argument} |
|
602 |
unless $VALID_UPDATE_ARGS{$name}; |
|
removed reconnect method
|
603 |
} |
added cache_method attribute
|
604 |
|
cleanup
|
605 |
# Arguments |
606 |
my $table = $args{table} || ''; |
|
607 |
my $param = $args{param} || {}; |
|
608 |
my $where = $args{where} || {}; |
|
609 |
my $append = $args{append} || ''; |
|
610 |
my $filter = $args{filter}; |
|
611 |
my $allow_update_all = $args{allow_update_all}; |
|
version 0.0901
|
612 |
|
cleanup
|
613 |
# Update keys |
614 |
my @update_keys = keys %$param; |
|
renamed fetch_rows to fetch_...
|
615 |
|
cleanup
|
616 |
# Where keys |
617 |
my @where_keys = keys %$where; |
|
removed reconnect method
|
618 |
|
cleanup
|
619 |
# Not exists where keys |
620 |
croak qq{"where" argument must be specified and } . |
|
621 |
qq{contains the pairs of column name and value} |
|
622 |
if !@where_keys && !$allow_update_all; |
|
removed experimental registe...
|
623 |
|
cleanup
|
624 |
# Update clause |
625 |
my $update_clause = '{update_param ' . join(' ', @update_keys) . '}'; |
|
removed experimental registe...
|
626 |
|
cleanup
|
627 |
# Where clause |
628 |
my $where_clause = ''; |
|
629 |
my $new_where = {}; |
|
removed reconnect method
|
630 |
|
cleanup
|
631 |
if (@where_keys) { |
632 |
$where_clause = 'where '; |
|
633 |
$where_clause .= "{= $_} and " for @where_keys; |
|
634 |
$where_clause =~ s/ and $//; |
|
removed reconnect method
|
635 |
} |
636 |
|
|
cleanup
|
637 |
# Source of SQL |
638 |
my $source = "update $table $update_clause $where_clause"; |
|
639 |
$source .= " $append" if $append; |
|
removed reconnect method
|
640 |
|
cleanup
|
641 |
# Rearrange parameters |
642 |
foreach my $wkey (@where_keys) { |
|
removed reconnect method
|
643 |
|
cleanup
|
644 |
if (exists $param->{$wkey}) { |
645 |
$param->{$wkey} = [$param->{$wkey}] |
|
646 |
unless ref $param->{$wkey} eq 'ARRAY'; |
|
647 |
|
|
648 |
push @{$param->{$wkey}}, $where->{$wkey}; |
|
649 |
} |
|
650 |
else { |
|
651 |
$param->{$wkey} = $where->{$wkey}; |
|
652 |
} |
|
removed reconnect method
|
653 |
} |
cleanup
|
654 |
|
655 |
# Execute query |
|
656 |
my $ret_val = $self->execute($source, param => $param, |
|
added auto_filter method
|
657 |
filter => $filter, |
renamed auto_filter to apply...
|
658 |
table => $table); |
cleanup
|
659 |
|
660 |
return $ret_val; |
|
removed reconnect method
|
661 |
} |
662 | ||
cleanup
|
663 |
sub update_all { shift->update(allow_update_all => 1, @_) }; |
664 | ||
cleanup
|
665 |
sub _build_binds { |
666 |
my ($self, $params, $columns, $filter) = @_; |
|
removed reconnect method
|
667 |
|
cleanup
|
668 |
# bind values |
669 |
my @binds; |
|
add tests
|
670 |
|
removed reconnect method
|
671 |
# Build bind values |
672 |
my $count = {}; |
|
cleanup
|
673 |
foreach my $column (@$columns) { |
removed reconnect method
|
674 |
|
675 |
# Value |
|
676 |
my $value = ref $params->{$column} eq 'ARRAY' |
|
677 |
? $params->{$column}->[$count->{$column} || 0] |
|
678 |
: $params->{$column}; |
|
679 |
|
|
cleanup
|
680 |
# Filter |
681 |
my $f = $filter->{$column} || $self->{default_out_filter} || ''; |
|
cleanup
|
682 |
|
cleanup
|
683 |
push @binds, $f ? $f->($value) : $value; |
removed reconnect method
|
684 |
|
685 |
# Count up |
|
686 |
$count->{$column}++; |
|
687 |
} |
|
688 |
|
|
cleanup
|
689 |
return \@binds; |
removed reconnect method
|
690 |
} |
691 | ||
cleanup
|
692 |
sub _croak { |
693 |
my ($self, $error, $append) = @_; |
|
694 |
$append ||= ""; |
|
695 |
|
|
696 |
# Verbose |
|
697 |
if ($Carp::Verbose) { croak $error } |
|
698 |
|
|
699 |
# Not verbose |
|
700 |
else { |
|
701 |
|
|
702 |
# Remove line and module infromation |
|
703 |
my $at_pos = rindex($error, ' at '); |
|
704 |
$error = substr($error, 0, $at_pos); |
|
705 |
$error =~ s/\s+$//; |
|
706 |
|
|
707 |
croak "$error$append"; |
|
708 |
} |
|
709 |
} |
|
710 | ||
cleanup
|
711 |
# Deprecated |
712 |
__PACKAGE__->attr(cache_method => sub { |
|
713 |
sub { |
|
714 |
my $self = shift; |
|
715 |
|
|
716 |
$self->{_cached} ||= {}; |
|
717 |
|
|
718 |
if (@_ > 1) { |
|
719 |
$self->{_cached}{$_[0]} = $_[1] |
|
720 |
} |
|
721 |
else { |
|
722 |
return $self->{_cached}{$_[0]} |
|
723 |
} |
|
724 |
} |
|
725 |
}); |
|
726 | ||
727 |
sub default_bind_filter { |
|
728 |
my $self = shift; |
|
729 |
|
|
730 |
if (@_) { |
|
731 |
my $fname = $_[0]; |
|
732 |
|
|
733 |
if (@_ && !$fname) { |
|
734 |
$self->{default_out_filter} = undef; |
|
735 |
} |
|
736 |
else { |
|
737 |
croak qq{"$fname" is not registered} |
|
738 |
unless exists $self->filters->{$fname}; |
|
739 |
|
|
740 |
$self->{default_out_filter} = $self->filters->{$fname}; |
|
741 |
} |
|
742 |
return $self; |
|
743 |
} |
|
744 |
|
|
745 |
return $self->{default_out_filter}; |
|
746 |
} |
|
747 | ||
748 |
sub default_fetch_filter { |
|
749 |
my $self = shift; |
|
750 |
my $fname = $_[0]; |
|
751 |
|
|
752 |
if (@_) { |
|
753 |
if (@_ && !$fname) { |
|
754 |
$self->{default_in_filter} = undef; |
|
755 |
} |
|
756 |
else { |
|
757 |
croak qq{"$fname" is not registered} |
|
758 |
unless exists $self->filters->{$fname}; |
|
759 |
|
|
760 |
$self->{default_in_filter} = $self->filters->{$fname}; |
|
761 |
} |
|
762 |
|
|
763 |
return $self; |
|
764 |
} |
|
765 |
|
|
766 |
return $self->{default_in_filter} |
|
767 |
} |
|
768 | ||
fixed DBIx::Custom::QueryBui...
|
769 |
1; |
770 | ||
removed reconnect method
|
771 |
=head1 NAME |
772 | ||
renamed build_query to creat...
|
773 |
DBIx::Custom - DBI interface, having hash parameter binding and filtering system |
removed reconnect method
|
774 | |
775 |
=head1 SYNOPSYS |
|
cleanup
|
776 | |
renamed build_query to creat...
|
777 |
Connect to the database. |
778 |
|
|
779 |
use DBIx::Custom; |
|
renamed update tag to update...
|
780 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname", |
removed reconnect method
|
781 |
user => 'ken', password => '!LFKD%$&'); |
cleanup
|
782 | |
renamed build_query to creat...
|
783 |
Insert, update, and delete |
cleanup
|
784 | |
removed reconnect method
|
785 |
# Insert |
added insert, update, update...
|
786 |
$dbi->insert(table => 'book', |
renamed update tag to update...
|
787 |
param => {title => 'Perl', author => 'Ken'}, |
removed reconnect method
|
788 |
filter => {title => 'encode_utf8'}); |
789 |
|
|
790 |
# Update |
|
added insert, update, update...
|
791 |
$dbi->update(table => 'book', |
renamed update tag to update...
|
792 |
param => {title => 'Perl', author => 'Ken'}, |
removed reconnect method
|
793 |
where => {id => 5}, |
794 |
filter => {title => 'encode_utf8'}); |
|
795 |
|
|
796 |
# Update all |
|
added insert, update, update...
|
797 |
$dbi->update_all(table => 'book', |
renamed update tag to update...
|
798 |
param => {title => 'Perl'}, |
removed reconnect method
|
799 |
filter => {title => 'encode_utf8'}); |
800 |
|
|
801 |
# Delete |
|
added insert, update, update...
|
802 |
$dbi->delete(table => 'book', |
removed reconnect method
|
803 |
where => {author => 'Ken'}, |
804 |
filter => {title => 'encode_utf8'}); |
|
805 |
|
|
806 |
# Delete all |
|
added insert, update, update...
|
807 |
$dbi->delete_all(table => 'book'); |
cleanup
|
808 | |
renamed build_query to creat...
|
809 |
Select |
cleanup
|
810 | |
removed reconnect method
|
811 |
# Select |
added insert, update, update...
|
812 |
my $result = $dbi->select(table => 'book'); |
renamed fetch_rows to fetch_...
|
813 |
|
renamed build_query to creat...
|
814 |
# Select, more complex |
renamed fetch_rows to fetch_...
|
815 |
my $result = $dbi->select( |
added insert, update, update...
|
816 |
table => 'book', |
update document
|
817 |
column => [qw/author title/], |
818 |
where => {author => 'Ken'}, |
|
updated document
|
819 |
append => 'order by id limit 5', |
renamed build_query to creat...
|
820 |
filter => {title => 'encode_utf8'} |
renamed fetch_rows to fetch_...
|
821 |
); |
added commit method
|
822 |
|
renamed build_query to creat...
|
823 |
# Select, join table |
added commit method
|
824 |
my $result = $dbi->select( |
added insert, update, update...
|
825 |
table => ['book', 'rental'], |
826 |
column => ['book.name as book_name'] |
|
827 |
relation => {'book.id' => 'rental.book_id'} |
|
added commit method
|
828 |
); |
updated document
|
829 |
|
830 |
# Select, more flexible where |
|
831 |
my $result = $dbi->select( |
|
added insert, update, update...
|
832 |
table => 'book', |
updated document
|
833 |
where => ['{= author} and {like title}', |
834 |
{author => 'Ken', title => '%Perl%'}] |
|
835 |
); |
|
cleanup
|
836 | |
renamed build_query to creat...
|
837 |
Execute SQL |
cleanup
|
838 | |
renamed build_query to creat...
|
839 |
# Execute SQL |
added insert, update, update...
|
840 |
$dbi->execute("select title from book"); |
removed register_format()
|
841 |
|
renamed build_query to creat...
|
842 |
# Execute SQL with hash binding and filtering |
added insert, update, update...
|
843 |
$dbi->execute("select id from book where {= author} and {like title}", |
removed register_format()
|
844 |
param => {author => 'ken', title => '%Perl%'}, |
renamed build_query to creat...
|
845 |
filter => {title => 'encode_utf8'}); |
removed reconnect method
|
846 | |
847 |
# Create query and execute it |
|
renamed build_query to creat...
|
848 |
my $query = $dbi->create_query( |
added insert, update, update...
|
849 |
"select id from book where {= author} and {like title}" |
removed reconnect method
|
850 |
); |
updated document
|
851 |
$dbi->execute($query, param => {author => 'Ken', title => '%Perl%'}) |
cleanup
|
852 | |
updated document
|
853 |
Other features. |
cleanup
|
854 | |
855 |
# Get DBI object |
|
856 |
my $dbh = $dbi->dbh; |
|
857 | ||
858 |
Fetch row. |
|
859 | ||
removed register_format()
|
860 |
# Fetch |
861 |
while (my $row = $result->fetch) { |
|
862 |
# ... |
|
863 |
} |
|
864 |
|
|
865 |
# Fetch hash |
|
866 |
while (my $row = $result->fetch_hash) { |
|
867 |
|
|
868 |
} |
|
869 |
|
|
renamed update tag to update...
|
870 |
=head1 DESCRIPTIONS |
removed reconnect method
|
871 | |
renamed build_query to creat...
|
872 |
L<DBIx::Custom> is one of L<DBI> interface modules, |
873 |
such as L<DBIx::Class>, L<DBIx::Simple>. |
|
removed reconnect method
|
874 | |
renamed build_query to creat...
|
875 |
This module is not O/R mapper. O/R mapper is useful, |
876 |
but you must learn many syntax of the O/R mapper, |
|
updated document
|
877 |
which is almost another language. |
878 |
Created SQL statement is offten not effcient and damage SQL performance. |
|
renamed build_query to creat...
|
879 |
so you have to execute raw SQL in the end. |
removed reconnect method
|
880 | |
renamed build_query to creat...
|
881 |
L<DBIx::Custom> is middle area between L<DBI> and O/R mapper. |
updated document
|
882 |
L<DBIx::Custom> provide flexible hash parameter binding and filtering system, |
added experimental expand me...
|
883 |
and suger methods, such as C<insert()>, C<update()>, C<delete()>, C<select()> |
updated document
|
884 |
to execute SQL easily. |
removed reconnect method
|
885 | |
updated document
|
886 |
L<DBIx::Custom> respects SQL. SQL is very complex and not beautiful, |
887 |
but de-facto standard, |
|
888 |
so all people learing database know it. |
|
renamed update tag to update...
|
889 |
If you already know SQL, |
890 |
you learn a little thing to use L<DBIx::Custom>. |
|
removed reconnect method
|
891 | |
added DBIx::Custom::Guides
|
892 |
See L<DBIx::Custom::Guides> for more details. |
updated document
|
893 | |
update document
|
894 |
=head1 ATTRIBUTES |
packaging one directory
|
895 | |
cleanup
|
896 |
=head2 C<cache> |
packaging one directory
|
897 | |
cleanup
|
898 |
my $cache = $dbi->cache; |
899 |
$dbi = $dbi->cache(1); |
|
removed DESTROY method(not b...
|
900 | |
cleanup
|
901 |
Enable parsed L<DBIx::Custom::Query> object caching. |
902 |
Default to 1. |
|
packaging one directory
|
903 | |
removed DBIx::Custom commit ...
|
904 |
=head2 C<data_source> |
packaging one directory
|
905 | |
cleanup
|
906 |
my $data_source = $dbi->data_source; |
cleanup
|
907 |
$dbi = $dbi->data_source("DBI:mysql:database=dbname"); |
removed DESTROY method(not b...
|
908 | |
cleanup
|
909 |
Data source. |
910 |
C<connect()> method use this value to connect the database. |
|
removed DESTROY method(not b...
|
911 | |
removed DBIx::Custom commit ...
|
912 |
=head2 C<dbh> |
packaging one directory
|
913 | |
cleanup
|
914 |
my $dbh = $dbi->dbh; |
915 |
$dbi = $dbi->dbh($dbh); |
|
packaging one directory
|
916 | |
cleanup
|
917 |
L<DBI> object. You can call all methods of L<DBI>. |
packaging one directory
|
918 | |
added dbi_options attribute
|
919 |
=head2 C<dbi_options> |
920 | ||
921 |
my $dbi_options = $dbi->dbi_options; |
|
922 |
$dbi = $dbi->dbi_options($dbi_options); |
|
923 | ||
924 |
DBI options. |
|
925 |
C<connect()> method use this value to connect the database. |
|
926 | ||
cleanup
|
927 |
Default filter when row is fetched. |
packaging one directory
|
928 | |
cleanup
|
929 |
=head2 C<filters> |
bind_filter argument is chan...
|
930 | |
cleanup
|
931 |
my $filters = $dbi->filters; |
932 |
$dbi = $dbi->filters(\%filters); |
|
packaging one directory
|
933 | |
cleanup
|
934 |
Filter functions. |
935 |
"encode_utf8" and "decode_utf8" is registered by default. |
|
936 | ||
937 |
=head2 C<filter_check> |
|
938 | ||
939 |
my $filter_check = $dbi->filter_check; |
|
940 |
$dbi = $dbi->filter_check(0); |
|
941 | ||
cleanup
|
942 |
B<this attribute is now deprecated and has no mean |
943 |
because check is always done>. |
|
cleanup
|
944 | |
945 |
=head2 C<password> |
|
946 | ||
947 |
my $password = $dbi->password; |
|
948 |
$dbi = $dbi->password('lkj&le`@s'); |
|
949 | ||
950 |
Password. |
|
951 |
C<connect()> method use this value to connect the database. |
|
update document
|
952 | |
renamed update tag to update...
|
953 |
=head2 C<query_builder> |
added commit method
|
954 | |
renamed update tag to update...
|
955 |
my $sql_class = $dbi->query_builder; |
956 |
$dbi = $dbi->query_builder(DBIx::Custom::QueryBuilder->new); |
|
added commit method
|
957 | |
renamed update tag to update...
|
958 |
SQL builder. C<query_builder()> must be |
renamed build_query to creat...
|
959 |
the instance of L<DBIx::Custom::QueryBuilder> subclass. |
960 |
Default to L<DBIx::Custom::QueryBuilder> object. |
|
cleanup
|
961 | |
cleanup
|
962 |
=head2 C<result_class> |
cleanup
|
963 | |
cleanup
|
964 |
my $result_class = $dbi->result_class; |
965 |
$dbi = $dbi->result_class('DBIx::Custom::Result'); |
|
cleanup
|
966 | |
cleanup
|
967 |
Result class for select statement. |
968 |
Default to L<DBIx::Custom::Result>. |
|
cleanup
|
969 | |
cleanup
|
970 |
=head2 C<user> |
cleanup
|
971 | |
cleanup
|
972 |
my $user = $dbi->user; |
973 |
$dbi = $dbi->user('Ken'); |
|
cleanup
|
974 | |
cleanup
|
975 |
User name. |
976 |
C<connect()> method use this value to connect the database. |
|
977 |
|
|
978 |
=head1 METHODS |
|
added commit method
|
979 | |
cleanup
|
980 |
L<DBIx::Custom> inherits all methods from L<Object::Simple> |
981 |
and implements the following new ones. |
|
added check_filter attribute
|
982 | |
renamed auto_filter to apply...
|
983 |
=head2 C<(experimental) apply_filter > |
added auto_filter method
|
984 | |
renamed auto_filter to apply...
|
985 |
$dbi->apply_filter( |
added auto_filter method
|
986 |
$table, |
renamed auto_filter to apply...
|
987 |
$column1 => {in => $infilter1, out => $outfilter1} |
988 |
$column2 => {in => $infilter2, out => $outfilter2} |
|
989 |
..., |
|
added auto_filter method
|
990 |
); |
991 | ||
renamed auto_filter to apply...
|
992 |
C<apply_filter> is automatically filter for columns of table. |
added auto_filter method
|
993 |
This have effect C<insert>, C<update>, C<delete>. C<select> |
cleanup
|
994 |
and L<DBIx::Custom::Result> object. but this has'nt C<execute> method. |
995 | ||
cleanup
|
996 |
If you want to have effect C<execute()> method, use C<table> |
cleanup
|
997 |
arguments. |
added auto_filter method
|
998 | |
cleanup
|
999 |
$result = $dbi->execute( |
1000 |
"select * from table1 where {= key1} and {= key2};", |
|
1001 |
param => {key1 => 1, key2 => 2}, |
|
renamed auto_filter to apply...
|
1002 |
table => ['table1'] |
cleanup
|
1003 |
); |
1004 |
|
|
added auto_filter method
|
1005 |
=head2 C<begin_work> |
added check_filter attribute
|
1006 | |
cleanup
|
1007 |
$dbi->begin_work; |
added check_filter attribute
|
1008 | |
cleanup
|
1009 |
Start transaction. |
1010 |
This is same as L<DBI>'s C<begin_work>. |
|
added commit method
|
1011 | |
cleanup
|
1012 |
L<DBIx::Custom> inherits all methods from L<Object::Simple> |
1013 |
and implements the following new ones. |
|
added commit method
|
1014 | |
added auto_filter method
|
1015 |
=head2 C<commit> |
cleanup
|
1016 | |
1017 |
$dbi->commit; |
|
1018 | ||
1019 |
Commit transaction. |
|
1020 |
This is same as L<DBI>'s C<commit>. |
|
1021 | ||
removed DBIx::Custom commit ...
|
1022 |
=head2 C<connect> |
packaging one directory
|
1023 | |
cleanup
|
1024 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname", |
update document
|
1025 |
user => 'ken', password => '!LFKD%$&'); |
bind_filter argument is chan...
|
1026 | |
cleanup
|
1027 |
Create a new L<DBIx::Custom> object and connect to the database. |
renamed build_query to creat...
|
1028 |
L<DBIx::Custom> is a wrapper of L<DBI>. |
cleanup
|
1029 |
C<AutoCommit> and C<RaiseError> options are true, |
renamed build_query to creat...
|
1030 |
and C<PrintError> option is false by default. |
packaging one directory
|
1031 | |
cleanup
|
1032 |
=head2 C<create_query> |
1033 |
|
|
1034 |
my $query = $dbi->create_query( |
|
added insert, update, update...
|
1035 |
"select * from book where {= author} and {like title};" |
cleanup
|
1036 |
); |
update document
|
1037 | |
cleanup
|
1038 |
Create the instance of L<DBIx::Custom::Query> from the source of SQL. |
1039 |
If you want to get high performance, |
|
1040 |
use C<create_query()> method and execute it by C<execute()> method |
|
1041 |
instead of suger methods. |
|
bind_filter argument is chan...
|
1042 | |
cleanup
|
1043 |
$dbi->execute($query, {author => 'Ken', title => '%Perl%'}); |
version 0.0901
|
1044 | |
cleanup
|
1045 |
=head2 C<execute> |
packaging one directory
|
1046 | |
cleanup
|
1047 |
my $result = $dbi->execute($query, param => $params, filter => \%filter); |
1048 |
my $result = $dbi->execute($source, param => $params, filter => \%filter); |
|
update document
|
1049 | |
cleanup
|
1050 |
Execute query or the source of SQL. |
1051 |
Query is L<DBIx::Custom::Query> object. |
|
1052 |
Return value is L<DBIx::Custom::Result> if select statement is executed, |
|
1053 |
or the count of affected rows if insert, update, delete statement is executed. |
|
version 0.0901
|
1054 | |
removed DESTROY method(not b...
|
1055 |
B<Example:> |
update document
|
1056 | |
cleanup
|
1057 |
my $result = $dbi->execute( |
added insert, update, update...
|
1058 |
"select * from book where {= author} and {like title}", |
cleanup
|
1059 |
param => {author => 'Ken', title => '%Perl%'} |
1060 |
); |
|
1061 |
|
|
1062 |
while (my $row = $result->fetch) { |
|
1063 |
my $author = $row->[0]; |
|
1064 |
my $title = $row->[1]; |
|
1065 |
} |
|
packaging one directory
|
1066 | |
added experimental expand me...
|
1067 |
=head2 C<(experimental) expand> |
1068 | ||
1069 |
my %expand = $dbi->expand($source); |
|
1070 | ||
1071 |
The following hash |
|
1072 | ||
added insert, update, update...
|
1073 |
{book => {title => 'Perl', author => 'Ken'}} |
added experimental expand me...
|
1074 | |
1075 |
is expanded to |
|
1076 | ||
added insert, update, update...
|
1077 |
('book.title' => 'Perl', 'book.author' => 'Ken') |
added experimental expand me...
|
1078 | |
1079 |
This is used in C<select()> |
|
1080 | ||
1081 | ||
1082 |
|
|
removed DBIx::Custom commit ...
|
1083 |
=head2 C<delete> |
packaging one directory
|
1084 | |
cleanup
|
1085 |
$dbi->delete(table => $table, |
1086 |
where => \%where, |
|
1087 |
append => $append, |
|
1088 |
filter => \%filter); |
|
bind_filter argument is chan...
|
1089 | |
renamed build_query to creat...
|
1090 |
Execute delete statement. |
1091 |
C<delete> method have C<table>, C<where>, C<append>, and C<filter> arguments. |
|
1092 |
C<table> is a table name. |
|
1093 |
C<where> is where clause. this must be hash reference. |
|
1094 |
C<append> is a string added at the end of the SQL statement. |
|
1095 |
C<filter> is filters when parameter binding is executed. |
|
cleanup
|
1096 |
Return value of C<delete()> is the count of affected rows. |
renamed build_query to creat...
|
1097 | |
removed DESTROY method(not b...
|
1098 |
B<Example:> |
packaging one directory
|
1099 | |
added insert, update, update...
|
1100 |
$dbi->delete(table => 'book', |
removed register_format()
|
1101 |
where => {id => 5}, |
1102 |
append => 'some statement', |
|
removed reconnect method
|
1103 |
filter => {id => 'encode_utf8'}); |
version 0.0901
|
1104 | |
removed DBIx::Custom commit ...
|
1105 |
=head2 C<delete_all> |
packaging one directory
|
1106 | |
cleanup
|
1107 |
$dbi->delete_all(table => $table); |
packaging one directory
|
1108 | |
renamed build_query to creat...
|
1109 |
Execute delete statement to delete all rows. |
1110 |
Arguments is same as C<delete> method, |
|
1111 |
except that C<delete_all> don't have C<where> argument. |
|
cleanup
|
1112 |
Return value of C<delete_all()> is the count of affected rows. |
bind_filter argument is chan...
|
1113 | |
removed DESTROY method(not b...
|
1114 |
B<Example:> |
removed register_format()
|
1115 |
|
added insert, update, update...
|
1116 |
$dbi->delete_all(table => 'book'); |
packaging one directory
|
1117 | |
added helper method
|
1118 |
=head2 C<(experimental) helper> |
1119 | ||
1120 |
$dbi->helper( |
|
1121 |
update_or_insert => sub { |
|
1122 |
my $self = shift; |
|
1123 |
# do something |
|
1124 |
}, |
|
1125 |
find_or_create => sub { |
|
1126 |
my $self = shift; |
|
1127 |
# do something |
|
1128 |
} |
|
1129 |
); |
|
1130 | ||
1131 |
Register helper methods. These method is called from L<DBIx::Custom> object directory. |
|
1132 | ||
1133 |
$dbi->update_or_insert; |
|
1134 |
$dbi->find_or_create; |
|
1135 | ||
cleanup
|
1136 |
=head2 C<insert> |
1137 | ||
1138 |
$dbi->insert(table => $table, |
|
1139 |
param => \%param, |
|
1140 |
append => $append, |
|
1141 |
filter => \%filter); |
|
1142 | ||
1143 |
Execute insert statement. |
|
1144 |
C<insert> method have C<table>, C<param>, C<append> |
|
1145 |
and C<filter> arguments. |
|
1146 |
C<table> is a table name. |
|
1147 |
C<param> is the pairs of column name value. this must be hash reference. |
|
1148 |
C<append> is a string added at the end of the SQL statement. |
|
1149 |
C<filter> is filters when parameter binding is executed. |
|
1150 |
This is overwrites C<default_bind_filter>. |
|
1151 |
Return value of C<insert()> is the count of affected rows. |
|
1152 | ||
1153 |
B<Example:> |
|
1154 | ||
added insert, update, update...
|
1155 |
$dbi->insert(table => 'book', |
cleanup
|
1156 |
param => {title => 'Perl', author => 'Taro'}, |
1157 |
append => "some statement", |
|
1158 |
filter => {title => 'encode_utf8'}) |
|
1159 | ||
added dbi_options attribute
|
1160 |
=head2 C<new> |
1161 | ||
1162 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname", |
|
1163 |
user => 'ken', password => '!LFKD%$&'); |
|
1164 | ||
1165 |
Create a new L<DBIx::Custom> object. |
|
1166 | ||
added experimental iterate_a...
|
1167 |
=head2 C<(experimental) iterate_all_columns> |
1168 | ||
1169 |
$dbi->iterate_all_columns( |
|
1170 |
sub { |
|
1171 |
my ($table, $column, $column_info) = @_; |
|
1172 |
|
|
1173 |
# do something; |
|
1174 |
} |
|
1175 |
); |
|
1176 | ||
1177 |
Iterate all columns of all tables. Argument is callback. |
|
1178 |
You can do anything by callback. |
|
1179 | ||
cleanup
|
1180 |
=head2 C<register_filter> |
1181 | ||
1182 |
$dbi->register_filter(%filters); |
|
1183 |
$dbi->register_filter(\%filters); |
|
1184 |
|
|
1185 |
Register filter. Registered filters is available in the following attributes |
|
1186 |
or arguments. |
|
1187 | ||
1188 |
=over 4 |
|
1189 | ||
1190 |
=item * |
|
1191 | ||
1192 |
C<filter> argument of C<insert()>, C<update()>, |
|
1193 |
C<update_all()>, C<delete()>, C<delete_all()>, C<select()> |
|
1194 |
methods |
|
1195 | ||
1196 |
=item * |
|
1197 | ||
1198 |
C<execute()> method |
|
1199 | ||
1200 |
=item * |
|
1201 | ||
1202 |
C<default_filter> and C<filter> of C<DBIx::Custom::Query> |
|
1203 | ||
1204 |
=item * |
|
1205 | ||
1206 |
C<default_filter> and C<filter> of C<DBIx::Custom::Result> |
|
1207 | ||
1208 |
=back |
|
1209 | ||
1210 |
B<Example:> |
|
1211 | ||
1212 |
$dbi->register_filter( |
|
1213 |
encode_utf8 => sub { |
|
1214 |
my $value = shift; |
|
1215 |
|
|
1216 |
require Encode; |
|
1217 |
|
|
1218 |
return Encode::encode('UTF-8', $value); |
|
1219 |
}, |
|
1220 |
decode_utf8 => sub { |
|
1221 |
my $value = shift; |
|
1222 |
|
|
1223 |
require Encode; |
|
1224 |
|
|
1225 |
return Encode::decode('UTF-8', $value) |
|
1226 |
} |
|
1227 |
); |
|
1228 | ||
added auto_filter method
|
1229 |
=head2 C<rollback> |
cleanup
|
1230 | |
1231 |
$dbi->rollback; |
|
1232 | ||
1233 |
Rollback transaction. |
|
1234 |
This is same as L<DBI>'s C<rollback>. |
|
1235 | ||
removed DBIx::Custom commit ...
|
1236 |
=head2 C<select> |
packaging one directory
|
1237 |
|
cleanup
|
1238 |
my $result = $dbi->select(table => $table, |
1239 |
column => [@column], |
|
1240 |
where => \%where, |
|
1241 |
append => $append, |
|
1242 |
relation => \%relation, |
|
1243 |
filter => \%filter); |
|
update document
|
1244 | |
renamed build_query to creat...
|
1245 |
Execute select statement. |
cleanup
|
1246 |
C<select> method have C<table>, C<column>, C<where>, C<append>, |
renamed build_query to creat...
|
1247 |
C<relation> and C<filter> arguments. |
1248 |
C<table> is a table name. |
|
cleanup
|
1249 |
C<where> is where clause. this is normally hash reference. |
renamed build_query to creat...
|
1250 |
C<append> is a string added at the end of the SQL statement. |
1251 |
C<filter> is filters when parameter binding is executed. |
|
update document
|
1252 | |
removed DESTROY method(not b...
|
1253 |
B<Example:> |
update document
|
1254 | |
added insert, update, update...
|
1255 |
# select * from book; |
1256 |
my $result = $dbi->select(table => 'book'); |
|
packaging one directory
|
1257 |
|
added insert, update, update...
|
1258 |
# select * from book where title = ?; |
1259 |
my $result = $dbi->select(table => 'book', where => {title => 'Perl'}); |
|
update document
|
1260 |
|
added insert, update, update...
|
1261 |
# select title, author from book where id = ? for update; |
cleanup
|
1262 |
my $result = $dbi->select( |
added insert, update, update...
|
1263 |
table => 'book', |
removed reconnect method
|
1264 |
column => ['title', 'author'], |
removed register_format()
|
1265 |
where => {id => 1}, |
1266 |
appned => 'for update' |
|
update document
|
1267 |
); |
1268 |
|
|
added insert, update, update...
|
1269 |
# select book.name as book_name from book, rental |
1270 |
# where book.id = rental.book_id; |
|
added commit method
|
1271 |
my $result = $dbi->select( |
added insert, update, update...
|
1272 |
table => ['book', 'rental'], |
1273 |
column => ['book.name as book_name'] |
|
1274 |
relation => {'book.id' => 'rental.book_id'} |
|
update document
|
1275 |
); |
1276 | ||
cleanup
|
1277 |
If you use more complex condition, |
1278 |
you can specify a array reference to C<where> argument. |
|
1279 | ||
1280 |
my $result = $dbi->select( |
|
added insert, update, update...
|
1281 |
table => 'book', |
cleanup
|
1282 |
column => ['title', 'author'], |
1283 |
where => ['{= title} or {like author}', |
|
1284 |
{title => '%Perl%', author => 'Ken'}] |
|
1285 |
); |
|
1286 | ||
1287 |
First element is a string. it contains tags, |
|
1288 |
such as "{= title} or {like author}". |
|
1289 |
Second element is paramters. |
|
1290 | ||
cleanup
|
1291 |
=head2 C<update> |
removed reconnect method
|
1292 | |
cleanup
|
1293 |
$dbi->update(table => $table, |
1294 |
param => \%params, |
|
1295 |
where => \%where, |
|
1296 |
append => $append, |
|
1297 |
filter => \%filter) |
|
removed reconnect method
|
1298 | |
cleanup
|
1299 |
Execute update statement. |
1300 |
C<update> method have C<table>, C<param>, C<where>, C<append> |
|
1301 |
and C<filter> arguments. |
|
1302 |
C<table> is a table name. |
|
1303 |
C<param> is column-value pairs. this must be hash reference. |
|
1304 |
C<where> is where clause. this must be hash reference. |
|
1305 |
C<append> is a string added at the end of the SQL statement. |
|
1306 |
C<filter> is filters when parameter binding is executed. |
|
1307 |
This is overwrites C<default_bind_filter>. |
|
1308 |
Return value of C<update()> is the count of affected rows. |
|
removed reconnect method
|
1309 | |
removed DBIx::Custom commit ...
|
1310 |
B<Example:> |
removed reconnect method
|
1311 | |
added insert, update, update...
|
1312 |
$dbi->update(table => 'book', |
cleanup
|
1313 |
param => {title => 'Perl', author => 'Taro'}, |
1314 |
where => {id => 5}, |
|
1315 |
append => "some statement", |
|
1316 |
filter => {title => 'encode_utf8'}); |
|
renamed build_query to creat...
|
1317 | |
added insert, update, update...
|
1318 |
=head2 C<(experimental) txn_scope> |
1319 | ||
1320 |
{ |
|
1321 |
my $txn = $dbi->txn_scope; |
|
1322 |
$dbi->insert(table => 'book', param => {title => 'Perl'}); |
|
1323 |
$dbi->insert(table => 'book', param => {title => 'Good days'}); |
|
1324 |
$txn->commit; |
|
1325 |
} |
|
1326 | ||
1327 |
Create transaction scope. If you escape scope(that is { .. }) and commited, |
|
1328 |
Rollback is automatically done. |
|
1329 | ||
1330 |
Note that this is feature of L<DBIx::TransactionManager> |
|
1331 |
L<DBIx::TransactionManager> is required. |
|
1332 | ||
cleanup
|
1333 |
=head2 C<(experimental) table> |
remove DBIx::Custom::Model
|
1334 | |
1335 |
$dbi->table('book', |
|
1336 |
insert => sub { ... }, |
|
1337 |
update => sub { ... } |
|
1338 |
); |
|
1339 |
|
|
1340 |
my $table = $dbi->table('book'); |
|
1341 | ||
1342 |
Create a L<DBIx::Custom::Table> object, |
|
1343 |
or get a L<DBIx::Custom::Table> object. |
|
1344 | ||
cleanup
|
1345 |
=head2 C<update_all> |
renamed build_query to creat...
|
1346 | |
cleanup
|
1347 |
$dbi->update_all(table => $table, |
1348 |
param => \%params, |
|
1349 |
filter => \%filter, |
|
1350 |
append => $append); |
|
renamed build_query to creat...
|
1351 | |
cleanup
|
1352 |
Execute update statement to update all rows. |
1353 |
Arguments is same as C<update> method, |
|
1354 |
except that C<update_all> don't have C<where> argument. |
|
1355 |
Return value of C<update_all()> is the count of affected rows. |
|
removed DBIx::Custom commit ...
|
1356 | |
1357 |
B<Example:> |
|
packaging one directory
|
1358 | |
added insert, update, update...
|
1359 |
$dbi->update_all(table => 'book', |
cleanup
|
1360 |
param => {author => 'taro'}, |
1361 |
filter => {author => 'encode_utf8'}); |
|
removed reconnect method
|
1362 | |
cleanup
|
1363 |
=head2 C<(deprecated) default_bind_filter> |
1364 | ||
1365 |
my $default_bind_filter = $dbi->default_bind_filter; |
|
1366 |
$dbi = $dbi->default_bind_filter($fname); |
|
1367 | ||
1368 |
Default filter when parameter binding is executed. |
|
1369 | ||
1370 |
=head2 C<(deprecated) default_fetch_filter> |
|
1371 | ||
1372 |
my $default_fetch_filter = $dbi->default_fetch_filter; |
|
1373 |
$dbi = $dbi->default_fetch_filter($fname); |
|
1374 | ||
1375 |
=head2 C<(deprecated) cache_method> |
|
1376 | ||
1377 |
$dbi = $dbi->cache_method(\&cache_method); |
|
1378 |
$cache_method = $dbi->cache_method |
|
1379 | ||
1380 |
Method to set and get caches. |
|
1381 | ||
1382 |
B<Example:> |
|
1383 | ||
1384 |
$dbi->cache_method( |
|
1385 |
sub { |
|
1386 |
my $self = shift; |
|
1387 |
|
|
1388 |
$self->{_cached} ||= {}; |
|
1389 |
|
|
1390 |
if (@_ > 1) { |
|
1391 |
$self->{_cached}{$_[0]} = $_[1] |
|
1392 |
} |
|
1393 |
else { |
|
1394 |
return $self->{_cached}{$_[0]} |
|
1395 |
} |
|
1396 |
} |
|
1397 |
); |
|
1398 | ||
DBIx::Custom is now stable
|
1399 |
=head1 STABILITY |
1400 | ||
1401 |
L<DBIx::Custom> is now stable. APIs keep backword compatible in the feature. |
|
1402 | ||
removed DESTROY method(not b...
|
1403 |
=head1 BUGS |
1404 | ||
renamed build_query to creat...
|
1405 |
Please tell me bugs if found. |
removed DESTROY method(not b...
|
1406 | |
1407 |
C<< <kimoto.yuki at gmail.com> >> |
|
1408 | ||
1409 |
L<http://github.com/yuki-kimoto/DBIx-Custom> |
|
1410 | ||
removed reconnect method
|
1411 |
=head1 AUTHOR |
1412 | ||
1413 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
|
version 0.0901
|
1414 | |
packaging one directory
|
1415 |
=head1 COPYRIGHT & LICENSE |
1416 | ||
1417 |
Copyright 2009 Yuki Kimoto, all rights reserved. |
|
1418 | ||
1419 |
This program is free software; you can redistribute it and/or modify it |
|
1420 |
under the same terms as Perl itself. |
|
1421 | ||
1422 |
=cut |
|
added cache_method attribute
|
1423 | |
1424 |