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