Showing 3 changed files with 65 additions and 6 deletions
+1
Changes
... ...
@@ -1,4 +1,5 @@
1 1
 0.1655
2
+    - added experimental update_param and insert_param
2 3
     - remove experimental DBIx::Custom::Model relation
3 4
 0.1654
4 5
     - selection can contain where clause.
+24 -6
lib/DBIx/Custom.pm
... ...
@@ -538,14 +538,15 @@ sub insert_at {
538 538
 sub insert_param {
539 539
     my ($self, $param) = @_;
540 540
     
541
+    # Insert paramter tag
541 542
     my @tag;
542
-    
543 543
     push @tag, '{insert_param';
544
-    
544
+    my $safety = $self->safety_column_name;
545 545
     foreach my $column (keys %$param) {
546
+        croak qq{"$column" is not safety column name}
547
+          unless $column =~ /$safety/;
546 548
         push @tag, $column;
547 549
     }
548
-    
549 550
     push @tag, '}';
550 551
     
551 552
     return join ' ', @tag;
... ...
@@ -1057,14 +1058,15 @@ sub update_at {
1057 1058
 sub update_param {
1058 1059
     my ($self, $param) = @_;
1059 1060
     
1061
+    # Update parameter tag
1060 1062
     my @tag;
1061
-    
1062 1063
     push @tag, '{update_param';
1063
-    
1064
+    my $safety = $self->safety_column_name;
1064 1065
     foreach my $column (keys %$param) {
1066
+        croak qq{"$column" is not safety column name}
1067
+          unless $column =~ /$safety/;
1065 1068
         push @tag, $column;
1066 1069
     }
1067
-    
1068 1070
     push @tag, '}';
1069 1071
     
1070 1072
     return join ' ', @tag;
... ...
@@ -1575,6 +1577,14 @@ NOTE that you must pass array reference as C<where>.
1575 1577
 If C<param> contains primary key,
1576 1578
 the key and value is delete from C<param>.
1577 1579
 
1580
+=head2 C<(experimental) insert_param>
1581
+
1582
+    my $insert_param = $dbi->insert_param({title => 'a', age => 2});
1583
+
1584
+Create insert parameter tag.
1585
+
1586
+    {title => 'a', age => 2}   ->   {insert_param title age}
1587
+
1578 1588
 =head2 C<(experimental) each_column>
1579 1589
 
1580 1590
     $dbi->each_column(
... ...
@@ -1778,6 +1788,14 @@ default to 0. This is experimental.
1778 1788
 This is overwrites C<default_bind_filter>.
1779 1789
 Return value of C<update()> is the count of affected rows.
1780 1790
 
1791
+=head2 C<(experimental) update_param>
1792
+
1793
+    my $update_param = $dbi->update_param({title => 'a', age => 2});
1794
+
1795
+Create update parameter tag.
1796
+
1797
+    {title => 'a', age => 2}   ->   {update_param title age}
1798
+
1781 1799
 =head2 C<(experimental) model>
1782 1800
 
1783 1801
     $dbi->model('book')->method(
+40
t/dbix-custom-core-sqlite.t
... ...
@@ -50,6 +50,7 @@ my @sources;
50 50
 my $select_SOURCE;
51 51
 my $insert_SOURCE;
52 52
 my $update_SOURCE;
53
+my $param;
53 54
 my $params;
54 55
 my $sql;
55 56
 my $result;
... ...
@@ -65,6 +66,8 @@ my $ret_val;
65 66
 my $infos;
66 67
 my $model;
67 68
 my $where;
69
+my $update_param;
70
+my $insert_param;
68 71
 
69 72
 # Prepare table
70 73
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
... ...
@@ -1681,4 +1684,41 @@ is_deeply($result->fetch_hash_first, {key2 => 2});
1681 1684
 $result = $model->select(relation => {'table1.key1' => 'table2.key1'}, column => $model->column_clause(add => ['key3']), where => {'table1.key1' => 1});
1682 1685
 is_deeply($result->fetch_hash_first, {key1 => 1, key2 => 2, key3 => 3});
1683 1686
 
1687
+test 'update_param';
1688
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
1689
+$dbi->execute($CREATE_TABLE->{1});
1690
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1691
+$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1692
+
1693
+$param = {key2 => 11};
1694
+$update_param = $dbi->update_param($param);
1695
+$sql = <<"EOS";
1696
+update {table table1} $update_param
1697
+where key1 = 1
1698
+EOS
1699
+$dbi->execute($sql, param => $param);
1700
+$result = $dbi->execute($SELECT_SOURCES->{0});
1701
+$rows   = $result->fetch_hash_all;
1702
+is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
1703
+                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
1704
+                  "basic");
1705
+
1706
+
1707
+eval { $dbi->update_param({";" => 1}) };
1708
+like($@, qr/not safety/);
1709
+
1710
+
1711
+test 'insert_param';
1712
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
1713
+$dbi->execute($CREATE_TABLE->{1});
1714
+$param = {key1 => 1};
1715
+$insert_param = $dbi->insert_param($param);
1716
+$sql = <<"EOS";
1717
+insert into {table table1} $insert_param
1718
+EOS
1719
+
1720
+$dbi->execute($sql, param => $param);
1721
+is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1);
1684 1722
 
1723
+eval { $dbi->insert_param({";" => 1}) };
1724
+like($@, qr/not safety/);