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