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 | ||
updated document
|
460 |
our $VERSION = '0.1609'; |
removed reconnect method
|
461 | |
462 |
=head1 STABILITY |
|
463 | ||
updated document
|
464 |
B<This module is not stable>. |
renamed build_query to creat...
|
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'}, |
|
updated document
|
511 |
append => 'order by id limit 5', |
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 |
); |
|
updated document
|
521 |
|
522 |
# Select, more flexible where |
|
523 |
my $result = $dbi->select( |
|
524 |
table => 'books', |
|
525 |
where => ['{= author} and {like title}', |
|
526 |
{author => 'Ken', title => '%Perl%'}] |
|
527 |
); |
|
cleanup
|
528 | |
renamed build_query to creat...
|
529 |
Execute SQL |
cleanup
|
530 | |
renamed build_query to creat...
|
531 |
# Execute SQL |
removed register_format()
|
532 |
$dbi->execute("select title from books"); |
533 |
|
|
renamed build_query to creat...
|
534 |
# Execute SQL with hash binding and filtering |
updated document
|
535 |
$dbi->execute("select id from books where {= author} and {like title}", |
removed register_format()
|
536 |
param => {author => 'ken', title => '%Perl%'}, |
renamed build_query to creat...
|
537 |
filter => {title => 'encode_utf8'}); |
removed reconnect method
|
538 | |
539 |
# Create query and execute it |
|
renamed build_query to creat...
|
540 |
my $query = $dbi->create_query( |
updated document
|
541 |
"select id from books where {= author} and {like title}" |
removed reconnect method
|
542 |
); |
updated document
|
543 |
$dbi->execute($query, param => {author => 'Ken', title => '%Perl%'}) |
cleanup
|
544 | |
updated document
|
545 |
Other features. |
cleanup
|
546 | |
removed register_format()
|
547 |
# Default filter |
renamed default_query_filter...
|
548 |
$dbi->default_bind_filter('encode_utf8'); |
removed register_format()
|
549 |
$dbi->default_fetch_filter('decode_utf8'); |
cleanup
|
550 | |
551 |
# Get DBI object |
|
552 |
my $dbh = $dbi->dbh; |
|
553 | ||
554 |
Fetch row. |
|
555 | ||
removed register_format()
|
556 |
# Fetch |
557 |
while (my $row = $result->fetch) { |
|
558 |
# ... |
|
559 |
} |
|
560 |
|
|
561 |
# Fetch hash |
|
562 |
while (my $row = $result->fetch_hash) { |
|
563 |
|
|
564 |
} |
|
565 |
|
|
removed reconnect method
|
566 |
=head1 DESCRIPTION |
567 | ||
renamed build_query to creat...
|
568 |
=head2 1. Features |
removed reconnect method
|
569 | |
renamed build_query to creat...
|
570 |
L<DBIx::Custom> is one of L<DBI> interface modules, |
571 |
such as L<DBIx::Class>, L<DBIx::Simple>. |
|
removed reconnect method
|
572 | |
renamed build_query to creat...
|
573 |
This module is not O/R mapper. O/R mapper is useful, |
574 |
but you must learn many syntax of the O/R mapper, |
|
updated document
|
575 |
which is almost another language. |
576 |
Created SQL statement is offten not effcient and damage SQL performance. |
|
renamed build_query to creat...
|
577 |
so you have to execute raw SQL in the end. |
removed reconnect method
|
578 | |
renamed build_query to creat...
|
579 |
L<DBIx::Custom> is middle area between L<DBI> and O/R mapper. |
updated document
|
580 |
L<DBIx::Custom> provide flexible hash parameter binding and filtering system, |
581 |
and suger methods, such as C<select()>, C<update()>, C<delete()>, C<select()> |
|
582 |
to execute SQL easily. |
|
removed reconnect method
|
583 | |
updated document
|
584 |
L<DBIx::Custom> respects SQL. SQL is very complex and not beautiful, |
585 |
but de-facto standard, |
|
586 |
so all people learing database know it. |
|
587 |
If you know SQL, |
|
588 |
you learn a little thing to do your works, using L<DBIx::Custom> |
|
removed reconnect method
|
589 | |
update document
|
590 |
=head2 1. Connect to the database |
removed reconnect method
|
591 | |
renamed build_query to creat...
|
592 |
C<connect()> method create a new L<DBIx::Custom> |
593 |
object and connect to the database. |
|
removed reconnect method
|
594 | |
renamed build_query to creat...
|
595 |
use DBIx::Custom; |
596 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=books", |
|
597 |
user => 'ken', password => '!LFKD%$&'); |
|
598 | ||
update document
|
599 |
If database is SQLite, use L<DBIx::Custom::SQLite>. you connect database easy way. |
600 | ||
601 |
use DBIx::Custom::SQLite; |
|
602 |
my $dbi = DBIx::Custom->connect(database => 'books'); |
|
603 |
|
|
updated document
|
604 |
If database is MySQL, use L<DBIx::Costom::MySQL>. |
update document
|
605 | |
606 |
use DBIx::Custom::MySQL; |
|
607 |
my $dbi = DBIx::Custom->connect(database => 'books', |
|
608 |
user => 'ken', password => '!LFKD%$&'); |
|
609 | ||
610 |
=head2 2. Suger methods |
|
renamed build_query to creat...
|
611 | |
612 |
L<DBIx::Custom> has suger methods, such as C<insert()>, C<update()>, |
|
updated document
|
613 |
C<delete()> and C<select()>. If you want to do small works, |
614 |
You don't have to create SQL statements. |
|
renamed build_query to creat...
|
615 | |
update document
|
616 |
=head3 insert() |
617 | ||
updated document
|
618 |
Execute insert statement. |
619 | ||
update document
|
620 |
$dbi->insert(table => 'books', |
621 |
param => {title => 'perl', author => 'Ken'}); |
|
622 | ||
623 |
The following SQL is executed. |
|
624 | ||
updated document
|
625 |
insert into (title, author) values (?, ?); |
update document
|
626 | |
627 |
The values of C<title> and C<author> is embedded into placeholders. |
|
628 | ||
updated document
|
629 |
C<append> and C<filter> argument can be specified |
630 |
to C<insert()> method if you need. |
|
update document
|
631 | |
632 |
=head3 update() |
|
633 | ||
updated document
|
634 |
Execute update statement. |
635 | ||
update document
|
636 |
$dbi->update(table => 'books', |
637 |
param => {title => 'aaa', author => 'Ken'}, |
|
638 |
where => {id => 5}); |
|
639 | ||
640 |
The following SQL is executed. |
|
641 | ||
642 |
update books set title = ?, author = ?; |
|
643 | ||
644 |
The values of C<title> and C<author> is embedded into placeholders. |
|
645 | ||
updated document
|
646 |
C<append> and C<filter> argument can be specified |
647 |
to C<update()> method if you need. |
|
update document
|
648 | |
updated document
|
649 |
If you want to update all rows, use C<update_all()> method instead. |
update document
|
650 | |
651 |
=head3 delete() |
|
652 | ||
updated document
|
653 |
Execute delete statement. |
654 | ||
update document
|
655 |
$dbi->delete(table => 'books', |
656 |
where => {author => 'Ken'}); |
|
657 | ||
658 |
The following SQL is executed. |
|
659 | ||
660 |
delete from books where id = ?; |
|
661 | ||
updated document
|
662 |
The value of C<id> is embedded into the placehodler. |
update document
|
663 | |
updated document
|
664 |
C<append> and C<filter> argument can be specified |
665 |
to C<delete()> method if you need. |
|
update document
|
666 | |
667 |
If you want to delete all rows, use C<delete_all()> method instead. |
|
668 | ||
669 |
=head3 select() |
|
670 | ||
updated document
|
671 |
Execute select statement, only C<table> argument specified : |
update document
|
672 | |
673 |
my $result = $dbi->select(table => 'books'); |
|
674 | ||
675 |
The following SQL is executed. |
|
676 | ||
677 |
select * from books; |
|
678 | ||
679 |
the result of C<select()> method is L<DBIx::Custom::Result> object. |
|
updated document
|
680 |
You can fetch row. |
update document
|
681 | |
682 |
while (my $row = $result->fetch) { |
|
683 |
my $title = $row->[0]; |
|
684 |
my $author = $row->[1]; |
|
685 |
} |
|
686 | ||
687 |
L<DBIx::Custom::Result> has various methods to fetch row. |
|
updated document
|
688 |
See "3. Fetch row". |
update document
|
689 | |
updated document
|
690 |
Specify C<column> and C<where> arguments. |
update document
|
691 | |
692 |
my $result = $dbi->select( |
|
693 |
table => 'books', |
|
694 |
column => [qw/author title/], |
|
695 |
where => {author => 'Ken'}); |
|
696 | ||
697 |
The following SQL is executed. |
|
698 | ||
699 |
select author, title from books where author = ?; |
|
700 | ||
701 |
the value of C<author> is embdded into placeholder. |
|
702 | ||
updated document
|
703 |
If you want to join tables, specify C<relation> argument. |
update document
|
704 | |
705 |
my $result = $dbi->select( |
|
706 |
table => ['books', 'rental'], |
|
707 |
column => ['books.name as book_name'] |
|
708 |
relation => {'books.id' => 'rental.book_id'} |
|
709 |
); |
|
710 | ||
711 |
The following SQL is executed. |
|
712 | ||
713 |
select books.name as book_name from books |
|
714 |
where books.id = rental.book_id; |
|
715 | ||
716 |
C<append> argument add a string to the end of SQL statement. |
|
updated document
|
717 |
You can add "order by" or "limit" cluase. |
update document
|
718 | |
719 |
# Select, more complex |
|
720 |
my $result = $dbi->select( |
|
721 |
table => 'books', |
|
722 |
where => {author => 'Ken'}, |
|
723 |
append => 'order by price limit 5', |
|
724 |
); |
|
725 | ||
726 |
The following SQL is executed. |
|
727 | ||
728 |
select * books where author = ? order by price limit 5; |
|
729 | ||
updated document
|
730 |
C<filter> argument can be specified to filter parameters |
731 |
if you need. |
|
update document
|
732 | |
updated document
|
733 |
=head2 3. Fetch row |
update document
|
734 | |
updated document
|
735 |
C<select()> method return L<DBIx::Custom::Result> object. |
736 |
You can fetch row by various methods. |
|
update document
|
737 | |
738 |
Fetch row into array. |
|
739 |
|
|
740 |
while (my $row = $result->fetch) { |
|
741 |
my $author = $row->[0]; |
|
742 |
my $title = $row->[1]; |
|
743 |
|
|
744 |
} |
|
745 | ||
746 |
Fetch only a first row into array. |
|
747 | ||
748 |
my $row = $result->fetch_first; |
|
749 | ||
750 |
Fetch multiple rows into array of array. |
|
751 | ||
752 |
while (my $rows = $result->fetch_multi(5)) { |
|
753 |
my $first_author = $rows->[0][0]; |
|
754 |
my $first_title = $rows->[0][1]; |
|
755 |
my $second_author = $rows->[1][0]; |
|
756 |
my $second_value = $rows->[1][1]; |
|
757 |
|
|
758 |
} |
|
759 |
|
|
760 |
Fetch all rows into array of array. |
|
761 | ||
762 |
my $rows = $result->fetch_all; |
|
763 | ||
764 |
Fetch row into hash. |
|
765 | ||
766 |
# Fetch a row into hash |
|
767 |
while (my $row = $result->fetch_hash) { |
|
768 |
my $title = $row->{title}; |
|
769 |
my $author = $row->{author}; |
|
770 |
|
|
771 |
} |
|
772 | ||
773 |
Fetch only a first row into hash |
|
774 | ||
775 |
my $row = $result->fetch_hash_first; |
|
776 |
|
|
777 |
Fetch multiple rows into array of hash |
|
778 | ||
779 |
while (my $rows = $result->fetch_hash_multi(5)) { |
|
780 |
my $first_title = $rows->[0]{title}; |
|
781 |
my $first_author = $rows->[0]{author}; |
|
782 |
my $second_title = $rows->[1]{title}; |
|
783 |
my $second_author = $rows->[1]{author}; |
|
784 |
|
|
785 |
} |
|
786 |
|
|
787 |
Fetch all rows into array of hash |
|
788 | ||
789 |
my $rows = $result->fetch_hash_all; |
|
removed DESTROY method(not b...
|
790 | |
updated document
|
791 |
If you want to access raw statement handle of L<DBI>, use C<sth()> attribute. |
removed DESTROY method(not b...
|
792 | |
update document
|
793 |
my $sth = $result->sth; |
removed DESTROY method(not b...
|
794 | |
update document
|
795 |
=head2 4. Hash parameter binding |
removed reconnect method
|
796 | |
update document
|
797 |
L<DBIx::Custom> provides hash parameter binding. |
798 | ||
799 |
At frist, I show normal way of parameter binding. |
|
800 | ||
801 |
use DBI; |
|
802 |
my $dbh = DBI->connect(...); |
|
803 |
my $sth = $dbh->prepare( |
|
804 |
"select * from books where author = ? and title like ?;" |
|
805 |
); |
|
806 |
$sth->execute('Ken', '%Perl%'); |
|
807 | ||
updated document
|
808 |
This is very good way because database system can enable SQL caching, |
809 |
and parameter is quoted automatically, it is secure. |
|
update document
|
810 | |
updated document
|
811 |
L<DBIx::Custom> hash parameter binding system improve |
812 |
normal parameter binding way to specify hash parameter. |
|
update document
|
813 | |
814 |
my $result = $dbi->execute( |
|
815 |
"select * from books where {= author} and {like title};" |
|
816 |
param => {author => 'Ken', title => '%Perl%'} |
|
817 |
); |
|
818 | ||
819 |
This is same as the normal way, execpt that the parameter is hash. |
|
updated document
|
820 |
{= author} is called C<tag>. tag is expand to placeholder string internally. |
update document
|
821 | |
822 |
select * from books where {= author} and {like title} |
|
823 |
-> select * from books where author = ? and title like ?; |
|
824 | ||
updated document
|
825 |
The following tags is available. |
826 | ||
827 |
=head1 Tags |
|
828 | ||
829 |
The following tags is available. |
|
830 | ||
831 |
[TAG] [REPLACED] |
|
832 |
{? NAME} -> ? |
|
833 |
{= NAME} -> NAME = ? |
|
834 |
{<> NAME} -> NAME <> ? |
|
835 |
|
|
836 |
{< NAME} -> NAME < ? |
|
837 |
{> NAME} -> NAME > ? |
|
838 |
{>= NAME} -> NAME >= ? |
|
839 |
{<= NAME} -> NAME <= ? |
|
840 |
|
|
841 |
{like NAME} -> NAME like ? |
|
842 |
{in NAME COUNT} -> NAME in [?, ?, ..] |
|
843 |
|
|
844 |
{insert NAME1 NAME2} -> (NAME1, NAME2) values (?, ?) |
|
845 |
{update NAME1 NAME2} -> set NAME1 = ?, NAME2 = ? |
|
846 | ||
847 |
See also L<DBIx::Custom::QueryBuilder>. |
|
848 | ||
849 |
Default start tag is '{'. end tag is '}'. |
|
850 |
You can change this tag. |
|
851 | ||
852 |
$dbi->query_builder->start_tag('|'); |
|
853 |
$dbi->query_builder->end_tag('|'); |
|
update document
|
854 | |
855 |
=head2 5. Filtering |
|
856 | ||
857 |
Usually, Perl string is kept as internal string. |
|
858 |
If you want to save the string to database, You must encode the string. |
|
859 |
Filtering system help you to convert a data to another data |
|
860 |
when you save to the data and get the data form database. |
|
861 | ||
updated document
|
862 |
If you want to register filter, use C<register_filter()> method. |
update document
|
863 | |
864 |
$dbi->register_filter( |
|
865 |
to_upper_case => sub { |
|
866 |
my $value = shift; |
|
867 |
return uc $value; |
|
868 |
} |
|
869 |
); |
|
870 | ||
871 |
C<encode_utf8> and C<decode_utf8> filter is registerd by default. |
|
872 | ||
873 |
You can specify these filters to C<filter> argument of C<execute()> method. |
|
874 | ||
875 |
my $result = $dbi->execute( |
|
876 |
"select * from books where {= author} and {like title};" |
|
updated document
|
877 |
param => {author => 'Ken', title => '%Perl%'}, |
update document
|
878 |
filter => {author => 'to_upper_case, title => 'encode_utf8'} |
879 |
); |
|
880 | ||
881 |
you can also specify filter in suger methods, such as select(), update(), update_all, |
|
882 |
delete(), delete_all(), select(). |
|
883 | ||
884 |
$dbi->insert(table => 'books', |
|
885 |
param => {title => 'perl', author => 'Ken'}, |
|
886 |
filter => {title => 'encode_utf8'}); |
|
887 | ||
888 |
my $result = $dbi->select( |
|
889 |
table => 'books', |
|
890 |
column => [qw/author title/], |
|
891 |
where => {author => 'Ken'}, |
|
892 |
append => 'order by id limit 1', |
|
893 |
filter => {title => 'encode_utf8'} |
|
894 |
); |
|
895 | ||
updated document
|
896 |
Filter works each parmeter, but you prepare default filter for all parameters. |
update document
|
897 | |
898 |
$dbi->default_bind_filter('encode_utf8'); |
|
899 | ||
900 |
C<filter()> argument overwrites the filter specified by C<default_bind_filter()>. |
|
901 |
|
|
902 |
$dbi->default_bind_filter('encode_utf8'); |
|
903 |
$dbi->insert( |
|
904 |
table => 'books', |
|
905 |
param => {title => 'perl', author => 'Ken', price => 1000}, |
|
906 |
filter => {author => 'to_upper_case', price => undef} |
|
907 |
); |
|
908 | ||
909 |
This is same as the following one. |
|
910 | ||
911 |
$dbi->insert( |
|
912 |
table => 'books', |
|
913 |
param => {title => 'perl', author => 'Ken', price => 1000}, |
|
914 |
filter => {title => 'encode_uft8' author => 'to_upper_case'} |
|
915 |
); |
|
916 | ||
updated document
|
917 |
You can also specify filter when the row is fetched. This is reverse of bind filter. |
update document
|
918 | |
919 |
my $result = $dbi->select(table => 'books'); |
|
920 |
$result->filter({title => 'decode_utf8', author => 'to_upper_case'}); |
|
921 | ||
922 |
you can specify C<default_fetch_filter()>. |
|
923 | ||
924 |
$dbi->default_fetch_filter('decode_utf8'); |
|
925 | ||
updated document
|
926 |
C<DBIx::Custom::Result::filter()> overwrites the filter specified |
update document
|
927 |
by C<default_fetch_filter()> |
928 | ||
929 |
$dbi->default_fetch_filter('decode_utf8'); |
|
930 |
my $result = $dbi->select( |
|
931 |
table => 'books', |
|
932 |
columns => ['title', 'author', 'price'] |
|
933 |
); |
|
934 |
$result->filter({author => 'to_upper_case', price => undef}); |
|
935 | ||
936 |
This is same as the following one. |
|
937 | ||
938 |
my $result = $dbi->select( |
|
939 |
table => 'books', |
|
940 |
columns => ['title', 'author', 'price'] |
|
941 |
); |
|
942 |
$result->filter({title => 'decode_utf8', author => 'to_upper_case'}); |
|
removed reconnect method
|
943 | |
updated document
|
944 |
=head2 6. Performance |
945 | ||
946 |
If you execute insert statement by using select() method, |
|
947 |
you sometimes can't meet performance requirment. |
|
948 | ||
949 |
C<insert()> method is a little slow because SQL statement and statement handle |
|
950 |
is created every time. |
|
951 | ||
952 |
In that case, you can prepare a query by C<create_query()> method. |
|
953 |
|
|
954 |
my $query = $dbi->create_query( |
|
955 |
"insert into books {insert title author};" |
|
956 |
); |
|
957 |
|
|
958 |
# (In the case of update statement) |
|
959 |
my $query = $dbi->create_query( |
|
960 |
"update books {update author};"; |
|
961 |
); |
|
962 | ||
963 |
Execute query repeatedly |
|
964 |
|
|
965 |
my $inputs = [ |
|
966 |
{title => 'Perl', author => 'Ken'}, |
|
967 |
{title => 'Good days', author => 'Mike'} |
|
968 |
]; |
|
969 |
|
|
970 |
foreach my $input (@$inputs) { |
|
971 |
$dbi->execute($query, $input); |
|
972 |
} |
|
973 | ||
974 |
This is faster than C<insert()> and C<update()> method. |
|
975 | ||
976 |
C<execute()> method cache the parsing result of SQL soruce. |
|
977 |
Default to 1 |
|
978 | ||
979 |
$dbi->cache(1); |
|
980 | ||
981 |
Caching is on memory, but you can change this by C<cache_method()>. |
|
982 |
First argument is L<DBIx::Custom> object. |
|
983 |
Second argument is SQL source, |
|
984 |
such as "select * from books where {=title} and {=author};"; |
|
985 |
Third argument is parsed result, such as |
|
986 |
{sql => "select * from books where title = ? and author = ?", |
|
987 |
columns => ['title', 'author']}, this is hash reference. |
|
988 |
If argument is more than two, this is called ti set cache. |
|
989 |
otherwise, called to get cache. |
|
990 | ||
991 |
$dbi->cache_mehod(sub { |
|
992 |
sub { |
|
993 |
my $self = shift; |
|
994 |
|
|
995 |
$self->{_cached} ||= {}; |
|
996 |
|
|
997 |
# Set cache |
|
998 |
if (@_ > 1) { |
|
999 |
$self->{_cached}{$_[0]} = $_[1] |
|
1000 |
} |
|
1001 |
|
|
1002 |
# Get cache |
|
1003 |
else { |
|
1004 |
return $self->{_cached}{$_[0]} |
|
1005 |
} |
|
1006 |
} |
|
1007 |
}); |
|
1008 | ||
1009 |
=head2 7. More features |
|
1010 | ||
1011 |
=head3 Get DBI object |
|
1012 | ||
1013 |
You can get L<DBI> object and call any method of L<DBI>. |
|
1014 | ||
1015 |
$dbi->dbh->begin_work; |
|
1016 |
$dbi->dbh->commit; |
|
1017 |
$dbi->dbh->rollback; |
|
1018 | ||
1019 |
=head3 Change Result class |
|
1020 | ||
1021 |
You can change Result class if you need. |
|
1022 | ||
1023 |
package Your::Result; |
|
1024 |
use base 'DBIx::Custom::Result'; |
|
1025 |
|
|
1026 |
sub some_method { ... } |
|
1027 | ||
1028 |
1; |
|
1029 |
|
|
1030 |
package main; |
|
1031 |
|
|
1032 |
use Your::Result; |
|
1033 |
|
|
1034 |
my $dbi = DBIx::Custom->connect(...); |
|
1035 |
$dbi->result_class('Your::Result'); |
|
1036 | ||
1037 |
=head3 Custamize SQL builder object |
|
1038 | ||
1039 |
You can custamize SQL builder object |
|
1040 | ||
1041 |
my $dbi = DBIx::Custom->connect(...); |
|
1042 |
$dbi->query_builder->start_tag('|'); |
|
1043 |
$dbi->query_builder->end_tag('|'); |
|
1044 |
$dbi->query_builder->register_tag_processor( |
|
1045 |
name => sub { |
|
1046 |
... |
|
1047 |
} |
|
1048 |
); |
|
1049 | ||
update document
|
1050 |
=head1 ATTRIBUTES |
packaging one directory
|
1051 | |
removed DBIx::Custom commit ...
|
1052 |
=head2 C<user> |
packaging one directory
|
1053 | |
cleanup
|
1054 |
my $user = $dbi->user; |
1055 |
$dbi = $dbi->user('Ken'); |
|
removed DESTROY method(not b...
|
1056 | |
cleanup
|
1057 |
User name. |
1058 |
C<connect()> method use this value to connect the database. |
|
bind_filter argument is chan...
|
1059 |
|
removed DBIx::Custom commit ...
|
1060 |
=head2 C<password> |
packaging one directory
|
1061 | |
cleanup
|
1062 |
my $password = $dbi->password; |
1063 |
$dbi = $dbi->password('lkj&le`@s'); |
|
packaging one directory
|
1064 | |
cleanup
|
1065 |
Password. |
1066 |
C<connect()> method use this value to connect the database. |
|
removed DESTROY method(not b...
|
1067 | |
removed DBIx::Custom commit ...
|
1068 |
=head2 C<data_source> |
packaging one directory
|
1069 | |
cleanup
|
1070 |
my $data_source = $dbi->data_source; |
cleanup
|
1071 |
$dbi = $dbi->data_source("DBI:mysql:database=dbname"); |
removed DESTROY method(not b...
|
1072 | |
cleanup
|
1073 |
Data source. |
1074 |
C<connect()> method use this value to connect the database. |
|
removed DESTROY method(not b...
|
1075 | |
removed DBIx::Custom commit ...
|
1076 |
=head2 C<dbh> |
packaging one directory
|
1077 | |
cleanup
|
1078 |
my $dbh = $dbi->dbh; |
1079 |
$dbi = $dbi->dbh($dbh); |
|
packaging one directory
|
1080 | |
cleanup
|
1081 |
L<DBI> object. You can call all methods of L<DBI>. |
packaging one directory
|
1082 | |
removed DBIx::Custom commit ...
|
1083 |
=head2 C<filters> |
packaging one directory
|
1084 | |
cleanup
|
1085 |
my $filters = $dbi->filters; |
cleanup
|
1086 |
$dbi = $dbi->filters(\%filters); |
version 0.0901
|
1087 | |
removed DESTROY method(not b...
|
1088 |
Filter functions. |
renamed build_query to creat...
|
1089 |
"encode_utf8" and "decode_utf8" is registered by default. |
version 0.0901
|
1090 | |
renamed default_query_filter...
|
1091 |
=head2 C<default_bind_filter> |
packaging one directory
|
1092 | |
cleanup
|
1093 |
my $default_bind_filter = $dbi->default_bind_filter |
1094 |
$dbi = $dbi->default_bind_filter('encode_utf8'); |
|
packaging one directory
|
1095 | |
cleanup
|
1096 |
Default filter when parameter binding is executed. |
packaging one directory
|
1097 | |
removed DESTROY method(not b...
|
1098 |
=head2 C<default_fetch_filter> |
bind_filter argument is chan...
|
1099 | |
cleanup
|
1100 |
my $default_fetch_filter = $dbi->default_fetch_filter; |
1101 |
$dbi = $dbi->default_fetch_filter('decode_utf8'); |
|
bind_filter argument is chan...
|
1102 | |
cleanup
|
1103 |
Default filter when row is fetched. |
packaging one directory
|
1104 | |
removed DESTROY method(not b...
|
1105 |
=head2 C<result_class> |
bind_filter argument is chan...
|
1106 | |
cleanup
|
1107 |
my $result_class = $dbi->result_class; |
1108 |
$dbi = $dbi->result_class('DBIx::Custom::Result'); |
|
packaging one directory
|
1109 | |
removed DESTROY method(not b...
|
1110 |
Result class for select statement. |
renamed default_query_filter...
|
1111 |
Default to L<DBIx::Custom::Result>. |
update document
|
1112 | |
renamed default_query_filter...
|
1113 |
=head2 C<sql_builder> |
added commit method
|
1114 | |
cleanup
|
1115 |
my $sql_class = $dbi->sql_builder; |
1116 |
$dbi = $dbi->sql_builder(DBIx::Custom::QueryBuilder->new); |
|
added commit method
|
1117 | |
cleanup
|
1118 |
SQL builder. sql_builder must be |
renamed build_query to creat...
|
1119 |
the instance of L<DBIx::Custom::QueryBuilder> subclass. |
1120 |
Default to L<DBIx::Custom::QueryBuilder> object. |
|
cleanup
|
1121 | |
1122 |
=head2 C<cache> |
|
1123 | ||
1124 |
my $cache = $dbi->cache; |
|
1125 |
$dbi = $dbi->cache(1); |
|
1126 | ||
renamed build_query to creat...
|
1127 |
Enable parsed L<DBIx::Custom::Query> object caching. |
cleanup
|
1128 |
Default to 1. |
1129 | ||
1130 |
=head2 C<cache_method> |
|
1131 | ||
1132 |
$dbi = $dbi->cache_method(\&cache_method); |
|
1133 |
$cache_method = $dbi->cache_method |
|
1134 | ||
renamed build_query to creat...
|
1135 |
Method to set and get caches. |
cleanup
|
1136 | |
1137 |
B<Example:> |
|
1138 | ||
1139 |
$dbi->cache_method( |
|
1140 |
sub { |
|
1141 |
my $self = shift; |
|
1142 |
|
|
1143 |
$self->{_cached} ||= {}; |
|
1144 |
|
|
1145 |
if (@_ > 1) { |
|
1146 |
$self->{_cached}{$_[0]} = $_[1] |
|
1147 |
} |
|
1148 |
else { |
|
1149 |
return $self->{_cached}{$_[0]} |
|
1150 |
} |
|
1151 |
} |
|
1152 |
); |
|
added commit method
|
1153 | |
removed reconnect method
|
1154 |
=head1 METHODS |
added commit method
|
1155 | |
cleanup
|
1156 |
L<DBIx::Custom> inherits all methods from L<Object::Simple> |
1157 |
and implements the following new ones. |
|
added commit method
|
1158 | |
removed DBIx::Custom commit ...
|
1159 |
=head2 C<connect> |
packaging one directory
|
1160 | |
cleanup
|
1161 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname", |
update document
|
1162 |
user => 'ken', password => '!LFKD%$&'); |
bind_filter argument is chan...
|
1163 | |
cleanup
|
1164 |
Create a new L<DBIx::Custom> object and connect to the database. |
renamed build_query to creat...
|
1165 |
L<DBIx::Custom> is a wrapper of L<DBI>. |
1166 |
C<AutoCommit> and C<RaiseError> option is true, |
|
1167 |
and C<PrintError> option is false by default. |
|
packaging one directory
|
1168 | |
removed DBIx::Custom commit ...
|
1169 |
=head2 C<insert> |
packaging one directory
|
1170 | |
cleanup
|
1171 |
$dbi->insert(table => $table, |
1172 |
param => \%param, |
|
1173 |
append => $append, |
|
1174 |
filter => \%filter); |
|
update document
|
1175 | |
renamed build_query to creat...
|
1176 |
Execute insert statement. |
1177 |
C<insert> method have C<table>, C<param>, C<append> |
|
1178 |
and C<filter> arguments. |
|
1179 |
C<table> is a table name. |
|
1180 |
C<param> is column-value pairs. this must be hash reference. |
|
1181 |
C<append> is a string added at the end of the SQL statement. |
|
1182 |
C<filter> is filters when parameter binding is executed. |
|
1183 |
This is overwrites C<default_bind_filter>. |
|
1184 |
Return value of C<insert> is the count of affected rows. |
|
1185 | ||
removed DESTROY method(not b...
|
1186 |
B<Example:> |
version 0.0901
|
1187 | |
removed register_format()
|
1188 |
$dbi->insert(table => 'books', |
1189 |
param => {title => 'Perl', author => 'Taro'}, |
|
1190 |
append => "some statement", |
|
1191 |
filter => {title => 'encode_utf8'}) |
|
version 0.0901
|
1192 | |
removed DBIx::Custom commit ...
|
1193 |
=head2 C<update> |
packaging one directory
|
1194 | |
cleanup
|
1195 |
$dbi->update(table => $table, |
1196 |
param => \%params, |
|
1197 |
where => \%where, |
|
1198 |
append => $append, |
|
1199 |
filter => \%filter) |
|
version 0.0901
|
1200 | |
renamed build_query to creat...
|
1201 |
Execute update statement. |
1202 |
C<update> method have C<table>, C<param>, C<where>, C<append> |
|
1203 |
and C<filter> arguments. |
|
1204 |
C<table> is a table name. |
|
1205 |
C<param> is column-value pairs. this must be hash reference. |
|
1206 |
C<where> is where clause. this must be hash reference. |
|
1207 |
C<append> is a string added at the end of the SQL statement. |
|
1208 |
C<filter> is filters when parameter binding is executed. |
|
1209 |
This is overwrites C<default_bind_filter>. |
|
1210 |
Return value of C<update> is the count of affected rows. |
|
update document
|
1211 | |
removed DESTROY method(not b...
|
1212 |
B<Example:> |
bind_filter argument is chan...
|
1213 | |
removed register_format()
|
1214 |
$dbi->update(table => 'books', |
1215 |
param => {title => 'Perl', author => 'Taro'}, |
|
1216 |
where => {id => 5}, |
|
renamed build_query to creat...
|
1217 |
append => "for update", |
added commit method
|
1218 |
filter => {title => 'encode_utf8'}); |
version 0.0901
|
1219 | |
removed DBIx::Custom commit ...
|
1220 |
=head2 C<update_all> |
packaging one directory
|
1221 | |
cleanup
|
1222 |
$dbi->update_all(table => $table, |
1223 |
param => \%params, |
|
1224 |
filter => \%filter, |
|
1225 |
append => $append); |
|
update document
|
1226 | |
renamed build_query to creat...
|
1227 |
Execute update statement to update all rows. |
1228 |
Arguments is same as C<update> method, |
|
1229 |
except that C<update_all> don't have C<where> argument. |
|
1230 |
Return value of C<update_all> is the count of affected rows. |
|
version 0.0901
|
1231 | |
removed DESTROY method(not b...
|
1232 |
B<Example:> |
update document
|
1233 | |
removed register_format()
|
1234 |
$dbi->update_all(table => 'books', |
1235 |
param => {author => 'taro'}, |
|
1236 |
filter => {author => 'encode_utf8'}); |
|
packaging one directory
|
1237 | |
removed DBIx::Custom commit ...
|
1238 |
=head2 C<delete> |
packaging one directory
|
1239 | |
cleanup
|
1240 |
$dbi->delete(table => $table, |
1241 |
where => \%where, |
|
1242 |
append => $append, |
|
1243 |
filter => \%filter); |
|
bind_filter argument is chan...
|
1244 | |
renamed build_query to creat...
|
1245 |
Execute delete statement. |
1246 |
C<delete> method have C<table>, C<where>, C<append>, and C<filter> arguments. |
|
1247 |
C<table> is a table name. |
|
1248 |
C<where> is where clause. this must be hash reference. |
|
1249 |
C<append> is a string added at the end of the SQL statement. |
|
1250 |
C<filter> is filters when parameter binding is executed. |
|
1251 |
Return value of C<delete> is the count of affected rows. |
|
1252 | ||
removed DESTROY method(not b...
|
1253 |
B<Example:> |
packaging one directory
|
1254 | |
removed register_format()
|
1255 |
$dbi->delete(table => 'books', |
1256 |
where => {id => 5}, |
|
1257 |
append => 'some statement', |
|
removed reconnect method
|
1258 |
filter => {id => 'encode_utf8'}); |
version 0.0901
|
1259 | |
removed DBIx::Custom commit ...
|
1260 |
=head2 C<delete_all> |
packaging one directory
|
1261 | |
cleanup
|
1262 |
$dbi->delete_all(table => $table); |
packaging one directory
|
1263 | |
renamed build_query to creat...
|
1264 |
Execute delete statement to delete all rows. |
1265 |
Arguments is same as C<delete> method, |
|
1266 |
except that C<delete_all> don't have C<where> argument. |
|
1267 |
Return value of C<delete_all> is the count of affected rows. |
|
bind_filter argument is chan...
|
1268 | |
removed DESTROY method(not b...
|
1269 |
B<Example:> |
removed register_format()
|
1270 |
|
removed reconnect method
|
1271 |
$dbi->delete_all(table => 'books'); |
packaging one directory
|
1272 | |
removed DBIx::Custom commit ...
|
1273 |
=head2 C<select> |
packaging one directory
|
1274 |
|
cleanup
|
1275 |
my $result = $dbi->select(table => $table, |
1276 |
column => [@column], |
|
1277 |
where => \%where, |
|
1278 |
append => $append, |
|
1279 |
relation => \%relation, |
|
1280 |
filter => \%filter); |
|
update document
|
1281 | |
renamed build_query to creat...
|
1282 |
Execute select statement. |
1283 |
C<select> method have C<table>, C<column>, C<where>, C<append> |
|
1284 |
C<relation> and C<filter> arguments. |
|
1285 |
C<table> is a table name. |
|
1286 |
C<where> is where clause. this must be hash reference |
|
1287 |
or a string containing such tags as "{= title} or {= author}". |
|
1288 |
C<append> is a string added at the end of the SQL statement. |
|
1289 |
C<filter> is filters when parameter binding is executed. |
|
update document
|
1290 | |
removed DESTROY method(not b...
|
1291 |
B<Example:> |
update document
|
1292 | |
added commit method
|
1293 |
# select * from books; |
cleanup
|
1294 |
my $result = $dbi->select(table => 'books'); |
packaging one directory
|
1295 |
|
renamed build_query to creat...
|
1296 |
# select * from books where title = ?; |
1297 |
my $result = $dbi->select(table => 'books', where => {title => 'Perl'}); |
|
update document
|
1298 |
|
renamed build_query to creat...
|
1299 |
# select title, author from books where id = ? for update; |
cleanup
|
1300 |
my $result = $dbi->select( |
removed register_format()
|
1301 |
table => 'books', |
removed reconnect method
|
1302 |
column => ['title', 'author'], |
removed register_format()
|
1303 |
where => {id => 1}, |
1304 |
appned => 'for update' |
|
update document
|
1305 |
); |
1306 |
|
|
added commit method
|
1307 |
# select books.name as book_name from books, rental |
1308 |
# where books.id = rental.book_id; |
|
1309 |
my $result = $dbi->select( |
|
removed reconnect method
|
1310 |
table => ['books', 'rental'], |
1311 |
column => ['books.name as book_name'] |
|
added commit method
|
1312 |
relation => {'books.id' => 'rental.book_id'} |
update document
|
1313 |
); |
1314 | ||
renamed build_query to creat...
|
1315 |
=head2 C<create_query> |
removed reconnect method
|
1316 |
|
renamed build_query to creat...
|
1317 |
my $query = $dbi->create_query( |
removed DBIx::Custom commit ...
|
1318 |
"select * from authors where {= name} and {= age};" |
1319 |
); |
|
1320 | ||
renamed build_query to creat...
|
1321 |
Create the instance of L<DBIx::Custom::Query> from SQL source. |
packaging one directory
|
1322 | |
removed DBIx::Custom commit ...
|
1323 |
=head2 C<execute> |
removed reconnect method
|
1324 | |
cleanup
|
1325 |
my $result = $dbi->execute($query, param => $params, filter => \%filter); |
1326 |
my $result = $dbi->execute($source, param => $params, filter => \%filter); |
|
removed reconnect method
|
1327 | |
renamed build_query to creat...
|
1328 |
Execute query or SQL source. Query is L<DBIx::Csutom::Query> object. |
1329 |
Return value is L<DBIx::Custom::Result> in select statement, |
|
1330 |
or the count of affected rows in insert, update, delete statement. |
|
removed reconnect method
|
1331 | |
removed DBIx::Custom commit ...
|
1332 |
B<Example:> |
removed reconnect method
|
1333 | |
cleanup
|
1334 |
my $result = $dbi->execute("select * from authors where {= name} and {= age}", |
removed reconnect method
|
1335 |
param => {name => 'taro', age => 19}); |
1336 |
|
|
1337 |
while (my $row = $result->fetch) { |
|
1338 |
# do something |
|
1339 |
} |
|
1340 | ||
removed DBIx::Custom commit ...
|
1341 |
=head2 C<register_filter> |
removed reconnect method
|
1342 | |
1343 |
$dbi->register_filter(%filters); |
|
removed DESTROY method(not b...
|
1344 |
$dbi->register_filter(\%filters); |
removed reconnect method
|
1345 |
|
renamed build_query to creat...
|
1346 |
Register filter. Registered filters is available in the following methods |
1347 |
or arguments. |
|
1348 | ||
1349 |
=over 4 |
|
1350 | ||
1351 |
=item * |
|
1352 | ||
1353 |
C<default_bind_filter()> |
|
1354 | ||
1355 |
=item * |
|
1356 | ||
1357 |
C<default_fetch_filter()> |
|
1358 | ||
1359 |
=item * |
|
1360 | ||
1361 |
C<filter> argument of C<insert()>, C<update()>, |
|
1362 |
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>, |
|
1363 |
C<execute> method. |
|
1364 | ||
1365 |
=item * |
|
1366 | ||
1367 |
C<DBIx::Custom::Query::default_filter()> |
|
1368 | ||
1369 |
=item * |
|
1370 | ||
1371 |
C<DBIx::Csutom::Query::filter()> |
|
1372 | ||
1373 |
=item * |
|
1374 | ||
1375 |
C<DBIx::Custom::Result::default_filter()> |
|
1376 | ||
1377 |
=item * |
|
1378 | ||
1379 |
C<DBIx::Custom::Result::filter()> |
|
1380 | ||
1381 |
=back |
|
removed DBIx::Custom commit ...
|
1382 | |
1383 |
B<Example:> |
|
packaging one directory
|
1384 | |
removed reconnect method
|
1385 |
$dbi->register_filter( |
1386 |
encode_utf8 => sub { |
|
1387 |
my $value = shift; |
|
1388 |
|
|
1389 |
require Encode; |
|
1390 |
|
|
1391 |
return Encode::encode('UTF-8', $value); |
|
1392 |
}, |
|
1393 |
decode_utf8 => sub { |
|
1394 |
my $value = shift; |
|
1395 |
|
|
1396 |
require Encode; |
|
1397 |
|
|
1398 |
return Encode::decode('UTF-8', $value) |
|
1399 |
} |
|
1400 |
); |
|
1401 | ||
removed DESTROY method(not b...
|
1402 |
=head1 BUGS |
1403 | ||
renamed build_query to creat...
|
1404 |
Please tell me bugs if found. |
removed DESTROY method(not b...
|
1405 | |
1406 |
C<< <kimoto.yuki at gmail.com> >> |
|
1407 | ||
1408 |
L<http://github.com/yuki-kimoto/DBIx-Custom> |
|
1409 | ||
removed reconnect method
|
1410 |
=head1 AUTHOR |
1411 | ||
1412 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
|
version 0.0901
|
1413 | |
packaging one directory
|
1414 |
=head1 COPYRIGHT & LICENSE |
1415 | ||
1416 |
Copyright 2009 Yuki Kimoto, all rights reserved. |
|
1417 | ||
1418 |
This program is free software; you can redistribute it and/or modify it |
|
1419 |
under the same terms as Perl itself. |
|
1420 | ||
1421 |
=cut |
|
added cache_method attribute
|
1422 | |
1423 |