cleanup
|
1 |
package DBIx::Custom; |
2 | ||
update pod
|
3 |
our $VERSION = '0.1660'; |
fixed DBIx::Custom::QueryBui...
|
4 | |
5 |
use 5.008001; |
|
cleanup
|
6 |
use strict; |
7 |
use warnings; |
|
8 | ||
remove run_transaction().
|
9 |
use base 'Object::Simple'; |
many change
|
10 | |
packaging one directory
|
11 |
use Carp 'croak'; |
12 |
use DBI; |
|
13 |
use DBIx::Custom::Result; |
|
cleanup
|
14 |
use DBIx::Custom::Query; |
cleanup
|
15 |
use DBIx::Custom::QueryBuilder; |
added experimental DBIx::Cus...
|
16 |
use DBIx::Custom::Where; |
add feture. all model class ...
|
17 |
use DBIx::Custom::Model; |
cleanup
|
18 |
use DBIx::Custom::Tag; |
all filter can receive array...
|
19 |
use DBIx::Custom::Util; |
update document
|
20 |
use Encode qw/encode_utf8 decode_utf8/; |
packaging one directory
|
21 | |
fix tests
|
22 |
__PACKAGE__->attr( |
EXPERIMETAL fork safety impl...
|
23 |
[qw/data_source password pid user/], |
fix tests
|
24 |
cache => 1, |
many changed
|
25 |
cache_method => sub { |
26 |
sub { |
|
27 |
my $self = shift; |
|
28 |
|
|
29 |
$self->{_cached} ||= {}; |
|
30 |
|
|
31 |
if (@_ > 1) { |
|
update pod
|
32 |
$self->{_cached}{$_[0]} = $_[1]; |
many changed
|
33 |
} |
34 |
else { |
|
update pod
|
35 |
return $self->{_cached}{$_[0]}; |
many changed
|
36 |
} |
37 |
} |
|
update pod
|
38 |
}, |
39 |
dbi_option => sub { {} }, |
|
40 |
default_dbi_option => sub { |
|
41 |
{ |
|
42 |
RaiseError => 1, |
|
43 |
PrintError => 0, |
|
44 |
AutoCommit => 1 |
|
45 |
} |
|
46 |
}, |
|
fix tests
|
47 |
filters => sub { |
48 |
{ |
|
49 |
encode_utf8 => sub { encode_utf8($_[0]) }, |
|
50 |
decode_utf8 => sub { decode_utf8($_[0]) } |
|
51 |
} |
|
update pod
|
52 |
}, |
53 |
models => sub { {} }, |
|
54 |
query_builder => sub { DBIx::Custom::QueryBuilder->new }, |
|
55 |
result_class => 'DBIx::Custom::Result', |
|
56 |
safety_character => '\w', |
|
57 |
stash => sub { {} } |
|
fix tests
|
58 |
); |
cleanup
|
59 | |
added helper method
|
60 |
our $AUTOLOAD; |
61 |
sub AUTOLOAD { |
|
62 |
my $self = shift; |
|
63 | ||
renamed helper to method.
|
64 |
# Method name |
65 |
my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/; |
|
added helper method
|
66 | |
renamed helper to method.
|
67 |
# Method |
68 |
$self->{_methods} ||= {}; |
|
add feture. all model class ...
|
69 |
if (my $method = $self->{_methods}->{$mname}) { |
70 |
return $self->$method(@_) |
|
71 |
} |
|
72 |
elsif ($self->dbh->can($mname)) { |
|
73 |
$self->dbh->$mname(@_); |
|
74 |
} |
|
75 |
else { |
|
76 |
croak qq/Can't locate object method "$mname" via "$package"/ |
|
77 |
} |
|
added helper method
|
78 |
} |
79 | ||
renamed auto_filter to apply...
|
80 |
sub apply_filter { |
many changed
|
81 |
my ($self, $table, @cinfos) = @_; |
82 | ||
83 |
# Initialize filters |
|
cleanup
|
84 |
$self->{filter} ||= {}; |
many changed
|
85 |
$self->{filter}{out} ||= {}; |
86 |
$self->{filter}{in} ||= {}; |
|
all filter can receive array...
|
87 |
$self->{filter}{end} ||= {}; |
cleanup
|
88 |
|
many changed
|
89 |
# Create filters |
90 |
my $usage = "Usage: \$dbi->apply_filter(" . |
|
fix bug : filter can't over...
|
91 |
"TABLE, COLUMN1, {in => INFILTER1, out => OUTFILTER1, end => ENDFILTER1}, " . |
92 |
"COLUMN2, {in => INFILTER2, out => OUTFILTER2, end => ENDFILTER2}, ...)"; |
|
many changed
|
93 | |
94 |
for (my $i = 0; $i < @cinfos; $i += 2) { |
|
added auto_filter method
|
95 |
|
many changed
|
96 |
# Column |
97 |
my $column = $cinfos[$i]; |
|
added auto_filter method
|
98 |
|
all filter can receive array...
|
99 |
if (ref $column eq 'ARRAY') { |
100 |
foreach my $c (@$column) { |
|
101 |
push @cinfos, $c, $cinfos[$i + 1]; |
|
102 |
} |
|
103 |
next; |
|
104 |
} |
|
105 |
|
|
fix bug : filter can't over...
|
106 |
# Filter info |
107 |
my $finfo = $cinfos[$i + 1] || {}; |
|
all filter can receive array...
|
108 |
croak "$usage (table: $table)" unless ref $finfo eq 'HASH'; |
fix bug : filter can't over...
|
109 |
foreach my $ftype (keys %$finfo) { |
all filter can receive array...
|
110 |
croak "$usage (table: $table 2)" unless $ftype eq 'in' || $ftype eq 'out' |
fix bug : filter can't over...
|
111 |
|| $ftype eq 'end'; |
many changed
|
112 |
} |
113 |
|
|
fix bug : filter can't over...
|
114 |
foreach my $way (qw/in out end/) { |
115 |
my $filter = $finfo->{$way}; |
|
cleanup
|
116 |
|
fix bug : filter can't over...
|
117 |
# State |
118 |
my $state = !exists $finfo->{$way} ? 'not_exists' |
|
119 |
: !defined $filter ? 'not_defined' |
|
120 |
: ref $filter eq 'CODE' ? 'code' |
|
121 |
: 'name'; |
|
122 |
|
|
123 |
next if $state eq 'not_exists'; |
|
124 |
|
|
125 |
# Check filter |
|
126 |
croak qq{Filter "$filter" is not registered} |
|
127 |
if $state eq 'name' |
|
128 |
&& ! exists $self->filters->{$filter}; |
|
129 |
|
|
130 |
# Filter |
|
131 |
my $f = $state eq 'not_defined' ? undef |
|
132 |
: $state eq 'code' ? $filter |
|
133 |
: $self->filters->{$filter}; |
|
134 |
$self->{filter}{$way}{$table}{$column} = $f; |
|
135 |
$self->{filter}{$way}{$table}{"$table.$column"} = $f; |
|
136 |
$self->{filter}{$way}{$table}{"${table}__$column"} = $f; |
|
many changed
|
137 |
} |
added auto_filter method
|
138 |
} |
139 |
|
|
many changed
|
140 |
return $self; |
added auto_filter method
|
141 |
} |
142 | ||
renamed helper to method.
|
143 |
sub method { |
added helper method
|
144 |
my $self = shift; |
145 |
|
|
146 |
# Merge |
|
renamed helper to method.
|
147 |
my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
148 |
$self->{_methods} = {%{$self->{_methods} || {}}, %$methods}; |
|
added helper method
|
149 |
|
150 |
return $self; |
|
151 |
} |
|
152 | ||
packaging one directory
|
153 |
sub connect { |
cleanup
|
154 |
my $self = ref $_[0] ? shift : shift->new(@_);; |
removed register_format()
|
155 |
|
EXPERIMETAL fork safety impl...
|
156 |
my $dbh = $self->_connect; |
packaging one directory
|
157 |
|
update document
|
158 |
# Database handle |
packaging one directory
|
159 |
$self->dbh($dbh); |
update document
|
160 |
|
EXPERIMETAL fork safety impl...
|
161 |
# Process ID |
162 |
$self->pid($$); |
|
163 |
|
|
packaging one directory
|
164 |
return $self; |
165 |
} |
|
166 | ||
cleanup
|
167 |
sub create_query { |
168 |
my ($self, $source) = @_; |
|
update document
|
169 |
|
cleanup
|
170 |
# Cache |
171 |
my $cache = $self->cache; |
|
update document
|
172 |
|
cleanup
|
173 |
# Create query |
174 |
my $query; |
|
175 |
if ($cache) { |
|
176 |
|
|
177 |
# Get query |
|
178 |
my $q = $self->cache_method->($self, $source); |
|
179 |
|
|
180 |
# Create query |
|
add table tag
|
181 |
if ($q) { |
182 |
$query = DBIx::Custom::Query->new($q); |
|
183 |
$query->filters($self->filters); |
|
184 |
} |
|
cleanup
|
185 |
} |
186 |
|
|
187 |
unless ($query) { |
|
cleanup insert
|
188 | |
cleanup
|
189 |
# Create SQL object |
190 |
my $builder = $self->query_builder; |
|
191 |
|
|
192 |
# Create query |
|
193 |
$query = $builder->build_query($source); |
|
removed register_format()
|
194 | |
cleanup
|
195 |
# Cache query |
196 |
$self->cache_method->($self, $source, |
|
197 |
{sql => $query->sql, |
|
add table tag
|
198 |
columns => $query->columns, |
199 |
tables => $query->tables}) |
|
cleanup
|
200 |
if $cache; |
cleanup insert
|
201 |
} |
202 |
|
|
cleanup
|
203 |
# Prepare statement handle |
204 |
my $sth; |
|
205 |
eval { $sth = $self->dbh->prepare($query->{sql})}; |
|
renamed DBIx::Custom::TagPro...
|
206 |
$self->_croak($@, qq{. Following SQL is executed. "$query->{sql}"}) if $@; |
packaging one directory
|
207 |
|
cleanup
|
208 |
# Set statement handle |
209 |
$query->sth($sth); |
|
packaging one directory
|
210 |
|
cleanup
|
211 |
# Set filters |
212 |
$query->filters($self->filters); |
|
213 |
|
|
cleanup
|
214 |
return $query; |
packaging one directory
|
215 |
} |
216 | ||
update pod
|
217 |
sub dbh { |
218 |
my $self = shift; |
|
219 | ||
220 |
if (@_) { |
|
221 |
$self->{dbh} = $_[0]; |
|
222 |
return $self; |
|
223 |
} |
|
224 |
else { |
|
225 |
my $pid = $$; |
|
226 |
if ($self->pid eq $pid) { |
|
227 |
return $self->{dbh}; |
|
228 |
} |
|
229 |
else { |
|
230 |
# Create new connection in child process |
|
231 |
croak "Process is forked in transaction" |
|
232 |
unless $self->{dbh}->{AutoCommit}; |
|
233 |
$self->pid($pid); |
|
234 |
$self->{dbh}->{InactiveDestroy} = 1; |
|
235 |
return $self->{dbh} = $self->_connect; |
|
236 |
} |
|
237 |
} |
|
238 |
} |
|
239 | ||
cleanup
|
240 |
our %VALID_DELETE_ARGS |
added experimental sugar met...
|
241 |
= map { $_ => 1 } qw/table where append filter allow_delete_all query/; |
cleanup update and update_al...
|
242 | |
cleanup
|
243 |
sub delete { |
select, insert, update, upda...
|
244 |
my ($self, %args) = @_; |
cleanup update and update_al...
|
245 |
|
cleanup
|
246 |
# Check argument names |
select, insert, update, upda...
|
247 |
foreach my $name (keys %args) { |
cleanup
|
248 |
croak qq{Argument "$name" is invalid name} |
cleanup
|
249 |
unless $VALID_DELETE_ARGS{$name}; |
cleanup update and update_al...
|
250 |
} |
251 |
|
|
252 |
# Arguments |
|
select, insert, update, upda...
|
253 |
my $table = $args{table} || ''; |
added table not specified ex...
|
254 |
croak qq{"table" option must be specified} unless $table; |
select, insert, update, upda...
|
255 |
my $where = $args{where} || {}; |
select() where can't receive...
|
256 |
my $append = $args{append}; |
select, insert, update, upda...
|
257 |
my $filter = $args{filter}; |
cleanup
|
258 |
my $allow_delete_all = $args{allow_delete_all}; |
added auto_filter method
|
259 | |
make delete() using where ob...
|
260 |
# Where |
261 |
my $w; |
|
262 |
if (ref $where eq 'HASH') { |
|
263 |
my $clause = ['and']; |
|
264 |
push @$clause, "{= $_}" for keys %$where; |
|
265 |
$w = $self->where; |
|
266 |
$w->clause($clause); |
|
improved delete() and update...
|
267 |
$w->param($where); |
packaging one directory
|
268 |
} |
select() where can't receive...
|
269 |
elsif (ref $where eq 'DBIx::Custom::Where') { |
270 |
$w = $where; |
|
271 |
$where = $w->param; |
|
272 |
} |
|
make delete() using where ob...
|
273 |
croak qq{"where" must be hash refernce or DBIx::Custom::Where object} |
274 |
unless ref $w eq 'DBIx::Custom::Where'; |
|
275 |
|
|
select() where can't receive...
|
276 |
# String where |
277 |
my $swhere = "$w"; |
|
improved delete() and update...
|
278 |
|
make delete() using where ob...
|
279 |
croak qq{"where" must be specified} |
select() where can't receive...
|
280 |
if $swhere eq '' && !$allow_delete_all; |
make delete() using where ob...
|
281 | |
cleanup
|
282 |
# SQL stack |
283 |
my @sql; |
|
284 | ||
285 |
# Delete |
|
286 |
push @sql, "delete from $table $swhere"; |
|
287 |
push @sql, $append if $append; |
|
288 |
|
|
289 |
my $sql = join(' ', @sql); |
|
packaging one directory
|
290 |
|
added experimental sugar met...
|
291 |
# Create query |
cleanup
|
292 |
my $query = $self->create_query($sql); |
added experimental sugar met...
|
293 |
return $query if $args{query}; |
294 |
|
|
packaging one directory
|
295 |
# Execute query |
added auto_filter method
|
296 |
my $ret_val = $self->execute( |
added experimental sugar met...
|
297 |
$query, param => $where, filter => $filter, |
renamed auto_filter to apply...
|
298 |
table => $table); |
packaging one directory
|
299 |
|
300 |
return $ret_val; |
|
301 |
} |
|
302 | ||
cleanup
|
303 |
sub delete_all { shift->delete(allow_delete_all => 1, @_) } |
packaging one directory
|
304 | |
add experimental update_at()...
|
305 |
our %VALID_DELETE_AT_ARGS |
update pod
|
306 |
= map { $_ => 1 } qw/table where append filter query primary_key param/; |
add experimental update_at()...
|
307 | |
308 |
sub delete_at { |
|
309 |
my ($self, %args) = @_; |
|
310 |
|
|
cleanup
|
311 |
# Check argument names |
add experimental update_at()...
|
312 |
foreach my $name (keys %args) { |
cleanup
|
313 |
croak qq{Argument "$name" is invalid name} |
add experimental update_at()...
|
314 |
unless $VALID_DELETE_AT_ARGS{$name}; |
315 |
} |
|
316 |
|
|
317 |
# Primary key |
|
318 |
my $primary_keys = delete $args{primary_key}; |
|
319 |
$primary_keys = [$primary_keys] unless ref $primary_keys; |
|
320 |
|
|
321 |
# Where clause |
|
322 |
my $where = {}; |
|
323 |
if (exists $args{where}) { |
|
324 |
my $where_columns = delete $args{where}; |
|
325 |
$where_columns = [$where_columns] unless ref $where_columns; |
|
- added experimental DBIx::C...
|
326 | |
327 |
croak qq{"where" must be constant value or array reference} |
|
328 |
unless !ref $where_columns || ref $where_columns eq 'ARRAY'; |
|
add experimental update_at()...
|
329 |
|
330 |
for(my $i = 0; $i < @$primary_keys; $i ++) { |
|
331 |
$where->{$primary_keys->[$i]} = $where_columns->[$i]; |
|
332 |
} |
|
333 |
} |
|
- added experimental DBIx::C...
|
334 |
|
335 |
if (exists $args{param}) { |
|
add experimental update_at()...
|
336 |
my $param = delete $args{param}; |
337 |
|
|
338 |
for(my $i = 0; $i < @$primary_keys; $i ++) { |
|
- added experimental DBIx::C...
|
339 |
delete $param->{$primary_keys->[$i]}; |
add experimental update_at()...
|
340 |
} |
341 |
} |
|
342 |
|
|
343 |
return $self->delete(where => $where, %args); |
|
344 |
} |
|
345 | ||
added helper method
|
346 |
sub DESTROY { } |
347 | ||
renamed auto_filter to apply...
|
348 |
our %VALID_EXECUTE_ARGS = map { $_ => 1 } qw/param filter table/; |
refactoring delete and delet...
|
349 | |
cleanup
|
350 |
sub execute{ |
351 |
my ($self, $query, %args) = @_; |
|
refactoring delete and delet...
|
352 |
|
cleanup
|
353 |
# Check argument names |
select, insert, update, upda...
|
354 |
foreach my $name (keys %args) { |
cleanup
|
355 |
croak qq{Argument "$name" is invalid name} |
cleanup
|
356 |
unless $VALID_EXECUTE_ARGS{$name}; |
refactoring delete and delet...
|
357 |
} |
358 |
|
|
cleanup
|
359 |
my $params = $args{param} || {}; |
packaging one directory
|
360 |
|
cleanup
|
361 |
# First argument is the soruce of SQL |
362 |
$query = $self->create_query($query) |
|
363 |
unless ref $query; |
|
packaging one directory
|
364 |
|
add table tag
|
365 |
# Applied filter |
cleanup
|
366 |
my $filter = {}; |
all filter can receive array...
|
367 |
|
add table tag
|
368 |
my $tables = $query->tables; |
369 |
my $arg_tables = $args{table} || []; |
|
370 |
$arg_tables = [$arg_tables] |
|
371 |
unless ref $arg_tables eq 'ARRAY'; |
|
372 |
push @$tables, @$arg_tables; |
|
cleanup
|
373 | |
374 |
# Organize tables |
|
375 |
my %table_set = map {defined $_ ? ($_ => 1) : ()} @$tables; |
|
376 |
my $main_table = pop @$tables; |
|
377 |
delete $table_set{$main_table} if $main_table; |
|
378 |
$tables = [keys %table_set]; |
|
379 |
push @$tables, $main_table if $main_table; |
|
remove experimental DBIx::Cu...
|
380 |
|
renamed auto_filter to apply...
|
381 |
foreach my $table (@$tables) { |
remove experimental DBIx::Cu...
|
382 |
next unless $table; |
cleanup
|
383 |
$filter = { |
384 |
%$filter, |
|
385 |
%{$self->{filter}{out}->{$table} || {}} |
|
added auto_filter method
|
386 |
} |
387 |
} |
|
388 |
|
|
cleanup
|
389 |
# Filter argument |
all filter can receive array...
|
390 |
my $f = DBIx::Custom::Util::array_filter_to_hash($args{filter}) |
391 |
|| $query->filter || {}; |
|
cleanup
|
392 |
foreach my $column (keys %$f) { |
393 |
my $fname = $f->{$column}; |
|
renamed auto_filter to apply...
|
394 |
if (!defined $fname) { |
cleanup
|
395 |
$f->{$column} = undef; |
renamed auto_filter to apply...
|
396 |
} |
397 |
elsif (ref $fname ne 'CODE') { |
|
many changed
|
398 |
croak qq{Filter "$fname" is not registered"} |
cleanup
|
399 |
unless exists $self->filters->{$fname}; |
400 |
|
|
cleanup
|
401 |
$f->{$column} = $self->filters->{$fname}; |
cleanup
|
402 |
} |
403 |
} |
|
cleanup
|
404 |
$filter = {%$filter, %$f}; |
packaging one directory
|
405 |
|
added experimental not_exist...
|
406 |
# Bind |
407 |
my $bind = $self->_bind($params, $query->columns, $filter); |
|
cleanup
|
408 |
|
409 |
# Execute |
|
added experimental not_exist...
|
410 |
my $sth = $query->sth; |
cleanup
|
411 |
my $affected; |
added experimental not_exist...
|
412 |
eval {$affected = $sth->execute(@$bind)}; |
renamed DBIx::Custom::TagPro...
|
413 |
$self->_croak($@, qq{. Following SQL is executed. "$query->{sql}"}) if $@; |
cleanup
|
414 |
|
415 |
# Return resultset if select statement is executed |
|
416 |
if ($sth->{NUM_OF_FIELDS}) { |
|
417 |
|
|
fix bug : filter can't over...
|
418 |
# Result in and end filter |
419 |
my $in_filter = {}; |
|
420 |
my $end_filter = {}; |
|
cleanup
|
421 |
foreach my $table (@$tables) { |
remove experimental DBIx::Cu...
|
422 |
next unless $table; |
cleanup
|
423 |
$in_filter = { |
424 |
%$in_filter, |
|
425 |
%{$self->{filter}{in}{$table} || {}} |
|
fix bug : filter can't over...
|
426 |
}; |
427 |
$end_filter = { |
|
428 |
%$end_filter, |
|
429 |
%{$self->{filter}{end}{$table} || {}} |
|
430 |
}; |
|
cleanup
|
431 |
} |
432 |
|
|
433 |
# Result |
|
434 |
my $result = $self->result_class->new( |
|
cleanup
|
435 |
sth => $sth, |
436 |
filters => $self->filters, |
|
437 |
filter_check => $self->filter_check, |
|
cleanup
|
438 |
default_filter => $self->{default_in_filter}, |
fix bug : filter can't over...
|
439 |
filter => $in_filter || {}, |
440 |
end_filter => $end_filter || {} |
|
cleanup
|
441 |
); |
442 | ||
443 |
return $result; |
|
444 |
} |
|
445 |
return $affected; |
|
446 |
} |
|
447 | ||
update pod
|
448 |
our %VALID_INSERT_ARGS |
449 |
= map { $_ => 1 } qw/table param append filter query/; |
|
450 | ||
cleanup
|
451 |
sub insert { |
452 |
my ($self, %args) = @_; |
|
453 | ||
cleanup
|
454 |
# Check argument names |
cleanup
|
455 |
foreach my $name (keys %args) { |
cleanup
|
456 |
croak qq{Argument "$name" is invalid name} |
cleanup
|
457 |
unless $VALID_INSERT_ARGS{$name}; |
packaging one directory
|
458 |
} |
459 |
|
|
cleanup
|
460 |
# Arguments |
added table not specified ex...
|
461 |
my $table = $args{table}; |
462 |
croak qq{"table" option must be specified} unless $table; |
|
cleanup
|
463 |
my $param = $args{param} || {}; |
464 |
my $append = $args{append} || ''; |
|
465 |
my $filter = $args{filter}; |
|
466 |
|
|
select() where can't receive...
|
467 |
# Columns |
468 |
my @columns; |
|
- remaned experimental safty...
|
469 |
my $safety = $self->safety_character; |
select() where can't receive...
|
470 |
foreach my $column (keys %$param) { |
471 |
croak qq{"$column" is not safety column name} |
|
- remaned experimental safty...
|
472 |
unless $column =~ /^[$safety\.]+$/; |
select() where can't receive...
|
473 |
push @columns, $column; |
474 |
} |
|
cleanup
|
475 |
|
cleanup
|
476 |
# SQL stack |
477 |
my @sql; |
|
478 |
|
|
479 |
# Insert |
|
480 |
push @sql, "insert into $table {insert_param ". join(' ', @columns) . '}'; |
|
481 |
push @sql, $append if $append; |
|
482 |
|
|
select() where can't receive...
|
483 |
# SQL |
cleanup
|
484 |
my $sql = join (' ', @sql); |
packaging one directory
|
485 |
|
added experimental sugar met...
|
486 |
# Create query |
cleanup
|
487 |
my $query = $self->create_query($sql); |
added experimental sugar met...
|
488 |
return $query if $args{query}; |
489 |
|
|
packaging one directory
|
490 |
# Execute query |
added auto_filter method
|
491 |
my $ret_val = $self->execute( |
added experimental sugar met...
|
492 |
$query, |
added auto_filter method
|
493 |
param => $param, |
494 |
filter => $filter, |
|
renamed auto_filter to apply...
|
495 |
table => $table |
added auto_filter method
|
496 |
); |
packaging one directory
|
497 |
|
498 |
return $ret_val; |
|
499 |
} |
|
500 | ||
- added experimental DBIx::C...
|
501 |
our %VALID_INSERT_AT_ARGS |
update pod
|
502 |
= map { $_ => 1 } qw/table param where append filter |
503 |
query primary_key param/; |
|
- added experimental DBIx::C...
|
504 | |
505 |
sub insert_at { |
|
506 |
my ($self, %args) = @_; |
|
507 |
|
|
cleanup
|
508 |
# Check argument names |
- added experimental DBIx::C...
|
509 |
foreach my $name (keys %args) { |
cleanup
|
510 |
croak qq{Argument "$name" is invalid name} |
- added experimental DBIx::C...
|
511 |
unless $VALID_INSERT_AT_ARGS{$name}; |
512 |
} |
|
513 |
|
|
514 |
# Primary key |
|
515 |
my $primary_keys = delete $args{primary_key}; |
|
516 |
$primary_keys = [$primary_keys] unless ref $primary_keys; |
|
517 |
|
|
518 |
# Where clause |
|
519 |
my $where = {}; |
|
520 |
my $param = {}; |
|
521 |
|
|
522 |
if (exists $args{where}) { |
|
523 |
my $where_columns = delete $args{where}; |
|
524 |
$where_columns = [$where_columns] unless ref $where_columns; |
|
525 | ||
526 |
croak qq{"where" must be constant value or array reference} |
|
527 |
unless !ref $where_columns || ref $where_columns eq 'ARRAY'; |
|
528 |
|
|
529 |
for(my $i = 0; $i < @$primary_keys; $i ++) { |
|
530 |
$where->{$primary_keys->[$i]} = $where_columns->[$i]; |
|
531 |
} |
|
532 |
} |
|
533 |
|
|
534 |
if (exists $args{param}) { |
|
535 |
$param = delete $args{param}; |
|
536 |
for(my $i = 0; $i < @$primary_keys; $i ++) { |
|
537 |
delete $param->{$primary_keys->[$i]}; |
|
538 |
} |
|
539 |
} |
|
540 |
|
|
541 |
$param = {%$param, %$where}; |
|
542 |
|
|
543 |
return $self->insert(param => $param, %args); |
|
544 |
} |
|
545 | ||
remove experimental DBIx::Cu...
|
546 |
sub insert_param { |
547 |
my ($self, $param) = @_; |
|
548 |
|
|
update pod
|
549 |
# Insert parameter tag |
remove experimental DBIx::Cu...
|
550 |
my @tag; |
551 |
push @tag, '{insert_param'; |
|
- remaned experimental safty...
|
552 |
my $safety = $self->safety_character; |
remove experimental DBIx::Cu...
|
553 |
foreach my $column (keys %$param) { |
added experimental update_pa...
|
554 |
croak qq{"$column" is not safety column name} |
- remaned experimental safty...
|
555 |
unless $column =~ /^[$safety\.]+$/; |
remove experimental DBIx::Cu...
|
556 |
push @tag, $column; |
557 |
} |
|
558 |
push @tag, '}'; |
|
559 |
|
|
560 |
return join ' ', @tag; |
|
561 |
} |
|
562 | ||
pod fix
|
563 |
sub each_column { |
added experimental iterate_a...
|
564 |
my ($self, $cb) = @_; |
565 |
|
|
566 |
# Iterate all tables |
|
567 |
my $sth_tables = $self->dbh->table_info; |
|
568 |
while (my $table_info = $sth_tables->fetchrow_hashref) { |
|
569 |
|
|
570 |
# Table |
|
571 |
my $table = $table_info->{TABLE_NAME}; |
|
572 |
|
|
573 |
# Iterate all columns |
|
574 |
my $sth_columns = $self->dbh->column_info(undef, undef, $table, '%'); |
|
575 |
while (my $column_info = $sth_columns->fetchrow_hashref) { |
|
576 |
my $column = $column_info->{COLUMN_NAME}; |
|
removed experimental txn_sco...
|
577 |
$self->$cb($table, $column, $column_info); |
added experimental iterate_a...
|
578 |
} |
579 |
} |
|
580 |
} |
|
581 | ||
added dbi_options attribute
|
582 |
sub new { |
583 |
my $self = shift->SUPER::new(@_); |
|
584 |
|
|
585 |
# Check attribute names |
|
586 |
my @attrs = keys %$self; |
|
587 |
foreach my $attr (@attrs) { |
|
588 |
croak qq{"$attr" is invalid attribute name} |
|
589 |
unless $self->can($attr); |
|
590 |
} |
|
cleanup
|
591 | |
592 |
$self->register_tag( |
|
593 |
'?' => \&DBIx::Custom::Tag::placeholder, |
|
594 |
'=' => \&DBIx::Custom::Tag::equal, |
|
595 |
'<>' => \&DBIx::Custom::Tag::not_equal, |
|
596 |
'>' => \&DBIx::Custom::Tag::greater_than, |
|
597 |
'<' => \&DBIx::Custom::Tag::lower_than, |
|
598 |
'>=' => \&DBIx::Custom::Tag::greater_than_equal, |
|
599 |
'<=' => \&DBIx::Custom::Tag::lower_than_equal, |
|
600 |
'like' => \&DBIx::Custom::Tag::like, |
|
601 |
'in' => \&DBIx::Custom::Tag::in, |
|
602 |
'insert_param' => \&DBIx::Custom::Tag::insert_param, |
|
603 |
'update_param' => \&DBIx::Custom::Tag::update_param |
|
604 |
); |
|
added dbi_options attribute
|
605 |
|
606 |
return $self; |
|
607 |
} |
|
608 | ||
added experimental not_exist...
|
609 |
sub not_exists { bless {}, 'DBIx::Custom::NotExists' } |
610 | ||
cleanup
|
611 |
sub register_filter { |
612 |
my $invocant = shift; |
|
613 |
|
|
614 |
# Register filter |
|
615 |
my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
616 |
$invocant->filters({%{$invocant->filters}, %$filters}); |
|
617 |
|
|
618 |
return $invocant; |
|
619 |
} |
|
packaging one directory
|
620 | |
renamed DBIx::Custom::TagPro...
|
621 |
sub register_tag { shift->query_builder->register_tag(@_) } |
added register_tag_processor
|
622 | |
refactoring select
|
623 |
our %VALID_SELECT_ARGS |
added select() all_column op...
|
624 |
= map { $_ => 1 } qw/table column where append relation filter query |
625 |
selection join all_column/; |
|
refactoring select
|
626 | |
packaging one directory
|
627 |
sub select { |
select, insert, update, upda...
|
628 |
my ($self, %args) = @_; |
packaging one directory
|
629 |
|
cleanup
|
630 |
# Check argument names |
select, insert, update, upda...
|
631 |
foreach my $name (keys %args) { |
cleanup
|
632 |
croak qq{Argument "$name" is invalid name} |
refactoring select
|
633 |
unless $VALID_SELECT_ARGS{$name}; |
634 |
} |
|
packaging one directory
|
635 |
|
refactoring select
|
636 |
# Arguments |
added table not specified ex...
|
637 |
my $table = $args{table}; |
638 |
my $tables = ref $table eq 'ARRAY' ? $table |
|
639 |
: defined $table ? [$table] |
|
640 |
: []; |
|
add experimental selection o...
|
641 |
my $columns = $args{column} || []; |
added select() all_column op...
|
642 |
$columns = [$columns] unless ref $columns eq 'ARRAY'; |
643 |
my $all_column = $args{all_column}; |
|
add experimental selection o...
|
644 |
my $selection = $args{selection} || ''; |
645 |
my $where = $args{where} || {}; |
|
646 |
my $append = $args{append}; |
|
647 |
my $filter = $args{filter}; |
|
- added experimental DBIx::C...
|
648 |
my $join = $args{join} || []; |
649 |
croak qq{"join" must be array reference} |
|
650 |
unless ref $join eq 'ARRAY'; |
|
remove experimental DBIx::Cu...
|
651 |
|
cleanup
|
652 |
# Add relation tables(DEPRECATED!); |
653 |
$self->_add_relation_table($tables, $args{relation}); |
|
packaging one directory
|
654 |
|
cleanup
|
655 |
# SQL stack |
656 |
my @sql; |
|
657 |
push @sql, 'select'; |
|
packaging one directory
|
658 |
|
cleanup
|
659 |
# Selection |
fixed some select() join opi...
|
660 |
if ($selection) { |
add experimental selection o...
|
661 |
push @sql, $selection; |
fixed some select() join opi...
|
662 |
if ($selection =~ /from\s+(?:\{table\s+)?([^\s\{]+?)\b/) { |
663 |
unshift @$tables, $1; |
|
664 |
} |
|
665 |
unshift @$tables, @{$self->_tables($selection)}; |
|
add experimental selection o...
|
666 |
} |
cleanup
|
667 |
|
added select() all_column op...
|
668 |
# Clumn clause, countains all columns of joined tables |
669 |
elsif ($all_column) { |
|
670 |
|
|
671 |
# Find tables |
|
672 |
my $main_table = $tables->[-1] || ''; |
|
673 |
my %tables; |
|
674 |
foreach my $j (@$join) { |
|
675 |
my $tables = $self->_tables($j); |
|
676 |
foreach my $table (@$tables) { |
|
677 |
$tables{$table} = 1; |
|
add experimental selection o...
|
678 |
} |
679 |
} |
|
added select() all_column op...
|
680 |
delete $tables{$main_table}; |
681 |
my @column_clause; |
|
add experimental selection o...
|
682 |
|
added select() all_column op...
|
683 |
# Column clause of main table |
684 |
push @sql, $self->model($main_table)->column_clause; |
|
685 |
push @sql, ','; |
|
686 |
|
|
687 |
# Column cluase of other tables |
|
688 |
foreach my $table (keys %tables) { |
|
689 |
unshift @$tables, $table; |
|
690 |
push @sql, $self->model($table) |
|
691 |
->column_clause(prefix => "${table}__"); |
|
692 |
push @sql, ','; |
|
693 |
} |
|
694 |
pop @sql if $sql[-1] eq ','; |
|
695 |
} |
|
696 |
|
|
697 |
# Column clause |
|
698 |
elsif (@$columns) { |
|
699 |
foreach my $column (@$columns) { |
|
700 |
unshift @$tables, @{$self->_tables($column)}; |
|
701 |
push @sql, ($column, ','); |
|
702 |
} |
|
703 |
pop @sql if $sql[-1] eq ','; |
|
704 |
} |
|
705 |
|
|
706 |
# "*" is default |
|
707 |
else { push @sql, '*' } |
|
708 |
|
|
709 |
# Table |
|
710 |
unless ($selection) { |
|
add experimental selection o...
|
711 |
push @sql, 'from'; |
fixed some select() join opi...
|
712 |
if ($args{relation}) { |
713 |
my $found = {}; |
|
714 |
foreach my $table (@$tables) { |
|
715 |
push @sql, ($table, ',') unless $found->{$table}; |
|
716 |
$found->{$table} = 1; |
|
717 |
} |
|
packaging one directory
|
718 |
} |
fixed some select() join opi...
|
719 |
else { push @sql, $tables->[-1] } |
720 |
pop @sql if ($sql[-1] || '') eq ','; |
|
packaging one directory
|
721 |
} |
722 |
|
|
fixed some select() join opi...
|
723 |
# Main table |
724 |
croak "Not found table name" unless $tables->[-1]; |
|
725 |
|
|
select() where can't receive...
|
726 |
# Where |
727 |
my $w; |
|
added experimental DBIx::Cus...
|
728 |
if (ref $where eq 'HASH') { |
select() where can't receive...
|
729 |
my $clause = ['and']; |
730 |
push @$clause, "{= $_}" for keys %$where; |
|
cleanup
|
731 |
$w = $self->where(clause => $clause, param => $where); |
added experimental DBIx::Cus...
|
732 |
} |
733 |
elsif (ref $where eq 'DBIx::Custom::Where') { |
|
select() where can't receive...
|
734 |
$w = $where; |
735 |
$where = $w->param; |
|
packaging one directory
|
736 |
} |
737 |
|
|
select() where can't receive...
|
738 |
croak qq{"where" must be hash reference or DBIx::Custom::Where object} |
739 |
unless ref $w eq 'DBIx::Custom::Where'; |
|
740 |
|
|
741 |
# String where |
|
742 |
my $swhere = "$w"; |
|
remove experimental DBIx::Cu...
|
743 |
|
fixed some select() join opi...
|
744 |
# Add table names in where clause |
745 |
unshift @$tables, @{$self->_tables($swhere)}; |
|
remove experimental DBIx::Cu...
|
746 |
|
fixed some select() join opi...
|
747 |
# Push join |
748 |
$self->_push_join(\@sql, $join, $tables); |
|
remove experimental DBIx::Cu...
|
749 |
|
cleanup
|
750 |
# Add where clause |
cleanup
|
751 |
push @sql, $swhere; |
select() where can't receive...
|
752 |
|
cleanup
|
753 |
# Relation(DEPRECATED!); |
754 |
$self->_push_relation(\@sql, $tables, $args{relation}, $swhere eq '' ? 1 : 0); |
|
755 |
|
|
cleanup
|
756 |
# Append statement |
757 |
push @sql, $append if $append; |
|
758 |
|
|
759 |
# SQL |
|
760 |
my $sql = join (' ', @sql); |
|
packaging one directory
|
761 |
|
added experimental sugar met...
|
762 |
# Create query |
cleanup
|
763 |
my $query = $self->create_query($sql); |
added experimental sugar met...
|
764 |
return $query if $args{query}; |
765 |
|
|
packaging one directory
|
766 |
# Execute query |
added auto_filter method
|
767 |
my $result = $self->execute( |
select() where can't receive...
|
768 |
$query, param => $where, filter => $filter, |
769 |
table => $tables); |
|
packaging one directory
|
770 |
|
771 |
return $result; |
|
772 |
} |
|
773 | ||
add experimental update_at()...
|
774 |
our %VALID_SELECT_AT_ARGS |
775 |
= map { $_ => 1 } qw/table column where append relation filter query selection |
|
added select() all_column op...
|
776 |
param primary_key join all_column/; |
add experimental update_at()...
|
777 | |
778 |
sub select_at { |
|
779 |
my ($self, %args) = @_; |
|
780 |
|
|
cleanup
|
781 |
# Check argument names |
add experimental update_at()...
|
782 |
foreach my $name (keys %args) { |
cleanup
|
783 |
croak qq{Argument "$name" is invalid name} |
add experimental update_at()...
|
784 |
unless $VALID_SELECT_AT_ARGS{$name}; |
785 |
} |
|
786 |
|
|
787 |
# Primary key |
|
788 |
my $primary_keys = delete $args{primary_key}; |
|
789 |
$primary_keys = [$primary_keys] unless ref $primary_keys; |
|
790 |
|
|
DBIx::Custom::Model select()...
|
791 |
# Table |
792 |
croak qq{"table" option must be specified} unless $args{table}; |
|
793 |
my $table = ref $args{table} ? $args{table}->[-1] : $args{table}; |
|
794 |
|
|
add experimental update_at()...
|
795 |
# Where clause |
796 |
my $where = {}; |
|
797 |
if (exists $args{where}) { |
|
798 |
my $where_columns = delete $args{where}; |
|
- added experimental DBIx::C...
|
799 |
|
800 |
croak qq{"where" must be constant value or array reference} |
|
801 |
unless !ref $where_columns || ref $where_columns eq 'ARRAY'; |
|
802 |
|
|
add experimental update_at()...
|
803 |
$where_columns = [$where_columns] unless ref $where_columns; |
804 |
|
|
805 |
for(my $i = 0; $i < @$primary_keys; $i ++) { |
|
DBIx::Custom::Model select()...
|
806 |
$where->{$table . '.' . $primary_keys->[$i]} = $where_columns->[$i]; |
add experimental update_at()...
|
807 |
} |
808 |
} |
|
- added experimental DBIx::C...
|
809 |
|
810 |
if (exists $args{param}) { |
|
add experimental update_at()...
|
811 |
my $param = delete $args{param}; |
812 |
for(my $i = 0; $i < @$primary_keys; $i ++) { |
|
- added experimental DBIx::C...
|
813 |
delete $param->{$primary_keys->[$i]}; |
add experimental update_at()...
|
814 |
} |
815 |
} |
|
816 |
|
|
817 |
return $self->select(where => $where, %args); |
|
818 |
} |
|
819 | ||
add feture. all model class ...
|
820 |
sub model { |
821 |
my ($self, $name, $model) = @_; |
|
removed experimental base_ta...
|
822 |
|
823 |
# Set |
|
add feture. all model class ...
|
824 |
if ($model) { |
add models() attribute
|
825 |
$self->models->{$name} = $model; |
removed experimental base_ta...
|
826 |
return $self; |
827 |
} |
|
828 |
|
|
add feture. all model class ...
|
829 |
# Check model existance |
830 |
croak qq{Model "$name" is not included} |
|
add models() attribute
|
831 |
unless $self->models->{$name}; |
removed experimental base_ta...
|
832 |
|
833 |
# Get |
|
add models() attribute
|
834 |
return $self->models->{$name}; |
removed experimental base_ta...
|
835 |
} |
836 | ||
add feture. all model class ...
|
837 |
sub include_model { |
838 |
my ($self, $name_space, $model_infos) = @_; |
|
remove DBIx::Custom::Model
|
839 |
|
add feture. all model class ...
|
840 |
$name_space ||= ''; |
841 |
unless ($model_infos) { |
|
842 |
# Load name space module |
|
843 |
croak qq{"$name_space" is invalid class name} |
|
844 |
if $name_space =~ /[^\w:]/; |
|
845 |
eval "use $name_space"; |
|
846 |
croak qq{Name space module "$name_space.pm" is needed. $@} if $@; |
|
table object call dbi object...
|
847 |
|
add feture. all model class ...
|
848 |
# Search model modules |
849 |
my $path = $INC{"$name_space.pm"}; |
|
850 |
$path =~ s/\.pm$//; |
|
851 |
opendir my $dh, $path |
|
852 |
or croak qq{Can't open directory "$path": $!}; |
|
853 |
$model_infos = []; |
|
854 |
while (my $module = readdir $dh) { |
|
855 |
push @$model_infos, $module |
|
856 |
if $module =~ s/\.pm$//; |
|
removed experimental base_ta...
|
857 |
} |
add feture. all model class ...
|
858 |
|
859 |
close $dh; |
|
860 |
} |
|
861 |
|
|
862 |
foreach my $model_info (@$model_infos) { |
|
863 |
|
|
add experimental DBIx::Custo...
|
864 |
# Model class, name, table |
add feture. all model class ...
|
865 |
my $model_class; |
add experimental DBIx::Custo...
|
866 |
my $model_name; |
867 |
my $model_table; |
|
add feture. all model class ...
|
868 |
if (ref $model_info eq 'HASH') { |
add experimental DBIx::Custo...
|
869 |
$model_class = $model_info->{class}; |
870 |
$model_name = $model_info->{name}; |
|
871 |
$model_table = $model_info->{table}; |
|
872 |
|
|
873 |
$model_name ||= $model_class; |
|
874 |
$model_table ||= $model_name; |
|
add feture. all model class ...
|
875 |
} |
add experimental DBIx::Custo...
|
876 |
else { $model_class =$model_name = $model_table = $model_info } |
add feture. all model class ...
|
877 |
my $mclass = "${name_space}::$model_class"; |
table object call dbi object...
|
878 |
|
removed experimental base_ta...
|
879 |
# Load |
add feture. all model class ...
|
880 |
croak qq{"$mclass" is invalid class name} |
881 |
if $mclass =~ /[^\w:]/; |
|
882 |
unless ($mclass->can('isa')) { |
|
883 |
eval "use $mclass"; |
|
removed experimental base_ta...
|
884 |
croak $@ if $@; |
885 |
} |
|
table object call dbi object...
|
886 |
|
removed experimental base_ta...
|
887 |
# Instantiate |
add feture. all model class ...
|
888 |
my $model = $mclass->new(dbi => $self); |
add experimental DBIx::Custo...
|
889 |
$model->name($model_name) unless $model->name; |
890 |
$model->table($model_table) unless $model->table; |
|
removed experimental DBIx::C...
|
891 |
|
removed experimental base_ta...
|
892 |
# Set |
add experimental DBIx::Custo...
|
893 |
$self->model($model->name, $model); |
add experimental DBIx::Custo...
|
894 |
|
895 |
# Apply filter |
|
all filter can receive array...
|
896 |
croak "${name_space}::$model_class filter must be array reference" |
897 |
unless ref $model->filter eq 'ARRAY'; |
|
898 |
$self->apply_filter($model->table, @{$model->filter}); |
|
table object call dbi object...
|
899 |
} |
removed experimental base_ta...
|
900 |
return $self; |
remove DBIx::Custom::Model
|
901 |
} |
902 | ||
add experimental setup_model...
|
903 |
sub setup_model { |
904 |
my $self = shift; |
|
905 |
|
|
906 |
$self->each_column( |
|
907 |
sub { |
|
908 |
my ($self, $table, $column, $column_info) = @_; |
|
909 |
|
|
910 |
if (my $model = $self->models->{$table}) { |
|
911 |
push @{$model->columns}, $column; |
|
912 |
} |
|
913 |
} |
|
914 |
); |
|
add experimental DBIx::Custo...
|
915 |
return $self; |
add experimental setup_model...
|
916 |
} |
917 | ||
cleanup
|
918 |
our %VALID_UPDATE_ARGS |
update pod
|
919 |
= map { $_ => 1 } qw/table param where append filter |
920 |
allow_update_all query/; |
|
cleanup
|
921 | |
922 |
sub update { |
|
923 |
my ($self, %args) = @_; |
|
version 0.0901
|
924 |
|
cleanup
|
925 |
# Check argument names |
cleanup
|
926 |
foreach my $name (keys %args) { |
cleanup
|
927 |
croak qq{Argument "$name" is invalid name} |
cleanup
|
928 |
unless $VALID_UPDATE_ARGS{$name}; |
removed reconnect method
|
929 |
} |
added cache_method attribute
|
930 |
|
cleanup
|
931 |
# Arguments |
932 |
my $table = $args{table} || ''; |
|
added table not specified ex...
|
933 |
croak qq{"table" option must be specified} unless $table; |
cleanup
|
934 |
my $param = $args{param} || {}; |
935 |
my $where = $args{where} || {}; |
|
select() where can't receive...
|
936 |
my $append = $args{append} || ''; |
cleanup
|
937 |
my $filter = $args{filter}; |
938 |
my $allow_update_all = $args{allow_update_all}; |
|
version 0.0901
|
939 |
|
cleanup
|
940 |
# Update keys |
select() where can't receive...
|
941 |
my @clumns = keys %$param; |
942 | ||
943 |
# Columns |
|
944 |
my @columns; |
|
- remaned experimental safty...
|
945 |
my $safety = $self->safety_character; |
select() where can't receive...
|
946 |
foreach my $column (keys %$param) { |
947 |
croak qq{"$column" is not safety column name} |
|
- remaned experimental safty...
|
948 |
unless $column =~ /^[$safety\.]+$/; |
select() where can't receive...
|
949 |
push @columns, $column; |
950 |
} |
|
951 |
|
|
cleanup
|
952 |
# Update clause |
select() where can't receive...
|
953 |
my $update_clause = '{update_param ' . join(' ', @clumns) . '}'; |
improved delete() and update...
|
954 | |
955 |
# Where |
|
956 |
my $w; |
|
957 |
if (ref $where eq 'HASH') { |
|
958 |
my $clause = ['and']; |
|
959 |
push @$clause, "{= $_}" for keys %$where; |
|
960 |
$w = $self->where; |
|
961 |
$w->clause($clause); |
|
962 |
$w->param($where); |
|
963 |
} |
|
select() where can't receive...
|
964 |
elsif (ref $where eq 'DBIx::Custom::Where') { |
965 |
$w = $where; |
|
966 |
$where = $w->param; |
|
967 |
} |
|
removed experimental registe...
|
968 |
|
improved delete() and update...
|
969 |
croak qq{"where" must be hash refernce or DBIx::Custom::Where object} |
970 |
unless ref $w eq 'DBIx::Custom::Where'; |
|
removed reconnect method
|
971 |
|
select() where can't receive...
|
972 |
# String where |
973 |
my $swhere = "$w"; |
|
improved delete() and update...
|
974 |
|
975 |
croak qq{"where" must be specified} |
|
select() where can't receive...
|
976 |
if "$swhere" eq '' && !$allow_update_all; |
removed reconnect method
|
977 |
|
cleanup
|
978 |
# SQL stack |
979 |
my @sql; |
|
980 |
|
|
981 |
# Update |
|
982 |
push @sql, "update $table $update_clause $swhere"; |
|
983 |
push @sql, $append if $append; |
|
removed reconnect method
|
984 |
|
cleanup
|
985 |
# Rearrange parameters |
improved delete() and update...
|
986 |
foreach my $wkey (keys %$where) { |
removed reconnect method
|
987 |
|
cleanup
|
988 |
if (exists $param->{$wkey}) { |
989 |
$param->{$wkey} = [$param->{$wkey}] |
|
990 |
unless ref $param->{$wkey} eq 'ARRAY'; |
|
991 |
|
|
992 |
push @{$param->{$wkey}}, $where->{$wkey}; |
|
993 |
} |
|
994 |
else { |
|
995 |
$param->{$wkey} = $where->{$wkey}; |
|
996 |
} |
|
removed reconnect method
|
997 |
} |
cleanup
|
998 |
|
cleanup
|
999 |
# SQL |
1000 |
my $sql = join(' ', @sql); |
|
1001 |
|
|
added experimental sugar met...
|
1002 |
# Create query |
cleanup
|
1003 |
my $query = $self->create_query($sql); |
added experimental sugar met...
|
1004 |
return $query if $args{query}; |
1005 |
|
|
cleanup
|
1006 |
# Execute query |
added experimental sugar met...
|
1007 |
my $ret_val = $self->execute($query, param => $param, |
added auto_filter method
|
1008 |
filter => $filter, |
renamed auto_filter to apply...
|
1009 |
table => $table); |
cleanup
|
1010 |
|
1011 |
return $ret_val; |
|
removed reconnect method
|
1012 |
} |
1013 | ||
cleanup
|
1014 |
sub update_all { shift->update(allow_update_all => 1, @_) }; |
1015 | ||
add experimental update_at()...
|
1016 |
our %VALID_UPDATE_AT_ARGS |
update pod
|
1017 |
= map { $_ => 1 } qw/table param where append filter |
1018 |
query primary_key param/; |
|
add experimental update_at()...
|
1019 | |
1020 |
sub update_at { |
|
1021 |
my ($self, %args) = @_; |
|
1022 |
|
|
cleanup
|
1023 |
# Check argument names |
add experimental update_at()...
|
1024 |
foreach my $name (keys %args) { |
cleanup
|
1025 |
croak qq{Argument "$name" is invalid name} |
add experimental update_at()...
|
1026 |
unless $VALID_UPDATE_AT_ARGS{$name}; |
1027 |
} |
|
1028 |
|
|
1029 |
# Primary key |
|
1030 |
my $primary_keys = delete $args{primary_key}; |
|
1031 |
$primary_keys = [$primary_keys] unless ref $primary_keys; |
|
1032 |
|
|
1033 |
# Where clause |
|
1034 |
my $where = {}; |
|
1035 |
my $param = {}; |
|
1036 |
|
|
1037 |
if (exists $args{where}) { |
|
1038 |
my $where_columns = delete $args{where}; |
|
1039 |
$where_columns = [$where_columns] unless ref $where_columns; |
|
- added experimental DBIx::C...
|
1040 | |
1041 |
croak qq{"where" must be constant value or array reference} |
|
1042 |
unless !ref $where_columns || ref $where_columns eq 'ARRAY'; |
|
add experimental update_at()...
|
1043 |
|
1044 |
for(my $i = 0; $i < @$primary_keys; $i ++) { |
|
1045 |
$where->{$primary_keys->[$i]} = $where_columns->[$i]; |
|
1046 |
} |
|
1047 |
} |
|
- added experimental DBIx::C...
|
1048 |
|
1049 |
if (exists $args{param}) { |
|
add experimental update_at()...
|
1050 |
$param = delete $args{param}; |
1051 |
for(my $i = 0; $i < @$primary_keys; $i ++) { |
|
- added experimental DBIx::C...
|
1052 |
delete $param->{$primary_keys->[$i]}; |
add experimental update_at()...
|
1053 |
} |
1054 |
} |
|
1055 |
|
|
1056 |
return $self->update(where => $where, param => $param, %args); |
|
1057 |
} |
|
1058 | ||
remove experimental DBIx::Cu...
|
1059 |
sub update_param { |
1060 |
my ($self, $param) = @_; |
|
1061 |
|
|
added experimental update_pa...
|
1062 |
# Update parameter tag |
remove experimental DBIx::Cu...
|
1063 |
my @tag; |
1064 |
push @tag, '{update_param'; |
|
- remaned experimental safty...
|
1065 |
my $safety = $self->safety_character; |
remove experimental DBIx::Cu...
|
1066 |
foreach my $column (keys %$param) { |
added experimental update_pa...
|
1067 |
croak qq{"$column" is not safety column name} |
- remaned experimental safty...
|
1068 |
unless $column =~ /^[$safety\.]+$/; |
remove experimental DBIx::Cu...
|
1069 |
push @tag, $column; |
1070 |
} |
|
1071 |
push @tag, '}'; |
|
1072 |
|
|
1073 |
return join ' ', @tag; |
|
1074 |
} |
|
1075 | ||
cleanup
|
1076 |
sub where { |
select() where can't receive...
|
1077 |
my $self = shift; |
1078 | ||
1079 |
return DBIx::Custom::Where->new( |
|
1080 |
query_builder => $self->query_builder, |
|
- remaned experimental safty...
|
1081 |
safety_character => $self->safety_character, |
cleanup
|
1082 |
@_ |
select() where can't receive...
|
1083 |
); |
cleanup
|
1084 |
} |
added experimental DBIx::Cus...
|
1085 | |
added experimental not_exist...
|
1086 |
sub _bind { |
cleanup
|
1087 |
my ($self, $params, $columns, $filter) = @_; |
removed reconnect method
|
1088 |
|
cleanup
|
1089 |
# bind values |
added experimental not_exist...
|
1090 |
my @bind; |
add tests
|
1091 |
|
removed reconnect method
|
1092 |
# Build bind values |
1093 |
my $count = {}; |
|
added experimental not_exist...
|
1094 |
my $not_exists = {}; |
cleanup
|
1095 |
foreach my $column (@$columns) { |
removed reconnect method
|
1096 |
|
1097 |
# Value |
|
added experimental not_exist...
|
1098 |
my $value; |
1099 |
if(ref $params->{$column} eq 'ARRAY') { |
|
1100 |
my $i = $count->{$column} || 0; |
|
1101 |
$i += $not_exists->{$column} || 0; |
|
1102 |
my $found; |
|
1103 |
for (my $k = $i; $i < @{$params->{$column}}; $k++) { |
|
1104 |
if (ref $params->{$column}->[$k] eq 'DBIx::Custom::NotExists') { |
|
1105 |
$not_exists->{$column}++; |
|
1106 |
} |
|
1107 |
else { |
|
1108 |
$value = $params->{$column}->[$k]; |
|
1109 |
$found = 1; |
|
1110 |
last |
|
1111 |
} |
|
1112 |
} |
|
1113 |
next unless $found; |
|
1114 |
} |
|
1115 |
else { $value = $params->{$column} } |
|
removed reconnect method
|
1116 |
|
cleanup
|
1117 |
# Filter |
1118 |
my $f = $filter->{$column} || $self->{default_out_filter} || ''; |
|
cleanup
|
1119 |
|
added experimental not_exist...
|
1120 |
push @bind, $f ? $f->($value) : $value; |
removed reconnect method
|
1121 |
|
1122 |
# Count up |
|
1123 |
$count->{$column}++; |
|
1124 |
} |
|
1125 |
|
|
added experimental not_exist...
|
1126 |
return \@bind; |
removed reconnect method
|
1127 |
} |
1128 | ||
EXPERIMETAL fork safety impl...
|
1129 |
sub _connect { |
1130 |
my $self = shift; |
|
1131 |
|
|
1132 |
# Attributes |
|
1133 |
my $data_source = $self->data_source; |
|
1134 |
croak qq{"data_source" must be specified to connect()"} |
|
1135 |
unless $data_source; |
|
1136 |
my $user = $self->user; |
|
1137 |
my $password = $self->password; |
|
1138 |
my $dbi_option = {%{$self->dbi_options}, %{$self->dbi_option}}; |
|
1139 |
|
|
1140 |
# Connect |
|
1141 |
my $dbh = eval {DBI->connect( |
|
1142 |
$data_source, |
|
1143 |
$user, |
|
1144 |
$password, |
|
1145 |
{ |
|
1146 |
%{$self->default_dbi_option}, |
|
1147 |
%$dbi_option |
|
1148 |
} |
|
1149 |
)}; |
|
1150 |
|
|
1151 |
# Connect error |
|
1152 |
croak $@ if $@; |
|
1153 |
|
|
1154 |
return $dbh; |
|
1155 |
} |
|
1156 | ||
cleanup
|
1157 |
sub _croak { |
1158 |
my ($self, $error, $append) = @_; |
|
1159 |
$append ||= ""; |
|
1160 |
|
|
1161 |
# Verbose |
|
1162 |
if ($Carp::Verbose) { croak $error } |
|
1163 |
|
|
1164 |
# Not verbose |
|
1165 |
else { |
|
1166 |
|
|
1167 |
# Remove line and module infromation |
|
1168 |
my $at_pos = rindex($error, ' at '); |
|
1169 |
$error = substr($error, 0, $at_pos); |
|
1170 |
$error =~ s/\s+$//; |
|
1171 |
|
|
1172 |
croak "$error$append"; |
|
1173 |
} |
|
1174 |
} |
|
1175 | ||
added select() all_column op...
|
1176 |
sub _need_tables { |
1177 |
my ($self, $tree, $need_tables, $tables) = @_; |
|
1178 |
|
|
1179 |
foreach my $table (@$tables) { |
|
1180 |
|
|
1181 |
if ($tree->{$table}) { |
|
1182 |
$need_tables->{$table} = 1; |
|
1183 |
$self->_need_tables($tree, $need_tables, [$tree->{$table}{parent}]) |
|
1184 |
} |
|
1185 |
} |
|
1186 |
} |
|
1187 | ||
remove experimental DBIx::Cu...
|
1188 |
sub _tables { |
1189 |
my ($self, $source) = @_; |
|
1190 |
|
|
1191 |
my $tables = []; |
|
1192 |
|
|
- remaned experimental safty...
|
1193 |
my $safety_character = $self->safety_character; |
remove experimental DBIx::Cu...
|
1194 |
|
- remaned experimental safty...
|
1195 |
while ($source =~ /\b($safety_character+)\./g) { |
remove experimental DBIx::Cu...
|
1196 |
push @$tables, $1; |
1197 |
} |
|
1198 |
|
|
1199 |
return $tables; |
|
1200 |
} |
|
1201 | ||
fixed some select() join opi...
|
1202 |
sub _push_join { |
1203 |
my ($self, $sql, $join, $join_tables) = @_; |
|
1204 |
|
|
1205 |
return unless @$join; |
|
1206 |
|
|
1207 |
my $tree = {}; |
|
1208 |
|
|
1209 |
for (my $i = 0; $i < @$join; $i++) { |
|
1210 |
|
|
1211 |
my $join_clause = $join->[$i]; |
|
1212 |
|
|
- added experimental DBIx::C...
|
1213 |
if ($join_clause =~ /\s([^\.\s]+?)\..+\s([^\.\s]+?)\..+?$/) { |
fixed some select() join opi...
|
1214 |
|
1215 |
my $table1 = $1; |
|
1216 |
my $table2 = $2; |
|
1217 |
|
|
1218 |
croak qq{right side table of "$join_clause" must be uniq} |
|
1219 |
if exists $tree->{$table2}; |
|
1220 |
|
|
1221 |
$tree->{$table2} |
|
1222 |
= {position => $i, parent => $table1, join => $join_clause}; |
|
1223 |
} |
|
1224 |
else { |
|
1225 |
croak qq{join "$join_clause" must be two table name}; |
|
1226 |
} |
|
1227 |
} |
|
1228 |
|
|
1229 |
my $need_tables = {}; |
|
1230 |
$self->_need_tables($tree, $need_tables, $join_tables); |
|
1231 |
|
|
1232 |
my @need_tables = sort { $tree->{$a}{position} <=> $tree->{$b}{position} } keys %$need_tables; |
|
cleanup
|
1233 | |
fixed some select() join opi...
|
1234 |
foreach my $need_table (@need_tables) { |
1235 |
push @$sql, $tree->{$need_table}{join}; |
|
1236 |
} |
|
1237 |
} |
|
cleanup
|
1238 | |
cleanup
|
1239 |
# DEPRECATED! |
cleanup
|
1240 |
__PACKAGE__->attr( |
1241 |
dbi_options => sub { {} }, |
|
1242 |
filter_check => 1 |
|
1243 |
); |
|
renamed dbi_options to dbi_o...
|
1244 | |
cleanup
|
1245 |
# DEPRECATED! |
cleanup
|
1246 |
sub default_bind_filter { |
1247 |
my $self = shift; |
|
1248 |
|
|
1249 |
if (@_) { |
|
1250 |
my $fname = $_[0]; |
|
1251 |
|
|
1252 |
if (@_ && !$fname) { |
|
1253 |
$self->{default_out_filter} = undef; |
|
1254 |
} |
|
1255 |
else { |
|
many changed
|
1256 |
croak qq{Filter "$fname" is not registered} |
cleanup
|
1257 |
unless exists $self->filters->{$fname}; |
1258 |
|
|
1259 |
$self->{default_out_filter} = $self->filters->{$fname}; |
|
1260 |
} |
|
1261 |
return $self; |
|
1262 |
} |
|
1263 |
|
|
1264 |
return $self->{default_out_filter}; |
|
1265 |
} |
|
1266 | ||
cleanup
|
1267 |
# DEPRECATED! |
cleanup
|
1268 |
sub default_fetch_filter { |
1269 |
my $self = shift; |
|
1270 |
|
|
1271 |
if (@_) { |
|
many changed
|
1272 |
my $fname = $_[0]; |
1273 | ||
cleanup
|
1274 |
if (@_ && !$fname) { |
1275 |
$self->{default_in_filter} = undef; |
|
1276 |
} |
|
1277 |
else { |
|
many changed
|
1278 |
croak qq{Filter "$fname" is not registered} |
cleanup
|
1279 |
unless exists $self->filters->{$fname}; |
1280 |
|
|
1281 |
$self->{default_in_filter} = $self->filters->{$fname}; |
|
1282 |
} |
|
1283 |
|
|
1284 |
return $self; |
|
1285 |
} |
|
1286 |
|
|
many changed
|
1287 |
return $self->{default_in_filter}; |
cleanup
|
1288 |
} |
1289 | ||
cleanup
|
1290 |
# DEPRECATED! |
renamed DBIx::Custom::TagPro...
|
1291 |
sub register_tag_processor { |
1292 |
return shift->query_builder->register_tag_processor(@_); |
|
1293 |
} |
|
1294 | ||
cleanup
|
1295 |
# DEPRECATED! |
1296 |
sub _push_relation { |
|
1297 |
my ($self, $sql, $tables, $relation, $need_where) = @_; |
|
1298 |
|
|
1299 |
if (keys %{$relation || {}}) { |
|
1300 |
push @$sql, $need_where ? 'where' : 'and'; |
|
1301 |
foreach my $rcolumn (keys %$relation) { |
|
1302 |
my $table1 = (split (/\./, $rcolumn))[0]; |
|
1303 |
my $table2 = (split (/\./, $relation->{$rcolumn}))[0]; |
|
1304 |
push @$tables, ($table1, $table2); |
|
1305 |
push @$sql, ("$rcolumn = " . $relation->{$rcolumn}, 'and'); |
|
1306 |
} |
|
1307 |
} |
|
1308 |
pop @$sql if $sql->[-1] eq 'and'; |
|
1309 |
} |
|
1310 | ||
1311 |
# DEPRECATED! |
|
1312 |
sub _add_relation_table { |
|
cleanup
|
1313 |
my ($self, $tables, $relation) = @_; |
cleanup
|
1314 |
|
1315 |
if (keys %{$relation || {}}) { |
|
1316 |
foreach my $rcolumn (keys %$relation) { |
|
1317 |
my $table1 = (split (/\./, $rcolumn))[0]; |
|
1318 |
my $table2 = (split (/\./, $relation->{$rcolumn}))[0]; |
|
1319 |
my $table1_exists; |
|
1320 |
my $table2_exists; |
|
1321 |
foreach my $table (@$tables) { |
|
1322 |
$table1_exists = 1 if $table eq $table1; |
|
1323 |
$table2_exists = 1 if $table eq $table2; |
|
1324 |
} |
|
1325 |
unshift @$tables, $table1 unless $table1_exists; |
|
1326 |
unshift @$tables, $table2 unless $table2_exists; |
|
1327 |
} |
|
1328 |
} |
|
1329 |
} |
|
1330 | ||
fixed DBIx::Custom::QueryBui...
|
1331 |
1; |
1332 | ||
removed reconnect method
|
1333 |
=head1 NAME |
1334 | ||
- remaned experimental safty...
|
1335 |
DBIx::Custom - Useful database access, respecting SQL! |
removed reconnect method
|
1336 | |
1337 |
=head1 SYNOPSYS |
|
cleanup
|
1338 | |
renamed build_query to creat...
|
1339 |
use DBIx::Custom; |
- remaned experimental safty...
|
1340 |
|
1341 |
# Connect |
|
1342 |
my $dbi = DBIx::Custom->connect( |
|
1343 |
data_source => "dbi:mysql:database=dbname", |
|
1344 |
user => 'ken', |
|
1345 |
password => '!LFKD%$&', |
|
1346 |
dbi_option => {mysql_enable_utf8 => 1} |
|
1347 |
); |
|
cleanup
|
1348 | |
removed reconnect method
|
1349 |
# Insert |
- remaned experimental safty...
|
1350 |
$dbi->insert( |
1351 |
table => 'book', |
|
1352 |
param => {title => 'Perl', author => 'Ken'} |
|
1353 |
); |
|
removed reconnect method
|
1354 |
|
1355 |
# Update |
|
- remaned experimental safty...
|
1356 |
$dbi->update( |
1357 |
table => 'book', |
|
1358 |
param => {title => 'Perl', author => 'Ken'}, |
|
1359 |
where => {id => 5}, |
|
1360 |
); |
|
removed reconnect method
|
1361 |
|
1362 |
# Delete |
|
- remaned experimental safty...
|
1363 |
$dbi->delete( |
1364 |
table => 'book', |
|
1365 |
where => {author => 'Ken'}, |
|
1366 |
); |
|
cleanup
|
1367 | |
removed reconnect method
|
1368 |
# Select |
renamed fetch_rows to fetch_...
|
1369 |
my $result = $dbi->select( |
added insert, update, update...
|
1370 |
table => 'book', |
update document
|
1371 |
where => {author => 'Ken'}, |
added commit method
|
1372 |
); |
cleanup
|
1373 | |
- remaned experimental safty...
|
1374 |
# Select, more complex |
1375 |
my $result = $dbi->select( |
|
1376 |
table => 'book', |
|
1377 |
column => [ |
|
1378 |
'book.author as book__author', |
|
1379 |
'company.name as company__name' |
|
1380 |
], |
|
1381 |
where => {'book.author' => 'Ken'}, |
|
1382 |
join => ['left outer join company on book.company_id = company.id'], |
|
1383 |
append => 'order by id limit 5' |
|
removed reconnect method
|
1384 |
); |
- remaned experimental safty...
|
1385 |
|
removed register_format()
|
1386 |
# Fetch |
1387 |
while (my $row = $result->fetch) { |
|
- remaned experimental safty...
|
1388 |
|
removed register_format()
|
1389 |
} |
1390 |
|
|
- remaned experimental safty...
|
1391 |
# Fetch as hash |
removed register_format()
|
1392 |
while (my $row = $result->fetch_hash) { |
1393 |
|
|
1394 |
} |
|
1395 |
|
|
- remaned experimental safty...
|
1396 |
# Execute SQL with parameter. |
1397 |
$dbi->execute( |
|
1398 |
"select id from book where {= author} and {like title}", |
|
1399 |
param => {author => 'ken', title => '%Perl%'} |
|
1400 |
); |
|
1401 |
|
|
renamed update tag to update...
|
1402 |
=head1 DESCRIPTIONS |
removed reconnect method
|
1403 | |
- remaned experimental safty...
|
1404 |
L<DBIx::Custom> is L<DBI> wrapper module. |
1405 | ||
1406 |
=head1 FEATURES |
|
removed reconnect method
|
1407 | |
- remaned experimental safty...
|
1408 |
=over 4 |
removed reconnect method
|
1409 | |
- remaned experimental safty...
|
1410 |
=item * |
removed reconnect method
|
1411 | |
- remaned experimental safty...
|
1412 |
There are many basic methods to execute various queries. |
1413 |
C<insert()>, C<update()>, C<update_all()>,C<delete()>, |
|
1414 |
C<delete_all()>, C<select()>, |
|
1415 |
C<insert_at()>, C<update_at()>, |
|
1416 |
C<delete_at()>, C<select_at()>, C<execute()> |
|
removed reconnect method
|
1417 | |
- remaned experimental safty...
|
1418 |
=item * |
1419 | ||
1420 |
Filter when data is send or receive. |
|
1421 | ||
1422 |
=item * |
|
1423 | ||
1424 |
Data filtering system |
|
1425 | ||
1426 |
=item * |
|
1427 | ||
1428 |
Model support. |
|
1429 | ||
1430 |
=item * |
|
1431 | ||
1432 |
Generate where clause dinamically. |
|
1433 | ||
1434 |
=item * |
|
1435 | ||
1436 |
Generate join clause dinamically. |
|
1437 | ||
1438 |
=back |
|
pod fix
|
1439 | |
1440 |
=head1 GUIDE |
|
1441 | ||
- remaned experimental safty...
|
1442 |
L<DBIx::Custom::Guide> - L<DBIx::Custom> Guide |
pod fix
|
1443 | |
- remaned experimental safty...
|
1444 |
=head1 Wiki |
pod fix
|
1445 | |
- remaned experimental safty...
|
1446 |
L<DBIx::Custom Wiki|https://github.com/yuki-kimoto/DBIx-Custom/wiki> |
updated document
|
1447 | |
update document
|
1448 |
=head1 ATTRIBUTES |
packaging one directory
|
1449 | |
cleanup
|
1450 |
=head2 C<cache> |
packaging one directory
|
1451 | |
cleanup
|
1452 |
my $cache = $dbi->cache; |
1453 |
$dbi = $dbi->cache(1); |
|
removed DESTROY method(not b...
|
1454 | |
update pod
|
1455 |
Enable caching L<DBIx::Custom::Query>, |
- remaned experimental safty...
|
1456 |
default to 1. |
packaging one directory
|
1457 | |
removed DBIx::Custom commit ...
|
1458 |
=head2 C<data_source> |
packaging one directory
|
1459 | |
cleanup
|
1460 |
my $data_source = $dbi->data_source; |
cleanup
|
1461 |
$dbi = $dbi->data_source("DBI:mysql:database=dbname"); |
removed DESTROY method(not b...
|
1462 | |
- remaned experimental safty...
|
1463 |
Data source, used when C<connect()> is executed. |
removed DESTROY method(not b...
|
1464 | |
renamed dbi_options to dbi_o...
|
1465 |
=head2 C<dbi_option> |
added dbi_options attribute
|
1466 | |
renamed dbi_options to dbi_o...
|
1467 |
my $dbi_option = $dbi->dbi_option; |
- remaned experimental safty...
|
1468 |
$dbi = $dbi->dbi_option($dbi_option); |
add default_dbi_option()
|
1469 | |
- remaned experimental safty...
|
1470 |
L<DBI> option, used when C<connect()> is executed. |
1471 |
Each value in option override the value of C<default_dbi_option>. |
|
add default_dbi_option()
|
1472 | |
1473 |
=head2 C<default_dbi_option> |
|
1474 | ||
1475 |
my $default_dbi_option = $dbi->default_dbi_option; |
|
1476 |
$dbi = $dbi->default_dbi_option($default_dbi_option); |
|
1477 | ||
- remaned experimental safty...
|
1478 |
L<DBI> default option, used when C<connect()> is executed, |
1479 |
default to the following values. |
|
add default_dbi_option()
|
1480 | |
- remaned experimental safty...
|
1481 |
{ |
1482 |
RaiseError => 1, |
|
1483 |
PrintError => 0, |
|
1484 |
AutoCommit => 1, |
|
1485 |
} |
|
packaging one directory
|
1486 | |
update pod
|
1487 |
You should not change C<AutoCommit> value directly, |
1488 |
the value is used to check if the process is in transaction. |
|
EXPERIMETAL fork safety impl...
|
1489 | |
cleanup
|
1490 |
=head2 C<filters> |
bind_filter argument is chan...
|
1491 | |
cleanup
|
1492 |
my $filters = $dbi->filters; |
1493 |
$dbi = $dbi->filters(\%filters); |
|
packaging one directory
|
1494 | |
- remaned experimental safty...
|
1495 |
Filters, registered by C<register_filter()>. |
add models() attribute
|
1496 | |
update pod
|
1497 |
=head2 C<models> EXPERIMENTAL |
add models() attribute
|
1498 | |
1499 |
my $models = $dbi->models; |
|
1500 |
$dbi = $dbi->models(\%models); |
|
1501 | ||
- remaned experimental safty...
|
1502 |
Models, included by C<include_model()>. |
add models() attribute
|
1503 | |
cleanup
|
1504 |
=head2 C<password> |
1505 | ||
1506 |
my $password = $dbi->password; |
|
1507 |
$dbi = $dbi->password('lkj&le`@s'); |
|
1508 | ||
- remaned experimental safty...
|
1509 |
Password, used when C<connect()> is executed. |
update document
|
1510 | |
renamed update tag to update...
|
1511 |
=head2 C<query_builder> |
added commit method
|
1512 | |
renamed update tag to update...
|
1513 |
my $sql_class = $dbi->query_builder; |
1514 |
$dbi = $dbi->query_builder(DBIx::Custom::QueryBuilder->new); |
|
added commit method
|
1515 | |
- remaned experimental safty...
|
1516 |
Query builder, default to L<DBIx::Custom::QueryBuilder> object. |
cleanup
|
1517 | |
cleanup
|
1518 |
=head2 C<result_class> |
cleanup
|
1519 | |
cleanup
|
1520 |
my $result_class = $dbi->result_class; |
1521 |
$dbi = $dbi->result_class('DBIx::Custom::Result'); |
|
cleanup
|
1522 | |
- remaned experimental safty...
|
1523 |
Result class, default to L<DBIx::Custom::Result>. |
cleanup
|
1524 | |
update pod
|
1525 |
=head2 C<safety_character> EXPERIMENTAL |
update pod
|
1526 | |
- remaned experimental safty...
|
1527 |
my $safety_character = $self->safety_character; |
cleanup
|
1528 |
$dbi = $self->safety_character($character); |
update pod
|
1529 | |
update pod
|
1530 |
Regex of safety character for table and column name, default to '\w'. |
cleanup
|
1531 |
Note that you don't have to specify like '[\w]'. |
update pod
|
1532 | |
cleanup
|
1533 |
=head2 C<user> |
cleanup
|
1534 | |
cleanup
|
1535 |
my $user = $dbi->user; |
1536 |
$dbi = $dbi->user('Ken'); |
|
cleanup
|
1537 | |
cleanup
|
1538 |
User name, used when C<connect()> is executed. |
update pod
|
1539 | |
cleanup
|
1540 |
=head1 METHODS |
added commit method
|
1541 | |
cleanup
|
1542 |
L<DBIx::Custom> inherits all methods from L<Object::Simple> |
cleanup
|
1543 |
and use all methods of L<DBI> |
cleanup
|
1544 |
and implements the following new ones. |
added check_filter attribute
|
1545 | |
update pod
|
1546 |
=head2 C<apply_filter> EXPERIMENTAL |
added auto_filter method
|
1547 | |
renamed auto_filter to apply...
|
1548 |
$dbi->apply_filter( |
cleanup
|
1549 |
'book', |
update pod
|
1550 |
'issue_date' => { |
1551 |
out => 'tp_to_date', |
|
1552 |
in => 'date_to_tp', |
|
1553 |
end => 'tp_to_displaydate' |
|
1554 |
}, |
|
1555 |
'write_date' => { |
|
1556 |
out => 'tp_to_date', |
|
1557 |
in => 'date_to_tp', |
|
1558 |
end => 'tp_to_displaydate' |
|
1559 |
} |
|
added auto_filter method
|
1560 |
); |
1561 | ||
update pod
|
1562 |
Apply filter to columns. |
1563 |
C<out> filter is executed before data is send to database. |
|
1564 |
C<in> filter is executed after a row is fetch. |
|
1565 |
C<end> filter is execute after C<in> filter is executed. |
|
1566 | ||
1567 |
Filter is applied to the follwoing tree column name pattern. |
|
cleanup
|
1568 | |
update pod
|
1569 |
PETTERN EXAMPLE |
1570 |
1. Column : author |
|
1571 |
2. Table.Column : book.author |
|
1572 |
3. Table__Column : book__author |
|
fix bug : filter can't over...
|
1573 | |
update pod
|
1574 |
If column name is duplicate with other table, |
1575 |
Main filter specified by C<table> option is used. |
|
1576 | ||
1577 |
You can set multiple filters at once. |
|
1578 | ||
1579 |
$dbi->apply_filter( |
|
1580 |
'book', |
|
1581 |
[qw/issue_date write_date/] => { |
|
1582 |
out => 'tp_to_date', |
|
1583 |
in => 'date_to_tp', |
|
1584 |
end => 'tp_to_displaydate' |
|
1585 |
} |
|
1586 |
); |
|
fix bug : filter can't over...
|
1587 | |
update pod
|
1588 |
=head2 C<cache_method> |
1589 | ||
1590 |
$dbi = $dbi->cache_method(\&cache_method); |
|
1591 |
$cache_method = $dbi->cache_method |
|
1592 | ||
update pod
|
1593 |
Method to set and get cache. |
1594 |
Default to the following one. |
|
1595 | ||
1596 |
sub { |
|
1597 |
my $self = shift; |
|
1598 |
|
|
1599 |
$self->{_cached} ||= {}; |
|
1600 |
|
|
1601 |
if (@_ > 1) { |
|
1602 |
$self->{_cached}{$_[0]} = $_[1]; |
|
1603 |
} |
|
1604 |
else { |
|
1605 |
return $self->{_cached}{$_[0]}; |
|
1606 |
} |
|
1607 |
} |
|
update pod
|
1608 | |
removed DBIx::Custom commit ...
|
1609 |
=head2 C<connect> |
packaging one directory
|
1610 | |
update pod
|
1611 |
my $dbi = DBIx::Custom->connect( |
1612 |
data_source => "dbi:mysql:database=dbname", |
|
1613 |
user => 'ken', |
|
1614 |
password => '!LFKD%$&', |
|
1615 |
dbi_option => {mysql_enable_utf8 => 1} |
|
1616 |
); |
|
1617 | ||
1618 |
Connect to the database and create a new L<DBIx::Custom> object. |
|
bind_filter argument is chan...
|
1619 | |
renamed build_query to creat...
|
1620 |
L<DBIx::Custom> is a wrapper of L<DBI>. |
cleanup
|
1621 |
C<AutoCommit> and C<RaiseError> options are true, |
update pod
|
1622 |
and C<PrintError> option is false by default. |
packaging one directory
|
1623 | |
cleanup
|
1624 |
=head2 C<create_query> |
1625 |
|
|
1626 |
my $query = $dbi->create_query( |
|
update pod
|
1627 |
"insert into book {insert_param title author};"; |
cleanup
|
1628 |
); |
update document
|
1629 | |
update pod
|
1630 |
Create L<DBIx::Custom::Query> object. |
1631 | ||
cleanup
|
1632 |
If you want to get high performance, |
update pod
|
1633 |
create L<DBIx::Custom::Query> object and execute the query by C<execute()> |
1634 |
instead of other methods, such as C<insert>, C<update>. |
|
bind_filter argument is chan...
|
1635 | |
cleanup
|
1636 |
$dbi->execute($query, {author => 'Ken', title => '%Perl%'}); |
version 0.0901
|
1637 | |
EXPERIMETAL fork safety impl...
|
1638 |
=head2 C<dbh> |
1639 | ||
1640 |
my $dbh = $dbi->dbh; |
|
1641 |
$dbi = $dbi->dbh($dbh); |
|
1642 | ||
1643 |
Get and set database handle of L<DBI>. |
|
1644 | ||
update pod
|
1645 |
If process is spawn by forking, new connection is created automatically. |
1646 |
This feature is EXPERIMETNAL. |
|
EXPERIMETAL fork safety impl...
|
1647 | |
cleanup
|
1648 |
=head2 C<execute> |
packaging one directory
|
1649 | |
update pod
|
1650 |
my $result = $dbi->execute( |
1651 |
"select * from book where {= title} and {like author}", |
|
1652 |
param => {title => 'Perl', author => '%Ken%'} |
|
1653 |
); |
|
1654 | ||
1655 |
Execute SQL, containing tags. |
|
1656 |
Return value is L<DBIx::Custom::Result> in select statement, or |
|
1657 |
the count of affected rows in insert, update, delete statement. |
|
1658 | ||
1659 |
Tag is turned into the statement containing place holder |
|
1660 |
before SQL is executed. |
|
1661 | ||
1662 |
select * from where title = ? and author like ?; |
|
1663 | ||
1664 |
See also L<Tags/Tags>. |
|
1665 | ||
1666 |
The following opitons are currently available. |
|
1667 | ||
1668 |
=over 4 |
|
1669 | ||
1670 |
=item C<filter> |
|
1671 | ||
1672 |
Filter, executed before data is send to database. This is array reference. |
|
1673 |
Filter value is code reference or |
|
1674 |
filter name registerd by C<register_filter()>. |
|
1675 | ||
1676 |
# Basic |
|
1677 |
$dbi->execute( |
|
1678 |
$sql, |
|
1679 |
filter => [ |
|
1680 |
title => sub { uc $_[0] } |
|
1681 |
author => sub { uc $_[0] } |
|
1682 |
] |
|
1683 |
); |
|
1684 |
|
|
1685 |
# At once |
|
1686 |
$dbi->execute( |
|
1687 |
$sql, |
|
1688 |
filter => [ |
|
1689 |
[qw/title author/] => sub { uc $_[0] } |
|
1690 |
] |
|
1691 |
); |
|
1692 |
|
|
1693 |
# Filter name |
|
1694 |
$dbi->execute( |
|
1695 |
$sql, |
|
1696 |
filter => [ |
|
1697 |
title => 'upper_case', |
|
1698 |
author => 'upper_case' |
|
1699 |
] |
|
1700 |
); |
|
1701 | ||
1702 |
These filters are added to the C<out> filters, set by C<apply_filter()>. |
|
update document
|
1703 | |
update pod
|
1704 |
=back |
version 0.0901
|
1705 | |
removed DBIx::Custom commit ...
|
1706 |
=head2 C<delete> |
packaging one directory
|
1707 | |
update pod
|
1708 |
$dbi->delete(table => 'book', where => {title => 'Perl'}); |
1709 | ||
1710 |
Delete statement. |
|
1711 | ||
1712 |
The following opitons are currently available. |
|
1713 | ||
update pod
|
1714 |
=over 4 |
1715 | ||
update pod
|
1716 |
=item C<table> |
1717 | ||
1718 |
Table name. |
|
1719 | ||
1720 |
$dbi->delete(table => 'book'); |
|
1721 | ||
1722 |
=item C<where> |
|
1723 | ||
1724 |
Where clause. This is hash reference or L<DBIx::Custom::Where> object. |
|
1725 |
|
|
1726 |
# Hash reference |
|
1727 |
$dbi->delete(where => {title => 'Perl'}); |
|
1728 |
|
|
1729 |
# DBIx::Custom::Where object |
|
1730 |
my $where = $dbi->where( |
|
1731 |
clause => ['and', '{= author}', '{like title}'], |
|
1732 |
param => {author => 'Ken', title => '%Perl%'} |
|
1733 |
); |
|
1734 |
$dbi->delete(where => $where); |
|
1735 | ||
1736 |
=item C<append> |
|
1737 | ||
1738 |
Append statement to last of SQL. This is string. |
|
1739 | ||
1740 |
$dbi->delete(append => 'order by title'); |
|
1741 | ||
1742 |
=item C<filter> |
|
1743 | ||
1744 |
Filter, executed before data is send to database. This is array reference. |
|
1745 |
Filter value is code reference or |
|
1746 |
filter name registerd by C<register_filter()>. |
|
1747 | ||
1748 |
# Basic |
|
1749 |
$dbi->delete( |
|
1750 |
filter => [ |
|
1751 |
title => sub { uc $_[0] } |
|
1752 |
author => sub { uc $_[0] } |
|
1753 |
] |
|
1754 |
); |
|
1755 |
|
|
1756 |
# At once |
|
1757 |
$dbi->delete( |
|
1758 |
filter => [ |
|
1759 |
[qw/title author/] => sub { uc $_[0] } |
|
1760 |
] |
|
1761 |
); |
|
1762 |
|
|
1763 |
# Filter name |
|
1764 |
$dbi->delete( |
|
1765 |
filter => [ |
|
1766 |
title => 'upper_case', |
|
1767 |
author => 'upper_case' |
|
1768 |
] |
|
1769 |
); |
|
1770 | ||
1771 |
These filters are added to the C<out> filters, set by C<apply_filter()>. |
|
1772 | ||
update pod
|
1773 |
=item C<query> EXPERIMENTAL |
update pod
|
1774 | |
1775 |
Get L<DBIx::Custom::Query> object instead of executing SQL. |
|
1776 |
This is true or false value. |
|
1777 | ||
1778 |
my $query = $dbi->delete(query => 1); |
|
1779 | ||
1780 |
You can check SQL. |
|
1781 | ||
1782 |
my $sql = $query->sql; |
|
renamed build_query to creat...
|
1783 | |
update pod
|
1784 |
=back |
1785 | ||
removed DBIx::Custom commit ...
|
1786 |
=head2 C<delete_all> |
packaging one directory
|
1787 | |
cleanup
|
1788 |
$dbi->delete_all(table => $table); |
packaging one directory
|
1789 | |
update pod
|
1790 |
Delete statement to delete all rows. |
1791 |
Options is same as C<delete()>. |
|
bind_filter argument is chan...
|
1792 | |
update pod
|
1793 |
=head2 C<delete_at()> EXPERIMENTAL |
add experimental update_at()...
|
1794 | |
update pod
|
1795 |
Delete statement, using primary key. |
add experimental update_at()...
|
1796 | |
1797 |
$dbi->delete_at( |
|
1798 |
table => 'book', |
|
update pod
|
1799 |
primary_key => 'id', |
1800 |
where => '5' |
|
add experimental update_at()...
|
1801 |
); |
1802 | ||
update pod
|
1803 |
This method is same as C<delete()> exept that |
1804 |
C<primary_key> is specified and C<where> is constant value or array refrence. |
|
1805 |
all option of C<delete()> is available. |
|
add experimental update_at()...
|
1806 | |
update pod
|
1807 |
=over 4 |
1808 | ||
1809 |
=item C<primary_key> |
|
add experimental update_at()...
|
1810 | |
update pod
|
1811 |
Primary key. This is constant value or array reference. |
1812 |
|
|
1813 |
# Constant value |
|
1814 |
$dbi->delete(primary_key => 'id'); |
|
1815 | ||
1816 |
# Array reference |
|
1817 |
$dbi->delete(primary_key => ['id1', 'id2' ]); |
|
1818 | ||
1819 |
This is used to create where clause. |
|
1820 | ||
update pod
|
1821 |
=item C<where> |
update pod
|
1822 | |
1823 |
Where clause, created from primary key information. |
|
1824 |
This is constant value or array reference. |
|
1825 | ||
1826 |
# Constant value |
|
1827 |
$dbi->delete(where => 5); |
|
1828 | ||
1829 |
# Array reference |
|
1830 |
$dbi->delete(where => [3, 5]); |
|
1831 | ||
1832 |
In first examle, the following SQL is created. |
|
1833 | ||
1834 |
delete from book where id = ?; |
|
1835 | ||
1836 |
Place holder is set to 5. |
|
add experimental update_at()...
|
1837 | |
update pod
|
1838 |
=back |
1839 | ||
cleanup
|
1840 |
=head2 C<insert> |
1841 | ||
update pod
|
1842 |
$dbi->insert( |
1843 |
table => 'book', |
|
1844 |
param => {title => 'Perl', author => 'Ken'} |
|
1845 |
); |
|
1846 | ||
1847 |
Insert statement. |
|
1848 | ||
1849 |
The following opitons are currently available. |
|
1850 | ||
update pod
|
1851 |
=over 4 |
1852 | ||
update pod
|
1853 |
=item C<table> |
1854 | ||
1855 |
Table name. |
|
1856 | ||
1857 |
$dbi->insert(table => 'book'); |
|
1858 | ||
1859 |
=item C<param> |
|
1860 | ||
1861 |
Insert data. This is hash reference. |
|
1862 | ||
1863 |
$dbi->insert(param => {title => 'Perl'}); |
|
1864 | ||
1865 |
=item C<append> |
|
1866 | ||
1867 |
Append statement to last of SQL. This is string. |
|
1868 | ||
1869 |
$dbi->insert(append => 'order by title'); |
|
1870 | ||
1871 |
=item C<filter> |
|
1872 | ||
1873 |
Filter, executed before data is send to database. This is array reference. |
|
1874 |
Filter value is code reference or |
|
1875 |
filter name registerd by C<register_filter()>. |
|
1876 | ||
1877 |
# Basic |
|
1878 |
$dbi->insert( |
|
1879 |
filter => [ |
|
1880 |
title => sub { uc $_[0] } |
|
1881 |
author => sub { uc $_[0] } |
|
1882 |
] |
|
1883 |
); |
|
1884 |
|
|
1885 |
# At once |
|
1886 |
$dbi->insert( |
|
1887 |
filter => [ |
|
1888 |
[qw/title author/] => sub { uc $_[0] } |
|
1889 |
] |
|
1890 |
); |
|
1891 |
|
|
1892 |
# Filter name |
|
1893 |
$dbi->insert( |
|
1894 |
filter => [ |
|
1895 |
title => 'upper_case', |
|
1896 |
author => 'upper_case' |
|
1897 |
] |
|
1898 |
); |
|
1899 | ||
1900 |
These filters are added to the C<out> filters, set by C<apply_filter()>. |
|
1901 | ||
update pod
|
1902 |
=item C<query> EXPERIMENTAL |
update pod
|
1903 | |
1904 |
Get L<DBIx::Custom::Query> object instead of executing SQL. |
|
1905 |
This is true or false value. |
|
1906 | ||
1907 |
my $query = $dbi->insert(query => 1); |
|
cleanup
|
1908 | |
update pod
|
1909 |
You can check SQL. |
cleanup
|
1910 | |
update pod
|
1911 |
my $sql = $query->sql; |
1912 | ||
update pod
|
1913 |
=back |
1914 | ||
1915 |
=head2 C<insert_at()> EXPERIMENTAL |
|
added experimental DBIx::Cus...
|
1916 | |
update pod
|
1917 |
Insert statement, using primary key. |
added experimental DBIx::Cus...
|
1918 | |
1919 |
$dbi->insert_at( |
|
1920 |
table => 'book', |
|
update pod
|
1921 |
primary_key => 'id', |
1922 |
where => '5', |
|
1923 |
param => {title => 'Perl'} |
|
added experimental DBIx::Cus...
|
1924 |
); |
1925 | ||
update pod
|
1926 |
This method is same as C<insert()> exept that |
1927 |
C<primary_key> is specified and C<where> is constant value or array refrence. |
|
1928 |
all option of C<insert()> is available. |
|
1929 | ||
update pod
|
1930 |
=over 4 |
1931 | ||
1932 |
=item C<primary_key> |
|
update pod
|
1933 | |
1934 |
Primary key. This is constant value or array reference. |
|
1935 |
|
|
1936 |
# Constant value |
|
1937 |
$dbi->insert(primary_key => 'id'); |
|
1938 | ||
1939 |
# Array reference |
|
1940 |
$dbi->insert(primary_key => ['id1', 'id2' ]); |
|
1941 | ||
1942 |
This is used to create parts of insert data. |
|
1943 | ||
update pod
|
1944 |
=item C<where> |
update pod
|
1945 | |
1946 |
Parts of Insert data, create from primary key information. |
|
1947 |
This is constant value or array reference. |
|
1948 | ||
1949 |
# Constant value |
|
1950 |
$dbi->insert(where => 5); |
|
1951 | ||
1952 |
# Array reference |
|
1953 |
$dbi->insert(where => [3, 5]); |
|
1954 | ||
1955 |
In first examle, the following SQL is created. |
|
1956 | ||
1957 |
insert into book (id, title) values (?, ?); |
|
1958 | ||
1959 |
Place holders are set to 5 and 'Perl'. |
|
added experimental DBIx::Cus...
|
1960 | |
update pod
|
1961 |
=back |
1962 | ||
1963 |
=head2 C<insert_param> EXPERIMENTAL |
|
added experimental update_pa...
|
1964 | |
1965 |
my $insert_param = $dbi->insert_param({title => 'a', age => 2}); |
|
1966 | ||
1967 |
Create insert parameter tag. |
|
1968 | ||
update pod
|
1969 |
{insert_param title age} |
added experimental update_pa...
|
1970 | |
update pod
|
1971 |
=head2 C<each_column> EXPERIMENTAL |
added experimental iterate_a...
|
1972 | |
pod fix
|
1973 |
$dbi->each_column( |
added experimental iterate_a...
|
1974 |
sub { |
update pod
|
1975 |
my ($dbi, $table, $column, $column_info) = @_; |
added experimental iterate_a...
|
1976 |
|
add experimental setup_model...
|
1977 |
my $type = $column_info->{TYPE_NAME}; |
pod fix
|
1978 |
|
1979 |
if ($type eq 'DATE') { |
|
1980 |
# ... |
|
1981 |
} |
|
added experimental iterate_a...
|
1982 |
} |
1983 |
); |
|
update pod
|
1984 | |
1985 |
Iterate all column informations of all table from database. |
|
1986 |
Argument is callback when one column is found. |
|
added experimental not_exist...
|
1987 |
Callback receive four arguments, dbi object, table name, |
add experimental setup_model...
|
1988 |
column name and column information. |
added experimental not_exist...
|
1989 | |
update pod
|
1990 |
=head2 C<include_model> EXPERIMENTAL |
removed experimental base_ta...
|
1991 | |
update pod
|
1992 |
$dbi->include_model('MyModel'); |
removed experimental base_ta...
|
1993 | |
update pod
|
1994 |
Include models from specified namespace, |
1995 |
the following layout is needed to include models. |
|
removed experimental base_ta...
|
1996 | |
update pod
|
1997 |
lib / MyModel.pm |
1998 |
/ MyModel / book.pm |
|
1999 |
/ company.pm |
|
add feture. all model class ...
|
2000 | |
update pod
|
2001 |
Name space module, extending L<DBIx::Custom::Model>. |
add feture. all model class ...
|
2002 | |
update pod
|
2003 |
B<MyModel.pm> |
add feture. all model class ...
|
2004 | |
2005 |
package MyModel; |
|
2006 |
|
|
2007 |
use base 'DBIx::Custom::Model'; |
|
update pod
|
2008 |
|
2009 |
1; |
|
add feture. all model class ...
|
2010 | |
update pod
|
2011 |
Model modules, extending name space module. |
removed experimental base_ta...
|
2012 | |
update pod
|
2013 |
B<MyModel/book.pm> |
removed experimental base_ta...
|
2014 | |
update pod
|
2015 |
package MyModel::book; |
2016 |
|
|
2017 |
use base 'MyModel'; |
|
2018 |
|
|
2019 |
1; |
|
removed experimental base_ta...
|
2020 | |
update pod
|
2021 |
B<MyModel/company.pm> |
removed experimental base_ta...
|
2022 | |
update pod
|
2023 |
package MyModel::company; |
2024 |
|
|
2025 |
use base 'MyModel'; |
|
2026 |
|
|
2027 |
1; |
|
2028 |
|
|
2029 |
MyModel::book and MyModel::company is included by C<include_model()>. |
|
removed experimental base_ta...
|
2030 | |
update pod
|
2031 |
You can get model object by C<model()>. |
2032 | ||
2033 |
my $book_model = $dbi->model('book'); |
|
2034 |
my $company_model = $dbi->model('company'); |
|
removed experimental base_ta...
|
2035 | |
update pod
|
2036 |
See L<DBIx::Custom::Model> to know model features. |
2037 | ||
2038 |
=head2 C<method> EXPERIMENTAL |
|
added experimental not_exist...
|
2039 | |
2040 |
$dbi->method( |
|
2041 |
update_or_insert => sub { |
|
2042 |
my $self = shift; |
|
update pod
|
2043 |
|
2044 |
# Process |
|
added experimental not_exist...
|
2045 |
}, |
2046 |
find_or_create => sub { |
|
2047 |
my $self = shift; |
|
update pod
|
2048 |
|
2049 |
# Process |
|
added experimental not_exist...
|
2050 |
} |
2051 |
); |
|
2052 | ||
update pod
|
2053 |
Register method. These method is called directly from L<DBIx::Custom> object. |
added experimental not_exist...
|
2054 | |
2055 |
$dbi->update_or_insert; |
|
2056 |
$dbi->find_or_create; |
|
2057 | ||
update pod
|
2058 |
=head2 C<model> EXPERIMENTAL |
2059 | ||
2060 |
$dbi->model('book')->method( |
|
2061 |
insert => sub { ... }, |
|
2062 |
update => sub { ... } |
|
2063 |
); |
|
2064 |
|
|
2065 |
my $model = $dbi->model('book'); |
|
2066 | ||
2067 |
Set and get a L<DBIx::Custom::Model> object, |
|
2068 | ||
added experimental not_exist...
|
2069 |
=head2 C<new> |
2070 | ||
update pod
|
2071 |
my $dbi = DBIx::Custom->new( |
2072 |
data_source => "dbi:mysql:database=dbname", |
|
2073 |
user => 'ken', |
|
2074 |
password => '!LFKD%$&', |
|
2075 |
dbi_option => {mysql_enable_utf8 => 1} |
|
2076 |
); |
|
added experimental not_exist...
|
2077 | |
2078 |
Create a new L<DBIx::Custom> object. |
|
2079 | ||
update pod
|
2080 |
=head2 C<not_exists> EXPERIMENTAL |
added experimental not_exist...
|
2081 | |
2082 |
my $not_exists = $dbi->not_exists; |
|
2083 | ||
update pod
|
2084 |
DBIx::Custom::NotExists object, indicating the column is not exists. |
2085 |
This is used by C<clause> of L<DBIx::Custom::Where> . |
|
experimental extended select...
|
2086 | |
cleanup
|
2087 |
=head2 C<register_filter> |
2088 | ||
update pod
|
2089 |
$dbi->register_filter( |
2090 |
# Time::Piece object to database DATE format |
|
2091 |
tp_to_date => sub { |
|
2092 |
my $tp = shift; |
|
2093 |
return $tp->strftime('%Y-%m-%d'); |
|
2094 |
}, |
|
2095 |
# database DATE format to Time::Piece object |
|
2096 |
date_to_tp => sub { |
|
2097 |
my $date = shift; |
|
2098 |
return Time::Piece->strptime($date, '%Y-%m-%d'); |
|
2099 |
} |
|
2100 |
); |
|
cleanup
|
2101 |
|
update pod
|
2102 |
Register filters, used by C<filter> option of many methods. |
cleanup
|
2103 | |
update pod
|
2104 |
=head2 C<register_tag> |
cleanup
|
2105 | |
update pod
|
2106 |
$dbi->register_tag( |
2107 |
update => sub { |
|
2108 |
my @columns = @_; |
|
2109 |
|
|
2110 |
# Update parameters |
|
2111 |
my $s = 'set '; |
|
2112 |
$s .= "$_ = ?, " for @columns; |
|
2113 |
$s =~ s/, $//; |
|
2114 |
|
|
2115 |
return [$s, \@columns]; |
|
2116 |
} |
|
2117 |
); |
|
cleanup
|
2118 | |
update pod
|
2119 |
Register tag, used by C<execute()>. |
cleanup
|
2120 | |
update pod
|
2121 |
See also L<Tags/Tags> about tag registered by default. |
cleanup
|
2122 | |
update pod
|
2123 |
Tag parser receive arguments specified in tag. |
2124 |
In the following tag, 'title' and 'author' is parser arguments |
|
cleanup
|
2125 | |
update pod
|
2126 |
{update_param title author} |
cleanup
|
2127 | |
update pod
|
2128 |
Tag parser must return array refrence, |
2129 |
first element is the result statement, |
|
2130 |
second element is column names corresponding to place holders. |
|
cleanup
|
2131 | |
update pod
|
2132 |
In this example, result statement is |
cleanup
|
2133 | |
update pod
|
2134 |
set title = ?, author = ? |
added register_tag_processor
|
2135 | |
update pod
|
2136 |
Column names is |
added register_tag_processor
|
2137 | |
update pod
|
2138 |
['title', 'author'] |
added register_tag_processor
|
2139 | |
removed DBIx::Custom commit ...
|
2140 |
=head2 C<select> |
added select() all_column op...
|
2141 | |
select method column option ...
|
2142 |
my $result = $dbi->select( |
added select() all_column op...
|
2143 |
table => 'book', |
2144 |
column => ['author', 'title'], |
|
2145 |
where => {author => 'Ken'}, |
|
select method column option ...
|
2146 |
); |
added select() all_column op...
|
2147 |
|
update pod
|
2148 |
Select statement. |
added select() all_column op...
|
2149 | |
2150 |
The following opitons are currently available. |
|
2151 | ||
2152 |
=over 4 |
|
2153 | ||
2154 |
=item C<table> |
|
2155 | ||
2156 |
Table name. |
|
2157 | ||
update pod
|
2158 |
$dbi->select(table => 'book'); |
added select() all_column op...
|
2159 | |
2160 |
=item C<column> |
|
2161 | ||
2162 |
Column clause. This is array reference or constant value. |
|
2163 | ||
2164 |
# Hash refernce |
|
2165 |
$dbi->select(column => ['author', 'title']); |
|
2166 |
|
|
2167 |
# Constant value |
|
2168 |
$dbi->select(column => 'author'); |
|
2169 | ||
2170 |
Default is '*' unless C<column> is specified. |
|
2171 | ||
2172 |
# Default |
|
2173 |
$dbi->select(column => '*'); |
|
2174 | ||
update pod
|
2175 |
=item C<all_column> EXPERIMENTAL |
added select() all_column op...
|
2176 | |
update pod
|
2177 |
Colum clause, contains all columns of joined table. This is true or false value |
added select() all_column op...
|
2178 | |
update pod
|
2179 |
$dbi->select(all_column => 1); |
added select() all_column op...
|
2180 | |
2181 |
If main table is C<book> and joined table is C<company>, |
|
2182 |
This create the following column clause. |
|
2183 | ||
2184 |
book.author as author |
|
2185 |
book.company_id as company_id |
|
2186 |
company.id as company__id |
|
2187 |
company.name as company__name |
|
2188 | ||
2189 |
Columns of main table is consist of only column name, |
|
2190 |
Columns of joined table is consist of table and column name joined C<__>. |
|
2191 | ||
update pod
|
2192 |
Note that this option is failed unless L<DBIx::Custom::Model> object is set to |
2193 |
C<model> and C<columns> of the object is set. |
|
2194 | ||
2195 |
# Generally do the following way before using all_column option |
|
2196 |
$dbi->include_model('MyModel')->setup_model; |
|
2197 | ||
added select() all_column op...
|
2198 |
=item C<where> |
2199 | ||
2200 |
Where clause. This is hash reference or L<DBIx::Custom::Where> object. |
|
2201 |
|
|
2202 |
# Hash reference |
|
update pod
|
2203 |
$dbi->select(where => {author => 'Ken', 'title' => 'Perl'}); |
added select() all_column op...
|
2204 |
|
update pod
|
2205 |
# DBIx::Custom::Where object |
added select() all_column op...
|
2206 |
my $where = $dbi->where( |
2207 |
clause => ['and', '{= author}', '{like title}'], |
|
2208 |
param => {author => 'Ken', title => '%Perl%'} |
|
2209 |
); |
|
update pod
|
2210 |
$dbi->select(where => $where); |
added select() all_column op...
|
2211 | |
update pod
|
2212 |
=item C<join> EXPERIMENTAL |
added select() all_column op...
|
2213 | |
update pod
|
2214 |
Join clause used in need. This is array reference. |
2215 | ||
2216 |
$dbi->select(join => |
|
2217 |
[ |
|
2218 |
'left outer join company on book.company_id = company_id', |
|
2219 |
'left outer join location on company.location_id = location.id' |
|
2220 |
] |
|
2221 |
); |
|
2222 | ||
2223 |
If column cluase or where clause contain table name like "company.name", |
|
2224 |
needed join clause is used automatically. |
|
2225 | ||
2226 |
$dbi->select( |
|
2227 |
table => 'book', |
|
2228 |
column => ['company.location_id as company__location_id'], |
|
2229 |
where => {'company.name' => 'Orange'}, |
|
2230 |
join => [ |
|
2231 |
'left outer join company on book.company_id = company.id', |
|
2232 |
'left outer join location on company.location_id = location.id' |
|
2233 |
] |
|
2234 |
); |
|
2235 | ||
2236 |
In above select, the following SQL is created. |
|
2237 | ||
2238 |
select company.location_id as company__location_id |
|
2239 |
from book |
|
2240 |
left outer join company on book.company_id = company.id |
|
2241 |
where company.name = Orange |
|
2242 | ||
2243 |
=item C<append> |
|
2244 | ||
update pod
|
2245 |
Append statement to last of SQL. This is string. |
update pod
|
2246 | |
2247 |
$dbi->select(append => 'order by title'); |
|
2248 | ||
2249 |
=item C<filter> |
|
2250 | ||
update pod
|
2251 |
Filter, executed before data is send to database. This is array reference. |
2252 |
Filter value is code reference or |
|
update pod
|
2253 |
filter name registerd by C<register_filter()>. |
2254 | ||
2255 |
# Basic |
|
2256 |
$dbi->select( |
|
2257 |
filter => [ |
|
2258 |
title => sub { uc $_[0] } |
|
2259 |
author => sub { uc $_[0] } |
|
2260 |
] |
|
2261 |
); |
|
2262 |
|
|
2263 |
# At once |
|
2264 |
$dbi->select( |
|
2265 |
filter => [ |
|
2266 |
[qw/title author/] => sub { uc $_[0] } |
|
2267 |
] |
|
2268 |
); |
|
2269 |
|
|
2270 |
# Filter name |
|
2271 |
$dbi->select( |
|
2272 |
filter => [ |
|
2273 |
title => 'upper_case', |
|
2274 |
author => 'upper_case' |
|
2275 |
] |
|
2276 |
); |
|
add experimental selection o...
|
2277 | |
update pod
|
2278 |
These filters are added to the C<out> filters, set by C<apply_filter()>. |
update document
|
2279 | |
update pod
|
2280 |
=item C<query> EXPERIMENTAL |
cleanup
|
2281 | |
update pod
|
2282 |
Get L<DBIx::Custom::Query> object instead of executing SQL. |
2283 |
This is true or false value. |
|
2284 | ||
update pod
|
2285 |
my $query = $dbi->select(query => 1); |
update pod
|
2286 | |
update pod
|
2287 |
You can check SQL. |
update pod
|
2288 | |
2289 |
my $sql = $query->sql; |
|
2290 | ||
2291 |
=back |
|
cleanup
|
2292 | |
update pod
|
2293 |
=head2 C<select_at()> EXPERIMENTAL |
add experimental update_at()...
|
2294 | |
update pod
|
2295 |
Select statement, using primary key. |
2296 | ||
2297 |
$dbi->select_at( |
|
2298 |
table => 'book', |
|
2299 |
primary_key => 'id', |
|
2300 |
where => '5' |
|
2301 |
); |
|
2302 | ||
update pod
|
2303 |
This method is same as C<select()> exept that |
2304 |
C<primary_key> is specified and C<where> is constant value or array refrence. |
|
update pod
|
2305 |
all option of C<select()> is available. |
add experimental update_at()...
|
2306 | |
update pod
|
2307 |
=over 4 |
2308 | ||
2309 |
=item C<primary_key> |
|
add experimental update_at()...
|
2310 | |
update pod
|
2311 |
Primary key. This is constant value or array reference. |
2312 |
|
|
2313 |
# Constant value |
|
2314 |
$dbi->select(primary_key => 'id'); |
|
2315 | ||
2316 |
# Array reference |
|
2317 |
$dbi->select(primary_key => ['id1', 'id2' ]); |
|
2318 | ||
update pod
|
2319 |
This is used to create where clause. |
2320 | ||
update pod
|
2321 |
=item C<where> |
update pod
|
2322 | |
update pod
|
2323 |
Where clause, created from primary key information. |
update pod
|
2324 |
This is constant value or array reference. |
2325 | ||
2326 |
# Constant value |
|
2327 |
$dbi->select(where => 5); |
|
2328 | ||
2329 |
# Array reference |
|
2330 |
$dbi->select(where => [3, 5]); |
|
2331 | ||
2332 |
In first examle, the following SQL is created. |
|
2333 | ||
2334 |
select * from book where id = ? |
|
2335 | ||
2336 |
Place holder is set to 5. |
|
add experimental update_at()...
|
2337 | |
update pod
|
2338 |
=back |
2339 | ||
cleanup
|
2340 |
=head2 C<update> |
removed reconnect method
|
2341 | |
update pod
|
2342 |
$dbi->update( |
2343 |
table => 'book', |
|
2344 |
param => {title => 'Perl'}, |
|
2345 |
where => {id => 4} |
|
2346 |
); |
|
removed reconnect method
|
2347 | |
update pod
|
2348 |
Update statement. |
added experimental update_pa...
|
2349 | |
update pod
|
2350 |
The following opitons are currently available. |
added experimental update_pa...
|
2351 | |
update pod
|
2352 |
=over 4 |
2353 | ||
update pod
|
2354 |
=item C<table> |
2355 | ||
update pod
|
2356 |
Table name. |
2357 | ||
2358 |
$dbi->update(table => 'book'); |
|
2359 | ||
2360 |
=item C<param> |
|
2361 | ||
2362 |
Update data. This is hash reference. |
|
2363 | ||
2364 |
$dbi->update(param => {title => 'Perl'}); |
|
2365 | ||
2366 |
=item C<where> |
|
2367 | ||
2368 |
Where clause. This is hash reference or L<DBIx::Custom::Where> object. |
|
2369 |
|
|
2370 |
# Hash reference |
|
2371 |
$dbi->update(where => {author => 'Ken', 'title' => 'Perl'}); |
|
2372 |
|
|
2373 |
# DBIx::Custom::Where object |
|
2374 |
my $where = $dbi->where( |
|
2375 |
clause => ['and', '{= author}', '{like title}'], |
|
2376 |
param => {author => 'Ken', title => '%Perl%'} |
|
2377 |
); |
|
2378 |
$dbi->update(where => $where); |
|
2379 | ||
2380 |
=item C<append> |
|
2381 | ||
2382 |
Append statement to last of SQL. This is string. |
|
2383 | ||
2384 |
$dbi->update(append => 'order by title'); |
|
2385 | ||
2386 |
=item C<filter> |
|
2387 | ||
2388 |
Filter, executed before data is send to database. This is array reference. |
|
2389 |
Filter value is code reference or |
|
2390 |
filter name registerd by C<register_filter()>. |
|
2391 | ||
2392 |
# Basic |
|
2393 |
$dbi->update( |
|
2394 |
filter => [ |
|
2395 |
title => sub { uc $_[0] } |
|
2396 |
author => sub { uc $_[0] } |
|
2397 |
] |
|
2398 |
); |
|
2399 |
|
|
2400 |
# At once |
|
2401 |
$dbi->update( |
|
2402 |
filter => [ |
|
2403 |
[qw/title author/] => sub { uc $_[0] } |
|
2404 |
] |
|
2405 |
); |
|
2406 |
|
|
2407 |
# Filter name |
|
2408 |
$dbi->update( |
|
2409 |
filter => [ |
|
2410 |
title => 'upper_case', |
|
2411 |
author => 'upper_case' |
|
2412 |
] |
|
2413 |
); |
|
added experimental update_pa...
|
2414 | |
update pod
|
2415 |
These filters are added to the C<out> filters, set by C<apply_filter()>. |
added experimental update_pa...
|
2416 | |
update pod
|
2417 |
=item C<query> EXPERIMENTAL |
add experimental setup_model...
|
2418 | |
update pod
|
2419 |
Get L<DBIx::Custom::Query> object instead of executing SQL. |
2420 |
This is true or false value. |
|
add experimental setup_model...
|
2421 | |
update pod
|
2422 |
my $query = $dbi->update(query => 1); |
2423 | ||
2424 |
You can check SQL. |
|
2425 | ||
2426 |
my $sql = $query->sql; |
|
add experimental setup_model...
|
2427 | |
update pod
|
2428 |
=back |
2429 | ||
cleanup
|
2430 |
=head2 C<update_all> |
renamed build_query to creat...
|
2431 | |
update pod
|
2432 |
$dbi->update_all(table => 'book', param => {title => 'Perl'}); |
renamed build_query to creat...
|
2433 | |
update pod
|
2434 |
Update statement to update all rows. |
2435 |
Options is same as C<update()>. |
|
removed DBIx::Custom commit ...
|
2436 | |
update pod
|
2437 |
=head2 C<update_at()> EXPERIMENTAL |
add experimental update_at()...
|
2438 | |
update pod
|
2439 |
Update statement, using primary key. |
add experimental update_at()...
|
2440 | |
2441 |
$dbi->update_at( |
|
2442 |
table => 'book', |
|
update pod
|
2443 |
primary_key => 'id', |
2444 |
where => '5', |
|
2445 |
param => {title => 'Perl'} |
|
add experimental update_at()...
|
2446 |
); |
2447 | ||
update pod
|
2448 |
This method is same as C<update()> exept that |
2449 |
C<primary_key> is specified and C<where> is constant value or array refrence. |
|
2450 |
all option of C<update()> is available. |
|
2451 | ||
update pod
|
2452 |
=over 4 |
2453 | ||
2454 |
=item C<primary_key> |
|
update pod
|
2455 | |
2456 |
Primary key. This is constant value or array reference. |
|
2457 |
|
|
2458 |
# Constant value |
|
2459 |
$dbi->update(primary_key => 'id'); |
|
2460 | ||
2461 |
# Array reference |
|
2462 |
$dbi->update(primary_key => ['id1', 'id2' ]); |
|
2463 | ||
2464 |
This is used to create where clause. |
|
2465 | ||
update pod
|
2466 |
=item C<where> |
update pod
|
2467 | |
2468 |
Where clause, created from primary key information. |
|
2469 |
This is constant value or array reference. |
|
2470 | ||
2471 |
# Constant value |
|
2472 |
$dbi->update(where => 5); |
|
2473 | ||
2474 |
# Array reference |
|
2475 |
$dbi->update(where => [3, 5]); |
|
2476 | ||
2477 |
In first examle, the following SQL is created. |
|
2478 | ||
2479 |
update book set title = ? where id = ? |
|
2480 | ||
2481 |
Place holders are set to 'Perl' and 5. |
|
2482 | ||
update pod
|
2483 |
=back |
2484 | ||
2485 |
=head2 C<update_param> EXPERIMENTAL |
|
update pod
|
2486 | |
2487 |
my $update_param = $dbi->update_param({title => 'a', age => 2}); |
|
2488 | ||
2489 |
Create update parameter tag. |
|
2490 | ||
2491 |
{update_param title age} |
|
add experimental update_at()...
|
2492 | |
update pod
|
2493 |
=head2 C<where> EXPERIMENTAL |
fix tests
|
2494 | |
cleanup
|
2495 |
my $where = $dbi->where( |
2496 |
clause => ['and', '{= title}', '{= author}'], |
|
2497 |
param => {title => 'Perl', author => 'Ken'} |
|
2498 |
); |
|
fix tests
|
2499 | |
2500 |
Create a new L<DBIx::Custom::Where> object. |
|
2501 | ||
update pod
|
2502 |
=head2 C<setup_model> EXPERIMENTAL |
cleanup
|
2503 | |
update pod
|
2504 |
$dbi->setup_model; |
cleanup
|
2505 | |
update pod
|
2506 |
Setup all model objects. |
update pod
|
2507 |
C<columns> of model object is automatically set, parsing database information. |
cleanup
|
2508 | |
cleanup
|
2509 |
=head1 Tags |
2510 | ||
2511 |
The following tags is available. |
|
2512 | ||
update pod
|
2513 |
=head2 C<table> EXPERIMENTAL |
add table tag
|
2514 | |
2515 |
Table tag |
|
2516 | ||
2517 |
{table TABLE} -> TABLE |
|
2518 | ||
update pod
|
2519 |
This is used to tell C<execute()> what table is needed . |
add table tag
|
2520 | |
cleanup
|
2521 |
=head2 C<?> |
2522 | ||
2523 |
Placeholder tag. |
|
2524 | ||
2525 |
{? NAME} -> ? |
|
2526 | ||
2527 |
=head2 C<=> |
|
2528 | ||
2529 |
Equal tag. |
|
2530 | ||
2531 |
{= NAME} -> NAME = ? |
|
2532 | ||
2533 |
=head2 C<E<lt>E<gt>> |
|
2534 | ||
2535 |
Not equal tag. |
|
2536 | ||
2537 |
{<> NAME} -> NAME <> ? |
|
2538 | ||
2539 |
=head2 C<E<lt>> |
|
2540 | ||
2541 |
Lower than tag |
|
2542 | ||
2543 |
{< NAME} -> NAME < ? |
|
2544 | ||
2545 |
=head2 C<E<gt>> |
|
2546 | ||
2547 |
Greater than tag |
|
2548 | ||
2549 |
{> NAME} -> NAME > ? |
|
2550 | ||
2551 |
=head2 C<E<gt>=> |
|
2552 | ||
2553 |
Greater than or equal tag |
|
2554 | ||
2555 |
{>= NAME} -> NAME >= ? |
|
2556 | ||
2557 |
=head2 C<E<lt>=> |
|
2558 | ||
2559 |
Lower than or equal tag |
|
2560 | ||
2561 |
{<= NAME} -> NAME <= ? |
|
2562 | ||
2563 |
=head2 C<like> |
|
2564 | ||
2565 |
Like tag |
|
2566 | ||
2567 |
{like NAME} -> NAME like ? |
|
2568 | ||
2569 |
=head2 C<in> |
|
2570 | ||
2571 |
In tag. |
|
2572 | ||
2573 |
{in NAME COUNT} -> NAME in [?, ?, ..] |
|
2574 | ||
2575 |
=head2 C<insert_param> |
|
2576 | ||
2577 |
Insert parameter tag. |
|
2578 | ||
2579 |
{insert_param NAME1 NAME2} -> (NAME1, NAME2) values (?, ?) |
|
2580 | ||
2581 |
=head2 C<update_param> |
|
2582 | ||
2583 |
Updata parameter tag. |
|
2584 | ||
2585 |
{update_param NAME1 NAME2} -> set NAME1 = ?, NAME2 = ? |
|
2586 | ||
DBIx::Custom is now stable
|
2587 |
=head1 STABILITY |
2588 | ||
cleanup
|
2589 |
L<DBIx::Custom> is stable. APIs keep backword compatible |
added select() all_column op...
|
2590 |
except EXPERIMENTAL one in the feature. |
DBIx::Custom is now stable
|
2591 | |
removed DESTROY method(not b...
|
2592 |
=head1 BUGS |
2593 | ||
renamed build_query to creat...
|
2594 |
Please tell me bugs if found. |
removed DESTROY method(not b...
|
2595 | |
2596 |
C<< <kimoto.yuki at gmail.com> >> |
|
2597 | ||
2598 |
L<http://github.com/yuki-kimoto/DBIx-Custom> |
|
2599 | ||
removed reconnect method
|
2600 |
=head1 AUTHOR |
2601 | ||
2602 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
|
version 0.0901
|
2603 | |
packaging one directory
|
2604 |
=head1 COPYRIGHT & LICENSE |
2605 | ||
cleanup
|
2606 |
Copyright 2009-2011 Yuki Kimoto, all rights reserved. |
packaging one directory
|
2607 | |
2608 |
This program is free software; you can redistribute it and/or modify it |
|
2609 |
under the same terms as Perl itself. |
|
2610 | ||
2611 |
=cut |
|
added cache_method attribute
|
2612 | |
2613 |