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; |
|
many many changes
|
11 |
use DBIx::Custom::SQLTemplate; |
cleanup
|
12 |
use DBIx::Custom::Query; |
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/]); |
update document
|
17 |
__PACKAGE__->attr([qw/default_query_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'); |
removed register_format()
|
27 |
__PACKAGE__->attr(sql_template => sub { DBIx::Custom::SQLTemplate->new }); |
28 | ||
some changed
|
29 |
sub register_filter { |
packaging one directory
|
30 |
my $invocant = shift; |
31 |
|
|
update document
|
32 |
# Add filter |
catch up with Object::Simple...
|
33 |
my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
34 |
$invocant->filters({%{$invocant->filters}, %$filters}); |
|
35 |
|
|
packaging one directory
|
36 |
return $invocant; |
37 |
} |
|
38 | ||
removed register_format()
|
39 |
sub auto_commit { |
packaging one directory
|
40 |
my $self = shift; |
41 |
|
|
42 |
if (@_) { |
|
update document
|
43 |
|
44 |
# Set AutoCommit |
|
packaging one directory
|
45 |
$self->dbh->{AutoCommit} = $_[0]; |
update document
|
46 |
|
packaging one directory
|
47 |
return $self; |
48 |
} |
|
49 |
return $self->dbh->{AutoCommit}; |
|
50 |
} |
|
51 | ||
added commit method
|
52 |
sub commit { shift->dbh->commit } |
53 |
sub rollback { shift->dbh->rollback } |
|
54 | ||
packaging one directory
|
55 |
sub connect { |
removed register_format()
|
56 |
my $proto = shift; |
57 |
|
|
58 |
# Create |
|
59 |
my $self = ref $proto ? $proto : $proto->new(@_); |
|
update document
|
60 |
|
61 |
# Information |
|
packaging one directory
|
62 |
my $data_source = $self->data_source; |
63 |
my $user = $self->user; |
|
64 |
my $password = $self->password; |
|
65 |
|
|
update document
|
66 |
# Connect |
packaging one directory
|
67 |
my $dbh = eval{DBI->connect( |
68 |
$data_source, |
|
69 |
$user, |
|
70 |
$password, |
|
71 |
{ |
|
72 |
RaiseError => 1, |
|
73 |
PrintError => 0, |
|
74 |
AutoCommit => 1, |
|
75 |
} |
|
76 |
)}; |
|
77 |
|
|
update document
|
78 |
# Connect error |
packaging one directory
|
79 |
croak $@ if $@; |
80 |
|
|
update document
|
81 |
# Database handle |
packaging one directory
|
82 |
$self->dbh($dbh); |
update document
|
83 |
|
packaging one directory
|
84 |
return $self; |
85 |
} |
|
86 | ||
87 |
sub DESTROY { |
|
88 |
my $self = shift; |
|
update document
|
89 |
|
90 |
# Disconnect |
|
packaging one directory
|
91 |
$self->disconnect if $self->connected; |
92 |
} |
|
93 | ||
update document
|
94 |
sub connected { ref shift->{dbh} eq 'DBI::db' } |
packaging one directory
|
95 | |
96 |
sub disconnect { |
|
97 |
my $self = shift; |
|
update document
|
98 |
|
packaging one directory
|
99 |
if ($self->connected) { |
update document
|
100 |
|
101 |
# Disconnect |
|
packaging one directory
|
102 |
$self->dbh->disconnect; |
103 |
delete $self->{dbh}; |
|
104 |
} |
|
update document
|
105 |
|
106 |
return $self; |
|
packaging one directory
|
107 |
} |
108 | ||
109 |
sub reconnect { |
|
110 |
my $self = shift; |
|
update document
|
111 |
|
112 |
# Reconnect |
|
packaging one directory
|
113 |
$self->disconnect if $self->connected; |
114 |
$self->connect; |
|
update document
|
115 |
|
116 |
return $self; |
|
packaging one directory
|
117 |
} |
118 | ||
119 |
sub create_query { |
|
120 |
my ($self, $template) = @_; |
|
cleanup
|
121 |
|
packaging one directory
|
122 |
# Create query from SQL template |
removed register_format()
|
123 |
my $sql_template = $self->sql_template; |
packaging one directory
|
124 |
|
update document
|
125 |
# Get cached query |
added commit method
|
126 |
my $cache = $self->{_cache}->{$template}; |
packaging one directory
|
127 |
|
128 |
# Create query |
|
fix timeformat tests
|
129 |
my $query; |
update document
|
130 |
if ($cache) { |
cleanup
|
131 |
$query = DBIx::Custom::Query->new( |
update document
|
132 |
sql => $cache->sql, |
133 |
columns => $cache->columns |
|
cleanup
|
134 |
); |
fix timeformat tests
|
135 |
} |
136 |
else { |
|
removed register_format()
|
137 |
$query = eval{$sql_template->create_query($template)}; |
packaging one directory
|
138 |
croak($@) if $@; |
139 |
|
|
update document
|
140 |
$self->{_cache}->{$template} = $query |
added commit method
|
141 |
unless $self->{_cache}->{$template}; |
packaging one directory
|
142 |
} |
143 |
|
|
144 |
# Prepare statement handle |
|
add all tests
|
145 |
my $sth = $self->dbh->prepare($query->{sql}); |
packaging one directory
|
146 |
|
147 |
# Set statement handle |
|
148 |
$query->sth($sth); |
|
149 |
|
|
150 |
return $query; |
|
151 |
} |
|
152 | ||
removed register_format()
|
153 |
our %VALID_EXECUTE_ARGS = map { $_ => 1 } qw/param filter/; |
154 | ||
rename query() to execute()
|
155 |
sub execute{ |
removed register_format()
|
156 |
my $self = shift; |
157 |
my $query = shift; |
|
158 |
|
|
159 |
# Arguments |
|
160 |
my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
161 |
|
|
162 |
# Check arguments |
|
163 |
foreach my $name (keys %$args) { |
|
164 |
croak "\"$name\" is invalid name" |
|
165 |
unless $VALID_EXECUTE_ARGS{$name}; |
|
166 |
} |
|
167 |
|
|
168 |
my $params = $args->{param} || {}; |
|
packaging one directory
|
169 |
|
170 |
# First argument is SQL template |
|
Simplify key search
|
171 |
unless (ref $query eq 'DBIx::Custom::Query') { |
172 |
my $template; |
|
173 |
|
|
174 |
if (ref $query eq 'ARRAY') { |
|
many many changes
|
175 |
$template = $query->[0]; |
Simplify key search
|
176 |
} |
177 |
else { $template = $query } |
|
178 |
|
|
many many changes
|
179 |
$query = $self->create_query($template); |
packaging one directory
|
180 |
} |
removed register_format()
|
181 |
|
many change
|
182 |
my $filter = $args->{filter} || $query->filter || {}; |
removed register_format()
|
183 |
|
packaging one directory
|
184 |
# Create bind value |
many many changes
|
185 |
my $bind_values = $self->_build_bind_values($query, $params, $filter); |
packaging one directory
|
186 |
|
187 |
# Execute |
|
version 0.0901
|
188 |
my $sth = $query->sth; |
189 |
my $affected = eval{$sth->execute(@$bind_values)}; |
|
packaging one directory
|
190 |
|
191 |
# Execute error |
|
192 |
if (my $execute_error = $@) { |
|
193 |
require Data::Dumper; |
|
194 |
my $sql = $query->{sql} || ''; |
|
195 |
my $params_dump = Data::Dumper->Dump([$params], ['*params']); |
|
196 |
|
|
197 |
croak("$execute_error" . |
|
198 |
"<Your SQL>\n$sql\n" . |
|
199 |
"<Your parameters>\n$params_dump"); |
|
200 |
} |
|
201 |
|
|
202 |
# Return resultset if select statement is executed |
|
203 |
if ($sth->{NUM_OF_FIELDS}) { |
|
204 |
|
|
205 |
# Get result class |
|
206 |
my $result_class = $self->result_class; |
|
207 |
|
|
208 |
# Create result |
|
209 |
my $result = $result_class->new({ |
|
many many changes
|
210 |
sth => $sth, |
211 |
default_filter => $self->default_fetch_filter, |
|
212 |
filters => $self->filters |
|
packaging one directory
|
213 |
}); |
214 |
return $result; |
|
215 |
} |
|
version 0.0901
|
216 |
return $affected; |
packaging one directory
|
217 |
} |
218 | ||
219 |
sub _build_bind_values { |
|
many many changes
|
220 |
my ($self, $query, $params, $filter) = @_; |
packaging one directory
|
221 |
|
222 |
# binding values |
|
223 |
my @bind_values; |
|
224 |
|
|
Simplify key search
|
225 |
# Build bind values |
simplify filtering system
|
226 |
my $count = {}; |
227 |
foreach my $column (@{$query->columns}) { |
|
packaging one directory
|
228 |
|
add query filter error check
|
229 |
croak "\"$column\" is not exists in params" |
230 |
unless exists $params->{$column}; |
|
231 |
|
|
Simplify key search
|
232 |
# Value |
add query filter error check
|
233 |
my $value = ref $params->{$column} eq 'ARRAY' |
simplify filtering system
|
234 |
? $params->{$column}->[$count->{$column} || 0] |
235 |
: $params->{$column}; |
|
packaging one directory
|
236 |
|
Simplify key search
|
237 |
# Filter |
simplify filtering system
|
238 |
$filter ||= {}; |
many many changes
|
239 |
|
simplify filtering system
|
240 |
# Filter name |
241 |
my $fname = $filter->{$column} || $self->default_query_filter || ''; |
|
242 |
|
|
add query filter error check
|
243 |
my $filter_func; |
244 |
if ($fname) { |
|
245 |
|
|
246 |
if (ref $fname eq 'CODE') { |
|
247 |
$filter_func = $fname; |
|
248 |
} |
|
249 |
else { |
|
250 |
my $filters = $self->filters; |
|
251 |
croak "Not exists filter \"$fname\"" unless exists $filters->{$fname}; |
|
252 |
$filter_func = $filters->{$fname}; |
|
253 |
} |
|
254 |
} |
|
255 |
|
|
256 |
push @bind_values, $filter_func |
|
257 |
? $filter_func->($value) |
|
many change
|
258 |
: $value; |
simplify filtering system
|
259 |
|
260 |
# Count up |
|
261 |
$count->{$column}++; |
|
cleanup
|
262 |
} |
263 |
|
|
Simplify key search
|
264 |
return \@bind_values; |
cleanup
|
265 |
} |
266 | ||
removed register_format()
|
267 |
our %VALID_INSERT_ARGS = map { $_ => 1 } qw/table param append filter/; |
cleanup insert
|
268 | |
packaging one directory
|
269 |
sub insert { |
removed register_format()
|
270 |
my $self = shift; |
cleanup insert
|
271 |
|
272 |
# Arguments |
|
removed register_format()
|
273 |
my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
274 | ||
cleanup insert
|
275 |
# Check arguments |
276 |
foreach my $name (keys %$args) { |
|
277 |
croak "\"$name\" is invalid name" |
|
278 |
unless $VALID_INSERT_ARGS{$name}; |
|
279 |
} |
|
280 |
|
|
removed register_format()
|
281 |
# Arguments |
282 |
my $table = $args->{table} || ''; |
|
283 |
my $param = $args->{param} || {}; |
|
284 |
my $append = $args->{append} || ''; |
|
285 |
my $filter = $args->{filter}; |
|
packaging one directory
|
286 |
|
287 |
# Insert keys |
|
removed register_format()
|
288 |
my @insert_keys = keys %$param; |
packaging one directory
|
289 |
|
290 |
# Not exists insert keys |
|
291 |
croak("Key-value pairs for insert must be specified to 'insert' second argument") |
|
292 |
unless @insert_keys; |
|
293 |
|
|
294 |
# Templte for insert |
|
295 |
my $template = "insert into $table {insert " . join(' ', @insert_keys) . '}'; |
|
removed register_format()
|
296 |
$template .= " $append" if $append; |
packaging one directory
|
297 |
|
298 |
# Execute query |
|
removed register_format()
|
299 |
my $ret_val = $self->execute($template, param => $param, |
300 |
filter => $filter); |
|
packaging one directory
|
301 |
|
302 |
return $ret_val; |
|
303 |
} |
|
304 | ||
cleanup update and update_al...
|
305 |
our %VALID_UPDATE_ARGS |
removed register_format()
|
306 |
= map { $_ => 1 } qw/table param where append filter allow_update_all/; |
cleanup update and update_al...
|
307 | |
packaging one directory
|
308 |
sub update { |
removed register_format()
|
309 |
my $self = shift; |
310 | ||
311 |
my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
cleanup update and update_al...
|
312 |
|
313 |
# Check arguments |
|
314 |
foreach my $name (keys %$args) { |
|
315 |
croak "\"$name\" is invalid name" |
|
316 |
unless $VALID_UPDATE_ARGS{$name}; |
|
317 |
} |
|
318 |
|
|
319 |
# Arguments |
|
removed register_format()
|
320 |
my $table = $args->{table} || ''; |
321 |
my $param = $args->{param} || {}; |
|
322 |
my $where = $args->{where} || {}; |
|
cleanup update and update_al...
|
323 |
my $append_statement = $args->{append} || ''; |
many many changes
|
324 |
my $filter = $args->{filter}; |
cleanup update and update_al...
|
325 |
my $allow_update_all = $args->{allow_update_all}; |
packaging one directory
|
326 |
|
327 |
# Update keys |
|
removed register_format()
|
328 |
my @update_keys = keys %$param; |
packaging one directory
|
329 |
|
330 |
# Not exists update kyes |
|
331 |
croak("Key-value pairs for update must be specified to 'update' second argument") |
|
332 |
unless @update_keys; |
|
333 |
|
|
334 |
# Where keys |
|
removed register_format()
|
335 |
my @where_keys = keys %$where; |
packaging one directory
|
336 |
|
337 |
# Not exists where keys |
|
338 |
croak("Key-value pairs for where clause must be specified to 'update' third argument") |
|
cleanup update and update_al...
|
339 |
if !@where_keys && !$allow_update_all; |
packaging one directory
|
340 |
|
341 |
# Update clause |
|
342 |
my $update_clause = '{update ' . join(' ', @update_keys) . '}'; |
|
343 |
|
|
344 |
# Where clause |
|
345 |
my $where_clause = ''; |
|
simplify filtering system
|
346 |
my $new_where = {}; |
many change
|
347 |
|
packaging one directory
|
348 |
if (@where_keys) { |
349 |
$where_clause = 'where '; |
|
350 |
foreach my $where_key (@where_keys) { |
|
simplify filtering system
|
351 |
|
352 |
$where_clause .= "{= $where_key} and "; |
|
packaging one directory
|
353 |
} |
354 |
$where_clause =~ s/ and $//; |
|
355 |
} |
|
356 |
|
|
357 |
# Template for update |
|
358 |
my $template = "update $table $update_clause $where_clause"; |
|
insert, update, delete appnd...
|
359 |
$template .= " $append_statement" if $append_statement; |
packaging one directory
|
360 |
|
361 |
# Rearrange parammeters |
|
removed register_format()
|
362 |
foreach my $wkey (@where_keys) { |
simplify filtering system
|
363 |
|
removed register_format()
|
364 |
if (exists $param->{$wkey}) { |
365 |
$param->{$wkey} = [$param->{$wkey}] |
|
366 |
unless ref $param->{$wkey} eq 'ARRAY'; |
|
simplify filtering system
|
367 |
|
removed register_format()
|
368 |
push @{$param->{$wkey}}, $where->{$wkey}; |
simplify filtering system
|
369 |
} |
add tests
|
370 |
else { |
removed register_format()
|
371 |
$param->{$wkey} = $where->{$wkey}; |
add tests
|
372 |
} |
simplify filtering system
|
373 |
} |
packaging one directory
|
374 |
|
375 |
# Execute query |
|
removed register_format()
|
376 |
my $ret_val = $self->execute($template, param => $param, |
377 |
filter => $filter); |
|
packaging one directory
|
378 |
|
379 |
return $ret_val; |
|
380 |
} |
|
381 | ||
382 |
sub update_all { |
|
removed register_format()
|
383 |
my $self = shift;; |
cleanup update and update_al...
|
384 |
|
removed register_format()
|
385 |
# Arguments |
386 |
my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
387 |
|
|
refactoring select
|
388 |
# Allow all update |
cleanup update and update_al...
|
389 |
$args->{allow_update_all} = 1; |
insert, update, delete appnd...
|
390 |
|
update document
|
391 |
# Update all rows |
removed register_format()
|
392 |
return $self->update($args); |
packaging one directory
|
393 |
} |
394 | ||
refactoring delete and delet...
|
395 |
our %VALID_DELETE_ARGS |
removed register_format()
|
396 |
= map { $_ => 1 } qw/table where append filter allow_delete_all/; |
refactoring delete and delet...
|
397 | |
packaging one directory
|
398 |
sub delete { |
removed register_format()
|
399 |
my $self = shift; |
400 |
|
|
401 |
# Arguments |
|
402 |
my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
refactoring delete and delet...
|
403 |
|
404 |
# Check arguments |
|
405 |
foreach my $name (keys %$args) { |
|
406 |
croak "\"$name\" is invalid name" |
|
407 |
unless $VALID_DELETE_ARGS{$name}; |
|
408 |
} |
|
409 |
|
|
410 |
# Arguments |
|
removed register_format()
|
411 |
my $table = $args->{table} || ''; |
412 |
my $where = $args->{where} || {}; |
|
refactoring delete and delet...
|
413 |
my $append_statement = $args->{append}; |
removed register_format()
|
414 |
my $filter = $args->{filter}; |
refactoring delete and delet...
|
415 |
my $allow_delete_all = $args->{allow_delete_all}; |
packaging one directory
|
416 |
|
417 |
# Where keys |
|
removed register_format()
|
418 |
my @where_keys = keys %$where; |
packaging one directory
|
419 |
|
420 |
# Not exists where keys |
|
421 |
croak("Key-value pairs for where clause must be specified to 'delete' second argument") |
|
refactoring delete and delet...
|
422 |
if !@where_keys && !$allow_delete_all; |
packaging one directory
|
423 |
|
424 |
# Where clause |
|
425 |
my $where_clause = ''; |
|
426 |
if (@where_keys) { |
|
427 |
$where_clause = 'where '; |
|
removed register_format()
|
428 |
foreach my $wkey (@where_keys) { |
429 |
$where_clause .= "{= $wkey} and "; |
|
packaging one directory
|
430 |
} |
431 |
$where_clause =~ s/ and $//; |
|
432 |
} |
|
433 |
|
|
434 |
# Template for delete |
|
435 |
my $template = "delete from $table $where_clause"; |
|
insert, update, delete appnd...
|
436 |
$template .= " $append_statement" if $append_statement; |
packaging one directory
|
437 |
|
438 |
# Execute query |
|
removed register_format()
|
439 |
my $ret_val = $self->execute($template, param => $where, |
440 |
filter => $filter); |
|
packaging one directory
|
441 |
|
442 |
return $ret_val; |
|
443 |
} |
|
444 | ||
445 |
sub delete_all { |
|
removed register_format()
|
446 |
my $self = shift; |
447 |
|
|
448 |
# Arguments |
|
449 |
my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
refactoring delete and delet...
|
450 |
|
refactoring select
|
451 |
# Allow all delete |
refactoring delete and delet...
|
452 |
$args->{allow_delete_all} = 1; |
insert, update, delete appnd...
|
453 |
|
update document
|
454 |
# Delete all rows |
removed register_format()
|
455 |
return $self->delete($args); |
packaging one directory
|
456 |
} |
457 | ||
refactoring select
|
458 |
our %VALID_SELECT_ARGS |
added commit method
|
459 |
= map { $_ => 1 } qw/table column where append relation filter param/; |
refactoring select
|
460 | |
packaging one directory
|
461 |
sub select { |
removed register_format()
|
462 |
my $self = shift;; |
packaging one directory
|
463 |
|
removed register_format()
|
464 |
my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
packaging one directory
|
465 |
|
refactoring select
|
466 |
# Check arguments |
467 |
foreach my $name (keys %$args) { |
|
468 |
croak "\"$name\" is invalid name" |
|
469 |
unless $VALID_SELECT_ARGS{$name}; |
|
470 |
} |
|
packaging one directory
|
471 |
|
refactoring select
|
472 |
# Arguments |
removed register_format()
|
473 |
my $tables = $args->{table} || []; |
474 |
$tables = [$tables] unless ref $tables eq 'ARRAY'; |
|
added commit method
|
475 |
my $columns = $args->{column} || []; |
476 |
my $where = $args->{where} || {}; |
|
477 |
my $relation = $args->{relation}; |
|
478 |
my $append = $args->{append}; |
|
479 |
my $filter = $args->{filter}; |
|
480 |
my $param = $args->{param} || {}; |
|
packaging one directory
|
481 |
|
482 |
# SQL template for select statement |
|
483 |
my $template = 'select '; |
|
484 |
|
|
added commit method
|
485 |
# Column clause |
packaging one directory
|
486 |
if (@$columns) { |
487 |
foreach my $column (@$columns) { |
|
488 |
$template .= "$column, "; |
|
489 |
} |
|
490 |
$template =~ s/, $/ /; |
|
491 |
} |
|
492 |
else { |
|
493 |
$template .= '* '; |
|
494 |
} |
|
495 |
|
|
added commit method
|
496 |
# Table |
packaging one directory
|
497 |
$template .= 'from '; |
498 |
foreach my $table (@$tables) { |
|
499 |
$template .= "$table, "; |
|
500 |
} |
|
501 |
$template =~ s/, $/ /; |
|
502 |
|
|
added commit method
|
503 |
# Where clause |
504 |
my @where_keys = keys %$where; |
|
packaging one directory
|
505 |
if (@where_keys) { |
506 |
$template .= 'where '; |
|
507 |
foreach my $where_key (@where_keys) { |
|
compile success
|
508 |
$template .= "{= $where_key} and "; |
packaging one directory
|
509 |
} |
510 |
} |
|
511 |
$template =~ s/ and $//; |
|
512 |
|
|
added commit method
|
513 |
# Relation |
514 |
if ($relation) { |
|
515 |
$template .= @where_keys ? "and " : "where "; |
|
516 |
foreach my $rkey (keys %$relation) { |
|
517 |
$template .= "$rkey = " . $relation->{$rkey} . " and "; |
|
packaging one directory
|
518 |
} |
519 |
} |
|
added commit method
|
520 |
$template =~ s/ and $//; |
521 |
|
|
522 |
# Append some statement |
|
523 |
$template .= " $append" if $append; |
|
packaging one directory
|
524 |
|
525 |
# Execute query |
|
added commit method
|
526 |
my $result = $self->execute($template, param => $where, |
removed register_format()
|
527 |
filter => $filter); |
packaging one directory
|
528 |
|
529 |
return $result; |
|
530 |
} |
|
531 | ||
532 |
=head1 NAME |
|
533 | ||
update document
|
534 |
DBIx::Custom - DBI with hash parameter binding and filtering system |
packaging one directory
|
535 | |
version 0.0901
|
536 |
=head1 VERSION |
packaging one directory
|
537 | |
update document
|
538 |
Version 0.1502 |
catch up with Object::Simple...
|
539 | |
540 |
=cut |
|
541 | ||
update document
|
542 |
our $VERSION = '0.1502'; |
some changed
|
543 |
$VERSION = eval $VERSION; |
packaging one directory
|
544 | |
cleanup
|
545 |
=head1 STATE |
546 | ||
547 |
This module is not stable. Method name and functionality will be change. |
|
548 | ||
version 0.0901
|
549 |
=head1 SYNOPSYS |
550 |
|
|
removed register_format()
|
551 |
# Connect |
552 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=books", |
|
553 |
user => 'ken', password => '!LFKD%$&'); |
|
some changed
|
554 |
|
version 0.0901
|
555 |
# Insert |
removed register_format()
|
556 |
$dbi->insert(table => 'books', |
update document
|
557 |
param => {title => 'perl', author => 'Ken'}, |
removed register_format()
|
558 |
filter => {title => 'encode_utf8'}); |
version 0.0901
|
559 |
|
560 |
# Update |
|
removed register_format()
|
561 |
$dbi->update(table => 'books', |
562 |
param => {title => 'aaa', author => 'Ken'}, |
|
update document
|
563 |
where => {id => 5}, |
564 |
filter => {title => 'encode_utf8'}); |
|
removed register_format()
|
565 |
|
566 |
# Update all |
|
567 |
$dbi->update_all(table => 'books', |
|
update document
|
568 |
param => {title => 'aaa'}, |
removed register_format()
|
569 |
filter => {title => 'encode_utf8'}); |
version 0.0901
|
570 |
|
571 |
# Delete |
|
removed register_format()
|
572 |
$dbi->delete(table => 'books', |
update document
|
573 |
where => {author => 'Ken'}, |
removed register_format()
|
574 |
filter => {title => 'encode_utf8'}); |
version 0.0901
|
575 |
|
removed register_format()
|
576 |
# Delete all |
577 |
$dbi->delete_all(table => 'books'); |
|
renamed fetch_rows to fetch_...
|
578 |
|
removed register_format()
|
579 |
# Select |
580 |
my $result = $dbi->select(table => 'books'); |
|
renamed fetch_rows to fetch_...
|
581 |
|
removed register_format()
|
582 |
# Select(more complex) |
renamed fetch_rows to fetch_...
|
583 |
my $result = $dbi->select( |
update document
|
584 |
table => 'books', |
585 |
column => [qw/author title/], |
|
586 |
where => {author => 'Ken'}, |
|
587 |
append => 'order by id limit 1', |
|
588 |
filter => {tilte => 'encode_utf8'} |
|
renamed fetch_rows to fetch_...
|
589 |
); |
added commit method
|
590 |
|
591 |
# Select(Join table) |
|
592 |
my $result = $dbi->select( |
|
593 |
table => ['books', 'rental'], |
|
594 |
column => ['books.name as book_name'] |
|
595 |
relation => {'books.id' => 'rental.book_id'} |
|
596 |
); |
|
packaging one directory
|
597 | |
removed register_format()
|
598 |
# Execute SQL |
599 |
$dbi->execute("select title from books"); |
|
600 |
|
|
601 |
# Execute SQL with parameters and filter |
|
602 |
$dbi->execute("select id from books where {= author} && {like title}", |
|
603 |
param => {author => 'ken', title => '%Perl%'}, |
|
604 |
filter => {tilte => 'encode_utf8'}); |
|
605 |
|
|
606 |
# Default filter |
|
607 |
$dbi->default_query_filter('encode_utf8'); |
|
608 |
$dbi->default_fetch_filter('decode_utf8'); |
|
609 |
|
|
610 |
# Fetch |
|
611 |
while (my $row = $result->fetch) { |
|
612 |
# ... |
|
613 |
} |
|
614 |
|
|
615 |
# Fetch hash |
|
616 |
while (my $row = $result->fetch_hash) { |
|
617 |
|
|
618 |
} |
|
619 |
|
|
update document
|
620 |
# DBI instance |
621 |
my $dbh = $dbi->dbh; |
|
removed register_format()
|
622 |
|
update document
|
623 |
=head1 ATTRIBUTES |
packaging one directory
|
624 | |
625 |
=head2 user |
|
626 | ||
update document
|
627 |
Database user name. |
bind_filter argument is chan...
|
628 |
|
version 0.0901
|
629 |
$dbi = $dbi->user('Ken'); |
630 |
$user = $dbi->user; |
|
bind_filter argument is chan...
|
631 |
|
packaging one directory
|
632 |
=head2 password |
633 | ||
update document
|
634 |
Database password. |
bind_filter argument is chan...
|
635 |
|
version 0.0901
|
636 |
$dbi = $dbi->password('lkj&le`@s'); |
637 |
$password = $dbi->password; |
|
packaging one directory
|
638 | |
639 |
=head2 data_source |
|
640 | ||
update document
|
641 |
Database data source. |
bind_filter argument is chan...
|
642 |
|
version 0.0901
|
643 |
$dbi = $dbi->data_source("dbi:mysql:dbname=$database"); |
644 |
$data_source = $dbi->data_source; |
|
packaging one directory
|
645 |
|
version 0.0901
|
646 |
If you know data source more, See also L<DBI>. |
647 | ||
removed register_format()
|
648 |
=head2 sql_template |
packaging one directory
|
649 | |
update document
|
650 |
SQLTemplate instance. sql_template attribute must be |
651 |
the instance of L<DBIx::Cutom::SQLTemplate> subclass. |
|
packaging one directory
|
652 | |
removed register_format()
|
653 |
$dbi = $dbi->sql_template(DBIx::Cutom::SQLTemplate->new); |
654 |
$sql_template = $dbi->sql_template; |
|
packaging one directory
|
655 | |
update document
|
656 |
the instance of DBIx::Cutom::SQLTemplate is set to |
657 |
this attribute by default. |
|
packaging one directory
|
658 | |
bind_filter argument is chan...
|
659 |
=head2 filters |
packaging one directory
|
660 | |
update document
|
661 |
Filters |
packaging one directory
|
662 | |
update document
|
663 |
$dbi = $dbi->filters({%filters}); |
version 0.0901
|
664 |
$filters = $dbi->filters; |
665 | ||
update document
|
666 |
encode_utf8 and decode_utf8 is set to this attribute by default. |
version 0.0901
|
667 | |
update document
|
668 |
$encode_utf8 = $dbi->filters->{encode_utf8}; |
669 |
$decode_utf8 = $dbi->filters->{decode_utf8}; |
|
packaging one directory
|
670 | |
rename bind_filter to query_...
|
671 |
=head2 default_query_filter |
packaging one directory
|
672 | |
update document
|
673 |
Default query filter. |
packaging one directory
|
674 | |
update document
|
675 |
$dbi = $dbi->default_query_filter('encode_utf8'); |
rename bind_filter to query_...
|
676 |
$default_query_filter = $dbi->default_query_filter |
packaging one directory
|
677 | |
cleanup
|
678 |
=head2 default_fetch_filter |
packaging one directory
|
679 | |
update document
|
680 |
Fetching filter. |
bind_filter argument is chan...
|
681 | |
update document
|
682 |
$dbi = $dbi->default_fetch_filter('decode_utf8'); |
cleanup
|
683 |
$default_fetch_filter = $dbi->default_fetch_filter; |
bind_filter argument is chan...
|
684 | |
packaging one directory
|
685 |
=head2 result_class |
686 | ||
update document
|
687 |
Result class. |
bind_filter argument is chan...
|
688 | |
version 0.0901
|
689 |
$dbi = $dbi->result_class('DBIx::Custom::Result'); |
packaging one directory
|
690 |
$result_class = $dbi->result_class; |
691 | ||
update document
|
692 |
L<DBIx::Custom::Result> is set to this attribute by default. |
update document
|
693 | |
packaging one directory
|
694 |
=head2 dbh |
695 | ||
update document
|
696 |
Database handle. |
bind_filter argument is chan...
|
697 |
|
version 0.0901
|
698 |
$dbi = $dbi->dbh($dbh); |
699 |
$dbh = $dbi->dbh; |
|
bind_filter argument is chan...
|
700 |
|
update document
|
701 |
=head1 METHODS |
702 | ||
703 |
This class is L<Object::Simple> subclass. |
|
704 |
You can use all methods of L<Object::Simple> |
|
packaging one directory
|
705 | |
removed register_format()
|
706 |
=head2 auto_commit |
707 | ||
update document
|
708 |
Auto commit. |
removed register_format()
|
709 | |
update document
|
710 |
$self = $dbi->auto_commit(1); |
removed register_format()
|
711 |
$auto_commit = $dbi->auto_commit; |
update document
|
712 | |
713 |
This is equal to |
|
714 | ||
715 |
$dbi->dbh->{AutoCommit} = 1; |
|
716 |
$auto_commit = $dbi->dbh->{AutoCommit}; |
|
717 | ||
added commit method
|
718 |
=head2 commit |
719 | ||
720 |
Commit. |
|
721 | ||
722 |
$dbi->commit; |
|
723 | ||
724 |
This is equal to |
|
725 | ||
726 |
$dbi->dbh->commit; |
|
727 | ||
728 |
=head2 rollback |
|
729 | ||
730 |
Rollback. |
|
731 | ||
732 |
$dbi->rollback |
|
733 | ||
734 |
This is equal to |
|
735 | ||
736 |
$dbi->dbh->rollback; |
|
737 | ||
packaging one directory
|
738 |
=head2 connect |
739 | ||
update document
|
740 |
Connect to database. |
741 |
|
|
742 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=books", |
|
743 |
user => 'ken', password => '!LFKD%$&'); |
|
bind_filter argument is chan...
|
744 | |
update document
|
745 |
"AutoCommit" and "RaiseError" option is true, |
746 |
and "PrintError" option is false by dfault. |
|
packaging one directory
|
747 | |
748 |
=head2 disconnect |
|
749 | ||
update document
|
750 |
Disconnect database. |
bind_filter argument is chan...
|
751 | |
packaging one directory
|
752 |
$dbi->disconnect; |
753 | ||
version 0.0901
|
754 |
If database is already disconnected, this method do nothing. |
packaging one directory
|
755 | |
756 |
=head2 reconnect |
|
757 | ||
update document
|
758 |
Reconnect to database. |
bind_filter argument is chan...
|
759 | |
packaging one directory
|
760 |
$dbi->reconnect; |
761 | ||
762 |
=head2 connected |
|
763 | ||
version 0.0901
|
764 |
Check if database is connected. |
bind_filter argument is chan...
|
765 |
|
version 0.0901
|
766 |
$is_connected = $dbi->connected; |
packaging one directory
|
767 |
|
some changed
|
768 |
=head2 register_filter |
packaging one directory
|
769 | |
update document
|
770 |
Resister filter. |
packaging one directory
|
771 |
|
update document
|
772 |
$dbi->register_filter(%filters); |
bind_filter argument is chan...
|
773 |
|
update document
|
774 |
Example. |
version 0.0901
|
775 | |
some changed
|
776 |
$dbi->register_filter( |
packaging one directory
|
777 |
encode_utf8 => sub { |
removed register_format()
|
778 |
my $value = shift; |
779 |
|
|
780 |
require Encode; |
|
781 |
|
|
782 |
return Encode::encode('UTF-8', $value); |
|
packaging one directory
|
783 |
}, |
bind_filter argument is chan...
|
784 |
decode_utf8 => sub { |
removed register_format()
|
785 |
my $value = shift; |
786 |
|
|
787 |
require Encode; |
|
788 |
|
|
789 |
return Encode::decode('UTF-8', $value) |
|
packaging one directory
|
790 |
} |
791 |
); |
|
792 | ||
version 0.0901
|
793 |
=head2 create_query |
bind_filter argument is chan...
|
794 |
|
added commit method
|
795 |
Create the instance of L<DBIx::Custom::Query>. |
796 |
This receive the string written by SQL template. |
|
bind_filter argument is chan...
|
797 | |
version 0.0901
|
798 |
my $query = $dbi->create_query("select * from authors where {= name} and {= age}"); |
bind_filter argument is chan...
|
799 | |
rename query() to execute()
|
800 |
=head2 execute |
packaging one directory
|
801 | |
added commit method
|
802 |
Execute the query or the string written by SQL template. |
bind_filter argument is chan...
|
803 | |
added commit method
|
804 |
$result = $dbi->execute($query, param => $params, filter => {%filter}); |
805 |
$result = $dbi->execute($template, param => $params, filter => {%filter}); |
|
packaging one directory
|
806 | |
added commit method
|
807 |
Example. |
bind_filter argument is chan...
|
808 | |
rename query() to execute()
|
809 |
$result = $dbi->execute("select * from authors where {= name} and {= age}", |
removed register_format()
|
810 |
{name => 'taro', age => 19}); |
packaging one directory
|
811 |
|
812 |
while (my @row = $result->fetch) { |
|
813 |
# do something |
|
814 |
} |
|
815 | ||
added commit method
|
816 |
See also L<DBIx::Custom::SQLTemplate>. |
version 0.0901
|
817 | |
added commit method
|
818 |
Returned value L<DBIx::Custom::Result> instance. |
version 0.0901
|
819 | |
packaging one directory
|
820 |
=head2 insert |
821 | ||
added commit method
|
822 |
Insert row. |
bind_filter argument is chan...
|
823 | |
removed register_format()
|
824 |
$affected = $dbi->insert(table => $table, |
825 |
param => {%param}, |
|
826 |
append => $append, |
|
827 |
filter => {%filter}); |
|
update document
|
828 | |
added commit method
|
829 |
Retruned value is affected rows count. |
packaging one directory
|
830 |
|
added commit method
|
831 |
Example. |
version 0.0901
|
832 | |
removed register_format()
|
833 |
# insert |
834 |
$dbi->insert(table => 'books', |
|
835 |
param => {title => 'Perl', author => 'Taro'}, |
|
836 |
append => "some statement", |
|
837 |
filter => {title => 'encode_utf8'}) |
|
version 0.0901
|
838 | |
packaging one directory
|
839 |
=head2 update |
840 | ||
added commit method
|
841 |
Update rows. |
update document
|
842 | |
removed register_format()
|
843 |
$affected = $dbi->update(table => $table, |
844 |
param => {%params}, |
|
845 |
where => {%where}, |
|
846 |
append => $append, |
|
847 |
filter => {%filter}) |
|
version 0.0901
|
848 | |
added commit method
|
849 |
Retruned value is affected rows count |
update document
|
850 | |
added commit method
|
851 |
Example. |
bind_filter argument is chan...
|
852 | |
removed register_format()
|
853 |
#update |
854 |
$dbi->update(table => 'books', |
|
855 |
param => {title => 'Perl', author => 'Taro'}, |
|
856 |
where => {id => 5}, |
|
857 |
append => "some statement", |
|
added commit method
|
858 |
filter => {title => 'encode_utf8'}); |
version 0.0901
|
859 | |
packaging one directory
|
860 |
=head2 update_all |
861 | ||
added commit method
|
862 |
Update all rows. |
bind_filter argument is chan...
|
863 | |
removed register_format()
|
864 |
$affected = $dbi->update_all(table => $table, |
865 |
param => {%params}, |
|
866 |
filter => {%filter}, |
|
867 |
append => $append); |
|
update document
|
868 | |
added commit method
|
869 |
Retruned value is affected rows count. |
version 0.0901
|
870 | |
added commit method
|
871 |
Example. |
update document
|
872 | |
removed register_format()
|
873 |
# update_all |
874 |
$dbi->update_all(table => 'books', |
|
875 |
param => {author => 'taro'}, |
|
876 |
filter => {author => 'encode_utf8'}); |
|
packaging one directory
|
877 | |
878 |
=head2 delete |
|
879 | ||
added commit method
|
880 |
Delete rows. |
update document
|
881 | |
removed register_format()
|
882 |
$affected = $dbi->delete(table => $table, |
883 |
where => {%where}, |
|
added commit method
|
884 |
append => $append, |
removed register_format()
|
885 |
filter => {%filter}); |
bind_filter argument is chan...
|
886 | |
version 0.0901
|
887 |
Retrun value is affected rows count |
packaging one directory
|
888 |
|
added commit method
|
889 |
Example. |
packaging one directory
|
890 | |
removed register_format()
|
891 |
# delete |
892 |
$dbi->delete(table => 'books', |
|
893 |
where => {id => 5}, |
|
894 |
append => 'some statement', |
|
895 |
filter => {id => 'encode_utf8'); |
|
version 0.0901
|
896 | |
packaging one directory
|
897 |
=head2 delete_all |
898 | ||
added commit method
|
899 |
Delete all rows. |
bind_filter argument is chan...
|
900 | |
removed register_format()
|
901 |
$affected = $dbi->delete_all(table => $table); |
packaging one directory
|
902 | |
added commit method
|
903 |
Retruned value is affected rows count. |
bind_filter argument is chan...
|
904 | |
removed register_format()
|
905 |
Example |
906 |
|
|
907 |
# delete_all |
|
version 0.0901
|
908 |
$dbi->delete_all('books'); |
packaging one directory
|
909 | |
910 |
=head2 select |
|
911 |
|
|
added commit method
|
912 |
Select rows. |
bind_filter argument is chan...
|
913 | |
added commit method
|
914 |
$result = $dbi->select(table => $table, |
915 |
column => [@column], |
|
916 |
where => {%where}, |
|
917 |
append => $append, |
|
918 |
relation => {%relation} |
|
919 |
filter => {%filter}); |
|
update document
|
920 | |
added commit method
|
921 |
Returned value is L<DBIx::Custom::Result> instance. |
update document
|
922 | |
added commit method
|
923 |
Example. |
update document
|
924 | |
added commit method
|
925 |
# select * from books; |
update document
|
926 |
$result = $dbi->select('books'); |
packaging one directory
|
927 |
|
update document
|
928 |
# select * from books where title = 'Perl'; |
929 |
$result = $dbi->select('books', {title => 1}); |
|
930 |
|
|
931 |
# select title, author from books where id = 1 for update; |
|
932 |
$result = $dbi->select( |
|
removed register_format()
|
933 |
table => 'books', |
added commit method
|
934 |
column => ['title', 'author'], |
removed register_format()
|
935 |
where => {id => 1}, |
936 |
appned => 'for update' |
|
update document
|
937 |
); |
938 |
|
|
added commit method
|
939 |
# select books.name as book_name from books, rental |
940 |
# where books.id = rental.book_id; |
|
941 |
my $result = $dbi->select( |
|
942 |
table => ['books', 'rental'], |
|
943 |
column => ['books.name as book_name'] |
|
944 |
relation => {'books.id' => 'rental.book_id'} |
|
update document
|
945 |
); |
946 | ||
packaging one directory
|
947 |
=head1 AUTHOR |
948 | ||
949 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
|
950 | ||
951 |
Github L<http://github.com/yuki-kimoto> |
|
952 | ||
added commit method
|
953 |
I develope this module on L<http://github.com/yuki-kimoto/DBIx-Custom> |
version 0.0901
|
954 | |
packaging one directory
|
955 |
=head1 COPYRIGHT & LICENSE |
956 | ||
957 |
Copyright 2009 Yuki Kimoto, all rights reserved. |
|
958 | ||
959 |
This program is free software; you can redistribute it and/or modify it |
|
960 |
under the same terms as Perl itself. |
|
961 | ||
962 |
=cut |