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