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