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; |
update document
|
13 |
use Encode qw/encode_utf8 decode_utf8/; |
packaging one directory
|
14 | |
catch up with Object::Simple...
|
15 |
__PACKAGE__->attr('dbh'); |
version 0.0901
|
16 | |
cleanup
|
17 |
__PACKAGE__->attr([qw/user password data_source/]); |
update document
|
18 |
__PACKAGE__->attr([qw/default_query_filter default_fetch_filter/]); |
packaging one directory
|
19 | |
removed register_format()
|
20 |
__PACKAGE__->dual_attr('filters', default => sub { {} }, |
21 |
inherit => 'hash_copy'); |
|
22 |
__PACKAGE__->register_filter( |
|
update document
|
23 |
encode_utf8 => sub { encode_utf8($_[0]) }, |
24 |
decode_utf8 => sub { decode_utf8($_[0]) } |
|
removed register_format()
|
25 |
); |
packaging one directory
|
26 | |
cleanup
|
27 |
__PACKAGE__->attr(result_class => 'DBIx::Custom::Result'); |
removed register_format()
|
28 |
__PACKAGE__->attr(sql_template => sub { DBIx::Custom::SQLTemplate->new }); |
29 | ||
some changed
|
30 |
sub register_filter { |
packaging one directory
|
31 |
my $invocant = shift; |
32 |
|
|
update document
|
33 |
# Add filter |
catch up with Object::Simple...
|
34 |
my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
35 |
$invocant->filters({%{$invocant->filters}, %$filters}); |
|
36 |
|
|
packaging one directory
|
37 |
return $invocant; |
38 |
} |
|
39 | ||
removed register_format()
|
40 |
sub auto_commit { |
packaging one directory
|
41 |
my $self = shift; |
42 |
|
|
update document
|
43 |
# Not connected |
removed register_format()
|
44 |
croak("Not yet connect to database") unless $self->connected; |
packaging one directory
|
45 |
|
46 |
if (@_) { |
|
update document
|
47 |
|
48 |
# Set AutoCommit |
|
packaging one directory
|
49 |
$self->dbh->{AutoCommit} = $_[0]; |
update document
|
50 |
|
packaging one directory
|
51 |
return $self; |
52 |
} |
|
53 |
return $self->dbh->{AutoCommit}; |
|
54 |
} |
|
55 | ||
56 |
sub connect { |
|
removed register_format()
|
57 |
my $proto = shift; |
58 |
|
|
59 |
# Create |
|
60 |
my $self = ref $proto ? $proto : $proto->new(@_); |
|
update document
|
61 |
|
62 |
# Information |
|
packaging one directory
|
63 |
my $data_source = $self->data_source; |
64 |
my $user = $self->user; |
|
65 |
my $password = $self->password; |
|
66 |
|
|
update document
|
67 |
# Connect |
packaging one directory
|
68 |
my $dbh = eval{DBI->connect( |
69 |
$data_source, |
|
70 |
$user, |
|
71 |
$password, |
|
72 |
{ |
|
73 |
RaiseError => 1, |
|
74 |
PrintError => 0, |
|
75 |
AutoCommit => 1, |
|
76 |
} |
|
77 |
)}; |
|
78 |
|
|
update document
|
79 |
# Connect error |
packaging one directory
|
80 |
croak $@ if $@; |
81 |
|
|
update document
|
82 |
# Database handle |
packaging one directory
|
83 |
$self->dbh($dbh); |
update document
|
84 |
|
packaging one directory
|
85 |
return $self; |
86 |
} |
|
87 | ||
88 |
sub DESTROY { |
|
89 |
my $self = shift; |
|
update document
|
90 |
|
91 |
# Disconnect |
|
packaging one directory
|
92 |
$self->disconnect if $self->connected; |
93 |
} |
|
94 | ||
update document
|
95 |
sub connected { ref shift->{dbh} eq 'DBI::db' } |
packaging one directory
|
96 | |
97 |
sub disconnect { |
|
98 |
my $self = shift; |
|
update document
|
99 |
|
packaging one directory
|
100 |
if ($self->connected) { |
update document
|
101 |
|
102 |
# Disconnect |
|
packaging one directory
|
103 |
$self->dbh->disconnect; |
104 |
delete $self->{dbh}; |
|
105 |
} |
|
update document
|
106 |
|
107 |
return $self; |
|
packaging one directory
|
108 |
} |
109 | ||
110 |
sub reconnect { |
|
111 |
my $self = shift; |
|
update document
|
112 |
|
113 |
# Reconnect |
|
packaging one directory
|
114 |
$self->disconnect if $self->connected; |
115 |
$self->connect; |
|
update document
|
116 |
|
117 |
return $self; |
|
packaging one directory
|
118 |
} |
119 | ||
120 |
sub create_query { |
|
121 |
my ($self, $template) = @_; |
|
cleanup
|
122 |
|
packaging one directory
|
123 |
# Create query from SQL template |
removed register_format()
|
124 |
my $sql_template = $self->sql_template; |
packaging one directory
|
125 |
|
update document
|
126 |
# Get cached query |
127 |
my $cache = $self->{_cache}->{"$template"}; |
|
packaging one directory
|
128 |
|
129 |
# Create query |
|
fix timeformat tests
|
130 |
my $query; |
update document
|
131 |
if ($cache) { |
cleanup
|
132 |
$query = DBIx::Custom::Query->new( |
update document
|
133 |
sql => $cache->sql, |
134 |
columns => $cache->columns |
|
cleanup
|
135 |
); |
fix timeformat tests
|
136 |
} |
137 |
else { |
|
removed register_format()
|
138 |
$query = eval{$sql_template->create_query($template)}; |
packaging one directory
|
139 |
croak($@) if $@; |
140 |
|
|
update document
|
141 |
$self->{_cache}->{$template} = $query |
142 |
unless $self->{_cache}; |
|
packaging one directory
|
143 |
} |
144 |
|
|
145 |
# Prepare statement handle |
|
add all tests
|
146 |
my $sth = $self->dbh->prepare($query->{sql}); |
packaging one directory
|
147 |
|
148 |
# Set statement handle |
|
149 |
$query->sth($sth); |
|
150 |
|
|
151 |
return $query; |
|
152 |
} |
|
153 | ||
removed register_format()
|
154 |
our %VALID_EXECUTE_ARGS = map { $_ => 1 } qw/param filter/; |
155 | ||
rename query() to execute()
|
156 |
sub execute{ |
removed register_format()
|
157 |
my $self = shift; |
158 |
my $query = shift; |
|
159 |
|
|
160 |
# Arguments |
|
161 |
my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
162 |
|
|
163 |
# Check arguments |
|
164 |
foreach my $name (keys %$args) { |
|
165 |
croak "\"$name\" is invalid name" |
|
166 |
unless $VALID_EXECUTE_ARGS{$name}; |
|
167 |
} |
|
168 |
|
|
169 |
my $params = $args->{param} || {}; |
|
packaging one directory
|
170 |
|
171 |
# First argument is SQL template |
|
Simplify key search
|
172 |
unless (ref $query eq 'DBIx::Custom::Query') { |
173 |
my $template; |
|
174 |
|
|
175 |
if (ref $query eq 'ARRAY') { |
|
many many changes
|
176 |
$template = $query->[0]; |
Simplify key search
|
177 |
} |
178 |
else { $template = $query } |
|
179 |
|
|
many many changes
|
180 |
$query = $self->create_query($template); |
packaging one directory
|
181 |
} |
removed register_format()
|
182 |
|
many change
|
183 |
my $filter = $args->{filter} || $query->filter || {}; |
removed register_format()
|
184 |
|
packaging one directory
|
185 |
# Create bind value |
many many changes
|
186 |
my $bind_values = $self->_build_bind_values($query, $params, $filter); |
packaging one directory
|
187 |
|
188 |
# Execute |
|
version 0.0901
|
189 |
my $sth = $query->sth; |
190 |
my $affected = eval{$sth->execute(@$bind_values)}; |
|
packaging one directory
|
191 |
|
192 |
# Execute error |
|
193 |
if (my $execute_error = $@) { |
|
194 |
require Data::Dumper; |
|
195 |
my $sql = $query->{sql} || ''; |
|
196 |
my $params_dump = Data::Dumper->Dump([$params], ['*params']); |
|
197 |
|
|
198 |
croak("$execute_error" . |
|
199 |
"<Your SQL>\n$sql\n" . |
|
200 |
"<Your parameters>\n$params_dump"); |
|
201 |
} |
|
202 |
|
|
203 |
# Return resultset if select statement is executed |
|
204 |
if ($sth->{NUM_OF_FIELDS}) { |
|
205 |
|
|
206 |
# Get result class |
|
207 |
my $result_class = $self->result_class; |
|
208 |
|
|
209 |
# Create result |
|
210 |
my $result = $result_class->new({ |
|
many many changes
|
211 |
sth => $sth, |
212 |
default_filter => $self->default_fetch_filter, |
|
213 |
filters => $self->filters |
|
packaging one directory
|
214 |
}); |
215 |
return $result; |
|
216 |
} |
|
version 0.0901
|
217 |
return $affected; |
packaging one directory
|
218 |
} |
219 | ||
220 |
sub _build_bind_values { |
|
many many changes
|
221 |
my ($self, $query, $params, $filter) = @_; |
packaging one directory
|
222 |
|
223 |
# binding values |
|
224 |
my @bind_values; |
|
225 |
|
|
Simplify key search
|
226 |
# Build bind values |
simplify filtering system
|
227 |
my $count = {}; |
228 |
foreach my $column (@{$query->columns}) { |
|
packaging one directory
|
229 |
|
add query filter error check
|
230 |
croak "\"$column\" is not exists in params" |
231 |
unless exists $params->{$column}; |
|
232 |
|
|
Simplify key search
|
233 |
# Value |
add query filter error check
|
234 |
my $value = ref $params->{$column} eq 'ARRAY' |
simplify filtering system
|
235 |
? $params->{$column}->[$count->{$column} || 0] |
236 |
: $params->{$column}; |
|
packaging one directory
|
237 |
|
Simplify key search
|
238 |
# Filter |
simplify filtering system
|
239 |
$filter ||= {}; |
many many changes
|
240 |
|
simplify filtering system
|
241 |
# Filter name |
242 |
my $fname = $filter->{$column} || $self->default_query_filter || ''; |
|
243 |
|
|
add query filter error check
|
244 |
my $filter_func; |
245 |
if ($fname) { |
|
246 |
|
|
247 |
if (ref $fname eq 'CODE') { |
|
248 |
$filter_func = $fname; |
|
249 |
} |
|
250 |
else { |
|
251 |
my $filters = $self->filters; |
|
252 |
croak "Not exists filter \"$fname\"" unless exists $filters->{$fname}; |
|
253 |
$filter_func = $filters->{$fname}; |
|
254 |
} |
|
255 |
} |
|
256 |
|
|
257 |
push @bind_values, $filter_func |
|
258 |
? $filter_func->($value) |
|
many change
|
259 |
: $value; |
simplify filtering system
|
260 |
|
261 |
# Count up |
|
262 |
$count->{$column}++; |
|
cleanup
|
263 |
} |
264 |
|
|
Simplify key search
|
265 |
return \@bind_values; |
cleanup
|
266 |
} |
267 | ||
removed register_format()
|
268 |
our %VALID_INSERT_ARGS = map { $_ => 1 } qw/table param append filter/; |
cleanup insert
|
269 | |
packaging one directory
|
270 |
sub insert { |
removed register_format()
|
271 |
my $self = shift; |
cleanup insert
|
272 |
|
273 |
# Arguments |
|
removed register_format()
|
274 |
my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
275 | ||
cleanup insert
|
276 |
# Check arguments |
277 |
foreach my $name (keys %$args) { |
|
278 |
croak "\"$name\" is invalid name" |
|
279 |
unless $VALID_INSERT_ARGS{$name}; |
|
280 |
} |
|
281 |
|
|
removed register_format()
|
282 |
# Arguments |
283 |
my $table = $args->{table} || ''; |
|
284 |
my $param = $args->{param} || {}; |
|
285 |
my $append = $args->{append} || ''; |
|
286 |
my $filter = $args->{filter}; |
|
packaging one directory
|
287 |
|
288 |
# Insert keys |
|
removed register_format()
|
289 |
my @insert_keys = keys %$param; |
packaging one directory
|
290 |
|
291 |
# Not exists insert keys |
|
292 |
croak("Key-value pairs for insert must be specified to 'insert' second argument") |
|
293 |
unless @insert_keys; |
|
294 |
|
|
295 |
# Templte for insert |
|
296 |
my $template = "insert into $table {insert " . join(' ', @insert_keys) . '}'; |
|
removed register_format()
|
297 |
$template .= " $append" if $append; |
packaging one directory
|
298 |
|
299 |
# Execute query |
|
removed register_format()
|
300 |
my $ret_val = $self->execute($template, param => $param, |
301 |
filter => $filter); |
|
packaging one directory
|
302 |
|
303 |
return $ret_val; |
|
304 |
} |
|
305 | ||
cleanup update and update_al...
|
306 |
our %VALID_UPDATE_ARGS |
removed register_format()
|
307 |
= map { $_ => 1 } qw/table param where append filter allow_update_all/; |
cleanup update and update_al...
|
308 | |
packaging one directory
|
309 |
sub update { |
removed register_format()
|
310 |
my $self = shift; |
311 | ||
312 |
my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
cleanup update and update_al...
|
313 |
|
314 |
# Check arguments |
|
315 |
foreach my $name (keys %$args) { |
|
316 |
croak "\"$name\" is invalid name" |
|
317 |
unless $VALID_UPDATE_ARGS{$name}; |
|
318 |
} |
|
319 |
|
|
320 |
# Arguments |
|
removed register_format()
|
321 |
my $table = $args->{table} || ''; |
322 |
my $param = $args->{param} || {}; |
|
323 |
my $where = $args->{where} || {}; |
|
cleanup update and update_al...
|
324 |
my $append_statement = $args->{append} || ''; |
many many changes
|
325 |
my $filter = $args->{filter}; |
cleanup update and update_al...
|
326 |
my $allow_update_all = $args->{allow_update_all}; |
packaging one directory
|
327 |
|
328 |
# Update keys |
|
removed register_format()
|
329 |
my @update_keys = keys %$param; |
packaging one directory
|
330 |
|
331 |
# Not exists update kyes |
|
332 |
croak("Key-value pairs for update must be specified to 'update' second argument") |
|
333 |
unless @update_keys; |
|
334 |
|
|
335 |
# Where keys |
|
removed register_format()
|
336 |
my @where_keys = keys %$where; |
packaging one directory
|
337 |
|
338 |
# Not exists where keys |
|
339 |
croak("Key-value pairs for where clause must be specified to 'update' third argument") |
|
cleanup update and update_al...
|
340 |
if !@where_keys && !$allow_update_all; |
packaging one directory
|
341 |
|
342 |
# Update clause |
|
343 |
my $update_clause = '{update ' . join(' ', @update_keys) . '}'; |
|
344 |
|
|
345 |
# Where clause |
|
346 |
my $where_clause = ''; |
|
simplify filtering system
|
347 |
my $new_where = {}; |
many change
|
348 |
|
packaging one directory
|
349 |
if (@where_keys) { |
350 |
$where_clause = 'where '; |
|
351 |
foreach my $where_key (@where_keys) { |
|
simplify filtering system
|
352 |
|
353 |
$where_clause .= "{= $where_key} and "; |
|
packaging one directory
|
354 |
} |
355 |
$where_clause =~ s/ and $//; |
|
356 |
} |
|
357 |
|
|
358 |
# Template for update |
|
359 |
my $template = "update $table $update_clause $where_clause"; |
|
insert, update, delete appnd...
|
360 |
$template .= " $append_statement" if $append_statement; |
packaging one directory
|
361 |
|
362 |
# Rearrange parammeters |
|
removed register_format()
|
363 |
foreach my $wkey (@where_keys) { |
simplify filtering system
|
364 |
|
removed register_format()
|
365 |
if (exists $param->{$wkey}) { |
366 |
$param->{$wkey} = [$param->{$wkey}] |
|
367 |
unless ref $param->{$wkey} eq 'ARRAY'; |
|
simplify filtering system
|
368 |
|
removed register_format()
|
369 |
push @{$param->{$wkey}}, $where->{$wkey}; |
simplify filtering system
|
370 |
} |
add tests
|
371 |
else { |
removed register_format()
|
372 |
$param->{$wkey} = $where->{$wkey}; |
add tests
|
373 |
} |
simplify filtering system
|
374 |
} |
packaging one directory
|
375 |
|
376 |
# Execute query |
|
removed register_format()
|
377 |
my $ret_val = $self->execute($template, param => $param, |
378 |
filter => $filter); |
|
packaging one directory
|
379 |
|
380 |
return $ret_val; |
|
381 |
} |
|
382 | ||
383 |
sub update_all { |
|
removed register_format()
|
384 |
my $self = shift;; |
cleanup update and update_al...
|
385 |
|
removed register_format()
|
386 |
# Arguments |
387 |
my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
388 |
|
|
refactoring select
|
389 |
# Allow all update |
cleanup update and update_al...
|
390 |
$args->{allow_update_all} = 1; |
insert, update, delete appnd...
|
391 |
|
update document
|
392 |
# Update all rows |
removed register_format()
|
393 |
return $self->update($args); |
packaging one directory
|
394 |
} |
395 | ||
refactoring delete and delet...
|
396 |
our %VALID_DELETE_ARGS |
removed register_format()
|
397 |
= map { $_ => 1 } qw/table where append filter allow_delete_all/; |
refactoring delete and delet...
|
398 | |
packaging one directory
|
399 |
sub delete { |
removed register_format()
|
400 |
my $self = shift; |
401 |
|
|
402 |
# Arguments |
|
403 |
my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
refactoring delete and delet...
|
404 |
|
405 |
# Check arguments |
|
406 |
foreach my $name (keys %$args) { |
|
407 |
croak "\"$name\" is invalid name" |
|
408 |
unless $VALID_DELETE_ARGS{$name}; |
|
409 |
} |
|
410 |
|
|
411 |
# Arguments |
|
removed register_format()
|
412 |
my $table = $args->{table} || ''; |
413 |
my $where = $args->{where} || {}; |
|
refactoring delete and delet...
|
414 |
my $append_statement = $args->{append}; |
removed register_format()
|
415 |
my $filter = $args->{filter}; |
refactoring delete and delet...
|
416 |
my $allow_delete_all = $args->{allow_delete_all}; |
packaging one directory
|
417 |
|
418 |
# Where keys |
|
removed register_format()
|
419 |
my @where_keys = keys %$where; |
packaging one directory
|
420 |
|
421 |
# Not exists where keys |
|
422 |
croak("Key-value pairs for where clause must be specified to 'delete' second argument") |
|
refactoring delete and delet...
|
423 |
if !@where_keys && !$allow_delete_all; |
packaging one directory
|
424 |
|
425 |
# Where clause |
|
426 |
my $where_clause = ''; |
|
427 |
if (@where_keys) { |
|
428 |
$where_clause = 'where '; |
|
removed register_format()
|
429 |
foreach my $wkey (@where_keys) { |
430 |
$where_clause .= "{= $wkey} and "; |
|
packaging one directory
|
431 |
} |
432 |
$where_clause =~ s/ and $//; |
|
433 |
} |
|
434 |
|
|
435 |
# Template for delete |
|
436 |
my $template = "delete from $table $where_clause"; |
|
insert, update, delete appnd...
|
437 |
$template .= " $append_statement" if $append_statement; |
packaging one directory
|
438 |
|
439 |
# Execute query |
|
removed register_format()
|
440 |
my $ret_val = $self->execute($template, param => $where, |
441 |
filter => $filter); |
|
packaging one directory
|
442 |
|
443 |
return $ret_val; |
|
444 |
} |
|
445 | ||
446 |
sub delete_all { |
|
removed register_format()
|
447 |
my $self = shift; |
448 |
|
|
449 |
# Arguments |
|
450 |
my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
refactoring delete and delet...
|
451 |
|
refactoring select
|
452 |
# Allow all delete |
refactoring delete and delet...
|
453 |
$args->{allow_delete_all} = 1; |
insert, update, delete appnd...
|
454 |
|
update document
|
455 |
# Delete all rows |
removed register_format()
|
456 |
return $self->delete($args); |
packaging one directory
|
457 |
} |
458 | ||
refactoring select
|
459 |
our %VALID_SELECT_ARGS |
removed register_format()
|
460 |
= map { $_ => 1 } qw/table column where append filter/; |
refactoring select
|
461 | |
packaging one directory
|
462 |
sub select { |
removed register_format()
|
463 |
my $self = shift;; |
packaging one directory
|
464 |
|
removed register_format()
|
465 |
my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
packaging one directory
|
466 |
|
refactoring select
|
467 |
# Check arguments |
468 |
foreach my $name (keys %$args) { |
|
469 |
croak "\"$name\" is invalid name" |
|
470 |
unless $VALID_SELECT_ARGS{$name}; |
|
471 |
} |
|
packaging one directory
|
472 |
|
refactoring select
|
473 |
# Arguments |
removed register_format()
|
474 |
my $tables = $args->{table} || []; |
475 |
$tables = [$tables] unless ref $tables eq 'ARRAY'; |
|
476 |
my $columns = $args->{column} || []; |
|
refactoring select
|
477 |
my $where_params = $args->{where} || {}; |
478 |
my $append_statement = $args->{append} || ''; |
|
many many changes
|
479 |
my $filter = $args->{filter}; |
packaging one directory
|
480 |
|
481 |
# SQL template for select statement |
|
482 |
my $template = 'select '; |
|
483 |
|
|
484 |
# Join column clause |
|
485 |
if (@$columns) { |
|
486 |
foreach my $column (@$columns) { |
|
487 |
$template .= "$column, "; |
|
488 |
} |
|
489 |
$template =~ s/, $/ /; |
|
490 |
} |
|
491 |
else { |
|
492 |
$template .= '* '; |
|
493 |
} |
|
494 |
|
|
495 |
# Join table |
|
496 |
$template .= 'from '; |
|
497 |
foreach my $table (@$tables) { |
|
498 |
$template .= "$table, "; |
|
499 |
} |
|
500 |
$template =~ s/, $/ /; |
|
501 |
|
|
502 |
# Where clause keys |
|
503 |
my @where_keys = keys %$where_params; |
|
504 |
|
|
505 |
# Join where clause |
|
506 |
if (@where_keys) { |
|
507 |
$template .= 'where '; |
|
508 |
foreach my $where_key (@where_keys) { |
|
compile success
|
509 |
$template .= "{= $where_key} and "; |
packaging one directory
|
510 |
} |
511 |
} |
|
512 |
$template =~ s/ and $//; |
|
513 |
|
|
514 |
# Append something to last of statement |
|
515 |
if ($append_statement =~ s/^where //) { |
|
516 |
if (@where_keys) { |
|
517 |
$template .= " and $append_statement"; |
|
518 |
} |
|
519 |
else { |
|
520 |
$template .= " where $append_statement"; |
|
521 |
} |
|
522 |
} |
|
523 |
else { |
|
524 |
$template .= " $append_statement"; |
|
525 |
} |
|
526 |
|
|
527 |
# Execute query |
|
removed register_format()
|
528 |
my $result = $self->execute($template, param => $where_params, |
529 |
filter => $filter); |
|
packaging one directory
|
530 |
|
531 |
return $result; |
|
532 |
} |
|
533 | ||
534 |
=head1 NAME |
|
535 | ||
update document
|
536 |
DBIx::Custom - DBI with hash parameter binding and filtering system |
packaging one directory
|
537 | |
version 0.0901
|
538 |
=head1 VERSION |
packaging one directory
|
539 | |
update document
|
540 |
Version 0.1502 |
catch up with Object::Simple...
|
541 | |
542 |
=cut |
|
543 | ||
update document
|
544 |
our $VERSION = '0.1502'; |
some changed
|
545 |
$VERSION = eval $VERSION; |
packaging one directory
|
546 | |
cleanup
|
547 |
=head1 STATE |
548 | ||
549 |
This module is not stable. Method name and functionality will be change. |
|
550 | ||
version 0.0901
|
551 |
=head1 SYNOPSYS |
552 |
|
|
removed register_format()
|
553 |
# Connect |
554 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=books", |
|
555 |
user => 'ken', password => '!LFKD%$&'); |
|
some changed
|
556 |
|
version 0.0901
|
557 |
# Insert |
removed register_format()
|
558 |
$dbi->insert(table => 'books', |
update document
|
559 |
param => {title => 'perl', author => 'Ken'}, |
removed register_format()
|
560 |
filter => {title => 'encode_utf8'}); |
version 0.0901
|
561 |
|
562 |
# Update |
|
removed register_format()
|
563 |
$dbi->update(table => 'books', |
564 |
param => {title => 'aaa', author => 'Ken'}, |
|
update document
|
565 |
where => {id => 5}, |
566 |
filter => {title => 'encode_utf8'}); |
|
removed register_format()
|
567 |
|
568 |
# Update all |
|
569 |
$dbi->update_all(table => 'books', |
|
update document
|
570 |
param => {title => 'aaa'}, |
removed register_format()
|
571 |
filter => {title => 'encode_utf8'}); |
version 0.0901
|
572 |
|
573 |
# Delete |
|
removed register_format()
|
574 |
$dbi->delete(table => 'books', |
update document
|
575 |
where => {author => 'Ken'}, |
removed register_format()
|
576 |
filter => {title => 'encode_utf8'}); |
version 0.0901
|
577 |
|
removed register_format()
|
578 |
# Delete all |
579 |
$dbi->delete_all(table => 'books'); |
|
renamed fetch_rows to fetch_...
|
580 |
|
removed register_format()
|
581 |
# Select |
582 |
my $result = $dbi->select(table => 'books'); |
|
renamed fetch_rows to fetch_...
|
583 |
|
removed register_format()
|
584 |
# Select(more complex) |
renamed fetch_rows to fetch_...
|
585 |
my $result = $dbi->select( |
update document
|
586 |
table => 'books', |
587 |
column => [qw/author title/], |
|
588 |
where => {author => 'Ken'}, |
|
589 |
append => 'order by id limit 1', |
|
590 |
filter => {tilte => 'encode_utf8'} |
|
renamed fetch_rows to fetch_...
|
591 |
); |
packaging one directory
|
592 | |
removed register_format()
|
593 |
# Execute SQL |
594 |
$dbi->execute("select title from books"); |
|
595 |
|
|
596 |
# Execute SQL with parameters and filter |
|
597 |
$dbi->execute("select id from books where {= author} && {like title}", |
|
598 |
param => {author => 'ken', title => '%Perl%'}, |
|
599 |
filter => {tilte => 'encode_utf8'}); |
|
600 |
|
|
601 |
# Default filter |
|
602 |
$dbi->default_query_filter('encode_utf8'); |
|
603 |
$dbi->default_fetch_filter('decode_utf8'); |
|
604 |
|
|
605 |
# Fetch |
|
606 |
while (my $row = $result->fetch) { |
|
607 |
# ... |
|
608 |
} |
|
609 |
|
|
610 |
# Fetch hash |
|
611 |
while (my $row = $result->fetch_hash) { |
|
612 |
|
|
613 |
} |
|
614 |
|
|
update document
|
615 |
# DBI instance |
616 |
my $dbh = $dbi->dbh; |
|
removed register_format()
|
617 |
|
update document
|
618 |
=head1 ATTRIBUTES |
packaging one directory
|
619 | |
620 |
=head2 user |
|
621 | ||
update document
|
622 |
Database user name. |
bind_filter argument is chan...
|
623 |
|
version 0.0901
|
624 |
$dbi = $dbi->user('Ken'); |
625 |
$user = $dbi->user; |
|
bind_filter argument is chan...
|
626 |
|
packaging one directory
|
627 |
=head2 password |
628 | ||
update document
|
629 |
Database password. |
bind_filter argument is chan...
|
630 |
|
version 0.0901
|
631 |
$dbi = $dbi->password('lkj&le`@s'); |
632 |
$password = $dbi->password; |
|
packaging one directory
|
633 | |
634 |
=head2 data_source |
|
635 | ||
update document
|
636 |
Database data source. |
bind_filter argument is chan...
|
637 |
|
version 0.0901
|
638 |
$dbi = $dbi->data_source("dbi:mysql:dbname=$database"); |
639 |
$data_source = $dbi->data_source; |
|
packaging one directory
|
640 |
|
version 0.0901
|
641 |
If you know data source more, See also L<DBI>. |
642 | ||
removed register_format()
|
643 |
=head2 sql_template |
packaging one directory
|
644 | |
update document
|
645 |
SQLTemplate instance. sql_template attribute must be |
646 |
the instance of L<DBIx::Cutom::SQLTemplate> subclass. |
|
packaging one directory
|
647 | |
removed register_format()
|
648 |
$dbi = $dbi->sql_template(DBIx::Cutom::SQLTemplate->new); |
649 |
$sql_template = $dbi->sql_template; |
|
packaging one directory
|
650 | |
update document
|
651 |
the instance of DBIx::Cutom::SQLTemplate is set to |
652 |
this attribute by default. |
|
packaging one directory
|
653 | |
bind_filter argument is chan...
|
654 |
=head2 filters |
packaging one directory
|
655 | |
update document
|
656 |
Filters |
packaging one directory
|
657 | |
update document
|
658 |
$dbi = $dbi->filters({%filters}); |
version 0.0901
|
659 |
$filters = $dbi->filters; |
660 | ||
update document
|
661 |
encode_utf8 and decode_utf8 is set to this attribute by default. |
version 0.0901
|
662 | |
update document
|
663 |
$encode_utf8 = $dbi->filters->{encode_utf8}; |
664 |
$decode_utf8 = $dbi->filters->{decode_utf8}; |
|
packaging one directory
|
665 | |
rename bind_filter to query_...
|
666 |
=head2 default_query_filter |
packaging one directory
|
667 | |
update document
|
668 |
Default query filter. |
packaging one directory
|
669 | |
update document
|
670 |
$dbi = $dbi->default_query_filter('encode_utf8'); |
rename bind_filter to query_...
|
671 |
$default_query_filter = $dbi->default_query_filter |
packaging one directory
|
672 | |
cleanup
|
673 |
=head2 default_fetch_filter |
packaging one directory
|
674 | |
update document
|
675 |
Fetching filter. |
bind_filter argument is chan...
|
676 | |
update document
|
677 |
$dbi = $dbi->default_fetch_filter('decode_utf8'); |
cleanup
|
678 |
$default_fetch_filter = $dbi->default_fetch_filter; |
bind_filter argument is chan...
|
679 | |
packaging one directory
|
680 |
=head2 result_class |
681 | ||
update document
|
682 |
Result class. |
bind_filter argument is chan...
|
683 | |
version 0.0901
|
684 |
$dbi = $dbi->result_class('DBIx::Custom::Result'); |
packaging one directory
|
685 |
$result_class = $dbi->result_class; |
686 | ||
update document
|
687 |
L<DBIx::Custom::Result> is set to this attribute by default. |
update document
|
688 | |
packaging one directory
|
689 |
=head2 dbh |
690 | ||
update document
|
691 |
Database handle. |
bind_filter argument is chan...
|
692 |
|
version 0.0901
|
693 |
$dbi = $dbi->dbh($dbh); |
694 |
$dbh = $dbi->dbh; |
|
bind_filter argument is chan...
|
695 |
|
update document
|
696 |
=head1 METHODS |
697 | ||
698 |
This class is L<Object::Simple> subclass. |
|
699 |
You can use all methods of L<Object::Simple> |
|
packaging one directory
|
700 | |
removed register_format()
|
701 |
=head2 auto_commit |
702 | ||
update document
|
703 |
Auto commit. |
removed register_format()
|
704 | |
update document
|
705 |
$self = $dbi->auto_commit(1); |
removed register_format()
|
706 |
$auto_commit = $dbi->auto_commit; |
update document
|
707 | |
708 |
This is equal to |
|
709 | ||
710 |
$dbi->dbh->{AutoCommit} = 1; |
|
711 |
$auto_commit = $dbi->dbh->{AutoCommit}; |
|
712 | ||
packaging one directory
|
713 |
=head2 connect |
714 | ||
update document
|
715 |
Connect to database. |
716 |
|
|
717 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=books", |
|
718 |
user => 'ken', password => '!LFKD%$&'); |
|
bind_filter argument is chan...
|
719 | |
update document
|
720 |
"AutoCommit" and "RaiseError" option is true, |
721 |
and "PrintError" option is false by dfault. |
|
packaging one directory
|
722 | |
723 |
=head2 disconnect |
|
724 | ||
update document
|
725 |
Disconnect database. |
bind_filter argument is chan...
|
726 | |
packaging one directory
|
727 |
$dbi->disconnect; |
728 | ||
version 0.0901
|
729 |
If database is already disconnected, this method do nothing. |
packaging one directory
|
730 | |
731 |
=head2 reconnect |
|
732 | ||
update document
|
733 |
Reconnect to database. |
bind_filter argument is chan...
|
734 | |
packaging one directory
|
735 |
$dbi->reconnect; |
736 | ||
737 |
=head2 connected |
|
738 | ||
version 0.0901
|
739 |
Check if database is connected. |
bind_filter argument is chan...
|
740 |
|
version 0.0901
|
741 |
$is_connected = $dbi->connected; |
packaging one directory
|
742 |
|
some changed
|
743 |
=head2 register_filter |
packaging one directory
|
744 | |
update document
|
745 |
Resister filter. |
packaging one directory
|
746 |
|
update document
|
747 |
$dbi->register_filter(%filters); |
bind_filter argument is chan...
|
748 |
|
update document
|
749 |
Example. |
version 0.0901
|
750 | |
some changed
|
751 |
$dbi->register_filter( |
packaging one directory
|
752 |
encode_utf8 => sub { |
removed register_format()
|
753 |
my $value = shift; |
754 |
|
|
755 |
require Encode; |
|
756 |
|
|
757 |
return Encode::encode('UTF-8', $value); |
|
packaging one directory
|
758 |
}, |
bind_filter argument is chan...
|
759 |
decode_utf8 => sub { |
removed register_format()
|
760 |
my $value = shift; |
761 |
|
|
762 |
require Encode; |
|
763 |
|
|
764 |
return Encode::decode('UTF-8', $value) |
|
packaging one directory
|
765 |
} |
766 |
); |
|
767 | ||
version 0.0901
|
768 |
=head2 create_query |
bind_filter argument is chan...
|
769 |
|
update document
|
770 |
Create Query instance parsing SQL template |
bind_filter argument is chan...
|
771 | |
version 0.0901
|
772 |
my $query = $dbi->create_query("select * from authors where {= name} and {= age}"); |
bind_filter argument is chan...
|
773 | |
removed register_format()
|
774 |
$query is <DBIx::Query> instance. This is executed by query method as the following |
bind_filter argument is chan...
|
775 | |
rename query() to execute()
|
776 |
$dbi->execute($query, $params); |
bind_filter argument is chan...
|
777 | |
many many changes
|
778 |
If you know SQL template, see also L<DBIx::Custom::SQLTemplate>. |
bind_filter argument is chan...
|
779 | |
rename query() to execute()
|
780 |
=head2 execute |
packaging one directory
|
781 | |
version 0.0901
|
782 |
Query |
bind_filter argument is chan...
|
783 | |
rename query() to execute()
|
784 |
$result = $dbi->execute($template, $params); |
packaging one directory
|
785 | |
rename query() to execute()
|
786 |
The following is query example |
bind_filter argument is chan...
|
787 | |
rename query() to execute()
|
788 |
$result = $dbi->execute("select * from authors where {= name} and {= age}", |
removed register_format()
|
789 |
{name => 'taro', age => 19}); |
packaging one directory
|
790 |
|
791 |
while (my @row = $result->fetch) { |
|
792 |
# do something |
|
793 |
} |
|
794 | ||
many many changes
|
795 |
If you now syntax of template, See also L<DBIx::Custom::SQLTemplate> |
version 0.0901
|
796 | |
removed register_format()
|
797 |
execute() return L<DBIx::Custom::Result> instance |
version 0.0901
|
798 | |
packaging one directory
|
799 |
=head2 insert |
800 | ||
update document
|
801 |
Insert row |
bind_filter argument is chan...
|
802 | |
removed register_format()
|
803 |
$affected = $dbi->insert(table => $table, |
804 |
param => {%param}, |
|
805 |
append => $append, |
|
806 |
filter => {%filter}); |
|
update document
|
807 | |
version 0.0901
|
808 |
Retrun value is affected rows count |
packaging one directory
|
809 |
|
removed register_format()
|
810 |
Example |
version 0.0901
|
811 | |
removed register_format()
|
812 |
# insert |
813 |
$dbi->insert(table => 'books', |
|
814 |
param => {title => 'Perl', author => 'Taro'}, |
|
815 |
append => "some statement", |
|
816 |
filter => {title => 'encode_utf8'}) |
|
version 0.0901
|
817 | |
packaging one directory
|
818 |
=head2 update |
819 | ||
update document
|
820 |
Update rows |
821 | ||
removed register_format()
|
822 |
$affected = $dbi->update(table => $table, |
823 |
param => {%params}, |
|
824 |
where => {%where}, |
|
825 |
append => $append, |
|
826 |
filter => {%filter}) |
|
version 0.0901
|
827 | |
828 |
Retrun value is affected rows count |
|
update document
|
829 | |
removed register_format()
|
830 |
Example |
bind_filter argument is chan...
|
831 | |
removed register_format()
|
832 |
#update |
833 |
$dbi->update(table => 'books', |
|
834 |
param => {title => 'Perl', author => 'Taro'}, |
|
835 |
where => {id => 5}, |
|
836 |
append => "some statement", |
|
837 |
filter => {title => 'encode_utf8'}) |
|
version 0.0901
|
838 | |
packaging one directory
|
839 |
=head2 update_all |
840 | ||
bind_filter argument is chan...
|
841 |
Update all rows |
842 | ||
removed register_format()
|
843 |
$affected = $dbi->update_all(table => $table, |
844 |
param => {%params}, |
|
845 |
filter => {%filter}, |
|
846 |
append => $append); |
|
update document
|
847 | |
version 0.0901
|
848 |
Retrun value is affected rows count |
849 | ||
removed register_format()
|
850 |
Example |
update document
|
851 | |
removed register_format()
|
852 |
# update_all |
853 |
$dbi->update_all(table => 'books', |
|
854 |
param => {author => 'taro'}, |
|
855 |
filter => {author => 'encode_utf8'}); |
|
packaging one directory
|
856 | |
857 |
=head2 delete |
|
858 | ||
update document
|
859 |
Delete rows |
860 | ||
removed register_format()
|
861 |
# delete |
862 |
$affected = $dbi->delete(table => $table, |
|
863 |
where => {%where}, |
|
864 |
append => $append |
|
865 |
filter => {%filter}); |
|
bind_filter argument is chan...
|
866 | |
version 0.0901
|
867 |
Retrun value is affected rows count |
packaging one directory
|
868 |
|
removed register_format()
|
869 |
Example |
packaging one directory
|
870 | |
removed register_format()
|
871 |
# delete |
872 |
$dbi->delete(table => 'books', |
|
873 |
where => {id => 5}, |
|
874 |
append => 'some statement', |
|
875 |
filter => {id => 'encode_utf8'); |
|
version 0.0901
|
876 | |
packaging one directory
|
877 |
=head2 delete_all |
878 | ||
bind_filter argument is chan...
|
879 |
Delete all rows |
880 | ||
removed register_format()
|
881 |
$affected = $dbi->delete_all(table => $table); |
packaging one directory
|
882 | |
version 0.0901
|
883 |
Retrun value is affected rows count |
bind_filter argument is chan...
|
884 | |
removed register_format()
|
885 |
Example |
886 |
|
|
887 |
# delete_all |
|
version 0.0901
|
888 |
$dbi->delete_all('books'); |
packaging one directory
|
889 | |
890 |
=head2 select |
|
891 |
|
|
update document
|
892 |
Select rows |
bind_filter argument is chan...
|
893 | |
removed register_format()
|
894 |
$result = $dbi->select(table => $table, |
895 |
column => [@column], |
|
896 |
where => {%where}, |
|
897 |
append => $append, |
|
898 |
filter => {%filter}); |
|
update document
|
899 | |
removed register_format()
|
900 |
$reslt is L<DBIx::Custom::Result> instance |
update document
|
901 | |
rename query() to execute()
|
902 |
The following is some select examples |
update document
|
903 | |
removed register_format()
|
904 |
# select |
update document
|
905 |
$result = $dbi->select('books'); |
packaging one directory
|
906 |
|
update document
|
907 |
# select * from books where title = 'Perl'; |
908 |
$result = $dbi->select('books', {title => 1}); |
|
909 |
|
|
910 |
# select title, author from books where id = 1 for update; |
|
911 |
$result = $dbi->select( |
|
removed register_format()
|
912 |
table => 'books', |
913 |
where => ['title', 'author'], |
|
914 |
where => {id => 1}, |
|
915 |
appned => 'for update' |
|
update document
|
916 |
); |
917 | ||
918 |
You can join multi tables |
|
919 |
|
|
920 |
$result = $dbi->select( |
|
921 |
['table1', 'table2'], # tables |
|
922 |
['table1.id as table1_id', 'title'], # columns (alias is ok) |
|
923 |
{table1.id => 1}, # where clase |
|
924 |
"where table1.id = table2.id", # join clause (must start 'where') |
|
925 |
); |
|
926 | ||
packaging one directory
|
927 |
=head1 AUTHOR |
928 | ||
929 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
|
930 | ||
931 |
Github L<http://github.com/yuki-kimoto> |
|
932 | ||
version 0.0901
|
933 |
I develope this module L<http://github.com/yuki-kimoto/DBIx-Custom> |
934 | ||
packaging one directory
|
935 |
=head1 COPYRIGHT & LICENSE |
936 | ||
937 |
Copyright 2009 Yuki Kimoto, all rights reserved. |
|
938 | ||
939 |
This program is free software; you can redistribute it and/or modify it |
|
940 |
under the same terms as Perl itself. |
|
941 | ||
942 |
=cut |