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