Showing 3 changed files with 73 additions and 55 deletions
+3 -2
lib/DBIx/Custom.pm
... ...
@@ -589,8 +589,9 @@ sub insert {
589 589
     my $query_return  = delete $args{query};
590 590
     my $id = delete $args{id};
591 591
     my $primary_key = delete $args{primary_key};
592
-    croak "primary_key must be specified when id is specified"
593
-        if defined $id && !defined $primary_key;
592
+    croak "select method primary_key option " .
593
+          "must be specified when id is specified " . _subname
594
+      if defined $id && !defined $primary_key;
594 595
     $primary_key = [$primary_key] unless ref $primary_key eq 'ARRAY';
595 596
 
596 597
     # Check arguments
+5 -2
lib/DBIx/Custom/Model.pm
... ...
@@ -53,8 +53,11 @@ foreach my $method (@methods) {
53 53
     my $code = sub {
54 54
         my $self = shift;
55 55
         
56
-        my @args = (table => $self->table, type => $self->type);
57
-        push @args, (primary_key => $self->primary_key) if $method =~ /_at$/;
56
+        my @args = (
57
+            table => $self->table,
58
+            type => $self->type,
59
+            primary_key => $self->primary_key
60
+        );
58 61
         push @args, (join => $self->join) if $method =~ /^select/;
59 62
         
60 63
         $self->dbi->$method(@args, @_);
+65 -51
t/dbix-custom-core-sqlite.t
... ...
@@ -740,7 +740,7 @@ $result = $dbi->execute($SELECT_SOURCES->{0});
740 740
 $row   = $result->fetch_hash_first;
741 741
 is_deeply($row, {key1 => 2, key2 => 6}, "insert");
742 742
 $result = $dbi->select(table => 'table1');
743
-$row   = $result->fetch_hash_first;
743
+$row   = $result->one;
744 744
 is_deeply($row, {key1 => 6, key2 => 12}, "insert");
745 745
 
746 746
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
... ...
@@ -755,7 +755,7 @@ $dbi->apply_filter(
755 755
 ); 
756 756
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
757 757
 $result = $dbi->execute($SELECT_SOURCES->{0});
758
-$row   = $result->fetch_hash_first;
758
+$row   = $result->one;
759 759
 is_deeply($row, {key1 => 1, key2 => 6}, "insert");
760 760
 
761 761
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
... ...
@@ -767,7 +767,7 @@ $dbi->apply_filter(
767 767
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => undef});
768 768
 $dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2});
769 769
 $result = $dbi->execute($SELECT_SOURCES->{0});
770
-$row   = $result->fetch_hash_first;
770
+$row   = $result->one;
771 771
 is_deeply($row, {key1 => 4, key2 => 2}, "update");
772 772
 
773 773
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
... ...
@@ -932,13 +932,13 @@ test 'connect super';
932 932
 $dbi = MyDBI->connect($NEW_ARGS->{0});
933 933
 $dbi->execute($CREATE_TABLE->{0});
934 934
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
935
-is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1);
935
+is($dbi->select(table => 'table1')->one->{key1}, 1);
936 936
 
937 937
 $dbi = MyDBI->new($NEW_ARGS->{0});
938 938
 $dbi->connect;
939 939
 $dbi->execute($CREATE_TABLE->{0});
940 940
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
941
-is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1);
941
+is($dbi->select(table => 'table1')->one->{key1}, 1);
942 942
 
943 943
 {
944 944
     package MyDBI2;
... ...
@@ -955,7 +955,7 @@ is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1);
955 955
 $dbi = MyDBI->connect($NEW_ARGS->{0});
956 956
 $dbi->execute($CREATE_TABLE->{0});
957 957
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
958
-is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1);
958
+is($dbi->select(table => 'table1')->one->{key1}, 1);
959 959
 
960 960
 test 'end_filter';
961 961
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
... ...
@@ -989,7 +989,7 @@ $dbi->register_filter(five_times => sub { $_[0] * 5 });
989 989
 $result = $dbi->select(table => 'table1');
