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