Showing 3 changed files with 133 additions and 16 deletions
+1
Changes
... ...
@@ -1,4 +1,5 @@
1 1
 0.1667
2
+    - added EXPERIMENTAL updat_param no_set option.
2 3
     - added EXPERIMENTAL reserved_word_quote attribute.
3 4
 0.1666
4 5
     - removed from cache() and cache_method() document for a while and cache() value
+72 -15
lib/DBIx/Custom.pm
... ...
@@ -1,6 +1,6 @@
1 1
 package DBIx::Custom;
2 2
 
3
-our $VERSION = '0.1667';
3
+our $VERSION = '0.1668';
4 4
 
5 5
 use 5.008001;
6 6
 use strict;
... ...
@@ -655,17 +655,25 @@ sub insert_param {
655 655
     my ($self, $param) = @_;
656 656
     
657 657
     # Insert parameter tag
658
-    my @tag;
659
-    push @tag, '{insert_param';
658
+    my @names;
659
+    my @placeholders;
660
+    
660 661
     my $safety = $self->safety_character;
662
+    my $q = $self->reserved_word_quote;
663
+    
661 664
     foreach my $column (keys %$param) {
662 665
         croak qq{"$column" is not safety column name}
663 666
           unless $column =~ /^[$safety\.]+$/;
664
-        push @tag, $column;
667
+        
668
+        my $c = "$q$column$q";
669
+        $c =~ s/\./$q.$q/;
670
+        
671
+        push @names, $c;
672
+        push @placeholders, "{? $c}";
665 673
     }
666
-    push @tag, '}';
667 674
     
668
-    return join ' ', @tag;
675
+    return '(' . join(', ', @names) . ') ' . 'values' .
676
+           ' (' . join(', ', @placeholders) . ')';
669 677
 }
670 678
 
