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