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