cleanup
|
1 |
package DBIx::Custom; |
2 | ||
renamed experimental DBIx::C...
|
3 |
our $VERSION = '0.1638'; |
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; |
table object call dbi object...
|
17 |
use DBIx::Custom::Table; |
cleanup
|
18 |
use DBIx::Custom::Tag; |
update document
|
19 |
use Encode qw/encode_utf8 decode_utf8/; |
packaging one directory
|
20 | |
fix tests
|
21 |
__PACKAGE__->attr( |
added experimental DBIx::Cus...
|
22 |
[qw/data_source dbh password user/], |
fix tests
|
23 |
cache => 1, |
table object call dbi object...
|
24 |
dbi_option => sub { {} }, |
cleanup
|
25 |
query_builder => sub { DBIx::Custom::QueryBuilder->new }, |
many changed
|
26 |
result_class => 'DBIx::Custom::Result', |
table object call dbi object...
|
27 |
base_table => sub { DBIx::Custom::Table->new(dbi => shift) } |
many changed
|
28 |
); |
29 | ||
30 |
__PACKAGE__->attr( |
|
31 |
cache_method => sub { |
|
32 |
sub { |
|
33 |
my $self = shift; |
|
34 |
|
|
35 |
$self->{_cached} ||= {}; |
|
36 |
|
|
37 |
if (@_ > 1) { |
|
38 |
$self->{_cached}{$_[0]} = $_[1] |
|
39 |
} |
|
40 |
else { |
|
41 |
return $self->{_cached}{$_[0]} |
|
42 |
} |
|
43 |
} |
|
44 |
} |
|
45 |
); |
|
46 | ||
47 |
__PACKAGE__->attr( |
|
fix tests
|
48 |
filters => sub { |
49 |
{ |
|
50 |
encode_utf8 => sub { encode_utf8($_[0]) }, |
|
51 |
decode_utf8 => sub { decode_utf8($_[0]) } |
|
52 |
} |
|
many changed
|
53 |
} |
fix tests
|
54 |
); |
cleanup
|
55 | |
56 |
# DBI methods |
|
57 |
foreach my $method (qw/begin_work commit rollback/) { |
|
58 |
my $code = sub { |
|
59 |
my $self = shift; |
|
60 |
my $ret = eval {$self->dbh->$method}; |
|
61 |
croak $@ if $@; |
|
62 |
return $ret; |
|
63 |
}; |
|
64 |
no strict 'refs'; |
|
65 |
my $pkg = __PACKAGE__; |
|
66 |
*{"${pkg}::$method"} = $code; |
|
67 |
}; |
|
68 | ||
added helper method
|
69 |
our $AUTOLOAD; |
70 | ||
71 |
sub AUTOLOAD { |
|
72 |
my $self = shift; |
|
73 | ||
renamed helper to method.
|
74 |
# Method name |
75 |
my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/; |
|
added helper method
|
76 | |
renamed helper to method.
|
77 |
# Method |
78 |
$self->{_methods} ||= {}; |
|
79 |
croak qq/Can't locate object method "$mname" via "$package"/ |
|
80 |
unless my $method = $self->{_methods}->{$mname}; |
|
81 |
|
|
82 |
return $self->$method(@_); |
|
added helper method
|
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 | ||
renamed helper to method.
|
149 |
sub method { |
added helper method
|
150 |
my $self = shift; |
151 |
|
|
152 |
# Merge |
|
renamed helper to method.
|
153 |
my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
154 |
$self->{_methods} = {%{$self->{_methods} || {}}, %$methods}; |
|
added helper method
|
155 |
|
156 |
return $self; |
|
157 |
} |
|
158 | ||
packaging one directory
|
159 |
sub connect { |
cleanup
|
160 |
my $self = ref $_[0] ? shift : shift->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})}; |
|
renamed DBIx::Custom::TagPro...
|
227 |
$self->_croak($@, qq{. Following SQL is executed. "$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 | |
make delete() using where ob...
|
255 |
# Where |
256 |
my $w; |
|
257 |
if (ref $where eq 'HASH') { |
|
258 |
my $clause = ['and']; |
|
259 |
push @$clause, "{= $_}" for keys %$where; |
|
260 |
$w = $self->where; |
|
261 |
$w->clause($clause); |
|
packaging one directory
|
262 |
} |
make delete() using where ob...
|
263 |
else { $w = $where } |
packaging one directory
|
264 |
|
make delete() using where ob...
|
265 |
croak qq{"where" must be hash refernce or DBIx::Custom::Where object} |
266 |
unless ref $w eq 'DBIx::Custom::Where'; |
|
267 |
|
|
268 |
croak qq{"where" must be specified} |
|
269 |
if "$w" eq '' && !$allow_delete_all; |
|
270 | ||
add tests
|
271 |
# Source of SQL |
make delete() using where ob...
|
272 |
my $source = "delete from $table $w"; |
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 |
|
added experimental not_exist...
|
336 |
# Bind |
337 |
my $bind = $self->_bind($params, $query->columns, $filter); |
|
cleanup
|
338 |
|
339 |
# Execute |
|
added experimental not_exist...
|
340 |
my $sth = $query->sth; |
cleanup
|
341 |
my $affected; |
added experimental not_exist...
|
342 |
eval {$affected = $sth->execute(@$bind)}; |
renamed DBIx::Custom::TagPro...
|
343 |
$self->_croak($@, qq{. Following SQL is executed. "$query->{sql}"}) if $@; |
cleanup
|
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}; |
|
removed experimental txn_sco...
|
441 |
$self->$cb($table, $column, $column_info); |
added experimental iterate_a...
|
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 |
} |
|
cleanup
|
455 | |
456 |
$self->register_tag( |
|
457 |
'?' => \&DBIx::Custom::Tag::placeholder, |
|
458 |
'=' => \&DBIx::Custom::Tag::equal, |
|
459 |
'<>' => \&DBIx::Custom::Tag::not_equal, |
|
460 |
'>' => \&DBIx::Custom::Tag::greater_than, |
|
461 |
'<' => \&DBIx::Custom::Tag::lower_than, |
|
462 |
'>=' => \&DBIx::Custom::Tag::greater_than_equal, |
|
463 |
'<=' => \&DBIx::Custom::Tag::lower_than_equal, |
|
464 |
'like' => \&DBIx::Custom::Tag::like, |
|
465 |
'in' => \&DBIx::Custom::Tag::in, |
|
466 |
'insert_param' => \&DBIx::Custom::Tag::insert_param, |
|
467 |
'update_param' => \&DBIx::Custom::Tag::update_param |
|
468 |
); |
|
added dbi_options attribute
|
469 |
|
470 |
return $self; |
|
471 |
} |
|
472 | ||
added experimental not_exist...
|
473 |
sub not_exists { bless {}, 'DBIx::Custom::NotExists' } |
474 | ||
cleanup
|
475 |
sub register_filter { |
476 |
my $invocant = shift; |
|
477 |
|
|
478 |
# Register filter |
|
479 |
my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
480 |
$invocant->filters({%{$invocant->filters}, %$filters}); |
|
481 |
|
|
482 |
return $invocant; |
|
483 |
} |
|
packaging one directory
|
484 | |
renamed DBIx::Custom::TagPro...
|
485 |
sub register_tag { shift->query_builder->register_tag(@_) } |
added register_tag_processor
|
486 | |
refactoring select
|
487 |
our %VALID_SELECT_ARGS |
added experimental sugar met...
|
488 |
= map { $_ => 1 } qw/table column where append relation filter query/; |
refactoring select
|
489 | |
packaging one directory
|
490 |
sub select { |
select, insert, update, upda...
|
491 |
my ($self, %args) = @_; |
packaging one directory
|
492 |
|
refactoring select
|
493 |
# Check arguments |
select, insert, update, upda...
|
494 |
foreach my $name (keys %args) { |
add tests
|
495 |
croak qq{"$name" is invalid argument} |
refactoring select
|
496 |
unless $VALID_SELECT_ARGS{$name}; |
497 |
} |
|
packaging one directory
|
498 |
|
refactoring select
|
499 |
# Arguments |
added table not specified ex...
|
500 |
my $table = $args{table}; |
501 |
my $tables = ref $table eq 'ARRAY' ? $table |
|
502 |
: defined $table ? [$table] |
|
503 |
: []; |
|
504 |
croak qq{"table" option must be specified} unless @$tables; |
|
select, insert, update, upda...
|
505 |
my $columns = $args{column} || []; |
update document
|
506 |
my $where = $args{where}; |
select, insert, update, upda...
|
507 |
my $relation = $args{relation}; |
508 |
my $append = $args{append}; |
|
509 |
my $filter = $args{filter}; |
|
packaging one directory
|
510 |
|
add tests
|
511 |
# Source of SQL |
renamed default_query_filter...
|
512 |
my $source = 'select '; |
packaging one directory
|
513 |
|
added commit method
|
514 |
# Column clause |
packaging one directory
|
515 |
if (@$columns) { |
516 |
foreach my $column (@$columns) { |
|
renamed default_query_filter...
|
517 |
$source .= "$column, "; |
packaging one directory
|
518 |
} |
renamed default_query_filter...
|
519 |
$source =~ s/, $/ /; |
packaging one directory
|
520 |
} |
521 |
else { |
|
renamed default_query_filter...
|
522 |
$source .= '* '; |
packaging one directory
|
523 |
} |
524 |
|
|
added commit method
|
525 |
# Table |
renamed default_query_filter...
|
526 |
$source .= 'from '; |
packaging one directory
|
527 |
foreach my $table (@$tables) { |
renamed default_query_filter...
|
528 |
$source .= "$table, "; |
packaging one directory
|
529 |
} |
renamed default_query_filter...
|
530 |
$source =~ s/, $/ /; |
packaging one directory
|
531 |
|
added commit method
|
532 |
# Where clause |
update document
|
533 |
my $param; |
fix select method empty wher...
|
534 |
my $wexists; |
added experimental DBIx::Cus...
|
535 |
if (ref $where eq 'HASH') { |
update document
|
536 |
$param = $where; |
fix select method empty wher...
|
537 |
$wexists = keys %$where; |
538 |
|
|
539 |
if ($wexists) { |
|
540 |
$source .= 'where ('; |
|
541 |
foreach my $where_key (keys %$where) { |
|
542 |
$source .= "{= $where_key} and "; |
|
543 |
} |
|
544 |
$source =~ s/ and $//; |
|
545 |
$source .= ') '; |
|
packaging one directory
|
546 |
} |
update document
|
547 |
} |
548 |
elsif (ref $where eq 'ARRAY') { |
|
fix select method empty wher...
|
549 |
my $w = $where->[0] || ''; |
update document
|
550 |
$param = $where->[1]; |
551 |
|
|
added experimental DBIx::Cus...
|
552 |
$wexists = $w =~ /\S/; |
553 |
$source .= "where ($w) " if $wexists; |
|
554 |
} |
|
555 |
elsif (ref $where eq 'DBIx::Custom::Where') { |
|
556 |
$param = $where->param; |
|
557 |
my $w = $where->to_string; |
|
558 |
$wexists = $w =~ /\S/; |
|
559 |
$source .= $w; |
|
packaging one directory
|
560 |
} |
561 |
|
|
added commit method
|
562 |
# Relation |
563 |
if ($relation) { |
|
fix select method empty wher...
|
564 |
$source .= $wexists ? "and " : "where "; |
added commit method
|
565 |
foreach my $rkey (keys %$relation) { |
renamed default_query_filter...
|
566 |
$source .= "$rkey = " . $relation->{$rkey} . " and "; |
packaging one directory
|
567 |
} |
568 |
} |
|
renamed default_query_filter...
|
569 |
$source =~ s/ and $//; |
added commit method
|
570 |
|
571 |
# Append some statement |
|
renamed default_query_filter...
|
572 |
$source .= " $append" if $append; |
packaging one directory
|
573 |
|
added experimental sugar met...
|
574 |
# Create query |
575 |
my $query = $self->create_query($source); |
|
576 |
return $query if $args{query}; |
|
577 |
|
|
packaging one directory
|
578 |
# Execute query |
added auto_filter method
|
579 |
my $result = $self->execute( |
added experimental sugar met...
|
580 |
$query, param => $param, filter => $filter, |
renamed auto_filter to apply...
|
581 |
table => $tables); |
packaging one directory
|
582 |
|
583 |
return $result; |
|
584 |
} |
|
585 | ||
remove DBIx::Custom::Model
|
586 |
sub table { |
587 |
my $self = shift; |
|
588 |
my $name = shift; |
|
589 |
|
|
590 |
# Create table |
|
591 |
$self->{_tables} ||= {}; |
|
table object call dbi object...
|
592 |
unless (defined $self->{_tables}->{$name}) { |
593 |
# Base table |
|
594 |
my $base_table = $self->base_table; |
|
595 |
|
|
596 |
# Base methods |
|
597 |
my $bmethods = ref $base_table->{_methods} eq 'HASH' |
|
598 |
? $base_table->{_methods} |
|
599 |
: {}; |
|
600 |
|
|
601 |
# Copy Methods |
|
602 |
my $methods = {}; |
|
603 |
$methods->{$_} = $bmethods->{$_} for keys %$bmethods; |
|
604 |
|
|
605 |
# Create table |
|
606 |
my $table = $base_table->new( |
|
607 |
dbi => $self, |
|
608 |
name => $name, |
|
609 |
base => $base_table, |
|
610 |
_methods => $methods |
|
611 |
); |
|
612 |
$self->{_tables}->{$name} = $table; |
|
613 |
} |
|
remove DBIx::Custom::Model
|
614 |
|
615 |
return $self->{_tables}{$name}; |
|
616 |
} |
|
617 | ||
cleanup
|
618 |
our %VALID_UPDATE_ARGS |
renamed auto_filter to apply...
|
619 |
= map { $_ => 1 } qw/table param |
added experimental sugar met...
|
620 |
where append filter allow_update_all query/; |
cleanup
|
621 | |
622 |
sub update { |
|
623 |
my ($self, %args) = @_; |
|
version 0.0901
|
624 |
|
cleanup
|
625 |
# Check arguments |
626 |
foreach my $name (keys %args) { |
|
627 |
croak qq{"$name" is invalid argument} |
|
628 |
unless $VALID_UPDATE_ARGS{$name}; |
|
removed reconnect method
|
629 |
} |
added cache_method attribute
|
630 |
|
cleanup
|
631 |
# Arguments |
632 |
my $table = $args{table} || ''; |
|
added table not specified ex...
|
633 |
croak qq{"table" option must be specified} unless $table; |
cleanup
|
634 |
my $param = $args{param} || {}; |
635 |
my $where = $args{where} || {}; |
|
636 |
my $append = $args{append} || ''; |
|
637 |
my $filter = $args{filter}; |
|
638 |
my $allow_update_all = $args{allow_update_all}; |
|
version 0.0901
|
639 |
|
cleanup
|
640 |
# Update keys |
641 |
my @update_keys = keys %$param; |
|
renamed fetch_rows to fetch_...
|
642 |
|
cleanup
|
643 |
# Where keys |
644 |
my @where_keys = keys %$where; |
|
removed reconnect method
|
645 |
|
cleanup
|
646 |
# Not exists where keys |
647 |
croak qq{"where" argument must be specified and } . |
|
648 |
qq{contains the pairs of column name and value} |
|
649 |
if !@where_keys && !$allow_update_all; |
|
removed experimental registe...
|
650 |
|
cleanup
|
651 |
# Update clause |
652 |
my $update_clause = '{update_param ' . join(' ', @update_keys) . '}'; |
|
removed experimental registe...
|
653 |
|
cleanup
|
654 |
# Where clause |
655 |
my $where_clause = ''; |
|
656 |
my $new_where = {}; |
|
removed reconnect method
|
657 |
|
cleanup
|
658 |
if (@where_keys) { |
659 |
$where_clause = 'where '; |
|
660 |
$where_clause .= "{= $_} and " for @where_keys; |
|
661 |
$where_clause =~ s/ and $//; |
|
removed reconnect method
|
662 |
} |
663 |
|
|
cleanup
|
664 |
# Source of SQL |
665 |
my $source = "update $table $update_clause $where_clause"; |
|
666 |
$source .= " $append" if $append; |
|
removed reconnect method
|
667 |
|
cleanup
|
668 |
# Rearrange parameters |
669 |
foreach my $wkey (@where_keys) { |
|
removed reconnect method
|
670 |
|
cleanup
|
671 |
if (exists $param->{$wkey}) { |
672 |
$param->{$wkey} = [$param->{$wkey}] |
|
673 |
unless ref $param->{$wkey} eq 'ARRAY'; |
|
674 |
|
|
675 |
push @{$param->{$wkey}}, $where->{$wkey}; |
|
676 |
} |
|
677 |
else { |
|
678 |
$param->{$wkey} = $where->{$wkey}; |
|
679 |
} |
|
removed reconnect method
|
680 |
} |
cleanup
|
681 |
|
added experimental sugar met...
|
682 |
# Create query |
683 |
my $query = $self->create_query($source); |
|
684 |
return $query if $args{query}; |
|
685 |
|
|
cleanup
|
686 |
# Execute query |
added experimental sugar met...
|
687 |
my $ret_val = $self->execute($query, param => $param, |
added auto_filter method
|
688 |
filter => $filter, |
renamed auto_filter to apply...
|
689 |
table => $table); |
cleanup
|
690 |
|
691 |
return $ret_val; |
|
removed reconnect method
|
692 |
} |
693 | ||
cleanup
|
694 |
sub update_all { shift->update(allow_update_all => 1, @_) }; |
695 | ||
cleanup
|
696 |
sub where { |
697 |
return DBIx::Custom::Where->new(query_builder => shift->query_builder) |
|
698 |
} |
|
added experimental DBIx::Cus...
|
699 | |
added experimental not_exist...
|
700 |
sub _bind { |
cleanup
|
701 |
my ($self, $params, $columns, $filter) = @_; |
removed reconnect method
|
702 |
|
cleanup
|
703 |
# bind values |
added experimental not_exist...
|
704 |
my @bind; |
add tests
|
705 |
|
removed reconnect method
|
706 |
# Build bind values |
707 |
my $count = {}; |
|
added experimental not_exist...
|
708 |
my $not_exists = {}; |
cleanup
|
709 |
foreach my $column (@$columns) { |
removed reconnect method
|
710 |
|
711 |
# Value |
|
added experimental not_exist...
|
712 |
my $value; |
713 |
if(ref $params->{$column} eq 'ARRAY') { |
|
714 |
my $i = $count->{$column} || 0; |
|
715 |
$i += $not_exists->{$column} || 0; |
|
716 |
my $found; |
|
717 |
for (my $k = $i; $i < @{$params->{$column}}; $k++) { |
|
718 |
if (ref $params->{$column}->[$k] eq 'DBIx::Custom::NotExists') { |
|
719 |
$not_exists->{$column}++; |
|
720 |
} |
|
721 |
else { |
|
722 |
$value = $params->{$column}->[$k]; |
|
723 |
$found = 1; |
|
724 |
last |
|
725 |
} |
|
726 |
} |
|
727 |
next unless $found; |
|
728 |
} |
|
729 |
else { $value = $params->{$column} } |
|
removed reconnect method
|
730 |
|
cleanup
|
731 |
# Filter |
732 |
my $f = $filter->{$column} || $self->{default_out_filter} || ''; |
|
cleanup
|
733 |
|
added experimental not_exist...
|
734 |
push @bind, $f ? $f->($value) : $value; |
removed reconnect method
|
735 |
|
736 |
# Count up |
|
737 |
$count->{$column}++; |
|
738 |
} |
|
739 |
|
|
added experimental not_exist...
|
740 |
return \@bind; |
removed reconnect method
|
741 |
} |
742 | ||
cleanup
|
743 |
sub _croak { |
744 |
my ($self, $error, $append) = @_; |
|
745 |
$append ||= ""; |
|
746 |
|
|
747 |
# Verbose |
|
748 |
if ($Carp::Verbose) { croak $error } |
|
749 |
|
|
750 |
# Not verbose |
|
751 |
else { |
|
752 |
|
|
753 |
# Remove line and module infromation |
|
754 |
my $at_pos = rindex($error, ' at '); |
|
755 |
$error = substr($error, 0, $at_pos); |
|
756 |
$error =~ s/\s+$//; |
|
757 |
|
|
758 |
croak "$error$append"; |
|
759 |
} |
|
760 |
} |
|
761 | ||
cleanup
|
762 |
# DEPRECATED! |
cleanup
|
763 |
__PACKAGE__->attr( |
764 |
dbi_options => sub { {} }, |
|
765 |
filter_check => 1 |
|
766 |
); |
|
renamed dbi_options to dbi_o...
|
767 | |
cleanup
|
768 |
# DEPRECATED! |
cleanup
|
769 |
sub default_bind_filter { |
770 |
my $self = shift; |
|
771 |
|
|
772 |
if (@_) { |
|
773 |
my $fname = $_[0]; |
|
774 |
|
|
775 |
if (@_ && !$fname) { |
|
776 |
$self->{default_out_filter} = undef; |
|
777 |
} |
|
778 |
else { |
|
many changed
|
779 |
croak qq{Filter "$fname" is not registered} |
cleanup
|
780 |
unless exists $self->filters->{$fname}; |
781 |
|
|
782 |
$self->{default_out_filter} = $self->filters->{$fname}; |
|
783 |
} |
|
784 |
return $self; |
|
785 |
} |
|
786 |
|
|
787 |
return $self->{default_out_filter}; |
|
788 |
} |
|
789 | ||
cleanup
|
790 |
# DEPRECATED! |
cleanup
|
791 |
sub default_fetch_filter { |
792 |
my $self = shift; |
|
793 |
|
|
794 |
if (@_) { |
|
many changed
|
795 |
my $fname = $_[0]; |
796 | ||
cleanup
|
797 |
if (@_ && !$fname) { |
798 |
$self->{default_in_filter} = undef; |
|
799 |
} |
|
800 |
else { |
|
many changed
|
801 |
croak qq{Filter "$fname" is not registered} |
cleanup
|
802 |
unless exists $self->filters->{$fname}; |
803 |
|
|
804 |
$self->{default_in_filter} = $self->filters->{$fname}; |
|
805 |
} |
|
806 |
|
|
807 |
return $self; |
|
808 |
} |
|
809 |
|
|
many changed
|
810 |
return $self->{default_in_filter}; |
cleanup
|
811 |
} |
812 | ||
cleanup
|
813 |
# DEPRECATED! |
renamed DBIx::Custom::TagPro...
|
814 |
sub register_tag_processor { |
815 |
return shift->query_builder->register_tag_processor(@_); |
|
816 |
} |
|
817 | ||
fixed DBIx::Custom::QueryBui...
|
818 |
1; |
819 | ||
removed reconnect method
|
820 |
=head1 NAME |
821 | ||
renamed build_query to creat...
|
822 |
DBIx::Custom - DBI interface, having hash parameter binding and filtering system |
removed reconnect method
|
823 | |
824 |
=head1 SYNOPSYS |
|
cleanup
|
825 | |
renamed build_query to creat...
|
826 |
use DBIx::Custom; |
renamed update tag to update...
|
827 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname", |
renamed dbi_options to dbi_o...
|
828 |
user => 'ken', password => '!LFKD%$&', |
829 |
dbi_option => {mysql_enable_utf8 => 1}); |
|
cleanup
|
830 | |
removed reconnect method
|
831 |
# Insert |
added insert, update, update...
|
832 |
$dbi->insert(table => 'book', |
renamed update tag to update...
|
833 |
param => {title => 'Perl', author => 'Ken'}, |
renamed dbi_options to dbi_o...
|
834 |
filter => {title => 'to_something'}); |
removed reconnect method
|
835 |
|
836 |
# Update |
|
added insert, update, update...
|
837 |
$dbi->update(table => 'book', |
renamed update tag to update...
|
838 |
param => {title => 'Perl', author => 'Ken'}, |
removed reconnect method
|
839 |
where => {id => 5}, |
renamed dbi_options to dbi_o...
|
840 |
filter => {title => 'to_something'}); |
removed reconnect method
|
841 |
|
842 |
# Update all |
|
added insert, update, update...
|
843 |
$dbi->update_all(table => 'book', |
renamed update tag to update...
|
844 |
param => {title => 'Perl'}, |
renamed dbi_options to dbi_o...
|
845 |
filter => {title => 'to_something'}); |
removed reconnect method
|
846 |
|
847 |
# Delete |
|
added insert, update, update...
|
848 |
$dbi->delete(table => 'book', |
removed reconnect method
|
849 |
where => {author => 'Ken'}, |
renamed dbi_options to dbi_o...
|
850 |
filter => {title => 'to_something'}); |
removed reconnect method
|
851 |
|
852 |
# Delete all |
|
added insert, update, update...
|
853 |
$dbi->delete_all(table => 'book'); |
cleanup
|
854 | |
removed reconnect method
|
855 |
# Select |
renamed fetch_rows to fetch_...
|
856 |
my $result = $dbi->select( |
added insert, update, update...
|
857 |
table => 'book', |
update document
|
858 |
column => [qw/author title/], |
859 |
where => {author => 'Ken'}, |
|
renamed dbi_options to dbi_o...
|
860 |
relation => {'book.id' => 'rental.book_id'}, |
updated document
|
861 |
append => 'order by id limit 5', |
renamed dbi_options to dbi_o...
|
862 |
filter => {title => 'to_something'} |
added commit method
|
863 |
); |
cleanup
|
864 | |
renamed build_query to creat...
|
865 |
# Execute SQL |
added insert, update, update...
|
866 |
$dbi->execute("select title from book"); |
removed register_format()
|
867 |
|
renamed build_query to creat...
|
868 |
# Execute SQL with hash binding and filtering |
added insert, update, update...
|
869 |
$dbi->execute("select id from book where {= author} and {like title}", |
removed register_format()
|
870 |
param => {author => 'ken', title => '%Perl%'}, |
renamed dbi_options to dbi_o...
|
871 |
filter => {title => 'to_something'}); |
removed reconnect method
|
872 | |
873 |
# Create query and execute it |
|
renamed build_query to creat...
|
874 |
my $query = $dbi->create_query( |
added insert, update, update...
|
875 |
"select id from book where {= author} and {like title}" |
removed reconnect method
|
876 |
); |
updated document
|
877 |
$dbi->execute($query, param => {author => 'Ken', title => '%Perl%'}) |
cleanup
|
878 | |
879 |
# Get DBI object |
|
880 |
my $dbh = $dbi->dbh; |
|
881 | ||
removed register_format()
|
882 |
# Fetch |
883 |
while (my $row = $result->fetch) { |
|
884 |
# ... |
|
885 |
} |
|
886 |
|
|
887 |
# Fetch hash |
|
888 |
while (my $row = $result->fetch_hash) { |
|
889 |
|
|
890 |
} |
|
891 |
|
|
renamed update tag to update...
|
892 |
=head1 DESCRIPTIONS |
removed reconnect method
|
893 | |
renamed build_query to creat...
|
894 |
L<DBIx::Custom> is one of L<DBI> interface modules, |
895 |
such as L<DBIx::Class>, L<DBIx::Simple>. |
|
removed reconnect method
|
896 | |
renamed build_query to creat...
|
897 |
This module is not O/R mapper. O/R mapper is useful, |
898 |
but you must learn many syntax of the O/R mapper, |
|
updated document
|
899 |
which is almost another language. |
900 |
Created SQL statement is offten not effcient and damage SQL performance. |
|
renamed build_query to creat...
|
901 |
so you have to execute raw SQL in the end. |
removed reconnect method
|
902 | |
renamed build_query to creat...
|
903 |
L<DBIx::Custom> is middle area between L<DBI> and O/R mapper. |
updated document
|
904 |
L<DBIx::Custom> provide flexible hash parameter binding and filtering system, |
added experimental expand me...
|
905 |
and suger methods, such as C<insert()>, C<update()>, C<delete()>, C<select()> |
updated document
|
906 |
to execute SQL easily. |
removed reconnect method
|
907 | |
updated document
|
908 |
L<DBIx::Custom> respects SQL. SQL is very complex and not beautiful, |
909 |
but de-facto standard, |
|
910 |
so all people learing database know it. |
|
renamed update tag to update...
|
911 |
If you already know SQL, |
912 |
you learn a little thing to use L<DBIx::Custom>. |
|
removed reconnect method
|
913 | |
pod fix
|
914 |
See L<DBIx::Custom::Guide> for more details. |
915 | ||
916 |
=head1 GUIDE |
|
917 | ||
918 |
L<DBIx::Custom::Guide> - L<DBIx::Custom> complete guide |
|
919 | ||
920 |
=head1 EXAMPLES |
|
921 | ||
922 |
L<DBIx::Custom Wiki|https://github.com/yuki-kimoto/DBIx-Custom/wiki> - Many useful examples |
|
updated document
|
923 | |
update document
|
924 |
=head1 ATTRIBUTES |
packaging one directory
|
925 | |
cleanup
|
926 |
=head2 C<cache> |
packaging one directory
|
927 | |
cleanup
|
928 |
my $cache = $dbi->cache; |
929 |
$dbi = $dbi->cache(1); |
|
removed DESTROY method(not b...
|
930 | |
cleanup
|
931 |
Enable parsed L<DBIx::Custom::Query> object caching. |
932 |
Default to 1. |
|
packaging one directory
|
933 | |
removed DBIx::Custom commit ...
|
934 |
=head2 C<data_source> |
packaging one directory
|
935 | |
cleanup
|
936 |
my $data_source = $dbi->data_source; |
cleanup
|
937 |
$dbi = $dbi->data_source("DBI:mysql:database=dbname"); |
removed DESTROY method(not b...
|
938 | |
cleanup
|
939 |
Data source. |
940 |
C<connect()> method use this value to connect the database. |
|
removed DESTROY method(not b...
|
941 | |
removed DBIx::Custom commit ...
|
942 |
=head2 C<dbh> |
packaging one directory
|
943 | |
cleanup
|
944 |
my $dbh = $dbi->dbh; |
945 |
$dbi = $dbi->dbh($dbh); |
|
packaging one directory
|
946 | |
cleanup
|
947 |
L<DBI> object. You can call all methods of L<DBI>. |
packaging one directory
|
948 | |
renamed dbi_options to dbi_o...
|
949 |
=head2 C<dbi_option> |
added dbi_options attribute
|
950 | |
renamed dbi_options to dbi_o...
|
951 |
my $dbi_option = $dbi->dbi_option; |
952 |
$dbi = $dbi->dbi_option($dbi_option); |
|
added dbi_options attribute
|
953 | |
954 |
DBI options. |
|
955 |
C<connect()> method use this value to connect the database. |
|
956 | ||
cleanup
|
957 |
Default filter when row is fetched. |
packaging one directory
|
958 | |
cleanup
|
959 |
=head2 C<filters> |
bind_filter argument is chan...
|
960 | |
cleanup
|
961 |
my $filters = $dbi->filters; |
962 |
$dbi = $dbi->filters(\%filters); |
|
packaging one directory
|
963 | |
cleanup
|
964 |
=head2 C<password> |
965 | ||
966 |
my $password = $dbi->password; |
|
967 |
$dbi = $dbi->password('lkj&le`@s'); |
|
968 | ||
969 |
Password. |
|
970 |
C<connect()> method use this value to connect the database. |
|
update document
|
971 | |
renamed update tag to update...
|
972 |
=head2 C<query_builder> |
added commit method
|
973 | |
renamed update tag to update...
|
974 |
my $sql_class = $dbi->query_builder; |
975 |
$dbi = $dbi->query_builder(DBIx::Custom::QueryBuilder->new); |
|
added commit method
|
976 | |
renamed update tag to update...
|
977 |
SQL builder. C<query_builder()> must be |
renamed build_query to creat...
|
978 |
the instance of L<DBIx::Custom::QueryBuilder> subclass. |
979 |
Default to L<DBIx::Custom::QueryBuilder> object. |
|
cleanup
|
980 | |
cleanup
|
981 |
=head2 C<result_class> |
cleanup
|
982 | |
cleanup
|
983 |
my $result_class = $dbi->result_class; |
984 |
$dbi = $dbi->result_class('DBIx::Custom::Result'); |
|
cleanup
|
985 | |
cleanup
|
986 |
Result class for select statement. |
987 |
Default to L<DBIx::Custom::Result>. |
|
cleanup
|
988 | |
cleanup
|
989 |
=head2 C<user> |
cleanup
|
990 | |
cleanup
|
991 |
my $user = $dbi->user; |
992 |
$dbi = $dbi->user('Ken'); |
|
cleanup
|
993 | |
cleanup
|
994 |
User name. |
995 |
C<connect()> method use this value to connect the database. |
|
996 |
|
|
997 |
=head1 METHODS |
|
added commit method
|
998 | |
cleanup
|
999 |
L<DBIx::Custom> inherits all methods from L<Object::Simple> |
1000 |
and implements the following new ones. |
|
added check_filter attribute
|
1001 | |
cleanup
|
1002 |
=head2 C<(experimental) apply_filter> |
added auto_filter method
|
1003 | |
renamed auto_filter to apply...
|
1004 |
$dbi->apply_filter( |
added auto_filter method
|
1005 |
$table, |
renamed auto_filter to apply...
|
1006 |
$column1 => {in => $infilter1, out => $outfilter1} |
1007 |
$column2 => {in => $infilter2, out => $outfilter2} |
|
1008 |
..., |
|
added auto_filter method
|
1009 |
); |
1010 | ||
renamed auto_filter to apply...
|
1011 |
C<apply_filter> is automatically filter for columns of table. |
added auto_filter method
|
1012 |
This have effect C<insert>, C<update>, C<delete>. C<select> |
cleanup
|
1013 |
and L<DBIx::Custom::Result> object. but this has'nt C<execute> method. |
1014 | ||
cleanup
|
1015 |
If you want to have effect C<execute()> method, use C<table> |
cleanup
|
1016 |
arguments. |
added auto_filter method
|
1017 | |
cleanup
|
1018 |
$result = $dbi->execute( |
1019 |
"select * from table1 where {= key1} and {= key2};", |
|
1020 |
param => {key1 => 1, key2 => 2}, |
|
renamed auto_filter to apply...
|
1021 |
table => ['table1'] |
cleanup
|
1022 |
); |
1023 |
|
|
added auto_filter method
|
1024 |
=head2 C<begin_work> |
added check_filter attribute
|
1025 | |
cleanup
|
1026 |
$dbi->begin_work; |
added check_filter attribute
|
1027 | |
cleanup
|
1028 |
Start transaction. |
1029 |
This is same as L<DBI>'s C<begin_work>. |
|
added commit method
|
1030 | |
cleanup
|
1031 |
L<DBIx::Custom> inherits all methods from L<Object::Simple> |
1032 |
and implements the following new ones. |
|
added commit method
|
1033 | |
added auto_filter method
|
1034 |
=head2 C<commit> |
cleanup
|
1035 | |
1036 |
$dbi->commit; |
|
1037 | ||
1038 |
Commit transaction. |
|
1039 |
This is same as L<DBI>'s C<commit>. |
|
1040 | ||
removed DBIx::Custom commit ...
|
1041 |
=head2 C<connect> |
packaging one directory
|
1042 | |
cleanup
|
1043 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname", |
update document
|
1044 |
user => 'ken', password => '!LFKD%$&'); |
bind_filter argument is chan...
|
1045 | |
cleanup
|
1046 |
Create a new L<DBIx::Custom> object and connect to the database. |
renamed build_query to creat...
|
1047 |
L<DBIx::Custom> is a wrapper of L<DBI>. |
cleanup
|
1048 |
C<AutoCommit> and C<RaiseError> options are true, |
renamed build_query to creat...
|
1049 |
and C<PrintError> option is false by default. |
packaging one directory
|
1050 | |
cleanup
|
1051 |
=head2 C<create_query> |
1052 |
|
|
1053 |
my $query = $dbi->create_query( |
|
added insert, update, update...
|
1054 |
"select * from book where {= author} and {like title};" |
cleanup
|
1055 |
); |
update document
|
1056 | |
cleanup
|
1057 |
Create the instance of L<DBIx::Custom::Query> from the source of SQL. |
1058 |
If you want to get high performance, |
|
1059 |
use C<create_query()> method and execute it by C<execute()> method |
|
1060 |
instead of suger methods. |
|
bind_filter argument is chan...
|
1061 | |
cleanup
|
1062 |
$dbi->execute($query, {author => 'Ken', title => '%Perl%'}); |
version 0.0901
|
1063 | |
cleanup
|
1064 |
=head2 C<execute> |
packaging one directory
|
1065 | |
cleanup
|
1066 |
my $result = $dbi->execute($query, param => $params, filter => \%filter); |
1067 |
my $result = $dbi->execute($source, param => $params, filter => \%filter); |
|
update document
|
1068 | |
cleanup
|
1069 |
Execute query or the source of SQL. |
1070 |
Query is L<DBIx::Custom::Query> object. |
|
1071 |
Return value is L<DBIx::Custom::Result> if select statement is executed, |
|
1072 |
or the count of affected rows if insert, update, delete statement is executed. |
|
version 0.0901
|
1073 | |
added experimental expand me...
|
1074 |
=head2 C<(experimental) expand> |
1075 | ||
1076 |
my %expand = $dbi->expand($source); |
|
1077 | ||
1078 |
The following hash |
|
1079 | ||
added insert, update, update...
|
1080 |
{book => {title => 'Perl', author => 'Ken'}} |
added experimental expand me...
|
1081 | |
1082 |
is expanded to |
|
1083 | ||
added insert, update, update...
|
1084 |
('book.title' => 'Perl', 'book.author' => 'Ken') |
added experimental expand me...
|
1085 | |
1086 |
This is used in C<select()> |
|
1087 | ||
removed DBIx::Custom commit ...
|
1088 |
=head2 C<delete> |
packaging one directory
|
1089 | |
cleanup
|
1090 |
$dbi->delete(table => $table, |
1091 |
where => \%where, |
|
1092 |
append => $append, |
|
added experimental sugar met...
|
1093 |
filter => \%filter, |
1094 |
query => 1); |
|
bind_filter argument is chan...
|
1095 | |
renamed build_query to creat...
|
1096 |
Execute delete statement. |
1097 |
C<delete> method have C<table>, C<where>, C<append>, and C<filter> arguments. |
|
1098 |
C<table> is a table name. |
|
1099 |
C<where> is where clause. this must be hash reference. |
|
1100 |
C<append> is a string added at the end of the SQL statement. |
|
1101 |
C<filter> is filters when parameter binding is executed. |
|
added experimental sugar met...
|
1102 |
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value. |
1103 |
default to 0. This is experimental. |
|
cleanup
|
1104 |
Return value of C<delete()> is the count of affected rows. |
renamed build_query to creat...
|
1105 | |
removed DBIx::Custom commit ...
|
1106 |
=head2 C<delete_all> |
packaging one directory
|
1107 | |
cleanup
|
1108 |
$dbi->delete_all(table => $table); |
packaging one directory
|
1109 | |
renamed build_query to creat...
|
1110 |
Execute delete statement to delete all rows. |
1111 |
Arguments is same as C<delete> method, |
|
1112 |
except that C<delete_all> don't have C<where> argument. |
|
cleanup
|
1113 |
Return value of C<delete_all()> is the count of affected rows. |
bind_filter argument is chan...
|
1114 | |
cleanup
|
1115 |
=head2 C<insert> |
1116 | ||
1117 |
$dbi->insert(table => $table, |
|
1118 |
param => \%param, |
|
1119 |
append => $append, |
|
added experimental sugar met...
|
1120 |
filter => \%filter, |
1121 |
query => 1); |
|
cleanup
|
1122 | |
1123 |
Execute insert statement. |
|
1124 |
C<insert> method have C<table>, C<param>, C<append> |
|
1125 |
and C<filter> arguments. |
|
1126 |
C<table> is a table name. |
|
1127 |
C<param> is the pairs of column name value. this must be hash reference. |
|
1128 |
C<append> is a string added at the end of the SQL statement. |
|
1129 |
C<filter> is filters when parameter binding is executed. |
|
added experimental sugar met...
|
1130 |
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value. |
1131 |
default to 0. This is experimental. |
|
cleanup
|
1132 |
This is overwrites C<default_bind_filter>. |
1133 |
Return value of C<insert()> is the count of affected rows. |
|
1134 | ||
pod fix
|
1135 |
=head2 C<(experimental) each_column> |
added experimental iterate_a...
|
1136 | |
pod fix
|
1137 |
$dbi->each_column( |
added experimental iterate_a...
|
1138 |
sub { |
added experimental not_exist...
|
1139 |
my ($self, $table, $column, $info) = @_; |
added experimental iterate_a...
|
1140 |
|
pod fix
|
1141 |
my $type = $info->{TYPE_NAME}; |
1142 |
|
|
1143 |
if ($type eq 'DATE') { |
|
1144 |
# ... |
|
1145 |
} |
|
added experimental iterate_a...
|
1146 |
} |
1147 |
); |
|
pod fix
|
1148 |
Get column informations from database. |
1149 |
Argument is callback. |
|
1150 |
You can do anything in callback. |
|
added experimental not_exist...
|
1151 |
Callback receive four arguments, dbi object, table name, |
1152 |
column name and columninformation. |
|
1153 | ||
1154 |
=head2 C<(experimental) method> |
|
1155 | ||
1156 |
$dbi->method( |
|
1157 |
update_or_insert => sub { |
|
1158 |
my $self = shift; |
|
1159 |
# do something |
|
1160 |
}, |
|
1161 |
find_or_create => sub { |
|
1162 |
my $self = shift; |
|
1163 |
# do something |
|
1164 |
} |
|
1165 |
); |
|
1166 | ||
1167 |
Register method. These method is called from L<DBIx::Custom> object directory. |
|
1168 | ||
1169 |
$dbi->update_or_insert; |
|
1170 |
$dbi->find_or_create; |
|
1171 | ||
1172 |
=head2 C<new> |
|
1173 | ||
1174 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname", |
|
1175 |
user => 'ken', password => '!LFKD%$&'); |
|
1176 | ||
1177 |
Create a new L<DBIx::Custom> object. |
|
1178 | ||
1179 |
=head2 C<(experimental) not_exists> |
|
1180 | ||
1181 |
my $not_exists = $dbi->not_exists; |
|
1182 | ||
1183 |
Get DBIx::Custom::NotExists object. |
|
experimental extended select...
|
1184 | |
cleanup
|
1185 |
=head2 C<register_filter> |
1186 | ||
1187 |
$dbi->register_filter(%filters); |
|
1188 |
$dbi->register_filter(\%filters); |
|
1189 |
|
|
1190 |
Register filter. Registered filters is available in the following attributes |
|
1191 |
or arguments. |
|
1192 | ||
1193 |
=over 4 |
|
1194 | ||
1195 |
=item * |
|
1196 | ||
1197 |
C<filter> argument of C<insert()>, C<update()>, |
|
1198 |
C<update_all()>, C<delete()>, C<delete_all()>, C<select()> |
|
1199 |
methods |
|
1200 | ||
1201 |
=item * |
|
1202 | ||
1203 |
C<execute()> method |
|
1204 | ||
1205 |
=item * |
|
1206 | ||
1207 |
C<default_filter> and C<filter> of C<DBIx::Custom::Query> |
|
1208 | ||
1209 |
=item * |
|
1210 | ||
1211 |
C<default_filter> and C<filter> of C<DBIx::Custom::Result> |
|
1212 | ||
1213 |
=back |
|
1214 | ||
renamed DBIx::Custom::TagPro...
|
1215 |
=head2 C<register_tag> |
added register_tag_processor
|
1216 | |
renamed DBIx::Custom::TagPro...
|
1217 |
$dbi->register_tag( |
added register_tag_processor
|
1218 |
limit => sub { |
1219 |
...; |
|
1220 |
} |
|
1221 |
); |
|
1222 | ||
cleanup
|
1223 |
Register tag. |
added register_tag_processor
|
1224 | |
added auto_filter method
|
1225 |
=head2 C<rollback> |
cleanup
|
1226 | |
1227 |
$dbi->rollback; |
|
1228 | ||
1229 |
Rollback transaction. |
|
1230 |
This is same as L<DBI>'s C<rollback>. |
|
1231 | ||
removed DBIx::Custom commit ...
|
1232 |
=head2 C<select> |
packaging one directory
|
1233 |
|
cleanup
|
1234 |
my $result = $dbi->select(table => $table, |
1235 |
column => [@column], |
|
1236 |
where => \%where, |
|
1237 |
append => $append, |
|
1238 |
relation => \%relation, |
|
added experimental sugar met...
|
1239 |
filter => \%filter, |
1240 |
query => 1); |
|
update document
|
1241 | |
renamed build_query to creat...
|
1242 |
Execute select statement. |
cleanup
|
1243 |
C<select> method have C<table>, C<column>, C<where>, C<append>, |
renamed build_query to creat...
|
1244 |
C<relation> and C<filter> arguments. |
1245 |
C<table> is a table name. |
|
cleanup
|
1246 |
C<where> is where clause. this is normally hash reference. |
renamed build_query to creat...
|
1247 |
C<append> is a string added at the end of the SQL statement. |
1248 |
C<filter> is filters when parameter binding is executed. |
|
added experimental sugar met...
|
1249 |
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value. |
1250 |
default to 0. This is experimental. |
|
update document
|
1251 | |
cleanup
|
1252 |
If you use more complex condition, |
1253 |
you can specify a array reference to C<where> argument. |
|
1254 | ||
1255 |
my $result = $dbi->select( |
|
added insert, update, update...
|
1256 |
table => 'book', |
cleanup
|
1257 |
column => ['title', 'author'], |
1258 |
where => ['{= title} or {like author}', |
|
1259 |
{title => '%Perl%', author => 'Ken'}] |
|
1260 |
); |
|
1261 | ||
1262 |
First element is a string. it contains tags, |
|
1263 |
such as "{= title} or {like author}". |
|
1264 |
Second element is paramters. |
|
1265 | ||
cleanup
|
1266 |
=head2 C<update> |
removed reconnect method
|
1267 | |
cleanup
|
1268 |
$dbi->update(table => $table, |
1269 |
param => \%params, |
|
1270 |
where => \%where, |
|
1271 |
append => $append, |
|
added experimental sugar met...
|
1272 |
filter => \%filter, |
1273 |
query => 1) |
|
removed reconnect method
|
1274 | |
cleanup
|
1275 |
Execute update statement. |
1276 |
C<update> method have C<table>, C<param>, C<where>, C<append> |
|
1277 |
and C<filter> arguments. |
|
1278 |
C<table> is a table name. |
|
1279 |
C<param> is column-value pairs. this must be hash reference. |
|
1280 |
C<where> is where clause. this must be hash reference. |
|
1281 |
C<append> is a string added at the end of the SQL statement. |
|
1282 |
C<filter> is filters when parameter binding is executed. |
|
added experimental sugar met...
|
1283 |
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value. |
1284 |
default to 0. This is experimental. |
|
cleanup
|
1285 |
This is overwrites C<default_bind_filter>. |
1286 |
Return value of C<update()> is the count of affected rows. |
|
removed reconnect method
|
1287 | |
cleanup
|
1288 |
=head2 C<(experimental) table> |
remove DBIx::Custom::Model
|
1289 | |
renamed experimental DBIx::C...
|
1290 |
$dbi->table('book')->method( |
remove DBIx::Custom::Model
|
1291 |
insert => sub { ... }, |
1292 |
update => sub { ... } |
|
1293 |
); |
|
1294 |
|
|
1295 |
my $table = $dbi->table('book'); |
|
1296 | ||
1297 |
Create a L<DBIx::Custom::Table> object, |
|
1298 |
or get a L<DBIx::Custom::Table> object. |
|
1299 | ||
cleanup
|
1300 |
=head2 C<update_all> |
renamed build_query to creat...
|
1301 | |
cleanup
|
1302 |
$dbi->update_all(table => $table, |
1303 |
param => \%params, |
|
1304 |
filter => \%filter, |
|
1305 |
append => $append); |
|
renamed build_query to creat...
|
1306 | |
cleanup
|
1307 |
Execute update statement to update all rows. |
1308 |
Arguments is same as C<update> method, |
|
1309 |
except that C<update_all> don't have C<where> argument. |
|
1310 |
Return value of C<update_all()> is the count of affected rows. |
|
removed DBIx::Custom commit ...
|
1311 | |
fix tests
|
1312 |
=head2 C<(experimental) where> |
1313 | ||
1314 |
my $where = $dbi->where; |
|
1315 | ||
1316 |
Create a new L<DBIx::Custom::Where> object. |
|
1317 | ||
cleanup
|
1318 |
=head2 C<cache_method> |
cleanup
|
1319 | |
1320 |
$dbi = $dbi->cache_method(\&cache_method); |
|
1321 |
$cache_method = $dbi->cache_method |
|
1322 | ||
1323 |
Method to set and get caches. |
|
1324 | ||
cleanup
|
1325 |
=head1 Tags |
1326 | ||
1327 |
The following tags is available. |
|
1328 | ||
1329 |
=head2 C<?> |
|
1330 | ||
1331 |
Placeholder tag. |
|
1332 | ||
1333 |
{? NAME} -> ? |
|
1334 | ||
1335 |
=head2 C<=> |
|
1336 | ||
1337 |
Equal tag. |
|
1338 | ||
1339 |
{= NAME} -> NAME = ? |
|
1340 | ||
1341 |
=head2 C<E<lt>E<gt>> |
|
1342 | ||
1343 |
Not equal tag. |
|
1344 | ||
1345 |
{<> NAME} -> NAME <> ? |
|
1346 | ||
1347 |
=head2 C<E<lt>> |
|
1348 | ||
1349 |
Lower than tag |
|
1350 | ||
1351 |
{< NAME} -> NAME < ? |
|
1352 | ||
1353 |
=head2 C<E<gt>> |
|
1354 | ||
1355 |
Greater than tag |
|
1356 | ||
1357 |
{> NAME} -> NAME > ? |
|
1358 | ||
1359 |
=head2 C<E<gt>=> |
|
1360 | ||
1361 |
Greater than or equal tag |
|
1362 | ||
1363 |
{>= NAME} -> NAME >= ? |
|
1364 | ||
1365 |
=head2 C<E<lt>=> |
|
1366 | ||
1367 |
Lower than or equal tag |
|
1368 | ||
1369 |
{<= NAME} -> NAME <= ? |
|
1370 | ||
1371 |
=head2 C<like> |
|
1372 | ||
1373 |
Like tag |
|
1374 | ||
1375 |
{like NAME} -> NAME like ? |
|
1376 | ||
1377 |
=head2 C<in> |
|
1378 | ||
1379 |
In tag. |
|
1380 | ||
1381 |
{in NAME COUNT} -> NAME in [?, ?, ..] |
|
1382 | ||
1383 |
=head2 C<insert_param> |
|
1384 | ||
1385 |
Insert parameter tag. |
|
1386 | ||
1387 |
{insert_param NAME1 NAME2} -> (NAME1, NAME2) values (?, ?) |
|
1388 | ||
1389 |
=head2 C<update_param> |
|
1390 | ||
1391 |
Updata parameter tag. |
|
1392 | ||
1393 |
{update_param NAME1 NAME2} -> set NAME1 = ?, NAME2 = ? |
|
1394 | ||
DBIx::Custom is now stable
|
1395 |
=head1 STABILITY |
1396 | ||
cleanup
|
1397 |
L<DBIx::Custom> is stable. APIs keep backword compatible |
1398 |
except experimental one in the feature. |
|
DBIx::Custom is now stable
|
1399 | |
removed DESTROY method(not b...
|
1400 |
=head1 BUGS |
1401 | ||
renamed build_query to creat...
|
1402 |
Please tell me bugs if found. |
removed DESTROY method(not b...
|
1403 | |
1404 |
C<< <kimoto.yuki at gmail.com> >> |
|
1405 | ||
1406 |
L<http://github.com/yuki-kimoto/DBIx-Custom> |
|
1407 | ||
removed reconnect method
|
1408 |
=head1 AUTHOR |
1409 | ||
1410 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
|
version 0.0901
|
1411 | |
packaging one directory
|
1412 |
=head1 COPYRIGHT & LICENSE |
1413 | ||
cleanup
|
1414 |
Copyright 2009-2011 Yuki Kimoto, all rights reserved. |
packaging one directory
|
1415 | |
1416 |
This program is free software; you can redistribute it and/or modify it |
|
1417 |
under the same terms as Perl itself. |
|
1418 | ||
1419 |
=cut |
|
added cache_method attribute
|
1420 | |
1421 |