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