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; |
packaging one directory
|
13 | |
catch up with Object::Simple...
|
14 |
__PACKAGE__->attr('dbh'); |
version 0.0901
|
15 | |
catch up with Object::Simple...
|
16 |
__PACKAGE__->class_attr(_query_caches => sub { {} }); |
17 |
__PACKAGE__->class_attr(_query_cache_keys => sub { [] }); |
|
packaging one directory
|
18 | |
catch up with Object::Simple...
|
19 |
__PACKAGE__->class_attr('query_cache_max', default => 50, |
20 |
inherit => 'scalar_copy'); |
|
21 | ||
cleanup
|
22 |
__PACKAGE__->attr([qw/user password data_source/]); |
23 |
__PACKAGE__->attr([qw/database host port/]); |
|
rename bind_filter to query_...
|
24 |
__PACKAGE__->attr([qw/default_query_filter default_fetch_filter options/]); |
packaging one directory
|
25 | |
cleanup
|
26 |
__PACKAGE__->dual_attr([qw/ filters formats/], |
catch up with Object::Simple...
|
27 |
default => sub { {} }, inherit => 'hash_copy'); |
packaging one directory
|
28 | |
cleanup
|
29 |
__PACKAGE__->attr(result_class => 'DBIx::Custom::Result'); |
many many changes
|
30 |
__PACKAGE__->attr(sql_tmpl => sub { DBIx::Custom::SQLTemplate->new }); |
packaging one directory
|
31 | |
many many changes
|
32 |
sub resist_filter { |
packaging one directory
|
33 |
my $invocant = shift; |
34 |
|
|
update document
|
35 |
# Add filter |
catch up with Object::Simple...
|
36 |
my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
37 |
$invocant->filters({%{$invocant->filters}, %$filters}); |
|
38 |
|
|
packaging one directory
|
39 |
return $invocant; |
40 |
} |
|
41 | ||
many many changes
|
42 |
sub resist_format{ |
packaging one directory
|
43 |
my $invocant = shift; |
44 |
|
|
update document
|
45 |
# Add format |
catch up with Object::Simple...
|
46 |
my $formats = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
47 |
$invocant->formats({%{$invocant->formats}, %$formats}); |
|
48 | ||
packaging one directory
|
49 |
return $invocant; |
50 |
} |
|
51 | ||
52 |
sub _auto_commit { |
|
53 |
my $self = shift; |
|
54 |
|
|
update document
|
55 |
# Not connected |
packaging one directory
|
56 |
croak("Not yet connect to database") unless $self->dbh; |
57 |
|
|
58 |
if (@_) { |
|
update document
|
59 |
|
60 |
# Set AutoCommit |
|
packaging one directory
|
61 |
$self->dbh->{AutoCommit} = $_[0]; |
update document
|
62 |
|
packaging one directory
|
63 |
return $self; |
64 |
} |
|
65 |
return $self->dbh->{AutoCommit}; |
|
66 |
} |
|
67 | ||
68 |
sub connect { |
|
69 |
my $self = shift; |
|
update document
|
70 |
|
71 |
# Information |
|
packaging one directory
|
72 |
my $data_source = $self->data_source; |
73 |
my $user = $self->user; |
|
74 |
my $password = $self->password; |
|
version 0.0901
|
75 |
my $options = $self->options; |
packaging one directory
|
76 |
|
update document
|
77 |
# Connect |
packaging one directory
|
78 |
my $dbh = eval{DBI->connect( |
79 |
$data_source, |
|
80 |
$user, |
|
81 |
$password, |
|
82 |
{ |
|
83 |
RaiseError => 1, |
|
84 |
PrintError => 0, |
|
85 |
AutoCommit => 1, |
|
version 0.0901
|
86 |
%{$options || {} } |
packaging one directory
|
87 |
} |
88 |
)}; |
|
89 |
|
|
update document
|
90 |
# Connect error |
packaging one directory
|
91 |
croak $@ if $@; |
92 |
|
|
update document
|
93 |
# Database handle |
packaging one directory
|
94 |
$self->dbh($dbh); |
update document
|
95 |
|
packaging one directory
|
96 |
return $self; |
97 |
} |
|
98 | ||
99 |
sub DESTROY { |
|
100 |
my $self = shift; |
|
update document
|
101 |
|
102 |
# Disconnect |
|
packaging one directory
|
103 |
$self->disconnect if $self->connected; |
104 |
} |
|
105 | ||
update document
|
106 |
sub connected { ref shift->{dbh} eq 'DBI::db' } |
packaging one directory
|
107 | |
108 |
sub disconnect { |
|
109 |
my $self = shift; |
|
update document
|
110 |
|
packaging one directory
|
111 |
if ($self->connected) { |
update document
|
112 |
|
113 |
# Disconnect |
|
packaging one directory
|
114 |
$self->dbh->disconnect; |
115 |
delete $self->{dbh}; |
|
116 |
} |
|
update document
|
117 |
|
118 |
return $self; |
|
packaging one directory
|
119 |
} |
120 | ||
121 |
sub reconnect { |
|
122 |
my $self = shift; |
|
update document
|
123 |
|
124 |
# Reconnect |
|
packaging one directory
|
125 |
$self->disconnect if $self->connected; |
126 |
$self->connect; |
|
update document
|
127 |
|
128 |
return $self; |
|
packaging one directory
|
129 |
} |
130 | ||
131 |
sub prepare { |
|
132 |
my ($self, $sql) = @_; |
|
133 |
|
|
134 |
# Connect if not |
|
135 |
$self->connect unless $self->connected; |
|
136 |
|
|
137 |
# Prepare |
|
138 |
my $sth = eval{$self->dbh->prepare($sql)}; |
|
139 |
|
|
140 |
# Error |
|
141 |
croak("$@<Your SQL>\n$sql") if $@; |
|
142 |
|
|
143 |
return $sth; |
|
144 |
} |
|
145 | ||
146 |
sub do{ |
|
147 |
my ($self, $sql, @bind_values) = @_; |
|
148 |
|
|
149 |
# Connect if not |
|
150 |
$self->connect unless $self->connected; |
|
151 |
|
|
152 |
# Do |
|
version 0.0901
|
153 |
my $affected = eval{$self->dbh->do($sql, @bind_values)}; |
packaging one directory
|
154 |
|
155 |
# Error |
|
156 |
if ($@) { |
|
157 |
my $error = $@; |
|
158 |
require Data::Dumper; |
|
159 |
|
|
160 |
my $bind_value_dump |
|
161 |
= Data::Dumper->Dump([\@bind_values], ['*bind_valuds']); |
|
162 |
|
|
163 |
croak("$error<Your SQL>\n$sql\n<Your bind values>\n$bind_value_dump\n"); |
|
164 |
} |
|
version 0.0901
|
165 |
|
166 |
return $affected; |
|
packaging one directory
|
167 |
} |
168 | ||
169 |
sub create_query { |
|
170 |
my ($self, $template) = @_; |
|
cleanup
|
171 |
|
packaging one directory
|
172 |
my $class = ref $self; |
173 |
|
|
cleanup
|
174 |
if (ref $template eq 'ARRAY') { |
175 |
$template = $template->[1]; |
|
176 |
} |
|
177 |
|
|
packaging one directory
|
178 |
# Create query from SQL template |
version 0.0901
|
179 |
my $sql_tmpl = $self->sql_tmpl; |
packaging one directory
|
180 |
|
181 |
# Try to get cached query |
|
many many changes
|
182 |
my $cached_query = $class->_query_caches->{"$template"}; |
packaging one directory
|
183 |
|
184 |
# Create query |
|
fix timeformat tests
|
185 |
my $query; |
cleanup
|
186 |
if ($cached_query) { |
187 |
$query = DBIx::Custom::Query->new( |
|
188 |
sql => $cached_query->sql, |
|
simplify filtering system
|
189 |
columns => $cached_query->columns |
cleanup
|
190 |
); |
fix timeformat tests
|
191 |
} |
192 |
else { |
|
Simplify key search
|
193 |
$query = eval{$sql_tmpl->create_query($template)}; |
packaging one directory
|
194 |
croak($@) if $@; |
195 |
|
|
many many changes
|
196 |
$class->_add_query_cache("$template", $query); |
packaging one directory
|
197 |
} |
198 |
|
|
199 |
# Connect if not |
|
200 |
$self->connect unless $self->connected; |
|
201 |
|
|
202 |
# Prepare statement handle |
|
203 |
my $sth = $self->prepare($query->{sql}); |
|
204 |
|
|
205 |
# Set statement handle |
|
206 |
$query->sth($sth); |
|
207 |
|
|
208 |
return $query; |
|
209 |
} |
|
210 | ||
version 0.0901
|
211 |
sub query{ |
many many changes
|
212 |
my ($self, $query, $params, $args) = @_; |
packaging one directory
|
213 |
$params ||= {}; |
214 |
|
|
215 |
# First argument is SQL template |
|
Simplify key search
|
216 |
unless (ref $query eq 'DBIx::Custom::Query') { |
217 |
my $template; |
|
218 |
|
|
219 |
if (ref $query eq 'ARRAY') { |
|
many many changes
|
220 |
$template = $query->[0]; |
Simplify key search
|
221 |
} |
222 |
else { $template = $query } |
|
223 |
|
|
many many changes
|
224 |
$query = $self->create_query($template); |
packaging one directory
|
225 |
} |
many many changes
|
226 | |
many change
|
227 |
# Filter |
228 |
my $filter = $args->{filter} || $query->filter || {}; |
|
229 | ||
packaging one directory
|
230 |
# Create bind value |
many many changes
|
231 |
my $bind_values = $self->_build_bind_values($query, $params, $filter); |
packaging one directory
|
232 |
|
233 |
# Execute |
|
version 0.0901
|
234 |
my $sth = $query->sth; |
235 |
my $affected = eval{$sth->execute(@$bind_values)}; |
|
packaging one directory
|
236 |
|
237 |
# Execute error |
|
238 |
if (my $execute_error = $@) { |
|
239 |
require Data::Dumper; |
|
240 |
my $sql = $query->{sql} || ''; |
|
241 |
my $params_dump = Data::Dumper->Dump([$params], ['*params']); |
|
242 |
|
|
243 |
croak("$execute_error" . |
|
244 |
"<Your SQL>\n$sql\n" . |
|
245 |
"<Your parameters>\n$params_dump"); |
|
246 |
} |
|
247 |
|
|
248 |
# Return resultset if select statement is executed |
|
249 |
if ($sth->{NUM_OF_FIELDS}) { |
|
250 |
|
|
251 |
# Get result class |
|
252 |
my $result_class = $self->result_class; |
|
253 |
|
|
254 |
# Create result |
|
255 |
my $result = $result_class->new({ |
|
many many changes
|
256 |
sth => $sth, |
257 |
default_filter => $self->default_fetch_filter, |
|
258 |
filters => $self->filters |
|
packaging one directory
|
259 |
}); |
260 |
return $result; |
|
261 |
} |
|
version 0.0901
|
262 |
return $affected; |
packaging one directory
|
263 |
} |
264 | ||
265 |
sub _build_bind_values { |
|
many many changes
|
266 |
my ($self, $query, $params, $filter) = @_; |
packaging one directory
|
267 |
|
268 |
# binding values |
|
269 |
my @bind_values; |
|
270 |
|
|
Simplify key search
|
271 |
# Build bind values |
simplify filtering system
|
272 |
my $count = {}; |
273 |
foreach my $column (@{$query->columns}) { |
|
packaging one directory
|
274 |
|
Simplify key search
|
275 |
# Value |
simplify filtering system
|
276 |
my $value = ref $params->{$column} |
277 |
? $params->{$column}->[$count->{$column} || 0] |
|
278 |
: $params->{$column}; |
|
packaging one directory
|
279 |
|
Simplify key search
|
280 |
# Filter |
simplify filtering system
|
281 |
$filter ||= {}; |
many many changes
|
282 |
|
simplify filtering system
|
283 |
# Filter name |
284 |
my $fname = $filter->{$column} || $self->default_query_filter || ''; |
|
285 |
|
|
286 |
my $filters = $self->filters; |
|
many change
|
287 |
push @bind_values, $filters->{$fname} |
288 |
? $filters->{$fname}->($value) |
|
289 |
: $value; |
|
simplify filtering system
|
290 |
|
291 |
# Count up |
|
292 |
$count->{$column}++; |
|
cleanup
|
293 |
} |
294 |
|
|
Simplify key search
|
295 |
return \@bind_values; |
cleanup
|
296 |
} |
297 | ||
remove run_transaction().
|
298 |
sub transaction { DBIx::Custom::Transaction->new(dbi => shift) } |
packaging one directory
|
299 | |
remove DBIx::Custom::Transac...
|
300 |
sub run_transaction { |
301 |
my ($self, $transaction) = @_; |
|
302 |
|
|
303 |
# Shorcut |
|
many change
|
304 |
return unless $self; |
remove DBIx::Custom::Transac...
|
305 |
|
306 |
# Check auto commit |
|
307 |
croak("AutoCommit must be true before transaction start") |
|
many change
|
308 |
unless $self->_auto_commit; |
remove DBIx::Custom::Transac...
|
309 |
|
310 |
# Auto commit off |
|
many change
|
311 |
$self->_auto_commit(0); |
remove DBIx::Custom::Transac...
|
312 |
|
313 |
# Run transaction |
|
314 |
eval {$transaction->()}; |
|
315 |
|
|
316 |
# Tranzaction error |
|
317 |
my $transaction_error = $@; |
|
318 |
|
|
319 |
# Tranzaction is failed. |
|
320 |
if ($transaction_error) { |
|
321 |
# Rollback |
|
many change
|
322 |
eval{$self->dbh->rollback}; |
remove DBIx::Custom::Transac...
|
323 |
|
324 |
# Rollback error |
|
325 |
my $rollback_error = $@; |
|
326 |
|
|
327 |
# Auto commit on |
|
many change
|
328 |
$self->_auto_commit(1); |
remove DBIx::Custom::Transac...
|
329 |
|
330 |
if ($rollback_error) { |
|
331 |
# Rollback is failed |
|
332 |
croak("${transaction_error}Rollback is failed : $rollback_error"); |
|
333 |
} |
|
334 |
else { |
|
335 |
# Rollback is success |
|
336 |
croak("${transaction_error}Rollback is success"); |
|
337 |
} |
|
338 |
} |
|
339 |
# Tranzaction is success |
|
340 |
else { |
|
341 |
# Commit |
|
many change
|
342 |
eval{$self->dbh->commit}; |
remove DBIx::Custom::Transac...
|
343 |
my $commit_error = $@; |
344 |
|
|
345 |
# Auto commit on |
|
many change
|
346 |
$self->_auto_commit(1); |
remove DBIx::Custom::Transac...
|
347 |
|
348 |
# Commit is failed |
|
349 |
croak($commit_error) if $commit_error; |
|
350 |
} |
|
351 |
} |
|
352 | ||
version 0.0901
|
353 |
sub create_table { |
354 |
my ($self, $table, @column_definitions) = @_; |
|
355 |
|
|
356 |
# Create table |
|
rename fetch_first to fetch_...
|
357 |
my $sql = "create table $table ("; |
version 0.0901
|
358 |
|
359 |
# Column definitions |
|
360 |
foreach my $column_definition (@column_definitions) { |
|
rename fetch_first to fetch_...
|
361 |
$sql .= "$column_definition,"; |
version 0.0901
|
362 |
} |
rename fetch_first to fetch_...
|
363 |
$sql =~ s/,$//; |
version 0.0901
|
364 |
|
365 |
# End |
|
rename fetch_first to fetch_...
|
366 |
$sql .= ");"; |
version 0.0901
|
367 |
|
368 |
# Do query |
|
369 |
return $self->do($sql); |
|
370 |
} |
|
371 | ||
372 |
sub drop_table { |
|
373 |
my ($self, $table) = @_; |
|
374 |
|
|
375 |
# Drop table |
|
376 |
my $sql = "drop table $table;"; |
|
377 | ||
378 |
# Do query |
|
379 |
return $self->do($sql); |
|
380 |
} |
|
381 | ||
many many changes
|
382 |
our %VALID_INSERT_ARGS = map { $_ => 1 } qw/append filter/; |
cleanup insert
|
383 | |
packaging one directory
|
384 |
sub insert { |
cleanup insert
|
385 |
my ($self, $table, $insert_params, $args) = @_; |
386 |
|
|
387 |
# Table |
|
388 |
$table ||= ''; |
|
389 |
|
|
390 |
# Insert params |
|
391 |
$insert_params ||= {}; |
|
392 |
|
|
393 |
# Arguments |
|
394 |
$args ||= {}; |
|
395 |
|
|
396 |
# Check arguments |
|
397 |
foreach my $name (keys %$args) { |
|
398 |
croak "\"$name\" is invalid name" |
|
399 |
unless $VALID_INSERT_ARGS{$name}; |
|
400 |
} |
|
401 |
|
|
402 |
my $append_statement = $args->{append} || ''; |
|
many many changes
|
403 |
my $filter = $args->{filter}; |
packaging one directory
|
404 |
|
405 |
# Insert keys |
|
406 |
my @insert_keys = keys %$insert_params; |
|
407 |
|
|
408 |
# Not exists insert keys |
|
409 |
croak("Key-value pairs for insert must be specified to 'insert' second argument") |
|
410 |
unless @insert_keys; |
|
411 |
|
|
412 |
# Templte for insert |
|
413 |
my $template = "insert into $table {insert " . join(' ', @insert_keys) . '}'; |
|
insert, update, delete appnd...
|
414 |
$template .= " $append_statement" if $append_statement; |
packaging one directory
|
415 |
|
416 |
# Execute query |
|
many many changes
|
417 |
my $ret_val = $self->query($template, $insert_params, {filter => $filter}); |
packaging one directory
|
418 |
|
419 |
return $ret_val; |
|
420 |
} |
|
421 | ||
cleanup update and update_al...
|
422 |
our %VALID_UPDATE_ARGS |
many many changes
|
423 |
= map { $_ => 1 } qw/where append filter allow_update_all/; |
cleanup update and update_al...
|
424 | |
packaging one directory
|
425 |
sub update { |
simplify filtering system
|
426 |
my ($self, $table, $params, $args) = @_; |
cleanup update and update_al...
|
427 |
|
428 |
# Check arguments |
|
429 |
foreach my $name (keys %$args) { |
|
430 |
croak "\"$name\" is invalid name" |
|
431 |
unless $VALID_UPDATE_ARGS{$name}; |
|
432 |
} |
|
433 |
|
|
434 |
# Arguments |
|
435 |
my $where_params = $args->{where} || {}; |
|
436 |
my $append_statement = $args->{append} || ''; |
|
many many changes
|
437 |
my $filter = $args->{filter}; |
cleanup update and update_al...
|
438 |
my $allow_update_all = $args->{allow_update_all}; |
packaging one directory
|
439 |
|
440 |
# Update keys |
|
simplify filtering system
|
441 |
my @update_keys = keys %$params; |
packaging one directory
|
442 |
|
443 |
# Not exists update kyes |
|
444 |
croak("Key-value pairs for update must be specified to 'update' second argument") |
|
445 |
unless @update_keys; |
|
446 |
|
|
447 |
# Where keys |
|
448 |
my @where_keys = keys %$where_params; |
|
449 |
|
|
450 |
# Not exists where keys |
|
451 |
croak("Key-value pairs for where clause must be specified to 'update' third argument") |
|
cleanup update and update_al...
|
452 |
if !@where_keys && !$allow_update_all; |
packaging one directory
|
453 |
|
454 |
# Update clause |
|
455 |
my $update_clause = '{update ' . join(' ', @update_keys) . '}'; |
|
456 |
|
|
457 |
# Where clause |
|
458 |
my $where_clause = ''; |
|
simplify filtering system
|
459 |
my $new_where = {}; |
many change
|
460 |
|
packaging one directory
|
461 |
if (@where_keys) { |
462 |
$where_clause = 'where '; |
|
463 |
foreach my $where_key (@where_keys) { |
|
simplify filtering system
|
464 |
|
465 |
$where_clause .= "{= $where_key} and "; |
|
packaging one directory
|
466 |
} |
467 |
$where_clause =~ s/ and $//; |
|
468 |
} |
|
469 |
|
|
470 |
# Template for update |
|
471 |
my $template = "update $table $update_clause $where_clause"; |
|
insert, update, delete appnd...
|
472 |
$template .= " $append_statement" if $append_statement; |
packaging one directory
|
473 |
|
474 |
# Rearrange parammeters |
|
simplify filtering system
|
475 |
foreach my $where_key (@where_keys) { |
476 |
|
|
477 |
if (exists $params->{$where_key}) { |
|
478 |
$params->{$where_key} = [$params->{$where_key}] |
|
479 |
unless ref $params->{$where_key} eq 'ARRAY'; |
|
480 |
|
|
481 |
push @{$params->{$where_key}}, $where_params->{$where_key}}; |
|
482 |
} |
|
483 |
} |
|
packaging one directory
|
484 |
|
485 |
# Execute query |
|
many many changes
|
486 |
my $ret_val = $self->query($template, $params, {filter => $filter}); |
packaging one directory
|
487 |
|
488 |
return $ret_val; |
|
489 |
} |
|
490 | ||
491 |
sub update_all { |
|
cleanup update and update_al...
|
492 |
my ($self, $table, $update_params, $args) = @_; |
493 |
|
|
refactoring select
|
494 |
# Allow all update |
cleanup update and update_al...
|
495 |
$args ||= {}; |
496 |
$args->{allow_update_all} = 1; |
|
insert, update, delete appnd...
|
497 |
|
update document
|
498 |
# Update all rows |
cleanup update and update_al...
|
499 |
return $self->update($table, $update_params, $args); |
packaging one directory
|
500 |
} |
501 | ||
refactoring delete and delet...
|
502 |
our %VALID_DELETE_ARGS |
many many changes
|
503 |
= map { $_ => 1 } qw/where append filter allow_delete_all/; |
refactoring delete and delet...
|
504 | |
packaging one directory
|
505 |
sub delete { |
refactoring delete and delet...
|
506 |
my ($self, $table, $args) = @_; |
507 |
|
|
508 |
# Table |
|
509 |
$table ||= ''; |
|
510 | ||
511 |
# Check arguments |
|
512 |
foreach my $name (keys %$args) { |
|
513 |
croak "\"$name\" is invalid name" |
|
514 |
unless $VALID_DELETE_ARGS{$name}; |
|
515 |
} |
|
516 |
|
|
517 |
# Arguments |
|
518 |
my $where_params = $args->{where} || {}; |
|
519 |
my $append_statement = $args->{append}; |
|
many many changes
|
520 |
my $filter = $args->{filter}; |
refactoring delete and delet...
|
521 |
my $allow_delete_all = $args->{allow_delete_all}; |
packaging one directory
|
522 |
|
523 |
# Where keys |
|
524 |
my @where_keys = keys %$where_params; |
|
525 |
|
|
526 |
# Not exists where keys |
|
527 |
croak("Key-value pairs for where clause must be specified to 'delete' second argument") |
|
refactoring delete and delet...
|
528 |
if !@where_keys && !$allow_delete_all; |
packaging one directory
|
529 |
|
530 |
# Where clause |
|
531 |
my $where_clause = ''; |
|
532 |
if (@where_keys) { |
|
533 |
$where_clause = 'where '; |
|
534 |
foreach my $where_key (@where_keys) { |
|
535 |
$where_clause .= "{= $where_key} and "; |
|
536 |
} |
|
537 |
$where_clause =~ s/ and $//; |
|
538 |
} |
|
539 |
|
|
540 |
# Template for delete |
|
541 |
my $template = "delete from $table $where_clause"; |
|
insert, update, delete appnd...
|
542 |
$template .= " $append_statement" if $append_statement; |
packaging one directory
|
543 |
|
544 |
# Execute query |
|
many many changes
|
545 |
my $ret_val = $self->query($template, $where_params, {filter => $filter}); |
packaging one directory
|
546 |
|
547 |
return $ret_val; |
|
548 |
} |
|
549 | ||
550 |
sub delete_all { |
|
refactoring delete and delet...
|
551 |
my ($self, $table, $args) = @_; |
552 |
|
|
refactoring select
|
553 |
# Allow all delete |
refactoring delete and delet...
|
554 |
$args ||= {}; |
555 |
$args->{allow_delete_all} = 1; |
|
insert, update, delete appnd...
|
556 |
|
update document
|
557 |
# Delete all rows |
refactoring delete and delet...
|
558 |
return $self->delete($table, $args); |
packaging one directory
|
559 |
} |
560 | ||
refactoring select
|
561 |
our %VALID_SELECT_ARGS |
many many changes
|
562 |
= map { $_ => 1 } qw/columns where append filter/; |
refactoring select
|
563 | |
packaging one directory
|
564 |
sub select { |
refactoring select
|
565 |
my ($self, $tables, $args) = @_; |
packaging one directory
|
566 |
|
refactoring select
|
567 |
# Table |
568 |
$tables ||= ''; |
|
569 |
$tables = [$tables] unless ref $tables; |
|
packaging one directory
|
570 |
|
refactoring select
|
571 |
# Check arguments |
572 |
foreach my $name (keys %$args) { |
|
573 |
croak "\"$name\" is invalid name" |
|
574 |
unless $VALID_SELECT_ARGS{$name}; |
|
575 |
} |
|
packaging one directory
|
576 |
|
refactoring select
|
577 |
# Arguments |
578 |
my $columns = $args->{columns} || []; |
|
579 |
my $where_params = $args->{where} || {}; |
|
580 |
my $append_statement = $args->{append} || ''; |
|
many many changes
|
581 |
my $filter = $args->{filter}; |
packaging one directory
|
582 |
|
583 |
# SQL template for select statement |
|
584 |
my $template = 'select '; |
|
585 |
|
|
586 |
# Join column clause |
|
587 |
if (@$columns) { |
|
588 |
foreach my $column (@$columns) { |
|
589 |
$template .= "$column, "; |
|
590 |
} |
|
591 |
$template =~ s/, $/ /; |
|
592 |
} |
|
593 |
else { |
|
594 |
$template .= '* '; |
|
595 |
} |
|
596 |
|
|
597 |
# Join table |
|
598 |
$template .= 'from '; |
|
599 |
foreach my $table (@$tables) { |
|
600 |
$template .= "$table, "; |
|
601 |
} |
|
602 |
$template =~ s/, $/ /; |
|
603 |
|
|
604 |
# Where clause keys |
|
605 |
my @where_keys = keys %$where_params; |
|
606 |
|
|
607 |
# Join where clause |
|
608 |
if (@where_keys) { |
|
609 |
$template .= 'where '; |
|
610 |
foreach my $where_key (@where_keys) { |
|
many change
|
611 |
$template .= "{= $column} and "; |
packaging one directory
|
612 |
} |
613 |
} |
|
614 |
$template =~ s/ and $//; |
|
615 |
|
|
616 |
# Append something to last of statement |
|
617 |
if ($append_statement =~ s/^where //) { |
|
618 |
if (@where_keys) { |
|
619 |
$template .= " and $append_statement"; |
|
620 |
} |
|
621 |
else { |
|
622 |
$template .= " where $append_statement"; |
|
623 |
} |
|
624 |
} |
|
625 |
else { |
|
626 |
$template .= " $append_statement"; |
|
627 |
} |
|
628 |
|
|
629 |
# Execute query |
|
simplify filtering system
|
630 |
my $result = $self->query($template, $where_params, {filter => $filter}); |
packaging one directory
|
631 |
|
632 |
return $result; |
|
633 |
} |
|
634 | ||
635 |
sub _add_query_cache { |
|
636 |
my ($class, $template, $query) = @_; |
|
update document
|
637 |
|
638 |
# Query information |
|
packaging one directory
|
639 |
my $query_cache_keys = $class->_query_cache_keys; |
640 |
my $query_caches = $class->_query_caches; |
|
641 |
|
|
update document
|
642 |
# Already cached |
packaging one directory
|
643 |
return $class if $query_caches->{$template}; |
644 |
|
|
update document
|
645 |
# Cache |
packaging one directory
|
646 |
$query_caches->{$template} = $query; |
647 |
push @$query_cache_keys, $template; |
|
648 |
|
|
update document
|
649 |
# Check cache overflow |
packaging one directory
|
650 |
my $overflow = @$query_cache_keys - $class->query_cache_max; |
651 |
for (my $i = 0; $i < $overflow; $i++) { |
|
652 |
my $template = shift @$query_cache_keys; |
|
653 |
delete $query_caches->{$template}; |
|
654 |
} |
|
655 |
|
|
656 |
return $class; |
|
657 |
} |
|
658 | ||
659 |
=head1 NAME |
|
660 | ||
version 0.0901
|
661 |
DBIx::Custom - Customizable DBI |
packaging one directory
|
662 | |
version 0.0901
|
663 |
=head1 VERSION |
packaging one directory
|
664 | |
many change
|
665 |
Version 0.1201 |
catch up with Object::Simple...
|
666 | |
667 |
=cut |
|
668 | ||
many change
|
669 |
our $VERSION = '0.1201'; |
packaging one directory
|
670 | |
cleanup
|
671 |
=head1 STATE |
672 | ||
673 |
This module is not stable. Method name and functionality will be change. |
|
674 | ||
version 0.0901
|
675 |
=head1 SYNOPSYS |
676 |
|
|
677 |
# New |
|
678 |
my $dbi = DBIx::Custom->new(data_source => "dbi:mysql:database=books" |
|
679 |
user => 'ken', password => '!LFKD%$&'); |
|
680 |
|
|
681 |
# Query |
|
682 |
$dbi->query("select title from books"); |
|
683 |
|
|
684 |
# Query with parameters |
|
685 |
$dbi->query("select id from books where {= author} && {like title}", |
|
686 |
{author => 'ken', title => '%Perl%'}); |
|
687 |
|
|
688 |
# Insert |
|
689 |
$dbi->insert('books', {title => 'perl', author => 'Ken'}); |
|
690 |
|
|
691 |
# Update |
|
692 |
$dbi->update('books', {title => 'aaa', author => 'Ken'}, {id => 5}); |
|
693 |
|
|
694 |
# Delete |
|
695 |
$dbi->delete('books', {author => 'Ken'}); |
|
696 |
|
|
697 |
# Select |
|
698 |
$dbi->select('books'); |
|
699 |
$dbi->select('books', {author => 'taro'}); |
|
700 |
$dbi->select('books', [qw/author title/], {author => 'Ken'}); |
|
701 |
$dbi->select('books', [qw/author title/], {author => 'Ken'}, |
|
702 |
'order by id limit 1'); |
|
packaging one directory
|
703 | |
update document
|
704 |
=head1 ATTRIBUTES |
packaging one directory
|
705 | |
706 |
=head2 user |
|
707 | ||
update document
|
708 |
Database user name |
bind_filter argument is chan...
|
709 |
|
version 0.0901
|
710 |
$dbi = $dbi->user('Ken'); |
711 |
$user = $dbi->user; |
|
bind_filter argument is chan...
|
712 |
|
packaging one directory
|
713 |
=head2 password |
714 | ||
update document
|
715 |
Database password |
bind_filter argument is chan...
|
716 |
|
version 0.0901
|
717 |
$dbi = $dbi->password('lkj&le`@s'); |
718 |
$password = $dbi->password; |
|
packaging one directory
|
719 | |
720 |
=head2 data_source |
|
721 | ||
update document
|
722 |
Database data source |
bind_filter argument is chan...
|
723 |
|
version 0.0901
|
724 |
$dbi = $dbi->data_source("dbi:mysql:dbname=$database"); |
725 |
$data_source = $dbi->data_source; |
|
packaging one directory
|
726 |
|
version 0.0901
|
727 |
If you know data source more, See also L<DBI>. |
728 | ||
packaging one directory
|
729 |
=head2 database |
730 | ||
update document
|
731 |
Database name |
bind_filter argument is chan...
|
732 | |
version 0.0901
|
733 |
$dbi = $dbi->database('books'); |
734 |
$database = $dbi->database; |
|
packaging one directory
|
735 | |
add port and host method
|
736 |
=head2 host |
737 | ||
update document
|
738 |
Host name |
bind_filter argument is chan...
|
739 | |
version 0.0901
|
740 |
$dbi = $dbi->host('somehost.com'); |
741 |
$host = $dbi->host; |
|
bind_filter argument is chan...
|
742 | |
version 0.0901
|
743 |
You can also set IP address like '127.03.45.12'. |
add port and host method
|
744 | |
745 |
=head2 port |
|
746 | ||
update document
|
747 |
Port number |
bind_filter argument is chan...
|
748 | |
version 0.0901
|
749 |
$dbi = $dbi->port(1198); |
750 |
$port = $dbi->port; |
|
add port and host method
|
751 | |
version 0.0901
|
752 |
=head2 options |
packaging one directory
|
753 | |
update document
|
754 |
DBI options |
packaging one directory
|
755 | |
version 0.0901
|
756 |
$dbi = $dbi->options({PrintError => 0, RaiseError => 1}); |
757 |
$options = $dbi->options; |
|
packaging one directory
|
758 | |
version 0.0901
|
759 |
=head2 sql_tmpl |
packaging one directory
|
760 | |
many many changes
|
761 |
SQLTemplate object |
packaging one directory
|
762 | |
many many changes
|
763 |
$dbi = $dbi->sql_tmpl(DBIx::Cutom::SQLTemplate->new); |
version 0.0901
|
764 |
$sql_tmpl = $dbi->sql_tmpl; |
packaging one directory
|
765 | |
many many changes
|
766 |
See also L<DBIx::Custom::SQLTemplate>. |
packaging one directory
|
767 | |
bind_filter argument is chan...
|
768 |
=head2 filters |
packaging one directory
|
769 | |
update document
|
770 |
Filters |
packaging one directory
|
771 | |
version 0.0901
|
772 |
$dbi = $dbi->filters({filter1 => sub { }, filter2 => sub {}}); |
773 |
$filters = $dbi->filters; |
|
packaging one directory
|
774 |
|
version 0.0901
|
775 |
This method is generally used to get a filter. |
776 | ||
777 |
$filter = $dbi->filters->{encode_utf8}; |
|
778 | ||
many many changes
|
779 |
If you add filter, use resist_filter method. |
packaging one directory
|
780 | |
bind_filter argument is chan...
|
781 |
=head2 formats |
packaging one directory
|
782 | |
update document
|
783 |
Formats |
packaging one directory
|
784 | |
version 0.0901
|
785 |
$dbi = $dbi->formats({format1 => sub { }, format2 => sub {}}); |
786 |
$formats = $dbi->formats; |
|
bind_filter argument is chan...
|
787 | |
version 0.0901
|
788 |
This method is generally used to get a format. |
bind_filter argument is chan...
|
789 | |
version 0.0901
|
790 |
$filter = $dbi->formats->{datetime}; |
791 | ||
many many changes
|
792 |
If you add format, use resist_format method. |
packaging one directory
|
793 | |
rename bind_filter to query_...
|
794 |
=head2 default_query_filter |
packaging one directory
|
795 | |
update document
|
796 |
Binding filter |
packaging one directory
|
797 | |
rename bind_filter to query_...
|
798 |
$dbi = $dbi->default_query_filter($default_query_filter); |
799 |
$default_query_filter = $dbi->default_query_filter |
|
packaging one directory
|
800 | |
version 0.0901
|
801 |
The following is bind filter sample |
cleanup
|
802 |
|
many many changes
|
803 |
$dbi->resist_filter(encode_utf8 => sub { |
cleanup
|
804 |
my $value = shift; |
bind_filter argument is chan...
|
805 |
|
cleanup
|
806 |
require Encode 'encode_utf8'; |
bind_filter argument is chan...
|
807 |
|
cleanup
|
808 |
return encode_utf8($value); |
bind_filter argument is chan...
|
809 |
}); |
cleanup
|
810 |
|
rename bind_filter to query_...
|
811 |
$dbi->default_query_filter('encode_utf8') |
packaging one directory
|
812 | |
version 0.0901
|
813 |
Bind filter arguemts is |
814 | ||
815 |
1. $value : Value |
|
816 |
2. $key : Key |
|
817 |
3. $dbi : DBIx::Custom object |
|
818 |
4. $infos : {table => $table, column => $column} |
|
819 | ||
cleanup
|
820 |
=head2 default_fetch_filter |
packaging one directory
|
821 | |
update document
|
822 |
Fetching filter |
bind_filter argument is chan...
|
823 | |
cleanup
|
824 |
$dbi = $dbi->default_fetch_filter($default_fetch_filter); |
825 |
$default_fetch_filter = $dbi->default_fetch_filter; |
|
bind_filter argument is chan...
|
826 | |
version 0.0901
|
827 |
The following is fetch filter sample |
packaging one directory
|
828 | |
many many changes
|
829 |
$dbi->resist_filter(decode_utf8 => sub { |
cleanup
|
830 |
my $value = shift; |
bind_filter argument is chan...
|
831 |
|
cleanup
|
832 |
require Encode 'decode_utf8'; |
bind_filter argument is chan...
|
833 |
|
cleanup
|
834 |
return decode_utf8($value); |
bind_filter argument is chan...
|
835 |
}); |
packaging one directory
|
836 | |
cleanup
|
837 |
$dbi->default_fetch_filter('decode_utf8'); |
838 | ||
version 0.0901
|
839 |
Bind filter arguemts is |
840 | ||
841 |
1. $value : Value |
|
842 |
2. $key : Key |
|
843 |
3. $dbi : DBIx::Custom object |
|
844 |
4. $infos : {type => $table, sth => $sth, index => $index} |
|
845 | ||
packaging one directory
|
846 |
=head2 result_class |
847 | ||
update document
|
848 |
Resultset class |
bind_filter argument is chan...
|
849 | |
version 0.0901
|
850 |
$dbi = $dbi->result_class('DBIx::Custom::Result'); |
packaging one directory
|
851 |
$result_class = $dbi->result_class; |
852 | ||
update document
|
853 |
Default is L<DBIx::Custom::Result> |
854 | ||
packaging one directory
|
855 |
=head2 dbh |
856 | ||
update document
|
857 |
Database handle |
bind_filter argument is chan...
|
858 |
|
version 0.0901
|
859 |
$dbi = $dbi->dbh($dbh); |
860 |
$dbh = $dbi->dbh; |
|
bind_filter argument is chan...
|
861 |
|
862 |
=head2 query_cache_max |
|
863 | ||
update document
|
864 |
Query cache max |
bind_filter argument is chan...
|
865 | |
version 0.0901
|
866 |
$class = DBIx::Custom->query_cache_max(50); |
867 |
$query_cache_max = DBIx::Custom->query_cache_max; |
|
868 | ||
869 |
Default value is 50 |
|
870 | ||
update document
|
871 |
=head1 METHODS |
872 | ||
873 |
This class is L<Object::Simple> subclass. |
|
874 |
You can use all methods of L<Object::Simple> |
|
packaging one directory
|
875 | |
876 |
=head2 connect |
|
877 | ||
bind_filter argument is chan...
|
878 |
Connect to database |
879 | ||
packaging one directory
|
880 |
$dbi->connect; |
881 | ||
882 |
=head2 disconnect |
|
883 | ||
bind_filter argument is chan...
|
884 |
Disconnect database |
885 | ||
packaging one directory
|
886 |
$dbi->disconnect; |
887 | ||
version 0.0901
|
888 |
If database is already disconnected, this method do nothing. |
packaging one directory
|
889 | |
890 |
=head2 reconnect |
|
891 | ||
bind_filter argument is chan...
|
892 |
Reconnect to database |
893 | ||
packaging one directory
|
894 |
$dbi->reconnect; |
895 | ||
896 |
=head2 connected |
|
897 | ||
version 0.0901
|
898 |
Check if database is connected. |
bind_filter argument is chan...
|
899 |
|
version 0.0901
|
900 |
$is_connected = $dbi->connected; |
packaging one directory
|
901 |
|
many many changes
|
902 |
=head2 resist_filter |
packaging one directory
|
903 | |
bind_filter argument is chan...
|
904 |
Resist filter |
packaging one directory
|
905 |
|
many many changes
|
906 |
$dbi->resist_filter($fname1 => $filter1, $fname => $filter2); |
bind_filter argument is chan...
|
907 |
|
many many changes
|
908 |
The following is resist_filter sample |
version 0.0901
|
909 | |
many many changes
|
910 |
$dbi->resist_filter( |
packaging one directory
|
911 |
encode_utf8 => sub { |
bind_filter argument is chan...
|
912 |
my ($value, $key, $dbi, $infos) = @_; |
913 |
utf8::upgrade($value) unless Encode::is_utf8($value); |
|
914 |
return encode('UTF-8', $value); |
|
packaging one directory
|
915 |
}, |
bind_filter argument is chan...
|
916 |
decode_utf8 => sub { |
917 |
my ($value, $key, $dbi, $infos) = @_; |
|
918 |
return decode('UTF-8', $value) |
|
packaging one directory
|
919 |
} |
920 |
); |
|
921 | ||
many many changes
|
922 |
=head2 resist_format |
packaging one directory
|
923 | |
version 0.0901
|
924 |
Add format |
bind_filter argument is chan...
|
925 | |
many many changes
|
926 |
$dbi->resist_format($fname1 => $format, $fname2 => $format2); |
bind_filter argument is chan...
|
927 |
|
many many changes
|
928 |
The following is resist_format sample. |
bind_filter argument is chan...
|
929 | |
many many changes
|
930 |
$dbi->resist_format(date => '%Y:%m:%d', datetime => '%Y-%m-%d %H:%M:%S'); |
bind_filter argument is chan...
|
931 | |
version 0.0901
|
932 |
=head2 create_query |
bind_filter argument is chan...
|
933 |
|
version 0.0901
|
934 |
Create Query object parsing SQL template |
bind_filter argument is chan...
|
935 | |
version 0.0901
|
936 |
my $query = $dbi->create_query("select * from authors where {= name} and {= age}"); |
bind_filter argument is chan...
|
937 | |
version 0.0901
|
938 |
$query is <DBIx::Query> object. This is executed by query method as the following |
bind_filter argument is chan...
|
939 | |
version 0.0901
|
940 |
$dbi->query($query, $params); |
bind_filter argument is chan...
|
941 | |
many many changes
|
942 |
If you know SQL template, see also L<DBIx::Custom::SQLTemplate>. |
bind_filter argument is chan...
|
943 | |
version 0.0901
|
944 |
=head2 query |
packaging one directory
|
945 | |
version 0.0901
|
946 |
Query |
bind_filter argument is chan...
|
947 | |
version 0.0901
|
948 |
$result = $dbi->query($template, $params); |
packaging one directory
|
949 | |
version 0.0901
|
950 |
The following is query sample |
bind_filter argument is chan...
|
951 | |
packaging one directory
|
952 |
$result = $dbi->query("select * from authors where {= name} and {= age}", |
953 |
{author => 'taro', age => 19}); |
|
954 |
|
|
955 |
while (my @row = $result->fetch) { |
|
956 |
# do something |
|
957 |
} |
|
958 | ||
many many changes
|
959 |
If you now syntax of template, See also L<DBIx::Custom::SQLTemplate> |
version 0.0901
|
960 | |
961 |
Return value of query method is L<DBIx::Custom::Result> object |
|
962 | ||
963 |
See also L<DBIx::Custom::Result>. |
|
packaging one directory
|
964 | |
remove run_transaction().
|
965 |
=head2 transaction |
packaging one directory
|
966 | |
remove run_transaction().
|
967 |
Get L<DBIx::Custom::Transaction> object, and you run a transaction. |
bind_filter argument is chan...
|
968 | |
remove run_transaction().
|
969 |
$dbi->transaction->run(sub { |
bind_filter argument is chan...
|
970 |
my $dbi = shift; |
971 |
|
|
packaging one directory
|
972 |
# do something |
973 |
}); |
|
974 | ||
975 |
If transaction is success, commit is execute. |
|
976 |
If tranzation is died, rollback is execute. |
|
977 | ||
version 0.0901
|
978 |
=head2 create_table |
979 | ||
980 |
Create table |
|
981 | ||
982 |
$dbi->create_table( |
|
983 |
'books', |
|
984 |
'name char(255)', |
|
985 |
'age int' |
|
986 |
); |
|
987 | ||
988 |
First argument is table name. Rest arguments is column definition. |
|
989 | ||
990 |
=head2 drop_table |
|
991 | ||
992 |
Drop table |
|
993 | ||
994 |
$dbi->drop_table('books'); |
|
995 | ||
packaging one directory
|
996 |
=head2 insert |
997 | ||
update document
|
998 |
Insert row |
bind_filter argument is chan...
|
999 | |
version 0.0901
|
1000 |
$affected = $dbi->insert($table, \%$insert_params); |
1001 |
$affected = $dbi->insert($table, \%$insert_params, $append); |
|
update document
|
1002 | |
version 0.0901
|
1003 |
Retrun value is affected rows count |
packaging one directory
|
1004 |
|
version 0.0901
|
1005 |
The following is insert sample. |
1006 | ||
packaging one directory
|
1007 |
$dbi->insert('books', {title => 'Perl', author => 'Taro'}); |
1008 | ||
version 0.0901
|
1009 |
You can add statement. |
1010 | ||
1011 |
$dbi->insert('books', {title => 'Perl', author => 'Taro'}, "some statement"); |
|
1012 | ||
packaging one directory
|
1013 |
=head2 update |
1014 | ||
update document
|
1015 |
Update rows |
1016 | ||
version 0.0901
|
1017 |
$affected = $dbi->update($table, \%update_params, \%where); |
1018 |
$affected = $dbi->update($table, \%update_params, \%where, $append); |
|
1019 | ||
1020 |
Retrun value is affected rows count |
|
update document
|
1021 | |
version 0.0901
|
1022 |
The following is update sample. |
bind_filter argument is chan...
|
1023 | |
packaging one directory
|
1024 |
$dbi->update('books', {title => 'Perl', author => 'Taro'}, {id => 5}); |
1025 | ||
version 0.0901
|
1026 |
You can add statement. |
1027 | ||
1028 |
$dbi->update('books', {title => 'Perl', author => 'Taro'}, |
|
1029 |
{id => 5}, "some statement"); |
|
1030 | ||
packaging one directory
|
1031 |
=head2 update_all |
1032 | ||
bind_filter argument is chan...
|
1033 |
Update all rows |
1034 | ||
version 0.0901
|
1035 |
$affected = $dbi->update_all($table, \%updat_params); |
update document
|
1036 | |
version 0.0901
|
1037 |
Retrun value is affected rows count |
1038 | ||
1039 |
The following is update_all sample. |
|
update document
|
1040 | |
1041 |
$dbi->update_all('books', {author => 'taro'}); |
|
packaging one directory
|
1042 | |
1043 |
=head2 delete |
|
1044 | ||
update document
|
1045 |
Delete rows |
1046 | ||
version 0.0901
|
1047 |
$affected = $dbi->delete($table, \%where); |
1048 |
$affected = $dbi->delete($table, \%where, $append); |
|
bind_filter argument is chan...
|
1049 | |
version 0.0901
|
1050 |
Retrun value is affected rows count |
packaging one directory
|
1051 |
|
version 0.0901
|
1052 |
The following is delete sample. |
1053 | ||
update document
|
1054 |
$dbi->delete('books', {id => 5}); |
packaging one directory
|
1055 | |
version 0.0901
|
1056 |
You can add statement. |
1057 | ||
1058 |
$dbi->delete('books', {id => 5}, "some statement"); |
|
1059 | ||
packaging one directory
|
1060 |
=head2 delete_all |
1061 | ||
bind_filter argument is chan...
|
1062 |
Delete all rows |
1063 | ||
version 0.0901
|
1064 |
$affected = $dbi->delete_all($table); |
packaging one directory
|
1065 | |
version 0.0901
|
1066 |
Retrun value is affected rows count |
bind_filter argument is chan...
|
1067 | |
version 0.0901
|
1068 |
The following is delete_all sample. |
1069 | ||
1070 |
$dbi->delete_all('books'); |
|
packaging one directory
|
1071 | |
1072 |
=head2 select |
|
1073 |
|
|
update document
|
1074 |
Select rows |
bind_filter argument is chan...
|
1075 | |
version 0.0901
|
1076 |
$resut = $dbi->select( |
packaging one directory
|
1077 |
$table, # must be string or array; |
version 0.0901
|
1078 |
\@$columns, # must be array reference. this can be ommited |
1079 |
\%$where_params, # must be hash reference. this can be ommited |
|
1080 |
$append_statement, # must be string. this can be ommited |
|
1081 |
$query_edit_callback # must be code reference. this can be ommited |
|
packaging one directory
|
1082 |
); |
update document
|
1083 | |
version 0.0901
|
1084 |
$reslt is L<DBIx::Custom::Result> object |
update document
|
1085 | |
1086 |
The following is some select samples |
|
1087 | ||
1088 |
# select * from books; |
|
1089 |
$result = $dbi->select('books'); |
|
packaging one directory
|
1090 |
|
update document
|
1091 |
# select * from books where title = 'Perl'; |
1092 |
$result = $dbi->select('books', {title => 1}); |
|
1093 |
|
|
1094 |
# select title, author from books where id = 1 for update; |
|
1095 |
$result = $dbi->select( |
|
1096 |
'books', # table |
|
1097 |
['title', 'author'], # columns |
|
1098 |
{id => 1}, # where clause |
|
1099 |
'for update', # append statement |
|
1100 |
); |
|
1101 | ||
1102 |
You can join multi tables |
|
1103 |
|
|
1104 |
$result = $dbi->select( |
|
1105 |
['table1', 'table2'], # tables |
|
1106 |
['table1.id as table1_id', 'title'], # columns (alias is ok) |
|
1107 |
{table1.id => 1}, # where clase |
|
1108 |
"where table1.id = table2.id", # join clause (must start 'where') |
|
1109 |
); |
|
1110 | ||
1111 |
You can also edit query |
|
1112 |
|
|
packaging one directory
|
1113 |
$dbi->select( |
update document
|
1114 |
'books', |
1115 |
# column, where clause, append statement, |
|
packaging one directory
|
1116 |
sub { |
1117 |
my $query = shift; |
|
rename bind_filter to query_...
|
1118 |
$query->query_filter(sub { |
packaging one directory
|
1119 |
# ... |
1120 |
}); |
|
1121 |
} |
|
update document
|
1122 |
} |
1123 | ||
version 0.0901
|
1124 |
=head2 prepare |
1125 | ||
1126 |
Prepare statement handle. |
|
1127 | ||
1128 |
$sth = $dbi->prepare('select * from books;'); |
|
1129 | ||
1130 |
This method is same as DBI prepare method. |
|
1131 | ||
1132 |
See also L<DBI>. |
|
1133 | ||
1134 |
=head2 do |
|
1135 | ||
1136 |
Execute SQL |
|
1137 | ||
1138 |
$affected = $dbi->do('insert into books (title, author) values (?, ?)', |
|
1139 |
'Perl', 'taro'); |
|
1140 | ||
1141 |
Retrun value is affected rows count. |
|
1142 | ||
1143 |
This method is same as DBI do method. |
|
1144 | ||
1145 |
See also L<DBI> |
|
1146 | ||
remove DBIx::Custom::Transac...
|
1147 |
=head2 run_transaction |
1148 | ||
1149 | ||
1150 | ||
version 0.0901
|
1151 |
=head1 DBIx::Custom default configuration |
packaging one directory
|
1152 | |
update document
|
1153 |
DBIx::Custom have DBI object. |
packaging one directory
|
1154 |
This module is work well in the following DBI condition. |
1155 | ||
1156 |
1. AutoCommit is true |
|
1157 |
2. RaiseError is true |
|
1158 | ||
1159 |
By default, Both AutoCommit and RaiseError is true. |
|
1160 |
You must not change these mode not to damage your data. |
|
1161 | ||
1162 |
If you change these mode, |
|
1163 |
you cannot get correct error message, |
|
1164 |
or run_transaction may fail. |
|
1165 | ||
version 0.0901
|
1166 |
=head1 Inheritance of DBIx::Custom |
1167 | ||
1168 |
DBIx::Custom is customizable DBI. |
|
1169 |
You can inherit DBIx::Custom and custumize attributes. |
|
1170 | ||
1171 |
package DBIx::Custom::Yours; |
|
1172 |
use base DBIx::Custom; |
|
1173 |
|
|
1174 |
my $class = __PACKAGE__; |
|
1175 |
|
|
1176 |
$class->user('your_name'); |
|
1177 |
$class->password('your_password'); |
|
1178 | ||
packaging one directory
|
1179 |
=head1 AUTHOR |
1180 | ||
1181 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
|
1182 | ||
1183 |
Github L<http://github.com/yuki-kimoto> |
|
1184 | ||
version 0.0901
|
1185 |
I develope this module L<http://github.com/yuki-kimoto/DBIx-Custom> |
1186 | ||
packaging one directory
|
1187 |
=head1 COPYRIGHT & LICENSE |
1188 | ||
1189 |
Copyright 2009 Yuki Kimoto, all rights reserved. |
|
1190 | ||
1191 |
This program is free software; you can redistribute it and/or modify it |
|
1192 |
under the same terms as Perl itself. |
|
1193 | ||
1194 |
=cut |