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