added EXPERIMENTAL insert, update, and select method...
...prefix option
... | ... |
@@ -1,10 +1,9 @@ |
1 | 1 |
package DBIx::Custom; |
2 |
+use Object::Simple -base; |
|
2 | 3 |
|
3 | 4 |
our $VERSION = '0.1696'; |
4 | 5 |
use 5.008001; |
5 | 6 |
|
6 |
-use Object::Simple -base; |
|
7 |
- |
|
8 | 7 |
use Carp 'croak'; |
9 | 8 |
use DBI; |
10 | 9 |
use DBIx::Custom::Result; |
... | ... |
@@ -231,8 +230,8 @@ sub dbh { |
231 | 230 |
} |
232 | 231 |
} |
233 | 232 |
|
234 |
-our %DELETE_ARGS |
|
235 |
- = map { $_ => 1 } @COMMON_ARGS, qw/where append allow_delete_all where_param/; |
|
233 |
+our %DELETE_ARGS = map { $_ => 1 } @COMMON_ARGS, |
|
234 |
+ qw/where append allow_delete_all where_param prefix/; |
|
236 | 235 |
|
237 | 236 |
sub delete { |
238 | 237 |
my ($self, %args) = @_; |
... | ... |
@@ -257,6 +256,7 @@ sub delete { |
257 | 256 |
"must be specified when id is specified " . _subname |
258 | 257 |
if defined $id && !defined $primary_key; |
259 | 258 |
$primary_key = [$primary_key] unless ref $primary_key eq 'ARRAY'; |
259 |
+ my $prefix = delete $args{prefix}; |
|
260 | 260 |
|
261 | 261 |
# Where |
262 | 262 |
$where = $self->_create_param_from_id($id, $primary_key) if defined $id; |
... | ... |
@@ -277,8 +277,10 @@ sub delete { |
277 | 277 |
# Delete statement |
278 | 278 |
my @sql; |
279 | 279 |
my $q = $self->_quote; |
280 |
- push @sql, "delete from $q$table$q $where_clause"; |
|
281 |
- push @sql, $append if $append; |
|
280 |
+ push @sql, "delete"; |
|
281 |
+ push @sql, $prefix if defined $prefix; |
|
282 |
+ push @sql, "from $q$table$q $where_clause"; |
|
283 |
+ push @sql, $append if defined $append; |
|
282 | 284 |
my $sql = join(' ', @sql); |
283 | 285 |
|
284 | 286 |
# Execute query |
... | ... |
@@ -537,6 +539,7 @@ sub insert { |
537 | 539 |
"must be specified when id is specified " . _subname |
538 | 540 |
if defined $id && !defined $primary_key; |
539 | 541 |
$primary_key = [$primary_key] unless ref $primary_key eq 'ARRAY'; |
542 |
+ my $prefix = delete $args{prefix}; |
|
540 | 543 |
|
541 | 544 |
# Check arguments |
542 | 545 |
foreach my $name (keys %args) { |
... | ... |
@@ -555,8 +558,10 @@ sub insert { |
555 | 558 |
|
556 | 559 |
# Insert statement |
557 | 560 |
my @sql; |
558 |
- push @sql, "insert into $q$table$q " . $self->insert_param($param); |
|
559 |
- push @sql, $append if $append; |
|
561 |
+ push @sql, "insert"; |
|
562 |
+ push @sql, $prefix if defined $prefix; |
|
563 |
+ push @sql, "into $q$table$q " . $self->insert_param($param); |
|
564 |
+ push @sql, $append if defined $append; |
|
560 | 565 |
my $sql = join (' ', @sql); |
561 | 566 |
|
562 | 567 |
# Execute query |
... | ... |
@@ -757,10 +762,8 @@ sub register_filter { |
757 | 762 |
return $self; |
758 | 763 |
} |
759 | 764 |
|
760 |
-our %SELECT_ARGS |
|
761 |
- = map { $_ => 1 } @COMMON_ARGS, |
|
762 |
- qw/column where relation join param where_param wrap |
|
763 |
- prefix/; |
|
765 |
+our %SELECT_ARGS = map { $_ => 1 } @COMMON_ARGS, |
|
766 |
+ qw/column where relation join param where_param wrap prefix/; |
|
764 | 767 |
|
765 | 768 |
sub select { |
766 | 769 |
my ($self, %args) = @_; |
... | ... |
@@ -878,7 +881,7 @@ sub select { |
878 | 881 |
$self->_push_relation(\@sql, $tables, $relation, $where_clause eq '' ? 1 : 0); |
879 | 882 |
|
880 | 883 |
# Append |
881 |
- push @sql, $append if $append; |
|
884 |
+ push @sql, $append if defined $append; |
|
882 | 885 |
|
883 | 886 |
# Wrap |
884 | 887 |
if ($wrap) { |
... | ... |
@@ -1024,8 +1027,8 @@ sub type_rule { |
1024 | 1027 |
return $self->{type_rule} || {}; |
1025 | 1028 |
} |
1026 | 1029 |
|
1027 |
-our %UPDATE_ARGS |
|
1028 |
- = map { $_ => 1 } @COMMON_ARGS, qw/param where allow_update_all where_param/; |
|
1030 |
+our %UPDATE_ARGS = map { $_ => 1 } @COMMON_ARGS, |
|
1031 |
+ qw/param where allow_update_all where_param prefix/; |
|
1029 | 1032 |
|
1030 | 1033 |
sub update { |
1031 | 1034 |
my $self = shift; |
... | ... |
@@ -1039,9 +1042,9 @@ sub update { |
1039 | 1042 |
unless $table; |
1040 | 1043 |
my $p = delete $args{param} || {}; |
1041 | 1044 |
$param ||= $p; |
1042 |
- my $where = delete $args{where} || {}; |
|
1043 |
- my $where_param = delete $args{where_param} || {}; |
|
1044 |
- my $append = delete $args{append} || ''; |
|
1045 |
+ my $where = delete $args{where} || {}; |
|
1046 |
+ my $where_param = delete $args{where_param} || {}; |
|
1047 |
+ my $append = delete $args{append} || ''; |
|
1045 | 1048 |
my $allow_update_all = delete $args{allow_update_all}; |
1046 | 1049 |
my $id = delete $args{id}; |
1047 | 1050 |
my $primary_key = delete $args{primary_key}; |
... | ... |
@@ -1049,6 +1052,7 @@ sub update { |
1049 | 1052 |
"must be specified when id is specified " . _subname |
1050 | 1053 |
if defined $id && !defined $primary_key; |
1051 | 1054 |
$primary_key = [$primary_key] unless ref $primary_key eq 'ARRAY'; |
1055 |
+ my $prefix = delete $args{prefix}; |
|
1052 | 1056 |
|
1053 | 1057 |
# Check argument names |
1054 | 1058 |
foreach my $name (keys %args) { |
... | ... |
@@ -1081,8 +1085,10 @@ sub update { |
1081 | 1085 |
# Update statement |
1082 | 1086 |
my @sql; |
1083 | 1087 |
my $q = $self->_quote; |
1084 |
- push @sql, "update $q$table$q $update_clause $where_clause"; |
|
1085 |
- push @sql, $append if $append; |
|
1088 |
+ push @sql, "update"; |
|
1089 |
+ push @sql, $prefix if defined $prefix; |
|
1090 |
+ push @sql, "$q$table$q $update_clause $where_clause"; |
|
1091 |
+ push @sql, $append if defined $append; |
|
1086 | 1092 |
|
1087 | 1093 |
# SQL |
1088 | 1094 |
my $sql = join(' ', @sql); |
... | ... |
@@ -2174,6 +2180,14 @@ The above is same as the followin one. |
2174 | 2180 |
|
2175 | 2181 |
$dbi->delete(where => {id1 => 4, id2 => 5}, table => 'book'); |
2176 | 2182 |
|
2183 |
+=item C<prefix> EXPERIMENTAL |
|
2184 |
+ |
|
2185 |
+ prefix => 'some' |
|
2186 |
+ |
|
2187 |
+prefix before table name section. |
|
2188 |
+ |
|
2189 |
+ delete some from book |
|
2190 |
+ |
|
2177 | 2191 |
=item C<query> |
2178 | 2192 |
|
2179 | 2193 |
Same as C<execute> method's C<query> option. |
... | ... |
@@ -2259,6 +2273,14 @@ The above is same as the followin one. |
2259 | 2273 |
table => 'book' |
2260 | 2274 |
); |
2261 | 2275 |
|
2276 |
+=item C<prefix> EXPERIMENTAL |
|
2277 |
+ |
|
2278 |
+ prefix => 'or replace' |
|
2279 |
+ |
|
2280 |
+prefix before table name section |
|
2281 |
+ |
|
2282 |
+ insert or replace into book |
|
2283 |
+ |
|
2262 | 2284 |
=item C<primary_key> |
2263 | 2285 |
|
2264 | 2286 |
primary_key => 'id' |
... | ... |
@@ -2767,6 +2789,14 @@ If C<update> method's arguments is odd numbers, first argument is received as C< |
2767 | 2789 |
|
2768 | 2790 |
$dbi->update({title => 'Perl'}, table => 'book', where => {id => 2}); |
2769 | 2791 |
|
2792 |
+=item C<prefix> EXPERIMENTAL |
|
2793 |
+ |
|
2794 |
+ prefix => 'or replace' |
|
2795 |
+ |
|
2796 |
+prefix before table name section |
|
2797 |
+ |
|
2798 |
+ update or replace book |
|
2799 |
+ |
|
2770 | 2800 |
=item C<primary_key> |
2771 | 2801 |
|
2772 | 2802 |
primary_key => 'id' |
... | ... |
@@ -249,9 +249,6 @@ use C<execute> to execute SQL. |
249 | 249 |
my $sql = "select * from book where title = :title and author like :author;" |
250 | 250 |
$dbi->execute($sql, {title => 'Perl', author => '%Ken%'}); |
251 | 251 |
|
252 |
-You can specify values embedded into place holder as hash reference using |
|
253 |
-C<param> option. |
|
254 |
- |
|
255 | 252 |
You can specify C<filter> at C<execute>. |
256 | 253 |
|
257 | 254 |
$dbi->execute($sql, {title => 'Perl', author => '%Ken%'} |
... | ... |
@@ -299,6 +299,14 @@ $result = $dbi->execute($SELECT_SOURCES->{0}); |
299 | 299 |
$rows = $result->all; |
300 | 300 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic"); |
301 | 301 |
|
302 |
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
303 |
+$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))"); |
|
304 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
305 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}, prefix => 'or replace'); |
|
306 |
+$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
307 |
+$rows = $result->all; |
|
308 |
+is_deeply($rows, [{key1 => 1, key2 => 4}], "basic"); |
|
309 |
+ |
|
302 | 310 |
test 'update'; |
303 | 311 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
304 | 312 |
$dbi->execute($CREATE_TABLE->{1}); |
... | ... |
@@ -424,6 +432,15 @@ is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5}, |
424 | 432 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
425 | 433 |
"basic"); |
426 | 434 |
|
435 |
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
436 |
+$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))"); |
|
437 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
438 |
+$dbi->update(table => 'table1', param => {key2 => 4}, |
|
439 |
+ where => {key1 => 1}, prefix => 'or replace'); |
|
440 |
+$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
441 |
+$rows = $result->all; |
|
442 |
+is_deeply($rows, [{key1 => 1, key2 => 4}], "basic"); |
|
443 |
+ |
|
427 | 444 |
test 'update_all'; |
428 | 445 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
429 | 446 |
$dbi->execute($CREATE_TABLE->{1}); |
... | ... |
@@ -494,6 +511,14 @@ $dbi->delete( |
494 | 511 |
$result = $dbi->select(table => 'table1'); |
495 | 512 |
is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where'); |
496 | 513 |
|
514 |
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
515 |
+$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))"); |
|
516 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
517 |
+$dbi->delete(table => 'table1', where => {key1 => 1}, prefix => ' '); |
|
518 |
+$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
519 |
+$rows = $result->all; |
|
520 |
+is_deeply($rows, [], "basic"); |
|
521 |
+ |
|
497 | 522 |
test 'delete error'; |
498 | 523 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
499 | 524 |
$dbi->execute($CREATE_TABLE->{0}); |