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