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