990 990
 $result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
991 991
 $result->end_filter({key1 => sub { $_[0] * 3 }, key2 => 'five_times' });
992
-$row = $result->fetch_hash_first;
992
+$row = $result->one;
993 993
 is_deeply($row, {key1 => 6, key2 => 40});
994 994
 
995 995
 $dbi->register_filter(five_times => sub { $_[0] * 5 });
... ...
@@ -999,7 +999,7 @@ $dbi->apply_filter('table1',
999 999
 );
1000 1000
 $result = $dbi->select(table => 'table1');
1001 1001
 $result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1002
-$row = $result->fetch_hash_first;
1002
+$row = $result->one;
1003 1003
 is_deeply($row, {key1 => 6, key2 => 40}, 'apply_filter');
1004 1004
 
1005 1005
 $dbi->register_filter(five_times => sub { $_[0] * 5 });
... ...
@@ -1011,7 +1011,7 @@ $result = $dbi->select(table => 'table1');
1011 1011
 $result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1012 1012
 $result->filter(key1 => undef);
1013 1013
 $result->end_filter(key1 => undef);
1014
-$row = $result->fetch_hash_first;
1014
+$row = $result->one;
1015 1015
 is_deeply($row, {key1 => 1, key2 => 40}, 'apply_filter overwrite');
1016 1016
 
1017 1017
 test 'remove_end_filter and remove_filter';
... ...
@@ -1032,7 +1032,7 @@ $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
1032 1032
 $dbi->execute($CREATE_TABLE->{0});
1033 1033
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1034 1034
 $result = $dbi->select(table => 'table1', where => {});
1035
-$row = $result->fetch_hash_first;
1035
+$row = $result->one;
1036 1036
 is_deeply($row, {key1 => 1, key2 => 2});
1037 1037
 
1038 1038
 test 'select query option';
... ...
@@ -1385,7 +1385,7 @@ $dbi->execute($CREATE_TABLE->{0});
1385 1385
 $dbi->register_filter(twice => sub { $_[0] * 2 });
1386 1386
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
1387 1387
              filter => {key1 => 'twice'});
1388
-$row = $dbi->select(table => 'table1')->fetch_hash_first;
1388
+$row = $dbi->select(table => 'table1')->one;
1389 1389
 is_deeply($row, {key1 => 2, key2 => 2});
1390 1390
 eval {$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
1391 1391
              filter => {key1 => 'no'}) };
... ...
@@ -1613,9 +1613,9 @@ $dbi->insert_at(
1613 1613
     where => [1, 2],
1614 1614
     param => {key3 => 3}
1615 1615
 );
1616
-is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1);
1617
-is($dbi->select(table => 'table1')->fetch_hash_first->{key2}, 2);
1618
-is($dbi->select(table => 'table1')->fetch_hash_first->{key3}, 3);
1616
+is($dbi->select(table => 'table1')->one->{key1}, 1);
1617
+is($dbi->select(table => 'table1')->one->{key2}, 2);
1618
+is($dbi->select(table => 'table1')->one->{key3}, 3);
1619 1619
 
1620 1620
 $dbi->delete_all(table => 'table1');
1621 1621
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
... ...
@@ -1626,9 +1626,9 @@ $dbi->insert_at(
1626 1626
     param => {key2 => 2, key3 => 3}
1627 1627
 );
1628 1628
 
1629
-is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1);
1630
-is($dbi->select(table => 'table1')->fetch_hash_first->{key2}, 2);
1631
-is($dbi->select(table => 'table1')->fetch_hash_first->{key3}, 3);
1629
+is($dbi->select(table => 'table1')->one->{key1}, 1);
1630
+is($dbi->select(table => 'table1')->one->{key2}, 2);
1631
+is($dbi->select(table => 'table1')->one->{key3}, 3);
1632 1632
 
