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