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