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