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