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