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