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