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