cleanup
|
1 |
package DBIx::Custom; |
2 | ||
remove experimental DBIx::Cu...
|
3 |
our $VERSION = '0.1655'; |
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( |
added experimental DBIx::Cus...
|
23 |
[qw/data_source dbh password 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', |
update pod
|
34 |
safety_column_name => sub { qr/^[\w\.]*$/ }, |
35 |
stash => sub { {} } |
|
many changed
|
36 |
); |
37 | ||
38 |
__PACKAGE__->attr( |
|
39 |
cache_method => sub { |
|
40 |
sub { |
|
41 |
my $self = shift; |
|
42 |
|
|
43 |
$self->{_cached} ||= {}; |
|
44 |
|
|
45 |
if (@_ > 1) { |
|
46 |
$self->{_cached}{$_[0]} = $_[1] |
|
47 |
} |
|
48 |
else { |
|
49 |
return $self->{_cached}{$_[0]} |
|
50 |
} |
|
51 |
} |
|
52 |
} |
|
53 |
); |
|
54 | ||
55 |
__PACKAGE__->attr( |
|
fix tests
|
56 |
filters => sub { |
57 |
{ |
|
58 |
encode_utf8 => sub { encode_utf8($_[0]) }, |
|
59 |
decode_utf8 => sub { decode_utf8($_[0]) } |
|
60 |
} |
|
many changed
|
61 |
} |
fix tests
|
62 |
); |
cleanup
|
63 | |
added helper method
|
64 |
our $AUTOLOAD; |
65 |
sub AUTOLOAD { |
|
66 |
my $self = shift; |
|
67 | ||
renamed helper to method.
|
68 |
# Method name |
69 |
my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/; |
|
added helper method
|
70 | |
renamed helper to method.
|
71 |
# Method |
72 |
$self->{_methods} ||= {}; |
|
add feture. all model class ...
|
73 |
if (my $method = $self->{_methods}->{$mname}) { |
74 |
return $self->$method(@_) |
|
75 |
} |
|
76 |
elsif ($self->dbh->can($mname)) { |
|
77 |
$self->dbh->$mname(@_); |
|
78 |
} |
|
79 |
else { |
|
80 |
croak qq/Can't locate object method "$mname" via "$package"/ |
|
81 |
} |
|
added helper method
|
82 |
} |
83 | ||
renamed auto_filter to apply...
|
84 |
sub apply_filter { |
many changed
|
85 |
my ($self, $table, @cinfos) = @_; |
86 | ||
87 |
# Initialize filters |
|
cleanup
|
88 |
$self->{filter} ||= {}; |
many changed
|
89 |
$self->{filter}{out} ||= {}; |
90 |
$self->{filter}{in} ||= {}; |
|
all filter can receive array...
|
91 |
$self->{filter}{end} ||= {}; |
cleanup
|
92 |
|
many changed
|
93 |
# Create filters |
94 |
my $usage = "Usage: \$dbi->apply_filter(" . |
|
fix bug : filter can't over...
|
95 |
"TABLE, COLUMN1, {in => INFILTER1, out => OUTFILTER1, end => ENDFILTER1}, " . |
96 |
"COLUMN2, {in => INFILTER2, out => OUTFILTER2, end => ENDFILTER2}, ...)"; |
|
many changed
|
97 | |
98 |
for (my $i = 0; $i < @cinfos; $i += 2) { |
|
added auto_filter method
|
99 |
|
many changed
|
100 |
# Column |
101 |
my $column = $cinfos[$i]; |
|
added auto_filter method
|
102 |
|
all filter can receive array...
|
103 |
if (ref $column eq 'ARRAY') { |
104 |
foreach my $c (@$column) { |
|
105 |
push @cinfos, $c, $cinfos[$i + 1]; |
|
106 |
} |
|
107 |
next; |
|
108 |
} |
|
109 |
|
|
fix bug : filter can't over...
|
110 |
# Filter info |
111 |
my $finfo = $cinfos[$i + 1] || {}; |
|
all filter can receive array...
|
112 |
croak "$usage (table: $table)" unless ref $finfo eq 'HASH'; |
fix bug : filter can't over...
|
113 |
foreach my $ftype (keys %$finfo) { |
all filter can receive array...
|
114 |
croak "$usage (table: $table 2)" unless $ftype eq 'in' || $ftype eq 'out' |
fix bug : filter can't over...
|
115 |
|| $ftype eq 'end'; |
many changed
|
116 |
} |
117 |
|
|
fix bug : filter can't over...
|
118 |
foreach my $way (qw/in out end/) { |
119 |
my $filter = $finfo->{$way}; |
|
cleanup
|
120 |
|
fix bug : filter can't over...
|
121 |
# State |
122 |
my $state = !exists $finfo->{$way} ? 'not_exists' |
|
123 |
: !defined $filter ? 'not_defined' |
|
124 |
: ref $filter eq 'CODE' ? 'code' |
|
125 |
: 'name'; |
|
126 |
|
|
127 |
next if $state eq 'not_exists'; |
|
128 |
|
|
129 |
# Check filter |
|
130 |
croak qq{Filter "$filter" is not registered} |
|
131 |
if $state eq 'name' |
|
132 |
&& ! exists $self->filters->{$filter}; |
|
133 |
|
|
134 |
# Filter |
|
135 |
my $f = $state eq 'not_defined' ? undef |
|
136 |
: $state eq 'code' ? $filter |
|
137 |
: $self->filters->{$filter}; |
|
138 |
$self->{filter}{$way}{$table}{$column} = $f; |
|
139 |
$self->{filter}{$way}{$table}{"$table.$column"} = $f; |
|
140 |
$self->{filter}{$way}{$table}{"${table}__$column"} = $f; |
|
many changed
|
141 |
} |
added auto_filter method
|
142 |
} |
143 |
|
|
many changed
|
144 |
return $self; |
added auto_filter method
|
145 |
} |
146 | ||
renamed helper to method.
|
147 |
sub method { |
added helper method
|
148 |
my $self = shift; |
149 |
|
|
150 |
# Merge |
|
renamed helper to method.
|
151 |
my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
152 |
$self->{_methods} = {%{$self->{_methods} || {}}, %$methods}; |
|
added helper method
|
153 |
|
154 |
return $self; |
|
155 |
} |
|
156 | ||
packaging one directory
|
157 |
sub connect { |
cleanup
|
158 |
my $self = ref $_[0] ? shift : shift->new(@_);; |
removed register_format()
|
159 |
|
many changed
|
160 |
# Attributes |
packaging one directory
|
161 |
my $data_source = $self->data_source; |
many changed
|
162 |
croak qq{"data_source" must be specified to connect()"} |
check arguments of connect m...
|
163 |
unless $data_source; |
packaging one directory
|
164 |
my $user = $self->user; |
165 |
my $password = $self->password; |
|
renamed dbi_options to dbi_o...
|
166 |
my $dbi_option = {%{$self->dbi_options}, %{$self->dbi_option}}; |
added dbi_options attribute
|
167 |
|
update document
|
168 |
# Connect |
select, insert, update, upda...
|
169 |
my $dbh = eval {DBI->connect( |
packaging one directory
|
170 |
$data_source, |
171 |
$user, |
|
172 |
$password, |
|
173 |
{ |
|
add default_dbi_option()
|
174 |
%{$self->default_dbi_option}, |
renamed dbi_options to dbi_o...
|
175 |
%$dbi_option |
packaging one directory
|
176 |
} |
177 |
)}; |
|
178 |
|
|
update document
|
179 |
# Connect error |
packaging one directory
|
180 |
croak $@ if $@; |
181 |
|
|
update document
|
182 |
# Database handle |
packaging one directory
|
183 |
$self->dbh($dbh); |
update document
|
184 |
|
packaging one directory
|
185 |
return $self; |
186 |
} |
|
187 | ||
cleanup
|
188 |
sub create_query { |
189 |
my ($self, $source) = @_; |
|
update document
|
190 |
|
cleanup
|
191 |
# Cache |
192 |
my $cache = $self->cache; |
|
update document
|
193 |
|
cleanup
|
194 |
# Create query |
195 |
my $query; |
|
196 |
if ($cache) { |
|
197 |
|
|
198 |
# Get query |
|
199 |
my $q = $self->cache_method->($self, $source); |
|
200 |
|
|
201 |
# Create query |
|
add table tag
|
202 |
if ($q) { |
203 |
$query = DBIx::Custom::Query->new($q); |
|
204 |
$query->filters($self->filters); |
|
205 |
} |
|
cleanup
|
206 |
} |
207 |
|
|
208 |
unless ($query) { |
|
cleanup insert
|
209 | |
cleanup
|
210 |
# Create SQL object |
211 |
my $builder = $self->query_builder; |
|
212 |
|
|
213 |
# Create query |
|
214 |
$query = $builder->build_query($source); |
|
removed register_format()
|
215 | |
cleanup
|
216 |
# Cache query |
217 |
$self->cache_method->($self, $source, |
|
218 |
{sql => $query->sql, |
|
add table tag
|
219 |
columns => $query->columns, |
220 |
tables => $query->tables}) |
|
cleanup
|
221 |
if $cache; |
cleanup insert
|
222 |
} |
223 |
|
|
cleanup
|
224 |
# Prepare statement handle |
225 |
my $sth; |
|
226 |
eval { $sth = $self->dbh->prepare($query->{sql})}; |
|
renamed DBIx::Custom::TagPro...
|
227 |
$self->_croak($@, qq{. Following SQL is executed. "$query->{sql}"}) if $@; |
packaging one directory
|
228 |
|
cleanup
|
229 |
# Set statement handle |
230 |
$query->sth($sth); |
|
packaging one directory
|
231 |
|
cleanup
|
232 |
# Set filters |
233 |
$query->filters($self->filters); |
|
234 |
|
|
cleanup
|
235 |
return $query; |
packaging one directory
|
236 |
} |
237 | ||
cleanup
|
238 |
our %VALID_DELETE_ARGS |
added experimental sugar met...
|
239 |
= map { $_ => 1 } qw/table where append filter allow_delete_all query/; |
cleanup update and update_al...
|
240 | |
cleanup
|
241 |
sub delete { |
select, insert, update, upda...
|
242 |
my ($self, %args) = @_; |
cleanup update and update_al...
|
243 |
|
244 |
# Check arguments |
|
select, insert, update, upda...
|
245 |
foreach my $name (keys %args) { |
add tests
|
246 |
croak qq{"$name" is invalid argument} |
cleanup
|
247 |
unless $VALID_DELETE_ARGS{$name}; |
cleanup update and update_al...
|
248 |
} |
249 |
|
|
250 |
# Arguments |
|
select, insert, update, upda...
|
251 |
my $table = $args{table} || ''; |
added table not specified ex...
|
252 |
croak qq{"table" option must be specified} unless $table; |
select, insert, update, upda...
|
253 |
my $where = $args{where} || {}; |
select() where can't receive...
|
254 |
my $append = $args{append}; |
select, insert, update, upda...
|
255 |
my $filter = $args{filter}; |
cleanup
|
256 |
my $allow_delete_all = $args{allow_delete_all}; |
added auto_filter method
|
257 | |
make delete() using where ob...
|
258 |
# Where |
259 |
my $w; |
|
260 |
if (ref $where eq 'HASH') { |
|
261 |
my $clause = ['and']; |
|
262 |
push @$clause, "{= $_}" for keys %$where; |
|
263 |
$w = $self->where; |
|
264 |
$w->clause($clause); |
|
improved delete() and update...
|
265 |
$w->param($where); |
packaging one directory
|
266 |
} |
select() where can't receive...
|
267 |
elsif (ref $where eq 'DBIx::Custom::Where') { |
268 |
$w = $where; |
|
269 |
$where = $w->param; |
|
270 |
} |
|
make delete() using where ob...
|
271 |
croak qq{"where" must be hash refernce or DBIx::Custom::Where object} |
272 |
unless ref $w eq 'DBIx::Custom::Where'; |
|
273 |
|
|
select() where can't receive...
|
274 |
# String where |
275 |
my $swhere = "$w"; |
|
improved delete() and update...
|
276 |
|
make delete() using where ob...
|
277 |
croak qq{"where" must be specified} |
select() where can't receive...
|
278 |
if $swhere eq '' && !$allow_delete_all; |
make delete() using where ob...
|
279 | |
cleanup
|
280 |
# SQL stack |
281 |
my @sql; |
|
282 | ||
283 |
# Delete |
|
284 |
push @sql, "delete from $table $swhere"; |
|
285 |
push @sql, $append if $append; |
|
286 |
|
|
287 |
my $sql = join(' ', @sql); |
|
packaging one directory
|
288 |
|
added experimental sugar met...
|
289 |
# Create query |
cleanup
|
290 |
my $query = $self->create_query($sql); |
added experimental sugar met...
|
291 |
return $query if $args{query}; |
292 |
|
|
packaging one directory
|
293 |
# Execute query |
added auto_filter method
|
294 |
my $ret_val = $self->execute( |
added experimental sugar met...
|
295 |
$query, param => $where, filter => $filter, |
renamed auto_filter to apply...
|
296 |
table => $table); |
packaging one directory
|
297 |
|
298 |
return $ret_val; |
|
299 |
} |
|
300 | ||
cleanup
|
301 |
sub delete_all { shift->delete(allow_delete_all => 1, @_) } |
packaging one directory
|
302 | |
add experimental update_at()...
|
303 |
our %VALID_DELETE_AT_ARGS |
304 |
= map { $_ => 1 } qw/table where append filter query |
|
305 |
primary_key param/; |
|
306 | ||
307 |
sub delete_at { |
|
308 |
my ($self, %args) = @_; |
|
309 |
|
|
310 |
# Check arguments |
|
311 |
foreach my $name (keys %args) { |
|
312 |
croak qq{"$name" is invalid argument} |
|
313 |
unless $VALID_DELETE_AT_ARGS{$name}; |
|
314 |
} |
|
315 |
|
|
316 |
# Primary key |
|
317 |
my $primary_keys = delete $args{primary_key}; |
|
318 |
$primary_keys = [$primary_keys] unless ref $primary_keys; |
|
319 |
|
|
320 |
# Where clause |
|
321 |
my $where = {}; |
|
322 |
if (exists $args{where}) { |
|
323 |
my $where_columns = delete $args{where}; |
|
324 |
$where_columns = [$where_columns] unless ref $where_columns; |
|
- added experimental DBIx::C...
|
325 | |
326 |
croak qq{"where" must be constant value or array reference} |
|
327 |
unless !ref $where_columns || ref $where_columns eq 'ARRAY'; |
|
add experimental update_at()...
|
328 |
|
329 |
for(my $i = 0; $i < @$primary_keys; $i ++) { |
|
330 |
$where->{$primary_keys->[$i]} = $where_columns->[$i]; |
|
331 |
} |
|
332 |
} |
|
- added experimental DBIx::C...
|
333 |
|
334 |
if (exists $args{param}) { |
|
add experimental update_at()...
|
335 |
my $param = delete $args{param}; |
336 |
|
|
337 |
for(my $i = 0; $i < @$primary_keys; $i ++) { |
|
- added experimental DBIx::C...
|
338 |
delete $param->{$primary_keys->[$i]}; |
add experimental update_at()...
|
339 |
} |
340 |
} |
|
341 |
|
|
342 |
return $self->delete(where => $where, %args); |
|
343 |
} |
|
344 | ||
added helper method
|
345 |
sub DESTROY { } |
346 | ||
renamed auto_filter to apply...
|
347 |
our %VALID_EXECUTE_ARGS = map { $_ => 1 } qw/param filter table/; |
refactoring delete and delet...
|
348 | |
cleanup
|
349 |
sub execute{ |
350 |
my ($self, $query, %args) = @_; |
|
refactoring delete and delet...
|
351 |
|
352 |
# Check arguments |
|
select, insert, update, upda...
|
353 |
foreach my $name (keys %args) { |
add tests
|
354 |
croak qq{"$name" is invalid argument} |
cleanup
|
355 |
unless $VALID_EXECUTE_ARGS{$name}; |
refactoring delete and delet...
|
356 |
} |
357 |
|
|
cleanup
|
358 |
my $params = $args{param} || {}; |
packaging one directory
|
359 |
|
cleanup
|
360 |
# First argument is the soruce of SQL |
361 |
$query = $self->create_query($query) |
|
362 |
unless ref $query; |
|
packaging one directory
|
363 |
|
add table tag
|
364 |
# Applied filter |
cleanup
|
365 |
my $filter = {}; |
all filter can receive array...
|
366 |
|
add table tag
|
367 |
my $tables = $query->tables; |
368 |
my $arg_tables = $args{table} || []; |
|
369 |
$arg_tables = [$arg_tables] |
|
370 |
unless ref $arg_tables eq 'ARRAY'; |
|
371 |
push @$tables, @$arg_tables; |
|
remove experimental DBIx::Cu...
|
372 |
|
renamed auto_filter to apply...
|
373 |
foreach my $table (@$tables) { |
remove experimental DBIx::Cu...
|
374 |
next unless $table; |
cleanup
|
375 |
$filter = { |
376 |
%$filter, |
|
377 |
%{$self->{filter}{out}->{$table} || {}} |
|
added auto_filter method
|
378 |
} |
379 |
} |
|
380 |
|
|
cleanup
|
381 |
# Filter argument |
all filter can receive array...
|
382 |
my $f = DBIx::Custom::Util::array_filter_to_hash($args{filter}) |
383 |
|| $query->filter || {}; |
|
cleanup
|
384 |
foreach my $column (keys %$f) { |
385 |
my $fname = $f->{$column}; |
|
renamed auto_filter to apply...
|
386 |
if (!defined $fname) { |
cleanup
|
387 |
$f->{$column} = undef; |
renamed auto_filter to apply...
|
388 |
} |
389 |
elsif (ref $fname ne 'CODE') { |
|
many changed
|
390 |
croak qq{Filter "$fname" is not registered"} |
cleanup
|
391 |
unless exists $self->filters->{$fname}; |
392 |
|
|
cleanup
|
393 |
$f->{$column} = $self->filters->{$fname}; |
cleanup
|
394 |
} |
395 |
} |
|
cleanup
|
396 |
$filter = {%$filter, %$f}; |
packaging one directory
|
397 |
|
added experimental not_exist...
|
398 |
# Bind |
399 |
my $bind = $self->_bind($params, $query->columns, $filter); |
|
cleanup
|
400 |
|
401 |
# Execute |
|
added experimental not_exist...
|
402 |
my $sth = $query->sth; |
cleanup
|
403 |
my $affected; |
added experimental not_exist...
|
404 |
eval {$affected = $sth->execute(@$bind)}; |
renamed DBIx::Custom::TagPro...
|
405 |
$self->_croak($@, qq{. Following SQL is executed. "$query->{sql}"}) if $@; |
cleanup
|
406 |
|
407 |
# Return resultset if select statement is executed |
|
408 |
if ($sth->{NUM_OF_FIELDS}) { |
|
409 |
|
|
fix bug : filter can't over...
|
410 |
# Result in and end filter |
411 |
my $in_filter = {}; |
|
412 |
my $end_filter = {}; |
|
cleanup
|
413 |
foreach my $table (@$tables) { |
remove experimental DBIx::Cu...
|
414 |
next unless $table; |
cleanup
|
415 |
$in_filter = { |
416 |
%$in_filter, |
|
417 |
%{$self->{filter}{in}{$table} || {}} |
|
fix bug : filter can't over...
|
418 |
}; |
419 |
$end_filter = { |
|
420 |
%$end_filter, |
|
421 |
%{$self->{filter}{end}{$table} || {}} |
|
422 |
}; |
|
cleanup
|
423 |
} |
424 |
|
|
425 |
# Result |
|
426 |
my $result = $self->result_class->new( |
|
cleanup
|
427 |
sth => $sth, |
428 |
filters => $self->filters, |
|
429 |
filter_check => $self->filter_check, |
|
cleanup
|
430 |
default_filter => $self->{default_in_filter}, |
fix bug : filter can't over...
|
431 |
filter => $in_filter || {}, |
432 |
end_filter => $end_filter || {} |
|
cleanup
|
433 |
); |
434 | ||
435 |
return $result; |
|
436 |
} |
|
437 |
return $affected; |
|
438 |
} |
|
439 | ||
added auto_filter method
|
440 |
our %VALID_INSERT_ARGS = map { $_ => 1 } qw/table param append |
added experimental sugar met...
|
441 |
filter query/; |
cleanup
|
442 |
sub insert { |
443 |
my ($self, %args) = @_; |
|
444 | ||
445 |
# Check arguments |
|
446 |
foreach my $name (keys %args) { |
|
447 |
croak qq{"$name" is invalid argument} |
|
448 |
unless $VALID_INSERT_ARGS{$name}; |
|
packaging one directory
|
449 |
} |
450 |
|
|
cleanup
|
451 |
# Arguments |
added table not specified ex...
|
452 |
my $table = $args{table}; |
453 |
croak qq{"table" option must be specified} unless $table; |
|
cleanup
|
454 |
my $param = $args{param} || {}; |
455 |
my $append = $args{append} || ''; |
|
456 |
my $filter = $args{filter}; |
|
457 |
|
|
select() where can't receive...
|
458 |
# Columns |
459 |
my @columns; |
|
460 |
my $safety = $self->safety_column_name; |
|
461 |
foreach my $column (keys %$param) { |
|
462 |
croak qq{"$column" is not safety column name} |
|
463 |
unless $column =~ /$safety/; |
|
464 |
push @columns, $column; |
|
465 |
} |
|
cleanup
|
466 |
|
cleanup
|
467 |
# SQL stack |
468 |
my @sql; |
|
469 |
|
|
470 |
# Insert |
|
471 |
push @sql, "insert into $table {insert_param ". join(' ', @columns) . '}'; |
|
472 |
push @sql, $append if $append; |
|
473 |
|
|
select() where can't receive...
|
474 |
# SQL |
cleanup
|
475 |
my $sql = join (' ', @sql); |
packaging one directory
|
476 |
|
added experimental sugar met...
|
477 |
# Create query |
cleanup
|
478 |
my $query = $self->create_query($sql); |
added experimental sugar met...
|
479 |
return $query if $args{query}; |
480 |
|
|
packaging one directory
|
481 |
# Execute query |
added auto_filter method
|
482 |
my $ret_val = $self->execute( |
added experimental sugar met...
|
483 |
$query, |
added auto_filter method
|
484 |
param => $param, |
485 |
filter => $filter, |
|
renamed auto_filter to apply...
|
486 |
table => $table |
added auto_filter method
|
487 |
); |
packaging one directory
|
488 |
|
489 |
return $ret_val; |
|
490 |
} |
|
491 | ||
- added experimental DBIx::C...
|
492 |
our %VALID_INSERT_AT_ARGS |
493 |
= map { $_ => 1 } qw/table param |
|
494 |
where append filter query |
|
495 |
primary_key param/; |
|
496 | ||
497 |
sub insert_at { |
|
498 |
my ($self, %args) = @_; |
|
499 |
|
|
500 |
# Check arguments |
|
501 |
foreach my $name (keys %args) { |
|
502 |
croak qq{"$name" is invalid argument} |
|
503 |
unless $VALID_INSERT_AT_ARGS{$name}; |
|
504 |
} |
|
505 |
|
|
506 |
# Primary key |
|
507 |
my $primary_keys = delete $args{primary_key}; |
|
508 |
$primary_keys = [$primary_keys] unless ref $primary_keys; |
|
509 |
|
|
510 |
# Where clause |
|
511 |
my $where = {}; |
|
512 |
my $param = {}; |
|
513 |
|
|
514 |
if (exists $args{where}) { |
|
515 |
my $where_columns = delete $args{where}; |
|
516 |
$where_columns = [$where_columns] unless ref $where_columns; |
|
517 | ||
518 |
croak qq{"where" must be constant value or array reference} |
|
519 |
unless !ref $where_columns || ref $where_columns eq 'ARRAY'; |
|
520 |
|
|
521 |
for(my $i = 0; $i < @$primary_keys; $i ++) { |
|
522 |
$where->{$primary_keys->[$i]} = $where_columns->[$i]; |
|
523 |
} |
|
524 |
} |
|
525 |
|
|
526 |
if (exists $args{param}) { |
|
527 |
$param = delete $args{param}; |
|
528 |
for(my $i = 0; $i < @$primary_keys; $i ++) { |
|
529 |
delete $param->{$primary_keys->[$i]}; |
|
530 |
} |
|
531 |
} |
|
532 |
|
|
533 |
$param = {%$param, %$where}; |
|
534 |
|
|
535 |
return $self->insert(param => $param, %args); |
|
536 |
} |
|
537 | ||
remove experimental DBIx::Cu...
|
538 |
sub insert_param { |
539 |
my ($self, $param) = @_; |
|
540 |
|
|
added experimental update_pa...
|
541 |
# Insert paramter tag |
remove experimental DBIx::Cu...
|
542 |
my @tag; |
543 |
push @tag, '{insert_param'; |
|
added experimental update_pa...
|
544 |
my $safety = $self->safety_column_name; |
remove experimental DBIx::Cu...
|
545 |
foreach my $column (keys %$param) { |
added experimental update_pa...
|
546 |
croak qq{"$column" is not safety column name} |
547 |
unless $column =~ /$safety/; |
|
remove experimental DBIx::Cu...
|
548 |
push @tag, $column; |
549 |
} |
|
550 |
push @tag, '}'; |
|
551 |
|
|
552 |
return join ' ', @tag; |
|
553 |
} |
|
554 | ||
pod fix
|
555 |
sub each_column { |
added experimental iterate_a...
|
556 |
my ($self, $cb) = @_; |
557 |
|
|
558 |
# Iterate all tables |
|
559 |
my $sth_tables = $self->dbh->table_info; |
|
560 |
while (my $table_info = $sth_tables->fetchrow_hashref) { |
|
561 |
|
|
562 |
# Table |
|
563 |
my $table = $table_info->{TABLE_NAME}; |
|
564 |
|
|
565 |
# Iterate all columns |
|
566 |
my $sth_columns = $self->dbh->column_info(undef, undef, $table, '%'); |
|
567 |
while (my $column_info = $sth_columns->fetchrow_hashref) { |
|
568 |
my $column = $column_info->{COLUMN_NAME}; |
|
removed experimental txn_sco...
|
569 |
$self->$cb($table, $column, $column_info); |
added experimental iterate_a...
|
570 |
} |
571 |
} |
|
572 |
} |
|
573 | ||
added dbi_options attribute
|
574 |
sub new { |
575 |
my $self = shift->SUPER::new(@_); |
|
576 |
|
|
577 |
# Check attribute names |
|
578 |
my @attrs = keys %$self; |
|
579 |
foreach my $attr (@attrs) { |
|
580 |
croak qq{"$attr" is invalid attribute name} |
|
581 |
unless $self->can($attr); |
|
582 |
} |
|
cleanup
|
583 | |
584 |
$self->register_tag( |
|
585 |
'?' => \&DBIx::Custom::Tag::placeholder, |
|
586 |
'=' => \&DBIx::Custom::Tag::equal, |
|
587 |
'<>' => \&DBIx::Custom::Tag::not_equal, |
|
588 |
'>' => \&DBIx::Custom::Tag::greater_than, |
|
589 |
'<' => \&DBIx::Custom::Tag::lower_than, |
|
590 |
'>=' => \&DBIx::Custom::Tag::greater_than_equal, |
|
591 |
'<=' => \&DBIx::Custom::Tag::lower_than_equal, |
|
592 |
'like' => \&DBIx::Custom::Tag::like, |
|
593 |
'in' => \&DBIx::Custom::Tag::in, |
|
594 |
'insert_param' => \&DBIx::Custom::Tag::insert_param, |
|
595 |
'update_param' => \&DBIx::Custom::Tag::update_param |
|
596 |
); |
|
added dbi_options attribute
|
597 |
|
598 |
return $self; |
|
599 |
} |
|
600 | ||
added experimental not_exist...
|
601 |
sub not_exists { bless {}, 'DBIx::Custom::NotExists' } |
602 | ||
cleanup
|
603 |
sub register_filter { |
604 |
my $invocant = shift; |
|
605 |
|
|
606 |
# Register filter |
|
607 |
my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
608 |
$invocant->filters({%{$invocant->filters}, %$filters}); |
|
609 |
|
|
610 |
return $invocant; |
|
611 |
} |
|
packaging one directory
|
612 | |
renamed DBIx::Custom::TagPro...
|
613 |
sub register_tag { shift->query_builder->register_tag(@_) } |
added register_tag_processor
|
614 | |
refactoring select
|
615 |
our %VALID_SELECT_ARGS |
remove experimental DBIx::Cu...
|
616 |
= map { $_ => 1 } qw/table column where append relation filter query selection left_join/; |
refactoring select
|
617 | |
packaging one directory
|
618 |
sub select { |
select, insert, update, upda...
|
619 |
my ($self, %args) = @_; |
packaging one directory
|
620 |
|
refactoring select
|
621 |
# Check arguments |
select, insert, update, upda...
|
622 |
foreach my $name (keys %args) { |
add tests
|
623 |
croak qq{"$name" is invalid argument} |
refactoring select
|
624 |
unless $VALID_SELECT_ARGS{$name}; |
625 |
} |
|
packaging one directory
|
626 |
|
refactoring select
|
627 |
# Arguments |
added table not specified ex...
|
628 |
my $table = $args{table}; |
629 |
my $tables = ref $table eq 'ARRAY' ? $table |
|
630 |
: defined $table ? [$table] |
|
631 |
: []; |
|
add experimental selection o...
|
632 |
my $columns = $args{column} || []; |
select method column option ...
|
633 |
$columns = [$columns] unless ref $columns; |
add experimental selection o...
|
634 |
my $selection = $args{selection} || ''; |
635 |
my $where = $args{where} || {}; |
|
DBIx::Custom::Model select()...
|
636 |
my $relation = $args{relation} || {}; |
add experimental selection o...
|
637 |
my $append = $args{append}; |
638 |
my $filter = $args{filter}; |
|
remove experimental DBIx::Cu...
|
639 |
my $left_join = $args{left_join} || []; |
640 |
|
|
641 |
my @left_join_tables; |
|
642 |
unshift @left_join_tables, $tables->[-1]; |
|
643 |
|
|
DBIx::Custom::Model select()...
|
644 |
# Relation |
645 |
if (!$selection && keys %$relation) { |
|
add experimental DBIx::Custo...
|
646 |
foreach my $rcolumn (keys %$relation) { |
647 |
my $table1 = (split (/\./, $rcolumn))[0]; |
|
648 |
my $table2 = (split (/\./, $relation->{$rcolumn}))[0]; |
|
649 |
|
|
650 |
my $table1_exists; |
|
651 |
my $table2_exists; |
|
652 |
foreach my $table (@$tables) { |
|
653 |
$table1_exists = 1 if $table eq $table1; |
|
654 |
$table2_exists = 1 if $table eq $table2; |
|
655 |
} |
|
DBIx::Custom::Model select()...
|
656 |
unshift @$tables, $table1 unless $table1_exists; |
657 |
unshift @$tables, $table2 unless $table2_exists; |
|
add experimental DBIx::Custo...
|
658 |
} |
659 |
} |
|
packaging one directory
|
660 |
|
cleanup
|
661 |
# SQL stack |
662 |
my @sql; |
|
663 |
|
|
664 |
push @sql, 'select'; |
|
packaging one directory
|
665 |
|
add experimental selection o...
|
666 |
if ($selection) { |
667 |
push @sql, $selection; |
|
remove experimental DBIx::Cu...
|
668 |
push @left_join_tables, @{$self->_tables($selection)}; |
add experimental selection o...
|
669 |
} |
670 |
else { |
|
671 |
# Column clause |
|
672 |
if (@$columns) { |
|
673 |
foreach my $column (@$columns) { |
|
remove experimental DBIx::Cu...
|
674 |
push @left_join_tables, @{$self->_tables($column)}; |
add experimental selection o...
|
675 |
push @sql, ($column, ','); |
676 |
} |
|
677 |
pop @sql if $sql[-1] eq ','; |
|
678 |
} |
|
679 |
else { push @sql, '*' } |
|
680 |
|
|
681 |
# Table |
|
682 |
croak qq{"table" option must be specified} unless @$tables; |
|
683 |
push @sql, 'from'; |
|
684 |
foreach my $table (@$tables) { |
|
685 |
push @sql, ($table, ','); |
|
packaging one directory
|
686 |
} |
cleanup
|
687 |
pop @sql if $sql[-1] eq ','; |
packaging one directory
|
688 |
} |
689 |
|
|
select() where can't receive...
|
690 |
# Where |
691 |
my $w; |
|
added experimental DBIx::Cus...
|
692 |
if (ref $where eq 'HASH') { |
select() where can't receive...
|
693 |
my $clause = ['and']; |
694 |
push @$clause, "{= $_}" for keys %$where; |
|
695 |
$w = $self->where; |
|
696 |
$w->clause($clause); |
|
697 |
$w->param($where); |
|
added experimental DBIx::Cus...
|
698 |
} |
699 |
elsif (ref $where eq 'DBIx::Custom::Where') { |
|
select() where can't receive...
|
700 |
$w = $where; |
701 |
$where = $w->param; |
|
packaging one directory
|
702 |
} |
703 |
|
|
select() where can't receive...
|
704 |
croak qq{"where" must be hash reference or DBIx::Custom::Where object} |
705 |
unless ref $w eq 'DBIx::Custom::Where'; |
|
706 |
|
|
707 |
# String where |
|
708 |
my $swhere = "$w"; |
|
remove experimental DBIx::Cu...
|
709 |
|
710 |
# Table name in Where |
|
711 |
unshift @left_join_tables, @{$self->_tables($swhere)}; |
|
712 |
|
|
713 |
# Left join |
|
714 |
if (@$left_join) { |
|
715 |
for (my $i = 0; $i < @$left_join; $i += 2) { |
|
716 |
my $column1 = $left_join->[$i]; |
|
717 |
my $column2 = $left_join->[$i + 1]; |
|
718 |
|
|
719 |
my $table1 = (split (/\./, $column1))[0]; |
|
720 |
my $table2 = (split (/\./, $column2))[0]; |
|
721 |
|
|
722 |
my $table1_exists; |
|
723 |
my $table2_exists; |
|
724 |
|
|
725 |
foreach my $table (@left_join_tables) { |
|
726 |
$table1_exists = 1 if $table eq $table1; |
|
727 |
$table2_exists = 1 if $table eq $table2; |
|
728 |
} |
|
729 |
|
|
730 |
if ($table1_exists && $table2_exists) { |
|
731 |
push @sql, "left outer join $table2 on $column1 = $column2"; |
|
732 |
} |
|
733 |
} |
|
734 |
} |
|
735 |
|
|
736 |
# Add where |
|
cleanup
|
737 |
push @sql, $swhere; |
select() where can't receive...
|
738 |
|
added commit method
|
739 |
# Relation |
DBIx::Custom::Model select()...
|
740 |
if (!$selection && keys %$relation) { |
cleanup
|
741 |
push @sql, $swhere eq '' ? 'where' : 'and'; |
742 |
foreach my $rcolumn (keys %$relation) { |
|
add experimental DBIx::Custo...
|
743 |
my $table1 = (split (/\./, $rcolumn))[0]; |
744 |
my $table2 = (split (/\./, $relation->{$rcolumn}))[0]; |
|
745 |
push @$tables, ($table1, $table2); |
|
746 |
|
|
cleanup
|
747 |
push @sql, ("$rcolumn = " . $relation->{$rcolumn}, 'and'); |
packaging one directory
|
748 |
} |
749 |
} |
|
cleanup
|
750 |
pop @sql if $sql[-1] eq 'and'; |
remove experimental DBIx::Cu...
|
751 |
|
cleanup
|
752 |
# Append statement |
753 |
push @sql, $append if $append; |
|
754 |
|
|
755 |
# SQL |
|
756 |
my $sql = join (' ', @sql); |
|
packaging one directory
|
757 |
|
added experimental sugar met...
|
758 |
# Create query |
cleanup
|
759 |
my $query = $self->create_query($sql); |
added experimental sugar met...
|
760 |
return $query if $args{query}; |
761 |
|
|
remove experimental DBIx::Cu...
|
762 |
unshift @$tables, @left_join_tables; |
763 |
|
|
packaging one directory
|
764 |
# Execute query |
added auto_filter method
|
765 |
my $result = $self->execute( |
select() where can't receive...
|
766 |
$query, param => $where, filter => $filter, |
767 |
table => $tables); |
|
packaging one directory
|
768 |
|
769 |
return $result; |
|
770 |
} |
|
771 | ||
add experimental update_at()...
|
772 |
our %VALID_SELECT_AT_ARGS |
773 |
= map { $_ => 1 } qw/table column where append relation filter query selection |
|
remove experimental DBIx::Cu...
|
774 |
param primary_key left_join/; |
add experimental update_at()...
|
775 | |
776 |
sub select_at { |
|
777 |
my ($self, %args) = @_; |
|
778 |
|
|
779 |
# Check arguments |
|
780 |
foreach my $name (keys %args) { |
|
781 |
croak qq{"$name" is invalid argument} |
|
782 |
unless $VALID_SELECT_AT_ARGS{$name}; |
|
783 |
} |
|
784 |
|
|
785 |
# Primary key |
|
786 |
my $primary_keys = delete $args{primary_key}; |
|
787 |
$primary_keys = [$primary_keys] unless ref $primary_keys; |
|
788 |
|
|
DBIx::Custom::Model select()...
|
789 |
# Table |
790 |
croak qq{"table" option must be specified} unless $args{table}; |
|
791 |
my $table = ref $args{table} ? $args{table}->[-1] : $args{table}; |
|
792 |
|
|
add experimental update_at()...
|
793 |
# Where clause |
794 |
my $where = {}; |
|
795 |
if (exists $args{where}) { |
|
796 |
my $where_columns = delete $args{where}; |
|
- added experimental DBIx::C...
|
797 |
|
798 |
croak qq{"where" must be constant value or array reference} |
|
799 |
unless !ref $where_columns || ref $where_columns eq 'ARRAY'; |
|
800 |
|
|
add experimental update_at()...
|
801 |
$where_columns = [$where_columns] unless ref $where_columns; |
802 |
|
|
803 |
for(my $i = 0; $i < @$primary_keys; $i ++) { |
|
DBIx::Custom::Model select()...
|
804 |
$where->{$table . '.' . $primary_keys->[$i]} = $where_columns->[$i]; |
add experimental update_at()...
|
805 |
} |
806 |
} |
|
- added experimental DBIx::C...
|
807 |
|
808 |
if (exists $args{param}) { |
|
add experimental update_at()...
|
809 |
my $param = delete $args{param}; |
810 |
for(my $i = 0; $i < @$primary_keys; $i ++) { |
|
- added experimental DBIx::C...
|
811 |
delete $param->{$primary_keys->[$i]}; |
add experimental update_at()...
|
812 |
} |
813 |
} |
|
814 |
|
|
815 |
return $self->select(where => $where, %args); |
|
816 |
} |
|
817 | ||
add feture. all model class ...
|
818 |
sub model { |
819 |
my ($self, $name, $model) = @_; |
|
removed experimental base_ta...
|
820 |
|
821 |
# Set |
|
add feture. all model class ...
|
822 |
if ($model) { |
add models() attribute
|
823 |
$self->models->{$name} = $model; |
removed experimental base_ta...
|
824 |
return $self; |
825 |
} |
|
826 |
|
|
add feture. all model class ...
|
827 |
# Check model existance |
828 |
croak qq{Model "$name" is not included} |
|
add models() attribute
|
829 |
unless $self->models->{$name}; |
removed experimental base_ta...
|
830 |
|
831 |
# Get |
|
add models() attribute
|
832 |
return $self->models->{$name}; |
removed experimental base_ta...
|
833 |
} |
834 | ||
add feture. all model class ...
|
835 |
sub include_model { |
836 |
my ($self, $name_space, $model_infos) = @_; |
|
remove DBIx::Custom::Model
|
837 |
|
add feture. all model class ...
|
838 |
$name_space ||= ''; |
839 |
unless ($model_infos) { |
|
840 |
# Load name space module |
|
841 |
croak qq{"$name_space" is invalid class name} |
|
842 |
if $name_space =~ /[^\w:]/; |
|
843 |
eval "use $name_space"; |
|
844 |
croak qq{Name space module "$name_space.pm" is needed. $@} if $@; |
|
table object call dbi object...
|
845 |
|
add feture. all model class ...
|
846 |
# Search model modules |
847 |
my $path = $INC{"$name_space.pm"}; |
|
848 |
$path =~ s/\.pm$//; |
|
849 |
opendir my $dh, $path |
|
850 |
or croak qq{Can't open directory "$path": $!}; |
|
851 |
$model_infos = []; |
|
852 |
while (my $module = readdir $dh) { |
|
853 |
push @$model_infos, $module |
|
854 |
if $module =~ s/\.pm$//; |
|
removed experimental base_ta...
|
855 |
} |
add feture. all model class ...
|
856 |
|
857 |
close $dh; |
|
858 |
} |
|
859 |
|
|
860 |
foreach my $model_info (@$model_infos) { |
|
861 |
|
|
add experimental DBIx::Custo...
|
862 |
# Model class, name, table |
add feture. all model class ...
|
863 |
my $model_class; |
add experimental DBIx::Custo...
|
864 |
my $model_name; |
865 |
my $model_table; |
|
add feture. all model class ...
|
866 |
if (ref $model_info eq 'HASH') { |
add experimental DBIx::Custo...
|
867 |
$model_class = $model_info->{class}; |
868 |
$model_name = $model_info->{name}; |
|
869 |
$model_table = $model_info->{table}; |
|
870 |
|
|
871 |
$model_name ||= $model_class; |
|
872 |
$model_table ||= $model_name; |
|
add feture. all model class ...
|
873 |
} |
add experimental DBIx::Custo...
|
874 |
else { $model_class =$model_name = $model_table = $model_info } |
add feture. all model class ...
|
875 |
my $mclass = "${name_space}::$model_class"; |
table object call dbi object...
|
876 |
|
removed experimental base_ta...
|
877 |
# Load |
add feture. all model class ...
|
878 |
croak qq{"$mclass" is invalid class name} |
879 |
if $mclass =~ /[^\w:]/; |
|
880 |
unless ($mclass->can('isa')) { |
|
881 |
eval "use $mclass"; |
|
removed experimental base_ta...
|
882 |
croak $@ if $@; |
883 |
} |
|
table object call dbi object...
|
884 |
|
removed experimental base_ta...
|
885 |
# Instantiate |
add feture. all model class ...
|
886 |
my $model = $mclass->new(dbi => $self); |
add experimental DBIx::Custo...
|
887 |
$model->name($model_name) unless $model->name; |
888 |
$model->table($model_table) unless $model->table; |
|
removed experimental DBIx::C...
|
889 |
|
removed experimental base_ta...
|
890 |
# Set |
add experimental DBIx::Custo...
|
891 |
$self->model($model->name, $model); |
add experimental DBIx::Custo...
|
892 |
|
893 |
# Apply filter |
|
all filter can receive array...
|
894 |
croak "${name_space}::$model_class filter must be array reference" |
895 |
unless ref $model->filter eq 'ARRAY'; |
|
896 |
$self->apply_filter($model->table, @{$model->filter}); |
|
table object call dbi object...
|
897 |
} |
removed experimental base_ta...
|
898 |
return $self; |
remove DBIx::Custom::Model
|
899 |
} |
900 | ||
add experimental setup_model...
|
901 |
sub setup_model { |
902 |
my $self = shift; |
|
903 |
|
|
904 |
$self->each_column( |
|
905 |
sub { |
|
906 |
my ($self, $table, $column, $column_info) = @_; |
|
907 |
|
|
908 |
if (my $model = $self->models->{$table}) { |
|
909 |
push @{$model->columns}, $column; |
|
910 |
} |
|
911 |
} |
|
912 |
); |
|
add experimental DBIx::Custo...
|
913 |
return $self; |
add experimental setup_model...
|
914 |
} |
915 | ||
cleanup
|
916 |
our %VALID_UPDATE_ARGS |
renamed auto_filter to apply...
|
917 |
= map { $_ => 1 } qw/table param |
added experimental sugar met...
|
918 |
where append filter allow_update_all query/; |
cleanup
|
919 | |
920 |
sub update { |
|
921 |
my ($self, %args) = @_; |
|
version 0.0901
|
922 |
|
cleanup
|
923 |
# Check arguments |
924 |
foreach my $name (keys %args) { |
|
925 |
croak qq{"$name" is invalid argument} |
|
926 |
unless $VALID_UPDATE_ARGS{$name}; |
|
removed reconnect method
|
927 |
} |
added cache_method attribute
|
928 |
|
cleanup
|
929 |
# Arguments |
930 |
my $table = $args{table} || ''; |
|
added table not specified ex...
|
931 |
croak qq{"table" option must be specified} unless $table; |
cleanup
|
932 |
my $param = $args{param} || {}; |
933 |
my $where = $args{where} || {}; |
|
select() where can't receive...
|
934 |
my $append = $args{append} || ''; |
cleanup
|
935 |
my $filter = $args{filter}; |
936 |
my $allow_update_all = $args{allow_update_all}; |
|
version 0.0901
|
937 |
|
cleanup
|
938 |
# Update keys |
select() where can't receive...
|
939 |
my @clumns = keys %$param; |
940 | ||
941 |
# Columns |
|
942 |
my @columns; |
|
943 |
my $safety = $self->safety_column_name; |
|
944 |
foreach my $column (keys %$param) { |
|
945 |
croak qq{"$column" is not safety column name} |
|
946 |
unless $column =~ /$safety/; |
|
947 |
push @columns, $column; |
|
948 |
} |
|
949 |
|
|
cleanup
|
950 |
# Update clause |
select() where can't receive...
|
951 |
my $update_clause = '{update_param ' . join(' ', @clumns) . '}'; |
improved delete() and update...
|
952 | |
953 |
# Where |
|
954 |
my $w; |
|
955 |
if (ref $where eq 'HASH') { |
|
956 |
my $clause = ['and']; |
|
957 |
push @$clause, "{= $_}" for keys %$where; |
|
958 |
$w = $self->where; |
|
959 |
$w->clause($clause); |
|
960 |
$w->param($where); |
|
961 |
} |
|
select() where can't receive...
|
962 |
elsif (ref $where eq 'DBIx::Custom::Where') { |
963 |
$w = $where; |
|
964 |
$where = $w->param; |
|
965 |
} |
|
removed experimental registe...
|
966 |
|
improved delete() and update...
|
967 |
croak qq{"where" must be hash refernce or DBIx::Custom::Where object} |
968 |
unless ref $w eq 'DBIx::Custom::Where'; |
|
removed reconnect method
|
969 |
|
select() where can't receive...
|
970 |
# String where |
971 |
my $swhere = "$w"; |
|
improved delete() and update...
|
972 |
|
973 |
croak qq{"where" must be specified} |
|
select() where can't receive...
|
974 |
if "$swhere" eq '' && !$allow_update_all; |
removed reconnect method
|
975 |
|
cleanup
|
976 |
# SQL stack |
977 |
my @sql; |
|
978 |
|
|
979 |
# Update |
|
980 |
push @sql, "update $table $update_clause $swhere"; |
|
981 |
push @sql, $append if $append; |
|
removed reconnect method
|
982 |
|
cleanup
|
983 |
# Rearrange parameters |
improved delete() and update...
|
984 |
foreach my $wkey (keys %$where) { |
removed reconnect method
|
985 |
|
cleanup
|
986 |
if (exists $param->{$wkey}) { |
987 |
$param->{$wkey} = [$param->{$wkey}] |
|
988 |
unless ref $param->{$wkey} eq 'ARRAY'; |
|
989 |
|
|
990 |
push @{$param->{$wkey}}, $where->{$wkey}; |
|
991 |
} |
|
992 |
else { |
|
993 |
$param->{$wkey} = $where->{$wkey}; |
|
994 |
} |
|
removed reconnect method
|
995 |
} |
cleanup
|
996 |
|
cleanup
|
997 |
# SQL |
998 |
my $sql = join(' ', @sql); |
|
999 |
|
|
added experimental sugar met...
|
1000 |
# Create query |
cleanup
|
1001 |
my $query = $self->create_query($sql); |
added experimental sugar met...
|
1002 |
return $query if $args{query}; |
1003 |
|
|
cleanup
|
1004 |
# Execute query |
added experimental sugar met...
|
1005 |
my $ret_val = $self->execute($query, param => $param, |
added auto_filter method
|
1006 |
filter => $filter, |
renamed auto_filter to apply...
|
1007 |
table => $table); |
cleanup
|
1008 |
|
1009 |
return $ret_val; |
|
removed reconnect method
|
1010 |
} |
1011 | ||
cleanup
|
1012 |
sub update_all { shift->update(allow_update_all => 1, @_) }; |
1013 | ||
add experimental update_at()...
|
1014 |
our %VALID_UPDATE_AT_ARGS |
1015 |
= map { $_ => 1 } qw/table param |
|
1016 |
where append filter query |
|
1017 |
primary_key param/; |
|
1018 | ||
1019 |
sub update_at { |
|
1020 |
my ($self, %args) = @_; |
|
1021 |
|
|
1022 |
# Check arguments |
|
1023 |
foreach my $name (keys %args) { |
|
1024 |
croak qq{"$name" is invalid argument} |
|
1025 |
unless $VALID_UPDATE_AT_ARGS{$name}; |
|
1026 |
} |
|
1027 |
|
|
1028 |
# Primary key |
|
1029 |
my $primary_keys = delete $args{primary_key}; |
|
1030 |
$primary_keys = [$primary_keys] unless ref $primary_keys; |
|
1031 |
|
|
1032 |
# Where clause |
|
1033 |
my $where = {}; |
|
1034 |
my $param = {}; |
|
1035 |
|
|
1036 |
if (exists $args{where}) { |
|
1037 |
my $where_columns = delete $args{where}; |
|
1038 |
$where_columns = [$where_columns] unless ref $where_columns; |
|
- added experimental DBIx::C...
|
1039 | |
1040 |
croak qq{"where" must be constant value or array reference} |
|
1041 |
unless !ref $where_columns || ref $where_columns eq 'ARRAY'; |
|
add experimental update_at()...
|
1042 |
|
1043 |
for(my $i = 0; $i < @$primary_keys; $i ++) { |
|
1044 |
$where->{$primary_keys->[$i]} = $where_columns->[$i]; |
|
1045 |
} |
|
1046 |
} |
|
- added experimental DBIx::C...
|
1047 |
|
1048 |
if (exists $args{param}) { |
|
add experimental update_at()...
|
1049 |
$param = delete $args{param}; |
1050 |
for(my $i = 0; $i < @$primary_keys; $i ++) { |
|
- added experimental DBIx::C...
|
1051 |
delete $param->{$primary_keys->[$i]}; |
add experimental update_at()...
|
1052 |
} |
1053 |
} |
|
1054 |
|
|
1055 |
return $self->update(where => $where, param => $param, %args); |
|
1056 |
} |
|
1057 | ||
remove experimental DBIx::Cu...
|
1058 |
sub update_param { |
1059 |
my ($self, $param) = @_; |
|
1060 |
|
|
added experimental update_pa...
|
1061 |
# Update parameter tag |
remove experimental DBIx::Cu...
|
1062 |
my @tag; |
1063 |
push @tag, '{update_param'; |
|
added experimental update_pa...
|
1064 |
my $safety = $self->safety_column_name; |
remove experimental DBIx::Cu...
|
1065 |
foreach my $column (keys %$param) { |
added experimental update_pa...
|
1066 |
croak qq{"$column" is not safety column name} |
1067 |
unless $column =~ /$safety/; |
|
remove experimental DBIx::Cu...
|
1068 |
push @tag, $column; |
1069 |
} |
|
1070 |
push @tag, '}'; |
|
1071 |
|
|
1072 |
return join ' ', @tag; |
|
1073 |
} |
|
1074 | ||
cleanup
|
1075 |
sub where { |
select() where can't receive...
|
1076 |
my $self = shift; |
1077 | ||
1078 |
return DBIx::Custom::Where->new( |
|
1079 |
query_builder => $self->query_builder, |
|
1080 |
safety_column_name => $self->safety_column_name |
|
1081 |
); |
|
cleanup
|
1082 |
} |
added experimental DBIx::Cus...
|
1083 | |
added experimental not_exist...
|
1084 |
sub _bind { |
cleanup
|
1085 |
my ($self, $params, $columns, $filter) = @_; |
removed reconnect method
|
1086 |
|
cleanup
|
1087 |
# bind values |
added experimental not_exist...
|
1088 |
my @bind; |
add tests
|
1089 |
|
removed reconnect method
|
1090 |
# Build bind values |
1091 |
my $count = {}; |
|
added experimental not_exist...
|
1092 |
my $not_exists = {}; |
cleanup
|
1093 |
foreach my $column (@$columns) { |
removed reconnect method
|
1094 |
|
1095 |
# Value |
|
added experimental not_exist...
|
1096 |
my $value; |
1097 |
if(ref $params->{$column} eq 'ARRAY') { |
|
1098 |
my $i = $count->{$column} || 0; |
|
1099 |
$i += $not_exists->{$column} || 0; |
|
1100 |
my $found; |
|
1101 |
for (my $k = $i; $i < @{$params->{$column}}; $k++) { |
|
1102 |
if (ref $params->{$column}->[$k] eq 'DBIx::Custom::NotExists') { |
|
1103 |
$not_exists->{$column}++; |
|
1104 |
} |
|
1105 |
else { |
|
1106 |
$value = $params->{$column}->[$k]; |
|
1107 |
$found = 1; |
|
1108 |
last |
|
1109 |
} |
|
1110 |
} |
|
1111 |
next unless $found; |
|
1112 |
} |
|
1113 |
else { $value = $params->{$column} } |
|
removed reconnect method
|
1114 |
|
cleanup
|
1115 |
# Filter |
1116 |
my $f = $filter->{$column} || $self->{default_out_filter} || ''; |
|
cleanup
|
1117 |
|
added experimental not_exist...
|
1118 |
push @bind, $f ? $f->($value) : $value; |
removed reconnect method
|
1119 |
|
1120 |
# Count up |
|
1121 |
$count->{$column}++; |
|
1122 |
} |
|
1123 |
|
|
added experimental not_exist...
|
1124 |
return \@bind; |
removed reconnect method
|
1125 |
} |
1126 | ||
cleanup
|
1127 |
sub _croak { |
1128 |
my ($self, $error, $append) = @_; |
|
1129 |
$append ||= ""; |
|
1130 |
|
|
1131 |
# Verbose |
|
1132 |
if ($Carp::Verbose) { croak $error } |
|
1133 |
|
|
1134 |
# Not verbose |
|
1135 |
else { |
|
1136 |
|
|
1137 |
# Remove line and module infromation |
|
1138 |
my $at_pos = rindex($error, ' at '); |
|
1139 |
$error = substr($error, 0, $at_pos); |
|
1140 |
$error =~ s/\s+$//; |
|
1141 |
|
|
1142 |
croak "$error$append"; |
|
1143 |
} |
|
1144 |
} |
|
1145 | ||
remove experimental DBIx::Cu...
|
1146 |
sub _tables { |
1147 |
my ($self, $source) = @_; |
|
1148 |
|
|
1149 |
my $tables = []; |
|
1150 |
|
|
1151 |
my $safety_name = $self->safety_column_name; |
|
1152 |
|
|
1153 |
while ($source =~ /\b(\w+)\./g) { |
|
1154 |
push @$tables, $1; |
|
1155 |
} |
|
1156 |
|
|
1157 |
return $tables; |
|
1158 |
} |
|
1159 | ||
cleanup
|
1160 |
# DEPRECATED! |
cleanup
|
1161 |
__PACKAGE__->attr( |
1162 |
dbi_options => sub { {} }, |
|
1163 |
filter_check => 1 |
|
1164 |
); |
|
renamed dbi_options to dbi_o...
|
1165 | |
cleanup
|
1166 |
# DEPRECATED! |
cleanup
|
1167 |
sub default_bind_filter { |
1168 |
my $self = shift; |
|
1169 |
|
|
1170 |
if (@_) { |
|
1171 |
my $fname = $_[0]; |
|
1172 |
|
|
1173 |
if (@_ && !$fname) { |
|
1174 |
$self->{default_out_filter} = undef; |
|
1175 |
} |
|
1176 |
else { |
|
many changed
|
1177 |
croak qq{Filter "$fname" is not registered} |
cleanup
|
1178 |
unless exists $self->filters->{$fname}; |
1179 |
|
|
1180 |
$self->{default_out_filter} = $self->filters->{$fname}; |
|
1181 |
} |
|
1182 |
return $self; |
|
1183 |
} |
|
1184 |
|
|
1185 |
return $self->{default_out_filter}; |
|
1186 |
} |
|
1187 | ||
cleanup
|
1188 |
# DEPRECATED! |
cleanup
|
1189 |
sub default_fetch_filter { |
1190 |
my $self = shift; |
|
1191 |
|
|
1192 |
if (@_) { |
|
many changed
|
1193 |
my $fname = $_[0]; |
1194 | ||
cleanup
|
1195 |
if (@_ && !$fname) { |
1196 |
$self->{default_in_filter} = undef; |
|
1197 |
} |
|
1198 |
else { |
|
many changed
|
1199 |
croak qq{Filter "$fname" is not registered} |
cleanup
|
1200 |
unless exists $self->filters->{$fname}; |
1201 |
|
|
1202 |
$self->{default_in_filter} = $self->filters->{$fname}; |
|
1203 |
} |
|
1204 |
|
|
1205 |
return $self; |
|
1206 |
} |
|
1207 |
|
|
many changed
|
1208 |
return $self->{default_in_filter}; |
cleanup
|
1209 |
} |
1210 | ||
cleanup
|
1211 |
# DEPRECATED! |
renamed DBIx::Custom::TagPro...
|
1212 |
sub register_tag_processor { |
1213 |
return shift->query_builder->register_tag_processor(@_); |
|
1214 |
} |
|
1215 | ||
fixed DBIx::Custom::QueryBui...
|
1216 |
1; |
1217 | ||
removed reconnect method
|
1218 |
=head1 NAME |
1219 | ||
renamed build_query to creat...
|
1220 |
DBIx::Custom - DBI interface, having hash parameter binding and filtering system |
removed reconnect method
|
1221 | |
1222 |
=head1 SYNOPSYS |
|
cleanup
|
1223 | |
renamed build_query to creat...
|
1224 |
use DBIx::Custom; |
renamed update tag to update...
|
1225 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname", |
renamed dbi_options to dbi_o...
|
1226 |
user => 'ken', password => '!LFKD%$&', |
1227 |
dbi_option => {mysql_enable_utf8 => 1}); |
|
cleanup
|
1228 | |
removed reconnect method
|
1229 |
# Insert |
added insert, update, update...
|
1230 |
$dbi->insert(table => 'book', |
renamed update tag to update...
|
1231 |
param => {title => 'Perl', author => 'Ken'}, |
all filter can receive array...
|
1232 |
filter => [title => 'to_something']); |
removed reconnect method
|
1233 |
|
1234 |
# Update |
|
added insert, update, update...
|
1235 |
$dbi->update(table => 'book', |
renamed update tag to update...
|
1236 |
param => {title => 'Perl', author => 'Ken'}, |
removed reconnect method
|
1237 |
where => {id => 5}, |
all filter can receive array...
|
1238 |
filter => [title => 'to_something']); |
removed reconnect method
|
1239 |
|
1240 |
# Update all |
|
added insert, update, update...
|
1241 |
$dbi->update_all(table => 'book', |
renamed update tag to update...
|
1242 |
param => {title => 'Perl'}, |
all filter can receive array...
|
1243 |
filter => [title => 'to_something']); |
removed reconnect method
|
1244 |
|
1245 |
# Delete |
|
added insert, update, update...
|
1246 |
$dbi->delete(table => 'book', |
removed reconnect method
|
1247 |
where => {author => 'Ken'}, |
all filter can receive array...
|
1248 |
filter => [title => 'to_something']); |
removed reconnect method
|
1249 |
|
1250 |
# Delete all |
|
added insert, update, update...
|
1251 |
$dbi->delete_all(table => 'book'); |
cleanup
|
1252 | |
removed reconnect method
|
1253 |
# Select |
renamed fetch_rows to fetch_...
|
1254 |
my $result = $dbi->select( |
added insert, update, update...
|
1255 |
table => 'book', |
update document
|
1256 |
column => [qw/author title/], |
1257 |
where => {author => 'Ken'}, |
|
renamed dbi_options to dbi_o...
|
1258 |
relation => {'book.id' => 'rental.book_id'}, |
updated document
|
1259 |
append => 'order by id limit 5', |
all filter can receive array...
|
1260 |
filter => [title => 'to_something'] |
added commit method
|
1261 |
); |
cleanup
|
1262 | |
renamed build_query to creat...
|
1263 |
# Execute SQL |
added insert, update, update...
|
1264 |
$dbi->execute("select title from book"); |
removed register_format()
|
1265 |
|
renamed build_query to creat...
|
1266 |
# Execute SQL with hash binding and filtering |
added insert, update, update...
|
1267 |
$dbi->execute("select id from book where {= author} and {like title}", |
removed register_format()
|
1268 |
param => {author => 'ken', title => '%Perl%'}, |
all filter can receive array...
|
1269 |
filter => [title => 'to_something']); |
removed reconnect method
|
1270 | |
1271 |
# Create query and execute it |
|
renamed build_query to creat...
|
1272 |
my $query = $dbi->create_query( |
added insert, update, update...
|
1273 |
"select id from book where {= author} and {like title}" |
removed reconnect method
|
1274 |
); |
updated document
|
1275 |
$dbi->execute($query, param => {author => 'Ken', title => '%Perl%'}) |
cleanup
|
1276 | |
1277 |
# Get DBI object |
|
1278 |
my $dbh = $dbi->dbh; |
|
1279 | ||
removed register_format()
|
1280 |
# Fetch |
1281 |
while (my $row = $result->fetch) { |
|
1282 |
# ... |
|
1283 |
} |
|
1284 |
|
|
1285 |
# Fetch hash |
|
1286 |
while (my $row = $result->fetch_hash) { |
|
1287 |
|
|
1288 |
} |
|
1289 |
|
|
renamed update tag to update...
|
1290 |
=head1 DESCRIPTIONS |
removed reconnect method
|
1291 | |
renamed build_query to creat...
|
1292 |
L<DBIx::Custom> is one of L<DBI> interface modules, |
1293 |
such as L<DBIx::Class>, L<DBIx::Simple>. |
|
removed reconnect method
|
1294 | |
renamed build_query to creat...
|
1295 |
This module is not O/R mapper. O/R mapper is useful, |
1296 |
but you must learn many syntax of the O/R mapper, |
|
updated document
|
1297 |
which is almost another language. |
1298 |
Created SQL statement is offten not effcient and damage SQL performance. |
|
renamed build_query to creat...
|
1299 |
so you have to execute raw SQL in the end. |
removed reconnect method
|
1300 | |
renamed build_query to creat...
|
1301 |
L<DBIx::Custom> is middle area between L<DBI> and O/R mapper. |
updated document
|
1302 |
L<DBIx::Custom> provide flexible hash parameter binding and filtering system, |
added experimental expand me...
|
1303 |
and suger methods, such as C<insert()>, C<update()>, C<delete()>, C<select()> |
updated document
|
1304 |
to execute SQL easily. |
removed reconnect method
|
1305 | |
updated document
|
1306 |
L<DBIx::Custom> respects SQL. SQL is very complex and not beautiful, |
1307 |
but de-facto standard, |
|
1308 |
so all people learing database know it. |
|
renamed update tag to update...
|
1309 |
If you already know SQL, |
1310 |
you learn a little thing to use L<DBIx::Custom>. |
|
removed reconnect method
|
1311 | |
pod fix
|
1312 |
See L<DBIx::Custom::Guide> for more details. |
1313 | ||
1314 |
=head1 GUIDE |
|
1315 | ||
1316 |
L<DBIx::Custom::Guide> - L<DBIx::Custom> complete guide |
|
1317 | ||
1318 |
=head1 EXAMPLES |
|
1319 | ||
1320 |
L<DBIx::Custom Wiki|https://github.com/yuki-kimoto/DBIx-Custom/wiki> - Many useful examples |
|
updated document
|
1321 | |
update document
|
1322 |
=head1 ATTRIBUTES |
packaging one directory
|
1323 | |
cleanup
|
1324 |
=head2 C<cache> |
packaging one directory
|
1325 | |
cleanup
|
1326 |
my $cache = $dbi->cache; |
1327 |
$dbi = $dbi->cache(1); |
|
removed DESTROY method(not b...
|
1328 | |
cleanup
|
1329 |
Enable parsed L<DBIx::Custom::Query> object caching. |
1330 |
Default to 1. |
|
packaging one directory
|
1331 | |
removed DBIx::Custom commit ...
|
1332 |
=head2 C<data_source> |
packaging one directory
|
1333 | |
cleanup
|
1334 |
my $data_source = $dbi->data_source; |
cleanup
|
1335 |
$dbi = $dbi->data_source("DBI:mysql:database=dbname"); |
removed DESTROY method(not b...
|
1336 | |
cleanup
|
1337 |
Data source. |
1338 |
C<connect()> method use this value to connect the database. |
|
removed DESTROY method(not b...
|
1339 | |
removed DBIx::Custom commit ...
|
1340 |
=head2 C<dbh> |
packaging one directory
|
1341 | |
cleanup
|
1342 |
my $dbh = $dbi->dbh; |
1343 |
$dbi = $dbi->dbh($dbh); |
|
packaging one directory
|
1344 | |
cleanup
|
1345 |
L<DBI> object. You can call all methods of L<DBI>. |
packaging one directory
|
1346 | |
renamed dbi_options to dbi_o...
|
1347 |
=head2 C<dbi_option> |
added dbi_options attribute
|
1348 | |
renamed dbi_options to dbi_o...
|
1349 |
my $dbi_option = $dbi->dbi_option; |
1350 |
$dbi = $dbi->dbi_option($dbi_option); |
|
added dbi_options attribute
|
1351 | |
1352 |
DBI options. |
|
add default_dbi_option()
|
1353 | |
1354 |
Each option specified can ovewrite C<default_dbi_option>. |
|
1355 | ||
1356 |
C<connect()> method use this value to connect the database. |
|
1357 | ||
1358 | ||
1359 |
=head2 C<default_dbi_option> |
|
1360 | ||
1361 |
my $default_dbi_option = $dbi->default_dbi_option; |
|
1362 |
$dbi = $dbi->default_dbi_option($default_dbi_option); |
|
1363 | ||
1364 |
DBI default options. |
|
1365 | ||
1366 |
RaiseError => 1, |
|
1367 |
PrintError => 0, |
|
1368 |
AutoCommit => 1, |
|
1369 | ||
added dbi_options attribute
|
1370 |
C<connect()> method use this value to connect the database. |
1371 | ||
cleanup
|
1372 |
Default filter when row is fetched. |
packaging one directory
|
1373 | |
cleanup
|
1374 |
=head2 C<filters> |
bind_filter argument is chan...
|
1375 | |
cleanup
|
1376 |
my $filters = $dbi->filters; |
1377 |
$dbi = $dbi->filters(\%filters); |
|
packaging one directory
|
1378 | |
add models() attribute
|
1379 |
Filters |
1380 | ||
1381 |
=head2 C<(experimental) models> |
|
1382 | ||
1383 |
my $models = $dbi->models; |
|
1384 |
$dbi = $dbi->models(\%models); |
|
1385 | ||
1386 |
Models |
|
1387 | ||
cleanup
|
1388 |
=head2 C<password> |
1389 | ||
1390 |
my $password = $dbi->password; |
|
1391 |
$dbi = $dbi->password('lkj&le`@s'); |
|
1392 | ||
1393 |
Password. |
|
1394 |
C<connect()> method use this value to connect the database. |
|
update document
|
1395 | |
renamed update tag to update...
|
1396 |
=head2 C<query_builder> |
added commit method
|
1397 | |
renamed update tag to update...
|
1398 |
my $sql_class = $dbi->query_builder; |
1399 |
$dbi = $dbi->query_builder(DBIx::Custom::QueryBuilder->new); |
|
added commit method
|
1400 | |
renamed update tag to update...
|
1401 |
SQL builder. C<query_builder()> must be |
renamed build_query to creat...
|
1402 |
the instance of L<DBIx::Custom::QueryBuilder> subclass. |
1403 |
Default to L<DBIx::Custom::QueryBuilder> object. |
|
cleanup
|
1404 | |
cleanup
|
1405 |
=head2 C<result_class> |
cleanup
|
1406 | |
cleanup
|
1407 |
my $result_class = $dbi->result_class; |
1408 |
$dbi = $dbi->result_class('DBIx::Custom::Result'); |
|
cleanup
|
1409 | |
cleanup
|
1410 |
Result class for select statement. |
1411 |
Default to L<DBIx::Custom::Result>. |
|
cleanup
|
1412 | |
update pod
|
1413 |
=head2 C<(experimental) safety_column_name> |
1414 | ||
1415 |
my $safety_column_name = $self->safety_column_name; |
|
1416 |
$dbi = $self->safety_column_name($name); |
|
1417 | ||
1418 |
Safety column name regex. |
|
1419 |
Default is qr/^[\w\.]*$/ |
|
1420 | ||
cleanup
|
1421 |
=head2 C<user> |
cleanup
|
1422 | |
cleanup
|
1423 |
my $user = $dbi->user; |
1424 |
$dbi = $dbi->user('Ken'); |
|
cleanup
|
1425 | |
cleanup
|
1426 |
User name. |
1427 |
C<connect()> method use this value to connect the database. |
|
update pod
|
1428 | |
cleanup
|
1429 |
=head1 METHODS |
added commit method
|
1430 | |
cleanup
|
1431 |
L<DBIx::Custom> inherits all methods from L<Object::Simple> |
autoload DBI method
|
1432 |
and use all method of L<DBI> |
cleanup
|
1433 |
and implements the following new ones. |
added check_filter attribute
|
1434 | |
cleanup
|
1435 |
=head2 C<(experimental) apply_filter> |
added auto_filter method
|
1436 | |
renamed auto_filter to apply...
|
1437 |
$dbi->apply_filter( |
added auto_filter method
|
1438 |
$table, |
fix bug : filter can't over...
|
1439 |
$column1 => {in => $infilter1, out => $outfilter1, end => $endfilter1} |
1440 |
$column2 => {in => $infilter2, out => $outfilter2, end =. $endfilter2} |
|
renamed auto_filter to apply...
|
1441 |
..., |
added auto_filter method
|
1442 |
); |
1443 | ||
renamed auto_filter to apply...
|
1444 |
C<apply_filter> is automatically filter for columns of table. |
added auto_filter method
|
1445 |
This have effect C<insert>, C<update>, C<delete>. C<select> |
cleanup
|
1446 |
and L<DBIx::Custom::Result> object. but this has'nt C<execute> method. |
1447 | ||
cleanup
|
1448 |
If you want to have effect C<execute()> method, use C<table> |
cleanup
|
1449 |
arguments. |
added auto_filter method
|
1450 | |
cleanup
|
1451 |
$result = $dbi->execute( |
1452 |
"select * from table1 where {= key1} and {= key2};", |
|
1453 |
param => {key1 => 1, key2 => 2}, |
|
renamed auto_filter to apply...
|
1454 |
table => ['table1'] |
cleanup
|
1455 |
); |
fix bug : filter can't over...
|
1456 | |
1457 |
You can use three name as column name. |
|
1458 | ||
1459 |
1. column : author |
|
1460 |
2. table.column : book.author |
|
1461 |
3. table__column : book__author |
|
1462 | ||
removed DBIx::Custom commit ...
|
1463 |
=head2 C<connect> |
packaging one directory
|
1464 | |
cleanup
|
1465 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname", |
update document
|
1466 |
user => 'ken', password => '!LFKD%$&'); |
bind_filter argument is chan...
|
1467 | |
cleanup
|
1468 |
Create a new L<DBIx::Custom> object and connect to the database. |
renamed build_query to creat...
|
1469 |
L<DBIx::Custom> is a wrapper of L<DBI>. |
cleanup
|
1470 |
C<AutoCommit> and C<RaiseError> options are true, |
renamed build_query to creat...
|
1471 |
and C<PrintError> option is false by default. |
packaging one directory
|
1472 | |
cleanup
|
1473 |
=head2 C<create_query> |
1474 |
|
|
1475 |
my $query = $dbi->create_query( |
|
added insert, update, update...
|
1476 |
"select * from book where {= author} and {like title};" |
cleanup
|
1477 |
); |
update document
|
1478 | |
cleanup
|
1479 |
Create the instance of L<DBIx::Custom::Query> from the source of SQL. |
1480 |
If you want to get high performance, |
|
1481 |
use C<create_query()> method and execute it by C<execute()> method |
|
1482 |
instead of suger methods. |
|
bind_filter argument is chan...
|
1483 | |
cleanup
|
1484 |
$dbi->execute($query, {author => 'Ken', title => '%Perl%'}); |
version 0.0901
|
1485 | |
cleanup
|
1486 |
=head2 C<execute> |
packaging one directory
|
1487 | |
all filter can receive array...
|
1488 |
my $result = $dbi->execute($query, param => $params, filter => \@filter); |
1489 |
my $result = $dbi->execute($source, param => $params, filter => \@filter); |
|
update document
|
1490 | |
cleanup
|
1491 |
Execute query or the source of SQL. |
1492 |
Query is L<DBIx::Custom::Query> object. |
|
1493 |
Return value is L<DBIx::Custom::Result> if select statement is executed, |
|
1494 |
or the count of affected rows if insert, update, delete statement is executed. |
|
version 0.0901
|
1495 | |
removed DBIx::Custom commit ...
|
1496 |
=head2 C<delete> |
packaging one directory
|
1497 | |
cleanup
|
1498 |
$dbi->delete(table => $table, |
1499 |
where => \%where, |
|
1500 |
append => $append, |
|
all filter can receive array...
|
1501 |
filter => \@filter, |
added experimental sugar met...
|
1502 |
query => 1); |
bind_filter argument is chan...
|
1503 | |
renamed build_query to creat...
|
1504 |
Execute delete statement. |
1505 |
C<delete> method have C<table>, C<where>, C<append>, and C<filter> arguments. |
|
1506 |
C<table> is a table name. |
|
1507 |
C<where> is where clause. this must be hash reference. |
|
1508 |
C<append> is a string added at the end of the SQL statement. |
|
1509 |
C<filter> is filters when parameter binding is executed. |
|
added experimental sugar met...
|
1510 |
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value. |
1511 |
default to 0. This is experimental. |
|
cleanup
|
1512 |
Return value of C<delete()> is the count of affected rows. |
renamed build_query to creat...
|
1513 | |
removed DBIx::Custom commit ...
|
1514 |
=head2 C<delete_all> |
packaging one directory
|
1515 | |
cleanup
|
1516 |
$dbi->delete_all(table => $table); |
packaging one directory
|
1517 | |
renamed build_query to creat...
|
1518 |
Execute delete statement to delete all rows. |
1519 |
Arguments is same as C<delete> method, |
|
1520 |
except that C<delete_all> don't have C<where> argument. |
|
cleanup
|
1521 |
Return value of C<delete_all()> is the count of affected rows. |
bind_filter argument is chan...
|
1522 | |
added experimental DBIx::Cus...
|
1523 |
=head3 C<(experimental) delete_at()> |
add experimental update_at()...
|
1524 | |
1525 |
To delete row by using primary key, use C<delete_at()> |
|
1526 | ||
1527 |
$dbi->delete_at( |
|
1528 |
table => 'book', |
|
1529 |
primary_key => ['id'], |
|
1530 |
where => ['123'] |
|
1531 |
); |
|
1532 | ||
1533 |
In this example, row which id column is 123 is deleted. |
|
1534 |
NOTE that you must pass array reference as C<where>. |
|
1535 | ||
1536 |
You can also write arguments like this. |
|
1537 | ||
1538 |
$dbi->delete_at( |
|
1539 |
table => 'book', |
|
1540 |
primary_key => ['id'], |
|
1541 |
param => {id => '123'} |
|
1542 |
); |
|
1543 | ||
cleanup
|
1544 |
=head2 C<insert> |
1545 | ||
1546 |
$dbi->insert(table => $table, |
|
1547 |
param => \%param, |
|
1548 |
append => $append, |
|
all filter can receive array...
|
1549 |
filter => \@filter, |
added experimental sugar met...
|
1550 |
query => 1); |
cleanup
|
1551 | |
1552 |
Execute insert statement. |
|
1553 |
C<insert> method have C<table>, C<param>, C<append> |
|
1554 |
and C<filter> arguments. |
|
1555 |
C<table> is a table name. |
|
1556 |
C<param> is the pairs of column name value. this must be hash reference. |
|
1557 |
C<append> is a string added at the end of the SQL statement. |
|
1558 |
C<filter> is filters when parameter binding is executed. |
|
added experimental sugar met...
|
1559 |
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value. |
1560 |
default to 0. This is experimental. |
|
cleanup
|
1561 |
This is overwrites C<default_bind_filter>. |
1562 |
Return value of C<insert()> is the count of affected rows. |
|
1563 | ||
added experimental DBIx::Cus...
|
1564 |
=head3 C<(experimental) insert_at()> |
1565 | ||
1566 |
To insert row by using primary key, use C<insert_at()> |
|
1567 | ||
1568 |
$dbi->insert_at( |
|
1569 |
table => 'book', |
|
1570 |
primary_key => ['id'], |
|
1571 |
where => ['123'], |
|
1572 |
param => {name => 'Ken'} |
|
1573 |
); |
|
1574 | ||
1575 |
In this example, row which id column is 123 is inserted. |
|
1576 |
NOTE that you must pass array reference as C<where>. |
|
1577 |
If C<param> contains primary key, |
|
1578 |
the key and value is delete from C<param>. |
|
1579 | ||
added experimental update_pa...
|
1580 |
=head2 C<(experimental) insert_param> |
1581 | ||
1582 |
my $insert_param = $dbi->insert_param({title => 'a', age => 2}); |
|
1583 | ||
1584 |
Create insert parameter tag. |
|
1585 | ||
1586 |
{title => 'a', age => 2} -> {insert_param title age} |
|
1587 | ||
pod fix
|
1588 |
=head2 C<(experimental) each_column> |
added experimental iterate_a...
|
1589 | |
pod fix
|
1590 |
$dbi->each_column( |
added experimental iterate_a...
|
1591 |
sub { |
add experimental setup_model...
|
1592 |
my ($self, $table, $column, $column_info) = @_; |
added experimental iterate_a...
|
1593 |
|
add experimental setup_model...
|
1594 |
my $type = $column_info->{TYPE_NAME}; |
pod fix
|
1595 |
|
1596 |
if ($type eq 'DATE') { |
|
1597 |
# ... |
|
1598 |
} |
|
added experimental iterate_a...
|
1599 |
} |
1600 |
); |
|
pod fix
|
1601 |
Get column informations from database. |
1602 |
Argument is callback. |
|
1603 |
You can do anything in callback. |
|
added experimental not_exist...
|
1604 |
Callback receive four arguments, dbi object, table name, |
add experimental setup_model...
|
1605 |
column name and column information. |
added experimental not_exist...
|
1606 | |
add feture. all model class ...
|
1607 |
=head2 C<(experimental) include_model> |
removed experimental base_ta...
|
1608 | |
add feture. all model class ...
|
1609 |
$dbi->include_model( |
1610 |
'MyModel' => [ |
|
removed experimental base_ta...
|
1611 |
'book', 'person', 'company' |
1612 |
] |
|
1613 |
); |
|
1614 | ||
add feture. all model class ...
|
1615 |
Include models. First argument is name space. |
removed experimental base_ta...
|
1616 |
Second argument is array reference of class base names. |
1617 | ||
add feture. all model class ...
|
1618 |
If you don't specify second argument, All models under name space is |
1619 |
included. |
|
1620 | ||
1621 |
$dbi->include_model('MyModel'); |
|
1622 | ||
1623 |
Note that in this case name spece module is needed. |
|
1624 | ||
1625 |
# MyModel.pm |
|
1626 |
package MyModel; |
|
1627 |
|
|
1628 |
use base 'DBIx::Custom::Model'; |
|
1629 | ||
1630 |
The following model is instantiated and included. |
|
removed experimental base_ta...
|
1631 | |
add feture. all model class ...
|
1632 |
MyModel::book |
1633 |
MyModel::person |
|
1634 |
MyModel::company |
|
removed experimental base_ta...
|
1635 | |
add feture. all model class ...
|
1636 |
You can get these instance by C<model()>. |
removed experimental base_ta...
|
1637 | |
add feture. all model class ...
|
1638 |
my $book_model = $dbi->model('book'); |
removed experimental base_ta...
|
1639 | |
add feture. all model class ...
|
1640 |
If you want to other name as model class, |
removed experimental base_ta...
|
1641 |
you can do like this. |
1642 | ||
add feture. all model class ...
|
1643 |
$dbi->include_model( |
1644 |
'MyModel' => [ |
|
removed experimental base_ta...
|
1645 |
{'book' => 'Book'}, |
1646 |
{'person' => 'Person'} |
|
1647 |
] |
|
1648 |
); |
|
1649 | ||
added experimental not_exist...
|
1650 |
=head2 C<(experimental) method> |
1651 | ||
1652 |
$dbi->method( |
|
1653 |
update_or_insert => sub { |
|
1654 |
my $self = shift; |
|
1655 |
# do something |
|
1656 |
}, |
|
1657 |
find_or_create => sub { |
|
1658 |
my $self = shift; |
|
1659 |
# do something |
|
1660 |
} |
|
1661 |
); |
|
1662 | ||
1663 |
Register method. These method is called from L<DBIx::Custom> object directory. |
|
1664 | ||
1665 |
$dbi->update_or_insert; |
|
1666 |
$dbi->find_or_create; |
|
1667 | ||
1668 |
=head2 C<new> |
|
1669 | ||
1670 |
my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname", |
|
1671 |
user => 'ken', password => '!LFKD%$&'); |
|
1672 | ||
1673 |
Create a new L<DBIx::Custom> object. |
|
1674 | ||
1675 |
=head2 C<(experimental) not_exists> |
|
1676 | ||
1677 |
my $not_exists = $dbi->not_exists; |
|
1678 | ||
1679 |
Get DBIx::Custom::NotExists object. |
|
experimental extended select...
|
1680 | |
cleanup
|
1681 |
=head2 C<register_filter> |
1682 | ||
1683 |
$dbi->register_filter(%filters); |
|
1684 |
$dbi->register_filter(\%filters); |
|
1685 |
|
|
1686 |
Register filter. Registered filters is available in the following attributes |
|
1687 |
or arguments. |
|
1688 | ||
1689 |
=over 4 |
|
1690 | ||
1691 |
=item * |
|
1692 | ||
1693 |
C<filter> argument of C<insert()>, C<update()>, |
|
1694 |
C<update_all()>, C<delete()>, C<delete_all()>, C<select()> |
|
1695 |
methods |
|
1696 | ||
1697 |
=item * |
|
1698 | ||
1699 |
C<execute()> method |
|
1700 | ||
1701 |
=item * |
|
1702 | ||
1703 |
C<default_filter> and C<filter> of C<DBIx::Custom::Query> |
|
1704 | ||
1705 |
=item * |
|
1706 | ||
1707 |
C<default_filter> and C<filter> of C<DBIx::Custom::Result> |
|
1708 | ||
1709 |
=back |
|
1710 | ||
renamed DBIx::Custom::TagPro...
|
1711 |
=head2 C<register_tag> |
added register_tag_processor
|
1712 | |
renamed DBIx::Custom::TagPro...
|
1713 |
$dbi->register_tag( |
added register_tag_processor
|
1714 |
limit => sub { |
1715 |
...; |
|
1716 |
} |
|
1717 |
); |
|
1718 | ||
cleanup
|
1719 |
Register tag. |
added register_tag_processor
|
1720 | |
added auto_filter method
|
1721 |
=head2 C<rollback> |
cleanup
|
1722 | |
1723 |
$dbi->rollback; |
|
1724 | ||
1725 |
Rollback transaction. |
|
1726 |
This is same as L<DBI>'s C<rollback>. |
|
1727 | ||
removed DBIx::Custom commit ...
|
1728 |
=head2 C<select> |
packaging one directory
|
1729 |
|
select method column option ...
|
1730 |
my $result = $dbi->select( |
1731 |
table => $table, |
|
1732 |
column => [@column], |
|
1733 |
where => \%where, |
|
1734 |
append => $append, |
|
1735 |
relation => \%relation, |
|
1736 |
filter => \%filter, |
|
1737 |
query => 1, |
|
1738 |
selection => $selection |
|
1739 |
); |
|
update document
|
1740 | |
renamed build_query to creat...
|
1741 |
Execute select statement. |
cleanup
|
1742 |
C<select> method have C<table>, C<column>, C<where>, C<append>, |
renamed build_query to creat...
|
1743 |
C<relation> and C<filter> arguments. |
1744 |
C<table> is a table name. |
|
select method column option ...
|
1745 |
C<column> is column names. this is array reference or string. |
cleanup
|
1746 |
C<where> is where clause. this is normally hash reference. |
renamed build_query to creat...
|
1747 |
C<append> is a string added at the end of the SQL statement. |
1748 |
C<filter> is filters when parameter binding is executed. |
|
added experimental sugar met...
|
1749 |
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value. |
1750 |
default to 0. This is experimental. |
|
add experimental selection o...
|
1751 |
C<selection> is string of column name and tables. This is experimental |
1752 | ||
1753 |
selection => 'name, location.name as location_name ' . |
|
1754 |
'from company inner join location' |
|
update document
|
1755 | |
cleanup
|
1756 |
First element is a string. it contains tags, |
1757 |
such as "{= title} or {like author}". |
|
1758 |
Second element is paramters. |
|
1759 | ||
added experimental DBIx::Cus...
|
1760 |
=head3 C<(experimental) select_at()> |
add experimental update_at()...
|
1761 | |
1762 |
To select row by using primary key, use C<select_at()>. |
|
1763 | ||
1764 |
$dbi->select_at(table => 'book', primary_key => ['id'], where => ['123']); |
|
1765 | ||
1766 |
In this example, row which id colunm is 123 is selected. |
|
1767 |
NOTE that you must pass array reference as C<where>. |
|
1768 | ||
cleanup
|
1769 |
=head2 C<update> |
removed reconnect method
|
1770 | |
cleanup
|
1771 |
$dbi->update(table => $table, |
1772 |
param => \%params, |
|
1773 |
where => \%where, |
|
1774 |
append => $append, |
|
all filter can receive array...
|
1775 |
filter => \@filter, |
added experimental sugar met...
|
1776 |
query => 1) |
removed reconnect method
|
1777 | |
cleanup
|
1778 |
Execute update statement. |
1779 |
C<update> method have C<table>, C<param>, C<where>, C<append> |
|
1780 |
and C<filter> arguments. |
|
1781 |
C<table> is a table name. |
|
1782 |
C<param> is column-value pairs. this must be hash reference. |
|
1783 |
C<where> is where clause. this must be hash reference. |
|
1784 |
C<append> is a string added at the end of the SQL statement. |
|
1785 |
C<filter> is filters when parameter binding is executed. |
|
added experimental sugar met...
|
1786 |
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value. |
1787 |
default to 0. This is experimental. |
|
cleanup
|
1788 |
This is overwrites C<default_bind_filter>. |
1789 |
Return value of C<update()> is the count of affected rows. |
|
removed reconnect method
|
1790 | |
added experimental update_pa...
|
1791 |
=head2 C<(experimental) update_param> |
1792 | ||
1793 |
my $update_param = $dbi->update_param({title => 'a', age => 2}); |
|
1794 | ||
1795 |
Create update parameter tag. |
|
1796 | ||
1797 |
{title => 'a', age => 2} -> {update_param title age} |
|
1798 | ||
add feture. all model class ...
|
1799 |
=head2 C<(experimental) model> |
remove DBIx::Custom::Model
|
1800 | |
add feture. all model class ...
|
1801 |
$dbi->model('book')->method( |
remove DBIx::Custom::Model
|
1802 |
insert => sub { ... }, |
1803 |
update => sub { ... } |
|
1804 |
); |
|
1805 |
|
|
add feture. all model class ...
|
1806 |
my $model = $dbi->model('book'); |
remove DBIx::Custom::Model
|
1807 | |
add feture. all model class ...
|
1808 |
Set and get a L<DBIx::Custom::Model> object, |
remove DBIx::Custom::Model
|
1809 | |
add experimental setup_model...
|
1810 |
=head2 C<(experimental) setup_model> |
1811 | ||
1812 |
$dbi->setup_model; |
|
1813 | ||
1814 |
Setup all model objects. |
|
1815 |
C<columns> and C<primary_key> is automatically set. |
|
1816 | ||
cleanup
|
1817 |
=head2 C<update_all> |
renamed build_query to creat...
|
1818 | |
cleanup
|
1819 |
$dbi->update_all(table => $table, |
1820 |
param => \%params, |
|
all filter can receive array...
|
1821 |
filter => \@filter, |
cleanup
|
1822 |
append => $append); |
renamed build_query to creat...
|
1823 | |
cleanup
|
1824 |
Execute update statement to update all rows. |
1825 |
Arguments is same as C<update> method, |
|
1826 |
except that C<update_all> don't have C<where> argument. |
|
1827 |
Return value of C<update_all()> is the count of affected rows. |
|
removed DBIx::Custom commit ...
|
1828 | |
added experimental DBIx::Cus...
|
1829 |
=head3 C<(experimental) update_at()> |
add experimental update_at()...
|
1830 | |
1831 |
To update row by using primary key, use C<update_at()> |
|
1832 | ||
1833 |
$dbi->update_at( |
|
1834 |
table => 'book', |
|
1835 |
primary_key => ['id'], |
|
1836 |
where => ['123'], |
|
1837 |
param => {name => 'Ken'} |
|
1838 |
); |
|
1839 | ||
1840 |
In this example, row which id column is 123 is updated. |
|
1841 |
NOTE that you must pass array reference as C<where>. |
|
1842 |
If C<param> contains primary key, |
|
1843 |
the key and value is delete from C<param>. |
|
1844 | ||
fix tests
|
1845 |
=head2 C<(experimental) where> |
1846 | ||
1847 |
my $where = $dbi->where; |
|
1848 | ||
1849 |
Create a new L<DBIx::Custom::Where> object. |
|
1850 | ||
cleanup
|
1851 |
=head2 C<cache_method> |
cleanup
|
1852 | |
1853 |
$dbi = $dbi->cache_method(\&cache_method); |
|
1854 |
$cache_method = $dbi->cache_method |
|
1855 | ||
1856 |
Method to set and get caches. |
|
1857 | ||
cleanup
|
1858 |
=head1 Tags |
1859 | ||
1860 |
The following tags is available. |
|
1861 | ||
add experimental selection o...
|
1862 |
=head2 C<(experimental) table> |
add table tag
|
1863 | |
1864 |
Table tag |
|
1865 | ||
1866 |
{table TABLE} -> TABLE |
|
1867 | ||
1868 |
This is used to teach what is applied table to C<execute()>. |
|
1869 | ||
cleanup
|
1870 |
=head2 C<?> |
1871 | ||
1872 |
Placeholder tag. |
|
1873 | ||
1874 |
{? NAME} -> ? |
|
1875 | ||
1876 |
=head2 C<=> |
|
1877 | ||
1878 |
Equal tag. |
|
1879 | ||
1880 |
{= NAME} -> NAME = ? |
|
1881 | ||
1882 |
=head2 C<E<lt>E<gt>> |
|
1883 | ||
1884 |
Not equal tag. |
|
1885 | ||
1886 |
{<> NAME} -> NAME <> ? |
|
1887 | ||
1888 |
=head2 C<E<lt>> |
|
1889 | ||
1890 |
Lower than tag |
|
1891 | ||
1892 |
{< NAME} -> NAME < ? |
|
1893 | ||
1894 |
=head2 C<E<gt>> |
|
1895 | ||
1896 |
Greater than tag |
|
1897 | ||
1898 |
{> NAME} -> NAME > ? |
|
1899 | ||
1900 |
=head2 C<E<gt>=> |
|
1901 | ||
1902 |
Greater than or equal tag |
|
1903 | ||
1904 |
{>= NAME} -> NAME >= ? |
|
1905 | ||
1906 |
=head2 C<E<lt>=> |
|
1907 | ||
1908 |
Lower than or equal tag |
|
1909 | ||
1910 |
{<= NAME} -> NAME <= ? |
|
1911 | ||
1912 |
=head2 C<like> |
|
1913 | ||
1914 |
Like tag |
|
1915 | ||
1916 |
{like NAME} -> NAME like ? |
|
1917 | ||
1918 |
=head2 C<in> |
|
1919 | ||
1920 |
In tag. |
|
1921 | ||
1922 |
{in NAME COUNT} -> NAME in [?, ?, ..] |
|
1923 | ||
1924 |
=head2 C<insert_param> |
|
1925 | ||
1926 |
Insert parameter tag. |
|
1927 | ||
1928 |
{insert_param NAME1 NAME2} -> (NAME1, NAME2) values (?, ?) |
|
1929 | ||
1930 |
=head2 C<update_param> |
|
1931 | ||
1932 |
Updata parameter tag. |
|
1933 | ||
1934 |
{update_param NAME1 NAME2} -> set NAME1 = ?, NAME2 = ? |
|
1935 | ||
DBIx::Custom is now stable
|
1936 |
=head1 STABILITY |
1937 | ||
cleanup
|
1938 |
L<DBIx::Custom> is stable. APIs keep backword compatible |
1939 |
except experimental one in the feature. |
|
DBIx::Custom is now stable
|
1940 | |
removed DESTROY method(not b...
|
1941 |
=head1 BUGS |
1942 | ||
renamed build_query to creat...
|
1943 |
Please tell me bugs if found. |
removed DESTROY method(not b...
|
1944 | |
1945 |
C<< <kimoto.yuki at gmail.com> >> |
|
1946 | ||
1947 |
L<http://github.com/yuki-kimoto/DBIx-Custom> |
|
1948 | ||
removed reconnect method
|
1949 |
=head1 AUTHOR |
1950 | ||
1951 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
|
version 0.0901
|
1952 | |
packaging one directory
|
1953 |
=head1 COPYRIGHT & LICENSE |
1954 | ||
cleanup
|
1955 |
Copyright 2009-2011 Yuki Kimoto, all rights reserved. |
packaging one directory
|
1956 | |
1957 |
This program is free software; you can redistribute it and/or modify it |
|
1958 |
under the same terms as Perl itself. |
|
1959 | ||
1960 |
=cut |
|
added cache_method attribute
|
1961 | |
1962 |