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