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