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