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