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