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