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