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