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