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