| ... | ... |
@@ -1999,7 +1999,7 @@ Create assign parameter. |
| 1999 | 1999 |
|
| 2000 | 2000 |
This is equal to C<update_param> exept that set is not added. |
| 2001 | 2001 |
|
| 2002 |
-=head2 C<column> EXPERIMETNAL |
|
| 2002 |
+=head2 C<column> |
|
| 2003 | 2003 |
|
| 2004 | 2004 |
my $column = $dbi->column(book => ['author', 'title']); |
| 2005 | 2005 |
|
| ... | ... |
@@ -2112,7 +2112,20 @@ Return value is L<DBIx::Custom::Result> object when select statement is executed |
| 2112 | 2112 |
or the count of affected rows when insert, update, delete statement is executed. |
| 2113 | 2113 |
|
| 2114 | 2114 |
Parameter is replaced by placeholder C<?>. |
| 2115 |
+ |
|
| 2116 |
+ # Before |
|
| 2117 |
+ select * from book where title = :title and author like :author |
|
| 2118 |
+ |
|
| 2119 |
+ # After |
|
| 2120 |
+ select * from where title = ? and author like ?; |
|
| 2115 | 2121 |
|
| 2122 |
+You can specify operator with parameter by C<name{operator}> syntax.
|
|
| 2123 |
+This is EXPERIMENTAL. |
|
| 2124 |
+ |
|
| 2125 |
+ # Before |
|
| 2126 |
+ select * from book where :title{=} and :author{like}
|
|
| 2127 |
+ |
|
| 2128 |
+ # After |
|
| 2116 | 2129 |
select * from where title = ? and author like ?; |
| 2117 | 2130 |
|
| 2118 | 2131 |
The following opitons are available. |
| ... | ... |
@@ -74,10 +74,10 @@ sub _parse_parameter {
|
| 74 | 74 |
my $columns = []; |
| 75 | 75 |
my $c = $self->safety_character; |
| 76 | 76 |
# Parameter regex |
| 77 |
- my $re = qr/^(.*?):([$c\.]+)(.*)/s; |
|
| 77 |
+ my $re = qr/^(.*?):([$c\.]+)(?:\{(.*?)\})?(.*)/s;
|
|
| 78 | 78 |
while ($sql =~ /$re/g) {
|
| 79 | 79 |
push @$columns, $2; |
| 80 |
- $sql = "$1?$3"; |
|
| 80 |
+ $sql = defined $3 ? "$1$2 $3 ?$4" : "$1?$4"; |
|
| 81 | 81 |
} |
| 82 | 82 |
|
| 83 | 83 |
# Create query |
| ... | ... |
@@ -3417,4 +3417,34 @@ test 'DBIx::Custom header'; |
| 3417 | 3417 |
|
| 3418 | 3418 |
} |
| 3419 | 3419 |
|
| 3420 |
+test 'parameter :name(operater) syntax'; |
|
| 3421 |
+$dbi->execute($DROP_TABLE->{0});
|
|
| 3422 |
+$dbi->execute($CREATE_TABLE->{1});
|
|
| 3423 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
|
|
| 3424 |
+$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
|
|
| 3425 |
+ |
|
| 3426 |
+$source = "select * from table1 where :key1{=} and :key2{=}";
|
|
| 3427 |
+$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
|
|
| 3428 |
+$rows = $result->all; |
|
| 3429 |
+is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
|
|
| 3430 |
+ |
|
| 3431 |
+$source = "select * from table1 where :key1{ = } and :key2{=}";
|
|
| 3432 |
+$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
|
|
| 3433 |
+$rows = $result->all; |
|
| 3434 |
+is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
|
|
| 3435 |
+ |
|
| 3436 |
+$source = "select * from table1 where :key1{<} and :key2{=}";
|
|
| 3437 |
+$result = $dbi->execute($source, param => {key1 => 5, key2 => 2});
|
|
| 3438 |
+$rows = $result->all; |
|
| 3439 |
+is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
|
|
| 3440 |
+ |
|
| 3441 |
+$source = "select * from table1 where :table1.key1{=} and :table1.key2{=}";
|
|
| 3442 |
+$result = $dbi->execute( |
|
| 3443 |
+ $source, |
|
| 3444 |
+ param => {'table1.key1' => 1, 'table1.key2' => 1},
|
|
| 3445 |
+ filter => {'table1.key2' => sub { $_[0] * 2 }}
|
|
| 3446 |
+); |
|
| 3447 |
+$rows = $result->all; |
|
| 3448 |
+is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
|
|
| 3449 |
+ |
|
| 3420 | 3450 |
=cut |