cleanup
|
1 |
package DBIx::Custom; |
2 | ||
cleanup
|
3 |
our $VERSION = '0.1618'; |
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; |
update document
|
16 |
use Encode qw/encode_utf8 decode_utf8/; |
packaging one directory
|
17 | |
cleanup
|
18 |
__PACKAGE__->attr([qw/data_source dbh default_bind_filter |
19 |
default_fetch_filter password user/]); |
|
added cache_method attribute
|
20 | |
add cache attribute
|
21 |
__PACKAGE__->attr(cache => 1); |
added cache_method attribute
|
22 |
__PACKAGE__->attr(cache_method => sub { |
23 |
sub { |
|
24 |
my $self = shift; |
|
25 |
|
|
26 |
$self->{_cached} ||= {}; |
|
27 |
|
|
28 |
if (@_ > 1) { |
|
29 |
$self->{_cached}{$_[0]} = $_[1] |
|
30 |
} |
|
31 |
else { |
|
32 |
return $self->{_cached}{$_[0]} |
|
33 |
} |
|
34 |
} |
|
35 |
}); |
|
removed register_format()
|
36 | |
cleanup
|
37 |
__PACKAGE__->dual_attr('filters', default => sub { {} }, |
38 |
inherit => 'hash_copy'); |
|
added check_filter attribute
|
39 |
__PACKAGE__->attr(filter_check => 1); |
cleanup
|
40 |
__PACKAGE__->attr(query_builder => sub {DBIx::Custom::QueryBuilder->new}); |
41 |
__PACKAGE__->attr(result_class => 'DBIx::Custom::Result'); |
|
42 | ||
43 |
# DBI methods |
|
44 |
foreach my $method (qw/begin_work commit rollback/) { |
|
45 |
my $code = sub { |
|
46 |
my $self = shift; |
|
47 |
my $ret = eval {$self->dbh->$method}; |
|
48 |
croak $@ if $@; |
|
49 |
return $ret; |
|
50 |
}; |
|
51 |
no strict 'refs'; |
|
52 |
my $pkg = __PACKAGE__; |
|
53 |
*{"${pkg}::$method"} = $code; |
|
54 |
}; |
|
55 | ||
56 |
# Regster filter |
|
57 |
__PACKAGE__->register_filter( |
|
58 |
encode_utf8 => sub { encode_utf8($_[0]) }, |
|
59 |
decode_utf8 => sub { decode_utf8($_[0]) } |
|
60 |
); |
|
added check_filter attribute
|
61 | |
packaging one directory
|
62 |
sub connect { |
removed register_format()
|
63 |
my $proto = shift; |
64 |
|
|
65 |
# Create |
|
66 |
my $self = ref $proto ? $proto : $proto->new(@_); |
|
update document
|
67 |
|
68 |
# Information |
|
packaging one directory
|
69 |
my $data_source = $self->data_source; |
70 |
my $user = $self->user; |
|
71 |
my $password = $self->password; |
|
72 |
|
|
removed experimental registe...
|
73 |
|
update document
|
74 |
# Connect |
select, insert, update, upda...
|
75 |
my $dbh = eval {DBI->connect( |
packaging one directory
|
76 |
$data_source, |
77 |
$user, |
|
78 |
$password, |
|
79 |
{ |
|
80 |
RaiseError => 1, |
|
81 |
PrintError => 0, |
|
82 |
AutoCommit => 1, |
|
83 |
} |
|
84 |
)}; |
|
85 |
|
|
update document
|
86 |
# Connect error |
packaging one directory
|
87 |
croak $@ if $@; |
88 |
|
|
update document
|
89 |
# Database handle |
packaging one directory
|
90 |
$self->dbh($dbh); |
update document
|
91 |
|
packaging one directory
|
92 |
return $self; |
93 |
} |
|
94 | ||
cleanup
|
95 |
sub create_query { |
96 |
my ($self, $source) = @_; |
|
update document
|
97 |
|
cleanup
|
98 |
# Cache |
99 |
my $cache = $self->cache; |
|
update document
|
100 |
|
cleanup
|
101 |
# Create query |
102 |
my $query; |
|
103 |
if ($cache) { |
|
104 |
|
|
105 |
# Get query |
|
106 |
my $q = $self->cache_method->($self, $source); |
|
107 |
|
|
108 |
# Create query |
|
109 |
$query = DBIx::Custom::Query->new($q) if $q; |
|
110 |
} |
|
111 |
|
|
112 |
unless ($query) { |
|
cleanup insert
|
113 | |
cleanup
|
114 |
# Create SQL object |
115 |
my $builder = $self->query_builder; |
|
116 |
|
|
117 |
# Create query |
|
118 |
$query = $builder->build_query($source); |
|
removed register_format()
|
119 | |
cleanup
|
120 |
# Cache query |
121 |
$self->cache_method->($self, $source, |
|
122 |
{sql => $query->sql, |
|
123 |
columns => $query->columns}) |
|
124 |
if $cache; |
|
cleanup insert
|
125 |
} |
126 |
|
|
cleanup
|
127 |
# Prepare statement handle |
128 |
my $sth; |
|
129 |
eval { $sth = $self->dbh->prepare($query->{sql})}; |
|
130 |
$self->_croak($@, qq{. SQL: "$query->{sql}"}) if $@; |
|
packaging one directory
|
131 |
|
cleanup
|
132 |
# Set statement handle |
133 |
$query->sth($sth); |
|
packaging one directory
|
134 |
|
cleanup
|
135 |
return $query; |
packaging one directory
|
136 |
} |
137 | ||
cleanup
|
138 |
our %VALID_DELETE_ARGS |
139 |
= map { $_ => 1 } qw/table where append filter allow_delete_all/; |
|
cleanup update and update_al...
|
140 | |
cleanup
|
141 |
sub delete { |
select, insert, update, upda...
|
142 |
my ($self, %args) = @_; |
cleanup update and update_al...
|
143 |
|
144 |
# Check arguments |
|
select, insert, update, upda...
|
145 |
foreach my $name (keys %args) { |
add tests
|
146 |
croak qq{"$name" is invalid argument} |
cleanup
|
147 |
unless $VALID_DELETE_ARGS{$name}; |
cleanup update and update_al...
|
148 |
} |
149 |
|
|
150 |
# Arguments |
|
select, insert, update, upda...
|
151 |
my $table = $args{table} || ''; |
152 |
my $where = $args{where} || {}; |
|
cleanup
|
153 |
my $append = $args{append}; |
select, insert, update, upda...
|
154 |
my $filter = $args{filter}; |
cleanup
|
155 |
my $allow_delete_all = $args{allow_delete_all}; |
packaging one directory
|
156 |
|
157 |
# Where keys |
|
removed register_format()
|
158 |
my @where_keys = keys %$where; |
packaging one directory
|
159 |
|
160 |
# Not exists where keys |
|
add tests
|
161 |
croak qq{"where" argument must be specified and } . |
162 |
qq{contains the pairs of column name and value} |
|
cleanup
|
163 |
if !@where_keys && !$allow_delete_all; |
packaging one directory
|
164 |
|
165 |
# Where clause |
|
166 |
my $where_clause = ''; |
|
167 |
if (@where_keys) { |
|
168 |
$where_clause = 'where '; |
|
add tests
|
169 |
$where_clause .= "{= $_} and " for @where_keys; |
packaging one directory
|
170 |
$where_clause =~ s/ and $//; |
171 |
} |
|
172 |
|
|
add tests
|
173 |
# Source of SQL |
cleanup
|
174 |
my $source = "delete from $table $where_clause"; |
add tests
|
175 |
$source .= " $append" if $append; |
packaging one directory
|
176 |
|
177 |
# Execute query |
|
cleanup
|
178 |
my $ret_val = $self->execute($source, param => $where, |
add tests
|
179 |
filter => $filter); |
packaging one directory
|
180 |
|
181 |
return $ret_val; |
|
182 |
} |
|
183 | ||
cleanup
|
184 |
sub delete_all { shift->delete(allow_delete_all => 1, @_) } |
packaging one directory
|
185 | |
cleanup
|
186 |
our %VALID_EXECUTE_ARGS = map { $_ => 1 } qw/param filter/; |
refactoring delete and delet...
|
187 | |
cleanup
|
188 |
sub execute{ |
189 |
my ($self, $query, %args) = @_; |
|
refactoring delete and delet...
|
190 |
|
191 |
# Check arguments |
|
select, insert, update, upda...
|
192 |
foreach my $name (keys %args) { |
add tests
|
193 |
croak qq{"$name" is invalid argument} |
cleanup
|
194 |
unless $VALID_EXECUTE_ARGS{$name}; |
refactoring delete and delet...
|
195 |
} |
196 |
|
|
cleanup
|
197 |
my $params = $args{param} || {}; |
packaging one directory
|
198 |
|
cleanup
|
199 |
# First argument is the soruce of SQL |
200 |
$query = $self->create_query($query) |
|
201 |
unless ref $query; |
|
packaging one directory
|
202 |
|
cleanup
|
203 |
my $filter = $args{filter} || $query->filter || {}; |
packaging one directory
|
204 |
|
cleanup
|
205 |
# Create bind value |
206 |
my $bind_values = $self->_build_bind_values($query, $params, $filter); |
|
207 |
|
|
208 |
# Execute |
|
209 |
my $sth = $query->sth; |
|
210 |
my $affected; |
|
211 |
eval {$affected = $sth->execute(@$bind_values)}; |
|
212 |
$self->_croak($@) if $@; |
|
213 |
|
|
214 |
# Return resultset if select statement is executed |
|
215 |
if ($sth->{NUM_OF_FIELDS}) { |
|
216 |
|
|
217 |
# Create result |
|
218 |
my $result = $self->result_class->new( |
|
219 |
sth => $sth, |
|
220 |
default_filter => $self->default_fetch_filter, |
|
221 |
filters => $self->filters, |
|
222 |
filter_check => $self->filter_check |
|
223 |
); |
|
224 | ||
225 |
return $result; |
|
226 |
} |
|
227 |
return $affected; |
|
228 |
} |
|
229 | ||
230 |
our %VALID_INSERT_ARGS = map { $_ => 1 } qw/table param append filter/; |
|
231 | ||
232 |
sub insert { |
|
233 |
my ($self, %args) = @_; |
|
234 | ||
235 |
# Check arguments |
|
236 |
foreach my $name (keys %args) { |
|
237 |
croak qq{"$name" is invalid argument} |
|
238 |
unless $VALID_INSERT_ARGS{$name}; |
|
packaging one directory
|
239 |
} |
240 |
|
|
cleanup
|
241 |
# Arguments |
242 |
my $table = $args{table} || ''; |
|
243 |
my $param = $args{param} || {}; |
|
244 |
my $append = $args{append} || ''; |
|
245 |
my $filter = $args{filter}; |
|
246 |
|
|
247 |
# Insert keys |
|
248 |
my @insert_keys = keys %$param; |
|
249 |
|
|
250 |
# Templte for insert |
|
251 |
my $source = "insert into $table {insert_param " |
|
252 |
. join(' ', @insert_keys) . '}'; |
|
add tests
|
253 |
$source .= " $append" if $append; |
packaging one directory
|
254 |
|
255 |
# Execute query |
|
cleanup
|
256 |
my $ret_val = $self->execute($source, param => $param, |
257 |
filter => $filter); |
|
packaging one directory
|
258 |
|
259 |
return $ret_val; |
|
260 |
} |
|
261 | ||
cleanup
|
262 |
sub register_filter { |
263 |
my $invocant = shift; |
|
264 |
|
|
265 |
# Register filter |
|
266 |
my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
267 |
$invocant->filters({%{$invocant->filters}, %$filters}); |
|
268 |
|
|
269 |
return $invocant; |
|
270 |
} |
|
packaging one directory
|
271 | |
refactoring select
|
272 |
our %VALID_SELECT_ARGS |
added commit method
|
273 |
= map { $_ => 1 } qw/table column where append relation filter param/; |
refactoring select
|
274 | |
packaging one directory
|
275 |
sub select { |
select, insert, update, upda...
|
276 |
my ($self, %args) = @_; |
packaging one directory
|
277 |
|
refactoring select
|
278 |
# Check arguments |
select, insert, update, upda...
|
279 |
foreach my $name (keys %args) { |
add tests
|
280 |
croak qq{"$name" is invalid argument} |
refactoring select
|
281 |
unless $VALID_SELECT_ARGS{$name}; |
282 |
} |
|
packaging one directory
|
283 |
|
refactoring select
|
284 |
# Arguments |
select, insert, update, upda...
|
285 |
my $tables = $args{table} || []; |
removed register_format()
|
286 |
$tables = [$tables] unless ref $tables eq 'ARRAY'; |
select, insert, update, upda...
|
287 |
my $columns = $args{column} || []; |
update document
|
288 |
my $where = $args{where}; |
select, insert, update, upda...
|
289 |
my $relation = $args{relation}; |
290 |
my $append = $args{append}; |
|
291 |
my $filter = $args{filter}; |
|
packaging one directory
|
292 |
|
add tests
|
293 |
# Source of SQL |
renamed default_query_filter...
|
294 |
my $source = 'select '; |
packaging one directory
|
295 |
|
added commit method
|
296 |
# Column clause |
packaging one directory
|
297 |
if (@$columns) { |
298 |
foreach my $column (@$columns) { |
|
renamed default_query_filter...
|
299 |
$source .= "$column, "; |
packaging one directory
|
300 |
} |
renamed default_query_filter...
|
301 |
$source =~ s/, $/ /; |
packaging one directory
|
302 |
} |
303 |
else { |
|
renamed default_query_filter...
|
304 |
$source .= '* '; |
packaging one directory
|
305 |
} |
306 |
|
|
added commit method
|
307 |
# Table |
renamed default_query_filter...
|
308 |
$source .= 'from '; |
packaging one directory
|
309 |
foreach my $table (@$tables) { |
renamed default_query_filter...
|
310 |
$source .= "$table, "; |
packaging one directory
|
311 |
} |
renamed default_query_filter...
|
312 |
$source =~ s/, $/ /; |
packaging one directory
|
313 |
|
added commit method
|
314 |
# Where clause |
update document
|
315 |
my $param; |
316 |
if (ref $where eq 'HASH') { |
|
317 |
$param = $where; |
|
318 |
$source .= 'where ('; |
|
319 |
foreach my $where_key (keys %$where) { |
|
renamed default_query_filter...
|
320 |
$source .= "{= $where_key} and "; |
packaging one directory
|
321 |
} |
update document
|
322 |
$source =~ s/ and $//; |
323 |
$source .= ') '; |
|
324 |
} |
|
325 |
elsif (ref $where eq 'ARRAY') { |
|
326 |
my$where_str = $where->[0] || ''; |
|
327 |
$param = $where->[1]; |
|
328 |
|
|
329 |
$source .= "where ($where_str) "; |
|
packaging one directory
|
330 |
} |
331 |
|
|
added commit method
|
332 |
# Relation |
333 |
if ($relation) { |
|
update document
|
334 |
$source .= $where ? "and " : "where "; |
added commit method
|
335 |
foreach my $rkey (keys %$relation) { |
renamed default_query_filter...
|
336 |
$source .= "$rkey = " . $relation->{$rkey} . " and "; |
packaging one directory
|
337 |
} |
338 |
} |
|
renamed default_query_filter...
|
339 |
$source =~ s/ and $//; |
added commit method
|
340 |
|
341 |
# Append some statement |
|
renamed default_query_filter...
|
342 |
$source .= " $append" if $append; |
packaging one directory
|
343 |
|
344 |
# Execute query |
|
update document
|
345 |
my $result = $self->execute($source, param => $param, |
346 |
filter => $filter); |
|
packaging one directory
|
347 |
|
348 |
return $result; |
|
349 |
} |
|
350 | ||
cleanup
|
351 |
our %VALID_UPDATE_ARGS |
352 |
= map { $_ => 1 } qw/table param where append filter allow_update_all/; |
|
353 | ||
354 |
sub update { |
|
355 |
my ($self, %args) = @_; |
|
version 0.0901
|
356 |
|
cleanup
|
357 |
# Check arguments |
358 |
foreach my $name (keys %args) { |
|
359 |
croak qq{"$name" is invalid argument} |
|
360 |
unless $VALID_UPDATE_ARGS{$name}; |
|
removed reconnect method
|
361 |
} |
added cache_method attribute
|
362 |
|
cleanup
|
363 |
# Arguments |
364 |
my $table = $args{table} || ''; |
|
365 |
my $param = $args{param} || {}; |
|
366 |
my $where = $args{where} || {}; |
|
367 |
my $append = $args{append} || ''; |
|
368 |
my $filter = $args{filter}; |
|
369 |
my $allow_update_all = $args{allow_update_all}; |
|
version 0.0901
|
370 |
|
cleanup
|
371 |
# Update keys |
372 |
my @update_keys = keys %$param; |
|
renamed fetch_rows to fetch_...
|
373 |
|
cleanup
|
374 |
# Where keys |
375 |
my @where_keys = keys %$where; |
|
removed reconnect method
|
376 |
|
cleanup
|
377 |
# Not exists where keys |
378 |
croak qq{"where" argument must be specified and } . |
|
379 |
qq{contains the pairs of column name and value} |
|
380 |
if !@where_keys && !$allow_update_all; |
|
removed experimental registe...
|
381 |
|
cleanup
|
382 |
# Update clause |
383 |
my $update_clause = '{update_param ' . join(' ', @update_keys) . '}'; |
|
removed experimental registe...
|
384 |
|
cleanup
|
385 |
# Where clause |
386 |
my $where_clause = ''; |
|
387 |
my $new_where = {}; |
|
removed reconnect method
|
388 |
|
cleanup
|
389 |
if (@where_keys) { |
390 |
$where_clause = 'where '; |
|
391 |
$where_clause .= "{= $_} and " for @where_keys; |
|
392 |
$where_clause =~ s/ and $//; |
|
removed reconnect method
|
393 |
} |
394 |
|
|
cleanup
|
395 |
# Source of SQL |
396 |
my $source = "update $table $update_clause $where_clause"; |
|
397 |
$source .= " $append" if $append; |
|
removed reconnect method
|
398 |
|
cleanup
|
399 |
# Rearrange parameters |
400 |
foreach my $wkey (@where_keys) { |
|
removed reconnect method
|
401 |
|
cleanup
|
402 |
if (exists $param->{$wkey}) { |
403 |
$param->{$wkey} = [$param->{$wkey}] |
|
404 |
unless ref $param->{$wkey} eq 'ARRAY'; |
|
405 |
|
|
406 |
push @{$param->{$wkey}}, $where->{$wkey}; |
|
407 |
} |
|
408 |
else { |
|
409 |
$param->{$wkey} = $where->{$wkey}; |
|
410 |
} |
|
removed reconnect method
|
411 |
} |
cleanup
|
412 |
|
413 |
# Execute query |
|
414 |
my $ret_val = $self->execute($source, param => $param, |
|
415 |
filter => $filter); |
|
416 |
|
|
417 |
return $ret_val; |
|
removed reconnect method
|
418 |
} |
419 | ||
cleanup
|
420 |
sub update_all { shift->update(allow_update_all => 1, @_) }; |
421 | ||
removed reconnect method
|
422 |
sub _build_bind_values { |
423 |
my ($self, $query, $params, $filter) = @_; |
|
424 |
|
|
425 |
# binding values |
|
426 |
my @bind_values; |
|
add tests
|
427 | |
428 |
# Filter |
|
429 |
$filter ||= {}; |
|
430 |
|
|
431 |
# Parameter |
|
432 |
$params ||= {}; |
|
433 |
|
|
434 |
# Check filter |
|
435 |
$self->_check_filter($self->filters, $filter, |
|
436 |
$self->default_bind_filter, $params) |
|
437 |
if $self->filter_check; |
|
removed reconnect method
|
438 |
|
439 |
# Build bind values |
|
440 |
my $count = {}; |
|
441 |
foreach my $column (@{$query->columns}) { |
|
442 |
|
|
443 |
# Value |
|
444 |
my $value = ref $params->{$column} eq 'ARRAY' |
|
445 |
? $params->{$column}->[$count->{$column} || 0] |
|
446 |
: $params->{$column}; |
|
447 |
|
|
add tests
|
448 |
# Filtering |
renamed default_query_filter...
|
449 |
my $fname = $filter->{$column} || $self->default_bind_filter || ''; |
add tests
|
450 |
my $filter_func = $fname ? $self->filters->{$fname} : undef; |
removed reconnect method
|
451 |
push @bind_values, $filter_func |
452 |
? $filter_func->($value) |
|
453 |
: $value; |
|
454 |
|
|
455 |
# Count up |
|
456 |
$count->{$column}++; |
|
457 |
} |
|
458 |
|
|
459 |
return \@bind_values; |
|
460 |
} |
|
461 | ||
add tests
|
462 |
sub _check_filter { |
463 |
my ($self, $filters, $filter, $default_filter, $params) = @_; |
|
464 |
|
|
465 |
# Filter name not exists |
|
466 |
foreach my $fname (values %$filter) { |
|
467 |
croak qq{Bind filter "$fname" is not registered} |
|
468 |
unless exists $filters->{$fname}; |
|
469 |
} |
|
470 |
|
|
471 |
# Default filter name not exists |
|
472 |
croak qq{Default bind filter "$default_filter" is not registered} |
|
473 |
if $default_filter && ! exists $filters->{$default_filter}; |
|
474 |
|
|
475 |
# Column name not exists |
|
476 |
foreach my $column (keys %$filter) { |
|
477 |
|
|
478 |
croak qq{Column name "$column" in bind filter is not found in paramters} |
|
479 |
unless exists $params->{$column}; |
|
480 |
} |
|
481 |
} |
|
482 | ||
cleanup
|
483 |
sub _croak { |
484 |
my ($self, $error, $append) = @_; |
|
485 |
$append ||= ""; |
|
486 |
|
|
487 |
# Verbose |
|
488 |
if ($Carp::Verbose) { croak $error } |
|
489 |
|
|
490 |
# Not verbose |
|
491 |
else { |
|
492 |
|
|
493 |
# Remove line and module infromation |
|
494 |
my $at_pos = rindex($error, ' at '); |
|
495 |
$error = substr($error, 0, $at_pos); |
|
496 |
$error =~ s/\s+$//; |
|
497 |
|
|
498 |
croak "$error$append"; |
|
499 |
} |
|
500 |
} |
|
501 | ||
fixed DBIx::Custom::QueryBui...
|
502 |
1; |
503 | ||
removed reconnect method
|
504 |
=head1 NAME |
505 | ||
renamed build_query to creat...
|
506 |
DBIx::Custom - DBI interface, having hash parameter binding and filtering system |
removed reconnect method
|
507 | |
508 |
=head1 SYNOPSYS |
|
cleanup
|
509 | |
renamed build_query to creat...
|
510 |
Connect to the database. |
511 |
|
|
512 |
use DBIx::Custom; |
|
renamed update tag to update...
|
513 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname", |
removed reconnect method
|
514 |
user => 'ken', password => '!LFKD%$&'); |
cleanup
|
515 | |
renamed build_query to creat...
|
516 |
Insert, update, and delete |
cleanup
|
517 | |
removed reconnect method
|
518 |
# Insert |
519 |
$dbi->insert(table => 'books', |
|
renamed update tag to update...
|
520 |
param => {title => 'Perl', author => 'Ken'}, |
removed reconnect method
|
521 |
filter => {title => 'encode_utf8'}); |
522 |
|
|
523 |
# Update |
|
524 |
$dbi->update(table => 'books', |
|
renamed update tag to update...
|
525 |
param => {title => 'Perl', author => 'Ken'}, |
removed reconnect method
|
526 |
where => {id => 5}, |
527 |
filter => {title => 'encode_utf8'}); |
|
528 |
|
|
529 |
# Update all |
|
530 |
$dbi->update_all(table => 'books', |
|
renamed update tag to update...
|
531 |
param => {title => 'Perl'}, |
removed reconnect method
|
532 |
filter => {title => 'encode_utf8'}); |
533 |
|
|
534 |
# Delete |
|
535 |
$dbi->delete(table => 'books', |
|
536 |
where => {author => 'Ken'}, |
|
537 |
filter => {title => 'encode_utf8'}); |
|
538 |
|
|
539 |
# Delete all |
|
540 |
$dbi->delete_all(table => 'books'); |
|
cleanup
|
541 | |
renamed build_query to creat...
|
542 |
Select |
cleanup
|
543 | |
removed reconnect method
|
544 |
# Select |
545 |
my $result = $dbi->select(table => 'books'); |
|
renamed fetch_rows to fetch_...
|
546 |
|
renamed build_query to creat...
|
547 |
# Select, more complex |
renamed fetch_rows to fetch_...
|
548 |
my $result = $dbi->select( |
update document
|
549 |
table => 'books', |
550 |
column => [qw/author title/], |
|
551 |
where => {author => 'Ken'}, |
|
updated document
|
552 |
append => 'order by id limit 5', |
renamed build_query to creat...
|
553 |
filter => {title => 'encode_utf8'} |
renamed fetch_rows to fetch_...
|
554 |
); |
added commit method
|
555 |
|
renamed build_query to creat...
|
556 |
# Select, join table |
added commit method
|
557 |
my $result = $dbi->select( |
renamed build_query to creat...
|
558 |
table => ['books', 'rental'], |
559 |
column => ['books.name as book_name'] |
|
added commit method
|
560 |
relation => {'books.id' => 'rental.book_id'} |
561 |
); |
|
updated document
|
562 |
|
563 |
# Select, more flexible where |
|
564 |
my $result = $dbi->select( |
|
565 |
table => 'books', |
|
566 |
where => ['{= author} and {like title}', |
|
567 |
{author => 'Ken', title => '%Perl%'}] |
|
568 |
); |
|
cleanup
|
569 | |
renamed build_query to creat...
|
570 |
Execute SQL |
cleanup
|
571 | |
renamed build_query to creat...
|
572 |
# Execute SQL |
removed register_format()
|
573 |
$dbi->execute("select title from books"); |
574 |
|
|
renamed build_query to creat...
|
575 |
# Execute SQL with hash binding and filtering |
updated document
|
576 |
$dbi->execute("select id from books where {= author} and {like title}", |
removed register_format()
|
577 |
param => {author => 'ken', title => '%Perl%'}, |
renamed build_query to creat...
|
578 |
filter => {title => 'encode_utf8'}); |
removed reconnect method
|
579 | |
580 |
# Create query and execute it |
|
renamed build_query to creat...
|
581 |
my $query = $dbi->create_query( |
updated document
|
582 |
"select id from books where {= author} and {like title}" |
removed reconnect method
|
583 |
); |
updated document
|
584 |
$dbi->execute($query, param => {author => 'Ken', title => '%Perl%'}) |
cleanup
|
585 | |
updated document
|
586 |
Other features. |
cleanup
|
587 | |
removed register_format()
|
588 |
# Default filter |
renamed default_query_filter...
|
589 |
$dbi->default_bind_filter('encode_utf8'); |
removed register_format()
|
590 |
$dbi->default_fetch_filter('decode_utf8'); |
cleanup
|
591 | |
592 |
# Get DBI object |
|
593 |
my $dbh = $dbi->dbh; |
|
594 | ||
595 |
Fetch row. |
|
596 | ||
removed register_format()
|
597 |
# Fetch |
598 |
while (my $row = $result->fetch) { |
|
599 |
# ... |
|
600 |
} |
|
601 |
|
|
602 |
# Fetch hash |
|
603 |
while (my $row = $result->fetch_hash) { |
|
604 |
|
|
605 |
} |
|
606 |
|
|
renamed update tag to update...
|
607 |
=head1 DESCRIPTIONS |
removed reconnect method
|
608 | |
renamed build_query to creat...
|
609 |
=head2 1. Features |
removed reconnect method
|
610 | |
renamed build_query to creat...
|
611 |
L<DBIx::Custom> is one of L<DBI> interface modules, |
612 |
such as L<DBIx::Class>, L<DBIx::Simple>. |
|
removed reconnect method
|
613 | |
renamed build_query to creat...
|
614 |
This module is not O/R mapper. O/R mapper is useful, |
615 |
but you must learn many syntax of the O/R mapper, |
|
updated document
|
616 |
which is almost another language. |
617 |
Created SQL statement is offten not effcient and damage SQL performance. |
|
renamed build_query to creat...
|
618 |
so you have to execute raw SQL in the end. |
removed reconnect method
|
619 | |
renamed build_query to creat...
|
620 |
L<DBIx::Custom> is middle area between L<DBI> and O/R mapper. |
updated document
|
621 |
L<DBIx::Custom> provide flexible hash parameter binding and filtering system, |
622 |
and suger methods, such as C<select()>, C<update()>, C<delete()>, C<select()> |
|
623 |
to execute SQL easily. |
|
removed reconnect method
|
624 | |
updated document
|
625 |
L<DBIx::Custom> respects SQL. SQL is very complex and not beautiful, |
626 |
but de-facto standard, |
|
627 |
so all people learing database know it. |
|
renamed update tag to update...
|
628 |
If you already know SQL, |
629 |
you learn a little thing to use L<DBIx::Custom>. |
|
removed reconnect method
|
630 | |
renamed update tag to update...
|
631 |
=head2 2. Connect to the database |
removed reconnect method
|
632 | |
renamed build_query to creat...
|
633 |
C<connect()> method create a new L<DBIx::Custom> |
634 |
object and connect to the database. |
|
removed reconnect method
|
635 | |
renamed build_query to creat...
|
636 |
use DBIx::Custom; |
renamed update tag to update...
|
637 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname", |
renamed build_query to creat...
|
638 |
user => 'ken', password => '!LFKD%$&'); |
639 | ||
renamed update tag to update...
|
640 |
If database is SQLite, use L<DBIx::Custom::SQLite> instead. |
641 |
you connect database easily. |
|
update document
|
642 | |
643 |
use DBIx::Custom::SQLite; |
|
renamed update tag to update...
|
644 |
my $dbi = DBIx::Custom::SQLite->connect(database => 'dbname'); |
update document
|
645 |
|
cleanup
|
646 |
If database is MySQL, use L<DBIx::Custom::MySQL>. |
update document
|
647 | |
648 |
use DBIx::Custom::MySQL; |
|
renamed update tag to update...
|
649 |
my $dbi = DBIx::Custom::MySQL->connect( |
650 |
database => 'dbname', |
|
651 |
user => 'ken', |
|
652 |
password => '!LFKD%$&' |
|
653 |
); |
|
update document
|
654 | |
renamed update tag to update...
|
655 |
=head2 3. Suger methods |
renamed build_query to creat...
|
656 | |
657 |
L<DBIx::Custom> has suger methods, such as C<insert()>, C<update()>, |
|
renamed update tag to update...
|
658 |
C<delete()> or C<select()>. If you want to do small works, |
updated document
|
659 |
You don't have to create SQL statements. |
renamed build_query to creat...
|
660 | |
update document
|
661 |
=head3 insert() |
662 | ||
updated document
|
663 |
Execute insert statement. |
664 | ||
update document
|
665 |
$dbi->insert(table => 'books', |
renamed update tag to update...
|
666 |
param => {title => 'Perl', author => 'Ken'}); |
update document
|
667 | |
668 |
The following SQL is executed. |
|
669 | ||
updated document
|
670 |
insert into (title, author) values (?, ?); |
update document
|
671 | |
renamed update tag to update...
|
672 |
The values of C<title> and C<author> is embedded into the placeholders. |
update document
|
673 | |
renamed update tag to update...
|
674 |
C<append> and C<filter> argument can be specified. |
675 |
See also "METHODS" section. |
|
update document
|
676 | |
677 |
=head3 update() |
|
678 | ||
updated document
|
679 |
Execute update statement. |
680 | ||
update document
|
681 |
$dbi->update(table => 'books', |
renamed update tag to update...
|
682 |
param => {title => 'Perl', author => 'Ken'}, |
update document
|
683 |
where => {id => 5}); |
684 | ||
685 |
The following SQL is executed. |
|
686 | ||
687 |
update books set title = ?, author = ?; |
|
688 | ||
renamed update tag to update...
|
689 |
The values of C<title> and C<author> is embedded into the placeholders. |
update document
|
690 | |
renamed update tag to update...
|
691 |
C<append> and C<filter> argument can be specified. |
692 |
See also "METHOD" section. |
|
update document
|
693 | |
renamed update tag to update...
|
694 |
If you want to update all rows, use C<update_all()> method. |
update document
|
695 | |
696 |
=head3 delete() |
|
697 | ||
updated document
|
698 |
Execute delete statement. |
699 | ||
update document
|
700 |
$dbi->delete(table => 'books', |
701 |
where => {author => 'Ken'}); |
|
702 | ||
703 |
The following SQL is executed. |
|
704 | ||
705 |
delete from books where id = ?; |
|
706 | ||
updated document
|
707 |
The value of C<id> is embedded into the placehodler. |
update document
|
708 | |
renamed update tag to update...
|
709 |
C<append> and C<filter> argument can be specified. |
710 |
see also "METHODS" section. |
|
update document
|
711 | |
renamed update tag to update...
|
712 |
If you want to delete all rows, use C<delete_all()> method. |
update document
|
713 | |
714 |
=head3 select() |
|
715 | ||
updated document
|
716 |
Execute select statement, only C<table> argument specified : |
update document
|
717 | |
718 |
my $result = $dbi->select(table => 'books'); |
|
719 | ||
720 |
The following SQL is executed. |
|
721 | ||
722 |
select * from books; |
|
723 | ||
724 |
the result of C<select()> method is L<DBIx::Custom::Result> object. |
|
renamed update tag to update...
|
725 |
You can fetch a row by C<fetch()> method. |
update document
|
726 | |
727 |
while (my $row = $result->fetch) { |
|
728 |
my $title = $row->[0]; |
|
729 |
my $author = $row->[1]; |
|
730 |
} |
|
731 | ||
732 |
L<DBIx::Custom::Result> has various methods to fetch row. |
|
renamed update tag to update...
|
733 |
See "4. Fetch row". |
update document
|
734 | |
renamed update tag to update...
|
735 |
C<column> and C<where> arguments specified. |
update document
|
736 | |
737 |
my $result = $dbi->select( |
|
738 |
table => 'books', |
|
739 |
column => [qw/author title/], |
|
renamed update tag to update...
|
740 |
where => {author => 'Ken'} |
741 |
); |
|
update document
|
742 | |
743 |
The following SQL is executed. |
|
744 | ||
745 |
select author, title from books where author = ?; |
|
746 | ||
renamed update tag to update...
|
747 |
the value of C<author> is embdded into the placeholder. |
update document
|
748 | |
updated document
|
749 |
If you want to join tables, specify C<relation> argument. |
update document
|
750 | |
751 |
my $result = $dbi->select( |
|
752 |
table => ['books', 'rental'], |
|
753 |
column => ['books.name as book_name'] |
|
754 |
relation => {'books.id' => 'rental.book_id'} |
|
755 |
); |
|
756 | ||
757 |
The following SQL is executed. |
|
758 | ||
renamed update tag to update...
|
759 |
select books.name as book_name from books, rental |
update document
|
760 |
where books.id = rental.book_id; |
761 | ||
renamed update tag to update...
|
762 |
If you want to add some string to the end of SQL statement, |
763 |
use C<append> argument. |
|
update document
|
764 | |
765 |
my $result = $dbi->select( |
|
766 |
table => 'books', |
|
767 |
where => {author => 'Ken'}, |
|
768 |
append => 'order by price limit 5', |
|
769 |
); |
|
770 | ||
771 |
The following SQL is executed. |
|
772 | ||
773 |
select * books where author = ? order by price limit 5; |
|
774 | ||
renamed update tag to update...
|
775 |
C<filter> argument can be specified. |
776 |
see also "METHODS" section. |
|
update document
|
777 | |
renamed update tag to update...
|
778 |
=head2 4. Fetch row |
update document
|
779 | |
updated document
|
780 |
C<select()> method return L<DBIx::Custom::Result> object. |
781 |
You can fetch row by various methods. |
|
renamed update tag to update...
|
782 |
Note that in this section, array means array reference, |
783 |
and hash meanse hash reference. |
|
update document
|
784 | |
785 |
Fetch row into array. |
|
786 |
|
|
787 |
while (my $row = $result->fetch) { |
|
788 |
my $author = $row->[0]; |
|
789 |
my $title = $row->[1]; |
|
790 |
|
|
791 |
} |
|
792 | ||
793 |
Fetch only a first row into array. |
|
794 | ||
795 |
my $row = $result->fetch_first; |
|
796 | ||
797 |
Fetch multiple rows into array of array. |
|
798 | ||
799 |
while (my $rows = $result->fetch_multi(5)) { |
|
800 |
my $first_author = $rows->[0][0]; |
|
801 |
my $first_title = $rows->[0][1]; |
|
802 |
my $second_author = $rows->[1][0]; |
|
803 |
my $second_value = $rows->[1][1]; |
|
804 |
|
|
805 |
} |
|
806 |
|
|
807 |
Fetch all rows into array of array. |
|
808 | ||
809 |
my $rows = $result->fetch_all; |
|
810 | ||
811 |
Fetch row into hash. |
|
812 | ||
813 |
# Fetch a row into hash |
|
814 |
while (my $row = $result->fetch_hash) { |
|
815 |
my $title = $row->{title}; |
|
816 |
my $author = $row->{author}; |
|
817 |
|
|
818 |
} |
|
819 | ||
820 |
Fetch only a first row into hash |
|
821 | ||
822 |
my $row = $result->fetch_hash_first; |
|
823 |
|
|
824 |
Fetch multiple rows into array of hash |
|
825 | ||
826 |
while (my $rows = $result->fetch_hash_multi(5)) { |
|
827 |
my $first_title = $rows->[0]{title}; |
|
828 |
my $first_author = $rows->[0]{author}; |
|
829 |
my $second_title = $rows->[1]{title}; |
|
830 |
my $second_author = $rows->[1]{author}; |
|
831 |
|
|
832 |
} |
|
833 |
|
|
834 |
Fetch all rows into array of hash |
|
835 | ||
836 |
my $rows = $result->fetch_hash_all; |
|
removed DESTROY method(not b...
|
837 | |
cleanup
|
838 |
If you want to access statement handle of L<DBI>, use C<sth> attribute. |
removed DESTROY method(not b...
|
839 | |
update document
|
840 |
my $sth = $result->sth; |
removed DESTROY method(not b...
|
841 | |
renamed update tag to update...
|
842 |
=head2 5. Hash parameter binding |
removed reconnect method
|
843 | |
update document
|
844 |
L<DBIx::Custom> provides hash parameter binding. |
845 | ||
renamed update tag to update...
|
846 |
At frist, I show normal parameter binding. |
update document
|
847 | |
848 |
use DBI; |
|
849 |
my $dbh = DBI->connect(...); |
|
850 |
my $sth = $dbh->prepare( |
|
851 |
"select * from books where author = ? and title like ?;" |
|
852 |
); |
|
853 |
$sth->execute('Ken', '%Perl%'); |
|
854 | ||
updated document
|
855 |
This is very good way because database system can enable SQL caching, |
renamed update tag to update...
|
856 |
and parameter is quoted automatically. this is secure. |
update document
|
857 | |
updated document
|
858 |
L<DBIx::Custom> hash parameter binding system improve |
renamed update tag to update...
|
859 |
normal parameter binding to use hash parameter. |
update document
|
860 | |
861 |
my $result = $dbi->execute( |
|
862 |
"select * from books where {= author} and {like title};" |
|
863 |
param => {author => 'Ken', title => '%Perl%'} |
|
864 |
); |
|
865 | ||
866 |
This is same as the normal way, execpt that the parameter is hash. |
|
renamed update tag to update...
|
867 |
{= author} and {like title} is called C<tag>. |
868 |
tag is expand to placeholder string internally. |
|
update document
|
869 | |
870 |
select * from books where {= author} and {like title} |
|
871 |
-> select * from books where author = ? and title like ?; |
|
872 | ||
updated document
|
873 |
The following tags is available. |
874 | ||
875 |
[TAG] [REPLACED] |
|
876 |
{? NAME} -> ? |
|
877 |
{= NAME} -> NAME = ? |
|
878 |
{<> NAME} -> NAME <> ? |
|
879 |
|
|
880 |
{< NAME} -> NAME < ? |
|
881 |
{> NAME} -> NAME > ? |
|
882 |
{>= NAME} -> NAME >= ? |
|
883 |
{<= NAME} -> NAME <= ? |
|
884 |
|
|
885 |
{like NAME} -> NAME like ? |
|
886 |
{in NAME COUNT} -> NAME in [?, ?, ..] |
|
887 |
|
|
renamed update tag to update...
|
888 |
{insert_param NAME1 NAME2} -> (NAME1, NAME2) values (?, ?) |
889 |
{update_param NAME1 NAME2} -> set NAME1 = ?, NAME2 = ? |
|
updated document
|
890 | |
891 |
See also L<DBIx::Custom::QueryBuilder>. |
|
892 | ||
removed DBIx::Custom::Query ...
|
893 |
C<{> and C<}> is reserved. If you use these charactors, |
894 |
you must escape them using '\'. Note that '\' is |
|
895 |
already perl escaped charactor, so you must write '\\'. |
|
updated document
|
896 | |
removed DBIx::Custom::Query ...
|
897 |
'select * from books \\{ something statement \\}' |
update document
|
898 | |
renamed update tag to update...
|
899 |
=head2 6. Filtering |
update document
|
900 | |
901 |
Usually, Perl string is kept as internal string. |
|
902 |
If you want to save the string to database, You must encode the string. |
|
903 |
Filtering system help you to convert a data to another data |
|
904 |
when you save to the data and get the data form database. |
|
905 | ||
updated document
|
906 |
If you want to register filter, use C<register_filter()> method. |
update document
|
907 | |
908 |
$dbi->register_filter( |
|
909 |
to_upper_case => sub { |
|
910 |
my $value = shift; |
|
911 |
return uc $value; |
|
912 |
} |
|
913 |
); |
|
914 | ||
915 |
C<encode_utf8> and C<decode_utf8> filter is registerd by default. |
|
916 | ||
917 |
You can specify these filters to C<filter> argument of C<execute()> method. |
|
918 | ||
919 |
my $result = $dbi->execute( |
|
920 |
"select * from books where {= author} and {like title};" |
|
updated document
|
921 |
param => {author => 'Ken', title => '%Perl%'}, |
update document
|
922 |
filter => {author => 'to_upper_case, title => 'encode_utf8'} |
923 |
); |
|
924 | ||
renamed update tag to update...
|
925 |
C<filter> argument can be specified to suger methods, such as |
cleanup
|
926 |
C<insert()>, C<update()>, C<update_all()>, |
renamed update tag to update...
|
927 |
C<delete()>, C<delete_all()>, C<select()>. |
update document
|
928 | |
renamed update tag to update...
|
929 |
# insert(), having filter argument |
update document
|
930 |
$dbi->insert(table => 'books', |
renamed update tag to update...
|
931 |
param => {title => 'Perl', author => 'Ken'}, |
update document
|
932 |
filter => {title => 'encode_utf8'}); |
renamed update tag to update...
|
933 |
|
934 |
# select(), having filter argument |
|
update document
|
935 |
my $result = $dbi->select( |
936 |
table => 'books', |
|
937 |
column => [qw/author title/], |
|
938 |
where => {author => 'Ken'}, |
|
939 |
append => 'order by id limit 1', |
|
940 |
filter => {title => 'encode_utf8'} |
|
941 |
); |
|
942 | ||
updated document
|
943 |
Filter works each parmeter, but you prepare default filter for all parameters. |
update document
|
944 | |
945 |
$dbi->default_bind_filter('encode_utf8'); |
|
946 | ||
renamed update tag to update...
|
947 |
C<filter()> argument overwrites this default filter. |
update document
|
948 |
|
949 |
$dbi->default_bind_filter('encode_utf8'); |
|
950 |
$dbi->insert( |
|
951 |
table => 'books', |
|
renamed update tag to update...
|
952 |
param => {title => 'Perl', author => 'Ken', price => 1000}, |
update document
|
953 |
filter => {author => 'to_upper_case', price => undef} |
954 |
); |
|
955 | ||
renamed update tag to update...
|
956 |
This is same as the following example. |
update document
|
957 | |
958 |
$dbi->insert( |
|
959 |
table => 'books', |
|
renamed update tag to update...
|
960 |
param => {title => 'Perl', author => 'Ken', price => 1000}, |
update document
|
961 |
filter => {title => 'encode_uft8' author => 'to_upper_case'} |
962 |
); |
|
963 | ||
updated document
|
964 |
You can also specify filter when the row is fetched. This is reverse of bind filter. |
update document
|
965 | |
966 |
my $result = $dbi->select(table => 'books'); |
|
967 |
$result->filter({title => 'decode_utf8', author => 'to_upper_case'}); |
|
968 | ||
renamed update tag to update...
|
969 |
Filter works each column value, but you prepare a default filter |
970 |
for all clumn value. |
|
update document
|
971 | |
972 |
$dbi->default_fetch_filter('decode_utf8'); |
|
973 | ||
renamed update tag to update...
|
974 |
C<filter()> method of L<DBIx::Custom::Result> |
975 |
overwrites this default filter. |
|
update document
|
976 | |
977 |
$dbi->default_fetch_filter('decode_utf8'); |
|
978 |
my $result = $dbi->select( |
|
979 |
table => 'books', |
|
980 |
columns => ['title', 'author', 'price'] |
|
981 |
); |
|
982 |
$result->filter({author => 'to_upper_case', price => undef}); |
|
983 | ||
984 |
This is same as the following one. |
|
985 | ||
986 |
my $result = $dbi->select( |
|
987 |
table => 'books', |
|
988 |
columns => ['title', 'author', 'price'] |
|
989 |
); |
|
990 |
$result->filter({title => 'decode_utf8', author => 'to_upper_case'}); |
|
removed reconnect method
|
991 | |
renamed update tag to update...
|
992 |
Note that in fetch filter, column names must be lower case |
993 |
even if the column name conatains upper case charactors. |
|
994 |
This is requirment not to depend database systems. |
|
added check_filter attribute
|
995 | |
cleanup
|
996 |
=head2 7. Get high performance |
updated document
|
997 | |
updated document
|
998 |
=head3 Disable filter checking |
999 | ||
renamed update tag to update...
|
1000 |
Filter checking is executed by default. |
1001 |
This is done to check right filter name is specified, |
|
1002 |
but sometimes damage performance. |
|
updated document
|
1003 | |
renamed update tag to update...
|
1004 |
If you disable this filter checking, |
1005 |
Set C<filter_check> attribute to 0. |
|
updated document
|
1006 | |
renamed update tag to update...
|
1007 |
$dbi->filter_check(0); |
updated document
|
1008 | |
renamed update tag to update...
|
1009 |
=head3 Use execute() method instead suger methods |
1010 | ||
1011 |
If you execute insert statement by C<insert()> method, |
|
1012 |
you sometimes can't get required performance. |
|
updated document
|
1013 | |
1014 |
C<insert()> method is a little slow because SQL statement and statement handle |
|
1015 |
is created every time. |
|
1016 | ||
1017 |
In that case, you can prepare a query by C<create_query()> method. |
|
1018 |
|
|
1019 |
my $query = $dbi->create_query( |
|
renamed update tag to update...
|
1020 |
"insert into books {insert_param title author};" |
updated document
|
1021 |
); |
cleanup
|
1022 | |
1023 |
Return value of C<create_query()> is L<DBIx::Custom::Query> object. |
|
1024 |
This keep the information of SQL and column names. |
|
1025 | ||
1026 |
{ |
|
1027 |
sql => 'insert into books (title, author) values (?, ?);', |
|
1028 |
columns => ['title', 'author'] |
|
1029 |
} |
|
1030 | ||
1031 |
Execute query repeatedly. |
|
updated document
|
1032 |
|
1033 |
my $inputs = [ |
|
1034 |
{title => 'Perl', author => 'Ken'}, |
|
1035 |
{title => 'Good days', author => 'Mike'} |
|
1036 |
]; |
|
1037 |
|
|
1038 |
foreach my $input (@$inputs) { |
|
1039 |
$dbi->execute($query, $input); |
|
1040 |
} |
|
1041 | ||
cleanup
|
1042 |
This is faster than C<insert()> method. |
updated document
|
1043 | |
cleanup
|
1044 |
=head3 caching |
updated document
|
1045 | |
cleanup
|
1046 |
C<execute()> method caches the parsed result of the source of SQL. |
updated document
|
1047 |
Default to 1 |
1048 | ||
1049 |
$dbi->cache(1); |
|
1050 | ||
1051 |
Caching is on memory, but you can change this by C<cache_method()>. |
|
1052 |
First argument is L<DBIx::Custom> object. |
|
cleanup
|
1053 |
Second argument is a source of SQL, |
1054 |
such as "select * from books where {= title} and {= author};"; |
|
updated document
|
1055 |
Third argument is parsed result, such as |
1056 |
{sql => "select * from books where title = ? and author = ?", |
|
1057 |
columns => ['title', 'author']}, this is hash reference. |
|
cleanup
|
1058 |
If arguments is more than two, this method is called to set cache. |
1059 |
If not, this method is called to get cache. |
|
updated document
|
1060 | |
cleanup
|
1061 |
$dbi->cache_method(sub { |
updated document
|
1062 |
sub { |
1063 |
my $self = shift; |
|
1064 |
|
|
1065 |
$self->{_cached} ||= {}; |
|
1066 |
|
|
1067 |
# Set cache |
|
1068 |
if (@_ > 1) { |
|
1069 |
$self->{_cached}{$_[0]} = $_[1] |
|
1070 |
} |
|
1071 |
|
|
1072 |
# Get cache |
|
1073 |
else { |
|
1074 |
return $self->{_cached}{$_[0]} |
|
1075 |
} |
|
1076 |
} |
|
1077 |
}); |
|
1078 | ||
renamed update tag to update...
|
1079 |
=head2 8. More features |
updated document
|
1080 | |
1081 |
=head3 Get DBI object |
|
1082 | ||
1083 |
You can get L<DBI> object and call any method of L<DBI>. |
|
1084 | ||
1085 |
$dbi->dbh->begin_work; |
|
1086 |
$dbi->dbh->commit; |
|
1087 |
$dbi->dbh->rollback; |
|
1088 | ||
1089 |
=head3 Change Result class |
|
1090 | ||
1091 |
You can change Result class if you need. |
|
1092 | ||
1093 |
package Your::Result; |
|
1094 |
use base 'DBIx::Custom::Result'; |
|
1095 |
|
|
1096 |
sub some_method { ... } |
|
1097 | ||
1098 |
1; |
|
1099 |
|
|
1100 |
package main; |
|
1101 |
|
|
1102 |
use Your::Result; |
|
1103 |
|
|
1104 |
my $dbi = DBIx::Custom->connect(...); |
|
1105 |
$dbi->result_class('Your::Result'); |
|
1106 | ||
1107 |
=head3 Custamize SQL builder object |
|
1108 | ||
1109 |
You can custamize SQL builder object |
|
1110 | ||
1111 |
my $dbi = DBIx::Custom->connect(...); |
|
1112 |
$dbi->query_builder->register_tag_processor( |
|
1113 |
name => sub { |
|
1114 |
... |
|
1115 |
} |
|
1116 |
); |
|
1117 | ||
update document
|
1118 |
=head1 ATTRIBUTES |
packaging one directory
|
1119 | |
cleanup
|
1120 |
=head2 C<cache> |
packaging one directory
|
1121 | |
cleanup
|
1122 |
my $cache = $dbi->cache; |
1123 |
$dbi = $dbi->cache(1); |
|
removed DESTROY method(not b...
|
1124 | |
cleanup
|
1125 |
Enable parsed L<DBIx::Custom::Query> object caching. |
1126 |
Default to 1. |
|
packaging one directory
|
1127 | |
cleanup
|
1128 |
=head2 C<cache_method> |
packaging one directory
|
1129 | |
cleanup
|
1130 |
$dbi = $dbi->cache_method(\&cache_method); |
1131 |
$cache_method = $dbi->cache_method |
|
1132 | ||
1133 |
Method to set and get caches. |
|
1134 | ||
1135 |
B<Example:> |
|
1136 | ||
1137 |
$dbi->cache_method( |
|
1138 |
sub { |
|
1139 |
my $self = shift; |
|
1140 |
|
|
1141 |
$self->{_cached} ||= {}; |
|
1142 |
|
|
1143 |
if (@_ > 1) { |
|
1144 |
$self->{_cached}{$_[0]} = $_[1] |
|
1145 |
} |
|
1146 |
else { |
|
1147 |
return $self->{_cached}{$_[0]} |
|
1148 |
} |
|
1149 |
} |
|
1150 |
); |
|
removed DESTROY method(not b...
|
1151 | |
removed DBIx::Custom commit ...
|
1152 |
=head2 C<data_source> |
packaging one directory
|
1153 | |
cleanup
|
1154 |
my $data_source = $dbi->data_source; |
cleanup
|
1155 |
$dbi = $dbi->data_source("DBI:mysql:database=dbname"); |
removed DESTROY method(not b...
|
1156 | |
cleanup
|
1157 |
Data source. |
1158 |
C<connect()> method use this value to connect the database. |
|
removed DESTROY method(not b...
|
1159 | |
removed DBIx::Custom commit ...
|
1160 |
=head2 C<dbh> |
packaging one directory
|
1161 | |
cleanup
|
1162 |
my $dbh = $dbi->dbh; |
1163 |
$dbi = $dbi->dbh($dbh); |
|
packaging one directory
|
1164 | |
cleanup
|
1165 |
L<DBI> object. You can call all methods of L<DBI>. |
packaging one directory
|
1166 | |
renamed default_query_filter...
|
1167 |
=head2 C<default_bind_filter> |
packaging one directory
|
1168 | |
cleanup
|
1169 |
my $default_bind_filter = $dbi->default_bind_filter |
1170 |
$dbi = $dbi->default_bind_filter('encode_utf8'); |
|
packaging one directory
|
1171 | |
cleanup
|
1172 |
Default filter when parameter binding is executed. |
packaging one directory
|
1173 | |
removed DESTROY method(not b...
|
1174 |
=head2 C<default_fetch_filter> |
bind_filter argument is chan...
|
1175 | |
cleanup
|
1176 |
my $default_fetch_filter = $dbi->default_fetch_filter; |
1177 |
$dbi = $dbi->default_fetch_filter('decode_utf8'); |
|
bind_filter argument is chan...
|
1178 | |
cleanup
|
1179 |
Default filter when row is fetched. |
packaging one directory
|
1180 | |
cleanup
|
1181 |
=head2 C<filters> |
bind_filter argument is chan...
|
1182 | |
cleanup
|
1183 |
my $filters = $dbi->filters; |
1184 |
$dbi = $dbi->filters(\%filters); |
|
packaging one directory
|
1185 | |
cleanup
|
1186 |
Filter functions. |
1187 |
"encode_utf8" and "decode_utf8" is registered by default. |
|
1188 | ||
1189 |
=head2 C<filter_check> |
|
1190 | ||
1191 |
my $filter_check = $dbi->filter_check; |
|
1192 |
$dbi = $dbi->filter_check(0); |
|
1193 | ||
1194 |
Enable filter check. |
|
1195 |
Default to 1. |
|
1196 |
This check maybe damege performance. |
|
1197 |
If you require performance, set C<filter_check> attribute to 0. |
|
1198 | ||
1199 |
=head2 C<password> |
|
1200 | ||
1201 |
my $password = $dbi->password; |
|
1202 |
$dbi = $dbi->password('lkj&le`@s'); |
|
1203 | ||
1204 |
Password. |
|
1205 |
C<connect()> method use this value to connect the database. |
|
update document
|
1206 | |
renamed update tag to update...
|
1207 |
=head2 C<query_builder> |
added commit method
|
1208 | |
renamed update tag to update...
|
1209 |
my $sql_class = $dbi->query_builder; |
1210 |
$dbi = $dbi->query_builder(DBIx::Custom::QueryBuilder->new); |
|
added commit method
|
1211 | |
renamed update tag to update...
|
1212 |
SQL builder. C<query_builder()> must be |
renamed build_query to creat...
|
1213 |
the instance of L<DBIx::Custom::QueryBuilder> subclass. |
1214 |
Default to L<DBIx::Custom::QueryBuilder> object. |
|
cleanup
|
1215 | |
cleanup
|
1216 |
=head2 C<result_class> |
cleanup
|
1217 | |
cleanup
|
1218 |
my $result_class = $dbi->result_class; |
1219 |
$dbi = $dbi->result_class('DBIx::Custom::Result'); |
|
cleanup
|
1220 | |
cleanup
|
1221 |
Result class for select statement. |
1222 |
Default to L<DBIx::Custom::Result>. |
|
cleanup
|
1223 | |
cleanup
|
1224 |
=head2 C<user> |
cleanup
|
1225 | |
cleanup
|
1226 |
my $user = $dbi->user; |
1227 |
$dbi = $dbi->user('Ken'); |
|
cleanup
|
1228 | |
cleanup
|
1229 |
User name. |
1230 |
C<connect()> method use this value to connect the database. |
|
1231 |
|
|
1232 |
=head1 METHODS |
|
added commit method
|
1233 | |
cleanup
|
1234 |
L<DBIx::Custom> inherits all methods from L<Object::Simple> |
1235 |
and implements the following new ones. |
|
added check_filter attribute
|
1236 | |
cleanup
|
1237 |
=head2 begin_work |
added check_filter attribute
|
1238 | |
cleanup
|
1239 |
$dbi->begin_work; |
added check_filter attribute
|
1240 | |
cleanup
|
1241 |
Start transaction. |
1242 |
This is same as L<DBI>'s C<begin_work>. |
|
added commit method
|
1243 | |
cleanup
|
1244 |
L<DBIx::Custom> inherits all methods from L<Object::Simple> |
1245 |
and implements the following new ones. |
|
added commit method
|
1246 | |
cleanup
|
1247 |
=head2 commit |
1248 | ||
1249 |
$dbi->commit; |
|
1250 | ||
1251 |
Commit transaction. |
|
1252 |
This is same as L<DBI>'s C<commit>. |
|
1253 | ||
removed DBIx::Custom commit ...
|
1254 |
=head2 C<connect> |
packaging one directory
|
1255 | |
cleanup
|
1256 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname", |
update document
|
1257 |
user => 'ken', password => '!LFKD%$&'); |
bind_filter argument is chan...
|
1258 | |
cleanup
|
1259 |
Create a new L<DBIx::Custom> object and connect to the database. |
renamed build_query to creat...
|
1260 |
L<DBIx::Custom> is a wrapper of L<DBI>. |
cleanup
|
1261 |
C<AutoCommit> and C<RaiseError> options are true, |
renamed build_query to creat...
|
1262 |
and C<PrintError> option is false by default. |
packaging one directory
|
1263 | |
cleanup
|
1264 |
=head2 C<create_query> |
1265 |
|
|
1266 |
my $query = $dbi->create_query( |
|
1267 |
"select * from books where {= author} and {like title};" |
|
1268 |
); |
|
update document
|
1269 | |
cleanup
|
1270 |
Create the instance of L<DBIx::Custom::Query> from the source of SQL. |
1271 |
If you want to get high performance, |
|
1272 |
use C<create_query()> method and execute it by C<execute()> method |
|
1273 |
instead of suger methods. |
|
bind_filter argument is chan...
|
1274 | |
cleanup
|
1275 |
$dbi->execute($query, {author => 'Ken', title => '%Perl%'}); |
version 0.0901
|
1276 | |
cleanup
|
1277 |
=head2 C<execute> |
packaging one directory
|
1278 | |
cleanup
|
1279 |
my $result = $dbi->execute($query, param => $params, filter => \%filter); |
1280 |
my $result = $dbi->execute($source, param => $params, filter => \%filter); |
|
update document
|
1281 | |
cleanup
|
1282 |
Execute query or the source of SQL. |
1283 |
Query is L<DBIx::Custom::Query> object. |
|
1284 |
Return value is L<DBIx::Custom::Result> if select statement is executed, |
|
1285 |
or the count of affected rows if insert, update, delete statement is executed. |
|
version 0.0901
|
1286 | |
removed DESTROY method(not b...
|
1287 |
B<Example:> |
update document
|
1288 | |
cleanup
|
1289 |
my $result = $dbi->execute( |
1290 |
"select * from books where {= author} and {like title}", |
|
1291 |
param => {author => 'Ken', title => '%Perl%'} |
|
1292 |
); |
|
1293 |
|
|
1294 |
while (my $row = $result->fetch) { |
|
1295 |
my $author = $row->[0]; |
|
1296 |
my $title = $row->[1]; |
|
1297 |
} |
|
packaging one directory
|
1298 | |
removed DBIx::Custom commit ...
|
1299 |
=head2 C<delete> |
packaging one directory
|
1300 | |
cleanup
|
1301 |
$dbi->delete(table => $table, |
1302 |
where => \%where, |
|
1303 |
append => $append, |
|
1304 |
filter => \%filter); |
|
bind_filter argument is chan...
|
1305 | |
renamed build_query to creat...
|
1306 |
Execute delete statement. |
1307 |
C<delete> method have C<table>, C<where>, C<append>, and C<filter> arguments. |
|
1308 |
C<table> is a table name. |
|
1309 |
C<where> is where clause. this must be hash reference. |
|
1310 |
C<append> is a string added at the end of the SQL statement. |
|
1311 |
C<filter> is filters when parameter binding is executed. |
|
cleanup
|
1312 |
Return value of C<delete()> is the count of affected rows. |
renamed build_query to creat...
|
1313 | |
removed DESTROY method(not b...
|
1314 |
B<Example:> |
packaging one directory
|
1315 | |
removed register_format()
|
1316 |
$dbi->delete(table => 'books', |
1317 |
where => {id => 5}, |
|
1318 |
append => 'some statement', |
|
removed reconnect method
|
1319 |
filter => {id => 'encode_utf8'}); |
version 0.0901
|
1320 | |
removed DBIx::Custom commit ...
|
1321 |
=head2 C<delete_all> |
packaging one directory
|
1322 | |
cleanup
|
1323 |
$dbi->delete_all(table => $table); |
packaging one directory
|
1324 | |
renamed build_query to creat...
|
1325 |
Execute delete statement to delete all rows. |
1326 |
Arguments is same as C<delete> method, |
|
1327 |
except that C<delete_all> don't have C<where> argument. |
|
cleanup
|
1328 |
Return value of C<delete_all()> is the count of affected rows. |
bind_filter argument is chan...
|
1329 | |
removed DESTROY method(not b...
|
1330 |
B<Example:> |
removed register_format()
|
1331 |
|
removed reconnect method
|
1332 |
$dbi->delete_all(table => 'books'); |
packaging one directory
|
1333 | |
cleanup
|
1334 |
=head2 C<insert> |
1335 | ||
1336 |
$dbi->insert(table => $table, |
|
1337 |
param => \%param, |
|
1338 |
append => $append, |
|
1339 |
filter => \%filter); |
|
1340 | ||
1341 |
Execute insert statement. |
|
1342 |
C<insert> method have C<table>, C<param>, C<append> |
|
1343 |
and C<filter> arguments. |
|
1344 |
C<table> is a table name. |
|
1345 |
C<param> is the pairs of column name value. this must be hash reference. |
|
1346 |
C<append> is a string added at the end of the SQL statement. |
|
1347 |
C<filter> is filters when parameter binding is executed. |
|
1348 |
This is overwrites C<default_bind_filter>. |
|
1349 |
Return value of C<insert()> is the count of affected rows. |
|
1350 | ||
1351 |
B<Example:> |
|
1352 | ||
1353 |
$dbi->insert(table => 'books', |
|
1354 |
param => {title => 'Perl', author => 'Taro'}, |
|
1355 |
append => "some statement", |
|
1356 |
filter => {title => 'encode_utf8'}) |
|
1357 | ||
1358 |
=head2 C<register_filter> |
|
1359 | ||
1360 |
$dbi->register_filter(%filters); |
|
1361 |
$dbi->register_filter(\%filters); |
|
1362 |
|
|
1363 |
Register filter. Registered filters is available in the following attributes |
|
1364 |
or arguments. |
|
1365 | ||
1366 |
=over 4 |
|
1367 | ||
1368 |
=item * |
|
1369 | ||
1370 |
C<default_bind_filter>, C<default_fetch_filter> |
|
1371 | ||
1372 |
=item * |
|
1373 | ||
1374 |
C<filter> argument of C<insert()>, C<update()>, |
|
1375 |
C<update_all()>, C<delete()>, C<delete_all()>, C<select()> |
|
1376 |
methods |
|
1377 | ||
1378 |
=item * |
|
1379 | ||
1380 |
C<execute()> method |
|
1381 | ||
1382 |
=item * |
|
1383 | ||
1384 |
C<default_filter> and C<filter> of C<DBIx::Custom::Query> |
|
1385 | ||
1386 |
=item * |
|
1387 | ||
1388 |
C<default_filter> and C<filter> of C<DBIx::Custom::Result> |
|
1389 | ||
1390 |
=back |
|
1391 | ||
1392 |
B<Example:> |
|
1393 | ||
1394 |
$dbi->register_filter( |
|
1395 |
encode_utf8 => sub { |
|
1396 |
my $value = shift; |
|
1397 |
|
|
1398 |
require Encode; |
|
1399 |
|
|
1400 |
return Encode::encode('UTF-8', $value); |
|
1401 |
}, |
|
1402 |
decode_utf8 => sub { |
|
1403 |
my $value = shift; |
|
1404 |
|
|
1405 |
require Encode; |
|
1406 |
|
|
1407 |
return Encode::decode('UTF-8', $value) |
|
1408 |
} |
|
1409 |
); |
|
1410 | ||
1411 |
=head2 rollback |
|
1412 | ||
1413 |
$dbi->rollback; |
|
1414 | ||
1415 |
Rollback transaction. |
|
1416 |
This is same as L<DBI>'s C<rollback>. |
|
1417 | ||
removed DBIx::Custom commit ...
|
1418 |
=head2 C<select> |
packaging one directory
|
1419 |
|
cleanup
|
1420 |
my $result = $dbi->select(table => $table, |
1421 |
column => [@column], |
|
1422 |
where => \%where, |
|
1423 |
append => $append, |
|
1424 |
relation => \%relation, |
|
1425 |
filter => \%filter); |
|
update document
|
1426 | |
renamed build_query to creat...
|
1427 |
Execute select statement. |
cleanup
|
1428 |
C<select> method have C<table>, C<column>, C<where>, C<append>, |
renamed build_query to creat...
|
1429 |
C<relation> and C<filter> arguments. |
1430 |
C<table> is a table name. |
|
cleanup
|
1431 |
C<where> is where clause. this is normally hash reference. |
renamed build_query to creat...
|
1432 |
C<append> is a string added at the end of the SQL statement. |
1433 |
C<filter> is filters when parameter binding is executed. |
|
update document
|
1434 | |
removed DESTROY method(not b...
|
1435 |
B<Example:> |
update document
|
1436 | |
added commit method
|
1437 |
# select * from books; |
cleanup
|
1438 |
my $result = $dbi->select(table => 'books'); |
packaging one directory
|
1439 |
|
renamed build_query to creat...
|
1440 |
# select * from books where title = ?; |
1441 |
my $result = $dbi->select(table => 'books', where => {title => 'Perl'}); |
|
update document
|
1442 |
|
renamed build_query to creat...
|
1443 |
# select title, author from books where id = ? for update; |
cleanup
|
1444 |
my $result = $dbi->select( |
removed register_format()
|
1445 |
table => 'books', |
removed reconnect method
|
1446 |
column => ['title', 'author'], |
removed register_format()
|
1447 |
where => {id => 1}, |
1448 |
appned => 'for update' |
|
update document
|
1449 |
); |
1450 |
|
|
renamed update tag to update...
|
1451 |
# select books.name as book_name from books, rental |
added commit method
|
1452 |
# where books.id = rental.book_id; |
1453 |
my $result = $dbi->select( |
|
removed reconnect method
|
1454 |
table => ['books', 'rental'], |
1455 |
column => ['books.name as book_name'] |
|
added commit method
|
1456 |
relation => {'books.id' => 'rental.book_id'} |
update document
|
1457 |
); |
1458 | ||
cleanup
|
1459 |
If you use more complex condition, |
1460 |
you can specify a array reference to C<where> argument. |
|
1461 | ||
1462 |
my $result = $dbi->select( |
|
1463 |
table => 'books', |
|
1464 |
column => ['title', 'author'], |
|
1465 |
where => ['{= title} or {like author}', |
|
1466 |
{title => '%Perl%', author => 'Ken'}] |
|
1467 |
); |
|
1468 | ||
1469 |
First element is a string. it contains tags, |
|
1470 |
such as "{= title} or {like author}". |
|
1471 |
Second element is paramters. |
|
1472 | ||
cleanup
|
1473 |
=head2 C<update> |
removed reconnect method
|
1474 | |
cleanup
|
1475 |
$dbi->update(table => $table, |
1476 |
param => \%params, |
|
1477 |
where => \%where, |
|
1478 |
append => $append, |
|
1479 |
filter => \%filter) |
|
removed reconnect method
|
1480 | |
cleanup
|
1481 |
Execute update statement. |
1482 |
C<update> method have C<table>, C<param>, C<where>, C<append> |
|
1483 |
and C<filter> arguments. |
|
1484 |
C<table> is a table name. |
|
1485 |
C<param> is column-value pairs. this must be hash reference. |
|
1486 |
C<where> is where clause. this must be hash reference. |
|
1487 |
C<append> is a string added at the end of the SQL statement. |
|
1488 |
C<filter> is filters when parameter binding is executed. |
|
1489 |
This is overwrites C<default_bind_filter>. |
|
1490 |
Return value of C<update()> is the count of affected rows. |
|
removed reconnect method
|
1491 | |
removed DBIx::Custom commit ...
|
1492 |
B<Example:> |
removed reconnect method
|
1493 | |
cleanup
|
1494 |
$dbi->update(table => 'books', |
1495 |
param => {title => 'Perl', author => 'Taro'}, |
|
1496 |
where => {id => 5}, |
|
1497 |
append => "some statement", |
|
1498 |
filter => {title => 'encode_utf8'}); |
|
renamed build_query to creat...
|
1499 | |
cleanup
|
1500 |
=head2 C<update_all> |
renamed build_query to creat...
|
1501 | |
cleanup
|
1502 |
$dbi->update_all(table => $table, |
1503 |
param => \%params, |
|
1504 |
filter => \%filter, |
|
1505 |
append => $append); |
|
renamed build_query to creat...
|
1506 | |
cleanup
|
1507 |
Execute update statement to update all rows. |
1508 |
Arguments is same as C<update> method, |
|
1509 |
except that C<update_all> don't have C<where> argument. |
|
1510 |
Return value of C<update_all()> is the count of affected rows. |
|
removed DBIx::Custom commit ...
|
1511 | |
1512 |
B<Example:> |
|
packaging one directory
|
1513 | |
cleanup
|
1514 |
$dbi->update_all(table => 'books', |
1515 |
param => {author => 'taro'}, |
|
1516 |
filter => {author => 'encode_utf8'}); |
|
removed reconnect method
|
1517 | |
DBIx::Custom is now stable
|
1518 |
=head1 STABILITY |
1519 | ||
1520 |
L<DBIx::Custom> is now stable. APIs keep backword compatible in the feature. |
|
1521 | ||
removed DESTROY method(not b...
|
1522 |
=head1 BUGS |
1523 | ||
renamed build_query to creat...
|
1524 |
Please tell me bugs if found. |
removed DESTROY method(not b...
|
1525 | |
1526 |
C<< <kimoto.yuki at gmail.com> >> |
|
1527 | ||
1528 |
L<http://github.com/yuki-kimoto/DBIx-Custom> |
|
1529 | ||
removed reconnect method
|
1530 |
=head1 AUTHOR |
1531 | ||
1532 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
|
version 0.0901
|
1533 | |
packaging one directory
|
1534 |
=head1 COPYRIGHT & LICENSE |
1535 | ||
1536 |
Copyright 2009 Yuki Kimoto, all rights reserved. |
|
1537 | ||
1538 |
This program is free software; you can redistribute it and/or modify it |
|
1539 |
under the same terms as Perl itself. |
|
1540 | ||
1541 |
=cut |
|
added cache_method attribute
|
1542 | |
1543 |