1633 1633
 eval {
1634 1634
     $dbi->insert_at(
... ...
@@ -1648,9 +1648,9 @@ $dbi->insert_at(
1648 1648
     table => 'table1',
1649 1649
     where => [1, 2],
1650 1650
 );
1651
-is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1);
1652
-is($dbi->select(table => 'table1')->fetch_hash_first->{key2}, 2);
1653
-is($dbi->select(table => 'table1')->fetch_hash_first->{key3}, 3);
1651
+is($dbi->select(table => 'table1')->one->{key1}, 1);
1652
+is($dbi->select(table => 'table1')->one->{key2}, 2);
1653
+is($dbi->select(table => 'table1')->one->{key3}, 3);
1654 1654
 
1655 1655
 test 'update_at';
1656 1656
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
... ...
@@ -1662,9 +1662,9 @@ $dbi->update_at(
1662 1662
     where => [1, 2],
1663 1663
     param => {key3 => 4}
1664 1664
 );
1665
-is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1);
1666
-is($dbi->select(table => 'table1')->fetch_hash_first->{key2}, 2);
1667
-is($dbi->select(table => 'table1')->fetch_hash_first->{key3}, 4);
1665
+is($dbi->select(table => 'table1')->one->{key1}, 1);
1666
+is($dbi->select(table => 'table1')->one->{key2}, 2);
1667
+is($dbi->select(table => 'table1')->one->{key3}, 4);
1668 1668
 
1669 1669
 $dbi->delete_all(table => 'table1');
1670 1670
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
... ...
@@ -1674,9 +1674,9 @@ $dbi->update_at(
1674 1674
     where => 1,
1675 1675
     param => {key3 => 4}
1676 1676
 );
1677
-is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1);
1678
-is($dbi->select(table => 'table1')->fetch_hash_first->{key2}, 2);
1679
-is($dbi->select(table => 'table1')->fetch_hash_first->{key3}, 4);
1677
+is($dbi->select(table => 'table1')->one->{key1}, 1);
1678
+is($dbi->select(table => 'table1')->one->{key2}, 2);
1679
+is($dbi->select(table => 'table1')->one->{key3}, 4);
1680 1680
 
1681 1681
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
1682 1682
 $dbi->execute($CREATE_TABLE->{1});
... ...
@@ -1687,9 +1687,9 @@ $dbi->update_at(
1687 1687
     primary_key => ['key1', 'key2'],
1688 1688
     where => [1, 2]
1689 1689
 );
1690
-is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1);
1691
-is($dbi->select(table => 'table1')->fetch_hash_first->{key2}, 2);
1692
-is($dbi->select(table => 'table1')->fetch_hash_first->{key3}, 4);
1690
+is($dbi->select(table => 'table1')->one->{key1}, 1);
1691
+is($dbi->select(table => 'table1')->one->{key2}, 2);
1692
+is($dbi->select(table => 'table1')->one->{key3}, 4);
1693 1693
 
1694 1694
 test 'select_at';
1695 1695
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
... ...
@@ -1700,7 +1700,7 @@ $result = $dbi->select_at(
1700 1700
     primary_key => ['key1', 'key2'],
1701 1701
     where => [1, 2]
1702 1702
 );
1703
-$row = $result->fetch_hash_first;
1703
+$row = $result->one;
1704 1704
 is($row->{key1}, 1);
1705 1705
 is($row->{key2}, 2);
1706 1706
 is($row->{key3}, 3);
... ...
@@ -1712,7 +1712,7 @@ $result = $dbi->select_at(
1712 1712
     primary_key => 'key1',
1713 1713
     where => 1,
1714 1714
 );
1715
-$row = $result->fetch_hash_first;
1715
+$row = $result->one;
1716 1716
 is($row->{key1}, 1);
1717 1717
 is($row->{key2}, 2);
1718 1718
 is($row->{key3}, 3);
... ...
@@ -1724,7 +1724,7 @@ $result = $dbi->select_at(
1724 1724
     primary_key => ['key1', 'key2'],
1725 1725
     where => [1, 2]
1726 1726
 );
1727
-$row = $result->fetch_hash_first;
1727
+$row = $result->one;
1728 1728
 is($row->{key1}, 1);
1729 1729
 is($row->{key2}, 2);
