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