cleanup
|
1 |
package DBIx::Custom; |
2 | ||
cleanup
|
3 |
our $VERSION = '0.1667'; |
fixed DBIx::Custom::QueryBui...
|
4 | |
5 |
use 5.008001; |
|
cleanup
|
6 |
use strict; |
7 |
use warnings; |
|
8 | ||
remove run_transaction().
|
9 |
use base 'Object::Simple'; |
many change
|
10 | |
packaging one directory
|
11 |
use Carp 'croak'; |
12 |
use DBI; |
|
13 |
use DBIx::Custom::Result; |
|
cleanup
|
14 |
use DBIx::Custom::Query; |
cleanup
|
15 |
use DBIx::Custom::QueryBuilder; |
added experimental DBIx::Cus...
|
16 |
use DBIx::Custom::Where; |
add feture. all model class ...
|
17 |
use DBIx::Custom::Model; |
cleanup
|
18 |
use DBIx::Custom::Tag; |
all filter can receive array...
|
19 |
use DBIx::Custom::Util; |
update document
|
20 |
use Encode qw/encode_utf8 decode_utf8/; |
packaging one directory
|
21 | |
- added EXPERIMENTAL type() ...
|
22 |
our @COMMON_ARGS = qw/table query filter type/; |
cleanup
|
23 | |
fix tests
|
24 |
__PACKAGE__->attr( |
EXPERIMETAL fork safety impl...
|
25 |
[qw/data_source password pid user/], |
removed from cache() and cac...
|
26 |
cache => 0, |
many changed
|
27 |
cache_method => sub { |
28 |
sub { |
|
29 |
my $self = shift; |
|
30 |
|
|
31 |
$self->{_cached} ||= {}; |
|
32 |
|
|
33 |
if (@_ > 1) { |
|
update pod
|
34 |
$self->{_cached}{$_[0]} = $_[1]; |
many changed
|
35 |
} |
36 |
else { |
|
update pod
|
37 |
return $self->{_cached}{$_[0]}; |
many changed
|
38 |
} |
39 |
} |
|
update pod
|
40 |
}, |
41 |
dbi_option => sub { {} }, |
|
42 |
default_dbi_option => sub { |
|
43 |
{ |
|
44 |
RaiseError => 1, |
|
45 |
PrintError => 0, |
|
46 |
AutoCommit => 1 |
|
47 |
} |
|
48 |
}, |
|
fix tests
|
49 |
filters => sub { |
50 |
{ |
|
51 |
encode_utf8 => sub { encode_utf8($_[0]) }, |
|
52 |
decode_utf8 => sub { decode_utf8($_[0]) } |
|
53 |
} |
|
update pod
|
54 |
}, |
55 |
models => sub { {} }, |
|
56 |
query_builder => sub { DBIx::Custom::QueryBuilder->new }, |
|
57 |
result_class => 'DBIx::Custom::Result', |
|
added EXPERIMENTAL reserved_...
|
58 |
reserved_word_quote => '', |
update pod
|
59 |
safety_character => '\w', |
60 |
stash => sub { {} } |
|
fix tests
|
61 |
); |
cleanup
|
62 | |
added helper method
|
63 |
our $AUTOLOAD; |
64 |
sub AUTOLOAD { |
|
65 |
my $self = shift; |
|
66 | ||
renamed helper to method.
|
67 |
# Method name |
68 |
my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/; |
|
added helper method
|
69 | |
renamed helper to method.
|
70 |
# Method |
71 |
$self->{_methods} ||= {}; |
|
add feture. all model class ...
|
72 |
if (my $method = $self->{_methods}->{$mname}) { |
73 |
return $self->$method(@_) |
|
74 |
} |
|
- added EXPERIMENTAL DBIx::C...
|
75 |
elsif (my $dbh_method = $self->dbh->can($mname)) { |
76 |
$self->dbh->$dbh_method(@_); |
|
add feture. all model class ...
|
77 |
} |
78 |
else { |
|
79 |
croak qq/Can't locate object method "$mname" via "$package"/ |
|
80 |
} |
|
added helper method
|
81 |
} |
82 | ||
renamed auto_filter to apply...
|
83 |
sub apply_filter { |
many changed
|
84 |
my ($self, $table, @cinfos) = @_; |
85 | ||
86 |
# Initialize filters |
|
cleanup
|
87 |
$self->{filter} ||= {}; |
many changed
|
88 |
$self->{filter}{out} ||= {}; |
89 |
$self->{filter}{in} ||= {}; |
|
all filter can receive array...
|
90 |
$self->{filter}{end} ||= {}; |
cleanup
|
91 |
|
many changed
|
92 |
# Create filters |
93 |
my $usage = "Usage: \$dbi->apply_filter(" . |
|
fix bug : filter can't over...
|
94 |
"TABLE, COLUMN1, {in => INFILTER1, out => OUTFILTER1, end => ENDFILTER1}, " . |
95 |
"COLUMN2, {in => INFILTER2, out => OUTFILTER2, end => ENDFILTER2}, ...)"; |
|
many changed
|
96 | |
97 |
for (my $i = 0; $i < @cinfos; $i += 2) { |
|
added auto_filter method
|
98 |
|
many changed
|
99 |
# Column |
100 |
my $column = $cinfos[$i]; |
|
added auto_filter method
|
101 |
|
all filter can receive array...
|
102 |
if (ref $column eq 'ARRAY') { |
103 |
foreach my $c (@$column) { |
|
104 |
push @cinfos, $c, $cinfos[$i + 1]; |
|
105 |
} |
|
106 |
next; |
|
107 |
} |
|
108 |
|
|
fix bug : filter can't over...
|
109 |
# Filter info |
110 |
my $finfo = $cinfos[$i + 1] || {}; |
|
all filter can receive array...
|
111 |
croak "$usage (table: $table)" unless ref $finfo eq 'HASH'; |
fix bug : filter can't over...
|
112 |
foreach my $ftype (keys %$finfo) { |
all filter can receive array...
|
113 |
croak "$usage (table: $table 2)" unless $ftype eq 'in' || $ftype eq 'out' |
fix bug : filter can't over...
|
114 |
|| $ftype eq 'end'; |
many changed
|
115 |
} |
116 |
|
|
fix bug : filter can't over...
|
117 |
foreach my $way (qw/in out end/) { |
118 |
my $filter = $finfo->{$way}; |
|
cleanup
|
119 |
|
fix bug : filter can't over...
|
120 |
# State |
121 |
my $state = !exists $finfo->{$way} ? 'not_exists' |
|
122 |
: !defined $filter ? 'not_defined' |
|
123 |
: ref $filter eq 'CODE' ? 'code' |
|
124 |
: 'name'; |
|
125 |
|
|
126 |
next if $state eq 'not_exists'; |
|
127 |
|
|
128 |
# Check filter |
|
129 |
croak qq{Filter "$filter" is not registered} |
|
130 |
if $state eq 'name' |
|
131 |
&& ! exists $self->filters->{$filter}; |
|
132 |
|
|
133 |
# Filter |
|
134 |
my $f = $state eq 'not_defined' ? undef |
|
135 |
: $state eq 'code' ? $filter |
|
136 |
: $self->filters->{$filter}; |
|
137 |
$self->{filter}{$way}{$table}{$column} = $f; |
|
138 |
$self->{filter}{$way}{$table}{"$table.$column"} = $f; |
|
139 |
$self->{filter}{$way}{$table}{"${table}__$column"} = $f; |
|
many changed
|
140 |
} |
added auto_filter method
|
141 |
} |
142 |
|
|
many changed
|
143 |
return $self; |
added auto_filter method
|
144 |
} |
145 | ||
cleanup
|
146 |
sub column { |
147 |
my ($self, $table, $columns) = @_; |
|
added helper method
|
148 |
|
cleanup
|
149 |
$columns ||= []; |
added helper method
|
150 |
|
added EXPERIMENTAL reserved_...
|
151 |
my $q = $self->reserved_word_quote; |
152 |
|
|
cleanup
|
153 |
my @column; |
added EXPERIMENTAL reserved_...
|
154 |
push @column, "$q$table$q.$q$_$q as $q${table}${q}__$q$_$q" for @$columns; |
cleanup
|
155 |
|
156 |
return join (', ', @column); |
|
added helper method
|
157 |
} |
158 | ||
packaging one directory
|
159 |
sub connect { |
cleanup
|
160 |
my $self = ref $_[0] ? shift : shift->new(@_);; |
removed register_format()
|
161 |
|
EXPERIMETAL fork safety impl...
|
162 |
my $dbh = $self->_connect; |
packaging one directory
|
163 |
|
update document
|
164 |
# Database handle |
packaging one directory
|
165 |
$self->dbh($dbh); |
update document
|
166 |
|
EXPERIMETAL fork safety impl...
|
167 |
# Process ID |
168 |
$self->pid($$); |
|
169 |
|
|
packaging one directory
|
170 |
return $self; |
171 |
} |
|
172 | ||
cleanup
|
173 |
sub create_query { |
174 |
my ($self, $source) = @_; |
|
update document
|
175 |
|
cleanup
|
176 |
# Cache |
177 |
my $cache = $self->cache; |
|
update document
|
178 |
|
cleanup
|
179 |
# Create query |
180 |
my $query; |
|
181 |
if ($cache) { |
|
182 |
|
|
183 |
# Get query |
|
184 |
my $q = $self->cache_method->($self, $source); |
|
185 |
|
|
186 |
# Create query |
|
add table tag
|
187 |
if ($q) { |
188 |
$query = DBIx::Custom::Query->new($q); |
|
189 |
$query->filters($self->filters); |
|
190 |
} |
|
cleanup
|
191 |
} |
192 |
|
|
193 |
unless ($query) { |
|
cleanup insert
|
194 | |
cleanup
|
195 |
# Create SQL object |
196 |
my $builder = $self->query_builder; |
|
197 |
|
|
198 |
# Create query |
|
199 |
$query = $builder->build_query($source); |
|
removed register_format()
|
200 | |
added EXPERIMENTAL reserved_...
|
201 |
# Bind |
202 |
my $columns = $query->columns; |
|
203 |
if (my $q = $self->reserved_word_quote) { |
|
204 |
foreach my $column (@$columns) { |
|
205 |
$column =~ s/$q//g; |
|
206 |
} |
|
207 |
} |
|
208 | ||
cleanup
|
209 |
# Cache query |
210 |
$self->cache_method->($self, $source, |
|
211 |
{sql => $query->sql, |
|
add table tag
|
212 |
columns => $query->columns, |
213 |
tables => $query->tables}) |
|
cleanup
|
214 |
if $cache; |
cleanup insert
|
215 |
} |
216 |
|
|
cleanup
|
217 |
# Prepare statement handle |
218 |
my $sth; |
|
219 |
eval { $sth = $self->dbh->prepare($query->{sql})}; |
|
renamed DBIx::Custom::TagPro...
|
220 |
$self->_croak($@, qq{. Following SQL is executed. "$query->{sql}"}) if $@; |
packaging one directory
|
221 |
|
cleanup
|
222 |
# Set statement handle |
223 |
$query->sth($sth); |
|
packaging one directory
|
224 |
|
cleanup
|
225 |
# Set filters |
226 |
$query->filters($self->filters); |
|
227 |
|
|
cleanup
|
228 |
return $query; |
packaging one directory
|
229 |
} |
230 | ||
update pod
|
231 |
sub dbh { |
232 |
my $self = shift; |
|
233 | ||
234 |
if (@_) { |
|
235 |
$self->{dbh} = $_[0]; |
|
236 |
return $self; |
|
237 |
} |
|
238 |
else { |
|
239 |
my $pid = $$; |
|
240 |
if ($self->pid eq $pid) { |
|
241 |
return $self->{dbh}; |
|
242 |
} |
|
243 |
else { |
|
244 |
# Create new connection in child process |
|
245 |
croak "Process is forked in transaction" |
|
246 |
unless $self->{dbh}->{AutoCommit}; |
|
247 |
$self->pid($pid); |
|
248 |
$self->{dbh}->{InactiveDestroy} = 1; |
|
249 |
return $self->{dbh} = $self->_connect; |
|
250 |
} |
|
251 |
} |
|
252 |
} |
|
253 | ||
cleanup
|
254 |
our %DELETE_ARGS |
cleanup
|
255 |
= map { $_ => 1 } @COMMON_ARGS, qw/where append allow_delete_all/; |
cleanup update and update_al...
|
256 | |
cleanup
|
257 |
sub delete { |
select, insert, update, upda...
|
258 |
my ($self, %args) = @_; |
added EXPERIMENTAL reserved_...
|
259 | |
260 |
# Quote for reserved word |
|
261 |
my $q = $self->reserved_word_quote; |
|
cleanup update and update_al...
|
262 |
|
cleanup
|
263 |
# Check argument names |
select, insert, update, upda...
|
264 |
foreach my $name (keys %args) { |
cleanup
|
265 |
croak qq{Argument "$name" is invalid name} |
cleanup
|
266 |
unless $DELETE_ARGS{$name}; |
cleanup update and update_al...
|
267 |
} |
268 |
|
|
269 |
# Arguments |
|
cleanup
|
270 |
my $table = $args{table} || ''; |
added table not specified ex...
|
271 |
croak qq{"table" option must be specified} unless $table; |
cleanup
|
272 |
my $where = delete $args{where} || {}; |
273 |
my $append = delete $args{append}; |
|
274 |
my $allow_delete_all = delete $args{allow_delete_all}; |
|
added auto_filter method
|
275 | |
make delete() using where ob...
|
276 |
# Where |
where can recieve array refr...
|
277 |
my $w = $self->_where($where); |
278 |
$where = $w->param; |
|
make delete() using where ob...
|
279 |
|
select() where can't receive...
|
280 |
# String where |
281 |
my $swhere = "$w"; |
|
improved delete() and update...
|
282 |
|
make delete() using where ob...
|
283 |
croak qq{"where" must be specified} |
select() where can't receive...
|
284 |
if $swhere eq '' && !$allow_delete_all; |
make delete() using where ob...
|
285 | |
cleanup
|
286 |
# SQL stack |
287 |
my @sql; |
|
288 | ||
289 |
# Delete |
|
added EXPERIMENTAL reserved_...
|
290 |
push @sql, "delete from $q$table$q $swhere"; |
cleanup
|
291 |
push @sql, $append if $append; |
292 |
|
|
293 |
my $sql = join(' ', @sql); |
|
packaging one directory
|
294 |
|
added experimental sugar met...
|
295 |
# Create query |
cleanup
|
296 |
my $query = $self->create_query($sql); |
added experimental sugar met...
|
297 |
return $query if $args{query}; |
298 |
|
|
packaging one directory
|
299 |
# Execute query |
added auto_filter method
|
300 |
my $ret_val = $self->execute( |
cleanup
|
301 |
$query, |
302 |
param => $where, |
|
303 |
table => $table, |
|
304 |
%args |
|
305 |
); |
|
packaging one directory
|
306 |
|
307 |
return $ret_val; |
|
308 |
} |
|
309 | ||
cleanup
|
310 |
sub delete_all { shift->delete(allow_delete_all => 1, @_) } |
packaging one directory
|
311 | |
cleanup
|
312 |
our %DELETE_AT_ARGS = (%DELETE_ARGS, where => 1, primary_key => 1); |
add experimental update_at()...
|
313 | |
314 |
sub delete_at { |
|
315 |
my ($self, %args) = @_; |
|
316 |
|
|
cleanup
|
317 |
# Check argument names |
add experimental update_at()...
|
318 |
foreach my $name (keys %args) { |
cleanup
|
319 |
croak qq{Argument "$name" is invalid name} |
cleanup
|
320 |
unless $DELETE_AT_ARGS{$name}; |
add experimental update_at()...
|
321 |
} |
322 |
|
|
323 |
# Primary key |
|
324 |
my $primary_keys = delete $args{primary_key}; |
|
325 |
$primary_keys = [$primary_keys] unless ref $primary_keys; |
|
326 |
|
|
327 |
# Where clause |
|
328 |
my $where = {}; |
|
329 |
if (exists $args{where}) { |
|
330 |
my $where_columns = delete $args{where}; |
|
331 |
$where_columns = [$where_columns] unless ref $where_columns; |
|
- added experimental DBIx::C...
|
332 | |
333 |
croak qq{"where" must be constant value or array reference} |
|
334 |
unless !ref $where_columns || ref $where_columns eq 'ARRAY'; |
|
add experimental update_at()...
|
335 |
|
336 |
for(my $i = 0; $i < @$primary_keys; $i ++) { |
|
337 |
$where->{$primary_keys->[$i]} = $where_columns->[$i]; |
|
338 |
} |
|
339 |
} |
|
- added experimental DBIx::C...
|
340 |
|
341 |
if (exists $args{param}) { |
|
add experimental update_at()...
|
342 |
my $param = delete $args{param}; |
343 |
|
|
344 |
for(my $i = 0; $i < @$primary_keys; $i ++) { |
|
- added experimental DBIx::C...
|
345 |
delete $param->{$primary_keys->[$i]}; |
add experimental update_at()...
|
346 |
} |
347 |
} |
|
348 |
|
|
349 |
return $self->delete(where => $where, %args); |
|
350 |
} |
|
351 | ||
added helper method
|
352 |
sub DESTROY { } |
353 | ||
cleanup
|
354 |
our %EXECUTE_ARGS = map { $_ => 1 } @COMMON_ARGS, 'param'; |
refactoring delete and delet...
|
355 | |
removed EXPERIMETNAL flag fr...
|
356 |
sub create_model { |
357 |
my $self = shift; |
|
358 |
|
|
359 |
my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
360 |
$args->{dbi} = $self; |
|
361 |
|
|
362 |
my $model_class = delete $args->{model_class} || 'DBIx::Custom::Model'; |
|
363 |
my $model_name = delete $args->{name}; |
|
364 |
my $model_table = delete $args->{table}; |
|
365 |
$model_name ||= $model_table; |
|
366 |
|
|
367 |
my $model = $model_class->new($args); |
|
368 |
$model->name($model_name) unless $model->name; |
|
369 |
$model->table($model_table) unless $model->table; |
|
370 |
|
|
371 |
# Apply filter |
|
372 |
croak "$model_class filter must be array reference" |
|
373 |
unless ref $model->filter eq 'ARRAY'; |
|
374 |
$self->apply_filter($model->table, @{$model->filter}); |
|
375 |
|
|
376 |
# Table - Model |
|
377 |
croak "Table name is duplicated" |
|
378 |
if exists $self->{_model_from}->{$model->table}; |
|
379 |
$self->{_model_from}->{$model->table} = $model->name; |
|
380 | ||
381 |
# Table alias |
|
382 |
$self->{_table_alias} ||= {}; |
|
383 |
$self->{_table_alias} = {%{$self->{_table_alias}}, %{$model->table_alias}}; |
|
384 |
|
|
385 |
# Set model |
|
386 |
$self->model($model->name, $model); |
|
387 |
|
|
create_model() return model
|
388 |
return $self->model($model->name); |
removed EXPERIMETNAL flag fr...
|
389 |
} |
390 | ||
391 |
sub each_column { |
|
392 |
my ($self, $cb) = @_; |
|
393 |
|
|
394 |
# Iterate all tables |
|
395 |
my $sth_tables = $self->dbh->table_info; |
|
396 |
while (my $table_info = $sth_tables->fetchrow_hashref) { |
|
397 |
|
|
398 |
# Table |
|
399 |
my $table = $table_info->{TABLE_NAME}; |
|
400 |
|
|
401 |
# Iterate all columns |
|
402 |
my $sth_columns = $self->dbh->column_info(undef, undef, $table, '%'); |
|
403 |
while (my $column_info = $sth_columns->fetchrow_hashref) { |
|
404 |
my $column = $column_info->{COLUMN_NAME}; |
|
405 |
$self->$cb($table, $column, $column_info); |
|
406 |
} |
|
407 |
} |
|
408 |
} |
|
409 | ||
cleanup
|
410 |
sub execute{ |
411 |
my ($self, $query, %args) = @_; |
|
refactoring delete and delet...
|
412 |
|
added EXPERIMENTAL reserved_...
|
413 |
# Quote for reserved word |
414 |
my $q = $self->reserved_word_quote; |
|
415 |
|
|
cleanup
|
416 |
# Check argument names |
select, insert, update, upda...
|
417 |
foreach my $name (keys %args) { |
cleanup
|
418 |
croak qq{Argument "$name" is invalid name} |
cleanup
|
419 |
unless $EXECUTE_ARGS{$name}; |
refactoring delete and delet...
|
420 |
} |
421 |
|
|
cleanup
|
422 |
my $params = $args{param} || {}; |
packaging one directory
|
423 |
|
cleanup
|
424 |
# First argument is the soruce of SQL |
425 |
$query = $self->create_query($query) |
|
426 |
unless ref $query; |
|
packaging one directory
|
427 |
|
add table tag
|
428 |
# Applied filter |
cleanup
|
429 |
my $filter = {}; |
all filter can receive array...
|
430 |
|
add table tag
|
431 |
my $tables = $query->tables; |
added EXPERIMENTAL reserved_...
|
432 |
if ($q) { |
433 |
foreach my $table (@$tables) { |
|
434 |
$table =~ s/$q//g; |
|
435 |
} |
|
436 |
} |
|
add table tag
|
437 |
my $arg_tables = $args{table} || []; |
438 |
$arg_tables = [$arg_tables] |
|
439 |
unless ref $arg_tables eq 'ARRAY'; |
|
440 |
push @$tables, @$arg_tables; |
|
cleanup
|
441 | |
442 |
# Organize tables |
|
443 |
my %table_set = map {defined $_ ? ($_ => 1) : ()} @$tables; |
|
444 |
my $main_table = pop @$tables; |
|
445 |
delete $table_set{$main_table} if $main_table; |
|
- added EXPERIMENTAL DBIx::C...
|
446 |
foreach my $table (keys %table_set) { |
447 |
push @$tables, $table; |
|
448 |
|
|
449 |
if (my $dist = $self->{_table_alias}->{$table}) { |
|
450 |
$self->{filter} ||= {}; |
|
451 |
|
|
452 |
unless ($self->{filter}{out}{$table}) { |
|
453 |
$self->{filter}{out} ||= {}; |
|
454 |
$self->{filter}{in} ||= {}; |
|
455 |
$self->{filter}{end} ||= {}; |
|
456 |
|
|
457 |
foreach my $type (qw/out in end/) { |
|
458 |
|
|
459 |
foreach my $filter_name (keys %{$self->{filter}{$type}{$dist} || {}}) { |
|
460 |
my $filter_name_alias = $filter_name; |
|
461 |
$filter_name_alias =~ s/^$dist\./$table\./; |
|
462 |
$filter_name_alias =~ s/^${dist}__/${table}__/; |
|
463 |
|
|
464 |
$self->{filter}{$type}{$table}{$filter_name_alias} |
|
465 |
= $self->{filter}{$type}{$dist}{$filter_name} |
|
466 |
} |
|
467 |
} |
|
468 |
} |
|
469 |
} |
|
470 |
} |
|
471 |
|
|
cleanup
|
472 |
$tables = [keys %table_set]; |
473 |
push @$tables, $main_table if $main_table; |
|
remove experimental DBIx::Cu...
|
474 |
|
renamed auto_filter to apply...
|
475 |
foreach my $table (@$tables) { |
remove experimental DBIx::Cu...
|
476 |
next unless $table; |
cleanup
|
477 |
$filter = { |
478 |
%$filter, |
|
479 |
%{$self->{filter}{out}->{$table} || {}} |
|
added auto_filter method
|
480 |
} |
481 |
} |
|
482 |
|
|
cleanup
|
483 |
# Filter argument |
cleanup
|
484 |
my $f = DBIx::Custom::Util::array_to_hash($args{filter}) |
all filter can receive array...
|
485 |
|| $query->filter || {}; |
cleanup
|
486 |
foreach my $column (keys %$f) { |
487 |
my $fname = $f->{$column}; |
|
renamed auto_filter to apply...
|
488 |
if (!defined $fname) { |
cleanup
|
489 |
$f->{$column} = undef; |
renamed auto_filter to apply...
|
490 |
} |
491 |
elsif (ref $fname ne 'CODE') { |
|
many changed
|
492 |
croak qq{Filter "$fname" is not registered"} |
cleanup
|
493 |
unless exists $self->filters->{$fname}; |
494 |
|
|
cleanup
|
495 |
$f->{$column} = $self->filters->{$fname}; |
cleanup
|
496 |
} |
497 |
} |
|
cleanup
|
498 |
$filter = {%$filter, %$f}; |
packaging one directory
|
499 |
|
- added EXPERIMENTAL type() ...
|
500 |
# Type |
501 |
my $type = DBIx::Custom::Util::array_to_hash($args{type}); |
|
502 |
|
|
added experimental not_exist...
|
503 |
# Bind |
- added EXPERIMENTAL type() ...
|
504 |
my $bind = $self->_bind($params, $query->columns, $filter, $type); |
cleanup
|
505 |
|
506 |
# Execute |
|
added experimental not_exist...
|
507 |
my $sth = $query->sth; |
cleanup
|
508 |
my $affected; |
cleanup
|
509 |
eval { |
510 |
for (my $i = 0; $i < @$bind; $i++) { |
|
- added EXPERIMENTAL type() ...
|
511 |
if (my $type = $bind->[$i]->{type}) { |
512 |
$sth->bind_param($i + 1, $bind->[$i]->{value}, $type); |
|
513 |
} |
|
514 |
else { |
|
515 |
$sth->bind_param($i + 1, $bind->[$i]->{value}); |
|
516 |
} |
|
cleanup
|
517 |
} |
518 |
$affected = $sth->execute; |
|
519 |
}; |
|
renamed DBIx::Custom::TagPro...
|
520 |
$self->_croak($@, qq{. Following SQL is executed. "$query->{sql}"}) if $@; |
cleanup
|
521 |
|
522 |
# Return resultset if select statement is executed |
|
523 |
if ($sth->{NUM_OF_FIELDS}) { |
|
524 |
|
|
fix bug : filter can't over...
|
525 |
# Result in and end filter |
526 |
my $in_filter = {}; |
|
527 |
my $end_filter = {}; |
|
cleanup
|
528 |
foreach my $table (@$tables) { |
remove experimental DBIx::Cu...
|
529 |
next unless $table; |
cleanup
|
530 |
$in_filter = { |
531 |
%$in_filter, |
|
532 |
%{$self->{filter}{in}{$table} || {}} |
|
fix bug : filter can't over...
|
533 |
}; |
534 |
$end_filter = { |
|
535 |
%$end_filter, |
|
536 |
%{$self->{filter}{end}{$table} || {}} |
|
537 |
}; |
|
cleanup
|
538 |
} |
539 |
|
|
540 |
# Result |
|
541 |
my $result = $self->result_class->new( |
|
cleanup
|
542 |
sth => $sth, |
543 |
filters => $self->filters, |
|
544 |
filter_check => $self->filter_check, |
|
cleanup
|
545 |
default_filter => $self->{default_in_filter}, |
fix bug : filter can't over...
|
546 |
filter => $in_filter || {}, |
547 |
end_filter => $end_filter || {} |
|
cleanup
|
548 |
); |
549 | ||
550 |
return $result; |
|
551 |
} |
|
552 |
return $affected; |
|
553 |
} |
|
554 | ||
cleanup
|
555 |
our %INSERT_ARGS = map { $_ => 1 } @COMMON_ARGS, qw/param append/; |
update pod
|
556 | |
cleanup
|
557 |
sub insert { |
558 |
my ($self, %args) = @_; |
|
added EXPERIMENTAL reserved_...
|
559 |
|
560 |
# Quote for reserved word |
|
561 |
my $q = $self->reserved_word_quote; |
|
cleanup
|
562 | |
cleanup
|
563 |
# Check argument names |
cleanup
|
564 |
foreach my $name (keys %args) { |
cleanup
|
565 |
croak qq{Argument "$name" is invalid name} |
cleanup
|
566 |
unless $INSERT_ARGS{$name}; |
packaging one directory
|
567 |
} |
568 |
|
|
cleanup
|
569 |
# Arguments |
cleanup
|
570 |
my $table = delete $args{table}; |
added table not specified ex...
|
571 |
croak qq{"table" option must be specified} unless $table; |
cleanup
|
572 |
my $param = delete $args{param} || {}; |
573 |
my $append = delete $args{append} || ''; |
|
cleanup
|
574 |
|
select() where can't receive...
|
575 |
# Columns |
576 |
my @columns; |
|
- remaned experimental safty...
|
577 |
my $safety = $self->safety_character; |
select() where can't receive...
|
578 |
foreach my $column (keys %$param) { |
579 |
croak qq{"$column" is not safety column name} |
|
- remaned experimental safty...
|
580 |
unless $column =~ /^[$safety\.]+$/; |
added EXPERIMENTAL reserved_...
|
581 |
$column = "$q$column$q"; |
582 |
$column =~ s/\./$q.$q/; |
|
select() where can't receive...
|
583 |
push @columns, $column; |
584 |
} |
|
cleanup
|
585 |
|
cleanup
|
586 |
# SQL stack |
587 |
my @sql; |
|
588 |
|
|
589 |
# Insert |
|
added EXPERIMENTAL reserved_...
|
590 |
push @sql, "insert into $q$table$q {insert_param ". join(' ', @columns) . '}'; |
cleanup
|
591 |
push @sql, $append if $append; |
592 |
|
|
select() where can't receive...
|
593 |
# SQL |
cleanup
|
594 |
my $sql = join (' ', @sql); |
packaging one directory
|
595 |
|
added experimental sugar met...
|
596 |
# Create query |
cleanup
|
597 |
my $query = $self->create_query($sql); |
added experimental sugar met...
|
598 |
return $query if $args{query}; |
599 |
|
|
packaging one directory
|
600 |
# Execute query |
added auto_filter method
|
601 |
my $ret_val = $self->execute( |
added experimental sugar met...
|
602 |
$query, |
added auto_filter method
|
603 |
param => $param, |
cleanup
|
604 |
table => $table, |
605 |
%args |
|
added auto_filter method
|
606 |
); |
packaging one directory
|
607 |
|
608 |
return $ret_val; |
|
609 |
} |
|
610 | ||
cleanup
|
611 |
our %INSERT_AT_ARGS = (%INSERT_ARGS, where => 1, primary_key => 1); |
- added experimental DBIx::C...
|
612 | |
613 |
sub insert_at { |
|
614 |
my ($self, %args) = @_; |
|
615 |
|
|
cleanup
|
616 |
# Check argument names |
- added experimental DBIx::C...
|
617 |
foreach my $name (keys %args) { |
cleanup
|
618 |
croak qq{Argument "$name" is invalid name} |
cleanup
|
619 |
unless $INSERT_AT_ARGS{$name}; |
- added experimental DBIx::C...
|
620 |
} |
621 |
|
|
622 |
# Primary key |
|
623 |
my $primary_keys = delete $args{primary_key}; |
|
624 |
$primary_keys = [$primary_keys] unless ref $primary_keys; |
|
625 |
|
|
626 |
# Where clause |
|
627 |
my $where = {}; |
|
628 |
my $param = {}; |
|
629 |
|
|
630 |
if (exists $args{where}) { |
|
631 |
my $where_columns = delete $args{where}; |
|
632 |
$where_columns = [$where_columns] unless ref $where_columns; |
|
633 | ||
634 |
croak qq{"where" must be constant value or array reference} |
|
635 |
unless !ref $where_columns || ref $where_columns eq 'ARRAY'; |
|
636 |
|
|
637 |
for(my $i = 0; $i < @$primary_keys; $i ++) { |
|
638 |
$where->{$primary_keys->[$i]} = $where_columns->[$i]; |
|
639 |
} |
|
640 |
} |
|
641 |
|
|
642 |
if (exists $args{param}) { |
|
643 |
$param = delete $args{param}; |
|
644 |
for(my $i = 0; $i < @$primary_keys; $i ++) { |
|
645 |
delete $param->{$primary_keys->[$i]}; |
|
646 |
} |
|
647 |
} |
|
648 |
|
|
649 |
$param = {%$param, %$where}; |
|
650 |
|
|
651 |
return $self->insert(param => $param, %args); |
|
652 |
} |
|
653 | ||
remove experimental DBIx::Cu...
|
654 |
sub insert_param { |
655 |
my ($self, $param) = @_; |
|
656 |
|
|
update pod
|
657 |
# Insert parameter tag |
remove experimental DBIx::Cu...
|
658 |
my @tag; |
659 |
push @tag, '{insert_param'; |
|
- remaned experimental safty...
|
660 |
my $safety = $self->safety_character; |
remove experimental DBIx::Cu...
|
661 |
foreach my $column (keys %$param) { |
added experimental update_pa...
|
662 |
croak qq{"$column" is not safety column name} |
- remaned experimental safty...
|
663 |
unless $column =~ /^[$safety\.]+$/; |
remove experimental DBIx::Cu...
|
664 |
push @tag, $column; |
665 |
} |
|
666 |
push @tag, '}'; |
|
667 |
|
|
668 |
return join ' ', @tag; |
|
669 |
} |
|
670 | ||
- added EXPERIMENTAL DBIx::C...
|
671 |
sub include_model { |
672 |
my ($self, $name_space, $model_infos) = @_; |
|
673 |
|
|
674 |
$name_space ||= ''; |
|
675 |
unless ($model_infos) { |
|
676 |
# Load name space module |
|
677 |
croak qq{"$name_space" is invalid class name} |
|
678 |
if $name_space =~ /[^\w:]/; |
|
679 |
eval "use $name_space"; |
|
680 |
croak qq{Name space module "$name_space.pm" is needed. $@} if $@; |
|
681 |
|
|
682 |
# Search model modules |
|
683 |
my $path = $INC{"$name_space.pm"}; |
|
684 |
$path =~ s/\.pm$//; |
|
685 |
opendir my $dh, $path |
|
686 |
or croak qq{Can't open directory "$path": $!}; |
|
687 |
$model_infos = []; |
|
688 |
while (my $module = readdir $dh) { |
|
689 |
push @$model_infos, $module |
|
690 |
if $module =~ s/\.pm$//; |
|
691 |
} |
|
692 |
|
|
693 |
close $dh; |
|
694 |
} |
|
695 |
|
|
696 |
foreach my $model_info (@$model_infos) { |
|
697 |
|
|
698 |
# Model class, name, table |
|
699 |
my $model_class; |
|
700 |
my $model_name; |
|
701 |
my $model_table; |
|
702 |
if (ref $model_info eq 'HASH') { |
|
703 |
$model_class = $model_info->{class}; |
|
704 |
$model_name = $model_info->{name}; |
|
705 |
$model_table = $model_info->{table}; |
|
706 |
|
|
707 |
$model_name ||= $model_class; |
|
708 |
$model_table ||= $model_name; |
|
709 |
} |
|
removed EXPERIMETNAL flag fr...
|
710 |
else { $model_class = $model_name = $model_table = $model_info } |
- added EXPERIMENTAL DBIx::C...
|
711 |
my $mclass = "${name_space}::$model_class"; |
712 |
|
|
713 |
# Load |
|
714 |
croak qq{"$mclass" is invalid class name} |
|
715 |
if $mclass =~ /[^\w:]/; |
|
716 |
unless ($mclass->can('isa')) { |
|
717 |
eval "use $mclass"; |
|
718 |
croak $@ if $@; |
|
719 |
} |
|
720 |
|
|
721 |
# Instantiate |
|
removed EXPERIMETNAL flag fr...
|
722 |
my $args = {}; |
723 |
$args->{model_class} = $mclass if $mclass; |
|
724 |
$args->{name} = $model_name if $model_name; |
|
725 |
$args->{table} = $model_table if $model_table; |
|
- added EXPERIMENTAL DBIx::C...
|
726 |
|
removed EXPERIMETNAL flag fr...
|
727 |
# Create model |
728 |
$self->create_model($args); |
|
- added EXPERIMENTAL DBIx::C...
|
729 |
} |
730 |
|
|
731 |
return $self; |
|
732 |
} |
|
733 | ||
cleanup
|
734 |
sub method { |
735 |
my $self = shift; |
|
736 |
|
|
737 |
# Merge |
|
738 |
my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
739 |
$self->{_methods} = {%{$self->{_methods} || {}}, %$methods}; |
|
740 |
|
|
741 |
return $self; |
|
742 |
} |
|
743 | ||
- added EXPERIMENTAL DBIx::C...
|
744 |
sub model { |
745 |
my ($self, $name, $model) = @_; |
|
746 |
|
|
747 |
# Set |
|
748 |
if ($model) { |
|
749 |
$self->models->{$name} = $model; |
|
750 |
return $self; |
|
751 |
} |
|
752 |
|
|
753 |
# Check model existance |
|
754 |
croak qq{Model "$name" is not included} |
|
755 |
unless $self->models->{$name}; |
|
756 |
|
|
757 |
# Get |
|
758 |
return $self->models->{$name}; |
|
759 |
} |
|
760 | ||
cleanup
|
761 |
sub mycolumn { |
762 |
my ($self, $table, $columns) = @_; |
|
763 |
|
|
added EXPERIMENTAL reserved_...
|
764 |
my $q = $self->reserved_word_quote; |
765 |
|
|
cleanup
|
766 |
$columns ||= []; |
767 |
my @column; |
|
added EXPERIMENTAL reserved_...
|
768 |
push @column, "$q$table$q.$q$_$q as $q$_$q" for @$columns; |
cleanup
|
769 |
|
770 |
return join (', ', @column); |
|
771 |
} |
|
772 | ||
added dbi_options attribute
|
773 |
sub new { |
774 |
my $self = shift->SUPER::new(@_); |
|
775 |
|
|
776 |
# Check attribute names |
|
777 |
my @attrs = keys %$self; |
|
778 |
foreach my $attr (@attrs) { |
|
779 |
croak qq{"$attr" is invalid attribute name} |
|
780 |
unless $self->can($attr); |
|
781 |
} |
|
cleanup
|
782 | |
783 |
$self->register_tag( |
|
784 |
'?' => \&DBIx::Custom::Tag::placeholder, |
|
785 |
'=' => \&DBIx::Custom::Tag::equal, |
|
786 |
'<>' => \&DBIx::Custom::Tag::not_equal, |
|
787 |
'>' => \&DBIx::Custom::Tag::greater_than, |
|
788 |
'<' => \&DBIx::Custom::Tag::lower_than, |
|
789 |
'>=' => \&DBIx::Custom::Tag::greater_than_equal, |
|
790 |
'<=' => \&DBIx::Custom::Tag::lower_than_equal, |
|
791 |
'like' => \&DBIx::Custom::Tag::like, |
|
792 |
'in' => \&DBIx::Custom::Tag::in, |
|
793 |
'insert_param' => \&DBIx::Custom::Tag::insert_param, |
|
794 |
'update_param' => \&DBIx::Custom::Tag::update_param |
|
795 |
); |
|
added dbi_options attribute
|
796 |
|
797 |
return $self; |
|
798 |
} |
|
799 | ||
added experimental not_exist...
|
800 |
sub not_exists { bless {}, 'DBIx::Custom::NotExists' } |
801 | ||
cleanup
|
802 |
sub register_filter { |
803 |
my $invocant = shift; |
|
804 |
|
|
805 |
# Register filter |
|
806 |
my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
807 |
$invocant->filters({%{$invocant->filters}, %$filters}); |
|
808 |
|
|
809 |
return $invocant; |
|
810 |
} |
|
packaging one directory
|
811 | |
renamed DBIx::Custom::TagPro...
|
812 |
sub register_tag { shift->query_builder->register_tag(@_) } |
added register_tag_processor
|
813 | |
cleanup
|
814 |
our %SELECT_ARGS |
cleanup
|
815 |
= map { $_ => 1 } @COMMON_ARGS, qw/column where append relation join/; |
refactoring select
|
816 | |
packaging one directory
|
817 |
sub select { |
select, insert, update, upda...
|
818 |
my ($self, %args) = @_; |
added EXPERIMENTAL reserved_...
|
819 | |
820 |
# Quote for reserved word |
|
821 |
my $q = $self->reserved_word_quote; |
|
packaging one directory
|
822 |
|
cleanup
|
823 |
# Check argument names |
select, insert, update, upda...
|
824 |
foreach my $name (keys %args) { |
cleanup
|
825 |
croak qq{Argument "$name" is invalid name} |
cleanup
|
826 |
unless $SELECT_ARGS{$name}; |
refactoring select
|
827 |
} |
packaging one directory
|
828 |
|
refactoring select
|
829 |
# Arguments |
cleanup
|
830 |
my $table = delete $args{table}; |
added table not specified ex...
|
831 |
my $tables = ref $table eq 'ARRAY' ? $table |
832 |
: defined $table ? [$table] |
|
833 |
: []; |
|
cleanup
|
834 |
my $columns = delete $args{column}; |
835 |
my $where = delete $args{where} || {}; |
|
836 |
my $append = delete $args{append}; |
|
837 |
my $join = delete $args{join} || []; |
|
- added experimental DBIx::C...
|
838 |
croak qq{"join" must be array reference} |
839 |
unless ref $join eq 'ARRAY'; |
|
cleanup
|
840 |
my $relation = delete $args{relation}; |
remove experimental DBIx::Cu...
|
841 |
|
cleanup
|
842 |
# Add relation tables(DEPRECATED!); |
cleanup
|
843 |
$self->_add_relation_table($tables, $relation); |
packaging one directory
|
844 |
|
cleanup
|
845 |
# SQL stack |
846 |
my @sql; |
|
847 |
push @sql, 'select'; |
|
packaging one directory
|
848 |
|
cleanup
|
849 |
if ($columns) { |
- added EXPERIMENTAL DBIx::C...
|
850 | |
851 |
$columns = [$columns] if ! ref $columns; |
|
852 |
|
|
853 |
if (ref $columns eq 'HASH') { |
|
854 |
# Find tables |
|
855 |
my $main_table; |
|
856 |
my %tables; |
|
857 |
if ($columns->{table}) { |
|
858 |
foreach my $table (@{$columns->{table}}) { |
|
859 |
if (($table || '') eq $tables->[-1]) { |
|
860 |
$main_table = $table; |
|
861 |
} |
|
862 |
else { |
|
863 |
$tables{$table} = 1; |
|
864 |
} |
|
- added EXPERIMENTAL DBIx::C...
|
865 |
} |
- added EXPERIMENTAL DBIx::C...
|
866 |
} |
867 |
elsif ($columns->{all}) { |
|
868 |
$main_table = $tables->[-1] || ''; |
|
869 |
foreach my $j (@$join) { |
|
870 |
my $tables = $self->_tables($j); |
|
871 |
foreach my $table (@$tables) { |
|
872 |
$tables{$table} = 1; |
|
873 |
} |
|
- added EXPERIMENTAL DBIx::C...
|
874 |
} |
- added EXPERIMENTAL DBIx::C...
|
875 |
delete $tables{$main_table}; |
876 |
} |
|
877 |
|
|
878 |
push @sql, $columns->{prepend} if $columns->{prepend}; |
|
879 |
|
|
880 |
# Column clause of main table |
|
881 |
if ($main_table) { |
|
cleanup
|
882 |
push @sql, $self->model($main_table)->mycolumn; |
- added EXPERIMENTAL DBIx::C...
|
883 |
push @sql, ','; |
- added EXPERIMENTAL DBIx::C...
|
884 |
} |
- added EXPERIMENTAL DBIx::C...
|
885 |
|
886 |
# Column cluase of other tables |
|
887 |
foreach my $table (keys %tables) { |
|
888 |
unshift @$tables, $table; |
|
cleanup
|
889 |
push @sql, $self->model($table)->column($table); |
- added EXPERIMENTAL DBIx::C...
|
890 |
push @sql, ','; |
891 |
} |
|
892 |
pop @sql if $sql[-1] eq ','; |
|
- added EXPERIMENTAL DBIx::C...
|
893 |
} |
894 |
else { |
|
- added EXPERIMENTAL DBIx::C...
|
895 |
foreach my $column (@$columns) { |
896 |
unshift @$tables, @{$self->_tables($column)}; |
|
897 |
push @sql, ($column, ','); |
|
add experimental selection o...
|
898 |
} |
- added EXPERIMENTAL DBIx::C...
|
899 |
pop @sql if $sql[-1] eq ','; |
added select() all_column op...
|
900 |
} |
901 |
} |
|
902 |
|
|
903 |
# "*" is default |
|
904 |
else { push @sql, '*' } |
|
905 |
|
|
906 |
# Table |
|
cleanup
|
907 |
push @sql, 'from'; |
908 |
if ($relation) { |
|
909 |
my $found = {}; |
|
910 |
foreach my $table (@$tables) { |
|
added EXPERIMENTAL reserved_...
|
911 |
push @sql, ("$q$table$q", ',') unless $found->{$table}; |
cleanup
|
912 |
$found->{$table} = 1; |
- added EXPERIMENTAL DBIx::C...
|
913 |
} |
packaging one directory
|
914 |
} |
cleanup
|
915 |
else { |
916 |
my $main_table = $tables->[-1] || ''; |
|
added EXPERIMENTAL reserved_...
|
917 |
push @sql, "$q$main_table$q"; |
cleanup
|
918 |
} |
919 |
pop @sql if ($sql[-1] || '') eq ','; |
|
packaging one directory
|
920 |
|
fixed some select() join opi...
|
921 |
# Main table |
922 |
croak "Not found table name" unless $tables->[-1]; |
|
923 |
|
|
select() where can't receive...
|
924 |
# Where |
where can recieve array refr...
|
925 |
my $w = $self->_where($where); |
926 |
$where = $w->param; |
|
select() where can't receive...
|
927 |
|
928 |
# String where |
|
929 |
my $swhere = "$w"; |
|
remove experimental DBIx::Cu...
|
930 |
|
fixed some select() join opi...
|
931 |
# Add table names in where clause |
932 |
unshift @$tables, @{$self->_tables($swhere)}; |
|
remove experimental DBIx::Cu...
|
933 |
|
fixed some select() join opi...
|
934 |
# Push join |
935 |
$self->_push_join(\@sql, $join, $tables); |
|
remove experimental DBIx::Cu...
|
936 |
|
cleanup
|
937 |
# Add where clause |
cleanup
|
938 |
push @sql, $swhere; |
select() where can't receive...
|
939 |
|
cleanup
|
940 |
# Relation(DEPRECATED!); |
cleanup
|
941 |
$self->_push_relation(\@sql, $tables, $relation, $swhere eq '' ? 1 : 0); |
cleanup
|
942 |
|
cleanup
|
943 |
# Append statement |
944 |
push @sql, $append if $append; |
|
945 |
|
|
946 |
# SQL |
|
947 |
my $sql = join (' ', @sql); |
|
packaging one directory
|
948 |
|
added experimental sugar met...
|
949 |
# Create query |
cleanup
|
950 |
my $query = $self->create_query($sql); |
added experimental sugar met...
|
951 |
return $query if $args{query}; |
952 |
|
|
packaging one directory
|
953 |
# Execute query |
added auto_filter method
|
954 |
my $result = $self->execute( |
cleanup
|
955 |
$query, |
956 |
param => $where, |
|
957 |
table => $tables, |
|
958 |
%args |
|
959 |
); |
|
packaging one directory
|
960 |
|
961 |
return $result; |
|
962 |
} |
|
963 | ||
cleanup
|
964 |
our %SELECT_AT_ARGS = (%SELECT_ARGS, where => 1, primary_key => 1); |
add experimental update_at()...
|
965 | |
966 |
sub select_at { |
|
967 |
my ($self, %args) = @_; |
|
968 |
|
|
cleanup
|
969 |
# Check argument names |
add experimental update_at()...
|
970 |
foreach my $name (keys %args) { |
cleanup
|
971 |
croak qq{Argument "$name" is invalid name} |
cleanup
|
972 |
unless $SELECT_AT_ARGS{$name}; |
add experimental update_at()...
|
973 |
} |
974 |
|
|
975 |
# Primary key |
|
976 |
my $primary_keys = delete $args{primary_key}; |
|
977 |
$primary_keys = [$primary_keys] unless ref $primary_keys; |
|
978 |
|
|
DBIx::Custom::Model select()...
|
979 |
# Table |
980 |
croak qq{"table" option must be specified} unless $args{table}; |
|
981 |
my $table = ref $args{table} ? $args{table}->[-1] : $args{table}; |
|
982 |
|
|
add experimental update_at()...
|
983 |
# Where clause |
984 |
my $where = {}; |
|
985 |
if (exists $args{where}) { |
|
986 |
my $where_columns = delete $args{where}; |
|
- added experimental DBIx::C...
|
987 |
|
988 |
croak qq{"where" must be constant value or array reference} |
|
989 |
unless !ref $where_columns || ref $where_columns eq 'ARRAY'; |
|
990 |
|
|
add experimental update_at()...
|
991 |
$where_columns = [$where_columns] unless ref $where_columns; |
992 |
|
|
993 |
for(my $i = 0; $i < @$primary_keys; $i ++) { |
|
DBIx::Custom::Model select()...
|
994 |
$where->{$table . '.' . $primary_keys->[$i]} = $where_columns->[$i]; |
add experimental update_at()...
|
995 |
} |
996 |
} |
|
- added experimental DBIx::C...
|
997 |
|
998 |
if (exists $args{param}) { |
|
add experimental update_at()...
|
999 |
my $param = delete $args{param}; |
1000 |
for(my $i = 0; $i < @$primary_keys; $i ++) { |
|
- added experimental DBIx::C...
|
1001 |
delete $param->{$primary_keys->[$i]}; |
add experimental update_at()...
|
1002 |
} |
1003 |
} |
|
1004 |
|
|
1005 |
return $self->select(where => $where, %args); |
|
1006 |
} |
|
1007 | ||
add experimental setup_model...
|
1008 |
sub setup_model { |
1009 |
my $self = shift; |
|
1010 |
|
|
1011 |
$self->each_column( |
|
1012 |
sub { |
|
1013 |
my ($self, $table, $column, $column_info) = @_; |
|
1014 |
|
|
1015 |
if (my $model = $self->models->{$table}) { |
|
1016 |
push @{$model->columns}, $column; |
|
1017 |
} |
|
1018 |
} |
|
1019 |
); |
|
add experimental DBIx::Custo...
|
1020 |
return $self; |
add experimental setup_model...
|
1021 |
} |
1022 | ||
cleanup
|
1023 |
our %UPDATE_ARGS |
1024 |
= map { $_ => 1 } @COMMON_ARGS, qw/param where append allow_update_all/; |
|
cleanup
|
1025 | |
1026 |
sub update { |
|
1027 |
my ($self, %args) = @_; |
|
added EXPERIMENTAL reserved_...
|
1028 | |
1029 |
# Quote for reserved word |
|
1030 |
my $q = $self->reserved_word_quote; |
|
version 0.0901
|
1031 |
|
cleanup
|
1032 |
# Check argument names |
cleanup
|
1033 |
foreach my $name (keys %args) { |
cleanup
|
1034 |
croak qq{Argument "$name" is invalid name} |
cleanup
|
1035 |
unless $UPDATE_ARGS{$name}; |
removed reconnect method
|
1036 |
} |
added cache_method attribute
|
1037 |
|
cleanup
|
1038 |
# Arguments |
cleanup
|
1039 |
my $table = delete $args{table} || ''; |
added table not specified ex...
|
1040 |
croak qq{"table" option must be specified} unless $table; |
cleanup
|
1041 |
my $param = delete $args{param} || {}; |
1042 |
my $where = delete $args{where} || {}; |
|
1043 |
my $append = delete $args{append} || ''; |
|
1044 |
my $allow_update_all = delete $args{allow_update_all}; |
|
version 0.0901
|
1045 |
|
select() where can't receive...
|
1046 |
# Columns |
1047 |
my @columns; |
|
- remaned experimental safty...
|
1048 |
my $safety = $self->safety_character; |
select() where can't receive...
|
1049 |
foreach my $column (keys %$param) { |
1050 |
croak qq{"$column" is not safety column name} |
|
- remaned experimental safty...
|
1051 |
unless $column =~ /^[$safety\.]+$/; |
added EXPERIMENTAL reserved_...
|
1052 |
$column = "$q$column$q"; |
1053 |
$column =~ s/\./$q.$q/; |
|
1054 |
push @columns, "$column"; |
|
select() where can't receive...
|
1055 |
} |
1056 |
|
|
cleanup
|
1057 |
# Update clause |
added EXPERIMENTAL reserved_...
|
1058 |
my $update_clause = '{update_param ' . join(' ', @columns) . '}'; |
improved delete() and update...
|
1059 | |
1060 |
# Where |
|
where can recieve array refr...
|
1061 |
my $w = $self->_where($where); |
1062 |
$where = $w->param; |
|
removed reconnect method
|
1063 |
|
select() where can't receive...
|
1064 |
# String where |
1065 |
my $swhere = "$w"; |
|
improved delete() and update...
|
1066 |
|
1067 |
croak qq{"where" must be specified} |
|
select() where can't receive...
|
1068 |
if "$swhere" eq '' && !$allow_update_all; |
removed reconnect method
|
1069 |
|
cleanup
|
1070 |
# SQL stack |
1071 |
my @sql; |
|
1072 |
|
|
1073 |
# Update |
|
added EXPERIMENTAL reserved_...
|
1074 |
push @sql, "update $q$table$q $update_clause $swhere"; |
cleanup
|
1075 |
push @sql, $append if $append; |
removed reconnect method
|
1076 |
|
cleanup
|
1077 |
# Rearrange parameters |
improved delete() and update...
|
1078 |
foreach my $wkey (keys %$where) { |
removed reconnect method
|
1079 |
|
cleanup
|
1080 |
if (exists $param->{$wkey}) { |
1081 |
$param->{$wkey} = [$param->{$wkey}] |
|
1082 |
unless ref $param->{$wkey} eq 'ARRAY'; |
|
1083 |
|
|
1084 |
push @{$param->{$wkey}}, $where->{$wkey}; |
|
1085 |
} |
|
1086 |
else { |
|
1087 |
$param->{$wkey} = $where->{$wkey}; |
|
1088 |
} |
|
removed reconnect method
|
1089 |
} |
cleanup
|
1090 |
|
cleanup
|
1091 |
# SQL |
1092 |
my $sql = join(' ', @sql); |
|
1093 |
|
|
added experimental sugar met...
|
1094 |
# Create query |
cleanup
|
1095 |
my $query = $self->create_query($sql); |
added experimental sugar met...
|
1096 |
return $query if $args{query}; |
1097 |
|
|
cleanup
|
1098 |
# Execute query |
cleanup
|
1099 |
my $ret_val = $self->execute( |
1100 |
$query, |
|
1101 |
param => $param, |
|
1102 |
table => $table, |
|
1103 |
%args |
|
1104 |
); |
|
cleanup
|
1105 |
|
1106 |
return $ret_val; |
|
removed reconnect method
|
1107 |
} |
1108 | ||
cleanup
|
1109 |
sub update_all { shift->update(allow_update_all => 1, @_) }; |
1110 | ||
cleanup
|
1111 |
our %UPDATE_AT_ARGS = (%UPDATE_ARGS, where => 1, primary_key => 1); |
add experimental update_at()...
|
1112 | |
1113 |
sub update_at { |
|
1114 |
my ($self, %args) = @_; |
|
1115 |
|
|
cleanup
|
1116 |
# Check argument names |
add experimental update_at()...
|
1117 |
foreach my $name (keys %args) { |
cleanup
|
1118 |
croak qq{Argument "$name" is invalid name} |
cleanup
|
1119 |
unless $UPDATE_AT_ARGS{$name}; |
add experimental update_at()...
|
1120 |
} |
1121 |
|
|
1122 |
# Primary key |
|
1123 |
my $primary_keys = delete $args{primary_key}; |
|
1124 |
$primary_keys = [$primary_keys] unless ref $primary_keys; |
|
1125 |
|
|
1126 |
# Where clause |
|
1127 |
my $where = {}; |
|
1128 |
my $param = {}; |
|
1129 |
|
|
1130 |
if (exists $args{where}) { |
|
1131 |
my $where_columns = delete $args{where}; |
|
1132 |
$where_columns = [$where_columns] unless ref $where_columns; |
|
- added experimental DBIx::C...
|
1133 | |
1134 |
croak qq{"where" must be constant value or array reference} |
|
1135 |
unless !ref $where_columns || ref $where_columns eq 'ARRAY'; |
|
add experimental update_at()...
|
1136 |
|
1137 |
for(my $i = 0; $i < @$primary_keys; $i ++) { |
|
1138 |
$where->{$primary_keys->[$i]} = $where_columns->[$i]; |
|
1139 |
} |
|
1140 |
} |
|
- added experimental DBIx::C...
|
1141 |
|
1142 |
if (exists $args{param}) { |
|
add experimental update_at()...
|
1143 |
$param = delete $args{param}; |
1144 |
for(my $i = 0; $i < @$primary_keys; $i ++) { |
|
- added experimental DBIx::C...
|
1145 |
delete $param->{$primary_keys->[$i]}; |
add experimental update_at()...
|
1146 |
} |
1147 |
} |
|
1148 |
|
|
1149 |
return $self->update(where => $where, param => $param, %args); |
|
1150 |
} |
|
1151 | ||
remove experimental DBIx::Cu...
|
1152 |
sub update_param { |
1153 |
my ($self, $param) = @_; |
|
1154 |
|
|
added experimental update_pa...
|
1155 |
# Update parameter tag |
remove experimental DBIx::Cu...
|
1156 |
my @tag; |
1157 |
push @tag, '{update_param'; |
|
- remaned experimental safty...
|
1158 |
my $safety = $self->safety_character; |
remove experimental DBIx::Cu...
|
1159 |
foreach my $column (keys %$param) { |
added experimental update_pa...
|
1160 |
croak qq{"$column" is not safety column name} |
- remaned experimental safty...
|
1161 |
unless $column =~ /^[$safety\.]+$/; |
remove experimental DBIx::Cu...
|
1162 |
push @tag, $column; |
1163 |
} |
|
1164 |
push @tag, '}'; |
|
1165 |
|
|
1166 |
return join ' ', @tag; |
|
1167 |
} |
|
1168 | ||
cleanup
|
1169 |
sub where { |
select() where can't receive...
|
1170 |
my $self = shift; |
1171 | ||
1172 |
return DBIx::Custom::Where->new( |
|
1173 |
query_builder => $self->query_builder, |
|
- remaned experimental safty...
|
1174 |
safety_character => $self->safety_character, |
added EXPERIMENTAL reserved_...
|
1175 |
reserved_word_quote => $self->reserved_word_quote, |
cleanup
|
1176 |
@_ |
select() where can't receive...
|
1177 |
); |
cleanup
|
1178 |
} |
added experimental DBIx::Cus...
|
1179 | |
added experimental not_exist...
|
1180 |
sub _bind { |
- added EXPERIMENTAL type() ...
|
1181 |
my ($self, $params, $columns, $filter, $type) = @_; |
removed reconnect method
|
1182 |
|
cleanup
|
1183 |
# bind values |
- added EXPERIMENTAL type() ...
|
1184 |
my $bind = []; |
add tests
|
1185 |
|
removed reconnect method
|
1186 |
# Build bind values |
1187 |
my $count = {}; |
|
added experimental not_exist...
|
1188 |
my $not_exists = {}; |
cleanup
|
1189 |
foreach my $column (@$columns) { |
removed reconnect method
|
1190 |
|
1191 |
# Value |
|
added experimental not_exist...
|
1192 |
my $value; |
1193 |
if(ref $params->{$column} eq 'ARRAY') { |
|
1194 |
my $i = $count->{$column} || 0; |
|
1195 |
$i += $not_exists->{$column} || 0; |
|
1196 |
my $found; |
|
1197 |
for (my $k = $i; $i < @{$params->{$column}}; $k++) { |
|
1198 |
if (ref $params->{$column}->[$k] eq 'DBIx::Custom::NotExists') { |
|
1199 |
$not_exists->{$column}++; |
|
1200 |
} |
|
1201 |
else { |
|
1202 |
$value = $params->{$column}->[$k]; |
|
1203 |
$found = 1; |
|
1204 |
last |
|
1205 |
} |
|
1206 |
} |
|
1207 |
next unless $found; |
|
1208 |
} |
|
1209 |
else { $value = $params->{$column} } |
|
removed reconnect method
|
1210 |
|
cleanup
|
1211 |
# Filter |
1212 |
my $f = $filter->{$column} || $self->{default_out_filter} || ''; |
|
cleanup
|
1213 |
|
- added EXPERIMENTAL type() ...
|
1214 |
# Type |
1215 |
push @$bind, { |
|
1216 |
value => $f ? $f->($value) : $value, |
|
1217 |
type => $type->{$column} |
|
1218 |
}; |
|
removed reconnect method
|
1219 |
|
1220 |
# Count up |
|
1221 |
$count->{$column}++; |
|
1222 |
} |
|
1223 |
|
|
- added EXPERIMENTAL type() ...
|
1224 |
return $bind; |
removed reconnect method
|
1225 |
} |
1226 | ||
EXPERIMETAL fork safety impl...
|
1227 |
sub _connect { |
1228 |
my $self = shift; |
|
1229 |
|
|
1230 |
# Attributes |
|
1231 |
my $data_source = $self->data_source; |
|
1232 |
croak qq{"data_source" must be specified to connect()"} |
|
1233 |
unless $data_source; |
|
1234 |
my $user = $self->user; |
|
1235 |
my $password = $self->password; |
|
1236 |
my $dbi_option = {%{$self->dbi_options}, %{$self->dbi_option}}; |
|
1237 |
|
|
1238 |
# Connect |
|
1239 |
my $dbh = eval {DBI->connect( |
|
1240 |
$data_source, |
|
1241 |
$user, |
|
1242 |
$password, |
|
1243 |
{ |
|
1244 |
%{$self->default_dbi_option}, |
|
1245 |
%$dbi_option |
|
1246 |
} |
|
1247 |
)}; |
|
1248 |
|
|
1249 |
# Connect error |
|
1250 |
croak $@ if $@; |
|
1251 |
|
|
1252 |
return $dbh; |
|
1253 |
} |
|
1254 | ||
cleanup
|
1255 |
sub _croak { |
1256 |
my ($self, $error, $append) = @_; |
|
1257 |
$append ||= ""; |
|
1258 |
|
|
1259 |
# Verbose |
|
1260 |
if ($Carp::Verbose) { croak $error } |
|
1261 |
|
|
1262 |
# Not verbose |
|
1263 |
else { |
|
1264 |
|
|
1265 |
# Remove line and module infromation |
|
1266 |
my $at_pos = rindex($error, ' at '); |
|
1267 |
$error = substr($error, 0, $at_pos); |
|
1268 |
$error =~ s/\s+$//; |
|
1269 |
|
|
1270 |
croak "$error$append"; |
|
1271 |
} |
|
1272 |
} |
|
1273 | ||
added select() all_column op...
|
1274 |
sub _need_tables { |
1275 |
my ($self, $tree, $need_tables, $tables) = @_; |
|
1276 |
|
|
1277 |
foreach my $table (@$tables) { |
|
1278 |
|
|
1279 |
if ($tree->{$table}) { |
|
1280 |
$need_tables->{$table} = 1; |
|
1281 |
$self->_need_tables($tree, $need_tables, [$tree->{$table}{parent}]) |
|
1282 |
} |
|
1283 |
} |
|
1284 |
} |
|
1285 | ||
remove experimental DBIx::Cu...
|
1286 |
sub _tables { |
1287 |
my ($self, $source) = @_; |
|
1288 |
|
|
1289 |
my $tables = []; |
|
1290 |
|
|
- remaned experimental safty...
|
1291 |
my $safety_character = $self->safety_character; |
added EXPERIMENTAL reserved_...
|
1292 |
my $q = $self->reserved_word_quote; |
1293 |
my $q_re = quotemeta($q); |
|
remove experimental DBIx::Cu...
|
1294 |
|
added EXPERIMENTAL reserved_...
|
1295 |
my $table_re = $q ? qr/\b$q_re?([$safety_character]+)$q_re?\./ |
1296 |
: qr/\b([$safety_character]+)\./; |
|
1297 |
while ($source =~ /$table_re/g) { |
|
remove experimental DBIx::Cu...
|
1298 |
push @$tables, $1; |
1299 |
} |
|
1300 |
|
|
1301 |
return $tables; |
|
1302 |
} |
|
1303 | ||
fixed some select() join opi...
|
1304 |
sub _push_join { |
1305 |
my ($self, $sql, $join, $join_tables) = @_; |
|
1306 |
|
|
1307 |
return unless @$join; |
|
1308 |
|
|
added EXPERIMENTAL reserved_...
|
1309 |
my $q = $self->reserved_word_quote; |
1310 |
|
|
fixed some select() join opi...
|
1311 |
my $tree = {}; |
1312 |
|
|
1313 |
for (my $i = 0; $i < @$join; $i++) { |
|
1314 |
|
|
1315 |
my $join_clause = $join->[$i]; |
|
added EXPERIMENTAL reserved_...
|
1316 |
my $q_re = quotemeta($q); |
1317 |
my $join_re = $q ? qr/\s$q_re?([^\.\s$q_re]+?)$q_re?\..+\s$q_re?([^\.\s$q_re]+?)$q_re?\..+?$/ |
|
1318 |
: qr/\s([^\.\s]+?)\..+\s([^\.\s]+?)\..+?$/; |
|
1319 |
if ($join_clause =~ $join_re) { |
|
fixed some select() join opi...
|
1320 |
|
1321 |
my $table1 = $1; |
|
1322 |
my $table2 = $2; |
|
1323 |
|
|
1324 |
croak qq{right side table of "$join_clause" must be uniq} |
|
1325 |
if exists $tree->{$table2}; |
|
1326 |
|
|
1327 |
$tree->{$table2} |
|
1328 |
= {position => $i, parent => $table1, join => $join_clause}; |
|
1329 |
} |
|
1330 |
else { |
|
1331 |
croak qq{join "$join_clause" must be two table name}; |
|
1332 |
} |
|
1333 |
} |
|
1334 |
|
|
1335 |
my $need_tables = {}; |
|
1336 |
$self->_need_tables($tree, $need_tables, $join_tables); |
|
1337 |
|
|
1338 |
my @need_tables = sort { $tree->{$a}{position} <=> $tree->{$b}{position} } keys %$need_tables; |
|
cleanup
|
1339 | |
fixed some select() join opi...
|
1340 |
foreach my $need_table (@need_tables) { |
1341 |
push @$sql, $tree->{$need_table}{join}; |
|
1342 |
} |
|
1343 |
} |
|
cleanup
|
1344 | |
where can recieve array refr...
|
1345 |
sub _where { |
1346 |
my ($self, $where) = @_; |
|
1347 |
|
|
1348 |
my $w; |
|
1349 |
if (ref $where eq 'HASH') { |
|
1350 |
my $clause = ['and']; |
|
added EXPERIMENTAL reserved_...
|
1351 |
my $q = $self->reserved_word_quote; |
1352 |
foreach my $column (keys %$where) { |
|
1353 |
$column = "$q$column$q"; |
|
1354 |
$column =~ s/\./$q.$q/; |
|
1355 |
push @$clause, "{= $column}" for keys %$where; |
|
1356 |
} |
|
1357 |
|
|
where can recieve array refr...
|
1358 |
$w = $self->where(clause => $clause, param => $where); |
1359 |
} |
|
1360 |
elsif (ref $where eq 'DBIx::Custom::Where') { |
|
1361 |
$w = $where; |
|
1362 |
} |
|
1363 |
elsif (ref $where eq 'ARRAY') { |
|
1364 |
$w = $self->where( |
|
1365 |
clause => $where->[0], |
|
1366 |
param => $where->[1] |
|
1367 |
); |
|
1368 |
} |
|
1369 |
|
|
1370 |
croak qq{"where" must be hash reference or DBIx::Custom::Where object} . |
|
1371 |
qq{or array reference, which contains where clause and paramter} |
|
1372 |
unless ref $w eq 'DBIx::Custom::Where'; |
|
1373 |
|
|
1374 |
return $w; |
|
1375 |
} |
|
1376 | ||
cleanup
|
1377 |
# DEPRECATED! |
cleanup
|
1378 |
__PACKAGE__->attr( |
1379 |
dbi_options => sub { {} }, |
|
1380 |
filter_check => 1 |
|
1381 |
); |
|
renamed dbi_options to dbi_o...
|
1382 | |
cleanup
|
1383 |
# DEPRECATED! |
cleanup
|
1384 |
sub default_bind_filter { |
1385 |
my $self = shift; |
|
1386 |
|
|
1387 |
if (@_) { |
|
1388 |
my $fname = $_[0]; |
|
1389 |
|
|
1390 |
if (@_ && !$fname) { |
|
1391 |
$self->{default_out_filter} = undef; |
|
1392 |
} |
|
1393 |
else { |
|
many changed
|
1394 |
croak qq{Filter "$fname" is not registered} |
cleanup
|
1395 |
unless exists $self->filters->{$fname}; |
1396 |
|
|
1397 |
$self->{default_out_filter} = $self->filters->{$fname}; |
|
1398 |
} |
|
1399 |
return $self; |
|
1400 |
} |
|
1401 |
|
|
1402 |
return $self->{default_out_filter}; |
|
1403 |
} |
|
1404 | ||
cleanup
|
1405 |
# DEPRECATED! |
cleanup
|
1406 |
sub default_fetch_filter { |
1407 |
my $self = shift; |
|
1408 |
|
|
1409 |
if (@_) { |
|
many changed
|
1410 |
my $fname = $_[0]; |
1411 | ||
cleanup
|
1412 |
if (@_ && !$fname) { |
1413 |
$self->{default_in_filter} = undef; |
|
1414 |
} |
|
1415 |
else { |
|
many changed
|
1416 |
croak qq{Filter "$fname" is not registered} |
cleanup
|
1417 |
unless exists $self->filters->{$fname}; |
1418 |
|
|
1419 |
$self->{default_in_filter} = $self->filters->{$fname}; |
|
1420 |
} |
|
1421 |
|
|
1422 |
return $self; |
|
1423 |
} |
|
1424 |
|
|
many changed
|
1425 |
return $self->{default_in_filter}; |
cleanup
|
1426 |
} |
1427 | ||
cleanup
|
1428 |
# DEPRECATED! |
renamed DBIx::Custom::TagPro...
|
1429 |
sub register_tag_processor { |
1430 |
return shift->query_builder->register_tag_processor(@_); |
|
1431 |
} |
|
1432 | ||
cleanup
|
1433 |
# DEPRECATED! |
1434 |
sub _push_relation { |
|
1435 |
my ($self, $sql, $tables, $relation, $need_where) = @_; |
|
1436 |
|
|
1437 |
if (keys %{$relation || {}}) { |
|
1438 |
push @$sql, $need_where ? 'where' : 'and'; |
|
1439 |
foreach my $rcolumn (keys %$relation) { |
|
1440 |
my $table1 = (split (/\./, $rcolumn))[0]; |
|
1441 |
my $table2 = (split (/\./, $relation->{$rcolumn}))[0]; |
|
1442 |
push @$tables, ($table1, $table2); |
|
1443 |
push @$sql, ("$rcolumn = " . $relation->{$rcolumn}, 'and'); |
|
1444 |
} |
|
1445 |
} |
|
1446 |
pop @$sql if $sql->[-1] eq 'and'; |
|
1447 |
} |
|
1448 | ||
1449 |
# DEPRECATED! |
|
1450 |
sub _add_relation_table { |
|
cleanup
|
1451 |
my ($self, $tables, $relation) = @_; |
cleanup
|
1452 |
|
1453 |
if (keys %{$relation || {}}) { |
|
1454 |
foreach my $rcolumn (keys %$relation) { |
|
1455 |
my $table1 = (split (/\./, $rcolumn))[0]; |
|
1456 |
my $table2 = (split (/\./, $relation->{$rcolumn}))[0]; |
|
1457 |
my $table1_exists; |
|
1458 |
my $table2_exists; |
|
1459 |
foreach my $table (@$tables) { |
|
1460 |
$table1_exists = 1 if $table eq $table1; |
|
1461 |
$table2_exists = 1 if $table eq $table2; |
|
1462 |
} |
|
1463 |
unshift @$tables, $table1 unless $table1_exists; |
|
1464 |
unshift @$tables, $table2 unless $table2_exists; |
|
1465 |
} |
|
1466 |
} |
|
1467 |
} |
|
1468 | ||
fixed DBIx::Custom::QueryBui...
|
1469 |
1; |
1470 | ||
removed reconnect method
|
1471 |
=head1 NAME |
1472 | ||
- remaned experimental safty...
|
1473 |
DBIx::Custom - Useful database access, respecting SQL! |
removed reconnect method
|
1474 | |
1475 |
=head1 SYNOPSYS |
|
cleanup
|
1476 | |
renamed build_query to creat...
|
1477 |
use DBIx::Custom; |
- remaned experimental safty...
|
1478 |
|
1479 |
# Connect |
|
1480 |
my $dbi = DBIx::Custom->connect( |
|
1481 |
data_source => "dbi:mysql:database=dbname", |
|
1482 |
user => 'ken', |
|
1483 |
password => '!LFKD%$&', |
|
1484 |
dbi_option => {mysql_enable_utf8 => 1} |
|
1485 |
); |
|
cleanup
|
1486 | |
removed reconnect method
|
1487 |
# Insert |
- remaned experimental safty...
|
1488 |
$dbi->insert( |
1489 |
table => 'book', |
|
1490 |
param => {title => 'Perl', author => 'Ken'} |
|
1491 |
); |
|
removed reconnect method
|
1492 |
|
1493 |
# Update |
|
- remaned experimental safty...
|
1494 |
$dbi->update( |
1495 |
table => 'book', |
|
1496 |
param => {title => 'Perl', author => 'Ken'}, |
|
1497 |
where => {id => 5}, |
|
1498 |
); |
|
removed reconnect method
|
1499 |
|
1500 |
# Delete |
|
- remaned experimental safty...
|
1501 |
$dbi->delete( |
1502 |
table => 'book', |
|
1503 |
where => {author => 'Ken'}, |
|
1504 |
); |
|
cleanup
|
1505 | |
removed reconnect method
|
1506 |
# Select |
renamed fetch_rows to fetch_...
|
1507 |
my $result = $dbi->select( |
added insert, update, update...
|
1508 |
table => 'book', |
update document
|
1509 |
where => {author => 'Ken'}, |
added commit method
|
1510 |
); |
cleanup
|
1511 | |
- remaned experimental safty...
|
1512 |
# Select, more complex |
1513 |
my $result = $dbi->select( |
|
1514 |
table => 'book', |
|
1515 |
column => [ |
|
1516 |
'book.author as book__author', |
|
1517 |
'company.name as company__name' |
|
1518 |
], |
|
1519 |
where => {'book.author' => 'Ken'}, |
|
1520 |
join => ['left outer join company on book.company_id = company.id'], |
|
1521 |
append => 'order by id limit 5' |
|
removed reconnect method
|
1522 |
); |
- remaned experimental safty...
|
1523 |
|
removed register_format()
|
1524 |
# Fetch |
1525 |
while (my $row = $result->fetch) { |
|
- remaned experimental safty...
|
1526 |
|
removed register_format()
|
1527 |
} |
1528 |
|
|
- remaned experimental safty...
|
1529 |
# Fetch as hash |
removed register_format()
|
1530 |
while (my $row = $result->fetch_hash) { |
1531 |
|
|
1532 |
} |
|
1533 |
|
|
- remaned experimental safty...
|
1534 |
# Execute SQL with parameter. |
1535 |
$dbi->execute( |
|
1536 |
"select id from book where {= author} and {like title}", |
|
1537 |
param => {author => 'ken', title => '%Perl%'} |
|
1538 |
); |
|
1539 |
|
|
renamed update tag to update...
|
1540 |
=head1 DESCRIPTIONS |
removed reconnect method
|
1541 | |
- remaned experimental safty...
|
1542 |
L<DBIx::Custom> is L<DBI> wrapper module. |
1543 | ||
1544 |
=head1 FEATURES |
|
removed reconnect method
|
1545 | |
- remaned experimental safty...
|
1546 |
=over 4 |
removed reconnect method
|
1547 | |
- remaned experimental safty...
|
1548 |
=item * |
removed reconnect method
|
1549 | |
- remaned experimental safty...
|
1550 |
There are many basic methods to execute various queries. |
1551 |
C<insert()>, C<update()>, C<update_all()>,C<delete()>, |
|
1552 |
C<delete_all()>, C<select()>, |
|
1553 |
C<insert_at()>, C<update_at()>, |
|
1554 |
C<delete_at()>, C<select_at()>, C<execute()> |
|
removed reconnect method
|
1555 | |
- remaned experimental safty...
|
1556 |
=item * |
1557 | ||
1558 |
Filter when data is send or receive. |
|
1559 | ||
1560 |
=item * |
|
1561 | ||
1562 |
Data filtering system |
|
1563 | ||
1564 |
=item * |
|
1565 | ||
1566 |
Model support. |
|
1567 | ||
1568 |
=item * |
|
1569 | ||
1570 |
Generate where clause dinamically. |
|
1571 | ||
1572 |
=item * |
|
1573 | ||
1574 |
Generate join clause dinamically. |
|
1575 | ||
1576 |
=back |
|
pod fix
|
1577 | |
1578 |
=head1 GUIDE |
|
1579 | ||
- remaned experimental safty...
|
1580 |
L<DBIx::Custom::Guide> - L<DBIx::Custom> Guide |
pod fix
|
1581 | |
- remaned experimental safty...
|
1582 |
=head1 Wiki |
pod fix
|
1583 | |
- remaned experimental safty...
|
1584 |
L<DBIx::Custom Wiki|https://github.com/yuki-kimoto/DBIx-Custom/wiki> |
updated document
|
1585 | |
update document
|
1586 |
=head1 ATTRIBUTES |
packaging one directory
|
1587 | |
removed DBIx::Custom commit ...
|
1588 |
=head2 C<data_source> |
packaging one directory
|
1589 | |
cleanup
|
1590 |
my $data_source = $dbi->data_source; |
cleanup
|
1591 |
$dbi = $dbi->data_source("DBI:mysql:database=dbname"); |
removed DESTROY method(not b...
|
1592 | |
- remaned experimental safty...
|
1593 |
Data source, used when C<connect()> is executed. |
removed DESTROY method(not b...
|
1594 | |
renamed dbi_options to dbi_o...
|
1595 |
=head2 C<dbi_option> |
added dbi_options attribute
|
1596 | |
renamed dbi_options to dbi_o...
|
1597 |
my $dbi_option = $dbi->dbi_option; |
- remaned experimental safty...
|
1598 |
$dbi = $dbi->dbi_option($dbi_option); |
add default_dbi_option()
|
1599 | |
- remaned experimental safty...
|
1600 |
L<DBI> option, used when C<connect()> is executed. |
1601 |
Each value in option override the value of C<default_dbi_option>. |
|
add default_dbi_option()
|
1602 | |
1603 |
=head2 C<default_dbi_option> |
|
1604 | ||
1605 |
my $default_dbi_option = $dbi->default_dbi_option; |
|
1606 |
$dbi = $dbi->default_dbi_option($default_dbi_option); |
|
1607 | ||
- remaned experimental safty...
|
1608 |
L<DBI> default option, used when C<connect()> is executed, |
1609 |
default to the following values. |
|
add default_dbi_option()
|
1610 | |
- remaned experimental safty...
|
1611 |
{ |
1612 |
RaiseError => 1, |
|
1613 |
PrintError => 0, |
|
1614 |
AutoCommit => 1, |
|
1615 |
} |
|
packaging one directory
|
1616 | |
update pod
|
1617 |
You should not change C<AutoCommit> value directly, |
1618 |
the value is used to check if the process is in transaction. |
|
EXPERIMETAL fork safety impl...
|
1619 | |
cleanup
|
1620 |
=head2 C<filters> |
bind_filter argument is chan...
|
1621 | |
cleanup
|
1622 |
my $filters = $dbi->filters; |
1623 |
$dbi = $dbi->filters(\%filters); |
|
packaging one directory
|
1624 | |
- remaned experimental safty...
|
1625 |
Filters, registered by C<register_filter()>. |
add models() attribute
|
1626 | |
update pod
|
1627 |
=head2 C<models> EXPERIMENTAL |
add models() attribute
|
1628 | |
1629 |
my $models = $dbi->models; |
|
1630 |
$dbi = $dbi->models(\%models); |
|
1631 | ||
- remaned experimental safty...
|
1632 |
Models, included by C<include_model()>. |
add models() attribute
|
1633 | |
cleanup
|
1634 |
=head2 C<password> |
1635 | ||
1636 |
my $password = $dbi->password; |
|
1637 |
$dbi = $dbi->password('lkj&le`@s'); |
|
1638 | ||
- remaned experimental safty...
|
1639 |
Password, used when C<connect()> is executed. |
update document
|
1640 | |
renamed update tag to update...
|
1641 |
=head2 C<query_builder> |
added commit method
|
1642 | |
renamed update tag to update...
|
1643 |
my $sql_class = $dbi->query_builder; |
1644 |
$dbi = $dbi->query_builder(DBIx::Custom::QueryBuilder->new); |
|
added commit method
|
1645 | |
- remaned experimental safty...
|
1646 |
Query builder, default to L<DBIx::Custom::QueryBuilder> object. |
cleanup
|
1647 | |
added EXPERIMENTAL reserved_...
|
1648 |
=head2 C<reserved_word_quote> EXPERIMENTAL |
1649 | ||
1650 |
my reserved_word_quote = $dbi->reserved_word_quote; |
|
1651 |
$dbi = $dbi->reserved_word_quote('"'); |
|
1652 | ||
1653 |
Quote for reserved word, default to empty string. |
|
1654 | ||
cleanup
|
1655 |
=head2 C<result_class> |
cleanup
|
1656 | |
cleanup
|
1657 |
my $result_class = $dbi->result_class; |
1658 |
$dbi = $dbi->result_class('DBIx::Custom::Result'); |
|
cleanup
|
1659 | |
- remaned experimental safty...
|
1660 |
Result class, default to L<DBIx::Custom::Result>. |
cleanup
|
1661 | |
removed EXPERIMETNAL flag fr...
|
1662 |
=head2 C<safety_character> |
update pod
|
1663 | |
- remaned experimental safty...
|
1664 |
my $safety_character = $self->safety_character; |
cleanup
|
1665 |
$dbi = $self->safety_character($character); |
update pod
|
1666 | |
update pod
|
1667 |
Regex of safety character for table and column name, default to '\w'. |
cleanup
|
1668 |
Note that you don't have to specify like '[\w]'. |
update pod
|
1669 | |
cleanup
|
1670 |
=head2 C<user> |
cleanup
|
1671 | |
cleanup
|
1672 |
my $user = $dbi->user; |
1673 |
$dbi = $dbi->user('Ken'); |
|
cleanup
|
1674 | |
cleanup
|
1675 |
User name, used when C<connect()> is executed. |
update pod
|
1676 | |
cleanup
|
1677 |
=head1 METHODS |
added commit method
|
1678 | |
cleanup
|
1679 |
L<DBIx::Custom> inherits all methods from L<Object::Simple> |
cleanup
|
1680 |
and use all methods of L<DBI> |
cleanup
|
1681 |
and implements the following new ones. |
added check_filter attribute
|
1682 | |
update pod
|
1683 |
=head2 C<apply_filter> EXPERIMENTAL |
added auto_filter method
|
1684 | |
renamed auto_filter to apply...
|
1685 |
$dbi->apply_filter( |
cleanup
|
1686 |
'book', |
update pod
|
1687 |
'issue_date' => { |
1688 |
out => 'tp_to_date', |
|
1689 |
in => 'date_to_tp', |
|
1690 |
end => 'tp_to_displaydate' |
|
1691 |
}, |
|
1692 |
'write_date' => { |
|
1693 |
out => 'tp_to_date', |
|
1694 |
in => 'date_to_tp', |
|
1695 |
end => 'tp_to_displaydate' |
|
1696 |
} |
|
added auto_filter method
|
1697 |
); |
1698 | ||
update pod
|
1699 |
Apply filter to columns. |
1700 |
C<out> filter is executed before data is send to database. |
|
1701 |
C<in> filter is executed after a row is fetch. |
|
1702 |
C<end> filter is execute after C<in> filter is executed. |
|
1703 | ||
1704 |
Filter is applied to the follwoing tree column name pattern. |
|
cleanup
|
1705 | |
update pod
|
1706 |
PETTERN EXAMPLE |
1707 |
1. Column : author |
|
1708 |
2. Table.Column : book.author |
|
1709 |
3. Table__Column : book__author |
|
fix bug : filter can't over...
|
1710 | |
update pod
|
1711 |
If column name is duplicate with other table, |
1712 |
Main filter specified by C<table> option is used. |
|
1713 | ||
1714 |
You can set multiple filters at once. |
|
1715 | ||
1716 |
$dbi->apply_filter( |
|
1717 |
'book', |
|
1718 |
[qw/issue_date write_date/] => { |
|
1719 |
out => 'tp_to_date', |
|
1720 |
in => 'date_to_tp', |
|
1721 |
end => 'tp_to_displaydate' |
|
1722 |
} |
|
1723 |
); |
|
fix bug : filter can't over...
|
1724 | |
removed DBIx::Custom commit ...
|
1725 |
=head2 C<connect> |
packaging one directory
|
1726 | |
update pod
|
1727 |
my $dbi = DBIx::Custom->connect( |
1728 |
data_source => "dbi:mysql:database=dbname", |
|
1729 |
user => 'ken', |
|
1730 |
password => '!LFKD%$&', |
|
1731 |
dbi_option => {mysql_enable_utf8 => 1} |
|
1732 |
); |
|
1733 | ||
1734 |
Connect to the database and create a new L<DBIx::Custom> object. |
|
bind_filter argument is chan...
|
1735 | |
renamed build_query to creat...
|
1736 |
L<DBIx::Custom> is a wrapper of L<DBI>. |
cleanup
|
1737 |
C<AutoCommit> and C<RaiseError> options are true, |
update pod
|
1738 |
and C<PrintError> option is false by default. |
packaging one directory
|
1739 | |
removed EXPERIMETNAL flag fr...
|
1740 |
=head2 create_model |
1741 | ||
adeed EXPERIMENTAL DBIx::Cus...
|
1742 |
my $model = $dbi->create_model( |
removed EXPERIMETNAL flag fr...
|
1743 |
table => 'book', |
1744 |
primary_key => 'id', |
|
1745 |
join => [ |
|
1746 |
'inner join company on book.comparny_id = company.id' |
|
1747 |
], |
|
1748 |
filter => [ |
|
1749 |
publish_date => { |
|
1750 |
out => 'tp_to_date', |
|
1751 |
in => 'date_to_tp', |
|
1752 |
end => 'tp_to_displaydate' |
|
1753 |
} |
|
1754 |
] |
|
1755 |
); |
|
1756 | ||
1757 |
Create L<DBIx::Custom::Model> object and initialize model. |
|
adeed EXPERIMENTAL DBIx::Cus...
|
1758 |
the module is also used from model() method. |
removed EXPERIMETNAL flag fr...
|
1759 | |
1760 |
$dbi->model('book')->select(...); |
|
1761 | ||
cleanup
|
1762 |
=head2 C<create_query> |
1763 |
|
|
1764 |
my $query = $dbi->create_query( |
|
update pod
|
1765 |
"insert into book {insert_param title author};"; |
cleanup
|
1766 |
); |
update document
|
1767 | |
update pod
|
1768 |
Create L<DBIx::Custom::Query> object. |
1769 | ||
cleanup
|
1770 |
If you want to get high performance, |
update pod
|
1771 |
create L<DBIx::Custom::Query> object and execute the query by C<execute()> |
1772 |
instead of other methods, such as C<insert>, C<update>. |
|
bind_filter argument is chan...
|
1773 | |
cleanup
|
1774 |
$dbi->execute($query, {author => 'Ken', title => '%Perl%'}); |
version 0.0901
|
1775 | |
EXPERIMETAL fork safety impl...
|
1776 |
=head2 C<dbh> |
1777 | ||
1778 |
my $dbh = $dbi->dbh; |
|
1779 |
$dbi = $dbi->dbh($dbh); |
|
1780 | ||
1781 |
Get and set database handle of L<DBI>. |
|
1782 | ||
update pod
|
1783 |
If process is spawn by forking, new connection is created automatically. |
removed EXPERIMETNAL flag fr...
|
1784 | |
1785 |
=head2 C<each_column> |
|
1786 | ||
1787 |
$dbi->each_column( |
|
1788 |
sub { |
|
1789 |
my ($dbi, $table, $column, $column_info) = @_; |
|
1790 |
|
|
1791 |
my $type = $column_info->{TYPE_NAME}; |
|
1792 |
|
|
1793 |
if ($type eq 'DATE') { |
|
1794 |
# ... |
|
1795 |
} |
|
1796 |
} |
|
1797 |
); |
|
1798 | ||
1799 |
Iterate all column informations of all table from database. |
|
1800 |
Argument is callback when one column is found. |
|
1801 |
Callback receive four arguments, dbi object, table name, |
|
1802 |
column name and column information. |
|
EXPERIMETAL fork safety impl...
|
1803 | |
cleanup
|
1804 |
=head2 C<execute> |
packaging one directory
|
1805 | |
update pod
|
1806 |
my $result = $dbi->execute( |
1807 |
"select * from book where {= title} and {like author}", |
|
1808 |
param => {title => 'Perl', author => '%Ken%'} |
|
1809 |
); |
|
1810 | ||
1811 |
Execute SQL, containing tags. |
|
1812 |
Return value is L<DBIx::Custom::Result> in select statement, or |
|
1813 |
the count of affected rows in insert, update, delete statement. |
|
1814 | ||
1815 |
Tag is turned into the statement containing place holder |
|
1816 |
before SQL is executed. |
|
1817 | ||
1818 |
select * from where title = ? and author like ?; |
|
1819 | ||
1820 |
See also L<Tags/Tags>. |
|
1821 | ||
1822 |
The following opitons are currently available. |
|
1823 | ||
1824 |
=over 4 |
|
1825 | ||
1826 |
=item C<filter> |
|
1827 | ||
1828 |
Filter, executed before data is send to database. This is array reference. |
|
1829 |
Filter value is code reference or |
|
1830 |
filter name registerd by C<register_filter()>. |
|
1831 | ||
1832 |
# Basic |
|
1833 |
$dbi->execute( |
|
1834 |
$sql, |
|
1835 |
filter => [ |
|
1836 |
title => sub { uc $_[0] } |
|
1837 |
author => sub { uc $_[0] } |
|
1838 |
] |
|
1839 |
); |
|
1840 |
|
|
1841 |
# At once |
|
1842 |
$dbi->execute( |
|
1843 |
$sql, |
|
1844 |
filter => [ |
|
1845 |
[qw/title author/] => sub { uc $_[0] } |
|
1846 |
] |
|
1847 |
); |
|
1848 |
|
|
1849 |
# Filter name |
|
1850 |
$dbi->execute( |
|
1851 |
$sql, |
|
1852 |
filter => [ |
|
1853 |
title => 'upper_case', |
|
1854 |
author => 'upper_case' |
|
1855 |
] |
|
1856 |
); |
|
1857 | ||
1858 |
These filters are added to the C<out> filters, set by C<apply_filter()>. |
|
update document
|
1859 | |
update pod
|
1860 |
=back |
version 0.0901
|
1861 | |
removed DBIx::Custom commit ...
|
1862 |
=head2 C<delete> |
packaging one directory
|
1863 | |
update pod
|
1864 |
$dbi->delete(table => 'book', where => {title => 'Perl'}); |
1865 | ||
1866 |
Delete statement. |
|
1867 | ||
1868 |
The following opitons are currently available. |
|
1869 | ||
update pod
|
1870 |
=over 4 |
1871 | ||
update pod
|
1872 |
=item C<table> |
1873 | ||
1874 |
Table name. |
|
1875 | ||
1876 |
$dbi->delete(table => 'book'); |
|
1877 | ||
1878 |
=item C<where> |
|
1879 | ||
where can recieve array refr...
|
1880 |
Where clause. This is hash reference or L<DBIx::Custom::Where> object |
1881 |
or array refrence, which contains where clause and paramter. |
|
update pod
|
1882 |
|
1883 |
# Hash reference |
|
1884 |
$dbi->delete(where => {title => 'Perl'}); |
|
1885 |
|
|
1886 |
# DBIx::Custom::Where object |
|
1887 |
my $where = $dbi->where( |
|
1888 |
clause => ['and', '{= author}', '{like title}'], |
|
1889 |
param => {author => 'Ken', title => '%Perl%'} |
|
1890 |
); |
|
1891 |
$dbi->delete(where => $where); |
|
1892 | ||
where can recieve array refr...
|
1893 |
# Array refrendce (where clause and parameter) |
1894 |
$dbi->delete(where => |
|
1895 |
[ |
|
1896 |
['and', '{= author}', '{like title}'], |
|
1897 |
{author => 'Ken', title => '%Perl%'} |
|
1898 |
] |
|
1899 |
); |
|
1900 |
|
|
update pod
|
1901 |
=item C<append> |
1902 | ||
1903 |
Append statement to last of SQL. This is string. |
|
1904 | ||
1905 |
$dbi->delete(append => 'order by title'); |
|
1906 | ||
1907 |
=item C<filter> |
|
1908 | ||
1909 |
Filter, executed before data is send to database. This is array reference. |
|
1910 |
Filter value is code reference or |
|
1911 |
filter name registerd by C<register_filter()>. |
|
1912 | ||
1913 |
# Basic |
|
1914 |
$dbi->delete( |
|
1915 |
filter => [ |
|
1916 |
title => sub { uc $_[0] } |
|
1917 |
author => sub { uc $_[0] } |
|
1918 |
] |
|
1919 |
); |
|
1920 |
|
|
1921 |
# At once |
|
1922 |
$dbi->delete( |
|
1923 |
filter => [ |
|
1924 |
[qw/title author/] => sub { uc $_[0] } |
|
1925 |
] |
|
1926 |
); |
|
1927 |
|
|
1928 |
# Filter name |
|
1929 |
$dbi->delete( |
|
1930 |
filter => [ |
|
1931 |
title => 'upper_case', |
|
1932 |
author => 'upper_case' |
|
1933 |
] |
|
1934 |
); |
|
1935 | ||
1936 |
These filters are added to the C<out> filters, set by C<apply_filter()>. |
|
1937 | ||
cleanup
|
1938 |
=head2 C<column> EXPERIMENTAL |
1939 | ||
1940 |
my $column = $self->column(book => ['author', 'title']); |
|
1941 | ||
1942 |
Create column clause. The follwoing column clause is created. |
|
1943 | ||
1944 |
book.author as book__author, |
|
1945 |
book.title as book__title |
|
1946 | ||
removed EXPERIMETNAL flag fr...
|
1947 |
=item C<query> |
update pod
|
1948 | |
1949 |
Get L<DBIx::Custom::Query> object instead of executing SQL. |
|
1950 |
This is true or false value. |
|
1951 | ||
1952 |
my $query = $dbi->delete(query => 1); |
|
1953 | ||
1954 |
You can check SQL. |
|
1955 | ||
1956 |
my $sql = $query->sql; |
|
renamed build_query to creat...
|
1957 | |
update pod
|
1958 |
=back |
1959 | ||
removed DBIx::Custom commit ...
|
1960 |
=head2 C<delete_all> |
packaging one directory
|
1961 | |
cleanup
|
1962 |
$dbi->delete_all(table => $table); |
packaging one directory
|
1963 | |
update pod
|
1964 |
Delete statement to delete all rows. |
1965 |
Options is same as C<delete()>. |
|
bind_filter argument is chan...
|
1966 | |
removed EXPERIMETNAL flag fr...
|
1967 |
=head2 C<delete_at()> |
add experimental update_at()...
|
1968 | |
update pod
|
1969 |
Delete statement, using primary key. |
add experimental update_at()...
|
1970 | |
1971 |
$dbi->delete_at( |
|
1972 |
table => 'book', |
|
update pod
|
1973 |
primary_key => 'id', |
1974 |
where => '5' |
|
add experimental update_at()...
|
1975 |
); |
1976 | ||
update pod
|
1977 |
This method is same as C<delete()> exept that |
1978 |
C<primary_key> is specified and C<where> is constant value or array refrence. |
|
1979 |
all option of C<delete()> is available. |
|
add experimental update_at()...
|
1980 | |
update pod
|
1981 |
=over 4 |
1982 | ||
1983 |
=item C<primary_key> |
|
add experimental update_at()...
|
1984 | |
update pod
|
1985 |
Primary key. This is constant value or array reference. |
1986 |
|
|
1987 |
# Constant value |
|
1988 |
$dbi->delete(primary_key => 'id'); |
|
1989 | ||
1990 |
# Array reference |
|
1991 |
$dbi->delete(primary_key => ['id1', 'id2' ]); |
|
1992 | ||
1993 |
This is used to create where clause. |
|
1994 | ||
update pod
|
1995 |
=item C<where> |
update pod
|
1996 | |
1997 |
Where clause, created from primary key information. |
|
1998 |
This is constant value or array reference. |
|
1999 | ||
2000 |
# Constant value |
|
2001 |
$dbi->delete(where => 5); |
|
2002 | ||
2003 |
# Array reference |
|
2004 |
$dbi->delete(where => [3, 5]); |
|
2005 | ||
2006 |
In first examle, the following SQL is created. |
|
2007 | ||
2008 |
delete from book where id = ?; |
|
2009 | ||
2010 |
Place holder is set to 5. |
|
add experimental update_at()...
|
2011 | |
update pod
|
2012 |
=back |
2013 | ||
cleanup
|
2014 |
=head2 C<insert> |
2015 | ||
update pod
|
2016 |
$dbi->insert( |
2017 |
table => 'book', |
|
2018 |
param => {title => 'Perl', author => 'Ken'} |
|
2019 |
); |
|
2020 | ||
2021 |
Insert statement. |
|
2022 | ||
2023 |
The following opitons are currently available. |
|
2024 | ||
update pod
|
2025 |
=over 4 |
2026 | ||
update pod
|
2027 |
=item C<table> |
2028 | ||
2029 |
Table name. |
|
2030 | ||
2031 |
$dbi->insert(table => 'book'); |
|
2032 | ||
2033 |
=item C<param> |
|
2034 | ||
2035 |
Insert data. This is hash reference. |
|
2036 | ||
2037 |
$dbi->insert(param => {title => 'Perl'}); |
|
2038 | ||
2039 |
=item C<append> |
|
2040 | ||
2041 |
Append statement to last of SQL. This is string. |
|
2042 | ||
2043 |
$dbi->insert(append => 'order by title'); |
|
2044 | ||
2045 |
=item C<filter> |
|
2046 | ||
2047 |
Filter, executed before data is send to database. This is array reference. |
|
2048 |
Filter value is code reference or |
|
2049 |
filter name registerd by C<register_filter()>. |
|
2050 | ||
2051 |
# Basic |
|
2052 |
$dbi->insert( |
|
2053 |
filter => [ |
|
2054 |
title => sub { uc $_[0] } |
|
2055 |
author => sub { uc $_[0] } |
|
2056 |
] |
|
2057 |
); |
|
2058 |
|
|
2059 |
# At once |
|
2060 |
$dbi->insert( |
|
2061 |
filter => [ |
|
2062 |
[qw/title author/] => sub { uc $_[0] } |
|
2063 |
] |
|
2064 |
); |
|
2065 |
|
|
2066 |
# Filter name |
|
2067 |
$dbi->insert( |
|
2068 |
filter => [ |
|
2069 |
title => 'upper_case', |
|
2070 |
author => 'upper_case' |
|
2071 |
] |
|
2072 |
); |
|
2073 | ||
2074 |
These filters are added to the C<out> filters, set by C<apply_filter()>. |
|
2075 | ||
removed EXPERIMETNAL flag fr...
|
2076 |
=item C<query> |
update pod
|
2077 | |
2078 |
Get L<DBIx::Custom::Query> object instead of executing SQL. |
|
2079 |
This is true or false value. |
|
2080 | ||
2081 |
my $query = $dbi->insert(query => 1); |
|
cleanup
|
2082 | |
update pod
|
2083 |
You can check SQL. |
cleanup
|
2084 | |
update pod
|
2085 |
my $sql = $query->sql; |
2086 | ||
update pod
|
2087 |
=back |
2088 | ||
removed EXPERIMETNAL flag fr...
|
2089 |
=head2 C<insert_at()> |
added experimental DBIx::Cus...
|
2090 | |
update pod
|
2091 |
Insert statement, using primary key. |
added experimental DBIx::Cus...
|
2092 | |
2093 |
$dbi->insert_at( |
|
2094 |
table => 'book', |
|
update pod
|
2095 |
primary_key => 'id', |
2096 |
where => '5', |
|
2097 |
param => {title => 'Perl'} |
|
added experimental DBIx::Cus...
|
2098 |
); |
2099 | ||
update pod
|
2100 |
This method is same as C<insert()> exept that |
2101 |
C<primary_key> is specified and C<where> is constant value or array refrence. |
|
2102 |
all option of C<insert()> is available. |
|
2103 | ||
update pod
|
2104 |
=over 4 |
2105 | ||
2106 |
=item C<primary_key> |
|
update pod
|
2107 | |
2108 |
Primary key. This is constant value or array reference. |
|
2109 |
|
|
2110 |
# Constant value |
|
2111 |
$dbi->insert(primary_key => 'id'); |
|
2112 | ||
2113 |
# Array reference |
|
2114 |
$dbi->insert(primary_key => ['id1', 'id2' ]); |
|
2115 | ||
2116 |
This is used to create parts of insert data. |
|
2117 | ||
update pod
|
2118 |
=item C<where> |
update pod
|
2119 | |
2120 |
Parts of Insert data, create from primary key information. |
|
2121 |
This is constant value or array reference. |
|
2122 | ||
2123 |
# Constant value |
|
2124 |
$dbi->insert(where => 5); |
|
2125 | ||
2126 |
# Array reference |
|
2127 |
$dbi->insert(where => [3, 5]); |
|
2128 | ||
2129 |
In first examle, the following SQL is created. |
|
2130 | ||
2131 |
insert into book (id, title) values (?, ?); |
|
2132 | ||
2133 |
Place holders are set to 5 and 'Perl'. |
|
added experimental DBIx::Cus...
|
2134 | |
update pod
|
2135 |
=back |
2136 | ||
removed EXPERIMETNAL flag fr...
|
2137 |
=head2 C<insert_param> |
added experimental update_pa...
|
2138 | |
2139 |
my $insert_param = $dbi->insert_param({title => 'a', age => 2}); |
|
2140 | ||
2141 |
Create insert parameter tag. |
|
2142 | ||
update pod
|
2143 |
{insert_param title age} |
added experimental update_pa...
|
2144 | |
update pod
|
2145 |
=head2 C<include_model> EXPERIMENTAL |
removed experimental base_ta...
|
2146 | |
update pod
|
2147 |
$dbi->include_model('MyModel'); |
removed experimental base_ta...
|
2148 | |
update pod
|
2149 |
Include models from specified namespace, |
2150 |
the following layout is needed to include models. |
|
removed experimental base_ta...
|
2151 | |
update pod
|
2152 |
lib / MyModel.pm |
2153 |
/ MyModel / book.pm |
|
2154 |
/ company.pm |
|
add feture. all model class ...
|
2155 | |
update pod
|
2156 |
Name space module, extending L<DBIx::Custom::Model>. |
add feture. all model class ...
|
2157 | |
update pod
|
2158 |
B<MyModel.pm> |
add feture. all model class ...
|
2159 | |
2160 |
package MyModel; |
|
2161 |
|
|
2162 |
use base 'DBIx::Custom::Model'; |
|
update pod
|
2163 |
|
2164 |
1; |
|
add feture. all model class ...
|
2165 | |
update pod
|
2166 |
Model modules, extending name space module. |
removed experimental base_ta...
|
2167 | |
update pod
|
2168 |
B<MyModel/book.pm> |
removed experimental base_ta...
|
2169 | |
update pod
|
2170 |
package MyModel::book; |
2171 |
|
|
2172 |
use base 'MyModel'; |
|
2173 |
|
|
2174 |
1; |
|
removed experimental base_ta...
|
2175 | |
update pod
|
2176 |
B<MyModel/company.pm> |
removed experimental base_ta...
|
2177 | |
update pod
|
2178 |
package MyModel::company; |
2179 |
|
|
2180 |
use base 'MyModel'; |
|
2181 |
|
|
2182 |
1; |
|
2183 |
|
|
2184 |
MyModel::book and MyModel::company is included by C<include_model()>. |
|
removed experimental base_ta...
|
2185 | |
update pod
|
2186 |
You can get model object by C<model()>. |
2187 | ||
2188 |
my $book_model = $dbi->model('book'); |
|
2189 |
my $company_model = $dbi->model('company'); |
|
removed experimental base_ta...
|
2190 | |
update pod
|
2191 |
See L<DBIx::Custom::Model> to know model features. |
2192 | ||
2193 |
=head2 C<method> EXPERIMENTAL |
|
added experimental not_exist...
|
2194 | |
2195 |
$dbi->method( |
|
2196 |
update_or_insert => sub { |
|
2197 |
my $self = shift; |
|
update pod
|
2198 |
|
2199 |
# Process |
|
added experimental not_exist...
|
2200 |
}, |
2201 |
find_or_create => sub { |
|
2202 |
my $self = shift; |
|
update pod
|
2203 |
|
2204 |
# Process |
|
added experimental not_exist...
|
2205 |
} |
2206 |
); |
|
2207 | ||
update pod
|
2208 |
Register method. These method is called directly from L<DBIx::Custom> object. |
added experimental not_exist...
|
2209 | |
2210 |
$dbi->update_or_insert; |
|
2211 |
$dbi->find_or_create; |
|
2212 | ||
update pod
|
2213 |
=head2 C<model> EXPERIMENTAL |
2214 | ||
2215 |
$dbi->model('book')->method( |
|
2216 |
insert => sub { ... }, |
|
2217 |
update => sub { ... } |
|
2218 |
); |
|
2219 |
|
|
2220 |
my $model = $dbi->model('book'); |
|
2221 | ||
2222 |
Set and get a L<DBIx::Custom::Model> object, |
|
2223 | ||
cleanup
|
2224 |
=head2 C<mycolumn> EXPERIMENTAL |
2225 | ||
2226 |
my $column = $self->mycolumn(book => ['author', 'title']); |
|
2227 | ||
2228 |
Create column clause for myself. The follwoing column clause is created. |
|
2229 | ||
2230 |
book.author as author, |
|
2231 |
book.title as title |
|
2232 | ||
added experimental not_exist...
|
2233 |
=head2 C<new> |
2234 | ||
update pod
|
2235 |
my $dbi = DBIx::Custom->new( |
2236 |
data_source => "dbi:mysql:database=dbname", |
|
2237 |
user => 'ken', |
|
2238 |
password => '!LFKD%$&', |
|
2239 |
dbi_option => {mysql_enable_utf8 => 1} |
|
2240 |
); |
|
added experimental not_exist...
|
2241 | |
2242 |
Create a new L<DBIx::Custom> object. |
|
2243 | ||
removed EXPERIMETNAL flag fr...
|
2244 |
=head2 C<not_exists> |
added experimental not_exist...
|
2245 | |
2246 |
my $not_exists = $dbi->not_exists; |
|
2247 | ||
update pod
|
2248 |
DBIx::Custom::NotExists object, indicating the column is not exists. |
2249 |
This is used by C<clause> of L<DBIx::Custom::Where> . |
|
experimental extended select...
|
2250 | |
cleanup
|
2251 |
=head2 C<register_filter> |
2252 | ||
update pod
|
2253 |
$dbi->register_filter( |
2254 |
# Time::Piece object to database DATE format |
|
2255 |
tp_to_date => sub { |
|
2256 |
my $tp = shift; |
|
2257 |
return $tp->strftime('%Y-%m-%d'); |
|
2258 |
}, |
|
2259 |
# database DATE format to Time::Piece object |
|
2260 |
date_to_tp => sub { |
|
2261 |
my $date = shift; |
|
2262 |
return Time::Piece->strptime($date, '%Y-%m-%d'); |
|
2263 |
} |
|
2264 |
); |
|
cleanup
|
2265 |
|
update pod
|
2266 |
Register filters, used by C<filter> option of many methods. |
cleanup
|
2267 | |
update pod
|
2268 |
=head2 C<register_tag> |
cleanup
|
2269 | |
update pod
|
2270 |
$dbi->register_tag( |
2271 |
update => sub { |
|
2272 |
my @columns = @_; |
|
2273 |
|
|
2274 |
# Update parameters |
|
2275 |
my $s = 'set '; |
|
2276 |
$s .= "$_ = ?, " for @columns; |
|
2277 |
$s =~ s/, $//; |
|
2278 |
|
|
2279 |
return [$s, \@columns]; |
|
2280 |
} |
|
2281 |
); |
|
cleanup
|
2282 | |
update pod
|
2283 |
Register tag, used by C<execute()>. |
cleanup
|
2284 | |
update pod
|
2285 |
See also L<Tags/Tags> about tag registered by default. |
cleanup
|
2286 | |
update pod
|
2287 |
Tag parser receive arguments specified in tag. |
2288 |
In the following tag, 'title' and 'author' is parser arguments |
|
cleanup
|
2289 | |
update pod
|
2290 |
{update_param title author} |
cleanup
|
2291 | |
update pod
|
2292 |
Tag parser must return array refrence, |
2293 |
first element is the result statement, |
|
2294 |
second element is column names corresponding to place holders. |
|
cleanup
|
2295 | |
update pod
|
2296 |
In this example, result statement is |
cleanup
|
2297 | |
update pod
|
2298 |
set title = ?, author = ? |
added register_tag_processor
|
2299 | |
update pod
|
2300 |
Column names is |
added register_tag_processor
|
2301 | |
update pod
|
2302 |
['title', 'author'] |
added register_tag_processor
|
2303 | |
removed DBIx::Custom commit ...
|
2304 |
=head2 C<select> |
added select() all_column op...
|
2305 | |
select method column option ...
|
2306 |
my $result = $dbi->select( |
added select() all_column op...
|
2307 |
table => 'book', |
2308 |
column => ['author', 'title'], |
|
2309 |
where => {author => 'Ken'}, |
|
select method column option ...
|
2310 |
); |
added select() all_column op...
|
2311 |
|
update pod
|
2312 |
Select statement. |
added select() all_column op...
|
2313 | |
2314 |
The following opitons are currently available. |
|
2315 | ||
2316 |
=over 4 |
|
2317 | ||
2318 |
=item C<table> |
|
2319 | ||
2320 |
Table name. |
|
2321 | ||
update pod
|
2322 |
$dbi->select(table => 'book'); |
added select() all_column op...
|
2323 | |
2324 |
=item C<column> |
|
2325 | ||
2326 |
Column clause. This is array reference or constant value. |
|
2327 | ||
2328 |
# Hash refernce |
|
2329 |
$dbi->select(column => ['author', 'title']); |
|
2330 |
|
|
2331 |
# Constant value |
|
2332 |
$dbi->select(column => 'author'); |
|
2333 | ||
2334 |
Default is '*' unless C<column> is specified. |
|
2335 | ||
2336 |
# Default |
|
2337 |
$dbi->select(column => '*'); |
|
2338 | ||
- added EXPERIMENTAL DBIx::C...
|
2339 |
You can use hash option in C<column> |
2340 | ||
2341 |
=over 4 |
|
2342 | ||
2343 |
=item all EXPERIMENTAL |
|
added select() all_column op...
|
2344 | |
update pod
|
2345 |
Colum clause, contains all columns of joined table. This is true or false value |
added select() all_column op...
|
2346 | |
- added EXPERIMENTAL DBIx::C...
|
2347 |
$dbi->select(column => {all => 1}); |
added select() all_column op...
|
2348 | |
2349 |
If main table is C<book> and joined table is C<company>, |
|
2350 |
This create the following column clause. |
|
2351 | ||
2352 |
book.author as author |
|
2353 |
book.company_id as company_id |
|
2354 |
company.id as company__id |
|
2355 |
company.name as company__name |
|
2356 | ||
2357 |
Columns of main table is consist of only column name, |
|
2358 |
Columns of joined table is consist of table and column name joined C<__>. |
|
2359 | ||
- added EXPERIMENTAL DBIx::C...
|
2360 |
Note that this option is failed unless modles is included and |
2361 |
C<columns> attribute is set. |
|
update pod
|
2362 | |
2363 |
# Generally do the following way before using all_column option |
|
2364 |
$dbi->include_model('MyModel')->setup_model; |
|
2365 | ||
- added EXPERIMENTAL DBIx::C...
|
2366 |
=item table EXPERIMENTAL |
2367 | ||
2368 |
You can also specify table names by C<table> option |
|
- added EXPERIMENTAL DBIx::C...
|
2369 | |
- added EXPERIMENTAL DBIx::C...
|
2370 |
$dbi->select(column => {table => ['book', 'company']}); |
2371 | ||
2372 |
=item prepend EXPERIMENTAL |
|
2373 | ||
2374 |
You can add before created statement |
|
2375 | ||
2376 |
$dbi->select(column => {prepend => 'SOME', all => 1}); |
|
2377 | ||
2378 |
=back |
|
- added EXPERIMENTAL DBIx::C...
|
2379 | |
added select() all_column op...
|
2380 |
=item C<where> |
2381 | ||
where can recieve array refr...
|
2382 |
Where clause. This is hash reference or L<DBIx::Custom::Where> object, |
2383 |
or array refrence, which contains where clause and paramter. |
|
added select() all_column op...
|
2384 |
|
2385 |
# Hash reference |
|
update pod
|
2386 |
$dbi->select(where => {author => 'Ken', 'title' => 'Perl'}); |
added select() all_column op...
|
2387 |
|
update pod
|
2388 |
# DBIx::Custom::Where object |
added select() all_column op...
|
2389 |
my $where = $dbi->where( |
2390 |
clause => ['and', '{= author}', '{like title}'], |
|
2391 |
param => {author => 'Ken', title => '%Perl%'} |
|
2392 |
); |
|
update pod
|
2393 |
$dbi->select(where => $where); |
added select() all_column op...
|
2394 | |
where can recieve array refr...
|
2395 |
# Array refrendce (where clause and parameter) |
2396 |
$dbi->select(where => |
|
2397 |
[ |
|
2398 |
['and', '{= author}', '{like title}'], |
|
2399 |
{author => 'Ken', title => '%Perl%'} |
|
2400 |
] |
|
2401 |
); |
|
2402 |
|
|
update pod
|
2403 |
=item C<join> EXPERIMENTAL |
added select() all_column op...
|
2404 | |
update pod
|
2405 |
Join clause used in need. This is array reference. |
2406 | ||
2407 |
$dbi->select(join => |
|
2408 |
[ |
|
2409 |
'left outer join company on book.company_id = company_id', |
|
2410 |
'left outer join location on company.location_id = location.id' |
|
2411 |
] |
|
2412 |
); |
|
2413 | ||
2414 |
If column cluase or where clause contain table name like "company.name", |
|
2415 |
needed join clause is used automatically. |
|
2416 | ||
2417 |
$dbi->select( |
|
2418 |
table => 'book', |
|
2419 |
column => ['company.location_id as company__location_id'], |
|
2420 |
where => {'company.name' => 'Orange'}, |
|
2421 |
join => [ |
|
2422 |
'left outer join company on book.company_id = company.id', |
|
2423 |
'left outer join location on company.location_id = location.id' |
|
2424 |
] |
|
2425 |
); |
|
2426 | ||
2427 |
In above select, the following SQL is created. |
|
2428 | ||
2429 |
select company.location_id as company__location_id |
|
2430 |
from book |
|
2431 |
left outer join company on book.company_id = company.id |
|
2432 |
where company.name = Orange |
|
2433 | ||
2434 |
=item C<append> |
|
2435 | ||
update pod
|
2436 |
Append statement to last of SQL. This is string. |
update pod
|
2437 | |
2438 |
$dbi->select(append => 'order by title'); |
|
2439 | ||
2440 |
=item C<filter> |
|
2441 | ||
update pod
|
2442 |
Filter, executed before data is send to database. This is array reference. |
2443 |
Filter value is code reference or |
|
update pod
|
2444 |
filter name registerd by C<register_filter()>. |
2445 | ||
2446 |
# Basic |
|
2447 |
$dbi->select( |
|
2448 |
filter => [ |
|
2449 |
title => sub { uc $_[0] } |
|
2450 |
author => sub { uc $_[0] } |
|
2451 |
] |
|
2452 |
); |
|
2453 |
|
|
2454 |
# At once |
|
2455 |
$dbi->select( |
|
2456 |
filter => [ |
|
2457 |
[qw/title author/] => sub { uc $_[0] } |
|
2458 |
] |
|
2459 |
); |
|
2460 |
|
|
2461 |
# Filter name |
|
2462 |
$dbi->select( |
|
2463 |
filter => [ |
|
2464 |
title => 'upper_case', |
|
2465 |
author => 'upper_case' |
|
2466 |
] |
|
2467 |
); |
|
add experimental selection o...
|
2468 | |
update pod
|
2469 |
These filters are added to the C<out> filters, set by C<apply_filter()>. |
update document
|
2470 | |
removed EXPERIMETNAL flag fr...
|
2471 |
=item C<query> |
cleanup
|
2472 | |
update pod
|
2473 |
Get L<DBIx::Custom::Query> object instead of executing SQL. |
2474 |
This is true or false value. |
|
2475 | ||
update pod
|
2476 |
my $query = $dbi->select(query => 1); |
update pod
|
2477 | |
update pod
|
2478 |
You can check SQL. |
update pod
|
2479 | |
2480 |
my $sql = $query->sql; |
|
2481 | ||
- added EXPERIMENTAL type() ...
|
2482 |
=item C<type> EXPERIMENTAL |
2483 | ||
2484 |
Specify database data type. |
|
2485 | ||
2486 |
$dbi->select(type => [image => DBI::SQL_BLOB]); |
|
2487 |
$dbi->select(type => [[qw/image audio/] => DBI::SQL_BLOB]); |
|
2488 | ||
2489 |
This is used to bind paramter by C<bind_param()> of statment handle. |
|
2490 | ||
2491 |
$sth->bind_param($pos, $value, DBI::SQL_BLOB); |
|
2492 | ||
update pod
|
2493 |
=back |
cleanup
|
2494 | |
removed EXPERIMETNAL flag fr...
|
2495 |
=head2 C<select_at()> |
add experimental update_at()...
|
2496 | |
update pod
|
2497 |
Select statement, using primary key. |
2498 | ||
2499 |
$dbi->select_at( |
|
2500 |
table => 'book', |
|
2501 |
primary_key => 'id', |
|
2502 |
where => '5' |
|
2503 |
); |
|
2504 | ||
update pod
|
2505 |
This method is same as C<select()> exept that |
2506 |
C<primary_key> is specified and C<where> is constant value or array refrence. |
|
update pod
|
2507 |
all option of C<select()> is available. |
add experimental update_at()...
|
2508 | |
update pod
|
2509 |
=over 4 |
2510 | ||
2511 |
=item C<primary_key> |
|
add experimental update_at()...
|
2512 | |
update pod
|
2513 |
Primary key. This is constant value or array reference. |
2514 |
|
|
2515 |
# Constant value |
|
2516 |
$dbi->select(primary_key => 'id'); |
|
2517 | ||
2518 |
# Array reference |
|
2519 |
$dbi->select(primary_key => ['id1', 'id2' ]); |
|
2520 | ||
update pod
|
2521 |
This is used to create where clause. |
2522 | ||
update pod
|
2523 |
=item C<where> |
update pod
|
2524 | |
update pod
|
2525 |
Where clause, created from primary key information. |
update pod
|
2526 |
This is constant value or array reference. |
2527 | ||
2528 |
# Constant value |
|
2529 |
$dbi->select(where => 5); |
|
2530 | ||
2531 |
# Array reference |
|
2532 |
$dbi->select(where => [3, 5]); |
|
2533 | ||
2534 |
In first examle, the following SQL is created. |
|
2535 | ||
2536 |
select * from book where id = ? |
|
2537 | ||
2538 |
Place holder is set to 5. |
|
add experimental update_at()...
|
2539 | |
update pod
|
2540 |
=back |
2541 | ||
cleanup
|
2542 |
=head2 C<update> |
removed reconnect method
|
2543 | |
update pod
|
2544 |
$dbi->update( |
2545 |
table => 'book', |
|
2546 |
param => {title => 'Perl'}, |
|
2547 |
where => {id => 4} |
|
2548 |
); |
|
removed reconnect method
|
2549 | |
update pod
|
2550 |
Update statement. |
added experimental update_pa...
|
2551 | |
update pod
|
2552 |
The following opitons are currently available. |
added experimental update_pa...
|
2553 | |
update pod
|
2554 |
=over 4 |
2555 | ||
update pod
|
2556 |
=item C<table> |
2557 | ||
update pod
|
2558 |
Table name. |
2559 | ||
2560 |
$dbi->update(table => 'book'); |
|
2561 | ||
2562 |
=item C<param> |
|
2563 | ||
2564 |
Update data. This is hash reference. |
|
2565 | ||
2566 |
$dbi->update(param => {title => 'Perl'}); |
|
2567 | ||
2568 |
=item C<where> |
|
2569 | ||
where can recieve array refr...
|
2570 |
Where clause. This is hash reference or L<DBIx::Custom::Where> object |
2571 |
or array refrence. |
|
update pod
|
2572 |
|
2573 |
# Hash reference |
|
2574 |
$dbi->update(where => {author => 'Ken', 'title' => 'Perl'}); |
|
2575 |
|
|
2576 |
# DBIx::Custom::Where object |
|
2577 |
my $where = $dbi->where( |
|
2578 |
clause => ['and', '{= author}', '{like title}'], |
|
2579 |
param => {author => 'Ken', title => '%Perl%'} |
|
2580 |
); |
|
2581 |
$dbi->update(where => $where); |
|
where can recieve array refr...
|
2582 |
|
2583 |
# Array refrendce (where clause and parameter) |
|
2584 |
$dbi->update(where => |
|
2585 |
[ |
|
2586 |
['and', '{= author}', '{like title}'], |
|
2587 |
{author => 'Ken', title => '%Perl%'} |
|
2588 |
] |
|
2589 |
); |
|
update pod
|
2590 | |
2591 |
=item C<append> |
|
2592 | ||
2593 |
Append statement to last of SQL. This is string. |
|
2594 | ||
2595 |
$dbi->update(append => 'order by title'); |
|
2596 | ||
2597 |
=item C<filter> |
|
2598 | ||
2599 |
Filter, executed before data is send to database. This is array reference. |
|
2600 |
Filter value is code reference or |
|
2601 |
filter name registerd by C<register_filter()>. |
|
2602 | ||
2603 |
# Basic |
|
2604 |
$dbi->update( |
|
2605 |
filter => [ |
|
2606 |
title => sub { uc $_[0] } |
|
2607 |
author => sub { uc $_[0] } |
|
2608 |
] |
|
2609 |
); |
|
2610 |
|
|
2611 |
# At once |
|
2612 |
$dbi->update( |
|
2613 |
filter => [ |
|
2614 |
[qw/title author/] => sub { uc $_[0] } |
|
2615 |
] |
|
2616 |
); |
|
2617 |
|
|
2618 |
# Filter name |
|
2619 |
$dbi->update( |
|
2620 |
filter => [ |
|
2621 |
title => 'upper_case', |
|
2622 |
author => 'upper_case' |
|
2623 |
] |
|
2624 |
); |
|
added experimental update_pa...
|
2625 | |
update pod
|
2626 |
These filters are added to the C<out> filters, set by C<apply_filter()>. |
added experimental update_pa...
|
2627 | |
removed EXPERIMETNAL flag fr...
|
2628 |
=item C<query> |
add experimental setup_model...
|
2629 | |
update pod
|
2630 |
Get L<DBIx::Custom::Query> object instead of executing SQL. |
2631 |
This is true or false value. |
|
add experimental setup_model...
|
2632 | |
update pod
|
2633 |
my $query = $dbi->update(query => 1); |
2634 | ||
2635 |
You can check SQL. |
|
2636 | ||
2637 |
my $sql = $query->sql; |
|
add experimental setup_model...
|
2638 | |
update pod
|
2639 |
=back |
2640 | ||
cleanup
|
2641 |
=head2 C<update_all> |
renamed build_query to creat...
|
2642 | |
update pod
|
2643 |
$dbi->update_all(table => 'book', param => {title => 'Perl'}); |
renamed build_query to creat...
|
2644 | |
update pod
|
2645 |
Update statement to update all rows. |
2646 |
Options is same as C<update()>. |
|
removed DBIx::Custom commit ...
|
2647 | |
removed EXPERIMETNAL flag fr...
|
2648 |
=head2 C<update_at()> |
add experimental update_at()...
|
2649 | |
update pod
|
2650 |
Update statement, using primary key. |
add experimental update_at()...
|
2651 | |
2652 |
$dbi->update_at( |
|
2653 |
table => 'book', |
|
update pod
|
2654 |
primary_key => 'id', |
2655 |
where => '5', |
|
2656 |
param => {title => 'Perl'} |
|
add experimental update_at()...
|
2657 |
); |
2658 | ||
update pod
|
2659 |
This method is same as C<update()> exept that |
2660 |
C<primary_key> is specified and C<where> is constant value or array refrence. |
|
2661 |
all option of C<update()> is available. |
|
2662 | ||
update pod
|
2663 |
=over 4 |
2664 | ||
2665 |
=item C<primary_key> |
|
update pod
|
2666 | |
2667 |
Primary key. This is constant value or array reference. |
|
2668 |
|
|
2669 |
# Constant value |
|
2670 |
$dbi->update(primary_key => 'id'); |
|
2671 | ||
2672 |
# Array reference |
|
2673 |
$dbi->update(primary_key => ['id1', 'id2' ]); |
|
2674 | ||
2675 |
This is used to create where clause. |
|
2676 | ||
update pod
|
2677 |
=item C<where> |
update pod
|
2678 | |
2679 |
Where clause, created from primary key information. |
|
2680 |
This is constant value or array reference. |
|
2681 | ||
2682 |
# Constant value |
|
2683 |
$dbi->update(where => 5); |
|
2684 | ||
2685 |
# Array reference |
|
2686 |
$dbi->update(where => [3, 5]); |
|
2687 | ||
2688 |
In first examle, the following SQL is created. |
|
2689 | ||
2690 |
update book set title = ? where id = ? |
|
2691 | ||
2692 |
Place holders are set to 'Perl' and 5. |
|
2693 | ||
update pod
|
2694 |
=back |
2695 | ||
removed EXPERIMETNAL flag fr...
|
2696 |
=head2 C<update_param> |
update pod
|
2697 | |
2698 |
my $update_param = $dbi->update_param({title => 'a', age => 2}); |
|
2699 | ||
2700 |
Create update parameter tag. |
|
2701 | ||
2702 |
{update_param title age} |
|
add experimental update_at()...
|
2703 | |
removed EXPERIMETNAL flag fr...
|
2704 |
=head2 C<where> |
fix tests
|
2705 | |
cleanup
|
2706 |
my $where = $dbi->where( |
2707 |
clause => ['and', '{= title}', '{= author}'], |
|
2708 |
param => {title => 'Perl', author => 'Ken'} |
|
2709 |
); |
|
fix tests
|
2710 | |
2711 |
Create a new L<DBIx::Custom::Where> object. |
|
2712 | ||
update pod
|
2713 |
=head2 C<setup_model> EXPERIMENTAL |
cleanup
|
2714 | |
update pod
|
2715 |
$dbi->setup_model; |
cleanup
|
2716 | |
update pod
|
2717 |
Setup all model objects. |
update pod
|
2718 |
C<columns> of model object is automatically set, parsing database information. |
cleanup
|
2719 | |
cleanup
|
2720 |
=head1 Tags |
2721 | ||
2722 |
The following tags is available. |
|
2723 | ||
removed EXPERIMETNAL flag fr...
|
2724 |
=head2 C<table> |
add table tag
|
2725 | |
2726 |
Table tag |
|
2727 | ||
2728 |
{table TABLE} -> TABLE |
|
2729 | ||
update pod
|
2730 |
This is used to tell C<execute()> what table is needed . |
add table tag
|
2731 | |
cleanup
|
2732 |
=head2 C<?> |
2733 | ||
2734 |
Placeholder tag. |
|
2735 | ||
2736 |
{? NAME} -> ? |
|
2737 | ||
2738 |
=head2 C<=> |
|
2739 | ||
2740 |
Equal tag. |
|
2741 | ||
2742 |
{= NAME} -> NAME = ? |
|
2743 | ||
2744 |
=head2 C<E<lt>E<gt>> |
|
2745 | ||
2746 |
Not equal tag. |
|
2747 | ||
2748 |
{<> NAME} -> NAME <> ? |
|
2749 | ||
2750 |
=head2 C<E<lt>> |
|
2751 | ||
2752 |
Lower than tag |
|
2753 | ||
2754 |
{< NAME} -> NAME < ? |
|
2755 | ||
2756 |
=head2 C<E<gt>> |
|
2757 | ||
2758 |
Greater than tag |
|
2759 | ||
2760 |
{> NAME} -> NAME > ? |
|
2761 | ||
2762 |
=head2 C<E<gt>=> |
|
2763 | ||
2764 |
Greater than or equal tag |
|
2765 | ||
2766 |
{>= NAME} -> NAME >= ? |
|
2767 | ||
2768 |
=head2 C<E<lt>=> |
|
2769 | ||
2770 |
Lower than or equal tag |
|
2771 | ||
2772 |
{<= NAME} -> NAME <= ? |
|
2773 | ||
2774 |
=head2 C<like> |
|
2775 | ||
2776 |
Like tag |
|
2777 | ||
2778 |
{like NAME} -> NAME like ? |
|
2779 | ||
2780 |
=head2 C<in> |
|
2781 | ||
2782 |
In tag. |
|
2783 | ||
2784 |
{in NAME COUNT} -> NAME in [?, ?, ..] |
|
2785 | ||
2786 |
=head2 C<insert_param> |
|
2787 | ||
2788 |
Insert parameter tag. |
|
2789 | ||
2790 |
{insert_param NAME1 NAME2} -> (NAME1, NAME2) values (?, ?) |
|
2791 | ||
2792 |
=head2 C<update_param> |
|
2793 | ||
2794 |
Updata parameter tag. |
|
2795 | ||
2796 |
{update_param NAME1 NAME2} -> set NAME1 = ?, NAME2 = ? |
|
2797 | ||
DBIx::Custom is now stable
|
2798 |
=head1 STABILITY |
2799 | ||
cleanup
|
2800 |
L<DBIx::Custom> is stable. APIs keep backword compatible |
added select() all_column op...
|
2801 |
except EXPERIMENTAL one in the feature. |
DBIx::Custom is now stable
|
2802 | |
removed DESTROY method(not b...
|
2803 |
=head1 BUGS |
2804 | ||
renamed build_query to creat...
|
2805 |
Please tell me bugs if found. |
removed DESTROY method(not b...
|
2806 | |
2807 |
C<< <kimoto.yuki at gmail.com> >> |
|
2808 | ||
2809 |
L<http://github.com/yuki-kimoto/DBIx-Custom> |
|
2810 | ||
removed reconnect method
|
2811 |
=head1 AUTHOR |
2812 | ||
2813 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
|
version 0.0901
|
2814 | |
packaging one directory
|
2815 |
=head1 COPYRIGHT & LICENSE |
2816 | ||
cleanup
|
2817 |
Copyright 2009-2011 Yuki Kimoto, all rights reserved. |
packaging one directory
|
2818 | |
2819 |
This program is free software; you can redistribute it and/or modify it |
|
2820 |
under the same terms as Perl itself. |
|
2821 | ||
2822 |
=cut |
|
added cache_method attribute
|
2823 | |
2824 |