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