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