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