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