1730 1730
 is($row->{key3}, 3);
... ...
@@ -1808,7 +1808,7 @@ $dbi->model('table1')->insert_at(
1808 1808
     param => {key3 => 3}
1809 1809
 );
1810 1810
 $result = $dbi->model('table1')->select;
1811
-$row = $result->fetch_hash_first;
1811
+$row = $result->one;
1812 1812
 is($row->{key1}, 1);
1813 1813
 is($row->{key2}, 2);
1814 1814
 is($row->{key3}, 3);
... ...
@@ -1822,7 +1822,7 @@ $dbi->model('table1')->update_at(
1822 1822
     param => {key3 => 4}
1823 1823
 );
1824 1824
 $result = $dbi->model('table1')->select;
1825
-$row = $result->fetch_hash_first;
1825
+$row = $result->one;
1826 1826
 is($row->{key1}, 1);
1827 1827
 is($row->{key2}, 2);
1828 1828
 is($row->{key3}, 4);
... ...
@@ -1832,7 +1832,7 @@ $dbi = MyDBI6->connect($NEW_ARGS->{0});
1832 1832
 $dbi->execute($CREATE_TABLE->{1});
1833 1833
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1834 1834
 $result = $dbi->model('table1')->select_at(where => [1, 2]);
1835
-$row = $result->fetch_hash_first;
1835
+$row = $result->one;
1836 1836
 is($row->{key1}, 1);
1837 1837
 is($row->{key2}, 2);
1838 1838
 is($row->{key3}, 3);
... ...
@@ -1864,7 +1864,7 @@ $result = $model->select(
1864 1864
     column => [$model->mycolumn, $model->column('table2')],
1865 1865
     where => {'table1.key1' => 1}
1866 1866
 );
1867
-is_deeply($result->fetch_hash_first,
1867
+is_deeply($result->one,
1868 1868
           {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
1869 1869
 
1870 1870
 test 'update_param';
... ...
@@ -1957,8 +1957,8 @@ $sql = <<"EOS";
1957 1957
 insert into table1 $insert_param
1958 1958
 EOS
1959 1959
 $dbi->execute($sql, param => $param, table => 'table1');
1960
-is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1);
1961
-is($dbi->select(table => 'table1')->fetch_hash_first->{key2}, 2);
1960
+is($dbi->select(table => 'table1')->one->{key1}, 1);
1961
+is($dbi->select(table => 'table1')->one->{key2}, 2);
1962 1962
 
1963 1963
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
1964 1964
 $dbi->reserved_word_quote('"');
... ...
@@ -1969,8 +1969,8 @@ $sql = <<"EOS";
1969 1969
 insert into table1 $insert_param
1970 1970
 EOS
1971 1971
 $dbi->execute($sql, param => $param, table => 'table1');
1972
-is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1);
1973
-is($dbi->select(table => 'table1')->fetch_hash_first->{key2}, 2);
1972
+is($dbi->select(table => 'table1')->one->{key1}, 1);
1973
+is($dbi->select(table => 'table1')->one->{key2}, 2);
1974 1974
 
1975 1975
 eval { $dbi->insert_param({";" => 1}) };
1976 1976
 like($@, qr/not safety/);
... ...
@@ -2099,7 +2099,7 @@ $result = $model->select_at(
2099 2099
         $model->column('table2')
2100 2100
     ]
2101 2101
 );
