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