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