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