671 679
 sub include_model {
... ...
@@ -731,6 +739,27 @@ sub include_model {
731 739
     return $self;
732 740
 }
733 741
 
742
+sub merge_param {
743
+    my ($self, @params) = @_;
744
+    
745
+    my $param = {};
746
+    
747
+    foreach my $p (@params) {
748
+        foreach my $column (keys %$p) {
749
+            if (exists $param->{$column}) {
750
+                $param->{$column} = [$param->{$column}]
751
+                  unless ref $param->{$column} eq 'ARRAY';
752
+                push @{$param->{$column}}, $p->{$column};
753
+            }
754
+            else {
755
+                $param->{$column} = $p->{$column};
756
+            }
757
+        }
758
+    }
759
+    
760
+    return $param;
761
+}
762
+
734 763
 sub method {
735 764
     my $self = shift;
736 765
     
... ...
@@ -1150,20 +1179,29 @@ sub update_at {
1150 1179
 }
1151 1180
 
1152 1181
 sub update_param {
1153
-    my ($self, $param) = @_;
1182
+    my ($self, $param, $opt) = @_;
1183
+    
1184
+    # Insert parameter tag
1185
+    my @params;
1154 1186
     
1155
-    # Update parameter tag
1156
-    my @tag;
1157
-    push @tag, '{update_param';
1158 1187
     my $safety = $self->safety_character;
1188
+    my $q = $self->reserved_word_quote;
1189
+    
1159 1190
     foreach my $column (keys %$param) {
1160 1191
         croak qq{"$column" is not safety column name}
1161 1192
           unless $column =~ /^[$safety\.]+$/;
1162
-        push @tag, $column;
1193
+        
1194
+        my $c = "$q$column$q";
1195
+        $c =~ s/\./$q.$q/;
1196
+        
1197
+        push @params, "$c = {? $c}";
1163 1198
     }
1164
-    push @tag, '}';
1165 1199
     
1166
-    return join ' ', @tag;
1200
+    my $clause;
1201
+    $clause .= 'set ' unless $opt->{no_set};
1202
+    $clause .= join(', ', @params);
1203
+    
1204
+    return $clause;
1167 1205
 }
1168 1206
 
1169 1207
 sub where {
... ...
@@ -2140,7 +2178,7 @@ Place holders are set to 5 and 'Perl'.
2140 2178
 
2141 2179
 Create insert parameter tag.
2142 2180
 
2143
-    {insert_param title age}
2181
+    (title, author) values ({? title}, {? author});
2144 2182
 
2145 2183
 =head2 C<include_model> EXPERIMENTAL
2146 2184
 
... ...
@@ -2190,6 +2228,16 @@ You can get model object by C<model()>.
2190 2228
 
2191 2229
 See L<DBIx::Custom::Model> to know model features.
2192 2230
 
2231
+=head2 C<merge_param> EXPERIMENTAL
2232
+
2233
+    my $param = $dbi->merge_param({key1 => 1}, {key1 => 1, key2 => 2});
2234
+
2235
+Merge paramters.
2236
+
2237
+$param:
2238
+
2239
+    {key1 => [1, 1], key2 => 2}
2240
+
2193 2241
 =head2 C<method> EXPERIMENTAL
2194 2242
 
2195 2243
     $dbi->method(
... ...
@@ -2699,7 +2747,16 @@ Place holders are set to 'Perl' and 5.
2699 2747
 
2700 2748
 Create update parameter tag.
2701 2749
 
2702
-    {update_param title age}
2750
+    set title = {? title}, author = {? author}
2751
+
2752
+You can create tag without 'set ' by C<no_set> option. This option is EXPERIMENTAL.
2753
+
2754
+    my $update_param = $dbi->update_param(
2755
+        {title => 'a', age => 2}
2756
+        {no_set => 1}
2757
+    );
2758
+
2759
+    title = {? title}, author = {? author}
2703 2760
 
2704 2761
 =head2 C<where>
2705 2762
 
+60 -1
t/dbix-custom-core-sqlite.t
... ...
@@ -1788,6 +1788,43 @@ is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
1788 1788
                   "basic");
1789 1789
 
1790 1790
 
1791
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
1792
+$dbi->execute($CREATE_TABLE->{1});
1793
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1794
+$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1795
+
1796
+$param = {key2 => 11, key3 => 33};
1797
+$update_param = $dbi->update_param($param);
1798
+$sql = <<"EOS";
1799
+update {table table1} $update_param
1800
+where key1 = 1
1801
+EOS
1802
+$dbi->execute($sql, param => $param);
1803
+$result = $dbi->execute($SELECT_SOURCES->{0});
1804
+$rows   = $result->fetch_hash_all;
1805
+is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
1806
+                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
1807
+                  "basic");
1808
+
1809
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
1810
+$dbi->execute($CREATE_TABLE->{1});
1811
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1812
+$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1813
+
1814
+$param = {key2 => 11, key3 => 33};
1815
+$update_param = $dbi->update_param($param, {no_set => 1});
1816
+$sql = <<"EOS";
1817
+update {table table1} set $update_param
1818
+where key1 = 1
1819
+EOS
1820
+$dbi->execute($sql, param => $param);
1821
+$result = $dbi->execute($SELECT_SOURCES->{0});
1822
+$rows   = $result->fetch_hash_all;
1823
+is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
1824
+                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
1825
+                  "update param no_set");
1826
+
1827
+            
1791 1828
 eval { $dbi->update_param({";" => 1}) };
1792 1829
 like($@, qr/not safety/);
1793 1830
 
... ...
@@ -1795,14 +1832,26 @@ like($@, qr/not safety/);
1795 1832
 test 'insert_param';
1796 1833
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
1797 1834
 $dbi->execute($CREATE_TABLE->{1});
1798
-$param = {key1 => 1};
1835
+$param = {key1 => 1, key2 => 2};
1799 1836
 $insert_param = $dbi->insert_param($param);
1800 1837
 $sql = <<"EOS";
1801 1838
 insert into {table table1} $insert_param
1802 1839
 EOS
1840
+$dbi->execute($sql, param => $param);
1841
+is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1);
1842
+is($dbi->select(table => 'table1')->fetch_hash_first->{key2}, 2);
1803 1843
 
1844
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
1845
+$dbi->reserved_word_quote('"');
1846
+$dbi->execute($CREATE_TABLE->{1});
1847
+$param = {key1 => 1, key2 => 2};
1848
+$insert_param = $dbi->insert_param($param);
1849
+$sql = <<"EOS";
1850
+insert into {table table1} $insert_param
1851
+EOS
1804 1852
 $dbi->execute($sql, param => $param);
1805 1853
 is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1);
1854
+is($dbi->select(table => 'table1')->fetch_hash_first->{key2}, 2);
1806 1855
 
1807 1856
 eval { $dbi->insert_param({";" => 1}) };
1808 1857
 like($@, qr/not safety/);
... ...
@@ -2044,3 +2093,13 @@ $model = $dbi->create_model(
2044 2093
 );
2045 2094
 $model->method(foo => sub { shift->select(@_) });
2046 2095
 is_deeply($model->foo->fetch_hash_first, {key1 => 1, key3 => 3});
2096
+
2097
+test 'merge_param';
2098
+{
2099
+    my $dbi = DBIx::Custom->new;
2100
+    my $param1 = {key1 => 1, key2 => 2, key3 => 3};
2101
+    my $param2 = {key1 => 1, key2 => 2};
2102
+    my $param3 = {key1 => 1};
2103
+    my $param = $dbi->merge_param($param1, $param2, $param3);
2104
+    is_deeply($param, {key1 => [1, 1, 1], key2 => [2, 2], key3 => 3});
2105
+}