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