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