2102
-is_deeply($result->fetch_hash_first,
2102
+is_deeply($result->one,
2103 2103
           {key1 => 1, key2 => 2, table2__key1 => 1, table2__key3 => 3});
2104 2104
 
2105 2105
 $result = $model->select_at(
... ...
@@ -2108,7 +2108,7 @@ $result = $model->select_at(
2108 2108
         $model->column(table2 => ['key1'])
2109 2109
     ]
2110 2110
 );
2111
-is_deeply($result->fetch_hash_first,
2111
+is_deeply($result->one,
2112 2112
           {key1 => 1, table2__key1 => 1});
2113 2113
 
2114 2114
 $result = $model->select_at(
... ...
@@ -2117,7 +2117,7 @@ $result = $model->select_at(
2117 2117
         {table2 => ['key1']}
2118 2118
     ]
2119 2119
 );
2120
-is_deeply($result->fetch_hash_first,
2120
+is_deeply($result->one,
2121 2121
           {key1 => 1, table2__key1 => 1});
2122 2122
 
2123 2123
 test 'dbi method from model';
... ...
@@ -2154,7 +2154,7 @@ $result = $model->select(
2154 2154
     ],
2155 2155
     where => {'table2_alias.key3' => 2}
2156 2156
 );
2157
-is_deeply($result->fetch_hash_first, 
2157
+is_deeply($result->one, 
2158 2158
           {table2_alias__key1 => 1, table2_alias__key3 => 48});
2159 2159
 
2160 2160
 test 'type() option';
... ...
@@ -2168,18 +2168,18 @@ my $binary = pack("I3", 1, 2, 3);
2168 2168
 $dbi->execute('create table table1(key1, key2)');
2169 2169
 $dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]);
2170 2170
 $result = $dbi->select(table => 'table1');
2171
-$row   = $result->fetch_hash_first;
2171
+$row   = $result->one;
2172 2172
 is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2173 2173
 $result = $dbi->execute('select length(key1) as key1_length from table1');
2174
-$row = $result->fetch_hash_first;
2174
+$row = $result->one;
2175 2175
 is($row->{key1_length}, length $binary);
2176 2176
 
2177 2177
 $dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [['key1'] => DBI::SQL_BLOB]);
2178 2178
 $result = $dbi->select(table => 'table1');
2179
-$row   = $result->fetch_hash_first;
2179
+$row   = $result->one;
2180 2180
 is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2181 2181
 $result = $dbi->execute('select length(key1) as key1_length from table1');
2182
-$row = $result->fetch_hash_first;
2182
+$row = $result->one;
2183 2183
 is($row->{key1_length}, length $binary);
2184 2184
 
2185 2185
 test 'create_model';
... ...
@@ -2211,9 +2211,9 @@ $result = $model->select(
2211 2211
     column => [$model->mycolumn, $model->column('table2')],
2212 2212
     where => {'table1.key1' => 1}
2213 2213
 );
2214
-is_deeply($result->fetch_hash_first,
2214
+is_deeply($result->one,
2215 2215
           {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
2216
-is_deeply($model2->select->fetch_hash_first, {key1 => 1, key3 => 3});
2216
+is_deeply($model2->select->one, {key1 => 1, key3 => 3});
2217 2217
 
2218 2218
 test 'model method';
2219 2219
 test 'create_model';
... ...
@@ -2224,7 +2224,7 @@ $model = $dbi->create_model(
2224 2224
     table => 'table2'
2225 2225
 );
2226 2226
 $model->method(foo => sub { shift->select(@_) });
2227
-is_deeply($model->foo->fetch_hash_first, {key1 => 1, key3 => 3});
2227
+is_deeply($model->foo->one, {key1 => 1, key3 => 3});
2228 2228
 
2229 2229
 test 'merge_param';
2230 2230
 {
... ...
@@ -2361,4 +2361,18 @@ is($dbi->select(table => 'table1')->one->{key1}, 1);
2361 2361
 is($dbi->select(table => 'table1')->one->{key2}, 2);
2362 2362
 is($dbi->select(table => 'table1')->one->{key3}, 3);
2363 2363
 
2364
+test 'model insert id and primary_key option';
2365
+$dbi = MyDBI6->connect($NEW_ARGS->{0});
2366
+$dbi->execute($CREATE_TABLE->{1});
2367
+$dbi->model('table1')->insert(
2368
+    id => [1, 2],
2369
+    param => {key3 => 3}
2370
+);
2371
+$result = $dbi->model('table1')->select;
2372
+$row = $result->one;
2373
+is($row->{key1}, 1);
2374
+is($row->{key2}, 2);
2375
+is($row->{key3}, 3);
2376
+
2377
+
2364 2378
 =cut