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 default_query_filter...
|
27 |
__PACKAGE__->attr(sql_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 | |
packaging one directory
|
45 |
sub connect { |
removed register_format()
|
46 |
my $proto = shift; |
47 |
|
|
48 |
# Create |
|
49 |
my $self = ref $proto ? $proto : $proto->new(@_); |
|
update document
|
50 |
|
51 |
# Information |
|
packaging one directory
|
52 |
my $data_source = $self->data_source; |
53 |
my $user = $self->user; |
|
54 |
my $password = $self->password; |
|
55 |
|
|
update document
|
56 |
# Connect |
select, insert, update, upda...
|
57 |
my $dbh = eval {DBI->connect( |
packaging one directory
|
58 |
$data_source, |
59 |
$user, |
|
60 |
$password, |
|
61 |
{ |
|
62 |
RaiseError => 1, |
|
63 |
PrintError => 0, |
|
64 |
AutoCommit => 1, |
|
65 |
} |
|
66 |
)}; |
|
67 |
|
|
update document
|
68 |
# Connect error |
packaging one directory
|
69 |
croak $@ if $@; |
70 |
|
|
update document
|
71 |
# Database handle |
packaging one directory
|
72 |
$self->dbh($dbh); |
update document
|
73 |
|
packaging one directory
|
74 |
return $self; |
75 |
} |
|
76 | ||
removed DESTROY method(not b...
|
77 |
sub register_filter { |
78 |
my $invocant = shift; |
|
update document
|
79 |
|
changed argument of tag proc...
|
80 |
# Register filter |
removed DESTROY method(not b...
|
81 |
my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
82 |
$invocant->filters({%{$invocant->filters}, %$filters}); |
|
update document
|
83 |
|
removed DESTROY method(not b...
|
84 |
return $invocant; |
packaging one directory
|
85 |
} |
86 | ||
removed register_format()
|
87 |
our %VALID_INSERT_ARGS = map { $_ => 1 } qw/table param append filter/; |
cleanup insert
|
88 | |
packaging one directory
|
89 |
sub insert { |
select, insert, update, upda...
|
90 |
my ($self, %args) = @_; |
removed register_format()
|
91 | |
cleanup insert
|
92 |
# Check arguments |
select, insert, update, upda...
|
93 |
foreach my $name (keys %args) { |
changed argument of tag proc...
|
94 |
croak qq{"$name" is invalid name} |
cleanup insert
|
95 |
unless $VALID_INSERT_ARGS{$name}; |
96 |
} |
|
97 |
|
|
removed register_format()
|
98 |
# Arguments |
select, insert, update, upda...
|
99 |
my $table = $args{table} || ''; |
100 |
my $param = $args{param} || {}; |
|
101 |
my $append = $args{append} || ''; |
|
102 |
my $filter = $args{filter}; |
|
packaging one directory
|
103 |
|
104 |
# Insert keys |
|
removed register_format()
|
105 |
my @insert_keys = keys %$param; |
packaging one directory
|
106 |
|
107 |
# Templte for insert |
|
renamed default_query_filter...
|
108 |
my $source = "insert into $table {insert " |
update document
|
109 |
. join(' ', @insert_keys) . '}'; |
renamed default_query_filter...
|
110 |
$source .= " $append" if $append; |
packaging one directory
|
111 |
|
112 |
# Execute query |
|
renamed default_query_filter...
|
113 |
my $ret_val = $self->execute($source, param => $param, |
removed register_format()
|
114 |
filter => $filter); |
packaging one directory
|
115 |
|
116 |
return $ret_val; |
|
117 |
} |
|
118 | ||
cleanup update and update_al...
|
119 |
our %VALID_UPDATE_ARGS |
removed register_format()
|
120 |
= map { $_ => 1 } qw/table param where append filter allow_update_all/; |
cleanup update and update_al...
|
121 | |
packaging one directory
|
122 |
sub update { |
select, insert, update, upda...
|
123 |
my ($self, %args) = @_; |
cleanup update and update_al...
|
124 |
|
125 |
# Check arguments |
|
select, insert, update, upda...
|
126 |
foreach my $name (keys %args) { |
changed argument of tag proc...
|
127 |
croak qq{"$name" is invalid name} |
cleanup update and update_al...
|
128 |
unless $VALID_UPDATE_ARGS{$name}; |
129 |
} |
|
130 |
|
|
131 |
# Arguments |
|
select, insert, update, upda...
|
132 |
my $table = $args{table} || ''; |
133 |
my $param = $args{param} || {}; |
|
134 |
my $where = $args{where} || {}; |
|
135 |
my $append_statement = $args{append} || ''; |
|
136 |
my $filter = $args{filter}; |
|
137 |
my $allow_update_all = $args{allow_update_all}; |
|
packaging one directory
|
138 |
|
139 |
# Update keys |
|
removed register_format()
|
140 |
my @update_keys = keys %$param; |
packaging one directory
|
141 |
|
142 |
# Where keys |
|
removed register_format()
|
143 |
my @where_keys = keys %$where; |
packaging one directory
|
144 |
|
145 |
# Not exists where keys |
|
changed argument of tag proc...
|
146 |
croak qq{"where" must contain column-value pair} |
cleanup update and update_al...
|
147 |
if !@where_keys && !$allow_update_all; |
packaging one directory
|
148 |
|
149 |
# Update clause |
|
150 |
my $update_clause = '{update ' . join(' ', @update_keys) . '}'; |
|
151 |
|
|
152 |
# Where clause |
|
153 |
my $where_clause = ''; |
|
simplify filtering system
|
154 |
my $new_where = {}; |
many change
|
155 |
|
packaging one directory
|
156 |
if (@where_keys) { |
157 |
$where_clause = 'where '; |
|
158 |
foreach my $where_key (@where_keys) { |
|
simplify filtering system
|
159 |
|
160 |
$where_clause .= "{= $where_key} and "; |
|
packaging one directory
|
161 |
} |
162 |
$where_clause =~ s/ and $//; |
|
163 |
} |
|
164 |
|
|
165 |
# Template for update |
|
renamed default_query_filter...
|
166 |
my $source = "update $table $update_clause $where_clause"; |
167 |
$source .= " $append_statement" if $append_statement; |
|
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, |
removed register_format()
|
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) { |
changed argument of tag proc...
|
200 |
croak qq{"$name" is invalid name} |
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} || {}; |
|
207 |
my $append_statement = $args{append}; |
|
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 |
|
changed argument of tag proc...
|
215 |
croak qq{Key-value pairs for where clause must be specified to "delete" second argument} |
refactoring delete and delet...
|
216 |
if !@where_keys && !$allow_delete_all; |
packaging one directory
|
217 |
|
218 |
# Where clause |
|
219 |
my $where_clause = ''; |
|
220 |
if (@where_keys) { |
|
221 |
$where_clause = 'where '; |
|
removed register_format()
|
222 |
foreach my $wkey (@where_keys) { |
223 |
$where_clause .= "{= $wkey} and "; |
|
packaging one directory
|
224 |
} |
225 |
$where_clause =~ s/ and $//; |
|
226 |
} |
|
227 |
|
|
228 |
# Template for delete |
|
renamed default_query_filter...
|
229 |
my $source = "delete from $table $where_clause"; |
230 |
$source .= " $append_statement" if $append_statement; |
|
packaging one directory
|
231 |
|
232 |
# Execute query |
|
renamed default_query_filter...
|
233 |
my $ret_val = $self->execute($source, param => $where, |
removed register_format()
|
234 |
filter => $filter); |
packaging one directory
|
235 |
|
236 |
return $ret_val; |
|
237 |
} |
|
238 | ||
select, insert, update, upda...
|
239 |
sub delete_all { shift->delete(allow_delete_all => 1, @_) } |
packaging one directory
|
240 | |
refactoring select
|
241 |
our %VALID_SELECT_ARGS |
added commit method
|
242 |
= map { $_ => 1 } qw/table column where append relation filter param/; |
refactoring select
|
243 | |
packaging one directory
|
244 |
sub select { |
select, insert, update, upda...
|
245 |
my ($self, %args) = @_; |
packaging one directory
|
246 |
|
refactoring select
|
247 |
# Check arguments |
select, insert, update, upda...
|
248 |
foreach my $name (keys %args) { |
changed argument of tag proc...
|
249 |
croak qq{"$name" is invalid name} |
refactoring select
|
250 |
unless $VALID_SELECT_ARGS{$name}; |
251 |
} |
|
packaging one directory
|
252 |
|
refactoring select
|
253 |
# Arguments |
select, insert, update, upda...
|
254 |
my $tables = $args{table} || []; |
removed register_format()
|
255 |
$tables = [$tables] unless ref $tables eq 'ARRAY'; |
select, insert, update, upda...
|
256 |
my $columns = $args{column} || []; |
update document
|
257 |
my $where = $args{where}; |
select, insert, update, upda...
|
258 |
my $relation = $args{relation}; |
259 |
my $append = $args{append}; |
|
260 |
my $filter = $args{filter}; |
|
packaging one directory
|
261 |
|
262 |
# SQL template for select statement |
|
renamed default_query_filter...
|
263 |
my $source = 'select '; |
packaging one directory
|
264 |
|
added commit method
|
265 |
# Column clause |
packaging one directory
|
266 |
if (@$columns) { |
267 |
foreach my $column (@$columns) { |
|
renamed default_query_filter...
|
268 |
$source .= "$column, "; |
packaging one directory
|
269 |
} |
renamed default_query_filter...
|
270 |
$source =~ s/, $/ /; |
packaging one directory
|
271 |
} |
272 |
else { |
|
renamed default_query_filter...
|
273 |
$source .= '* '; |
packaging one directory
|
274 |
} |
275 |
|
|
added commit method
|
276 |
# Table |
renamed default_query_filter...
|
277 |
$source .= 'from '; |
packaging one directory
|
278 |
foreach my $table (@$tables) { |
renamed default_query_filter...
|
279 |
$source .= "$table, "; |
packaging one directory
|
280 |
} |
renamed default_query_filter...
|
281 |
$source =~ s/, $/ /; |
packaging one directory
|
282 |
|
added commit method
|
283 |
# Where clause |
update document
|
284 |
my $param; |
285 |
if (ref $where eq 'HASH') { |
|
286 |
$param = $where; |
|
287 |
$source .= 'where ('; |
|
288 |
foreach my $where_key (keys %$where) { |
|
renamed default_query_filter...
|
289 |
$source .= "{= $where_key} and "; |
packaging one directory
|
290 |
} |
update document
|
291 |
$source =~ s/ and $//; |
292 |
$source .= ') '; |
|
293 |
} |
|
294 |
elsif (ref $where eq 'ARRAY') { |
|
295 |
my$where_str = $where->[0] || ''; |
|
296 |
$param = $where->[1]; |
|
297 |
|
|
298 |
$source .= "where ($where_str) "; |
|
packaging one directory
|
299 |
} |
300 |
|
|
added commit method
|
301 |
# Relation |
302 |
if ($relation) { |
|
update document
|
303 |
$source .= $where ? "and " : "where "; |
added commit method
|
304 |
foreach my $rkey (keys %$relation) { |
renamed default_query_filter...
|
305 |
$source .= "$rkey = " . $relation->{$rkey} . " and "; |
packaging one directory
|
306 |
} |
307 |
} |
|
renamed default_query_filter...
|
308 |
$source =~ s/ and $//; |
added commit method
|
309 |
|
310 |
# Append some statement |
|
renamed default_query_filter...
|
311 |
$source .= " $append" if $append; |
packaging one directory
|
312 |
|
313 |
# Execute query |
|
update document
|
314 |
my $result = $self->execute($source, param => $param, |
315 |
filter => $filter); |
|
packaging one directory
|
316 |
|
317 |
return $result; |
|
318 |
} |
|
319 | ||
renamed build_query to creat...
|
320 |
sub create_query { |
renamed default_query_filter...
|
321 |
my ($self, $source) = @_; |
removed register_format()
|
322 |
|
renamed default_query_filter...
|
323 |
# Cache |
added cache_method attribute
|
324 |
my $cache = $self->cache; |
version 0.0901
|
325 |
|
removed reconnect method
|
326 |
# Create query |
327 |
my $query; |
|
added cache_method attribute
|
328 |
if ($cache) { |
329 |
|
|
renamed default_query_filter...
|
330 |
# Get query |
331 |
my $q = $self->cache_method->($self, $source); |
|
added cache_method attribute
|
332 |
|
333 |
# Create query |
|
334 |
$query = DBIx::Custom::Query->new($q) if $q; |
|
removed reconnect method
|
335 |
} |
added cache_method attribute
|
336 |
|
337 |
unless ($query) { |
|
renamed default_query_filter...
|
338 | |
339 |
# Create SQL object |
|
340 |
my $builder = $self->sql_builder; |
|
added cache_method attribute
|
341 |
|
342 |
# Create query |
|
renamed default_query_filter...
|
343 |
$query = eval{$builder->build_query($source)}; |
changed argument of tag proc...
|
344 |
croak $@ if $@; |
removed reconnect method
|
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 |
select, insert, update, upda...
|
354 |
my $sth = eval {$self->dbh->prepare($query->{sql})}; |
update document
|
355 |
croak qq{$@ SQL: "$query->{sql}"} if $@; |
renamed fetch_rows to fetch_...
|
356 |
|
removed reconnect method
|
357 |
# Set statement handle |
358 |
$query->sth($sth); |
|
359 |
|
|
360 |
return $query; |
|
361 |
} |
|
362 | ||
363 |
our %VALID_EXECUTE_ARGS = map { $_ => 1 } qw/param filter/; |
|
364 | ||
365 |
sub execute{ |
|
select, insert, update, upda...
|
366 |
my ($self, $query, %args) = @_; |
removed reconnect method
|
367 |
|
368 |
# Check arguments |
|
select, insert, update, upda...
|
369 |
foreach my $name (keys %args) { |
changed argument of tag proc...
|
370 |
croak qq{"$name" is invalid name} |
removed reconnect method
|
371 |
unless $VALID_EXECUTE_ARGS{$name}; |
372 |
} |
|
373 |
|
|
select, insert, update, upda...
|
374 |
my $params = $args{param} || {}; |
removed reconnect method
|
375 |
|
376 |
# First argument is SQL template |
|
renamed build_query to creat...
|
377 |
$query = $self->create_query($query) |
changed argument of tag proc...
|
378 |
unless ref $query; |
removed reconnect method
|
379 |
|
select, insert, update, upda...
|
380 |
my $filter = $args{filter} || $query->filter || {}; |
removed reconnect method
|
381 |
|
382 |
# Create bind value |
|
383 |
my $bind_values = $self->_build_bind_values($query, $params, $filter); |
|
384 |
|
|
385 |
# Execute |
|
386 |
my $sth = $query->sth; |
|
select, insert, update, upda...
|
387 |
my $affected = eval {$sth->execute(@$bind_values)}; |
388 |
croak $@ if $@; |
|
removed reconnect method
|
389 |
|
390 |
# Return resultset if select statement is executed |
|
391 |
if ($sth->{NUM_OF_FIELDS}) { |
|
392 |
|
|
393 |
# Create result |
|
renamed default_query_filter...
|
394 |
my $result = $self->result_class->new( |
395 |
sth => $sth, |
|
396 |
default_filter => $self->default_fetch_filter, |
|
397 |
filters => $self->filters |
|
398 |
); |
|
399 | ||
removed reconnect method
|
400 |
return $result; |
401 |
} |
|
402 |
return $affected; |
|
403 |
} |
|
404 | ||
405 |
sub _build_bind_values { |
|
406 |
my ($self, $query, $params, $filter) = @_; |
|
407 |
|
|
408 |
# binding values |
|
409 |
my @bind_values; |
|
410 |
|
|
411 |
# Build bind values |
|
412 |
my $count = {}; |
|
413 |
foreach my $column (@{$query->columns}) { |
|
414 |
|
|
changed argument of tag proc...
|
415 |
croak qq{"$column" is not exists in params} |
removed reconnect method
|
416 |
unless exists $params->{$column}; |
417 |
|
|
418 |
# Value |
|
419 |
my $value = ref $params->{$column} eq 'ARRAY' |
|
420 |
? $params->{$column}->[$count->{$column} || 0] |
|
421 |
: $params->{$column}; |
|
422 |
|
|
423 |
# Filter |
|
424 |
$filter ||= {}; |
|
425 |
|
|
426 |
# Filter name |
|
renamed default_query_filter...
|
427 |
my $fname = $filter->{$column} || $self->default_bind_filter || ''; |
removed reconnect method
|
428 |
|
429 |
my $filter_func; |
|
430 |
if ($fname) { |
|
431 |
|
|
432 |
if (ref $fname eq 'CODE') { |
|
433 |
$filter_func = $fname; |
|
434 |
} |
|
435 |
else { |
|
436 |
my $filters = $self->filters; |
|
changed argument of tag proc...
|
437 |
croak qq{Not exists filter "$fname"} |
removed DESTROY method(not b...
|
438 |
unless exists $filters->{$fname}; |
removed reconnect method
|
439 |
$filter_func = $filters->{$fname}; |
440 |
} |
|
441 |
} |
|
442 |
|
|
443 |
push @bind_values, $filter_func |
|
444 |
? $filter_func->($value) |
|
445 |
: $value; |
|
446 |
|
|
447 |
# Count up |
|
448 |
$count->{$column}++; |
|
449 |
} |
|
450 |
|
|
451 |
return \@bind_values; |
|
452 |
} |
|
453 | ||
454 |
=head1 NAME |
|
455 | ||
renamed build_query to creat...
|
456 |
DBIx::Custom - DBI interface, having hash parameter binding and filtering system |
removed reconnect method
|
457 | |
458 |
=cut |
|
459 | ||
update document
|
460 |
our $VERSION = '0.1608'; |
removed reconnect method
|
461 | |
462 |
=head1 STABILITY |
|
463 | ||
renamed build_query to creat...
|
464 |
B<This module is not stable>. |
465 |
Method name and implementations will be changed for a while. |
|
removed reconnect method
|
466 | |
467 |
=head1 SYNOPSYS |
|
cleanup
|
468 | |
renamed build_query to creat...
|
469 |
Connect to the database. |
470 |
|
|
471 |
use DBIx::Custom; |
|
removed reconnect method
|
472 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=books", |
473 |
user => 'ken', password => '!LFKD%$&'); |
|
cleanup
|
474 | |
renamed build_query to creat...
|
475 |
Insert, update, and delete |
cleanup
|
476 | |
removed reconnect method
|
477 |
# Insert |
478 |
$dbi->insert(table => 'books', |
|
479 |
param => {title => 'perl', author => 'Ken'}, |
|
480 |
filter => {title => 'encode_utf8'}); |
|
481 |
|
|
482 |
# Update |
|
483 |
$dbi->update(table => 'books', |
|
484 |
param => {title => 'aaa', author => 'Ken'}, |
|
485 |
where => {id => 5}, |
|
486 |
filter => {title => 'encode_utf8'}); |
|
487 |
|
|
488 |
# Update all |
|
489 |
$dbi->update_all(table => 'books', |
|
490 |
param => {title => 'aaa'}, |
|
491 |
filter => {title => 'encode_utf8'}); |
|
492 |
|
|
493 |
# Delete |
|
494 |
$dbi->delete(table => 'books', |
|
495 |
where => {author => 'Ken'}, |
|
496 |
filter => {title => 'encode_utf8'}); |
|
497 |
|
|
498 |
# Delete all |
|
499 |
$dbi->delete_all(table => 'books'); |
|
cleanup
|
500 | |
renamed build_query to creat...
|
501 |
Select |
cleanup
|
502 | |
removed reconnect method
|
503 |
# Select |
504 |
my $result = $dbi->select(table => 'books'); |
|
renamed fetch_rows to fetch_...
|
505 |
|
renamed build_query to creat...
|
506 |
# Select, more complex |
renamed fetch_rows to fetch_...
|
507 |
my $result = $dbi->select( |
update document
|
508 |
table => 'books', |
509 |
column => [qw/author title/], |
|
510 |
where => {author => 'Ken'}, |
|
511 |
append => 'order by id limit 1', |
|
renamed build_query to creat...
|
512 |
filter => {title => 'encode_utf8'} |
renamed fetch_rows to fetch_...
|
513 |
); |
added commit method
|
514 |
|
renamed build_query to creat...
|
515 |
# Select, join table |
added commit method
|
516 |
my $result = $dbi->select( |
renamed build_query to creat...
|
517 |
table => ['books', 'rental'], |
518 |
column => ['books.name as book_name'] |
|
added commit method
|
519 |
relation => {'books.id' => 'rental.book_id'} |
520 |
); |
|
cleanup
|
521 | |
renamed build_query to creat...
|
522 |
Execute SQL |
cleanup
|
523 | |
renamed build_query to creat...
|
524 |
# Execute SQL |
removed register_format()
|
525 |
$dbi->execute("select title from books"); |
526 |
|
|
renamed build_query to creat...
|
527 |
# Execute SQL with hash binding and filtering |
removed register_format()
|
528 |
$dbi->execute("select id from books where {= author} && {like title}", |
529 |
param => {author => 'ken', title => '%Perl%'}, |
|
renamed build_query to creat...
|
530 |
filter => {title => 'encode_utf8'}); |
removed reconnect method
|
531 | |
532 |
# Create query and execute it |
|
renamed build_query to creat...
|
533 |
my $query = $dbi->create_query( |
removed reconnect method
|
534 |
"select id from books where {= author} && {like title}" |
535 |
); |
|
536 |
$dbi->execute($query, param => {author => 'ken', title => '%Perl%'}) |
|
cleanup
|
537 | |
538 |
More features. |
|
539 | ||
removed register_format()
|
540 |
# Default filter |
renamed default_query_filter...
|
541 |
$dbi->default_bind_filter('encode_utf8'); |
removed register_format()
|
542 |
$dbi->default_fetch_filter('decode_utf8'); |
cleanup
|
543 | |
544 |
# Get DBI object |
|
545 |
my $dbh = $dbi->dbh; |
|
546 | ||
547 |
Fetch row. |
|
548 | ||
removed register_format()
|
549 |
# Fetch |
550 |
while (my $row = $result->fetch) { |
|
551 |
# ... |
|
552 |
} |
|
553 |
|
|
554 |
# Fetch hash |
|
555 |
while (my $row = $result->fetch_hash) { |
|
556 |
|
|
557 |
} |
|
558 |
|
|
removed reconnect method
|
559 | |
560 |
=head1 DESCRIPTION |
|
561 | ||
renamed build_query to creat...
|
562 |
=head2 1. Features |
removed reconnect method
|
563 | |
renamed build_query to creat...
|
564 |
L<DBIx::Custom> is one of L<DBI> interface modules, |
565 |
such as L<DBIx::Class>, L<DBIx::Simple>. |
|
removed reconnect method
|
566 | |
renamed build_query to creat...
|
567 |
This module is not O/R mapper. O/R mapper is useful, |
568 |
but you must learn many syntax of the O/R mapper, |
|
569 |
which is almost another language |
|
570 |
Create SQL statement is offten not effcient and damage SQL performance. |
|
571 |
so you have to execute raw SQL in the end. |
|
removed reconnect method
|
572 | |
renamed build_query to creat...
|
573 |
L<DBIx::Custom> is middle area between L<DBI> and O/R mapper. |
574 |
L<DBIx::Custom> provide flexible hash parameter binding adn filtering system, |
|
575 |
and suger method, such as C<select()>, C<update()>, C<delete()>, C<select()> |
|
576 |
to execute a query easily. |
|
removed reconnect method
|
577 | |
renamed build_query to creat...
|
578 |
L<DBIx::Custom> respects SQL. SQL is not beautiful, but de-facto standard, |
579 |
so all people learing database system know it. |
|
580 |
If you know SQL statement, |
|
581 |
you learn a little thing about L<DBIx::Custom> to do your works. |
|
removed reconnect method
|
582 | |
update document
|
583 |
=head2 1. Connect to the database |
removed reconnect method
|
584 | |
renamed build_query to creat...
|
585 |
C<connect()> method create a new L<DBIx::Custom> |
586 |
object and connect to the database. |
|
removed reconnect method
|
587 | |
renamed build_query to creat...
|
588 |
use DBIx::Custom; |
589 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=books", |
|
590 |
user => 'ken', password => '!LFKD%$&'); |
|
591 | ||
update document
|
592 |
If database is SQLite, use L<DBIx::Custom::SQLite>. you connect database easy way. |
593 | ||
594 |
use DBIx::Custom::SQLite; |
|
595 |
my $dbi = DBIx::Custom->connect(database => 'books'); |
|
596 |
|
|
597 |
If database is MySQL, use L<DBIx::Costm::MySQL>. |
|
598 | ||
599 |
use DBIx::Custom::MySQL; |
|
600 |
my $dbi = DBIx::Custom->connect(database => 'books', |
|
601 |
user => 'ken', password => '!LFKD%$&'); |
|
602 | ||
603 |
=head2 2. Suger methods |
|
renamed build_query to creat...
|
604 | |
605 |
L<DBIx::Custom> has suger methods, such as C<insert()>, C<update()>, |
|
606 |
C<delete()> and C<select()>. If you want to do simple works, |
|
607 |
You don't have to create SQL statement. |
|
608 | ||
update document
|
609 |
=head3 insert() |
610 | ||
611 |
$dbi->insert(table => 'books', |
|
612 |
param => {title => 'perl', author => 'Ken'}); |
|
613 | ||
614 |
The following SQL is executed. |
|
615 | ||
616 |
insert into (title, author) values (?, ?) |
|
617 | ||
618 |
The values of C<title> and C<author> is embedded into placeholders. |
|
619 | ||
620 |
C<append> and C<filter> argument can be specified if you need. |
|
621 | ||
622 |
=head3 update() |
|
623 | ||
624 |
$dbi->update(table => 'books', |
|
625 |
param => {title => 'aaa', author => 'Ken'}, |
|
626 |
where => {id => 5}); |
|
627 | ||
628 |
The following SQL is executed. |
|
629 | ||
630 |
update books set title = ?, author = ?; |
|
631 | ||
632 |
The values of C<title> and C<author> is embedded into placeholders. |
|
633 | ||
634 |
If you want to update all rows, use C<update_all()> method instead. |
|
635 | ||
636 |
C<append> and C<filter> argument can be specified if you need. |
|
637 | ||
638 |
=head3 delete() |
|
639 | ||
640 |
$dbi->delete(table => 'books', |
|
641 |
where => {author => 'Ken'}); |
|
642 | ||
643 |
The following SQL is executed. |
|
644 | ||
645 |
delete from books where id = ?; |
|
646 | ||
647 |
The value of C<id> is embedded into a placehodler. |
|
648 | ||
649 |
C<append> and C<filter> argument can be specified if you need. |
|
650 | ||
651 |
If you want to delete all rows, use C<delete_all()> method instead. |
|
652 | ||
653 |
=head3 select() |
|
654 | ||
655 |
Specify only table: |
|
656 | ||
657 |
my $result = $dbi->select(table => 'books'); |
|
658 | ||
659 |
The following SQL is executed. |
|
660 | ||
661 |
select * from books; |
|
662 | ||
663 |
the result of C<select()> method is L<DBIx::Custom::Result> object. |
|
664 |
use C<fetch()> method to fetch a row. |
|
665 | ||
666 |
while (my $row = $result->fetch) { |
|
667 |
my $title = $row->[0]; |
|
668 |
my $author = $row->[1]; |
|
669 |
} |
|
670 | ||
671 |
L<DBIx::Custom::Result> has various methods to fetch row. |
|
672 |
See "3. Result of select statement". |
|
673 | ||
674 |
Specify C<column> and C<where> arguments: |
|
675 | ||
676 |
my $result = $dbi->select( |
|
677 |
table => 'books', |
|
678 |
column => [qw/author title/], |
|
679 |
where => {author => 'Ken'}); |
|
680 | ||
681 |
The following SQL is executed. |
|
682 | ||
683 |
select author, title from books where author = ?; |
|
684 | ||
685 |
the value of C<author> is embdded into placeholder. |
|
686 | ||
687 |
If C<relation> argument is specifed, you can join tables. |
|
688 | ||
689 |
my $result = $dbi->select( |
|
690 |
table => ['books', 'rental'], |
|
691 |
column => ['books.name as book_name'] |
|
692 |
relation => {'books.id' => 'rental.book_id'} |
|
693 |
); |
|
694 | ||
695 |
The following SQL is executed. |
|
696 | ||
697 |
select books.name as book_name from books |
|
698 |
where books.id = rental.book_id; |
|
699 | ||
700 |
C<append> argument add a string to the end of SQL statement. |
|
701 |
It is useful to add "order by" or "limit" cluase. |
|
702 | ||
703 |
# Select, more complex |
|
704 |
my $result = $dbi->select( |
|
705 |
table => 'books', |
|
706 |
where => {author => 'Ken'}, |
|
707 |
append => 'order by price limit 5', |
|
708 |
); |
|
709 | ||
710 |
The following SQL is executed. |
|
711 | ||
712 |
select * books where author = ? order by price limit 5; |
|
713 | ||
714 |
C<filter> argument can be specified if you need. |
|
715 | ||
716 |
=head2 3. Result of select statement |
|
717 | ||
718 |
C<select> method reurn L<DBIx::Custom::Result> object. |
|
719 |
Using various methods, you can fetch row. |
|
720 | ||
721 |
Fetch row into array. |
|
722 |
|
|
723 |
while (my $row = $result->fetch) { |
|
724 |
my $author = $row->[0]; |
|
725 |
my $title = $row->[1]; |
|
726 |
|
|
727 |
} |
|
728 | ||
729 |
Fetch only a first row into array. |
|
730 | ||
731 |
my $row = $result->fetch_first; |
|
732 | ||
733 |
Fetch multiple rows into array of array. |
|
734 | ||
735 |
while (my $rows = $result->fetch_multi(5)) { |
|
736 |
my $first_author = $rows->[0][0]; |
|
737 |
my $first_title = $rows->[0][1]; |
|
738 |
my $second_author = $rows->[1][0]; |
|
739 |
my $second_value = $rows->[1][1]; |
|
740 |
|
|
741 |
} |
|
742 |
|
|
743 |
Fetch all rows into array of array. |
|
744 | ||
745 |
my $rows = $result->fetch_all; |
|
746 | ||
747 |
Fetch row into hash. |
|
748 | ||
749 |
# Fetch a row into hash |
|
750 |
while (my $row = $result->fetch_hash) { |
|
751 |
my $title = $row->{title}; |
|
752 |
my $author = $row->{author}; |
|
753 |
|
|
754 |
} |
|
755 | ||
756 |
Fetch only a first row into hash |
|
757 | ||
758 |
my $row = $result->fetch_hash_first; |
|
759 |
|
|
760 |
Fetch multiple rows into array of hash |
|
761 | ||
762 |
while (my $rows = $result->fetch_hash_multi(5)) { |
|
763 |
my $first_title = $rows->[0]{title}; |
|
764 |
my $first_author = $rows->[0]{author}; |
|
765 |
my $second_title = $rows->[1]{title}; |
|
766 |
my $second_author = $rows->[1]{author}; |
|
767 |
|
|
768 |
} |
|
769 |
|
|
770 |
Fetch all rows into array of hash |
|
771 | ||
772 |
my $rows = $result->fetch_hash_all; |
|
removed DESTROY method(not b...
|
773 | |
update document
|
774 |
If you want to access row statement handle of L<DBI>, use sth() attribute. |
removed DESTROY method(not b...
|
775 | |
update document
|
776 |
my $sth = $result->sth; |
removed DESTROY method(not b...
|
777 | |
update document
|
778 |
=head2 4. Hash parameter binding |
removed reconnect method
|
779 | |
update document
|
780 |
L<DBIx::Custom> provides hash parameter binding. |
781 | ||
782 |
At frist, I show normal way of parameter binding. |
|
783 | ||
784 |
use DBI; |
|
785 |
my $dbh = DBI->connect(...); |
|
786 |
my $sth = $dbh->prepare( |
|
787 |
"select * from books where author = ? and title like ?;" |
|
788 |
); |
|
789 |
$sth->execute('Ken', '%Perl%'); |
|
790 | ||
791 |
This is very good way because database system enable SQL caching, |
|
792 |
and parameter is quoted automatically. |
|
793 | ||
794 |
L<DBIx::Custom>hash parameter binding system improve normal parameter binding to |
|
795 |
specify hash parameter. |
|
796 | ||
797 |
my $result = $dbi->execute( |
|
798 |
"select * from books where {= author} and {like title};" |
|
799 |
param => {author => 'Ken', title => '%Perl%'} |
|
800 |
); |
|
801 | ||
802 |
This is same as the normal way, execpt that the parameter is hash. |
|
803 |
{= author} is called C<tag>. tag is expand to placeholder internally. |
|
804 | ||
805 |
select * from books where {= author} and {like title} |
|
806 |
-> select * from books where author = ? and title like ?; |
|
807 | ||
808 |
See L<DBIx::Custom::QueryBuilder> to know all tags. |
|
809 | ||
810 |
=head2 5. Filtering |
|
811 | ||
812 |
Usually, Perl string is kept as internal string. |
|
813 |
If you want to save the string to database, You must encode the string. |
|
814 |
Filtering system help you to convert a data to another data |
|
815 |
when you save to the data and get the data form database. |
|
816 | ||
817 |
If you want to register filter, use register_filter() method. |
|
818 | ||
819 |
$dbi->register_filter( |
|
820 |
to_upper_case => sub { |
|
821 |
my $value = shift; |
|
822 |
return uc $value; |
|
823 |
} |
|
824 |
); |
|
825 | ||
826 |
C<encode_utf8> and C<decode_utf8> filter is registerd by default. |
|
827 | ||
828 |
You can specify these filters to C<filter> argument of C<execute()> method. |
|
829 | ||
830 |
my $result = $dbi->execute( |
|
831 |
"select * from books where {= author} and {like title};" |
|
832 |
param => {author => 'Ken', title => '%Perl%'}); |
|
833 |
filter => {author => 'to_upper_case, title => 'encode_utf8'} |
|
834 |
); |
|
835 | ||
836 |
you can also specify filter in suger methods, such as select(), update(), update_all, |
|
837 |
delete(), delete_all(), select(). |
|
838 | ||
839 |
$dbi->insert(table => 'books', |
|
840 |
param => {title => 'perl', author => 'Ken'}, |
|
841 |
filter => {title => 'encode_utf8'}); |
|
842 | ||
843 |
my $result = $dbi->select( |
|
844 |
table => 'books', |
|
845 |
column => [qw/author title/], |
|
846 |
where => {author => 'Ken'}, |
|
847 |
append => 'order by id limit 1', |
|
848 |
filter => {title => 'encode_utf8'} |
|
849 |
); |
|
850 | ||
851 |
Filter work to each parmeter, but you prepare default filter for all parameters. |
|
852 |
you can use C<default_bind_filter()> attribute. |
|
853 | ||
854 |
$dbi->default_bind_filter('encode_utf8'); |
|
855 | ||
856 |
C<filter()> argument overwrites the filter specified by C<default_bind_filter()>. |
|
857 |
|
|
858 |
$dbi->default_bind_filter('encode_utf8'); |
|
859 |
$dbi->insert( |
|
860 |
table => 'books', |
|
861 |
param => {title => 'perl', author => 'Ken', price => 1000}, |
|
862 |
filter => {author => 'to_upper_case', price => undef} |
|
863 |
); |
|
864 | ||
865 |
This is same as the following one. |
|
866 | ||
867 |
$dbi->insert( |
|
868 |
table => 'books', |
|
869 |
param => {title => 'perl', author => 'Ken', price => 1000}, |
|
870 |
filter => {title => 'encode_uft8' author => 'to_upper_case'} |
|
871 |
); |
|
872 | ||
873 |
You can also specify filter when the row is fetching. This is reverse of bindig filter. |
|
874 | ||
875 |
my $result = $dbi->select(table => 'books'); |
|
876 |
$result->filter({title => 'decode_utf8', author => 'to_upper_case'}); |
|
877 | ||
878 |
you can specify C<default_fetch_filter()>. |
|
879 | ||
880 |
$dbi->default_fetch_filter('decode_utf8'); |
|
881 | ||
882 |
C<DBIx::Custom::Result::filter> overwrites the filter specified |
|
883 |
by C<default_fetch_filter()> |
|
884 | ||
885 |
$dbi->default_fetch_filter('decode_utf8'); |
|
886 |
my $result = $dbi->select( |
|
887 |
table => 'books', |
|
888 |
columns => ['title', 'author', 'price'] |
|
889 |
); |
|
890 |
$result->filter({author => 'to_upper_case', price => undef}); |
|
891 | ||
892 |
This is same as the following one. |
|
893 | ||
894 |
my $result = $dbi->select( |
|
895 |
table => 'books', |
|
896 |
columns => ['title', 'author', 'price'] |
|
897 |
); |
|
898 |
$result->filter({title => 'decode_utf8', author => 'to_upper_case'}); |
|
removed reconnect method
|
899 | |
update document
|
900 |
=head1 ATTRIBUTES |
packaging one directory
|
901 | |
removed DBIx::Custom commit ...
|
902 |
=head2 C<user> |
packaging one directory
|
903 | |
cleanup
|
904 |
my $user = $dbi->user; |
905 |
$dbi = $dbi->user('Ken'); |
|
removed DESTROY method(not b...
|
906 | |
cleanup
|
907 |
User name. |
908 |
C<connect()> method use this value to connect the database. |
|
bind_filter argument is chan...
|
909 |
|
removed DBIx::Custom commit ...
|
910 |
=head2 C<password> |
packaging one directory
|
911 | |
cleanup
|
912 |
my $password = $dbi->password; |
913 |
$dbi = $dbi->password('lkj&le`@s'); |
|
packaging one directory
|
914 | |
cleanup
|
915 |
Password. |
916 |
C<connect()> method use this value to connect the database. |
|
removed DESTROY method(not b...
|
917 | |
removed DBIx::Custom commit ...
|
918 |
=head2 C<data_source> |
packaging one directory
|
919 | |
cleanup
|
920 |
my $data_source = $dbi->data_source; |
cleanup
|
921 |
$dbi = $dbi->data_source("DBI:mysql:database=dbname"); |
removed DESTROY method(not b...
|
922 | |
cleanup
|
923 |
Data source. |
924 |
C<connect()> method use this value to connect the database. |
|
removed DESTROY method(not b...
|
925 | |
removed DBIx::Custom commit ...
|
926 |
=head2 C<dbh> |
packaging one directory
|
927 | |
cleanup
|
928 |
my $dbh = $dbi->dbh; |
929 |
$dbi = $dbi->dbh($dbh); |
|
packaging one directory
|
930 | |
cleanup
|
931 |
L<DBI> object. You can call all methods of L<DBI>. |
packaging one directory
|
932 | |
removed DBIx::Custom commit ...
|
933 |
=head2 C<filters> |
packaging one directory
|
934 | |
cleanup
|
935 |
my $filters = $dbi->filters; |
cleanup
|
936 |
$dbi = $dbi->filters(\%filters); |
version 0.0901
|
937 | |
removed DESTROY method(not b...
|
938 |
Filter functions. |
renamed build_query to creat...
|
939 |
"encode_utf8" and "decode_utf8" is registered by default. |
version 0.0901
|
940 | |
renamed default_query_filter...
|
941 |
=head2 C<default_bind_filter> |
packaging one directory
|
942 | |
cleanup
|
943 |
my $default_bind_filter = $dbi->default_bind_filter |
944 |
$dbi = $dbi->default_bind_filter('encode_utf8'); |
|
packaging one directory
|
945 | |
cleanup
|
946 |
Default filter when parameter binding is executed. |
packaging one directory
|
947 | |
removed DESTROY method(not b...
|
948 |
=head2 C<default_fetch_filter> |
bind_filter argument is chan...
|
949 | |
cleanup
|
950 |
my $default_fetch_filter = $dbi->default_fetch_filter; |
951 |
$dbi = $dbi->default_fetch_filter('decode_utf8'); |
|
bind_filter argument is chan...
|
952 | |
cleanup
|
953 |
Default filter when row is fetched. |
packaging one directory
|
954 | |
removed DESTROY method(not b...
|
955 |
=head2 C<result_class> |
bind_filter argument is chan...
|
956 | |
cleanup
|
957 |
my $result_class = $dbi->result_class; |
958 |
$dbi = $dbi->result_class('DBIx::Custom::Result'); |
|
packaging one directory
|
959 | |
removed DESTROY method(not b...
|
960 |
Result class for select statement. |
renamed default_query_filter...
|
961 |
Default to L<DBIx::Custom::Result>. |
update document
|
962 | |
renamed default_query_filter...
|
963 |
=head2 C<sql_builder> |
added commit method
|
964 | |
cleanup
|
965 |
my $sql_class = $dbi->sql_builder; |
966 |
$dbi = $dbi->sql_builder(DBIx::Custom::QueryBuilder->new); |
|
added commit method
|
967 | |
cleanup
|
968 |
SQL builder. sql_builder must be |
renamed build_query to creat...
|
969 |
the instance of L<DBIx::Custom::QueryBuilder> subclass. |
970 |
Default to L<DBIx::Custom::QueryBuilder> object. |
|
cleanup
|
971 | |
972 |
=head2 C<cache> |
|
973 | ||
974 |
my $cache = $dbi->cache; |
|
975 |
$dbi = $dbi->cache(1); |
|
976 | ||
renamed build_query to creat...
|
977 |
Enable parsed L<DBIx::Custom::Query> object caching. |
cleanup
|
978 |
Default to 1. |
979 | ||
980 |
=head2 C<cache_method> |
|
981 | ||
982 |
$dbi = $dbi->cache_method(\&cache_method); |
|
983 |
$cache_method = $dbi->cache_method |
|
984 | ||
renamed build_query to creat...
|
985 |
Method to set and get caches. |
cleanup
|
986 | |
987 |
B<Example:> |
|
988 | ||
989 |
$dbi->cache_method( |
|
990 |
sub { |
|
991 |
my $self = shift; |
|
992 |
|
|
993 |
$self->{_cached} ||= {}; |
|
994 |
|
|
995 |
if (@_ > 1) { |
|
996 |
$self->{_cached}{$_[0]} = $_[1] |
|
997 |
} |
|
998 |
else { |
|
999 |
return $self->{_cached}{$_[0]} |
|
1000 |
} |
|
1001 |
} |
|
1002 |
); |
|
added commit method
|
1003 | |
removed reconnect method
|
1004 |
=head1 METHODS |
added commit method
|
1005 | |
cleanup
|
1006 |
L<DBIx::Custom> inherits all methods from L<Object::Simple> |
1007 |
and implements the following new ones. |
|
added commit method
|
1008 | |
removed DBIx::Custom commit ...
|
1009 |
=head2 C<connect> |
packaging one directory
|
1010 | |
cleanup
|
1011 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname", |
update document
|
1012 |
user => 'ken', password => '!LFKD%$&'); |
bind_filter argument is chan...
|
1013 | |
cleanup
|
1014 |
Create a new L<DBIx::Custom> object and connect to the database. |
renamed build_query to creat...
|
1015 |
L<DBIx::Custom> is a wrapper of L<DBI>. |
1016 |
C<AutoCommit> and C<RaiseError> option is true, |
|
1017 |
and C<PrintError> option is false by default. |
|
packaging one directory
|
1018 | |
removed DBIx::Custom commit ...
|
1019 |
=head2 C<insert> |
packaging one directory
|
1020 | |
cleanup
|
1021 |
$dbi->insert(table => $table, |
1022 |
param => \%param, |
|
1023 |
append => $append, |
|
1024 |
filter => \%filter); |
|
update document
|
1025 | |
renamed build_query to creat...
|
1026 |
Execute insert statement. |
1027 |
C<insert> method have C<table>, C<param>, C<append> |
|
1028 |
and C<filter> arguments. |
|
1029 |
C<table> is a table name. |
|
1030 |
C<param> is column-value pairs. this must be hash reference. |
|
1031 |
C<append> is a string added at the end of the SQL statement. |
|
1032 |
C<filter> is filters when parameter binding is executed. |
|
1033 |
This is overwrites C<default_bind_filter>. |
|
1034 |
Return value of C<insert> is the count of affected rows. |
|
1035 | ||
removed DESTROY method(not b...
|
1036 |
B<Example:> |
version 0.0901
|
1037 | |
removed register_format()
|
1038 |
$dbi->insert(table => 'books', |
1039 |
param => {title => 'Perl', author => 'Taro'}, |
|
1040 |
append => "some statement", |
|
1041 |
filter => {title => 'encode_utf8'}) |
|
version 0.0901
|
1042 | |
removed DBIx::Custom commit ...
|
1043 |
=head2 C<update> |
packaging one directory
|
1044 | |
cleanup
|
1045 |
$dbi->update(table => $table, |
1046 |
param => \%params, |
|
1047 |
where => \%where, |
|
1048 |
append => $append, |
|
1049 |
filter => \%filter) |
|
version 0.0901
|
1050 | |
renamed build_query to creat...
|
1051 |
Execute update statement. |
1052 |
C<update> method have C<table>, C<param>, C<where>, C<append> |
|
1053 |
and C<filter> arguments. |
|
1054 |
C<table> is a table name. |
|
1055 |
C<param> is column-value pairs. this must be hash reference. |
|
1056 |
C<where> is where clause. this must be hash reference. |
|
1057 |
C<append> is a string added at the end of the SQL statement. |
|
1058 |
C<filter> is filters when parameter binding is executed. |
|
1059 |
This is overwrites C<default_bind_filter>. |
|
1060 |
Return value of C<update> is the count of affected rows. |
|
update document
|
1061 | |
removed DESTROY method(not b...
|
1062 |
B<Example:> |
bind_filter argument is chan...
|
1063 | |
removed register_format()
|
1064 |
$dbi->update(table => 'books', |
1065 |
param => {title => 'Perl', author => 'Taro'}, |
|
1066 |
where => {id => 5}, |
|
renamed build_query to creat...
|
1067 |
append => "for update", |
added commit method
|
1068 |
filter => {title => 'encode_utf8'}); |
version 0.0901
|
1069 | |
removed DBIx::Custom commit ...
|
1070 |
=head2 C<update_all> |
packaging one directory
|
1071 | |
cleanup
|
1072 |
$dbi->update_all(table => $table, |
1073 |
param => \%params, |
|
1074 |
filter => \%filter, |
|
1075 |
append => $append); |
|
update document
|
1076 | |
renamed build_query to creat...
|
1077 |
Execute update statement to update all rows. |
1078 |
Arguments is same as C<update> method, |
|
1079 |
except that C<update_all> don't have C<where> argument. |
|
1080 |
Return value of C<update_all> is the count of affected rows. |
|
version 0.0901
|
1081 | |
removed DESTROY method(not b...
|
1082 |
B<Example:> |
update document
|
1083 | |
removed register_format()
|
1084 |
$dbi->update_all(table => 'books', |
1085 |
param => {author => 'taro'}, |
|
1086 |
filter => {author => 'encode_utf8'}); |
|
packaging one directory
|
1087 | |
removed DBIx::Custom commit ...
|
1088 |
=head2 C<delete> |
packaging one directory
|
1089 | |
cleanup
|
1090 |
$dbi->delete(table => $table, |
1091 |
where => \%where, |
|
1092 |
append => $append, |
|
1093 |
filter => \%filter); |
|
bind_filter argument is chan...
|
1094 | |
renamed build_query to creat...
|
1095 |
Execute delete statement. |
1096 |
C<delete> method have C<table>, C<where>, C<append>, and C<filter> arguments. |
|
1097 |
C<table> is a table name. |
|
1098 |
C<where> is where clause. this must be hash reference. |
|
1099 |
C<append> is a string added at the end of the SQL statement. |
|
1100 |
C<filter> is filters when parameter binding is executed. |
|
1101 |
Return value of C<delete> is the count of affected rows. |
|
1102 | ||
removed DESTROY method(not b...
|
1103 |
B<Example:> |
packaging one directory
|
1104 | |
removed register_format()
|
1105 |
$dbi->delete(table => 'books', |
1106 |
where => {id => 5}, |
|
1107 |
append => 'some statement', |
|
removed reconnect method
|
1108 |
filter => {id => 'encode_utf8'}); |
version 0.0901
|
1109 | |
removed DBIx::Custom commit ...
|
1110 |
=head2 C<delete_all> |
packaging one directory
|
1111 | |
cleanup
|
1112 |
$dbi->delete_all(table => $table); |
packaging one directory
|
1113 | |
renamed build_query to creat...
|
1114 |
Execute delete statement to delete all rows. |
1115 |
Arguments is same as C<delete> method, |
|
1116 |
except that C<delete_all> don't have C<where> argument. |
|
1117 |
Return value of C<delete_all> is the count of affected rows. |
|
bind_filter argument is chan...
|
1118 | |
removed DESTROY method(not b...
|
1119 |
B<Example:> |
removed register_format()
|
1120 |
|
removed reconnect method
|
1121 |
$dbi->delete_all(table => 'books'); |
packaging one directory
|
1122 | |
removed DBIx::Custom commit ...
|
1123 |
=head2 C<select> |
packaging one directory
|
1124 |
|
cleanup
|
1125 |
my $result = $dbi->select(table => $table, |
1126 |
column => [@column], |
|
1127 |
where => \%where, |
|
1128 |
append => $append, |
|
1129 |
relation => \%relation, |
|
1130 |
filter => \%filter); |
|
update document
|
1131 | |
renamed build_query to creat...
|
1132 |
Execute select statement. |
1133 |
C<select> method have C<table>, C<column>, C<where>, C<append> |
|
1134 |
C<relation> and C<filter> arguments. |
|
1135 |
C<table> is a table name. |
|
1136 |
C<where> is where clause. this must be hash reference |
|
1137 |
or a string containing such tags as "{= title} or {= author}". |
|
1138 |
C<append> is a string added at the end of the SQL statement. |
|
1139 |
C<filter> is filters when parameter binding is executed. |
|
update document
|
1140 | |
removed DESTROY method(not b...
|
1141 |
B<Example:> |
update document
|
1142 | |
added commit method
|
1143 |
# select * from books; |
cleanup
|
1144 |
my $result = $dbi->select(table => 'books'); |
packaging one directory
|
1145 |
|
renamed build_query to creat...
|
1146 |
# select * from books where title = ?; |
1147 |
my $result = $dbi->select(table => 'books', where => {title => 'Perl'}); |
|
update document
|
1148 |
|
renamed build_query to creat...
|
1149 |
# select title, author from books where id = ? for update; |
cleanup
|
1150 |
my $result = $dbi->select( |
removed register_format()
|
1151 |
table => 'books', |
removed reconnect method
|
1152 |
column => ['title', 'author'], |
removed register_format()
|
1153 |
where => {id => 1}, |
1154 |
appned => 'for update' |
|
update document
|
1155 |
); |
1156 |
|
|
added commit method
|
1157 |
# select books.name as book_name from books, rental |
1158 |
# where books.id = rental.book_id; |
|
1159 |
my $result = $dbi->select( |
|
removed reconnect method
|
1160 |
table => ['books', 'rental'], |
1161 |
column => ['books.name as book_name'] |
|
added commit method
|
1162 |
relation => {'books.id' => 'rental.book_id'} |
update document
|
1163 |
); |
1164 | ||
renamed build_query to creat...
|
1165 |
=head2 C<create_query> |
removed reconnect method
|
1166 |
|
renamed build_query to creat...
|
1167 |
my $query = $dbi->create_query( |
removed DBIx::Custom commit ...
|
1168 |
"select * from authors where {= name} and {= age};" |
1169 |
); |
|
1170 | ||
renamed build_query to creat...
|
1171 |
Create the instance of L<DBIx::Custom::Query> from SQL source. |
packaging one directory
|
1172 | |
removed DBIx::Custom commit ...
|
1173 |
=head2 C<execute> |
removed reconnect method
|
1174 | |
cleanup
|
1175 |
my $result = $dbi->execute($query, param => $params, filter => \%filter); |
1176 |
my $result = $dbi->execute($source, param => $params, filter => \%filter); |
|
removed reconnect method
|
1177 | |
renamed build_query to creat...
|
1178 |
Execute query or SQL source. Query is L<DBIx::Csutom::Query> object. |
1179 |
Return value is L<DBIx::Custom::Result> in select statement, |
|
1180 |
or the count of affected rows in insert, update, delete statement. |
|
removed reconnect method
|
1181 | |
removed DBIx::Custom commit ...
|
1182 |
B<Example:> |
removed reconnect method
|
1183 | |
cleanup
|
1184 |
my $result = $dbi->execute("select * from authors where {= name} and {= age}", |
removed reconnect method
|
1185 |
param => {name => 'taro', age => 19}); |
1186 |
|
|
1187 |
while (my $row = $result->fetch) { |
|
1188 |
# do something |
|
1189 |
} |
|
1190 | ||
removed DBIx::Custom commit ...
|
1191 |
=head2 C<register_filter> |
removed reconnect method
|
1192 | |
1193 |
$dbi->register_filter(%filters); |
|
removed DESTROY method(not b...
|
1194 |
$dbi->register_filter(\%filters); |
removed reconnect method
|
1195 |
|
renamed build_query to creat...
|
1196 |
Register filter. Registered filters is available in the following methods |
1197 |
or arguments. |
|
1198 | ||
1199 |
=over 4 |
|
1200 | ||
1201 |
=item * |
|
1202 | ||
1203 |
C<default_bind_filter()> |
|
1204 | ||
1205 |
=item * |
|
1206 | ||
1207 |
C<default_fetch_filter()> |
|
1208 | ||
1209 |
=item * |
|
1210 | ||
1211 |
C<filter> argument of C<insert()>, C<update()>, |
|
1212 |
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>, |
|
1213 |
C<execute> method. |
|
1214 | ||
1215 |
=item * |
|
1216 | ||
1217 |
C<DBIx::Custom::Query::default_filter()> |
|
1218 | ||
1219 |
=item * |
|
1220 | ||
1221 |
C<DBIx::Csutom::Query::filter()> |
|
1222 | ||
1223 |
=item * |
|
1224 | ||
1225 |
C<DBIx::Custom::Result::default_filter()> |
|
1226 | ||
1227 |
=item * |
|
1228 | ||
1229 |
C<DBIx::Custom::Result::filter()> |
|
1230 | ||
1231 |
=back |
|
removed DBIx::Custom commit ...
|
1232 | |
1233 |
B<Example:> |
|
packaging one directory
|
1234 | |
removed reconnect method
|
1235 |
$dbi->register_filter( |
1236 |
encode_utf8 => sub { |
|
1237 |
my $value = shift; |
|
1238 |
|
|
1239 |
require Encode; |
|
1240 |
|
|
1241 |
return Encode::encode('UTF-8', $value); |
|
1242 |
}, |
|
1243 |
decode_utf8 => sub { |
|
1244 |
my $value = shift; |
|
1245 |
|
|
1246 |
require Encode; |
|
1247 |
|
|
1248 |
return Encode::decode('UTF-8', $value) |
|
1249 |
} |
|
1250 |
); |
|
1251 | ||
removed DESTROY method(not b...
|
1252 |
=head1 BUGS |
1253 | ||
renamed build_query to creat...
|
1254 |
Please tell me bugs if found. |
removed DESTROY method(not b...
|
1255 | |
1256 |
C<< <kimoto.yuki at gmail.com> >> |
|
1257 | ||
1258 |
L<http://github.com/yuki-kimoto/DBIx-Custom> |
|
1259 | ||
removed reconnect method
|
1260 |
=head1 AUTHOR |
1261 | ||
1262 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
|
version 0.0901
|
1263 | |
packaging one directory
|
1264 |
=head1 COPYRIGHT & LICENSE |
1265 | ||
1266 |
Copyright 2009 Yuki Kimoto, all rights reserved. |
|
1267 | ||
1268 |
This program is free software; you can redistribute it and/or modify it |
|
1269 |
under the same terms as Perl itself. |
|
1270 | ||
1271 |
=cut |
|
added cache_method attribute
|
1272 | |
1273 |