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