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