insert and update method's param can set constant value...
...by scalara reference
... | ... |
@@ -1,3 +1,6 @@ |
1 |
+0.1705 |
|
2 |
+ - insert and update method's param can set constant value by scalara reference |
|
3 |
+ such as {date => \"NOW()"} This is EXPERIMENTAL. |
|
1 | 4 |
0.1704 |
2 | 5 |
- added quote method's two character support like [] |
3 | 6 |
for Microsoft SQL Server and Access |
... | ... |
@@ -1,7 +1,7 @@ |
1 | 1 |
package DBIx::Custom; |
2 | 2 |
use Object::Simple -base; |
3 | 3 |
|
4 |
-our $VERSION = '0.1704'; |
|
4 |
+our $VERSION = '0.1705'; |
|
5 | 5 |
use 5.008001; |
6 | 6 |
|
7 | 7 |
use Carp 'croak'; |
... | ... |
@@ -84,12 +84,15 @@ sub assign_param { |
84 | 84 |
# Create set tag |
85 | 85 |
my @params; |
86 | 86 |
my $safety = $self->safety_character; |
87 |
- foreach my $column (keys %$param) { |
|
87 |
+ foreach my $column (sort keys %$param) { |
|
88 | 88 |
croak qq{"$column" is not safety column name } . _subname |
89 | 89 |
unless $column =~ /^[$safety\.]+$/; |
90 | 90 |
my $column_quote = $self->_q($column); |
91 | 91 |
$column_quote =~ s/\./$self->_q(".")/e; |
92 |
- push @params, "$column_quote = :$column"; |
|
92 |
+ push @params, ref $param->{$column} eq 'SCALAR' |
|
93 |
+ ? "$column_quote = " . ${$param->{$column}} |
|
94 |
+ : "$column_quote = :$column"; |
|
95 |
+ |
|
93 | 96 |
} |
94 | 97 |
my $tag = join(', ', @params); |
95 | 98 |
|
... | ... |
@@ -515,13 +518,14 @@ sub insert_param { |
515 | 518 |
my $safety = $self->safety_character; |
516 | 519 |
my @columns; |
517 | 520 |
my @placeholders; |
518 |
- foreach my $column (keys %$param) { |
|
521 |
+ foreach my $column (sort keys %$param) { |
|
519 | 522 |
croak qq{"$column" is not safety column name } . _subname |
520 | 523 |
unless $column =~ /^[$safety\.]+$/; |
521 | 524 |
my $column_quote = $self->_q($column); |
522 | 525 |
$column_quote =~ s/\./$self->_q(".")/e; |
523 | 526 |
push @columns, $column_quote; |
524 |
- push @placeholders, ":$column"; |
|
527 |
+ push @placeholders, ref $param->{$column} eq 'SCALAR' |
|
528 |
+ ? ${$param->{$column}} : ":$column"; |
|
525 | 529 |
} |
526 | 530 |
|
527 | 531 |
return '(' . join(', ', @columns) . ') ' . 'values ' . |
... | ... |
@@ -2346,6 +2350,11 @@ Options is same as C<delete>. |
2346 | 2350 |
Execute insert statement. First argument is row data. Return value is |
2347 | 2351 |
affected row count. |
2348 | 2352 |
|
2353 |
+If you want to set constant value to row data, use scalar reference |
|
2354 |
+as parameter value. |
|
2355 |
+ |
|
2356 |
+ {date => \"NOW()"} |
|
2357 |
+ |
|
2349 | 2358 |
The following opitons are available. |
2350 | 2359 |
|
2351 | 2360 |
=over 4 |
... | ... |
@@ -2911,7 +2920,12 @@ This option is for Oracle and SQL Server paging process. |
2911 | 2920 |
|
2912 | 2921 |
$dbi->update({title => 'Perl'}, table => 'book', where => {id => 4}); |
2913 | 2922 |
|
2914 |
-Execute update statement. First argument is update data. |
|
2923 |
+Execute update statement. First argument is update row data. |
|
2924 |
+ |
|
2925 |
+If you want to set constant value to row data, use scalar reference |
|
2926 |
+as parameter value. |
|
2927 |
+ |
|
2928 |
+ {date => \"NOW()"} |
|
2915 | 2929 |
|
2916 | 2930 |
The following opitons are available. |
2917 | 2931 |
|
... | ... |
@@ -307,6 +307,14 @@ $result = $dbi->execute($SELECT_SOURCES->{0}); |
307 | 307 |
$rows = $result->all; |
308 | 308 |
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic"); |
309 | 309 |
|
310 |
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
311 |
+$dbi->execute($CREATE_TABLE->{0}); |
|
312 |
+$dbi->insert(table => 'table1', param => {key1 => \"'1'", key2 => 2}); |
|
313 |
+$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
314 |
+$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
315 |
+$rows = $result->all; |
|
316 |
+is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic"); |
|
317 |
+ |
|
310 | 318 |
test 'update'; |
311 | 319 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
312 | 320 |
$dbi->execute($CREATE_TABLE->{1}); |
... | ... |
@@ -441,6 +449,17 @@ $result = $dbi->execute($SELECT_SOURCES->{0}); |
441 | 449 |
$rows = $result->all; |
442 | 450 |
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic"); |
443 | 451 |
|
452 |
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
453 |
+$dbi->execute($CREATE_TABLE->{1}); |
|
454 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
455 |
+$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
456 |
+$dbi->update(table => 'table1', param => {key2 => \"'11'"}, where => {key1 => 1}); |
|
457 |
+$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
458 |
+$rows = $result->all; |
|
459 |
+is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5}, |
|
460 |
+ {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
461 |
+ "basic"); |
|
462 |
+ |
|
444 | 463 |
test 'update_all'; |
445 | 464 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
446 | 465 |
$dbi->execute($CREATE_TABLE->{1}); |