cleanup
|
1 |
package DBIx::Custom; |
added EXPERIMENTAL insert, u...
|
2 |
use Object::Simple -base; |
cleanup
|
3 | |
- fixed bug that DBIx::Custo...
|
4 |
our $VERSION = '0.2102'; |
fixed DBIx::Custom::QueryBui...
|
5 |
use 5.008001; |
cleanup
|
6 | |
packaging one directory
|
7 |
use Carp 'croak'; |
8 |
use DBI; |
|
9 |
use DBIx::Custom::Result; |
|
cleanup
|
10 |
use DBIx::Custom::Query; |
cleanup
|
11 |
use DBIx::Custom::QueryBuilder; |
added experimental DBIx::Cus...
|
12 |
use DBIx::Custom::Where; |
add feture. all model class ...
|
13 |
use DBIx::Custom::Model; |
cleanup
|
14 |
use DBIx::Custom::Tag; |
- added EXPERIMENTAL order m...
|
15 |
use DBIx::Custom::Order; |
cleanup
|
16 |
use DBIx::Custom::Util qw/_array_to_hash _subname/; |
added tests
|
17 |
use DBIx::Custom::Mapper; |
- added EXPERIMENTAL pass at...
|
18 |
use DBIx::Custom::NotExists; |
improved debug message
|
19 |
use Encode qw/encode encode_utf8 decode_utf8/; |
cleanup
|
20 |
use Scalar::Util qw/weaken/; |
added experimental use_next_...
|
21 | |
packaging one directory
|
22 | |
- added EXPERIMENTAL get_col...
|
23 |
has [qw/connector dsn password quote user exclude_table user_table_info |
cleanup
|
24 |
user_column_info safety_character/], |
removed from cache() and cac...
|
25 |
cache => 0, |
many changed
|
26 |
cache_method => sub { |
27 |
sub { |
|
28 |
my $self = shift; |
|
29 |
|
|
30 |
$self->{_cached} ||= {}; |
|
31 |
|
|
32 |
if (@_ > 1) { |
|
update pod
|
33 |
$self->{_cached}{$_[0]} = $_[1]; |
many changed
|
34 |
} |
35 |
else { |
|
update pod
|
36 |
return $self->{_cached}{$_[0]}; |
many changed
|
37 |
} |
38 |
} |
|
update pod
|
39 |
}, |
- dbi_option attribute is re...
|
40 |
option => sub { {} }, |
41 |
default_option => sub { |
|
update pod
|
42 |
{ |
43 |
RaiseError => 1, |
|
44 |
PrintError => 0, |
|
45 |
AutoCommit => 1 |
|
46 |
} |
|
47 |
}, |
|
fix tests
|
48 |
filters => sub { |
49 |
{ |
|
50 |
encode_utf8 => sub { encode_utf8($_[0]) }, |
|
51 |
decode_utf8 => sub { decode_utf8($_[0]) } |
|
52 |
} |
|
update pod
|
53 |
}, |
added EXPERIMENTAL last_sql ...
|
54 |
last_sql => '', |
update pod
|
55 |
models => sub { {} }, |
- insert timestamp option is...
|
56 |
now => sub { |
57 |
sub { |
|
58 |
my ($sec, $min, $hour, $mday, $mon, $year) = localtime; |
|
59 |
$mon++; |
|
60 |
$year += 1900; |
|
added EXPERIMENTAL insert cr...
|
61 |
my $now = sprintf("%04d-%02d-%02d %02d:%02d:%02d", |
62 |
$year, $mon, $mday, $hour, $min, $sec); |
|
63 |
return $now; |
|
- insert timestamp option is...
|
64 |
} |
65 |
}, |
|
cleanup
|
66 |
query_builder => sub { |
67 |
my $self = shift; |
|
68 |
my $builder = DBIx::Custom::QueryBuilder->new(dbi => $self); |
|
69 |
weaken $builder->{dbi}; |
|
70 |
return $builder; |
|
71 |
}, |
|
update pod
|
72 |
result_class => 'DBIx::Custom::Result', |
cleanup test
|
73 |
separator => '.', |
- DBIx::Custom::QueryBuilder...
|
74 |
stash => sub { {} }; |
cleanup
|
75 | |
added EXPERIMENTAL system_ta...
|
76 |
sub available_datatype { |
test cleanup
|
77 |
my $self = shift; |
78 |
|
|
added EXPERIMENTAL system_ta...
|
79 |
my $data_types = ''; |
cleanup
|
80 |
for my $i (-1000 .. 1000) { |
added EXPERIMENTAL system_ta...
|
81 |
my $type_info = $self->dbh->type_info($i); |
82 |
my $data_type = $type_info->{DATA_TYPE}; |
|
83 |
my $type_name = $type_info->{TYPE_NAME}; |
|
84 |
$data_types .= "$data_type ($type_name)\n" |
|
85 |
if defined $data_type; |
|
86 |
} |
|
87 |
return "Data Type maybe equal to Type Name" unless $data_types; |
|
88 |
$data_types = "Data Type (Type name)\n" . $data_types; |
|
89 |
return $data_types; |
|
90 |
} |
|
91 | ||
92 |
sub available_typename { |
|
93 |
my $self = shift; |
|
94 |
|
|
95 |
# Type Names |
|
96 |
my $type_names = {}; |
|
97 |
$self->each_column(sub { |
|
98 |
my ($self, $table, $column, $column_info) = @_; |
|
99 |
$type_names->{$column_info->{TYPE_NAME}} = 1 |
|
100 |
if $column_info->{TYPE_NAME}; |
|
101 |
}); |
|
102 |
my @output = sort keys %$type_names; |
|
103 |
unshift @output, "Type Name"; |
|
104 |
return join "\n", @output; |
|
test cleanup
|
105 |
} |
106 | ||
added helper method
|
107 |
our $AUTOLOAD; |
108 |
sub AUTOLOAD { |
|
109 |
my $self = shift; |
|
110 | ||
renamed helper to method.
|
111 |
# Method name |
112 |
my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/; |
|
added helper method
|
113 | |
cleanup
|
114 |
# Call method |
renamed helper to method.
|
115 |
$self->{_methods} ||= {}; |
add feture. all model class ...
|
116 |
if (my $method = $self->{_methods}->{$mname}) { |
117 |
return $self->$method(@_) |
|
118 |
} |
|
- removed EXPERIMENTAL Prefo...
|
119 |
elsif ($self->{dbh} && (my $dbh_method = $self->dbh->can($mname))) { |
- added EXPERIMENTAL DBIx::C...
|
120 |
$self->dbh->$dbh_method(@_); |
add feture. all model class ...
|
121 |
} |
122 |
else { |
|
cleanup
|
123 |
croak qq{Can't locate object method "$mname" via "$package" } |
124 |
. _subname; |
|
add feture. all model class ...
|
125 |
} |
added helper method
|
126 |
} |
127 | ||
- update_param is DEPRECATED...
|
128 |
sub assign_clause { |
updated pod
|
129 |
my ($self, $param, $opts) = @_; |
130 |
|
|
131 |
my $wrap = $opts->{wrap} || {}; |
|
micro optimization
|
132 |
my ($q, $p) = split //, $self->q(''); |
added EXPERIMENTAL assign_ta...
|
133 |
|
micro optimization
|
134 |
# Assign clause (performance is important) |
micro optimization
|
135 |
join( |
136 |
', ', |
|
137 |
map { |
|
138 |
ref $param->{$_} eq 'SCALAR' ? "$q$_$p = " . ${$param->{$_}} |
|
139 |
: $wrap->{$_} ? "$q$_$p = " . $wrap->{$_}->(":$_") |
|
140 |
: "$q$_$p = :$_"; |
|
141 |
} sort keys %$param |
|
142 |
); |
|
added EXPERIMENTAL assign_ta...
|
143 |
} |
144 | ||
cleanup
|
145 |
sub column { |
- DBIx::Custom Model filter ...
|
146 |
my $self = shift; |
147 |
my $option = pop if ref $_[-1] eq 'HASH'; |
|
148 |
my $real_table = shift; |
|
149 |
my $columns = shift; |
|
150 |
my $table = $option->{alias} || $real_table; |
|
151 |
|
|
152 |
# Columns |
|
added EXPERIMENTAL DBIx::Cus...
|
153 |
unless (defined $columns) { |
- DBIx::Custom Model filter ...
|
154 |
$columns ||= $self->model($real_table)->columns; |
155 |
} |
|
added helper method
|
156 |
|
EXPERIMTANL column method th...
|
157 |
# Separator |
158 |
my $separator = $self->separator; |
|
159 |
|
|
cleanup
|
160 |
# Column clause |
cleanup
|
161 |
my @column; |
cleanup
|
162 |
$columns ||= []; |
added EXPERIMENTAL q method
|
163 |
push @column, $self->q($table) . "." . $self->q($_) . |
164 |
" as " . $self->q("${table}${separator}$_") |
|
EXPERIMTANL column method th...
|
165 |
for @$columns; |
cleanup
|
166 |
|
167 |
return join (', ', @column); |
|
added helper method
|
168 |
} |
169 | ||
packaging one directory
|
170 |
sub connect { |
cleanup
|
171 |
my $self = ref $_[0] ? shift : shift->new(@_); |
172 |
|
|
173 |
my $connector = $self->connector; |
|
174 |
|
|
175 |
if (!ref $connector && $connector) { |
|
176 |
require DBIx::Connector; |
|
177 |
|
|
178 |
my $dsn = $self->dsn; |
|
179 |
my $user = $self->user; |
|
180 |
my $password = $self->password; |
|
- dbi_option attribute is re...
|
181 |
my $option = $self->_option; |
cleanup
|
182 |
my $connector = DBIx::Connector->new($dsn, $user, $password, |
- dbi_option attribute is re...
|
183 |
{%{$self->default_option} , %$option}); |
cleanup
|
184 |
$self->connector($connector); |
185 |
} |
|
removed register_format()
|
186 |
|
- removed EXPERIMENTAL Prefo...
|
187 |
# Connect |
188 |
$self->dbh; |
|
update document
|
189 |
|
packaging one directory
|
190 |
return $self; |
191 |
} |
|
192 | ||
- added EXPERIMENTAL DBIx::C...
|
193 |
sub count { shift->select(column => 'count(*)', @_)->fetch_first->[0] } |
194 | ||
update pod
|
195 |
sub dbh { |
196 |
my $self = shift; |
|
cleanup
|
197 |
|
fixed dbh() method bug:wq
|
198 |
# Set |
199 |
if (@_) { |
|
200 |
$self->{dbh} = $_[0]; |
|
201 |
|
|
202 |
return $self; |
|
203 |
} |
|
204 |
|
|
205 |
# Get |
|
206 |
else { |
|
207 |
# From Connction manager |
|
208 |
if (my $connector = $self->connector) { |
|
cleanup
|
209 |
croak "connector must have dbh() method " . _subname |
fixed dbh() method bug:wq
|
210 |
unless ref $connector && $connector->can('dbh'); |
211 |
|
|
set reserved_word_quote auto...
|
212 |
$self->{dbh} = $connector->dbh; |
fixed dbh() method bug:wq
|
213 |
} |
214 |
|
|
set reserved_word_quote auto...
|
215 |
# Connect |
216 |
$self->{dbh} ||= $self->_connect; |
|
217 |
|
|
218 |
# Quote |
|
reserved_word_quote is DEPRE...
|
219 |
if (!defined $self->reserved_word_quote && !defined $self->quote) { |
prepare oracle test
|
220 |
my $driver = $self->_driver; |
added EXPERIMENTAL find_tabl...
|
221 |
my $quote = $driver eq 'odbc' ? '[]' |
222 |
: $driver eq 'ado' ? '[]' |
|
223 |
: $driver eq 'mysql' ? '`' |
|
fixex [] reserved_word_quote...
|
224 |
: '"'; |
reserved_word_quote is DEPRE...
|
225 |
$self->quote($quote); |
set reserved_word_quote auto...
|
226 |
} |
reserved_word_quote is DEPRE...
|
227 |
|
set reserved_word_quote auto...
|
228 |
return $self->{dbh}; |
update pod
|
229 |
} |
230 |
} |
|
231 | ||
cleanup
|
232 |
sub delete { |
233 |
my ($self, %opt) = @_; |
|
234 |
warn "delete method where_param option is DEPRECATED!" |
|
235 |
if $opt{where_param}; |
|
236 |
|
|
cleanup
|
237 |
# Don't allow delete all rows |
cleanup
|
238 |
croak qq{delete method where or id option must be specified } . _subname |
239 |
if !$opt{where} && !defined $opt{id} && !$opt{allow_delete_all}; |
|
240 |
|
|
241 |
# Where |
|
cleanup
|
242 |
my $w = $self->_where_clause_and_param($opt{where}, $opt{where_param}, |
cleanup
|
243 |
delete $opt{id}, $opt{primary_key}, $opt{table}); |
make delete() using where ob...
|
244 | |
cleanup
|
245 |
# Delete statement |
cleanup
|
246 |
my $sql = "delete "; |
cleanup
|
247 |
$sql .= "$opt{prefix} " if defined $opt{prefix}; |
added EXPERIMENTAL q method
|
248 |
$sql .= "from " . $self->q($opt{table}) . " $w->{clause} "; |
packaging one directory
|
249 |
|
250 |
# Execute query |
|
- insert method id value is ...
|
251 |
$opt{statement} = 'delete'; |
cleanup
|
252 |
$self->execute($sql, $w->{param}, %opt); |
packaging one directory
|
253 |
} |
254 | ||
cleanup
|
255 |
sub delete_all { shift->delete(@_, allow_delete_all => 1) } |
packaging one directory
|
256 | |
added memory leak check test
|
257 |
sub DESTROY {} |
added helper method
|
258 | |
removed EXPERIMETNAL flag fr...
|
259 |
sub create_model { |
260 |
my $self = shift; |
|
261 |
|
|
cleanup
|
262 |
# Options |
cleanup
|
263 |
my $opt = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
264 |
$opt->{dbi} = $self; |
|
265 |
my $model_class = delete $opt->{model_class} || 'DBIx::Custom::Model'; |
|
266 |
my $model_name = delete $opt->{name}; |
|
267 |
my $model_table = delete $opt->{table}; |
|
removed EXPERIMETNAL flag fr...
|
268 |
$model_name ||= $model_table; |
269 |
|
|
cleanup
|
270 |
# Create model |
cleanup
|
271 |
my $model = $model_class->new($opt); |
cleanup
|
272 |
weaken $model->{dbi}; |
removed EXPERIMETNAL flag fr...
|
273 |
$model->name($model_name) unless $model->name; |
274 |
$model->table($model_table) unless $model->table; |
|
275 |
|
|
micro optimization
|
276 |
# Apply filter(DEPRECATED logic) |
277 |
if ($model->{filter}) { |
|
278 |
my $filter = ref $model->filter eq 'HASH' |
|
279 |
? [%{$model->filter}] |
|
280 |
: $model->filter; |
|
281 |
$filter ||= []; |
|
282 |
warn "DBIx::Custom::Model filter method is DEPRECATED!" |
|
283 |
if @$filter; |
|
284 |
$self->_apply_filter($model->table, @$filter); |
|
285 |
} |
|
286 |
|
|
removed EXPERIMETNAL flag fr...
|
287 |
# Set model |
288 |
$self->model($model->name, $model); |
|
289 |
|
|
create_model() return model
|
290 |
return $self->model($model->name); |
removed EXPERIMETNAL flag fr...
|
291 |
} |
292 | ||
293 |
sub each_column { |
|
- added EXPERIMENTAL get_col...
|
294 |
my ($self, $cb, %options) = @_; |
added EXPERIMENTAL system_ta...
|
295 | |
- added EXPERIMENTAL get_col...
|
296 |
my $user_column_info = $self->user_column_info; |
removed EXPERIMETNAL flag fr...
|
297 |
|
- added EXPERIMENTAL get_col...
|
298 |
if ($user_column_info) { |
299 |
$self->$cb($_->{table}, $_->{column}, $_->{info}) for @$user_column_info; |
|
300 |
} |
|
301 |
else { |
|
302 |
|
|
303 |
my $re = $self->exclude_table || $options{exclude_table}; |
|
304 |
# Tables |
|
305 |
my %tables; |
|
306 |
$self->each_table(sub { $tables{$_[1]}++ }); |
|
added SQL Server test
|
307 | |
- added EXPERIMENTAL get_col...
|
308 |
# Iterate all tables |
309 |
my @tables = sort keys %tables; |
|
310 |
for (my $i = 0; $i < @tables; $i++) { |
|
311 |
my $table = $tables[$i]; |
|
312 |
|
|
313 |
# Iterate all columns |
|
314 |
my $sth_columns; |
|
315 |
eval {$sth_columns = $self->dbh->column_info(undef, undef, $table, '%')}; |
|
316 |
next if $@; |
|
317 |
while (my $column_info = $sth_columns->fetchrow_hashref) { |
|
318 |
my $column = $column_info->{COLUMN_NAME}; |
|
319 |
$self->$cb($table, $column, $column_info); |
|
320 |
} |
|
removed EXPERIMETNAL flag fr...
|
321 |
} |
322 |
} |
|
323 |
} |
|
324 | ||
added EXPERIMENTAL each_tabl...
|
325 |
sub each_table { |
added EXPERIMENTAL find_tabl...
|
326 |
my ($self, $cb, %option) = @_; |
added EXPERIMENTAL each_tabl...
|
327 |
|
cleanup test
|
328 |
my $user_table_infos = $self->user_table_info; |
added EXPERIMENTAL system_ta...
|
329 |
|
added test
|
330 |
# Iterate tables |
331 |
if ($user_table_infos) { |
|
332 |
$self->$cb($_->{table}, $_->{info}) for @$user_table_infos; |
|
333 |
} |
|
334 |
else { |
|
335 |
my $re = $self->exclude_table || $option{exclude}; |
|
336 |
my $sth_tables = $self->dbh->table_info; |
|
337 |
while (my $table_info = $sth_tables->fetchrow_hashref) { |
|
338 |
|
|
339 |
# Table |
|
340 |
my $table = $table_info->{TABLE_NAME}; |
|
341 |
next if defined $re && $table =~ /$re/; |
|
342 |
$self->$cb($table, $table_info); |
|
343 |
} |
|
added EXPERIMENTAL each_tabl...
|
344 |
} |
345 |
} |
|
346 | ||
cleanup
|
347 |
sub execute { |
execute method can second ar...
|
348 |
my $self = shift; |
cleanup
|
349 |
my $sql = shift; |
moved DBIx::Custom::Guide to...
|
350 | |
351 |
# Options |
|
execute method can second ar...
|
352 |
my $param; |
353 |
$param = shift if @_ % 2; |
|
cleanup
|
354 |
my %opt = @_; |
moved DBIx::Custom::Guide to...
|
355 |
warn "sqlfilter option is DEPRECATED" if $opt{sqlfilter}; |
356 |
$param ||= $opt{param} || {}; |
|
cleanup
|
357 |
my $tables = $opt{table} || []; |
cleanup
|
358 |
$tables = [$tables] unless ref $tables eq 'ARRAY'; |
micro optimization
|
359 |
my $filter = ref $opt{filter} eq 'ARRAY' ? |
360 |
_array_to_hash($opt{filter}) : $opt{filter}; |
|
refactoring delete and delet...
|
361 |
|
micro optimization and
|
362 |
# Merge second parameter |
363 |
my @cleanup; |
|
added EXPERIMENTAL insert cr...
|
364 |
my $saved_param; |
cleanup
|
365 |
if (($opt{statement} || '') ne 'insert' && ref $param eq 'ARRAY') { |
micro optimization and
|
366 |
my $param2 = $param->[1]; |
367 |
$param = $param->[0]; |
|
368 |
for my $column (keys %$param2) { |
|
369 |
if (!exists $param->{$column}) { |
|
370 |
$param->{$column} = $param2->{$column}; |
|
371 |
push @cleanup, $column; |
|
372 |
} |
|
373 |
else { |
|
374 |
delete $param->{$_} for @cleanup; |
|
375 |
@cleanup = (); |
|
added EXPERIMENTAL insert cr...
|
376 |
$saved_param = $param; |
micro optimization and
|
377 |
$param = $self->merge_param($param, $param2); |
added EXPERIMENTAL insert cr...
|
378 |
delete $saved_param->{$_} for (@{$opt{cleanup} || []}); |
micro optimization and
|
379 |
last; |
380 |
} |
|
381 |
} |
|
382 |
} |
|
cleanup
|
383 |
$param = [$param] unless ref $param eq 'ARRAY'; |
micro optimization and
|
384 |
|
moved DBIx::Custom::Guide to...
|
385 |
# Append |
386 |
$sql .= $opt{append} if defined $opt{append} && !ref $sql; |
|
387 |
|
|
388 |
# Query |
|
added EXPERIMENTAL reuse_que...
|
389 |
my $query; |
micro optimization and
|
390 |
if (ref $sql) { |
391 |
$query = $sql; |
|
392 |
warn "execute method receiving query object as first parameter is DEPRECATED!" . |
|
393 |
"because this is very buggy."; |
|
394 |
} |
|
added EXPERIMENTAL reuse_que...
|
395 |
else { |
396 |
$query = $opt{reuse}->{$sql} if $opt{reuse}; |
|
micro optimization
|
397 |
unless ($query) { |
cleanup
|
398 |
my $c = $self->{safety_character}; |
micro optimization
|
399 |
# Check unsafety keys |
cleanup
|
400 |
unless ((join('', keys %{$param->[0]}) || '') =~ /^[$c\.]+$/) { |
401 |
for my $column (keys %{$param->[0]}) { |
|
micro optimization
|
402 |
croak qq{"$column" is not safety column name } . _subname |
cleanup
|
403 |
unless $column =~ /^[$c\.]+$/; |
micro optimization
|
404 |
} |
405 |
} |
|
406 |
$query = $self->_create_query($sql,$opt{after_build_sql} || $opt{sqlfilter}); |
|
407 |
} |
|
micro optimization
|
408 |
$query->{statement} = $opt{statement} || ''; |
added EXPERIMENTAL reuse_que...
|
409 |
$opt{reuse}->{$sql} = $query if $opt{reuse}; |
410 |
} |
|
411 |
|
|
added EXPERIMENTAL last_sql ...
|
412 |
# Save query |
micro optimization
|
413 |
$self->{last_sql} = $query->{sql}; |
added EXPERIMENTAL last_sql ...
|
414 | |
moved DBIx::Custom::Guide to...
|
415 |
# Return query |
cleanup
|
416 |
if ($opt{query}) { |
cleanup
|
417 |
for my $column (@cleanup, @{$opt{cleanup} || []}) { |
418 |
delete $_->{$column} for @$param; |
|
419 |
} |
|
cleanup
|
420 |
return $query; |
421 |
}; |
|
micro optimization
|
422 |
|
moved DBIx::Custom::Guide to...
|
423 |
# Merge query filter(DEPRECATED!) |
DBIx::Custom::Query filter m...
|
424 |
$filter ||= $query->{filter} || {}; |
all filter can receive array...
|
425 |
|
cleanup
|
426 |
# Tables |
DBIx::Custom::Query tables a...
|
427 |
unshift @$tables, @{$query->{tables} || []}; |
micro optimization
|
428 |
my $main_table = @{$tables}[-1]; |
- insert method id value is ...
|
429 | |
430 |
# Merge id to parameter |
|
moved DBIx::Custom::Guide to...
|
431 |
if (defined $opt{id}) { |
micro optimization
|
432 |
my $statement = $query->{statement}; |
execute method id option is ...
|
433 |
warn "execute method id option is DEPRECATED!" unless $statement; |
- insert method id value is ...
|
434 |
croak "execute id option must be specified with primary_key option" |
435 |
unless $opt{primary_key}; |
|
436 |
$opt{primary_key} = [$opt{primary_key}] unless ref $opt{primary_key}; |
|
437 |
$opt{id} = [$opt{id}] unless ref $opt{id}; |
|
DBIx::Custom::Query is DEPRE...
|
438 |
for (my $i = 0; $i < @{$opt{id}}; $i++) { |
- insert method id value is ...
|
439 |
my $key = $opt{primary_key}->[$i]; |
440 |
$key = "$main_table.$key" if $statement eq 'update' || |
|
441 |
$statement eq 'delete' || $statement eq 'select'; |
|
cleanup
|
442 |
next if exists $param->[0]->{$key}; |
443 |
$param->[0]->{$key} = $opt{id}->[$i]; |
|
micro optimization and
|
444 |
push @cleanup, $key;1 |
- insert method id value is ...
|
445 |
} |
fixed id option bug when col...
|
446 |
} |
micro optimization
|
447 |
|
moved DBIx::Custom::Guide to...
|
448 |
# Cleanup tables(DEPRECATED!) |
micro optimization
|
449 |
$tables = $self->_remove_duplicate_table($tables, $main_table) |
450 |
if @$tables > 1; |
|
cleanup
|
451 |
|
added type_rule into logic
|
452 |
# Type rule |
EXPERIMENTAL type_rule argum...
|
453 |
my $type_filters = {}; |
cleanup
|
454 |
my $type_rule_off = !$self->{_type_rule_is_called} || $opt{type_rule_off}; |
455 |
unless ($type_rule_off) { |
|
456 |
my $type_rule_off_parts = { |
|
457 |
1 => $opt{type_rule1_off}, |
|
458 |
2 => $opt{type_rule2_off} |
|
459 |
}; |
|
460 |
for my $i (1, 2) { |
|
461 |
unless ($type_rule_off_parts->{$i}) { |
|
462 |
$type_filters->{$i} = {}; |
|
463 |
my $table_alias = $opt{table_alias} || {}; |
|
464 |
for my $alias (keys %$table_alias) { |
|
465 |
my $table = $table_alias->{$alias}; |
|
466 |
|
|
467 |
for my $column (keys %{$self->{"_into$i"}{key}{$table} || {}}) { |
|
468 |
$type_filters->{$i}->{"$alias.$column"} = $self->{"_into$i"}{key}{$table}{$column}; |
|
EXPERIMENTAL type_rule argum...
|
469 |
} |
470 |
} |
|
cleanup
|
471 |
$type_filters->{$i} = {%{$type_filters->{$i}}, %{$self->{"_into$i"}{key}{$main_table} || {}}} |
472 |
if $main_table; |
|
added EXPERIMENTAL execute()...
|
473 |
} |
added type_rule into logic
|
474 |
} |
475 |
} |
|
cleanup
|
476 |
|
moved DBIx::Custom::Guide to...
|
477 |
# Applied filter(DEPRECATED!) |
micro optimization
|
478 |
if ($self->{filter}{on}) { |
479 |
my $applied_filter = {}; |
|
cleanup
|
480 |
for my $table (@$tables) { |
micro optimization
|
481 |
$applied_filter = { |
482 |
%$applied_filter, |
|
483 |
%{$self->{filter}{out}->{$table} || {}} |
|
484 |
} |
|
added auto_filter method
|
485 |
} |
micro optimization
|
486 |
$filter = {%$applied_filter, %$filter}; |
added auto_filter method
|
487 |
} |
488 |
|
|
cleanup
|
489 |
# Replace filter name to code |
cleanup
|
490 |
for my $column (keys %$filter) { |
cleanup
|
491 |
my $name = $filter->{$column}; |
492 |
if (!defined $name) { |
|
493 |
$filter->{$column} = undef; |
|
renamed auto_filter to apply...
|
494 |
} |
cleanup
|
495 |
elsif (ref $name ne 'CODE') { |
cleanup
|
496 |
croak qq{Filter "$name" is not registered" } . _subname |
cleanup
|
497 |
unless exists $self->filters->{$name}; |
498 |
$filter->{$column} = $self->filters->{$name}; |
|
cleanup
|
499 |
} |
500 |
} |
|
added EXPERIMENTAL execute m...
|
501 | |
cleanup
|
502 |
# Execute |
micro optimization
|
503 |
my $sth = $query->{sth}; |
cleanup
|
504 |
my $affected; |
- fixed bug that DBIx::Custo...
|
505 |
if (!$query->{duplicate} && $type_rule_off && !keys %$filter && !$self->{default_out_filter} |
506 |
&& !$opt{bind_type} && !$opt{type} && !$ENV{DBIX_CUSTOM_DEBUG}) |
|
micro optimization
|
507 |
{ |
cleanup
|
508 |
eval { $affected = $sth->execute(map { $param->[0]->{$_} } @{$query->{columns}}) }; |
micro optimization
|
509 |
} |
510 |
else { |
|
511 |
# Create bind values |
|
cleanup
|
512 |
my ($bind, $bind_types) = $self->_create_bind_values($param->[0], $query->{columns}, |
micro optimization
|
513 |
$filter, $type_filters, $opt{bind_type} || $opt{type} || {}); |
514 | ||
515 |
# Execute |
|
516 |
eval { |
|
517 |
if ($opt{bind_type} || $opt{type}) { |
|
518 |
$sth->bind_param($_ + 1, $bind->[$_], |
|
519 |
$bind_types->[$_] ? $bind_types->[$_] : ()) |
|
520 |
for (0 .. @$bind - 1); |
|
521 |
$affected = $sth->execute; |
|
522 |
} |
|
523 |
else { |
|
524 |
$affected = $sth->execute(@$bind); |
|
525 |
} |
|
526 | ||
527 |
# DEBUG message |
|
528 |
if ($ENV{DBIX_CUSTOM_DEBUG}) { |
|
529 |
warn "SQL:\n" . $query->{sql} . "\n"; |
|
530 |
my @output; |
|
531 |
for my $value (@$bind) { |
|
532 |
$value = 'undef' unless defined $value; |
|
533 |
$value = encode($ENV{DBIX_CUSTOM_DEBUG_ENCODING} || 'UTF-8', $value) |
|
534 |
if utf8::is_utf8($value); |
|
535 |
push @output, $value; |
|
536 |
} |
|
537 |
warn "Bind values: " . join(', ', @output) . "\n\n"; |
|
538 |
} |
|
539 |
}; |
|
540 |
} |
|
improved error messages
|
541 |
|
micro optimization
|
542 |
$self->_croak($@, qq{. Following SQL is executed.\n} |
543 |
. qq{$query->{sql}\n} . _subname) if $@; |
|
- insert method id value is ...
|
544 | |
545 |
# Remove id from parameter |
|
cleanup
|
546 |
for my $column (@cleanup, @{$opt{cleanup} || []}) { |
547 |
delete $_->{$column} for @$param; |
|
548 |
} |
|
cleanup
|
549 |
|
micro optimization
|
550 |
# Not select statement |
551 |
return $affected unless $sth->{NUM_OF_FIELDS}; |
|
552 | ||
553 |
# Filter(DEPRECATED!) |
|
554 |
my $infilter = {}; |
|
555 |
if ($self->{filter}{on}) { |
|
556 |
$infilter->{in} = {}; |
|
557 |
$infilter->{end} = {}; |
|
558 |
push @$tables, $main_table if $main_table; |
|
559 |
for my $table (@$tables) { |
|
560 |
for my $way (qw/in end/) { |
|
561 |
$infilter->{$way} = {%{$infilter->{$way}}, |
|
562 |
%{$self->{filter}{$way}{$table} || {}}}; |
|
cleanup
|
563 |
} |
cleanup
|
564 |
} |
cleanup
|
565 |
} |
micro optimization
|
566 |
|
567 |
# Result |
|
micro optimization
|
568 |
$self->result_class->new( |
micro optimization
|
569 |
sth => $sth, |
570 |
dbi => $self, |
|
571 |
default_filter => $self->{default_in_filter}, |
|
572 |
filter => $infilter->{in} || {}, |
|
573 |
end_filter => $infilter->{end} || {}, |
|
574 |
type_rule => { |
|
575 |
from1 => $self->type_rule->{from1}, |
|
576 |
from2 => $self->type_rule->{from2} |
|
577 |
}, |
|
578 |
); |
|
cleanup
|
579 |
} |
580 | ||
added test
|
581 |
sub get_table_info { |
cleanup
|
582 |
my ($self, %opt) = @_; |
added EXPERIMENTAL find_tabl...
|
583 |
|
cleanup
|
584 |
my $exclude = delete $opt{exclude}; |
585 |
croak qq/"$_" is wrong option/ for keys %opt; |
|
added EXPERIMENTAL find_tabl...
|
586 |
|
added test
|
587 |
my $table_info = []; |
588 |
$self->each_table( |
|
589 |
sub { push @$table_info, {table => $_[1], info => $_[2] } }, |
|
590 |
exclude => $exclude |
|
591 |
); |
|
added EXPERIMENTAL find_tabl...
|
592 |
|
cleanup test
|
593 |
return [sort {$a->{table} cmp $b->{table} } @$table_info]; |
added EXPERIMENTAL find_tabl...
|
594 |
} |
595 | ||
- added EXPERIMENTAL get_col...
|
596 |
sub get_column_info { |
cleanup
|
597 |
my ($self, %opt) = @_; |
- added EXPERIMENTAL get_col...
|
598 |
|
cleanup
|
599 |
my $exclude_table = delete $opt{exclude_table}; |
600 |
croak qq/"$_" is wrong option/ for keys %opt; |
|
- added EXPERIMENTAL get_col...
|
601 |
|
602 |
my $column_info = []; |
|
603 |
$self->each_column( |
|
604 |
sub { push @$column_info, {table => $_[1], column => $_[2], info => $_[3] } }, |
|
605 |
exclude_table => $exclude_table |
|
606 |
); |
|
607 |
|
|
608 |
return [ |
|
609 |
sort {$a->{table} cmp $b->{table} || $a->{column} cmp $b->{column} } |
|
cleanup
|
610 |
@$column_info]; |
- added EXPERIMENTAL get_col...
|
611 |
} |
612 | ||
- method method of DBIx::Cus...
|
613 |
sub helper { |
614 |
my $self = shift; |
|
615 |
|
|
616 |
# Register method |
|
617 |
my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
618 |
$self->{_methods} = {%{$self->{_methods} || {}}, %$methods}; |
|
619 |
|
|
620 |
return $self; |
|
621 |
} |
|
622 | ||
cleanup
|
623 |
sub insert { |
- insert, insert_at, update,...
|
624 |
my $self = shift; |
added EXPERIMENTAL reserved_...
|
625 |
|
cleanup
|
626 |
# Options |
cleanup
|
627 |
my $params = @_ % 2 ? shift : undef; |
cleanup
|
628 |
my %opt = @_; |
micro optimization
|
629 |
warn "insert method param option is DEPRECATED!" if $opt{param}; |
cleanup
|
630 |
$params ||= delete $opt{param} || {}; |
631 |
|
|
632 |
my $multi; |
|
633 |
if (ref $params eq 'ARRAY') { $multi = 1 } |
|
634 |
else { $params = [$params] } |
|
- added EXPERIMENTAL timesta...
|
635 |
|
added EXPERIMENTAL insert cr...
|
636 |
# Timestamp(DEPRECATED!) |
cleanup
|
637 |
if (!$multi && $opt{timestamp} && (my $insert_timestamp = $self->insert_timestamp)) { |
- insert timestamp option is...
|
638 |
warn "insert timestamp option is DEPRECATED! use created_at with now attribute"; |
- added EXPERIMENTAL update_...
|
639 |
my $columns = $insert_timestamp->[0]; |
640 |
$columns = [$columns] unless ref $columns eq 'ARRAY'; |
|
641 |
my $value = $insert_timestamp->[1]; |
|
642 |
$value = $value->() if ref $value eq 'CODE'; |
|
cleanup
|
643 |
$params->[0]->{$_} = $value for @$columns; |
- added EXPERIMENTAL timesta...
|
644 |
} |
added EXPERIMENTAL insert cr...
|
645 | |
646 |
# Created time and updated time |
|
647 |
my @timestamp_cleanup; |
|
648 |
if (defined $opt{created_at} || defined $opt{updated_at}) { |
|
- insert method created_at a...
|
649 |
my $now = $self->now; |
650 |
$now = $now->() if ref $now eq 'CODE'; |
|
added EXPERIMENTAL insert cr...
|
651 |
if (defined $opt{created_at}) { |
cleanup
|
652 |
$_->{$opt{created_at}} = $now for @$params; |
added EXPERIMENTAL insert cr...
|
653 |
push @timestamp_cleanup, $opt{created_at}; |
654 |
} |
|
655 |
if (defined $opt{updated_at}) { |
|
cleanup
|
656 |
$_->{$opt{updated_at}} = $now for @$params; |
added EXPERIMENTAL insert cr...
|
657 |
push @timestamp_cleanup, $opt{updated_at}; |
658 |
} |
|
659 |
} |
|
cleanup
|
660 |
|
661 |
# Merge id to parameter |
|
- insert method id value is ...
|
662 |
my @cleanup; |
micro optimization and
|
663 |
my $id_param = {}; |
cleanup
|
664 |
if (defined $opt{id} && !$multi) { |
- insert method id value is ...
|
665 |
croak "insert id option must be specified with primary_key option" |
micro optimization
|
666 |
unless $opt{primary_key}; |
667 |
$opt{primary_key} = [$opt{primary_key}] unless ref $opt{primary_key}; |
|
668 |
$opt{id} = [$opt{id}] unless ref $opt{id}; |
|
669 |
for (my $i = 0; $i < @{$opt{primary_key}}; $i++) { |
|
670 |
my $key = $opt{primary_key}->[$i]; |
|
cleanup
|
671 |
next if exists $params->[0]->{$key}; |
672 |
$params->[0]->{$key} = $opt{id}->[$i]; |
|
- insert method id value is ...
|
673 |
push @cleanup, $key; |
micro optimization
|
674 |
} |
675 |
} |
|
cleanup
|
676 |
|
cleanup
|
677 |
# Insert statement |
cleanup
|
678 |
my $sql = "insert "; |
cleanup
|
679 |
$sql .= "$opt{prefix} " if defined $opt{prefix}; |
added EXPERIMENTAL q method
|
680 |
$sql .= "into " . $self->q($opt{table}) . " " |
cleanup
|
681 |
. $self->values_clause($params->[0], {wrap => $opt{wrap}}) . " "; |
micro optimization
|
682 | |
683 |
# Remove id from parameter |
|
cleanup
|
684 |
delete $params->[0]->{$_} for @cleanup; |
packaging one directory
|
685 |
|
686 |
# Execute query |
|
micro optimization
|
687 |
$opt{statement} = 'insert'; |
added EXPERIMENTAL insert cr...
|
688 |
$opt{cleanup} = \@timestamp_cleanup; |
cleanup
|
689 |
$self->execute($sql, $params, %opt); |
packaging one directory
|
690 |
} |
691 | ||
- added EXPERIMENTAL update_...
|
692 |
sub insert_timestamp { |
693 |
my $self = shift; |
|
694 |
|
|
- insert timestamp option is...
|
695 |
warn "insert_timestamp method is DEPRECATED! use now attribute"; |
696 |
|
|
- added EXPERIMENTAL update_...
|
697 |
if (@_) { |
698 |
$self->{insert_timestamp} = [@_]; |
|
699 |
|
|
700 |
return $self; |
|
701 |
} |
|
702 |
return $self->{insert_timestamp}; |
|
703 |
} |
|
704 | ||
- added EXPERIMENTAL DBIx::C...
|
705 |
sub include_model { |
706 |
my ($self, $name_space, $model_infos) = @_; |
|
707 |
|
|
cleanup
|
708 |
# Name space |
- added EXPERIMENTAL DBIx::C...
|
709 |
$name_space ||= ''; |
cleanup
|
710 |
|
711 |
# Get Model infomations |
|
- added EXPERIMENTAL DBIx::C...
|
712 |
unless ($model_infos) { |
cleanup
|
713 | |
- added EXPERIMENTAL DBIx::C...
|
714 |
# Load name space module |
cleanup
|
715 |
croak qq{"$name_space" is invalid class name } . _subname |
- added EXPERIMENTAL DBIx::C...
|
716 |
if $name_space =~ /[^\w:]/; |
717 |
eval "use $name_space"; |
|
cleanup
|
718 |
croak qq{Name space module "$name_space.pm" is needed. $@ } |
719 |
. _subname |
|
improved error messages
|
720 |
if $@; |
- added EXPERIMENTAL DBIx::C...
|
721 |
|
722 |
# Search model modules |
|
723 |
my $path = $INC{"$name_space.pm"}; |
|
724 |
$path =~ s/\.pm$//; |
|
725 |
opendir my $dh, $path |
|
cleanup
|
726 |
or croak qq{Can't open directory "$path": $! } . _subname |
- added EXPERIMENTAL DBIx::C...
|
727 |
$model_infos = []; |
728 |
while (my $module = readdir $dh) { |
|
729 |
push @$model_infos, $module |
|
730 |
if $module =~ s/\.pm$//; |
|
731 |
} |
|
732 |
close $dh; |
|
733 |
} |
|
734 |
|
|
cleanup
|
735 |
# Include models |
cleanup
|
736 |
for my $model_info (@$model_infos) { |
- added EXPERIMENTAL DBIx::C...
|
737 |
|
cleanup
|
738 |
# Load model |
- added EXPERIMENTAL DBIx::C...
|
739 |
my $model_class; |
740 |
my $model_name; |
|
741 |
my $model_table; |
|
742 |
if (ref $model_info eq 'HASH') { |
|
743 |
$model_class = $model_info->{class}; |
|
744 |
$model_name = $model_info->{name}; |
|
745 |
$model_table = $model_info->{table}; |
|
746 |
|
|
747 |
$model_name ||= $model_class; |
|
748 |
$model_table ||= $model_name; |
|
749 |
} |
|
removed EXPERIMETNAL flag fr...
|
750 |
else { $model_class = $model_name = $model_table = $model_info } |
- added EXPERIMENTAL DBIx::C...
|
751 |
my $mclass = "${name_space}::$model_class"; |
cleanup
|
752 |
croak qq{"$mclass" is invalid class name } . _subname |
- added EXPERIMENTAL DBIx::C...
|
753 |
if $mclass =~ /[^\w:]/; |
754 |
unless ($mclass->can('isa')) { |
|
755 |
eval "use $mclass"; |
|
cleanup
|
756 |
croak "$@ " . _subname if $@; |
- added EXPERIMENTAL DBIx::C...
|
757 |
} |
758 |
|
|
cleanup
|
759 |
# Create model |
cleanup
|
760 |
my $opt = {}; |
761 |
$opt->{model_class} = $mclass if $mclass; |
|
762 |
$opt->{name} = $model_name if $model_name; |
|
763 |
$opt->{table} = $model_table if $model_table; |
|
764 |
$self->create_model($opt); |
|
- added EXPERIMENTAL DBIx::C...
|
765 |
} |
766 |
|
|
767 |
return $self; |
|
768 |
} |
|
769 | ||
added EXPERIMENTAL like_valu...
|
770 |
sub like_value { sub { "%$_[0]%" } } |
771 | ||
added DBIx::Custom::Mapper
|
772 |
sub mapper { |
773 |
my $self = shift; |
|
774 |
return DBIx::Custom::Mapper->new(@_); |
|
775 |
} |
|
776 | ||
added EXPERIMENTAL updat_par...
|
777 |
sub merge_param { |
778 |
my ($self, @params) = @_; |
|
779 |
|
|
cleanup
|
780 |
# Merge parameters |
fixed merge_param bug
|
781 |
my $merge = {}; |
cleanup
|
782 |
for my $param (@params) { |
783 |
for my $column (keys %$param) { |
|
fixed merge_param bug
|
784 |
my $param_is_array = ref $param->{$column} eq 'ARRAY' ? 1 : 0; |
785 |
|
|
786 |
if (exists $merge->{$column}) { |
|
787 |
$merge->{$column} = [$merge->{$column}] |
|
788 |
unless ref $merge->{$column} eq 'ARRAY'; |
|
789 |
push @{$merge->{$column}}, |
|
790 |
ref $param->{$column} ? @{$param->{$column}} : $param->{$column}; |
|
added EXPERIMENTAL updat_par...
|
791 |
} |
792 |
else { |
|
fixed merge_param bug
|
793 |
$merge->{$column} = $param->{$column}; |
added EXPERIMENTAL updat_par...
|
794 |
} |
795 |
} |
|
796 |
} |
|
797 |
|
|
fixed merge_param bug
|
798 |
return $merge; |
added EXPERIMENTAL updat_par...
|
799 |
} |
800 | ||
- added EXPERIMENTAL DBIx::C...
|
801 |
sub model { |
802 |
my ($self, $name, $model) = @_; |
|
803 |
|
|
cleanup
|
804 |
# Set model |
- added EXPERIMENTAL DBIx::C...
|
805 |
if ($model) { |
806 |
$self->models->{$name} = $model; |
|
807 |
return $self; |
|
808 |
} |
|
809 |
|
|
810 |
# Check model existance |
|
cleanup
|
811 |
croak qq{Model "$name" is not included } . _subname |
- added EXPERIMENTAL DBIx::C...
|
812 |
unless $self->models->{$name}; |
813 |
|
|
cleanup
|
814 |
# Get model |
- added EXPERIMENTAL DBIx::C...
|
815 |
return $self->models->{$name}; |
816 |
} |
|
817 | ||
cleanup
|
818 |
sub mycolumn { |
819 |
my ($self, $table, $columns) = @_; |
|
820 |
|
|
cleanup
|
821 |
# Create column clause |
822 |
my @column; |
|
cleanup
|
823 |
$columns ||= []; |
added EXPERIMENTAL q method
|
824 |
push @column, $self->q($table) . "." . $self->q($_) . |
825 |
" as " . $self->q($_) |
|
added quote method's two cha...
|
826 |
for @$columns; |
cleanup
|
827 |
|
828 |
return join (', ', @column); |
|
829 |
} |
|
830 | ||
added dbi_options attribute
|
831 |
sub new { |
832 |
my $self = shift->SUPER::new(@_); |
|
833 |
|
|
cleanup
|
834 |
# Check attributes |
added dbi_options attribute
|
835 |
my @attrs = keys %$self; |
cleanup
|
836 |
for my $attr (@attrs) { |
- added EXPERIMENTAL DBIx::C...
|
837 |
croak qq{Invalid attribute: "$attr" } . _subname |
added dbi_options attribute
|
838 |
unless $self->can($attr); |
839 |
} |
|
cleanup
|
840 |
|
841 |
$self->{safety_character} = 'a-zA-Z0-9_' |
|
842 |
unless exists $self->{safety_character}; |
|
- added EXPERIMENTAL order m...
|
843 | |
added EXPERIMENTAL system_ta...
|
844 |
# DEPRECATED |
cleanup
|
845 |
$self->{_tags} = { |
added EXPERIMENTAL system_ta...
|
846 |
'?' => \&DBIx::Custom::Tag::placeholder, |
847 |
'=' => \&DBIx::Custom::Tag::equal, |
|
848 |
'<>' => \&DBIx::Custom::Tag::not_equal, |
|
849 |
'>' => \&DBIx::Custom::Tag::greater_than, |
|
850 |
'<' => \&DBIx::Custom::Tag::lower_than, |
|
851 |
'>=' => \&DBIx::Custom::Tag::greater_than_equal, |
|
852 |
'<=' => \&DBIx::Custom::Tag::lower_than_equal, |
|
853 |
'like' => \&DBIx::Custom::Tag::like, |
|
854 |
'in' => \&DBIx::Custom::Tag::in, |
|
855 |
'insert_param' => \&DBIx::Custom::Tag::insert_param, |
|
856 |
'update_param' => \&DBIx::Custom::Tag::update_param |
|
cleanup
|
857 |
}; |
cleanup
|
858 |
$self->{tag_parse} = 1 unless exists $self->{tag_parse}; |
859 |
$self->{cache} = 0 unless exists $self->{cache}; |
|
- DBIx::Custom::QueryBuilder...
|
860 |
|
cleanup
|
861 |
return $self; |
862 |
} |
|
863 | ||
- added EXPERIMENTAL pass at...
|
864 |
sub not_exists { DBIx::Custom::NotExists->singleton } |
cleanup
|
865 | |
866 |
sub order { |
|
867 |
my $self = shift; |
|
868 |
return DBIx::Custom::Order->new(dbi => $self, @_); |
|
added EXPERIMENTAL system_ta...
|
869 |
} |
870 | ||
added EXPERIMENTAL q method
|
871 |
sub q { |
872 |
my ($self, $value, $quotemeta) = @_; |
|
873 |
|
|
874 |
my $quote = $self->{reserved_word_quote} |
|
875 |
|| $self->{quote} || $self->quote || ''; |
|
876 |
return "$quote$value$quote" |
|
877 |
if !$quotemeta && ($quote eq '`' || $quote eq '"'); |
|
878 |
|
|
879 |
my $q = substr($quote, 0, 1) || ''; |
|
880 |
my $p; |
|
881 |
if (defined $quote && length $quote > 1) { |
|
882 |
$p = substr($quote, 1, 1); |
|
883 |
} |
|
884 |
else { $p = $q } |
|
885 |
|
|
886 |
if ($quotemeta) { |
|
887 |
$q = quotemeta($q); |
|
888 |
$p = quotemeta($p); |
|
889 |
} |
|
890 |
|
|
891 |
return "$q$value$p"; |
|
892 |
} |
|
893 | ||
cleanup
|
894 |
sub register_filter { |
cleanup
|
895 |
my $self = shift; |
cleanup
|
896 |
|
897 |
# Register filter |
|
898 |
my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
cleanup
|
899 |
$self->filters({%{$self->filters}, %$filters}); |
cleanup
|
900 |
|
cleanup
|
901 |
return $self; |
cleanup
|
902 |
} |
packaging one directory
|
903 | |
904 |
sub select { |
|
- select method can receive ...
|
905 |
my $self = shift; |
906 |
my $column = shift if @_ % 2; |
|
907 |
my %opt = @_; |
|
908 |
$opt{column} = $column if defined $column; |
|
added EXPERIMENTAL reserved_...
|
909 | |
cleanup
|
910 |
# Options |
cleanup
|
911 |
my $tables = ref $opt{table} eq 'ARRAY' ? $opt{table} |
912 |
: defined $opt{table} ? [$opt{table}] |
|
added table not specified ex...
|
913 |
: []; |
cleanup
|
914 |
$opt{table} = $tables; |
cleanup
|
915 |
my $where_param = $opt{where_param} || delete $opt{param} || {}; |
select method where_param op...
|
916 |
warn "select method where_param option is DEPRECATED!" |
917 |
if $opt{where_param}; |
|
select_at is DEPRECATED! use...
|
918 |
|
cleanup
|
919 |
# Add relation tables(DEPRECATED!); |
cleanup
|
920 |
if ($opt{relation}) { |
921 |
warn "select() relation option is DEPRECATED!"; |
|
922 |
$self->_add_relation_table($tables, $opt{relation}); |
|
923 |
} |
|
packaging one directory
|
924 |
|
cleanup
|
925 |
# Select statement |
micro optimization
|
926 |
my $sql = 'select '; |
packaging one directory
|
927 |
|
added EXPERIMENTAL select pr...
|
928 |
# Prefix |
cleanup
|
929 |
$sql .= "$opt{prefix} " if defined $opt{prefix}; |
added EXPERIMENTAL select pr...
|
930 |
|
cleanup
|
931 |
# Column |
cleanup
|
932 |
if (defined $opt{column}) { |
933 |
my $columns |
|
934 |
= ref $opt{column} eq 'ARRAY' ? $opt{column} : [$opt{column}]; |
|
cleanup
|
935 |
for my $column (@$columns) { |
- select() column option can...
|
936 |
if (ref $column eq 'HASH') { |
EXPERIMTANL column method th...
|
937 |
$column = $self->column(%$column) if ref $column eq 'HASH'; |
- select() column option can...
|
938 |
} |
939 |
elsif (ref $column eq 'ARRAY') { |
|
- insert method created_at a...
|
940 |
warn "select column option [COLUMN => ALIAS] syntax is DEPRECATED!" . |
941 |
"use q method to quote the value"; |
|
- select method column optio...
|
942 |
if (@$column == 3 && $column->[1] eq 'as') { |
943 |
warn "[COLUMN, as => ALIAS] is DEPRECATED! use [COLUMN => ALIAS]"; |
|
944 |
splice @$column, 1, 1; |
|
945 |
} |
|
946 |
|
|
added EXPERIMENTAL q method
|
947 |
$column = join(' ', $column->[0], 'as', $self->q($column->[1])); |
- select() column option can...
|
948 |
} |
cleanup
|
949 |
unshift @$tables, @{$self->_search_tables($column)}; |
micro optimization
|
950 |
$sql .= "$column, "; |
added select() all_column op...
|
951 |
} |
micro optimization
|
952 |
$sql =~ s/, $/ /; |
added select() all_column op...
|
953 |
} |
micro optimization
|
954 |
else { $sql .= '* ' } |
added select() all_column op...
|
955 |
|
956 |
# Table |
|
micro optimization
|
957 |
$sql .= 'from '; |
cleanup
|
958 |
if ($opt{relation}) { |
cleanup
|
959 |
my $found = {}; |
cleanup
|
960 |
for my $table (@$tables) { |
added EXPERIMENTAL q method
|
961 |
$sql .= $self->q($table) . ', ' unless $found->{$table}; |
cleanup
|
962 |
$found->{$table} = 1; |
- added EXPERIMENTAL DBIx::C...
|
963 |
} |
packaging one directory
|
964 |
} |
added EXPERIMENTAL q method
|
965 |
else { $sql .= $self->q($tables->[-1] || '') . ' ' } |
micro optimization
|
966 |
$sql =~ s/, $/ /; |
cleanup
|
967 |
croak "select method table option must be specified " . _subname |
added EXPERIMENTAL DBIx::Cus...
|
968 |
unless defined $tables->[-1]; |
cleanup
|
969 | |
cleanup
|
970 |
# Add tables in parameter |
DEPRECATED select() param op...
|
971 |
unshift @$tables, |
972 |
@{$self->_search_tables(join(' ', keys %$where_param) || '')}; |
|
fixed some select() join opi...
|
973 |
|
select() where can't receive...
|
974 |
# Where |
cleanup
|
975 |
my $w = $self->_where_clause_and_param($opt{where}, $where_param, |
976 |
delete $opt{id}, $opt{primary_key}, $tables->[-1]); |
|
remove experimental DBIx::Cu...
|
977 |
|
fixed some select() join opi...
|
978 |
# Add table names in where clause |
cleanup
|
979 |
unshift @$tables, @{$self->_search_tables($w->{clause})}; |
remove experimental DBIx::Cu...
|
980 |
|
cleanup
|
981 |
# Join statement |
cleanup
|
982 |
$self->_push_join(\$sql, $opt{join}, $tables) if defined $opt{join}; |
remove experimental DBIx::Cu...
|
983 |
|
cleanup
|
984 |
# Add where clause |
cleanup
|
985 |
$sql .= "$w->{clause} "; |
select() where can't receive...
|
986 |
|
cleanup
|
987 |
# Relation(DEPRECATED!); |
cleanup
|
988 |
$self->_push_relation(\$sql, $tables, $opt{relation}, $w->{clause} eq '' ? 1 : 0) |
cleanup
|
989 |
if $opt{relation}; |
cleanup
|
990 |
|
packaging one directory
|
991 |
# Execute query |
- insert method id value is ...
|
992 |
$opt{statement} = 'select'; |
cleanup
|
993 |
my $result = $self->execute($sql, $w->{param}, %opt); |
packaging one directory
|
994 |
|
micro optimization
|
995 |
$result; |
packaging one directory
|
996 |
} |
997 | ||
add experimental setup_model...
|
998 |
sub setup_model { |
999 |
my $self = shift; |
|
1000 |
|
|
cleanup
|
1001 |
# Setup model |
add experimental setup_model...
|
1002 |
$self->each_column( |
1003 |
sub { |
|
1004 |
my ($self, $table, $column, $column_info) = @_; |
|
1005 |
if (my $model = $self->models->{$table}) { |
|
1006 |
push @{$model->columns}, $column; |
|
1007 |
} |
|
1008 |
} |
|
1009 |
); |
|
add experimental DBIx::Custo...
|
1010 |
return $self; |
add experimental setup_model...
|
1011 |
} |
1012 | ||
update pod
|
1013 |
sub show_datatype { |
1014 |
my ($self, $table) = @_; |
|
1015 |
croak "Table name must be specified" unless defined $table; |
|
1016 |
print "$table\n"; |
|
1017 |
|
|
1018 |
my $result = $self->select(table => $table, where => "'0' <> '0'"); |
|
1019 |
my $sth = $result->sth; |
|
1020 | ||
1021 |
my $columns = $sth->{NAME}; |
|
1022 |
my $data_types = $sth->{TYPE}; |
|
1023 |
|
|
1024 |
for (my $i = 0; $i < @$columns; $i++) { |
|
1025 |
my $column = $columns->[$i]; |
|
show_datatype method return ...
|
1026 |
my $data_type = lc $data_types->[$i]; |
update pod
|
1027 |
print "$column: $data_type\n"; |
1028 |
} |
|
1029 |
} |
|
1030 | ||
1031 |
sub show_typename { |
|
1032 |
my ($self, $t) = @_; |
|
1033 |
croak "Table name must be specified" unless defined $t; |
|
1034 |
print "$t\n"; |
|
1035 |
|
|
1036 |
$self->each_column(sub { |
|
1037 |
my ($self, $table, $column, $infos) = @_; |
|
1038 |
return unless $table eq $t; |
|
show_datatype method return ...
|
1039 |
my $typename = lc $infos->{TYPE_NAME}; |
update pod
|
1040 |
print "$column: $typename\n"; |
1041 |
}); |
|
1042 |
|
|
1043 |
return $self; |
|
1044 |
} |
|
1045 | ||
test cleanup
|
1046 |
sub show_tables { |
1047 |
my $self = shift; |
|
1048 |
|
|
1049 |
my %tables; |
|
1050 |
$self->each_table(sub { $tables{$_[1]}++ }); |
|
1051 |
print join("\n", sort keys %tables) . "\n"; |
|
1052 |
return $self; |
|
1053 |
} |
|
1054 | ||
added type_rule method and f...
|
1055 |
sub type_rule { |
1056 |
my $self = shift; |
|
micro optimization
|
1057 | |
1058 |
$self->{_type_rule_is_called} = 1; |
|
added type_rule method and f...
|
1059 |
|
1060 |
if (@_) { |
|
changed type_rule arguments ...
|
1061 |
my $type_rule = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
fixed bug that type_rule fro...
|
1062 |
|
1063 |
# Into |
|
cleanup
|
1064 |
for my $i (1 .. 2) { |
EXPERIMENTAL type_rule argum...
|
1065 |
my $into = "into$i"; |
cleanup
|
1066 |
my $exists_into = exists $type_rule->{$into}; |
EXPERIMENTAL type_rule argum...
|
1067 |
$type_rule->{$into} = _array_to_hash($type_rule->{$into}); |
1068 |
$self->{type_rule} = $type_rule; |
|
1069 |
$self->{"_$into"} = {}; |
|
cleanup
|
1070 |
for my $type_name (keys %{$type_rule->{$into} || {}}) { |
EXPERIMENTAL type_rule argum...
|
1071 |
croak qq{type name of $into section must be lower case} |
1072 |
if $type_name =~ /[A-Z]/; |
|
1073 |
} |
|
cleanup
|
1074 |
|
EXPERIMENTAL type_rule argum...
|
1075 |
$self->each_column(sub { |
1076 |
my ($dbi, $table, $column, $column_info) = @_; |
|
1077 |
|
|
1078 |
my $type_name = lc $column_info->{TYPE_NAME}; |
|
1079 |
if ($type_rule->{$into} && |
|
1080 |
(my $filter = $type_rule->{$into}->{$type_name})) |
|
type_rule can receive filter...
|
1081 |
{ |
EXPERIMENTAL type_rule argum...
|
1082 |
return unless exists $type_rule->{$into}->{$type_name}; |
1083 |
if (defined $filter && ref $filter ne 'CODE') |
|
1084 |
{ |
|
1085 |
my $fname = $filter; |
|
1086 |
croak qq{Filter "$fname" is not registered" } . _subname |
|
1087 |
unless exists $self->filters->{$fname}; |
|
1088 |
|
|
1089 |
$filter = $self->filters->{$fname}; |
|
1090 |
} |
|
1091 | ||
micro optimization
|
1092 |
$self->{"_$into"}{key}{$table}{$column} = $filter; |
1093 |
$self->{"_$into"}{dot}{"$table.$column"} = $filter; |
|
EXPERIMENTAL type_rule argum...
|
1094 |
} |
1095 |
}); |
|
1096 |
} |
|
1097 | ||
1098 |
# From |
|
cleanup
|
1099 |
for my $i (1 .. 2) { |
EXPERIMENTAL type_rule argum...
|
1100 |
$type_rule->{"from$i"} = _array_to_hash($type_rule->{"from$i"}); |
cleanup
|
1101 |
for my $data_type (keys %{$type_rule->{"from$i"} || {}}) { |
EXPERIMENTAL type_rule argum...
|
1102 |
croak qq{data type of from$i section must be lower case or number} |
1103 |
if $data_type =~ /[A-Z]/; |
|
1104 |
my $fname = $type_rule->{"from$i"}{$data_type}; |
|
1105 |
if (defined $fname && ref $fname ne 'CODE') { |
|
type_rule can receive filter...
|
1106 |
croak qq{Filter "$fname" is not registered" } . _subname |
1107 |
unless exists $self->filters->{$fname}; |
|
1108 |
|
|
EXPERIMENTAL type_rule argum...
|
1109 |
$type_rule->{"from$i"}{$data_type} = $self->filters->{$fname}; |
type_rule can receive filter...
|
1110 |
} |
fixed bug that type_rule fro...
|
1111 |
} |
1112 |
} |
|
1113 |
|
|
added type_rule method and f...
|
1114 |
return $self; |
1115 |
} |
|
1116 |
|
|
1117 |
return $self->{type_rule} || {}; |
|
1118 |
} |
|
1119 | ||
cleanup
|
1120 |
sub update { |
- insert, insert_at, update,...
|
1121 |
my $self = shift; |
added EXPERIMENTAL reserved_...
|
1122 | |
cleanup
|
1123 |
# Options |
cleanup
|
1124 |
my $param = @_ % 2 ? shift : undef; |
cleanup
|
1125 |
my %opt = @_; |
cleanup
|
1126 |
warn "update param option is DEPRECATED!" if $opt{param}; |
cleanup
|
1127 |
warn "update method where_param option is DEPRECATED!" |
1128 |
if $opt{where_param}; |
|
cleanup
|
1129 |
$param ||= $opt{param} || {}; |
- added EXPERIMENTAL timesta...
|
1130 |
|
cleanup
|
1131 |
# Don't allow update all rows |
1132 |
croak qq{update method where option must be specified } . _subname |
|
1133 |
if !$opt{where} && !defined $opt{id} && !$opt{allow_update_all}; |
|
1134 |
|
|
added EXPERIMENTAL insert cr...
|
1135 |
# Timestamp(DEPRECATED!) |
cleanup
|
1136 |
if ($opt{timestamp} && (my $update_timestamp = $self->update_timestamp)) { |
- insert timestamp option is...
|
1137 |
warn "update timestamp option is DEPRECATED! use updated_at and now method"; |
- added EXPERIMENTAL update_...
|
1138 |
my $columns = $update_timestamp->[0]; |
1139 |
$columns = [$columns] unless ref $columns eq 'ARRAY'; |
|
1140 |
my $value = $update_timestamp->[1]; |
|
1141 |
$value = $value->() if ref $value eq 'CODE'; |
|
1142 |
$param->{$_} = $value for @$columns; |
|
- added EXPERIMENTAL timesta...
|
1143 |
} |
1144 | ||
added EXPERIMENTAL insert cr...
|
1145 |
# Created time and updated time |
1146 |
my @timestamp_cleanup; |
|
1147 |
if (defined $opt{updated_at}) { |
|
- insert method created_at a...
|
1148 |
my $now = $self->now; |
1149 |
$now = $now->() if ref $now eq 'CODE'; |
|
added EXPERIMENTAL insert cr...
|
1150 |
$param->{$opt{updated_at}} = $self->now->(); |
1151 |
push @timestamp_cleanup, $opt{updated_at}; |
|
1152 |
} |
|
1153 | ||
cleanup
|
1154 |
# Assign clause |
cleanup
|
1155 |
my $assign_clause = $self->assign_clause($param, {wrap => $opt{wrap}}); |
cleanup
|
1156 |
|
improved delete() and update...
|
1157 |
# Where |
cleanup
|
1158 |
my $w = $self->_where_clause_and_param($opt{where}, $opt{where_param}, |
1159 |
delete $opt{id}, $opt{primary_key}, $opt{table}); |
|
removed reconnect method
|
1160 |
|
cleanup
|
1161 |
# Update statement |
cleanup
|
1162 |
my $sql = "update "; |
1163 |
$sql .= "$opt{prefix} " if defined $opt{prefix}; |
|
added EXPERIMENTAL q method
|
1164 |
$sql .= $self->q($opt{table}) . " set $assign_clause $w->{clause} "; |
cleanup
|
1165 |
|
cleanup
|
1166 |
# Execute query |
- insert method id value is ...
|
1167 |
$opt{statement} = 'update'; |
added EXPERIMENTAL insert cr...
|
1168 |
$opt{cleanup} = \@timestamp_cleanup; |
micro optimization and
|
1169 |
$self->execute($sql, [$param, $w->{param}], %opt); |
removed reconnect method
|
1170 |
} |
1171 | ||
cleanup
|
1172 |
sub update_all { shift->update(@_, allow_update_all => 1) }; |
cleanup
|
1173 | |
cleanup
|
1174 |
sub update_or_insert { |
- EXPERIMENTAL update_or_ins...
|
1175 |
my ($self, $param, %opt) = @_; |
1176 |
croak "update_or_insert method need primary_key and id option " |
|
1177 |
unless defined $opt{id} && defined $opt{primary_key}; |
|
1178 |
my $statement_opt = $opt{option} || {}; |
|
- id option work if id count...
|
1179 | |
1180 |
my $rows = $self->select(%opt, %{$statement_opt->{select} || {}})->all; |
|
1181 |
if (@$rows == 0) { |
|
1182 |
return $self->insert($param, %opt, %{$statement_opt->{insert} || {}}); |
|
1183 |
} |
|
1184 |
elsif (@$rows == 1) { |
|
- fixed update_or_insert bug...
|
1185 |
return 0 unless keys %$param; |
- id option work if id count...
|
1186 |
return $self->update($param, %opt, %{$statement_opt->{update} || {}}); |
1187 |
} |
|
1188 |
else { |
|
1189 |
croak "selected row must be one " . _subname; |
|
1190 |
} |
|
cleanup
|
1191 |
} |
1192 | ||
- added EXPERIMENTAL update_...
|
1193 |
sub update_timestamp { |
1194 |
my $self = shift; |
|
1195 |
|
|
- insert timestamp option is...
|
1196 |
warn "update_timestamp method is DEPRECATED! use now method"; |
1197 |
|
|
- added EXPERIMENTAL update_...
|
1198 |
if (@_) { |
1199 |
$self->{update_timestamp} = [@_]; |
|
1200 |
|
|
1201 |
return $self; |
|
1202 |
} |
|
1203 |
return $self->{update_timestamp}; |
|
1204 |
} |
|
1205 | ||
- method method of DBIx::Cus...
|
1206 |
sub values_clause { |
1207 |
my ($self, $param, $opts) = @_; |
|
1208 |
|
|
1209 |
my $wrap = $opts->{wrap} || {}; |
|
1210 |
|
|
1211 |
# Create insert parameter tag |
|
micro optimization
|
1212 |
my ($q, $p) = split //, $self->q(''); |
- method method of DBIx::Cus...
|
1213 |
|
micro optimization
|
1214 |
# values clause(performance is important) |
micro optimization
|
1215 |
'(' . |
1216 |
join( |
|
1217 |
', ', |
|
1218 |
map { "$q$_$p" } sort keys %$param |
|
1219 |
) . |
|
1220 |
') values (' . |
|
1221 |
join( |
|
1222 |
', ', |
|
1223 |
map { |
|
1224 |
ref $param->{$_} eq 'SCALAR' ? ${$param->{$_}} : |
|
1225 |
$wrap->{$_} ? $wrap->{$_}->(":$_") : |
|
1226 |
":$_"; |
|
1227 |
} sort keys %$param |
|
1228 |
) . |
|
1229 |
')' |
|
- method method of DBIx::Cus...
|
1230 |
} |
1231 | ||
sub module use DBIx::Custom ...
|
1232 |
sub where { DBIx::Custom::Where->new(dbi => shift, @_) } |
added experimental DBIx::Cus...
|
1233 | |
updated pod
|
1234 |
sub _create_query { |
cleanup
|
1235 |
|
added EXPERIMENTAL reuse_que...
|
1236 |
my ($self, $source, $after_build_sql) = @_; |
cleanup
|
1237 |
|
updated pod
|
1238 |
# Cache |
cleanup
|
1239 |
my $cache = $self->{cache}; |
updated pod
|
1240 |
|
1241 |
# Query |
|
1242 |
my $query; |
|
1243 |
|
|
1244 |
# Get cached query |
|
1245 |
if ($cache) { |
|
cleanup
|
1246 |
|
updated pod
|
1247 |
# Get query |
1248 |
my $q = $self->cache_method->($self, $source); |
|
cleanup
|
1249 |
|
updated pod
|
1250 |
# Create query |
1251 |
if ($q) { |
|
1252 |
$query = DBIx::Custom::Query->new($q); |
|
DBIx::Custom::Query filters ...
|
1253 |
$query->{filters} = $self->filters; |
cleanup
|
1254 |
} |
updated pod
|
1255 |
} |
1256 |
|
|
1257 |
# Create query |
|
1258 |
unless ($query) { |
|
1259 | ||
1260 |
# Create query |
|
cleanup
|
1261 |
my $tag_parse = exists $ENV{DBIX_CUSTOM_TAG_PARSE} |
1262 |
? $ENV{DBIX_CUSTOM_TAG_PARSE} : $self->{tag_parse}; |
|
1263 | ||
micro optimization
|
1264 |
my $sql = " " . $source || ''; |
cleanup
|
1265 |
if ($tag_parse && ($sql =~ /\s\{/)) { |
cleanup
|
1266 |
$query = $self->query_builder->build_query($sql); |
1267 |
} |
|
1268 |
else { |
|
1269 |
my @columns; |
|
cleanup
|
1270 |
my $c = $self->{safety_character}; |
micro optimization
|
1271 |
my $re = $c eq 'a-zA-Z0-9_' |
1272 |
? qr/(.*?[^\\]):([$c\.]+)(?:\{(.*?)\})?(.*)/so |
|
1273 |
: qr/(.*?[^\\]):([$c\.]+)(?:\{(.*?)\})?(.*)/s; |
|
cleanup
|
1274 |
my %duplicate; |
1275 |
my $duplicate; |
|
1276 |
# Parameter regex |
|
cleanup
|
1277 |
$sql =~ s/([0-9]):/$1\\:/g; |
micro optimization
|
1278 |
my $new_sql = ''; |
micro optimization
|
1279 |
while ($sql =~ /$re/) { |
cleanup
|
1280 |
push @columns, $2; |
1281 |
$duplicate = 1 if ++$duplicate{$columns[-1]} > 1; |
|
micro optimization
|
1282 |
($new_sql, $sql) = defined $3 ? |
1283 |
($new_sql . "$1$2 $3 ?", " $4") : ($new_sql . "$1?", " $4"); |
|
cleanup
|
1284 |
} |
micro optimization
|
1285 |
$new_sql .= $sql; |
1286 |
$new_sql =~ s/\\:/:/g if index($new_sql, "\\:") != -1; |
|
cleanup
|
1287 | |
1288 |
# Create query |
|
micro optimization
|
1289 |
$query = {sql => $new_sql, columns => \@columns, duplicate => $duplicate}; |
cleanup
|
1290 |
} |
cleanup
|
1291 |
|
updated pod
|
1292 |
# Save query to cache |
1293 |
$self->cache_method->( |
|
1294 |
$self, $source, |
|
1295 |
{ |
|
micro optimization
|
1296 |
sql => $query->{sql}, |
1297 |
columns => $query->{columns}, |
|
DBIx::Custom::Query tables a...
|
1298 |
tables => $query->{tables} || [] |
updated pod
|
1299 |
} |
1300 |
) if $cache; |
|
cleanup
|
1301 |
} |
added EXPERIMENTAL execute m...
|
1302 | |
1303 |
# Filter SQL |
|
micro optimization
|
1304 |
$query->{sql} = $after_build_sql->($query->{sql}) if $after_build_sql; |
1305 |
|
|
added EXPERIMENTAL last_sql ...
|
1306 |
# Save sql |
micro optimization
|
1307 |
$self->{last_sql} = $query->{sql}; |
added EXPERIMENTAL last_sql ...
|
1308 |
|
updated pod
|
1309 |
# Prepare statement handle |
1310 |
my $sth; |
|
added EXPERIMENTAL reuse_que...
|
1311 |
eval { $sth = $self->dbh->prepare($query->{sql}) }; |
updated pod
|
1312 |
|
1313 |
if ($@) { |
|
1314 |
$self->_croak($@, qq{. Following SQL is executed.\n} |
|
1315 |
. qq{$query->{sql}\n} . _subname); |
|
1316 |
} |
|
1317 |
|
|
1318 |
# Set statement handle |
|
micro optimization
|
1319 |
$query->{sth} = $sth; |
updated pod
|
1320 |
|
1321 |
# Set filters |
|
cleanup
|
1322 |
$query->{filters} = $self->{filters} || $self->filters; |
updated pod
|
1323 |
|
1324 |
return $query; |
|
cleanup
|
1325 |
} |
1326 | ||
cleanup
|
1327 |
sub _create_bind_values { |
EXPERIMENTAL type_rule argum...
|
1328 |
my ($self, $params, $columns, $filter, $type_filters, $bind_type) = @_; |
removed reconnect method
|
1329 |
|
micro optimization
|
1330 |
$bind_type = _array_to_hash($bind_type) if ref $bind_type eq 'ARRAY'; |
1331 |
|
|
cleanup
|
1332 |
# Create bind values |
micro optimization
|
1333 |
my @bind; |
1334 |
my @types; |
|
1335 |
my %count; |
|
1336 |
my %not_exists; |
|
cleanup
|
1337 |
for my $column (@$columns) { |
removed reconnect method
|
1338 |
|
micro optimization
|
1339 |
# Bind value |
added experimental not_exist...
|
1340 |
if(ref $params->{$column} eq 'ARRAY') { |
micro optimization
|
1341 |
my $i = $count{$column} || 0; |
1342 |
$i += $not_exists{$column} || 0; |
|
added experimental not_exist...
|
1343 |
my $found; |
1344 |
for (my $k = $i; $i < @{$params->{$column}}; $k++) { |
|
1345 |
if (ref $params->{$column}->[$k] eq 'DBIx::Custom::NotExists') { |
|
micro optimization
|
1346 |
$not_exists{$column}++; |
added experimental not_exist...
|
1347 |
} |
1348 |
else { |
|
micro optimization
|
1349 |
push @bind, $params->{$column}->[$k]; |
added experimental not_exist...
|
1350 |
$found = 1; |
1351 |
last |
|
1352 |
} |
|
1353 |
} |
|
1354 |
next unless $found; |
|
1355 |
} |
|
micro optimization
|
1356 |
else { push @bind, $params->{$column} } |
removed reconnect method
|
1357 |
|
cleanup
|
1358 |
# Filter |
micro optimization
|
1359 |
if (my $f = $filter->{$column} || $self->{default_out_filter} || '') { |
micro optimization
|
1360 |
$bind[-1] = $f->($bind[-1]); |
micro optimization
|
1361 |
} |
separate DBIx::Custom type_r...
|
1362 |
|
1363 |
# Type rule |
|
micro optimization
|
1364 |
if ($self->{_type_rule_is_called}) { |
1365 |
my $tf1 = $self->{"_into1"}->{dot}->{$column} |
|
1366 |
|| $type_filters->{1}->{$column}; |
|
micro optimization
|
1367 |
$bind[-1] = $tf1->($bind[-1]) if $tf1; |
micro optimization
|
1368 |
my $tf2 = $self->{"_into2"}->{dot}->{$column} |
1369 |
|| $type_filters->{2}->{$column}; |
|
micro optimization
|
1370 |
$bind[-1] = $tf2->($bind[-1]) if $tf2; |
micro optimization
|
1371 |
} |
micro optimization
|
1372 |
|
micro optimization
|
1373 |
# Bind types |
micro optimization
|
1374 |
push @types, $bind_type->{$column}; |
removed reconnect method
|
1375 |
|
1376 |
# Count up |
|
micro optimization
|
1377 |
$count{$column}++; |
removed reconnect method
|
1378 |
} |
1379 |
|
|
micro optimization
|
1380 |
return (\@bind, \@types); |
removed reconnect method
|
1381 |
} |
1382 | ||
cleanup
|
1383 |
sub _id_to_param { |
fixed id option bug when col...
|
1384 |
my ($self, $id, $primary_keys, $table) = @_; |
cleanup
|
1385 |
|
1386 |
# Check primary key |
|
cleanup
|
1387 |
croak "primary_key option " . |
- id option work if id count...
|
1388 |
"must be specified when id option is used" . _subname |
cleanup
|
1389 |
unless defined $primary_keys; |
1390 |
$primary_keys = [$primary_keys] unless ref $primary_keys eq 'ARRAY'; |
|
improved error messages
|
1391 |
|
cleanup
|
1392 |
# Create parameter |
1393 |
my $param = {}; |
|
fixed small insert, update, ...
|
1394 |
if (defined $id) { |
cleanup
|
1395 |
$id = [$id] unless ref $id; |
- id option work if id count...
|
1396 |
for(my $i = 0; $i < @$id; $i++) { |
fixed id option bug when col...
|
1397 |
my $key = $primary_keys->[$i]; |
1398 |
$key = "$table." . $key if $table; |
|
1399 |
$param->{$key} = $id->[$i]; |
|
improved error messages
|
1400 |
} |
1401 |
} |
|
1402 |
|
|
cleanup
|
1403 |
return $param; |
improved error messages
|
1404 |
} |
1405 | ||
EXPERIMETAL fork safety impl...
|
1406 |
sub _connect { |
1407 |
my $self = shift; |
|
1408 |
|
|
1409 |
# Attributes |
|
added warnings
|
1410 |
my $dsn = $self->data_source; |
- added EXPERIMENTAL order m...
|
1411 |
warn "data_source is DEPRECATED!\n" |
fixed bug that data_source D...
|
1412 |
if $dsn; |
added warnings
|
1413 |
$dsn ||= $self->dsn; |
data_source is DEPRECATED! I...
|
1414 |
croak qq{"dsn" must be specified } . _subname |
1415 |
unless $dsn; |
|
EXPERIMETAL fork safety impl...
|
1416 |
my $user = $self->user; |
1417 |
my $password = $self->password; |
|
- dbi_option attribute is re...
|
1418 |
my $option = $self->_option; |
1419 |
$option = {%{$self->default_option}, %$option}; |
|
cleanup
|
1420 |
|
EXPERIMETAL fork safety impl...
|
1421 |
# Connect |
cleanup
|
1422 |
my $dbh; |
1423 |
eval { |
|
1424 |
$dbh = DBI->connect( |
|
1425 |
$dsn, |
|
1426 |
$user, |
|
1427 |
$password, |
|
- dbi_option attribute is re...
|
1428 |
$option |
cleanup
|
1429 |
); |
1430 |
}; |
|
EXPERIMETAL fork safety impl...
|
1431 |
|
1432 |
# Connect error |
|
cleanup
|
1433 |
croak "$@ " . _subname if $@; |
EXPERIMETAL fork safety impl...
|
1434 |
|
1435 |
return $dbh; |
|
1436 |
} |
|
1437 | ||
cleanup
|
1438 |
sub _croak { |
1439 |
my ($self, $error, $append) = @_; |
|
cleanup
|
1440 |
|
1441 |
# Append |
|
cleanup
|
1442 |
$append ||= ""; |
1443 |
|
|
1444 |
# Verbose |
|
1445 |
if ($Carp::Verbose) { croak $error } |
|
1446 |
|
|
1447 |
# Not verbose |
|
1448 |
else { |
|
1449 |
|
|
1450 |
# Remove line and module infromation |
|
1451 |
my $at_pos = rindex($error, ' at '); |
|
1452 |
$error = substr($error, 0, $at_pos); |
|
1453 |
$error =~ s/\s+$//; |
|
1454 |
croak "$error$append"; |
|
1455 |
} |
|
1456 |
} |
|
1457 | ||
prepare oracle test
|
1458 |
sub _driver { lc shift->{dbh}->{Driver}->{Name} } |
1459 | ||
added select() all_column op...
|
1460 |
sub _need_tables { |
1461 |
my ($self, $tree, $need_tables, $tables) = @_; |
|
1462 |
|
|
cleanup
|
1463 |
# Get needed tables |
cleanup
|
1464 |
for my $table (@$tables) { |
added select() all_column op...
|
1465 |
if ($tree->{$table}) { |
1466 |
$need_tables->{$table} = 1; |
|
1467 |
$self->_need_tables($tree, $need_tables, [$tree->{$table}{parent}]) |
|
1468 |
} |
|
1469 |
} |
|
1470 |
} |
|
1471 | ||
- dbi_option attribute is re...
|
1472 |
sub _option { |
1473 |
my $self = shift; |
|
1474 |
my $option = {%{$self->dbi_options}, %{$self->dbi_option}, %{$self->option}}; |
|
1475 |
warn "dbi_options is DEPRECATED! use option instead\n" |
|
1476 |
if keys %{$self->dbi_options}; |
|
1477 |
warn "dbi_option is DEPRECATED! use option instead\n" |
|
1478 |
if keys %{$self->dbi_option}; |
|
1479 |
return $option; |
|
1480 |
} |
|
1481 | ||
fixed some select() join opi...
|
1482 |
sub _push_join { |
1483 |
my ($self, $sql, $join, $join_tables) = @_; |
|
1484 |
|
|
cleanup
|
1485 |
$join = [$join] unless ref $join eq 'ARRAY'; |
cleanup
|
1486 |
|
cleanup
|
1487 |
# No join |
fixed some select() join opi...
|
1488 |
return unless @$join; |
1489 |
|
|
cleanup
|
1490 |
# Push join clause |
fixed some select() join opi...
|
1491 |
my $tree = {}; |
1492 |
for (my $i = 0; $i < @$join; $i++) { |
|
1493 |
|
|
cleanup
|
1494 |
# Arrange |
added join new syntax
|
1495 |
my $join_clause;; |
1496 |
my $option; |
|
1497 |
if (ref $join->[$i] eq 'HASH') { |
|
1498 |
$join_clause = $join->[$i]->{clause}; |
|
1499 |
$option = {table => $join->[$i]->{table}}; |
|
1500 |
} |
|
1501 |
else { |
|
1502 |
$join_clause = $join->[$i]; |
|
1503 |
$option = {}; |
|
1504 |
}; |
|
cleanup
|
1505 | |
1506 |
# Find tables in join clause |
|
added join new syntax
|
1507 |
my $table1; |
1508 |
my $table2; |
|
1509 |
if (my $table = $option->{table}) { |
|
1510 |
$table1 = $table->[0]; |
|
1511 |
$table2 = $table->[1]; |
|
fixed some select() join opi...
|
1512 |
} |
cleanup
|
1513 |
else { |
1514 |
my $q = $self->_quote; |
|
1515 |
my $j_clause = (split /\s+on\s+/, $join_clause)[-1]; |
|
1516 |
$j_clause =~ s/'.+?'//g; |
|
1517 |
my $q_re = quotemeta($q); |
|
added quote method's two cha...
|
1518 |
$j_clause =~ s/[$q_re]//g; |
improved join clause parsing
|
1519 |
|
1520 |
my @j_clauses = reverse split /\s(and|on)\s/, $j_clause; |
|
cleanup
|
1521 |
my $c = $self->{safety_character}; |
1522 |
my $join_re = qr/([$c]+)\.[$c]+[^$c].*?([$c]+)\.[$c]+/sm; |
|
improved join clause parsing
|
1523 |
for my $clause (@j_clauses) { |
1524 |
if ($clause =~ $join_re) { |
|
1525 |
$table1 = $1; |
|
1526 |
$table2 = $2; |
|
1527 |
last; |
|
1528 |
} |
|
cleanup
|
1529 |
} |
fixed some select() join opi...
|
1530 |
} |
added join new syntax
|
1531 |
croak qq{join clause must have two table name after "on" keyword. } . |
1532 |
qq{"$join_clause" is passed } . _subname |
|
1533 |
unless defined $table1 && defined $table2; |
|
1534 |
croak qq{right side table of "$join_clause" must be unique } |
|
1535 |
. _subname |
|
1536 |
if exists $tree->{$table2}; |
|
1537 |
croak qq{Same table "$table1" is specified} . _subname |
|
1538 |
if $table1 eq $table2; |
|
1539 |
$tree->{$table2} |
|
1540 |
= {position => $i, parent => $table1, join => $join_clause}; |
|
fixed some select() join opi...
|
1541 |
} |
1542 |
|
|
cleanup
|
1543 |
# Search need tables |
fixed some select() join opi...
|
1544 |
my $need_tables = {}; |
1545 |
$self->_need_tables($tree, $need_tables, $join_tables); |
|
cleanup
|
1546 |
my @need_tables = sort { $tree->{$a}{position} <=> $tree->{$b}{position} } |
1547 |
keys %$need_tables; |
|
cleanup
|
1548 |
|
1549 |
# Add join clause |
|
cleanup
|
1550 |
$$sql .= $tree->{$_}{join} . ' ' for @need_tables; |
fixed some select() join opi...
|
1551 |
} |
cleanup
|
1552 | |
reserved_word_quote is DEPRE...
|
1553 |
sub _quote { |
1554 |
my $self = shift; |
|
micro optimization
|
1555 |
return $self->{reserved_word_quote} || $self->quote || ''; |
reserved_word_quote is DEPRE...
|
1556 |
} |
1557 | ||
cleanup
|
1558 |
sub _remove_duplicate_table { |
1559 |
my ($self, $tables, $main_table) = @_; |
|
1560 |
|
|
1561 |
# Remove duplicate table |
|
1562 |
my %tables = map {defined $_ ? ($_ => 1) : ()} @$tables; |
|
1563 |
delete $tables{$main_table} if $main_table; |
|
1564 |
|
|
micro optimization
|
1565 |
my $new_tables = [keys %tables, $main_table ? $main_table : ()]; |
1566 |
if (my $q = $self->_quote) { |
|
1567 |
$q = quotemeta($q); |
|
1568 |
$_ =~ s/[$q]//g for @$new_tables; |
|
1569 |
} |
|
1570 | ||
1571 |
return $new_tables; |
|
cleanup
|
1572 |
} |
1573 | ||
cleanup
|
1574 |
sub _search_tables { |
cleanup
|
1575 |
my ($self, $source) = @_; |
1576 |
|
|
cleanup
|
1577 |
# Search tables |
cleanup
|
1578 |
my $tables = []; |
cleanup
|
1579 |
my $safety_character = $self->{safety_character}; |
reserved_word_quote is DEPRE...
|
1580 |
my $q = $self->_quote; |
added EXPERIMENTAL q method
|
1581 |
my $quoted_safety_character_re = $self->q("?([$safety_character]+)", 1); |
fixex [] reserved_word_quote...
|
1582 |
my $table_re = $q ? qr/(?:^|[^$safety_character])${quoted_safety_character_re}?\./ |
improved table search in col...
|
1583 |
: qr/(?:^|[^$safety_character])([$safety_character]+)\./; |
cleanup
|
1584 |
while ($source =~ /$table_re/g) { |
1585 |
push @$tables, $1; |
|
1586 |
} |
|
1587 |
|
|
1588 |
return $tables; |
|
1589 |
} |
|
1590 | ||
cleanup
|
1591 |
sub _where_clause_and_param { |
cleanup
|
1592 |
my ($self, $where, $where_param, $id, $primary_key, $table) = @_; |
cleanup
|
1593 | |
cleanup
|
1594 |
$where ||= {}; |
cleanup
|
1595 |
$where = $self->_id_to_param($id, $primary_key, $table) if defined $id; |
cleanup
|
1596 |
$where_param ||= {}; |
cleanup
|
1597 |
my $w = {}; |
1598 |
my $where_clause = ''; |
|
cleanup
|
1599 | |
cleanup
|
1600 |
my $obj; |
1601 |
|
|
- id option work if id count...
|
1602 |
if (ref $where) { |
cleanup
|
1603 |
if (ref $where eq 'HASH') { |
1604 |
my $clause = ['and']; |
|
cleanup
|
1605 |
my $column_join = ''; |
cleanup
|
1606 |
for my $column (keys %$where) { |
cleanup
|
1607 |
$column_join .= $column; |
cleanup
|
1608 |
my $table; |
1609 |
my $c; |
|
1610 |
if ($column =~ /(?:(.*?)\.)?(.*)/) { |
|
1611 |
$table = $1; |
|
1612 |
$c = $2; |
|
1613 |
} |
|
1614 |
|
|
1615 |
my $table_quote; |
|
added EXPERIMENTAL q method
|
1616 |
$table_quote = $self->q($table) if defined $table; |
1617 |
my $column_quote = $self->q($c); |
|
cleanup
|
1618 |
$column_quote = $table_quote . '.' . $column_quote |
1619 |
if defined $table_quote; |
|
cleanup
|
1620 |
push @$clause, "$column_quote = :$column"; |
cleanup
|
1621 |
} |
cleanup
|
1622 | |
1623 |
# Check unsafety column |
|
cleanup
|
1624 |
my $safety = $self->{safety_character}; |
cleanup
|
1625 |
unless ($column_join =~ /^[$safety\.]+$/) { |
1626 |
for my $column (keys %$where) { |
|
1627 |
croak qq{"$column" is not safety column name } . _subname |
|
1628 |
unless $column =~ /^[$safety\.]+$/; |
|
1629 |
} |
|
1630 |
} |
|
1631 |
|
|
cleanup
|
1632 |
$obj = $self->where(clause => $clause, param => $where); |
1633 |
} |
|
cleanup
|
1634 |
elsif (ref $where eq 'DBIx::Custom::Where') { $obj = $where } |
cleanup
|
1635 |
elsif (ref $where eq 'ARRAY') { |
cleanup
|
1636 |
$obj = $self->where(clause => $where->[0], param => $where->[1]); |
cleanup
|
1637 |
} |
1638 |
|
|
1639 |
# Check where argument |
|
1640 |
croak qq{"where" must be hash reference or DBIx::Custom::Where object} |
|
1641 |
. qq{or array reference, which contains where clause and parameter} |
|
1642 |
. _subname |
|
1643 |
unless ref $obj eq 'DBIx::Custom::Where'; |
|
1644 | ||
1645 |
$w->{param} = keys %$where_param |
|
1646 |
? $self->merge_param($where_param, $obj->param) |
|
1647 |
: $obj->param; |
|
1648 |
$w->{clause} = $obj->to_string; |
|
cleanup
|
1649 |
} |
1650 |
elsif ($where) { |
|
1651 |
$w->{clause} = "where $where"; |
|
cleanup
|
1652 |
$w->{param} = $where_param; |
cleanup
|
1653 |
} |
1654 |
|
|
1655 |
return $w; |
|
1656 |
} |
|
1657 | ||
updated pod
|
1658 |
sub _apply_filter { |
1659 |
my ($self, $table, @cinfos) = @_; |
|
1660 | ||
1661 |
# Initialize filters |
|
1662 |
$self->{filter} ||= {}; |
|
micro optimization
|
1663 |
$self->{filter}{on} = 1; |
updated pod
|
1664 |
$self->{filter}{out} ||= {}; |
1665 |
$self->{filter}{in} ||= {}; |
|
1666 |
$self->{filter}{end} ||= {}; |
|
1667 |
|
|
1668 |
# Usage |
|
1669 |
my $usage = "Usage: \$dbi->apply_filter(" . |
|
1670 |
"TABLE, COLUMN1, {in => INFILTER1, out => OUTFILTER1, end => ENDFILTER1}, " . |
|
1671 |
"COLUMN2, {in => INFILTER2, out => OUTFILTER2, end => ENDFILTER2}, ...)"; |
|
1672 |
|
|
1673 |
# Apply filter |
|
1674 |
for (my $i = 0; $i < @cinfos; $i += 2) { |
|
1675 |
|
|
1676 |
# Column |
|
1677 |
my $column = $cinfos[$i]; |
|
1678 |
if (ref $column eq 'ARRAY') { |
|
cleanup
|
1679 |
for my $c (@$column) { |
updated pod
|
1680 |
push @cinfos, $c, $cinfos[$i + 1]; |
1681 |
} |
|
1682 |
next; |
|
1683 |
} |
|
1684 |
|
|
1685 |
# Filter infomation |
|
1686 |
my $finfo = $cinfos[$i + 1] || {}; |
|
1687 |
croak "$usage (table: $table) " . _subname |
|
1688 |
unless ref $finfo eq 'HASH'; |
|
cleanup
|
1689 |
for my $ftype (keys %$finfo) { |
updated pod
|
1690 |
croak "$usage (table: $table) " . _subname |
1691 |
unless $ftype eq 'in' || $ftype eq 'out' || $ftype eq 'end'; |
|
1692 |
} |
|
1693 |
|
|
1694 |
# Set filters |
|
cleanup
|
1695 |
for my $way (qw/in out end/) { |
updated pod
|
1696 |
|
1697 |
# Filter |
|
1698 |
my $filter = $finfo->{$way}; |
|
1699 |
|
|
1700 |
# Filter state |
|
1701 |
my $state = !exists $finfo->{$way} ? 'not_exists' |
|
1702 |
: !defined $filter ? 'not_defined' |
|
1703 |
: ref $filter eq 'CODE' ? 'code' |
|
1704 |
: 'name'; |
|
1705 |
|
|
1706 |
# Filter is not exists |
|
1707 |
next if $state eq 'not_exists'; |
|
1708 |
|
|
1709 |
# Check filter name |
|
1710 |
croak qq{Filter "$filter" is not registered } . _subname |
|
1711 |
if $state eq 'name' |
|
1712 |
&& ! exists $self->filters->{$filter}; |
|
1713 |
|
|
1714 |
# Set filter |
|
1715 |
my $f = $state eq 'not_defined' ? undef |
|
1716 |
: $state eq 'code' ? $filter |
|
1717 |
: $self->filters->{$filter}; |
|
1718 |
$self->{filter}{$way}{$table}{$column} = $f; |
|
1719 |
$self->{filter}{$way}{$table}{"$table.$column"} = $f; |
|
1720 |
$self->{filter}{$way}{$table}{"${table}__$column"} = $f; |
|
1721 |
$self->{filter}{$way}{$table}{"${table}-$column"} = $f; |
|
1722 |
} |
|
1723 |
} |
|
1724 |
|
|
1725 |
return $self; |
|
1726 |
} |
|
1727 | ||
- update_param is DEPRECATED...
|
1728 |
# DEPRECATED! |
1729 |
has 'data_source'; |
|
1730 |
has dbi_options => sub { {} }; |
|
1731 |
has filter_check => 1; |
|
1732 |
has 'reserved_word_quote'; |
|
- dbi_option attribute is re...
|
1733 |
has dbi_option => sub { {} }; |
1734 |
has default_dbi_option => sub { |
|
1735 |
warn "default_dbi_option is DEPRECATED! use default_option instead"; |
|
1736 |
return shift->default_option; |
|
1737 |
}; |
|
1738 | ||
- DBIx::Custom::QueryBuilder...
|
1739 |
# DEPRECATED |
1740 |
sub tag_parse { |
|
1741 |
my $self = shift; |
|
1742 |
warn "tag_parse is DEPRECATED! use \$ENV{DBIX_CUSTOM_TAG_PARSE} " . |
|
1743 |
"environment variable"; |
|
1744 |
if (@_) { |
|
1745 |
$self->{tag_parse} = $_[0]; |
|
1746 |
return $self; |
|
1747 |
} |
|
1748 |
return $self->{tag_parse}; |
|
1749 |
} |
|
1750 | ||
- method method of DBIx::Cus...
|
1751 |
# DEPRECATED! |
1752 |
sub method { |
|
1753 |
warn "method is DEPRECATED! use helper instead"; |
|
1754 |
return shift->helper(@_); |
|
1755 |
} |
|
- update_param is DEPRECATED...
|
1756 | |
1757 |
# DEPRECATED! |
|
1758 |
sub assign_param { |
|
1759 |
my $self = shift; |
|
1760 |
warn "assing_param is DEPRECATED! use assign_clause instead"; |
|
1761 |
return $self->assign_clause(@_); |
|
1762 |
} |
|
1763 | ||
1764 |
# DEPRECATED |
|
1765 |
sub update_param { |
|
1766 |
my ($self, $param, $opts) = @_; |
|
1767 |
|
|
micro optimization
|
1768 |
warn "update_param is DEPRECATED! use assign_clause instead."; |
- update_param is DEPRECATED...
|
1769 |
|
1770 |
# Create update parameter tag |
|
1771 |
my $tag = $self->assign_clause($param, $opts); |
|
1772 |
$tag = "set $tag" unless $opts->{no_set}; |
|
1773 | ||
1774 |
return $tag; |
|
1775 |
} |
|
1776 | ||
updated pod
|
1777 |
# DEPRECATED! |
1778 |
sub create_query { |
|
1779 |
warn "create_query is DEPRECATED! use query option of each method"; |
|
1780 |
shift->_create_query(@_); |
|
1781 |
} |
|
1782 | ||
cleanup
|
1783 |
# DEPRECATED! |
1784 |
sub apply_filter { |
|
1785 |
my $self = shift; |
|
1786 |
|
|
- added EXPERIMENTAL order m...
|
1787 |
warn "apply_filter is DEPRECATED!"; |
cleanup
|
1788 |
return $self->_apply_filter(@_); |
1789 |
} |
|
1790 | ||
select_at is DEPRECATED! use...
|
1791 |
# DEPRECATED! |
1792 |
sub select_at { |
|
cleanup
|
1793 |
my ($self, %opt) = @_; |
select_at is DEPRECATED! use...
|
1794 | |
cleanup
|
1795 |
warn "select_at is DEPRECATED! use select method id option instead"; |
updated pod
|
1796 | |
cleanup
|
1797 |
# Options |
cleanup
|
1798 |
my $primary_keys = delete $opt{primary_key}; |
1799 |
my $where = delete $opt{where}; |
|
1800 |
my $param = delete $opt{param}; |
|
select_at is DEPRECATED! use...
|
1801 |
|
1802 |
# Table |
|
1803 |
croak qq{"table" option must be specified } . _subname |
|
cleanup
|
1804 |
unless $opt{table}; |
1805 |
my $table = ref $opt{table} ? $opt{table}->[-1] : $opt{table}; |
|
select_at is DEPRECATED! use...
|
1806 |
|
1807 |
# Create where parameter |
|
cleanup
|
1808 |
my $where_param = $self->_id_to_param($where, $primary_keys); |
select_at is DEPRECATED! use...
|
1809 |
|
cleanup
|
1810 |
return $self->select(where => $where_param, %opt); |
select_at is DEPRECATED! use...
|
1811 |
} |
1812 | ||
delete_at is DEPRECATED! use...
|
1813 |
# DEPRECATED! |
1814 |
sub delete_at { |
|
cleanup
|
1815 |
my ($self, %opt) = @_; |
updated pod
|
1816 | |
cleanup
|
1817 |
warn "delete_at is DEPRECATED! use delete method id option instead"; |
delete_at is DEPRECATED! use...
|
1818 |
|
cleanup
|
1819 |
# Options |
cleanup
|
1820 |
my $primary_keys = delete $opt{primary_key}; |
1821 |
my $where = delete $opt{where}; |
|
delete_at is DEPRECATED! use...
|
1822 |
|
1823 |
# Create where parameter |
|
cleanup
|
1824 |
my $where_param = $self->_id_to_param($where, $primary_keys); |
delete_at is DEPRECATED! use...
|
1825 |
|
cleanup
|
1826 |
return $self->delete(where => $where_param, %opt); |
delete_at is DEPRECATED! use...
|
1827 |
} |
1828 | ||
cleanup
|
1829 |
# DEPRECATED! |
1830 |
sub update_at { |
|
1831 |
my $self = shift; |
|
1832 | ||
cleanup
|
1833 |
warn "update_at is DEPRECATED! use update method id option instead"; |
cleanup
|
1834 |
|
cleanup
|
1835 |
# Options |
cleanup
|
1836 |
my $param; |
1837 |
$param = shift if @_ % 2; |
|
cleanup
|
1838 |
my %opt = @_; |
1839 |
my $primary_keys = delete $opt{primary_key}; |
|
1840 |
my $where = delete $opt{where}; |
|
1841 |
my $p = delete $opt{param} || {}; |
|
cleanup
|
1842 |
$param ||= $p; |
1843 |
|
|
1844 |
# Create where parameter |
|
cleanup
|
1845 |
my $where_param = $self->_id_to_param($where, $primary_keys); |
cleanup
|
1846 |
|
cleanup
|
1847 |
return $self->update(where => $where_param, param => $param, %opt); |
cleanup
|
1848 |
} |
1849 | ||
insert_at is DEPRECATED! add...
|
1850 |
# DEPRECATED! |
1851 |
sub insert_at { |
|
1852 |
my $self = shift; |
|
1853 |
|
|
cleanup
|
1854 |
warn "insert_at is DEPRECATED! use insert method id option instead"; |
insert_at is DEPRECATED! add...
|
1855 |
|
cleanup
|
1856 |
# Options |
insert_at is DEPRECATED! add...
|
1857 |
my $param; |
1858 |
$param = shift if @_ % 2; |
|
cleanup
|
1859 |
my %opt = @_; |
1860 |
my $primary_key = delete $opt{primary_key}; |
|
insert_at is DEPRECATED! add...
|
1861 |
$primary_key = [$primary_key] unless ref $primary_key; |
cleanup
|
1862 |
my $where = delete $opt{where}; |
1863 |
my $p = delete $opt{param} || {}; |
|
insert_at is DEPRECATED! add...
|
1864 |
$param ||= $p; |
1865 |
|
|
1866 |
# Create where parameter |
|
cleanup
|
1867 |
my $where_param = $self->_id_to_param($where, $primary_key); |
insert_at is DEPRECATED! add...
|
1868 |
$param = $self->merge_param($where_param, $param); |
1869 |
|
|
cleanup
|
1870 |
return $self->insert(param => $param, %opt); |
insert_at is DEPRECATED! add...
|
1871 |
} |
1872 | ||
added warnings
|
1873 |
# DEPRECATED! |
1874 |
sub register_tag { |
|
test cleanup
|
1875 |
my $self = shift; |
1876 |
|
|
added warnings
|
1877 |
warn "register_tag is DEPRECATED!"; |
test cleanup
|
1878 |
|
1879 |
# Merge tag |
|
1880 |
my $tags = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
1881 |
$self->{_tags} = {%{$self->{_tags} || {}}, %$tags}; |
|
1882 |
|
|
1883 |
return $self; |
|
1884 |
} |
|
1885 | ||
1886 |
# DEPRECATED! |
|
1887 |
sub register_tag_processor { |
|
1888 |
my $self = shift; |
|
1889 |
warn "register_tag_processor is DEPRECATED!"; |
|
1890 |
# Merge tag |
|
1891 |
my $tag_processors = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
1892 |
$self->{_tags} = {%{$self->{_tags} || {}}, %{$tag_processors}}; |
|
1893 |
return $self; |
|
added warnings
|
1894 |
} |
1895 | ||
cleanup
|
1896 |
# DEPRECATED! |
cleanup
|
1897 |
sub default_bind_filter { |
1898 |
my $self = shift; |
|
1899 |
|
|
cleanup
|
1900 |
warn "default_bind_filter is DEPRECATED!"; |
added warnings
|
1901 |
|
cleanup
|
1902 |
if (@_) { |
1903 |
my $fname = $_[0]; |
|
1904 |
|
|
1905 |
if (@_ && !$fname) { |
|
1906 |
$self->{default_out_filter} = undef; |
|
1907 |
} |
|
1908 |
else { |
|
many changed
|
1909 |
croak qq{Filter "$fname" is not registered} |
cleanup
|
1910 |
unless exists $self->filters->{$fname}; |
1911 |
|
|
1912 |
$self->{default_out_filter} = $self->filters->{$fname}; |
|
1913 |
} |
|
1914 |
return $self; |
|
1915 |
} |
|
1916 |
|
|
1917 |
return $self->{default_out_filter}; |
|
1918 |
} |
|
1919 | ||
cleanup
|
1920 |
# DEPRECATED! |
cleanup
|
1921 |
sub default_fetch_filter { |
1922 |
my $self = shift; |
|
added warnings
|
1923 | |
cleanup
|
1924 |
warn "default_fetch_filter is DEPRECATED!"; |
cleanup
|
1925 |
|
1926 |
if (@_) { |
|
many changed
|
1927 |
my $fname = $_[0]; |
1928 | ||
cleanup
|
1929 |
if (@_ && !$fname) { |
1930 |
$self->{default_in_filter} = undef; |
|
1931 |
} |
|
1932 |
else { |
|
many changed
|
1933 |
croak qq{Filter "$fname" is not registered} |
cleanup
|
1934 |
unless exists $self->filters->{$fname}; |
1935 |
|
|
1936 |
$self->{default_in_filter} = $self->filters->{$fname}; |
|
1937 |
} |
|
1938 |
|
|
1939 |
return $self; |
|
1940 |
} |
|
1941 |
|
|
many changed
|
1942 |
return $self->{default_in_filter}; |
cleanup
|
1943 |
} |
1944 | ||
- update_param is DEPRECATED...
|
1945 |
# DEPRECATED! |
1946 |
sub insert_param { |
|
1947 |
my $self = shift; |
|
1948 |
warn "insert_param is DEPRECATED! use values_clause instead"; |
|
1949 |
return $self->values_clause(@_); |
|
1950 |
} |
|
1951 | ||
- renamed update_param to up...
|
1952 |
# DEPRECATED! |
- update_param_tag is DEPREC...
|
1953 |
sub insert_param_tag { |
1954 |
warn "insert_param_tag is DEPRECATED! " . |
|
1955 |
"use insert_param instead!"; |
|
1956 |
return shift->insert_param(@_); |
|
- renamed update_param to up...
|
1957 |
} |
1958 | ||
1959 |
# DEPRECATED! |
|
- update_param_tag is DEPREC...
|
1960 |
sub update_param_tag { |
fixed DEPRECATED message bug
|
1961 |
warn "update_param_tag is DEPRECATED! " . |
- update_param_tag is DEPREC...
|
1962 |
"use update_param instead"; |
1963 |
return shift->update_param(@_); |
|
- renamed update_param to up...
|
1964 |
} |
cleanup
|
1965 |
# DEPRECATED! |
1966 |
sub _push_relation { |
|
1967 |
my ($self, $sql, $tables, $relation, $need_where) = @_; |
|
1968 |
|
|
1969 |
if (keys %{$relation || {}}) { |
|
micro optimization
|
1970 |
$$sql .= $need_where ? 'where ' : 'and '; |
cleanup
|
1971 |
for my $rcolumn (keys %$relation) { |
cleanup
|
1972 |
my $table1 = (split (/\./, $rcolumn))[0]; |
1973 |
my $table2 = (split (/\./, $relation->{$rcolumn}))[0]; |
|
1974 |
push @$tables, ($table1, $table2); |
|
micro optimization
|
1975 |
$$sql .= "$rcolumn = " . $relation->{$rcolumn} . 'and '; |
cleanup
|
1976 |
} |
1977 |
} |
|
micro optimization
|
1978 |
$$sql =~ s/and $/ /; |
cleanup
|
1979 |
} |
1980 | ||
1981 |
# DEPRECATED! |
|
1982 |
sub _add_relation_table { |
|
cleanup
|
1983 |
my ($self, $tables, $relation) = @_; |
cleanup
|
1984 |
|
1985 |
if (keys %{$relation || {}}) { |
|
cleanup
|
1986 |
for my $rcolumn (keys %$relation) { |
cleanup
|
1987 |
my $table1 = (split (/\./, $rcolumn))[0]; |
1988 |
my $table2 = (split (/\./, $relation->{$rcolumn}))[0]; |
|
1989 |
my $table1_exists; |
|
1990 |
my $table2_exists; |
|
cleanup
|
1991 |
for my $table (@$tables) { |
cleanup
|
1992 |
$table1_exists = 1 if $table eq $table1; |
1993 |
$table2_exists = 1 if $table eq $table2; |
|
1994 |
} |
|
1995 |
unshift @$tables, $table1 unless $table1_exists; |
|
1996 |
unshift @$tables, $table2 unless $table2_exists; |
|
1997 |
} |
|
1998 |
} |
|
1999 |
} |
|
2000 | ||
fixed DBIx::Custom::QueryBui...
|
2001 |
1; |
2002 | ||
removed reconnect method
|
2003 |
=head1 NAME |
2004 | ||
moved DBIx::Custom::Guide to...
|
2005 |
DBIx::Custom - DBI extension to execute insert, update, delete, and select easily |
removed reconnect method
|
2006 | |
fix heading typos
|
2007 |
=head1 SYNOPSIS |
cleanup
|
2008 | |
renamed build_query to creat...
|
2009 |
use DBIx::Custom; |
- remaned experimental safty...
|
2010 |
|
2011 |
# Connect |
|
2012 |
my $dbi = DBIx::Custom->connect( |
|
data_source is DEPRECATED! I...
|
2013 |
dsn => "dbi:mysql:database=dbname", |
- remaned experimental safty...
|
2014 |
user => 'ken', |
2015 |
password => '!LFKD%$&', |
|
- dbi_option attribute is re...
|
2016 |
option => {mysql_enable_utf8 => 1} |
- remaned experimental safty...
|
2017 |
); |
cleanup
|
2018 | |
removed reconnect method
|
2019 |
# Insert |
updated pod
|
2020 |
$dbi->insert({title => 'Perl', author => 'Ken'}, table => 'book'); |
removed reconnect method
|
2021 |
|
2022 |
# Update |
|
updated pod
|
2023 |
$dbi->update({title => 'Perl', author => 'Ken'}, table => 'book', |
2024 |
where => {id => 5}); |
|
removed reconnect method
|
2025 |
|
2026 |
# Delete |
|
updated pod
|
2027 |
$dbi->delete(table => 'book', where => {author => 'Ken'}); |
cleanup
|
2028 | |
removed reconnect method
|
2029 |
# Select |
updated pod
|
2030 |
my $result = $dbi->select(table => 'book', |
2031 |
column => ['title', 'author'], where => {author => 'Ken'}); |
|
cleanup
|
2032 | |
- remaned experimental safty...
|
2033 |
# Select, more complex |
2034 |
my $result = $dbi->select( |
|
2035 |
table => 'book', |
|
2036 |
column => [ |
|
cleanup
|
2037 |
{book => [qw/title author/]}, |
2038 |
{company => ['name']} |
|
- remaned experimental safty...
|
2039 |
], |
2040 |
where => {'book.author' => 'Ken'}, |
|
2041 |
join => ['left outer join company on book.company_id = company.id'], |
|
2042 |
append => 'order by id limit 5' |
|
removed reconnect method
|
2043 |
); |
- remaned experimental safty...
|
2044 |
|
removed register_format()
|
2045 |
# Fetch |
2046 |
while (my $row = $result->fetch) { |
|
- remaned experimental safty...
|
2047 |
|
removed register_format()
|
2048 |
} |
2049 |
|
|
- remaned experimental safty...
|
2050 |
# Fetch as hash |
removed register_format()
|
2051 |
while (my $row = $result->fetch_hash) { |
2052 |
|
|
2053 |
} |
|
2054 |
|
|
- remaned experimental safty...
|
2055 |
# Execute SQL with parameter. |
2056 |
$dbi->execute( |
|
- update_param_tag is DEPREC...
|
2057 |
"select id from book where author = :author and title like :title", |
updated pod
|
2058 |
{author => 'ken', title => '%Perl%'} |
- remaned experimental safty...
|
2059 |
); |
2060 |
|
|
fix heading typos
|
2061 |
=head1 DESCRIPTION |
removed reconnect method
|
2062 | |
cleanup
|
2063 |
L<DBIx::Custom> is L<DBI> wrapper module to execute SQL easily. |
updated pod
|
2064 |
This module have the following features. |
- remaned experimental safty...
|
2065 | |
updated pod
|
2066 |
=over 4 |
- remaned experimental safty...
|
2067 | |
cleanup
|
2068 |
=item * |
- remaned experimental safty...
|
2069 | |
cleanup
|
2070 |
Execute C<insert>, C<update>, C<delete>, or C<select> statement easily |
- remaned experimental safty...
|
2071 | |
cleanup
|
2072 |
=item * |
- remaned experimental safty...
|
2073 | |
cleanup
|
2074 |
Create C<where> clause flexibly |
- remaned experimental safty...
|
2075 | |
cleanup
|
2076 |
=item * |
- remaned experimental safty...
|
2077 | |
micro optimization
|
2078 |
Named place holder support |
2079 | ||
2080 |
=item * |
|
2081 | ||
cleanup
|
2082 |
Model support |
2083 | ||
2084 |
=item * |
|
2085 | ||
2086 |
Connection manager support |
|
2087 | ||
2088 |
=item * |
|
pod fix
|
2089 | |
cleanup
|
2090 |
Choice your favorite relational database management system, |
cleanup
|
2091 |
C<MySQL>, C<SQLite>, C<PostgreSQL>, C<Oracle>, |
2092 |
C<Microsoft SQL Server>, C<Microsoft Access>, C<DB2> or anything, |
|
2093 | ||
2094 |
=item * |
|
2095 | ||
- removed EXPERIMENTAL flag ...
|
2096 |
Filtering by data type or column name |
cleanup
|
2097 | |
2098 |
=item * |
|
2099 | ||
- removed EXPERIMENTAL flag ...
|
2100 |
Create C<order by> clause flexibly |
cleanup
|
2101 | |
2102 |
=back |
|
pod fix
|
2103 | |
fix heading typos
|
2104 |
=head1 DOCUMENTATION |
pod fix
|
2105 | |
cleanup
|
2106 |
L<DBIx::Custom::Guide> - How to use L<DBIx::Custom> |
pod fix
|
2107 | |
- remaned experimental safty...
|
2108 |
L<DBIx::Custom Wiki|https://github.com/yuki-kimoto/DBIx-Custom/wiki> |
cleanup
|
2109 |
- Theare are various examples. |
2110 | ||
micro optimization
|
2111 |
Module documentations - |
cleanup
|
2112 |
L<DBIx::Custom::Result>, |
2113 |
L<DBIx::Custom::Query>, |
|
2114 |
L<DBIx::Custom::Where>, |
|
2115 |
L<DBIx::Custom::Model>, |
|
2116 |
L<DBIx::Custom::Order> |
|
updated document
|
2117 | |
update document
|
2118 |
=head1 ATTRIBUTES |
packaging one directory
|
2119 | |
- removed DEPRECATED DBIx::C...
|
2120 |
=head2 C<connector> |
- removed EXPERIMENTAL Prefo...
|
2121 | |
2122 |
my $connector = $dbi->connector; |
|
micro optimization
|
2123 |
$dbi = $dbi->connector($connector); |
- removed EXPERIMENTAL Prefo...
|
2124 | |
micro optimization
|
2125 |
Connection manager object. if C<connector> is set, you can get C<dbh> |
2126 |
through connection manager. Conection manager object must have C<dbh> mehtod. |
|
- removed EXPERIMENTAL Prefo...
|
2127 | |
2128 |
This is L<DBIx::Connector> example. Please pass |
|
- dbi_option attribute is re...
|
2129 |
C<default_option> to L<DBIx::Connector> C<new> method. |
- removed EXPERIMENTAL Prefo...
|
2130 | |
2131 |
my $connector = DBIx::Connector->new( |
|
cleanup
|
2132 |
"dbi:mysql:database=$database", |
2133 |
$user, |
|
2134 |
$password, |
|
- dbi_option attribute is re...
|
2135 |
DBIx::Custom->new->default_option |
- removed EXPERIMENTAL Prefo...
|
2136 |
); |
2137 |
|
|
updated pod
|
2138 |
my $dbi = DBIx::Custom->connect(connector => $connector); |
- removed EXPERIMENTAL Prefo...
|
2139 | |
cleanup
|
2140 |
If C<connector> is set to 1 when connect method is called, |
2141 |
L<DBIx::Connector> is automatically set to C<connector> |
|
2142 | ||
2143 |
my $dbi = DBIx::Custom->connect( |
|
2144 |
dsn => $dsn, user => $user, password => $password, connector => 1); |
|
2145 |
|
|
2146 |
my $connector = $dbi->connector; # DBIx::Connector |
|
2147 | ||
2148 |
Note that L<DBIx::Connector> must be installed. |
|
2149 | ||
data_source is DEPRECATED! I...
|
2150 |
=head2 C<dsn> |
2151 | ||
2152 |
my $dsn = $dbi->dsn; |
|
reserved_word_quote is DEPRE...
|
2153 |
$dbi = $dbi->dsn("DBI:mysql:database=dbname"); |
packaging one directory
|
2154 | |
updated pod
|
2155 |
Data source name, used when C<connect> method is executed. |
removed DESTROY method(not b...
|
2156 | |
- dbi_option attribute is re...
|
2157 |
=head2 C<default_option> |
add default_dbi_option()
|
2158 | |
- dbi_option attribute is re...
|
2159 |
my $default_option = $dbi->default_option; |
2160 |
$dbi = $dbi->default_option($default_option); |
|
add default_dbi_option()
|
2161 | |
updated pod
|
2162 |
L<DBI> default option, used when C<connect> method is executed, |
- remaned experimental safty...
|
2163 |
default to the following values. |
add default_dbi_option()
|
2164 | |
- remaned experimental safty...
|
2165 |
{ |
2166 |
RaiseError => 1, |
|
2167 |
PrintError => 0, |
|
2168 |
AutoCommit => 1, |
|
2169 |
} |
|
packaging one directory
|
2170 | |
removed EXPERIMETNAL flag fr...
|
2171 |
=head2 C<exclude_table> |
2172 | ||
2173 |
my $exclude_table = $dbi->exclude_table; |
|
2174 |
$dbi = $dbi->exclude_table(qr/pg_/); |
|
2175 | ||
2176 |
Excluded table regex. |
|
2177 |
C<each_column>, C<each_table>, C<type_rule>, |
|
2178 |
and C<setup_model> methods ignore matching tables. |
|
2179 | ||
cleanup
|
2180 |
=head2 C<filters> |
bind_filter argument is chan...
|
2181 | |
cleanup
|
2182 |
my $filters = $dbi->filters; |
reserved_word_quote is DEPRE...
|
2183 |
$dbi = $dbi->filters(\%filters); |
packaging one directory
|
2184 | |
updated pod
|
2185 |
Filters, registered by C<register_filter> method. |
add models() attribute
|
2186 | |
- removed EXPERIMENTAL statu...
|
2187 |
=head2 C<last_sql> |
added EXPERIMENTAL last_sql ...
|
2188 | |
2189 |
my $last_sql = $dbi->last_sql; |
|
2190 |
$dbi = $dbi->last_sql($last_sql); |
|
2191 | ||
2192 |
Get last successed SQL executed by C<execute> method. |
|
2193 | ||
- id option work if id count...
|
2194 |
=head2 C<now> |
- insert timestamp option is...
|
2195 | |
2196 |
my $now = $dbi->now; |
|
2197 |
$dbi = $dbi->now($now); |
|
2198 | ||
- id option work if id count...
|
2199 |
Code reference which return current time, default to the following code reference. |
- insert timestamp option is...
|
2200 | |
2201 |
sub { |
|
2202 |
my ($sec, $min, $hour, $mday, $mon, $year) = localtime; |
|
2203 |
$mon++; |
|
2204 |
$year += 1900; |
|
2205 |
return sprintf("%04d-%02d-%02d %02d:%02d:%02d"); |
|
2206 |
} |
|
2207 | ||
- id option work if id count...
|
2208 |
This return the time like C<2011-10-14 05:05:27>. |
2209 | ||
2210 |
This is used by C<insert> method's C<created_at> option and C<updated_at> option, |
|
2211 |
and C<update> method's C<updated_at> option. |
|
- insert timestamp option is...
|
2212 | |
- removed DEPRECATED DBIx::C...
|
2213 |
=head2 C<models> |
add models() attribute
|
2214 | |
2215 |
my $models = $dbi->models; |
|
reserved_word_quote is DEPRE...
|
2216 |
$dbi = $dbi->models(\%models); |
add models() attribute
|
2217 | |
updated pod
|
2218 |
Models, included by C<include_model> method. |
add models() attribute
|
2219 | |
removed EXPERIMETNAL flag fr...
|
2220 |
=head2 C<option> |
2221 | ||
2222 |
my $option = $dbi->option; |
|
2223 |
$dbi = $dbi->option($option); |
|
2224 | ||
2225 |
L<DBI> option, used when C<connect> method is executed. |
|
2226 |
Each value in option override the value of C<default_option>. |
|
2227 | ||
cleanup
|
2228 |
=head2 C<password> |
2229 | ||
2230 |
my $password = $dbi->password; |
|
reserved_word_quote is DEPRE...
|
2231 |
$dbi = $dbi->password('lkj&le`@s'); |
cleanup
|
2232 | |
updated pod
|
2233 |
Password, used when C<connect> method is executed. |
update document
|
2234 | |
renamed update tag to update...
|
2235 |
=head2 C<query_builder> |
added commit method
|
2236 | |
test cleanup
|
2237 |
my $builder = $dbi->query_builder; |
added commit method
|
2238 | |
test cleanup
|
2239 |
Creat query builder. This is L<DBIx::Custom::QueryBuilder>. |
cleanup
|
2240 | |
reserved_word_quote is DEPRE...
|
2241 |
=head2 C<quote> |
added EXPERIMENTAL reserved_...
|
2242 | |
reserved_word_quote is DEPRE...
|
2243 |
my quote = $dbi->quote; |
2244 |
$dbi = $dbi->quote('"'); |
|
added EXPERIMENTAL reserved_...
|
2245 | |
DBIx::Custom::Model type att...
|
2246 |
Reserved word quote. |
2247 |
Default to double quote '"' except for mysql. |
|
2248 |
In mysql, default to back quote '`' |
|
added EXPERIMENTAL reserved_...
|
2249 | |
cleanup
|
2250 |
You can set quote pair. |
2251 | ||
2252 |
$dbi->quote('[]'); |
|
2253 | ||
cleanup
|
2254 |
=head2 C<result_class> |
cleanup
|
2255 | |
cleanup
|
2256 |
my $result_class = $dbi->result_class; |
reserved_word_quote is DEPRE...
|
2257 |
$dbi = $dbi->result_class('DBIx::Custom::Result'); |
cleanup
|
2258 | |
- remaned experimental safty...
|
2259 |
Result class, default to L<DBIx::Custom::Result>. |
cleanup
|
2260 | |
removed EXPERIMETNAL flag fr...
|
2261 |
=head2 C<safety_character> |
update pod
|
2262 | |
fixed pod
|
2263 |
my $safety_character = $dbi->safety_character; |
2264 |
$dbi = $dbi->safety_character($character); |
|
update pod
|
2265 | |
update pod
|
2266 |
Regex of safety character for table and column name, default to '\w'. |
cleanup
|
2267 |
Note that you don't have to specify like '[\w]'. |
update pod
|
2268 | |
cleanup test
|
2269 |
=head2 C<separator> |
2270 | ||
fixed pod
|
2271 |
my $separator = $dbi->separator; |
2272 |
$dbi = $dbi->separator('-'); |
|
improved join clause parsing
|
2273 | |
2274 |
Separator which join table name and column name. |
|
2275 |
This have effect to C<column> and C<mycolumn> method, |
|
2276 |
and C<select> method's column option. |
|
cleanup test
|
2277 | |
improved join clause parsing
|
2278 |
Default to C<.>. |
cleanup test
|
2279 | |
added tag_parse attribute
|
2280 |
=head2 C<tag_parse> |
2281 | ||
2282 |
my $tag_parse = $dbi->tag_parse(0); |
|
2283 |
$dbi = $dbi->tag_parse; |
|
2284 | ||
2285 |
Enable DEPRECATED tag parsing functionality, default to 1. |
|
2286 |
If you want to disable tag parsing functionality, set to 0. |
|
2287 | ||
cleanup
|
2288 |
=head2 C<user> |
cleanup
|
2289 | |
cleanup
|
2290 |
my $user = $dbi->user; |
reserved_word_quote is DEPRE...
|
2291 |
$dbi = $dbi->user('Ken'); |
cleanup
|
2292 | |
updated pod
|
2293 |
User name, used when C<connect> method is executed. |
update pod
|
2294 | |
- removed EXPERIMENTAL flag ...
|
2295 |
=head2 C<user_column_info> |
- added EXPERIMENTAL get_col...
|
2296 | |
2297 |
my $user_column_info = $dbi->user_column_info; |
|
2298 |
$dbi = $dbi->user_column_info($user_column_info); |
|
2299 | ||
improved join clause parsing
|
2300 |
You can set the date like the following one. |
- added EXPERIMENTAL get_col...
|
2301 | |
2302 |
[ |
|
2303 |
{table => 'book', column => 'title', info => {...}}, |
|
2304 |
{table => 'author', column => 'name', info => {...}} |
|
2305 |
] |
|
2306 | ||
improved join clause parsing
|
2307 |
Usually, you set return value of C<get_column_info>. |
- added EXPERIMENTAL get_col...
|
2308 | |
2309 |
my $user_column_info |
|
2310 |
= $dbi->get_column_info(exclude_table => qr/^system/); |
|
2311 |
$dbi->user_column_info($user_column_info); |
|
2312 | ||
2313 |
If C<user_column_info> is set, C<each_column> use C<user_column_info> |
|
- removed EXPERIMENTAL flag ...
|
2314 |
to find column info. this is very fast. |
- added EXPERIMENTAL get_col...
|
2315 | |
- removed EXPERIMENTAL flag ...
|
2316 |
=head2 C<user_table_info> |
added test
|
2317 | |
2318 |
my $user_table_info = $dbi->user_table_info; |
|
2319 |
$dbi = $dbi->user_table_info($user_table_info); |
|
2320 | ||
2321 |
You can set the following data. |
|
2322 | ||
2323 |
[ |
|
2324 |
{table => 'book', info => {...}}, |
|
2325 |
{table => 'author', info => {...}} |
|
2326 |
] |
|
2327 | ||
2328 |
Usually, you can set return value of C<get_table_info>. |
|
2329 | ||
2330 |
my $user_table_info = $dbi->get_table_info(exclude => qr/^system/); |
|
2331 |
$dbi->user_table_info($user_table_info); |
|
2332 | ||
2333 |
If C<user_table_info> is set, C<each_table> use C<user_table_info> |
|
2334 |
to find table info. |
|
2335 | ||
cleanup
|
2336 |
=head1 METHODS |
added commit method
|
2337 | |
cleanup
|
2338 |
L<DBIx::Custom> inherits all methods from L<Object::Simple> |
cleanup
|
2339 |
and use all methods of L<DBI> |
cleanup
|
2340 |
and implements the following new ones. |
added check_filter attribute
|
2341 | |
- removed EXPERIMENTAL flag ...
|
2342 |
=head2 C<available_datatype> |
changed type_rule arguments ...
|
2343 | |
added EXPERIMENTAL DBIx::Cus...
|
2344 |
print $dbi->available_datatype; |
changed type_rule arguments ...
|
2345 | |
added EXPERIMENTAL available...
|
2346 |
Get available data types. You can use these data types |
updated pod
|
2347 |
in C<type rule>'s C<from1> and C<from2> section. |
added EXPERIMENTAL available...
|
2348 | |
- removed EXPERIMENTAL flag ...
|
2349 |
=head2 C<available_typename> |
added EXPERIMENTAL available...
|
2350 | |
added EXPERIMENTAL DBIx::Cus...
|
2351 |
print $dbi->available_typename; |
added EXPERIMENTAL available...
|
2352 | |
2353 |
Get available type names. You can use these type names in |
|
updated pod
|
2354 |
C<type_rule>'s C<into1> and C<into2> section. |
changed type_rule arguments ...
|
2355 | |
- update_param is DEPRECATED...
|
2356 |
=head2 C<assign_clause> |
added EXPERIMENTAL assign_ta...
|
2357 | |
- update_param is DEPRECATED...
|
2358 |
my $assign_clause = $dbi->assign_clause({title => 'a', age => 2}); |
added EXPERIMENTAL assign_ta...
|
2359 | |
- update_param is DEPRECATED...
|
2360 |
Create assign clause |
added EXPERIMENTAL assign_ta...
|
2361 | |
- update_param_tag is DEPREC...
|
2362 |
title = :title, author = :author |
added EXPERIMENTAL assign_ta...
|
2363 | |
- update_param is DEPRECATED...
|
2364 |
This is used to create update clause. |
2365 | ||
2366 |
"update book set " . $dbi->assign_clause({title => 'a', age => 2}); |
|
added EXPERIMENTAL assign_ta...
|
2367 | |
added EXPERIMENTAL parameter...
|
2368 |
=head2 C<column> |
- select() EXPERIMETNAL colu...
|
2369 | |
cleanup
|
2370 |
my $column = $dbi->column(book => ['author', 'title']); |
- select() EXPERIMETNAL colu...
|
2371 | |
2372 |
Create column clause. The follwoing column clause is created. |
|
2373 | ||
2374 |
book.author as "book.author", |
|
2375 |
book.title as "book.title" |
|
2376 | ||
cleanup test
|
2377 |
You can change separator by C<separator> attribute. |
- select() EXPERIMETNAL colu...
|
2378 | |
cleanup
|
2379 |
# Separator is hyphen |
2380 |
$dbi->separator('-'); |
|
2381 |
|
|
2382 |
book.author as "book-author", |
|
2383 |
book.title as "book-title" |
|
2384 |
|
|
removed DBIx::Custom commit ...
|
2385 |
=head2 C<connect> |
packaging one directory
|
2386 | |
update pod
|
2387 |
my $dbi = DBIx::Custom->connect( |
data_source is DEPRECATED! I...
|
2388 |
dsn => "dbi:mysql:database=dbname", |
update pod
|
2389 |
user => 'ken', |
2390 |
password => '!LFKD%$&', |
|
- dbi_option attribute is re...
|
2391 |
option => {mysql_enable_utf8 => 1} |
update pod
|
2392 |
); |
2393 | ||
2394 |
Connect to the database and create a new L<DBIx::Custom> object. |
|
bind_filter argument is chan...
|
2395 | |
renamed build_query to creat...
|
2396 |
L<DBIx::Custom> is a wrapper of L<DBI>. |
cleanup
|
2397 |
C<AutoCommit> and C<RaiseError> options are true, |
update pod
|
2398 |
and C<PrintError> option is false by default. |
packaging one directory
|
2399 | |
- removed EXPERIMENTAL flag ...
|
2400 |
=head2 C<count> |
- added EXPERIMENTAL DBIx::C...
|
2401 | |
fixed id option bug when col...
|
2402 |
my $count = $dbi->count(table => 'book'); |
- added EXPERIMENTAL DBIx::C...
|
2403 | |
2404 |
Get rows count. |
|
2405 | ||
2406 |
Options is same as C<select> method's ones. |
|
2407 | ||
2408 |
=head2 C<create_model> |
|
removed EXPERIMETNAL flag fr...
|
2409 | |
adeed EXPERIMENTAL DBIx::Cus...
|
2410 |
my $model = $dbi->create_model( |
removed EXPERIMETNAL flag fr...
|
2411 |
table => 'book', |
2412 |
primary_key => 'id', |
|
2413 |
join => [ |
|
2414 |
'inner join company on book.comparny_id = company.id' |
|
2415 |
], |
|
2416 |
); |
|
2417 | ||
2418 |
Create L<DBIx::Custom::Model> object and initialize model. |
|
updated pod
|
2419 |
the module is also used from C<model> method. |
removed EXPERIMETNAL flag fr...
|
2420 | |
2421 |
$dbi->model('book')->select(...); |
|
2422 | ||
EXPERIMETAL fork safety impl...
|
2423 |
=head2 C<dbh> |
2424 | ||
2425 |
my $dbh = $dbi->dbh; |
|
2426 | ||
- removed EXPERIMENTAL Prefo...
|
2427 |
Get L<DBI> database handle. if C<connector> is set, you can get |
updated pod
|
2428 |
database handle through C<connector> object. |
removed EXPERIMETNAL flag fr...
|
2429 | |
added EXPERIMENTAL find_tabl...
|
2430 |
=head2 C<delete> |
2431 | ||
2432 |
$dbi->delete(table => 'book', where => {title => 'Perl'}); |
|
2433 | ||
2434 |
Execute delete statement. |
|
2435 | ||
2436 |
The following opitons are available. |
|
2437 | ||
cleanup
|
2438 |
B<OPTIONS> |
added EXPERIMENTAL find_tabl...
|
2439 | |
cleanup
|
2440 |
C<delete> method use all of C<execute> method's options, |
2441 |
and use the following new ones. |
|
added EXPERIMENTAL find_tabl...
|
2442 | |
cleanup
|
2443 |
=over 4 |
added EXPERIMENTAL find_tabl...
|
2444 | |
2445 |
=item C<id> |
|
2446 | ||
2447 |
id => 4 |
|
2448 |
id => [4, 5] |
|
2449 | ||
2450 |
ID corresponding to C<primary_key>. |
|
2451 |
You can delete rows by C<id> and C<primary_key>. |
|
2452 | ||
2453 |
$dbi->delete( |
|
fixed pod
|
2454 |
primary_key => ['id1', 'id2'], |
added EXPERIMENTAL find_tabl...
|
2455 |
id => [4, 5], |
2456 |
table => 'book', |
|
2457 |
); |
|
2458 | ||
2459 |
The above is same as the followin one. |
|
2460 | ||
2461 |
$dbi->delete(where => {id1 => 4, id2 => 5}, table => 'book'); |
|
2462 | ||
2463 |
=item C<prefix> |
|
2464 | ||
2465 |
prefix => 'some' |
|
2466 | ||
2467 |
prefix before table name section. |
|
2468 | ||
2469 |
delete some from book |
|
2470 | ||
2471 |
=item C<table> |
|
2472 | ||
2473 |
table => 'book' |
|
2474 | ||
2475 |
Table name. |
|
2476 | ||
2477 |
=item C<where> |
|
2478 | ||
2479 |
Same as C<select> method's C<where> option. |
|
2480 | ||
2481 |
=back |
|
2482 | ||
2483 |
=head2 C<delete_all> |
|
2484 | ||
2485 |
$dbi->delete_all(table => $table); |
|
2486 | ||
2487 |
Execute delete statement for all rows. |
|
2488 |
Options is same as C<delete>. |
|
2489 | ||
removed EXPERIMETNAL flag fr...
|
2490 |
=head2 C<each_column> |
2491 | ||
2492 |
$dbi->each_column( |
|
2493 |
sub { |
|
2494 |
my ($dbi, $table, $column, $column_info) = @_; |
|
2495 |
|
|
2496 |
my $type = $column_info->{TYPE_NAME}; |
|
2497 |
|
|
2498 |
if ($type eq 'DATE') { |
|
2499 |
# ... |
|
2500 |
} |
|
2501 |
} |
|
2502 |
); |
|
2503 | ||
improved pod
|
2504 |
Iterate all column informations in database. |
2505 |
Argument is callback which is executed when one column is found. |
|
2506 |
Callback receive four arguments. C<DBIx::Custom object>, C<table name>, |
|
2507 |
C<column name>, and C<column information>. |
|
2508 | ||
2509 |
If C<user_column_info> is set, C<each_column> method use C<user_column_info> |
|
2510 |
infromation, you can improve the performance of C<each_column> in |
|
2511 |
the following way. |
|
2512 | ||
2513 |
my $column_infos = $dbi->get_column_info(exclude_table => qr/^system_/); |
|
2514 |
$dbi->user_column_info($column_info); |
|
2515 |
$dbi->each_column(sub { ... }); |
|
EXPERIMETAL fork safety impl...
|
2516 | |
- removed EXPERIMENTAL statu...
|
2517 |
=head2 C<each_table> |
added EXPERIMENTAL each_tabl...
|
2518 | |
2519 |
$dbi->each_table( |
|
2520 |
sub { |
|
2521 |
my ($dbi, $table, $table_info) = @_; |
|
2522 |
|
|
2523 |
my $table_name = $table_info->{TABLE_NAME}; |
|
2524 |
} |
|
2525 |
); |
|
2526 | ||
improved pod
|
2527 |
Iterate all table informationsfrom in database. |
2528 |
Argument is callback which is executed when one table is found. |
|
2529 |
Callback receive three arguments, C<DBIx::Custom object>, C<table name>, |
|
2530 |
C<table information>. |
|
2531 | ||
2532 |
If C<user_table_info> is set, C<each_table> method use C<user_table_info> |
|
2533 |
infromation, you can improve the performance of C<each_table> in |
|
2534 |
the following way. |
|
2535 | ||
2536 |
my $table_infos = $dbi->get_table_info(exclude => qr/^system_/); |
|
2537 |
$dbi->user_table_info($table_info); |
|
2538 |
$dbi->each_table(sub { ... }); |
|
added EXPERIMENTAL each_tabl...
|
2539 | |
cleanup
|
2540 |
=head2 C<execute> |
packaging one directory
|
2541 | |
update pod
|
2542 |
my $result = $dbi->execute( |
updated pod
|
2543 |
"select * from book where title = :title and author like :author", |
2544 |
{title => 'Perl', author => '%Ken%'} |
|
2545 |
); |
|
2546 | ||
2547 |
my $result = $dbi->execute( |
|
2548 |
"select * from book where title = :book.title and author like :book.author", |
|
2549 |
{'book.title' => 'Perl', 'book.author' => '%Ken%'} |
|
update pod
|
2550 |
); |
2551 | ||
updated pod
|
2552 |
Execute SQL. SQL can contain column parameter such as :author and :title. |
2553 |
You can append table name to column name such as :book.title and :book.author. |
|
2554 |
Second argunet is data, embedded into column parameter. |
|
2555 |
Return value is L<DBIx::Custom::Result> object when select statement is executed, |
|
2556 |
or the count of affected rows when insert, update, delete statement is executed. |
|
update pod
|
2557 | |
I call :title named placehol...
|
2558 |
Named placeholder such as C<:title> is replaced by placeholder C<?>. |
added EXPERIMENTAL parameter...
|
2559 |
|
micro optimization
|
2560 |
# Original |
added EXPERIMENTAL parameter...
|
2561 |
select * from book where title = :title and author like :author |
2562 |
|
|
micro optimization
|
2563 |
# Replaced |
added EXPERIMENTAL parameter...
|
2564 |
select * from where title = ? and author like ?; |
update pod
|
2565 | |
I call :title named placehol...
|
2566 |
You can specify operator with named placeholder |
fixed pod
|
2567 |
by C<name{operator}> syntax. |
added EXPERIMENTAL parameter...
|
2568 | |
micro optimization
|
2569 |
# Original |
added EXPERIMENTAL parameter...
|
2570 |
select * from book where :title{=} and :author{like} |
2571 |
|
|
micro optimization
|
2572 |
# Replaced |
update pod
|
2573 |
select * from where title = ? and author like ?; |
2574 | ||
fixed named placeholder bug ...
|
2575 |
Note that colons in time format such as 12:13:15 is exeption, |
2576 |
it is not parsed as named placeholder. |
|
2577 |
If you want to use colon generally, you must escape it by C<\\> |
|
2578 | ||
2579 |
select * from where title = "aa\\:bb"; |
|
2580 | ||
cleanup
|
2581 |
B<OPTIONS> |
2582 | ||
updated pod
|
2583 |
The following opitons are available. |
update pod
|
2584 | |
2585 |
=over 4 |
|
2586 | ||
added EXPERIMENTAL reuse_sth...
|
2587 |
=item C<after_build_sql> |
2588 | ||
2589 |
You can filter sql after the sql is build. |
|
2590 | ||
2591 |
after_build_sql => $code_ref |
|
2592 | ||
2593 |
The following one is one example. |
|
2594 | ||
2595 |
$dbi->select( |
|
2596 |
table => 'book', |
|
2597 |
column => 'distinct(name)', |
|
2598 |
after_build_sql => sub { |
|
2599 |
"select count(*) from ($_[0]) as t1" |
|
2600 |
} |
|
2601 |
); |
|
2602 | ||
2603 |
The following SQL is executed. |
|
2604 | ||
2605 |
select count(*) from (select distinct(name) from book) as t1; |
|
2606 | ||
cleanup
|
2607 |
=item C<append> |
2608 | ||
2609 |
append => 'order by name' |
|
2610 | ||
2611 |
Append some statement after SQL. |
|
2612 | ||
added EXPERIMENTAL execute m...
|
2613 |
=item C<bind_type> |
2614 | ||
2615 |
Specify database bind data type. |
|
2616 | ||
2617 |
bind_type => [image => DBI::SQL_BLOB] |
|
2618 |
bind_type => [[qw/image audio/] => DBI::SQL_BLOB] |
|
2619 | ||
2620 |
This is used to bind parameter by C<bind_param> of statment handle. |
|
2621 | ||
2622 |
$sth->bind_param($pos, $value, DBI::SQL_BLOB); |
|
2623 | ||
update pod
|
2624 |
=item C<filter> |
updated pod
|
2625 |
|
2626 |
filter => { |
|
2627 |
title => sub { uc $_[0] } |
|
2628 |
author => sub { uc $_[0] } |
|
2629 |
} |
|
update pod
|
2630 | |
updated pod
|
2631 |
# Filter name |
2632 |
filter => { |
|
2633 |
title => 'upper_case', |
|
2634 |
author => 'upper_case' |
|
2635 |
} |
|
2636 |
|
|
2637 |
# At once |
|
2638 |
filter => [ |
|
2639 |
[qw/title author/] => sub { uc $_[0] } |
|
2640 |
] |
|
2641 | ||
separate DBIx::Custom type_r...
|
2642 |
Filter. You can set subroutine or filter name |
updated pod
|
2643 |
registered by by C<register_filter>. |
separate DBIx::Custom type_r...
|
2644 |
This filter is executed before data is saved into database. |
2645 |
and before type rule filter is executed. |
|
added EXPERIMENTAL execute()...
|
2646 | |
updated document
|
2647 |
=item C<query> |
2648 | ||
2649 |
query => 1 |
|
2650 | ||
DBIx::Custom::Query is DEPRE...
|
2651 |
C<execute> method return hash reference which contain SQL and column |
2652 |
infromation |
|
updated pod
|
2653 | |
DBIx::Custom::Query is DEPRE...
|
2654 |
my $sql = $query->{sql}; |
2655 |
my $columns = $query->{columns}; |
|
Added execute method's query...
|
2656 |
|
- DBIx::Custom::QueryBuilder...
|
2657 |
=item C<reuse> |
2658 |
|
|
2659 |
reuse => $hash_ref |
|
2660 | ||
2661 |
Reuse query object if the hash reference variable is set. |
|
2662 |
|
|
2663 |
my $queries = {}; |
|
2664 |
$dbi->execute($sql, $param, reuse => $queries); |
|
2665 | ||
2666 |
This will improved performance when you want to execute same query repeatedly |
|
2667 |
because generally creating query object is slow. |
|
2668 | ||
- removed placeholder count ...
|
2669 |
=item C<primary_key> |
2670 | ||
cleanup
|
2671 |
primary_key => 'id' |
2672 |
primary_key => ['id1', 'id2'] |
|
2673 | ||
execute method id option is ...
|
2674 |
Priamry key. This is used for C<id> option. |
- removed placeholder count ...
|
2675 | |
updated pod
|
2676 |
=item C<table> |
2677 |
|
|
2678 |
table => 'author' |
|
2679 | ||
updated pod
|
2680 |
If you want to omit table name in column name |
2681 |
and enable C<into1> and C<into2> type filter, |
|
2682 |
You must set C<table> option. |
|
updated pod
|
2683 | |
updated pod
|
2684 |
$dbi->execute("select * from book where title = :title and author = :author", |
2685 |
{title => 'Perl', author => 'Ken', table => 'book'); |
|
updated pod
|
2686 | |
updated pod
|
2687 |
# Same |
2688 |
$dbi->execute( |
|
2689 |
"select * from book where title = :book.title and author = :book.author", |
|
2690 |
{title => 'Perl', author => 'Ken'); |
|
added EXPERIMENTAL execute()...
|
2691 | |
- removed EXPERIMENTAL flag ...
|
2692 |
=item C<table_alias> |
added EXPERIMENTAL execute m...
|
2693 | |
cleanup
|
2694 |
table_alias => {user => 'worker'} |
added EXPERIMENTAL execute m...
|
2695 | |
2696 |
Table alias. Key is real table name, value is alias table name. |
|
2697 |
If you set C<table_alias>, you can enable C<into1> and C<into2> type rule |
|
2698 |
on alias table name. |
|
2699 | ||
- removed EXPERIMENTAL flag ...
|
2700 |
=item C<type_rule_off> |
added EXPERIMENTAL execute()...
|
2701 | |
2702 |
type_rule_off => 1 |
|
2703 | ||
EXPERIMENTAL type_rule argum...
|
2704 |
Turn C<into1> and C<into2> type rule off. |
2705 | ||
- removed EXPERIMENTAL flag ...
|
2706 |
=item C<type_rule1_off> |
EXPERIMENTAL type_rule argum...
|
2707 | |
2708 |
type_rule1_off => 1 |
|
2709 | ||
2710 |
Turn C<into1> type rule off. |
|
2711 | ||
- removed EXPERIMENTAL flag ...
|
2712 |
=item C<type_rule2_off> |
EXPERIMENTAL type_rule argum...
|
2713 | |
2714 |
type_rule2_off => 1 |
|
2715 | ||
2716 |
Turn C<into2> type rule off. |
|
update document
|
2717 | |
update pod
|
2718 |
=back |
version 0.0901
|
2719 | |
- removed EXPERIMENTAL flag ...
|
2720 |
=head2 C<get_column_info> |
- added EXPERIMENTAL get_col...
|
2721 | |
improved pod
|
2722 |
my $column_infos = $dbi->get_column_info(exclude_table => qr/^system_/); |
- added EXPERIMENTAL get_col...
|
2723 | |
2724 |
get column infomation except for one which match C<exclude_table> pattern. |
|
2725 | ||
2726 |
[ |
|
2727 |
{table => 'book', column => 'title', info => {...}}, |
|
2728 |
{table => 'author', column => 'name' info => {...}} |
|
2729 |
] |
|
2730 | ||
- removed EXPERIMENTAL flag ...
|
2731 |
=head2 C<get_table_info> |
added test
|
2732 | |
improved pod
|
2733 |
my $table_infos = $dbi->get_table_info(exclude => qr/^system_/); |
update pod
|
2734 | |
added test
|
2735 |
get table infomation except for one which match C<exclude> pattern. |
2736 | ||
2737 |
[ |
|
2738 |
{table => 'book', info => {...}}, |
|
2739 |
{table => 'author', info => {...}} |
|
2740 |
] |
|
add experimental update_at()...
|
2741 | |
added test
|
2742 |
You can set this value to C<user_table_info>. |
update pod
|
2743 | |
removed EXPERIMETNAL flag fr...
|
2744 |
=head2 C<helper> |
2745 | ||
2746 |
$dbi->helper( |
|
2747 |
find_or_create => sub { |
|
2748 |
my $self = shift; |
|
2749 |
|
|
2750 |
# Process |
|
update_or_insert method's re...
|
2751 |
}, |
2752 |
... |
|
removed EXPERIMETNAL flag fr...
|
2753 |
); |
2754 | ||
2755 |
Register helper. These helper is called directly from L<DBIx::Custom> object. |
|
2756 | ||
2757 |
$dbi->find_or_create; |
|
2758 | ||
cleanup
|
2759 |
=head2 C<insert> |
2760 | ||
cleanup
|
2761 |
$dbi->insert({title => 'Perl', author => 'Ken'}, table => 'book'); |
update pod
|
2762 | |
updated pod
|
2763 |
Execute insert statement. First argument is row data. Return value is |
2764 |
affected row count. |
|
update pod
|
2765 | |
insert and update method's p...
|
2766 |
If you want to set constant value to row data, use scalar reference |
2767 |
as parameter value. |
|
2768 | ||
2769 |
{date => \"NOW()"} |
|
2770 | ||
cleanup
|
2771 |
B<options> |
2772 | ||
cleanup
|
2773 |
C<insert> method use all of C<execute> method's options, |
cleanup
|
2774 |
and use the following new ones. |
update pod
|
2775 | |
cleanup
|
2776 |
=over 4 |
- insert, insert_at, update,...
|
2777 | |
- id option work if id count...
|
2778 |
=item C<created_at> |
added EXPERIMENTAL insert cr...
|
2779 | |
2780 |
created_at => 'created_datetime' |
|
2781 | ||
2782 |
Created timestamp column name. time when row is created is set to the column. |
|
2783 |
default time format is "YYYY-mm-dd HH:MM:SS", which can be changed by |
|
2784 |
C<now> attribute. |
|
2785 | ||
added EXPERIMENTAL execute()...
|
2786 |
=item C<id> |
2787 | ||
updated document
|
2788 |
id => 4 |
2789 |
id => [4, 5] |
|
update pod
|
2790 | |
updated document
|
2791 |
ID corresponding to C<primary_key>. |
2792 |
You can insert a row by C<id> and C<primary_key>. |
|
added EXPERIMENTAL execute()...
|
2793 | |
update pod
|
2794 |
$dbi->insert( |
updated document
|
2795 |
{title => 'Perl', author => 'Ken'} |
fixed pod
|
2796 |
primary_key => ['id1', 'id2'], |
added EXPERIMENTAL execute()...
|
2797 |
id => [4, 5], |
updated document
|
2798 |
table => 'book' |
update pod
|
2799 |
); |
added EXPERIMENTAL execute()...
|
2800 | |
updated document
|
2801 |
The above is same as the followin one. |
update pod
|
2802 | |
added EXPERIMENTAL execute()...
|
2803 |
$dbi->insert( |
updated document
|
2804 |
{id1 => 4, id2 => 5, title => 'Perl', author => 'Ken'}, |
2805 |
table => 'book' |
|
added EXPERIMENTAL execute()...
|
2806 |
); |
update pod
|
2807 | |
- removed EXPERIMENTAL statu...
|
2808 |
=item C<prefix> |
added EXPERIMENTAL insert, u...
|
2809 | |
2810 |
prefix => 'or replace' |
|
2811 | ||
2812 |
prefix before table name section |
|
2813 | ||
2814 |
insert or replace into book |
|
2815 | ||
updated document
|
2816 |
=item C<table> |
2817 | ||
2818 |
table => 'book' |
|
2819 | ||
2820 |
Table name. |
|
2821 | ||
- id option work if id count...
|
2822 |
=item C<updated_at> |
added EXPERIMENTAL insert cr...
|
2823 | |
2824 |
This option is same as C<update> method C<updated_at> option. |
|
2825 | ||
- removed EXPERIMENTAL flag ...
|
2826 |
=item C<wrap> |
updated pod
|
2827 | |
2828 |
wrap => {price => sub { "max($_[0])" }} |
|
2829 | ||
2830 |
placeholder wrapped string. |
|
2831 | ||
2832 |
If the following statement |
|
2833 | ||
2834 |
$dbi->insert({price => 100}, table => 'book', |
|
2835 |
{price => sub { "$_[0] + 5" }}); |
|
2836 | ||
2837 |
is executed, the following SQL is executed. |
|
2838 | ||
2839 |
insert into book price values ( ? + 5 ); |
|
2840 | ||
update pod
|
2841 |
=back |
2842 | ||
2843 |
=over 4 |
|
2844 | ||
- removed DEPRECATED DBIx::C...
|
2845 |
=head2 C<include_model> |
removed experimental base_ta...
|
2846 | |
update pod
|
2847 |
$dbi->include_model('MyModel'); |
removed experimental base_ta...
|
2848 | |
update pod
|
2849 |
Include models from specified namespace, |
2850 |
the following layout is needed to include models. |
|
removed experimental base_ta...
|
2851 | |
update pod
|
2852 |
lib / MyModel.pm |
2853 |
/ MyModel / book.pm |
|
2854 |
/ company.pm |
|
add feture. all model class ...
|
2855 | |
update pod
|
2856 |
Name space module, extending L<DBIx::Custom::Model>. |
add feture. all model class ...
|
2857 | |
update pod
|
2858 |
B<MyModel.pm> |
add feture. all model class ...
|
2859 | |
2860 |
package MyModel; |
|
updated pod
|
2861 |
use DBIx::Custom::Model -base; |
update pod
|
2862 |
|
2863 |
1; |
|
add feture. all model class ...
|
2864 | |
update pod
|
2865 |
Model modules, extending name space module. |
removed experimental base_ta...
|
2866 | |
update pod
|
2867 |
B<MyModel/book.pm> |
removed experimental base_ta...
|
2868 | |
update pod
|
2869 |
package MyModel::book; |
updated pod
|
2870 |
use MyModel -base; |
update pod
|
2871 |
|
2872 |
1; |
|
removed experimental base_ta...
|
2873 | |
update pod
|
2874 |
B<MyModel/company.pm> |
removed experimental base_ta...
|
2875 | |
update pod
|
2876 |
package MyModel::company; |
updated pod
|
2877 |
use MyModel -base; |
update pod
|
2878 |
|
2879 |
1; |
|
2880 |
|
|
updated pod
|
2881 |
MyModel::book and MyModel::company is included by C<include_model>. |
removed experimental base_ta...
|
2882 | |
updated pod
|
2883 |
You can get model object by C<model>. |
update pod
|
2884 | |
updated pod
|
2885 |
my $book_model = $dbi->model('book'); |
update pod
|
2886 |
my $company_model = $dbi->model('company'); |
removed experimental base_ta...
|
2887 | |
update pod
|
2888 |
See L<DBIx::Custom::Model> to know model features. |
2889 | ||
cleanup
|
2890 |
=head2 C<like_value> |
added EXPERIMENTAL like_valu...
|
2891 | |
2892 |
my $like_value = $dbi->like_value |
|
2893 | ||
cleanup
|
2894 |
Code reference which return a value for the like value. |
added EXPERIMENTAL like_valu...
|
2895 | |
2896 |
sub { "%$_[0]%" } |
|
2897 | ||
sqlfilter option is renamed ...
|
2898 |
=head2 C<mapper> |
- added EXPERIMENTAL pass at...
|
2899 | |
2900 |
my $mapper = $dbi->mapper(param => $param); |
|
2901 | ||
2902 |
Create a new L<DBIx::Custom::Mapper> object. |
|
2903 | ||
- removed DEPRECATED DBIx::C...
|
2904 |
=head2 C<merge_param> |
added EXPERIMENTAL updat_par...
|
2905 | |
2906 |
my $param = $dbi->merge_param({key1 => 1}, {key1 => 1, key2 => 2}); |
|
2907 | ||
cleanup
|
2908 |
Merge parameters. The following new parameter is created. |
added EXPERIMENTAL updat_par...
|
2909 | |
2910 |
{key1 => [1, 1], key2 => 2} |
|
2911 | ||
cleanup
|
2912 |
If same keys contains, the value is converted to array reference. |
2913 | ||
- removed DEPRECATED DBIx::C...
|
2914 |
=head2 C<model> |
update pod
|
2915 | |
2916 |
my $model = $dbi->model('book'); |
|
2917 | ||
cleanup
|
2918 |
Get a L<DBIx::Custom::Model> object |
2919 |
create by C<create_model> or C<include_model> |
|
update pod
|
2920 | |
- removed DEPRECATED DBIx::C...
|
2921 |
=head2 C<mycolumn> |
cleanup
|
2922 | |
fixed pod
|
2923 |
my $column = $dbi->mycolumn(book => ['author', 'title']); |
cleanup
|
2924 | |
2925 |
Create column clause for myself. The follwoing column clause is created. |
|
2926 | ||
2927 |
book.author as author, |
|
2928 |
book.title as title |
|
2929 | ||
added experimental not_exist...
|
2930 |
=head2 C<new> |
2931 | ||
update pod
|
2932 |
my $dbi = DBIx::Custom->new( |
data_source is DEPRECATED! I...
|
2933 |
dsn => "dbi:mysql:database=dbname", |
update pod
|
2934 |
user => 'ken', |
2935 |
password => '!LFKD%$&', |
|
- dbi_option attribute is re...
|
2936 |
option => {mysql_enable_utf8 => 1} |
update pod
|
2937 |
); |
added experimental not_exist...
|
2938 | |
2939 |
Create a new L<DBIx::Custom> object. |
|
2940 | ||
removed EXPERIMETNAL flag fr...
|
2941 |
=head2 C<not_exists> |
added experimental not_exist...
|
2942 | |
2943 |
my $not_exists = $dbi->not_exists; |
|
2944 | ||
update pod
|
2945 |
DBIx::Custom::NotExists object, indicating the column is not exists. |
cleanup
|
2946 |
This is used in C<param> of L<DBIx::Custom::Where> . |
experimental extended select...
|
2947 | |
- removed EXPERIMENTAL flag ...
|
2948 |
=head2 C<order> |
- added EXPERIMENTAL order m...
|
2949 | |
2950 |
my $order = $dbi->order; |
|
2951 | ||
2952 |
Create a new L<DBIx::Custom::Order> object. |
|
2953 | ||
- id option work if id count...
|
2954 |
=head2 C<q> |
added EXPERIMENTAL q method
|
2955 | |
2956 |
my $quooted = $dbi->q("title"); |
|
2957 | ||
2958 |
Quote string by value of C<quote>. |
|
2959 | ||
cleanup
|
2960 |
=head2 C<register_filter> |
2961 | ||
update pod
|
2962 |
$dbi->register_filter( |
2963 |
# Time::Piece object to database DATE format |
|
2964 |
tp_to_date => sub { |
|
2965 |
my $tp = shift; |
|
2966 |
return $tp->strftime('%Y-%m-%d'); |
|
2967 |
}, |
|
2968 |
# database DATE format to Time::Piece object |
|
2969 |
date_to_tp => sub { |
|
2970 |
my $date = shift; |
|
2971 |
return Time::Piece->strptime($date, '%Y-%m-%d'); |
|
2972 |
} |
|
2973 |
); |
|
cleanup
|
2974 |
|
update pod
|
2975 |
Register filters, used by C<filter> option of many methods. |
cleanup
|
2976 | |
removed DBIx::Custom commit ...
|
2977 |
=head2 C<select> |
added select() all_column op...
|
2978 | |
select method column option ...
|
2979 |
my $result = $dbi->select( |
added select() all_column op...
|
2980 |
column => ['author', 'title'], |
- select method can receive ...
|
2981 |
table => 'book', |
added select() all_column op...
|
2982 |
where => {author => 'Ken'}, |
select method column option ...
|
2983 |
); |
added select() all_column op...
|
2984 |
|
updated document
|
2985 |
Execute select statement. |
added select() all_column op...
|
2986 | |
- select method can receive ...
|
2987 |
You can pass odd number arguments. first argument is C<column>. |
2988 |
This is EXPERIMENTAL. |
|
2989 | ||
2990 |
my $result = $dbi->select(['author', 'title'], table => 'book'); |
|
2991 | ||
cleanup
|
2992 |
B<OPTIONS> |
added select() all_column op...
|
2993 | |
cleanup
|
2994 |
C<select> method use all of C<execute> method's options, |
2995 |
and use the following new ones. |
|
added EXPERIMENTAL execute m...
|
2996 | |
cleanup
|
2997 |
=over 4 |
added EXPERIMENTAL execute m...
|
2998 | |
added select() all_column op...
|
2999 |
=item C<column> |
3000 |
|
|
updated document
|
3001 |
column => 'author' |
3002 |
column => ['author', 'title'] |
|
3003 | ||
3004 |
Column clause. |
|
updated pod
|
3005 |
|
updated document
|
3006 |
if C<column> is not specified, '*' is set. |
added select() all_column op...
|
3007 | |
updated document
|
3008 |
column => '*' |
added select() all_column op...
|
3009 | |
- removed EXPERIMENTAL statu...
|
3010 |
You can specify hash of array reference. |
updated pod
|
3011 | |
updated document
|
3012 |
column => [ |
updated pod
|
3013 |
{book => [qw/author title/]}, |
3014 |
{person => [qw/name age/]} |
|
updated document
|
3015 |
] |
updated pod
|
3016 | |
updated pod
|
3017 |
This is expanded to the following one by using C<colomn> method. |
- select() column option can...
|
3018 | |
3019 |
book.author as "book.author", |
|
3020 |
book.title as "book.title", |
|
3021 |
person.name as "person.name", |
|
3022 |
person.age as "person.age" |
|
3023 | ||
- select method column optio...
|
3024 |
You can specify array of array reference, first argument is |
3025 |
column name, second argument is alias. |
|
- select() column option can...
|
3026 | |
updated document
|
3027 |
column => [ |
- select method column optio...
|
3028 |
['date(book.register_datetime)' => 'book.register_date'] |
updated document
|
3029 |
]; |
- select() column option can...
|
3030 | |
- select method column optio...
|
3031 |
Alias is quoted properly and joined. |
- select() column option can...
|
3032 | |
3033 |
date(book.register_datetime) as "book.register_date" |
|
updated pod
|
3034 | |
updated document
|
3035 |
=item C<id> |
3036 | ||
3037 |
id => 4 |
|
3038 |
id => [4, 5] |
|
3039 | ||
3040 |
ID corresponding to C<primary_key>. |
|
3041 |
You can select rows by C<id> and C<primary_key>. |
|
3042 | ||
3043 |
$dbi->select( |
|
fixed pod
|
3044 |
primary_key => ['id1', 'id2'], |
updated document
|
3045 |
id => [4, 5], |
3046 |
table => 'book' |
|
added select() all_column op...
|
3047 |
); |
3048 | ||
updated document
|
3049 |
The above is same as the followin one. |
3050 | ||
updated pod
|
3051 |
$dbi->select( |
updated document
|
3052 |
where => {id1 => 4, id2 => 5}, |
3053 |
table => 'book' |
|
where can recieve array refr...
|
3054 |
); |
3055 |
|
|
cleanup
|
3056 |
=item C<param> |
added select() all_column op...
|
3057 | |
updated document
|
3058 |
param => {'table2.key3' => 5} |
update pod
|
3059 | |
updated document
|
3060 |
Parameter shown before where clause. |
3061 |
|
|
3062 |
For example, if you want to contain tag in join clause, |
|
3063 |
you can pass parameter by C<param> option. |
|
update pod
|
3064 | |
updated document
|
3065 |
join => ['inner join (select * from table2 where table2.key3 = :table2.key3)' . |
3066 |
' as table2 on table1.key1 = table2.key1'] |
|
3067 | ||
- removed EXPERIMENTAL statu...
|
3068 |
=itme C<prefix> |
added EXPERIMENTAL select pr...
|
3069 | |
3070 |
prefix => 'SQL_CALC_FOUND_ROWS' |
|
3071 | ||
3072 |
Prefix of column cluase |
|
3073 | ||
3074 |
select SQL_CALC_FOUND_ROWS title, author from book; |
|
3075 | ||
updated document
|
3076 |
=item C<join> |
3077 | ||
3078 |
join => [ |
|
3079 |
'left outer join company on book.company_id = company_id', |
|
3080 |
'left outer join location on company.location_id = location.id' |
|
3081 |
] |
|
3082 |
|
|
3083 |
Join clause. If column cluase or where clause contain table name like "company.name", |
|
3084 |
join clausees needed when SQL is created is used automatically. |
|
update pod
|
3085 | |
3086 |
$dbi->select( |
|
3087 |
table => 'book', |
|
cleanup
|
3088 |
column => ['company.location_id as location_id'], |
update pod
|
3089 |
where => {'company.name' => 'Orange'}, |
3090 |
join => [ |
|
3091 |
'left outer join company on book.company_id = company.id', |
|
3092 |
'left outer join location on company.location_id = location.id' |
|
3093 |
] |
|
3094 |
); |
|
3095 | ||
updated document
|
3096 |
In above select, column and where clause contain "company" table, |
3097 |
the following SQL is created |
|
update pod
|
3098 | |
cleanup
|
3099 |
select company.location_id as location_id |
update pod
|
3100 |
from book |
3101 |
left outer join company on book.company_id = company.id |
|
cleanup
|
3102 |
where company.name = ?; |
update pod
|
3103 | |
added join new syntax
|
3104 |
You can specify two table by yourself. This is useful when join parser can't parse |
- removed EXPERIMENTAL flag ...
|
3105 |
the join clause correctly. |
added join new syntax
|
3106 | |
3107 |
$dbi->select( |
|
3108 |
table => 'book', |
|
3109 |
column => ['company.location_id as location_id'], |
|
3110 |
where => {'company.name' => 'Orange'}, |
|
3111 |
join => [ |
|
3112 |
{ |
|
3113 |
clause => 'left outer join location on company.location_id = location.id', |
|
3114 |
table => ['company', 'location'] |
|
3115 |
} |
|
3116 |
] |
|
3117 |
); |
|
3118 | ||
updated document
|
3119 |
=item C<table> |
updated pod
|
3120 | |
updated document
|
3121 |
table => 'book' |
updated pod
|
3122 | |
updated document
|
3123 |
Table name. |
updated pod
|
3124 | |
updated document
|
3125 |
=item C<where> |
3126 |
|
|
3127 |
# Hash refrence |
|
3128 |
where => {author => 'Ken', 'title' => 'Perl'} |
|
3129 |
|
|
3130 |
# DBIx::Custom::Where object |
|
3131 |
where => $dbi->where( |
|
- id option work if id count...
|
3132 |
clause => ['and', ':author{=}', ':title{like}'], |
updated document
|
3133 |
param => {author => 'Ken', title => '%Perl%'} |
3134 |
); |
|
updated pod
|
3135 |
|
- id option work if id count...
|
3136 |
# Array reference, this is same as above |
updated pod
|
3137 |
where => [ |
- id option work if id count...
|
3138 |
['and', ':author{=}', ':title{like}'], |
updated pod
|
3139 |
{author => 'Ken', title => '%Perl%'} |
- id option work if id count...
|
3140 |
]; |
updated pod
|
3141 |
|
3142 |
# String |
|
3143 |
where => 'title is null' |
|
update pod
|
3144 | |
cleanup
|
3145 |
Where clause. See L<DBIx::Custom::Where>. |
updated document
|
3146 |
|
update pod
|
3147 |
=back |
cleanup
|
3148 | |
removed EXPERIMETNAL flag fr...
|
3149 |
=head2 C<setup_model> |
3150 | ||
3151 |
$dbi->setup_model; |
|
3152 | ||
3153 |
Setup all model objects. |
|
3154 |
C<columns> of model object is automatically set, parsing database information. |
|
3155 | ||
3156 |
=head2 C<type_rule> |
|
3157 | ||
3158 |
$dbi->type_rule( |
|
3159 |
into1 => { |
|
3160 |
date => sub { ... }, |
|
3161 |
datetime => sub { ... } |
|
3162 |
}, |
|
3163 |
into2 => { |
|
3164 |
date => sub { ... }, |
|
3165 |
datetime => sub { ... } |
|
3166 |
}, |
|
3167 |
from1 => { |
|
3168 |
# DATE |
|
3169 |
9 => sub { ... }, |
|
3170 |
# DATETIME or TIMESTAMP |
|
3171 |
11 => sub { ... }, |
|
3172 |
} |
|
3173 |
from2 => { |
|
3174 |
# DATE |
|
3175 |
9 => sub { ... }, |
|
3176 |
# DATETIME or TIMESTAMP |
|
3177 |
11 => sub { ... }, |
|
3178 |
} |
|
3179 |
); |
|
3180 | ||
3181 |
Filtering rule when data is send into and get from database. |
|
3182 |
This has a little complex problem. |
|
3183 | ||
3184 |
In C<into1> and C<into2> you can specify |
|
3185 |
type name as same as type name defined |
|
3186 |
by create table, such as C<DATETIME> or C<DATE>. |
|
3187 | ||
3188 |
Note that type name and data type don't contain upper case. |
|
3189 |
If these contain upper case charactor, you convert it to lower case. |
|
3190 | ||
3191 |
C<into2> is executed after C<into1>. |
|
3192 | ||
3193 |
Type rule of C<into1> and C<into2> is enabled on the following |
|
3194 |
column name. |
|
3195 | ||
3196 |
=over 4 |
|
3197 | ||
3198 |
=item 1. column name |
|
3199 | ||
3200 |
issue_date |
|
3201 |
issue_datetime |
|
3202 | ||
3203 |
This need C<table> option in each method. |
|
3204 | ||
3205 |
=item 2. table name and column name, separator is dot |
|
3206 | ||
3207 |
book.issue_date |
|
3208 |
book.issue_datetime |
|
3209 | ||
3210 |
=back |
|
3211 | ||
3212 |
You get all type name used in database by C<available_typename>. |
|
3213 | ||
3214 |
print $dbi->available_typename; |
|
3215 | ||
3216 |
In C<from1> and C<from2> you specify data type, not type name. |
|
3217 |
C<from2> is executed after C<from1>. |
|
3218 |
You get all data type by C<available_datatype>. |
|
3219 | ||
3220 |
print $dbi->available_datatype; |
|
3221 | ||
3222 |
You can also specify multiple types at once. |
|
3223 | ||
3224 |
$dbi->type_rule( |
|
3225 |
into1 => [ |
|
3226 |
[qw/DATE DATETIME/] => sub { ... }, |
|
3227 |
], |
|
3228 |
); |
|
3229 | ||
cleanup
|
3230 |
=head2 C<update> |
removed reconnect method
|
3231 | |
updated document
|
3232 |
$dbi->update({title => 'Perl'}, table => 'book', where => {id => 4}); |
removed reconnect method
|
3233 | |
insert and update method's p...
|
3234 |
Execute update statement. First argument is update row data. |
3235 | ||
3236 |
If you want to set constant value to row data, use scalar reference |
|
3237 |
as parameter value. |
|
3238 | ||
3239 |
{date => \"NOW()"} |
|
added experimental update_pa...
|
3240 | |
cleanup
|
3241 |
B<OPTIONS> |
- insert, insert_at, update,...
|
3242 | |
cleanup
|
3243 |
C<update> method use all of C<execute> method's options, |
3244 |
and use the following new ones. |
|
- insert, insert_at, update,...
|
3245 | |
cleanup
|
3246 |
=over 4 |
- insert, insert_at, update,...
|
3247 | |
updated document
|
3248 |
=item C<id> |
- insert, insert_at, update,...
|
3249 | |
updated document
|
3250 |
id => 4 |
3251 |
id => [4, 5] |
|
- insert, insert_at, update,...
|
3252 | |
updated document
|
3253 |
ID corresponding to C<primary_key>. |
3254 |
You can update rows by C<id> and C<primary_key>. |
|
update pod
|
3255 | |
DEPRECATED select() param op...
|
3256 |
$dbi->update( |
updated document
|
3257 |
{title => 'Perl', author => 'Ken'} |
fixed pod
|
3258 |
primary_key => ['id1', 'id2'], |
updated document
|
3259 |
id => [4, 5], |
3260 |
table => 'book' |
|
where can recieve array refr...
|
3261 |
); |
update pod
|
3262 | |
updated document
|
3263 |
The above is same as the followin one. |
update pod
|
3264 | |
updated document
|
3265 |
$dbi->update( |
3266 |
{title => 'Perl', author => 'Ken'} |
|
3267 |
where => {id1 => 4, id2 => 5}, |
|
3268 |
table => 'book' |
|
3269 |
); |
|
update pod
|
3270 | |
- removed EXPERIMENTAL statu...
|
3271 |
=item C<prefix> |
added EXPERIMENTAL insert, u...
|
3272 | |
3273 |
prefix => 'or replace' |
|
3274 | ||
3275 |
prefix before table name section |
|
3276 | ||
3277 |
update or replace book |
|
3278 | ||
updated document
|
3279 |
=item C<table> |
update pod
|
3280 | |
updated document
|
3281 |
table => 'book' |
update pod
|
3282 | |
updated document
|
3283 |
Table name. |
update pod
|
3284 | |
added EXPERIMENTAL execute m...
|
3285 |
=item C<where> |
3286 | ||
3287 |
Same as C<select> method's C<where> option. |
|
3288 | ||
- removed EXPERIMENTAL flag ...
|
3289 |
=item C<wrap> |
updated pod
|
3290 | |
3291 |
wrap => {price => sub { "max($_[0])" }} |
|
3292 | ||
3293 |
placeholder wrapped string. |
|
3294 | ||
3295 |
If the following statement |
|
3296 | ||
3297 |
$dbi->update({price => 100}, table => 'book', |
|
3298 |
{price => sub { "$_[0] + 5" }}); |
|
3299 | ||
3300 |
is executed, the following SQL is executed. |
|
3301 | ||
3302 |
update book set price = ? + 5; |
|
3303 | ||
- id option work if id count...
|
3304 |
=item C<updated_at> |
added EXPERIMENTAL insert cr...
|
3305 | |
3306 |
updated_at => 'updated_datetime' |
|
3307 | ||
3308 |
Updated timestamp column name. time when row is updated is set to the column. |
|
3309 |
default time format is C<YYYY-mm-dd HH:MM:SS>, which can be changed by |
|
3310 |
C<now> attribute. |
|
3311 | ||
updated pod
|
3312 |
=back |
update pod
|
3313 | |
updated pod
|
3314 |
=head2 C<update_all> |
update pod
|
3315 | |
updated pod
|
3316 |
$dbi->update_all({title => 'Perl'}, table => 'book', ); |
update pod
|
3317 | |
updated document
|
3318 |
Execute update statement for all rows. |
updated pod
|
3319 |
Options is same as C<update> method. |
update pod
|
3320 | |
- id option work if id count...
|
3321 |
=head2 C<update_or_insert> |
- removed DEPRECATED status ...
|
3322 |
|
3323 |
# ID |
|
3324 |
$dbi->update_or_insert( |
|
3325 |
{title => 'Perl'}, |
|
3326 |
table => 'book', |
|
3327 |
id => 1, |
|
3328 |
primary_key => 'id', |
|
- EXPERIMENTAL update_or_ins...
|
3329 |
option => { |
3330 |
select => { |
|
3331 |
append => 'for update' |
|
3332 |
} |
|
3333 |
} |
|
- removed DEPRECATED status ...
|
3334 |
); |
- EXPERIMENTAL update_or_ins...
|
3335 | |
- removed DEPRECATED status ...
|
3336 |
Update or insert. |
3337 | ||
- EXPERIMENTAL update_or_ins...
|
3338 |
C<update_or_insert> method execute C<select> method first to find row. |
3339 |
If the row is exists, C<update> is executed. |
|
3340 |
If not, C<insert> is executed. |
|
- removed DEPRECATED status ...
|
3341 | |
- EXPERIMENTAL update_or_ins...
|
3342 |
C<OPTIONS> |
3343 | ||
3344 |
C<update_or_insert> method use all common option |
|
3345 |
in C<select>, C<update>, C<delete>, and has the following new ones. |
|
3346 | ||
3347 |
=over 4 |
|
3348 | ||
3349 |
=item C<option> |
|
3350 | ||
3351 |
option => { |
|
3352 |
select => { |
|
3353 |
append => '...' |
|
3354 |
}, |
|
3355 |
insert => { |
|
3356 |
prefix => '...' |
|
3357 |
}, |
|
3358 |
update => { |
|
3359 |
filter => {} |
|
3360 |
} |
|
3361 |
} |
|
- removed DEPRECATED status ...
|
3362 | |
- EXPERIMENTAL update_or_ins...
|
3363 |
If you want to pass option to each method, |
3364 |
you can use C<option> option. |
|
- removed DEPRECATED status ...
|
3365 | |
3366 |
=over 4 |
|
3367 | ||
3368 |
=item C<select_option> |
|
3369 | ||
3370 |
select_option => {append => 'for update'} |
|
3371 | ||
3372 |
select method option, |
|
3373 |
select method is used to check the row is already exists. |
|
3374 | ||
- removed EXPERIMENTAL flag ...
|
3375 |
=head2 C<show_datatype> |
update pod
|
3376 | |
3377 |
$dbi->show_datatype($table); |
|
3378 | ||
3379 |
Show data type of the columns of specified table. |
|
3380 | ||
3381 |
book |
|
3382 |
title: 5 |
|
3383 |
issue_date: 91 |
|
3384 | ||
3385 |
This data type is used in C<type_rule>'s C<from1> and C<from2>. |
|
3386 | ||
- removed EXPERIMENTAL the f...
|
3387 |
=head2 C<show_tables> |
test cleanup
|
3388 | |
3389 |
$dbi->show_tables; |
|
3390 | ||
3391 |
Show tables. |
|
3392 | ||
- removed EXPERIMENTAL flag ...
|
3393 |
=head2 C<show_typename> |
update pod
|
3394 | |
3395 |
$dbi->show_typename($table); |
|
3396 | ||
3397 |
Show type name of the columns of specified table. |
|
3398 | ||
3399 |
book |
|
3400 |
title: varchar |
|
3401 |
issue_date: date |
|
3402 | ||
3403 |
This type name is used in C<type_rule>'s C<into1> and C<into2>. |
|
3404 | ||
removed EXPERIMETNAL flag fr...
|
3405 |
=head2 C<values_clause> |
3406 | ||
3407 |
my $values_clause = $dbi->values_clause({title => 'a', age => 2}); |
|
3408 | ||
3409 |
Create values clause. |
|
3410 | ||
3411 |
(title, author) values (title = :title, age = :age); |
|
3412 | ||
3413 |
You can use this in insert statement. |
|
3414 | ||
3415 |
my $insert_sql = "insert into book $values_clause"; |
|
3416 | ||
3417 |
=head2 C<where> |
|
3418 | ||
3419 |
my $where = $dbi->where( |
|
3420 |
clause => ['and', 'title = :title', 'author = :author'], |
|
3421 |
param => {title => 'Perl', author => 'Ken'} |
|
3422 |
); |
|
3423 | ||
3424 |
Create a new L<DBIx::Custom::Where> object. |
|
3425 | ||
- added EXPERIMENTAL update_...
|
3426 |
=head1 ENVIRONMENTAL VARIABLES |
3427 | ||
3428 |
=head2 C<DBIX_CUSTOM_DEBUG> |
|
3429 | ||
3430 |
If environment variable C<DBIX_CUSTOM_DEBUG> is set to true, |
|
3431 |
executed SQL and bind values are printed to STDERR. |
|
3432 | ||
improved debug message
|
3433 |
=head2 C<DBIX_CUSTOM_DEBUG_ENCODING> |
3434 | ||
3435 |
DEBUG output encoding. Default to UTF-8. |
|
added environment variable D...
|
3436 | |
- fixed bug DBIx::Custom::Re...
|
3437 |
=head2 C<DBIX_CUSTOM_TAG_PARSE> |
3438 | ||
3439 |
If you set DBIX_CUSTOM_TAG_PARSE to 0, tag parsing is off. |
|
3440 | ||
3441 |
=head2 C<DBIX_CUSTOM_DISABLE_MODEL_EXECUTE> |
|
3442 | ||
3443 |
If you set DBIX_CUSTOM_DISABLE_MODEL_EXECUTE to 1, |
|
3444 |
L<DBIx::Custom::Model> execute method call L<DBIx::Custom> execute. |
|
3445 | ||
fix heading typos
|
3446 |
=head1 DEPRECATED FUNCTIONALITY |
- added EXPERIMENTAL order m...
|
3447 | |
3448 |
L<DBIx::Custom> |
|
3449 | ||
3450 |
# Attribute methods |
|
- DBIx::Custom::QueryBuilder...
|
3451 |
tag_parse # will be removed 2017/1/1 |
- dbi_option attribute is re...
|
3452 |
default_dbi_option # will be removed 2017/1/1 |
3453 |
dbi_option # will be removed 2017/1/1 |
|
added EXPERIMENTAL each_tabl...
|
3454 |
data_source # will be removed at 2017/1/1 |
3455 |
dbi_options # will be removed at 2017/1/1 |
|
3456 |
filter_check # will be removed at 2017/1/1 |
|
3457 |
reserved_word_quote # will be removed at 2017/1/1 |
|
3458 |
cache_method # will be removed at 2017/1/1 |
|
- added EXPERIMENTAL order m...
|
3459 |
|
3460 |
# Methods |
|
- insert timestamp option is...
|
3461 |
update_timestamp # will be removed at 2017/1/1 |
3462 |
insert_timestamp # will be removed at 2017/1/1 |
|
- method method of DBIx::Cus...
|
3463 |
method # will be removed at 2017/1/1 |
- update_param is DEPRECATED...
|
3464 |
assign_param # will be removed at 2017/1/1 |
3465 |
update_param # will be removed at 2017/1/1 |
|
3466 |
insert_param # will be removed at 2017/1/1 |
|
added EXPERIMENTAL each_tabl...
|
3467 |
create_query # will be removed at 2017/1/1 |
3468 |
apply_filter # will be removed at 2017/1/1 |
|
3469 |
select_at # will be removed at 2017/1/1 |
|
3470 |
delete_at # will be removed at 2017/1/1 |
|
3471 |
update_at # will be removed at 2017/1/1 |
|
3472 |
insert_at # will be removed at 2017/1/1 |
|
3473 |
register_tag # will be removed at 2017/1/1 |
|
3474 |
default_bind_filter # will be removed at 2017/1/1 |
|
3475 |
default_fetch_filter # will be removed at 2017/1/1 |
|
3476 |
insert_param_tag # will be removed at 2017/1/1 |
|
update pod
|
3477 |
register_tag # will be removed at 2017/1/1 |
added EXPERIMENTAL each_tabl...
|
3478 |
register_tag_processor # will be removed at 2017/1/1 |
3479 |
update_param_tag # will be removed at 2017/1/1 |
|
- added EXPERIMENTAL order m...
|
3480 |
|
3481 |
# Options |
|
- insert method created_at a...
|
3482 |
select column option [COLUMN => ALIAS] syntax # will be removed 2017/1/1 |
execute method id option is ...
|
3483 |
execute method id option # will be removed 2017/1/1 |
- insert timestamp option is...
|
3484 |
update timestamp option # will be removed 2017/1/1 |
3485 |
insert timestamp option # will be removed 2017/1/1 |
|
cleanup
|
3486 |
select method where_param option # will be removed 2017/1/1 |
cleanup
|
3487 |
delete method where_param option # will be removed 2017/1/1 |
cleanup
|
3488 |
update method where_param option # will be removed 2017/1/1 |
cleanup
|
3489 |
insert method param option # will be removed at 2017/1/1 |
insert method's id option is...
|
3490 |
insert method id option # will be removed at 2017/1/1 |
added EXPERIMENTAL each_tabl...
|
3491 |
select method relation option # will be removed at 2017/1/1 |
3492 |
select method column option [COLUMN, as => ALIAS] format |
|
3493 |
# will be removed at 2017/1/1 |
|
sqlfilter option is renamed ...
|
3494 |
execute method's sqlfilter option # will be removed at 2017/1/1 |
- added EXPERIMENTAL order m...
|
3495 |
|
3496 |
# Others |
|
micro optimization and
|
3497 |
execute($query, ...) # execute method receiving query object. |
3498 |
# this is removed at 2017/1/1 |
|
cleanup
|
3499 |
execute("select * from {= title}"); # execute method's |
3500 |
# tag parsing functionality |
|
added EXPERIMENTAL each_tabl...
|
3501 |
# will be removed at 2017/1/1 |
3502 |
Query caching # will be removed at 2017/1/1 |
|
- added EXPERIMENTAL order m...
|
3503 | |
3504 |
L<DBIx::Custom::Model> |
|
3505 | ||
DBIx::Custom::Query filters ...
|
3506 |
# Attribute methods |
DBIx::Custom::Model execute ...
|
3507 |
execute # will be removed at 2017/1/1 |
- method method of DBIx::Cus...
|
3508 |
method # will be removed at 2017/1/1 |
added EXPERIMENTAL each_tabl...
|
3509 |
filter # will be removed at 2017/1/1 |
3510 |
name # will be removed at 2017/1/1 |
|
3511 |
type # will be removed at 2017/1/1 |
|
- added EXPERIMENTAL order m...
|
3512 | |
3513 |
L<DBIx::Custom::Query> |
|
DBIx::Custom::Query is DEPRE...
|
3514 | |
3515 |
This module is DEPRECATED! # will be removed at 2017/1/1 |
|
- added EXPERIMENTAL order m...
|
3516 |
|
DBIx::Custom::Query filters ...
|
3517 |
# Attribute methods |
added EXPERIMENTAL each_tabl...
|
3518 |
default_filter # will be removed at 2017/1/1 |
DBIx::Custom::Query filters ...
|
3519 |
table # will be removed at 2017/1/1 |
3520 |
filters # will be removed at 2017/1/1 |
|
3521 |
|
|
3522 |
# Methods |
|
3523 |
filter # will be removed at 2017/1/1 |
|
- added EXPERIMENTAL order m...
|
3524 | |
3525 |
L<DBIx::Custom::QueryBuilder> |
|
- DBIx::Custom::QueryBuilder...
|
3526 | |
3527 |
This module is DEPRECATED! # will be removed at 2017/1/1 |
|
- added EXPERIMENTAL order m...
|
3528 |
|
DBIx::Custom::Query filters ...
|
3529 |
# Attribute methods |
added EXPERIMENTAL each_tabl...
|
3530 |
tags # will be removed at 2017/1/1 |
3531 |
tag_processors # will be removed at 2017/1/1 |
|
- added EXPERIMENTAL order m...
|
3532 |
|
DBIx::Custom::Query filters ...
|
3533 |
# Methods |
added EXPERIMENTAL each_tabl...
|
3534 |
register_tag # will be removed at 2017/1/1 |
3535 |
register_tag_processor # will be removed at 2017/1/1 |
|
- added EXPERIMENTAL order m...
|
3536 |
|
3537 |
# Others |
|
3538 |
build_query("select * from {= title}"); # tag parsing functionality |
|
added EXPERIMENTAL each_tabl...
|
3539 |
# will be removed at 2017/1/1 |
- added EXPERIMENTAL order m...
|
3540 | |
3541 |
L<DBIx::Custom::Result> |
|
3542 |
|
|
DBIx::Custom::Query filters ...
|
3543 |
# Attribute methods |
added EXPERIMENTAL each_tabl...
|
3544 |
filter_check # will be removed at 2017/1/1 |
- added EXPERIMENTAL order m...
|
3545 |
|
3546 |
# Methods |
|
- DBIx::Custom::Result filte...
|
3547 |
filter_on # will be removed at 2017/1/1 |
3548 |
filter_off # will be removed at 2017/1/1 |
|
added EXPERIMENTAL each_tabl...
|
3549 |
end_filter # will be removed at 2017/1/1 |
3550 |
remove_end_filter # will be removed at 2017/1/1 |
|
3551 |
remove_filter # will be removed at 2017/1/1 |
|
3552 |
default_filter # will be removed at 2017/1/1 |
|
- added EXPERIMENTAL order m...
|
3553 | |
3554 |
L<DBIx::Custom::Tag> |
|
3555 | ||
added EXPERIMENTAL each_tabl...
|
3556 |
This module is DEPRECATED! # will be removed at 2017/1/1 |
- added EXPERIMENTAL order m...
|
3557 | |
micro optimization
|
3558 |
L<DBIx::Custom::Order> |
3559 | ||
3560 |
# Other |
|
3561 |
prepend method array reference receiving |
|
3562 |
$order->prepend(['book', 'desc']); # will be removed 2017/1/1 |
|
3563 | ||
fix heading typos
|
3564 |
=head1 BACKWARDS COMPATIBILITY POLICY |
- added EXPERIMENTAL order m...
|
3565 | |
3566 |
If a functionality is DEPRECATED, you can know it by DEPRECATED warnings |
|
3567 |
except for attribute method. |
|
3568 |
You can check all DEPRECATED functionalities by document. |
|
3569 |
DEPRECATED functionality is removed after five years, |
|
3570 |
but if at least one person use the functionality and tell me that thing |
|
added EXPERIMENTAL each_tabl...
|
3571 |
I extend one year each time he tell me it. |
- added EXPERIMENTAL order m...
|
3572 | |
3573 |
EXPERIMENTAL functionality will be changed without warnings. |
|
DBIx::Custom is now stable
|
3574 | |
removed DESTROY method(not b...
|
3575 |
=head1 BUGS |
3576 | ||
renamed build_query to creat...
|
3577 |
Please tell me bugs if found. |
removed DESTROY method(not b...
|
3578 | |
3579 |
C<< <kimoto.yuki at gmail.com> >> |
|
3580 | ||
3581 |
L<http://github.com/yuki-kimoto/DBIx-Custom> |
|
3582 | ||
removed reconnect method
|
3583 |
=head1 AUTHOR |
3584 | ||
3585 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
|
version 0.0901
|
3586 | |
packaging one directory
|
3587 |
=head1 COPYRIGHT & LICENSE |
3588 | ||
cleanup
|
3589 |
Copyright 2009-2011 Yuki Kimoto, all rights reserved. |
packaging one directory
|
3590 | |
3591 |
This program is free software; you can redistribute it and/or modify it |
|
3592 |
under the same terms as Perl itself. |
|
3593 | ||
3594 |
=cut |