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