| ... | ... |
@@ -18,6 +18,7 @@ use DBIx::Custom; |
| 18 | 18 |
sub create_table1 { 'create table table1 (key1 varchar(255), key2 varchar(255)) engine=InnoDB;' }
|
| 19 | 19 |
sub create_table1_2 {'create table table1 (key1 varchar(255), key2 varchar(255), '
|
| 20 | 20 |
. 'key3 varchar(255), key4 varchar(255), key5 varchar(255)) engine=InnoDB;' } |
| 21 |
+ sub create_table1_type { 'create table table1 (key1 Date, key2 datetime) engine=InnoDB;;' }
|
|
| 21 | 22 |
sub create_table2 { 'create table table2 (key1 varchar(255), key3 varchar(255)) engine=InnoDB;' }
|
| 22 | 23 |
sub create_table_reserved {
|
| 23 | 24 |
'create table `table` (`select` varchar(255), `update` varchar(255)) engine=InnoDB;' } |
| ... | ... |
@@ -17,6 +17,7 @@ use DBIx::Custom; |
| 17 | 17 |
sub create_table1 { 'create table table1 (key1 varchar(255), key2 varchar(255));' }
|
| 18 | 18 |
sub create_table1_2 {'create table table1 (key1 varchar(255), key2 varchar(255), '
|
| 19 | 19 |
. 'key3 varchar(255), key4 varchar(255), key5 varchar(255));' } |
| 20 |
+ sub create_table1_type { 'create table table1 (key1 Date, key2 varchar(255));' }
|
|
| 20 | 21 |
sub create_table2 { 'create table table2 (key1 varchar(255), key3 varchar(255));' }
|
| 21 | 22 |
sub create_table_reserved { 'create table "table" ("select" varchar(255), "update" varchar(255))' }
|
| 22 | 23 |
} |
| ... | ... |
@@ -12,6 +12,7 @@ use DBIx::Custom; |
| 12 | 12 |
sub quote { '""' }
|
| 13 | 13 |
sub create_table1 { 'create table table1 (key1, key2);' }
|
| 14 | 14 |
sub create_table1_2 {'create table table1 (key1, key2, key3, key4, key5);' }
|
| 15 |
+ sub create_table1_type { 'create table table1 (key1 Date, key2 datetime);' }
|
|
| 15 | 16 |
sub create_table2 { 'create table table2 (key1, key3);' }
|
| 16 | 17 |
sub create_table_reserved { 'create table "table" ("select", "update")' }
|
| 17 | 18 |
} |
| ... | ... |
@@ -11,6 +11,7 @@ use DBIx::Custom; |
| 11 | 11 |
has dsn => 'dbi:SQLite:dbname=:memory:'; |
| 12 | 12 |
sub create_table1 { 'create table table1 (key1, key2);' }
|
| 13 | 13 |
sub create_table1_2 {'create table table1 (key1, key2, key3, key4, key5);' }
|
| 14 |
+ sub create_table1_type { 'create table table1 (key1 Date, key2 datetime);' }
|
|
| 14 | 15 |
sub create_table2 { 'create table table2 (key1, key3);' }
|
| 15 | 16 |
sub create_table_reserved { 'create table "table" ("select", "update")' }
|
| 16 | 17 |
} |
| ... | ... |
@@ -18,6 +18,7 @@ sub test { print "# $_[0]\n" }
|
| 18 | 18 |
# Constant |
| 19 | 19 |
my $create_table1 = $dbi->create_table1; |
| 20 | 20 |
my $create_table1_2 = $dbi->create_table1_2; |
| 21 |
+my $create_table1_type = $dbi->create_table1_type; |
|
| 21 | 22 |
my $create_table2 = $dbi->create_table2; |
| 22 | 23 |
my $create_table_reserved = $dbi->create_table_reserved; |
| 23 | 24 |
my $q = substr($dbi->quote, 0, 1); |
| ... | ... |
@@ -740,4 +741,444 @@ eval{ $dbi->begin_work };
|
| 740 | 741 |
ok($@, "exception"); |
| 741 | 742 |
$dbi->dbh->{AutoCommit} = 1;
|
| 742 | 743 |
|
| 744 |
+test 'cache'; |
|
| 745 |
+eval { $dbi->execute('drop table table1') };
|
|
| 746 |
+$dbi->cache(1); |
|
| 747 |
+$dbi->execute($create_table1); |
|
| 748 |
+$source = 'select * from table1 where key1 = :key1 and key2 = :key2;'; |
|
| 749 |
+$dbi->execute($source, {}, query => 1);
|
|
| 750 |
+is_deeply($dbi->{_cached}->{$source},
|
|
| 751 |
+ {sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2'], tables => []}, "cache");
|
|
| 752 |
+ |
|
| 753 |
+eval { $dbi->execute('drop table table1') };
|
|
| 754 |
+$dbi->execute($create_table1); |
|
| 755 |
+$dbi->{_cached} = {};
|
|
| 756 |
+$dbi->cache(0); |
|
| 757 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 758 |
+is(scalar keys %{$dbi->{_cached}}, 0, 'not cache');
|
|
| 759 |
+ |
|
| 760 |
+test 'execute'; |
|
| 761 |
+eval { $dbi->execute('drop table table1') };
|
|
| 762 |
+$dbi->execute($create_table1); |
|
| 763 |
+{
|
|
| 764 |
+ local $Carp::Verbose = 0; |
|
| 765 |
+ eval{$dbi->execute('select * frm table1')};
|
|
| 766 |
+ like($@, qr/\Qselect * frm table1;/, "fail prepare"); |
|
| 767 |
+ like($@, qr/\.t /, "fail : not verbose"); |
|
| 768 |
+} |
|
| 769 |
+{
|
|
| 770 |
+ local $Carp::Verbose = 1; |
|
| 771 |
+ eval{$dbi->execute('select * frm table1')};
|
|
| 772 |
+ like($@, qr/Custom.*\.t /s, "fail : verbose"); |
|
| 773 |
+} |
|
| 774 |
+ |
|
| 775 |
+eval{$dbi->execute('select * from table1', no_exists => 1)};
|
|
| 776 |
+like($@, qr/wrong/, "invald SQL"); |
|
| 777 |
+ |
|
| 778 |
+$query = $dbi->execute('select * from table1 where key1 = :key1', {}, query => 1);
|
|
| 779 |
+$dbi->dbh->disconnect; |
|
| 780 |
+eval{$dbi->execute($query, param => {key1 => {a => 1}})};
|
|
| 781 |
+ok($@, "execute fail"); |
|
| 782 |
+ |
|
| 783 |
+{
|
|
| 784 |
+ local $Carp::Verbose = 0; |
|
| 785 |
+ eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
|
|
| 786 |
+ like($@, qr/\Q.t /, "caller spec : not vebose"); |
|
| 787 |
+} |
|
| 788 |
+{
|
|
| 789 |
+ local $Carp::Verbose = 1; |
|
| 790 |
+ eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
|
|
| 791 |
+ like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose"); |
|
| 792 |
+} |
|
| 793 |
+ |
|
| 794 |
+ |
|
| 795 |
+test 'transaction'; |
|
| 796 |
+$dbi = DBIx::Custom->connect; |
|
| 797 |
+eval { $dbi->execute('drop table table1') };
|
|
| 798 |
+$dbi->execute($create_table1); |
|
| 799 |
+ |
|
| 800 |
+$dbi->begin_work; |
|
| 801 |
+ |
|
| 802 |
+eval {
|
|
| 803 |
+ $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 804 |
+ die "Error"; |
|
| 805 |
+ $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
|
|
| 806 |
+}; |
|
| 807 |
+ |
|
| 808 |
+$dbi->rollback if $@; |
|
| 809 |
+ |
|
| 810 |
+$result = $dbi->select(table => 'table1'); |
|
| 811 |
+$rows = $result->all; |
|
| 812 |
+is_deeply($rows, [], "rollback"); |
|
| 813 |
+ |
|
| 814 |
+$dbi->begin_work; |
|
| 815 |
+ |
|
| 816 |
+eval {
|
|
| 817 |
+ $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 818 |
+ $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
|
|
| 819 |
+}; |
|
| 820 |
+ |
|
| 821 |
+$dbi->commit unless $@; |
|
| 822 |
+ |
|
| 823 |
+$result = $dbi->select(table => 'table1'); |
|
| 824 |
+$rows = $result->all; |
|
| 825 |
+is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "commit");
|
|
| 826 |
+ |
|
| 827 |
+$dbi->dbh->{AutoCommit} = 0;
|
|
| 828 |
+eval{ $dbi->begin_work };
|
|
| 829 |
+ok($@, "exception"); |
|
| 830 |
+$dbi->dbh->{AutoCommit} = 1;
|
|
| 831 |
+ |
|
| 832 |
+ |
|
| 833 |
+test 'method'; |
|
| 834 |
+$dbi->method( |
|
| 835 |
+ one => sub { 1 }
|
|
| 836 |
+); |
|
| 837 |
+$dbi->method( |
|
| 838 |
+ two => sub { 2 }
|
|
| 839 |
+); |
|
| 840 |
+$dbi->method({
|
|
| 841 |
+ twice => sub {
|
|
| 842 |
+ my $self = shift; |
|
| 843 |
+ return $_[0] * 2; |
|
| 844 |
+ } |
|
| 845 |
+}); |
|
| 846 |
+ |
|
| 847 |
+is($dbi->one, 1, "first"); |
|
| 848 |
+is($dbi->two, 2, "second"); |
|
| 849 |
+is($dbi->twice(5), 10 , "second"); |
|
| 850 |
+ |
|
| 851 |
+eval {$dbi->XXXXXX};
|
|
| 852 |
+ok($@, "not exists"); |
|
| 853 |
+ |
|
| 854 |
+test 'out filter'; |
|
| 855 |
+$dbi = DBIx::Custom->connect; |
|
| 856 |
+eval { $dbi->execute('drop table table1') };
|
|
| 857 |
+$dbi->execute($create_table1); |
|
| 858 |
+$dbi->register_filter(twice => sub { $_[0] * 2 });
|
|
| 859 |
+$dbi->register_filter(three_times => sub { $_[0] * 3});
|
|
| 860 |
+$dbi->apply_filter( |
|
| 861 |
+ 'table1', 'key1' => {out => 'twice', in => 'three_times'},
|
|
| 862 |
+ 'key2' => {out => 'three_times', in => 'twice'});
|
|
| 863 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 864 |
+$result = $dbi->execute('select * from table1;');
|
|
| 865 |
+$row = $result->fetch_hash_first; |
|
| 866 |
+is_deeply($row, {key1 => 2, key2 => 6}, "insert");
|
|
| 867 |
+$result = $dbi->select(table => 'table1'); |
|
| 868 |
+$row = $result->one; |
|
| 869 |
+is_deeply($row, {key1 => 6, key2 => 12}, "insert");
|
|
| 870 |
+ |
|
| 871 |
+$dbi = DBIx::Custom->connect; |
|
| 872 |
+eval { $dbi->execute('drop table table1') };
|
|
| 873 |
+$dbi->execute($create_table1); |
|
| 874 |
+$dbi->register_filter(twice => sub { $_[0] * 2 });
|
|
| 875 |
+$dbi->register_filter(three_times => sub { $_[0] * 3});
|
|
| 876 |
+$dbi->apply_filter( |
|
| 877 |
+ 'table1', 'key1' => {out => 'twice', in => 'three_times'},
|
|
| 878 |
+ 'key2' => {out => 'three_times', in => 'twice'});
|
|
| 879 |
+$dbi->apply_filter( |
|
| 880 |
+ 'table1', 'key1' => {out => undef}
|
|
| 881 |
+); |
|
| 882 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 883 |
+$result = $dbi->execute('select * from table1;');
|
|
| 884 |
+$row = $result->one; |
|
| 885 |
+is_deeply($row, {key1 => 1, key2 => 6}, "insert");
|
|
| 886 |
+ |
|
| 887 |
+$dbi = DBIx::Custom->connect; |
|
| 888 |
+eval { $dbi->execute('drop table table1') };
|
|
| 889 |
+$dbi->execute($create_table1); |
|
| 890 |
+$dbi->register_filter(twice => sub { $_[0] * 2 });
|
|
| 891 |
+$dbi->apply_filter( |
|
| 892 |
+ 'table1', 'key1' => {out => 'twice', in => 'twice'}
|
|
| 893 |
+); |
|
| 894 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => undef});
|
|
| 895 |
+$dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2});
|
|
| 896 |
+$result = $dbi->execute('select * from table1;');
|
|
| 897 |
+$row = $result->one; |
|
| 898 |
+is_deeply($row, {key1 => 4, key2 => 2}, "update");
|
|
| 899 |
+ |
|
| 900 |
+$dbi = DBIx::Custom->connect; |
|
| 901 |
+eval { $dbi->execute('drop table table1') };
|
|
| 902 |
+$dbi->execute($create_table1); |
|
| 903 |
+$dbi->register_filter(twice => sub { $_[0] * 2 });
|
|
| 904 |
+$dbi->apply_filter( |
|
| 905 |
+ 'table1', 'key1' => {out => 'twice', in => 'twice'}
|
|
| 906 |
+); |
|
| 907 |
+$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1=> undef});
|
|
| 908 |
+$dbi->delete(table => 'table1', where => {key1 => 1});
|
|
| 909 |
+$result = $dbi->execute('select * from table1;');
|
|
| 910 |
+$rows = $result->all; |
|
| 911 |
+is_deeply($rows, [], "delete"); |
|
| 912 |
+ |
|
| 913 |
+$dbi = DBIx::Custom->connect; |
|
| 914 |
+eval { $dbi->execute('drop table table1') };
|
|
| 915 |
+$dbi->execute($create_table1); |
|
| 916 |
+$dbi->register_filter(twice => sub { $_[0] * 2 });
|
|
| 917 |
+$dbi->apply_filter( |
|
| 918 |
+ 'table1', 'key1' => {out => 'twice', in => 'twice'}
|
|
| 919 |
+); |
|
| 920 |
+$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
|
|
| 921 |
+$result = $dbi->select(table => 'table1', where => {key1 => 1});
|
|
| 922 |
+$result->filter({'key2' => 'twice'});
|
|
| 923 |
+$rows = $result->all; |
|
| 924 |
+is_deeply($rows, [{key1 => 4, key2 => 4}], "select");
|
|
| 925 |
+ |
|
| 926 |
+$dbi = DBIx::Custom->connect; |
|
| 927 |
+eval { $dbi->execute('drop table table1') };
|
|
| 928 |
+$dbi->execute($create_table1); |
|
| 929 |
+$dbi->register_filter(twice => sub { $_[0] * 2 });
|
|
| 930 |
+$dbi->apply_filter( |
|
| 931 |
+ 'table1', 'key1' => {out => 'twice', in => 'twice'}
|
|
| 932 |
+); |
|
| 933 |
+$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
|
|
| 934 |
+$result = $dbi->execute("select * from table1 where key1 = :key1 and key2 = :key2;",
|
|
| 935 |
+ param => {key1 => 1, key2 => 2},
|
|
| 936 |
+ table => ['table1']); |
|
| 937 |
+$rows = $result->all; |
|
| 938 |
+is_deeply($rows, [{key1 => 4, key2 => 2}], "execute");
|
|
| 939 |
+ |
|
| 940 |
+$dbi = DBIx::Custom->connect; |
|
| 941 |
+eval { $dbi->execute('drop table table1') };
|
|
| 942 |
+$dbi->execute($create_table1); |
|
| 943 |
+$dbi->register_filter(twice => sub { $_[0] * 2 });
|
|
| 944 |
+$dbi->apply_filter( |
|
| 945 |
+ 'table1', 'key1' => {out => 'twice', in => 'twice'}
|
|
| 946 |
+); |
|
| 947 |
+$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
|
|
| 948 |
+$result = $dbi->execute("select * from {table table1} where key1 = :key1 and key2 = :key2;",
|
|
| 949 |
+ param => {key1 => 1, key2 => 2});
|
|
| 950 |
+$rows = $result->all; |
|
| 951 |
+is_deeply($rows, [{key1 => 4, key2 => 2}], "execute table tag");
|
|
| 952 |
+ |
|
| 953 |
+$dbi = DBIx::Custom->connect; |
|
| 954 |
+eval { $dbi->execute('drop table table1') };
|
|
| 955 |
+eval { $dbi->execute('drop table table2') };
|
|
| 956 |
+$dbi->execute($create_table1); |
|
| 957 |
+$dbi->execute($create_table2); |
|
| 958 |
+$dbi->register_filter(twice => sub { $_[0] * 2 });
|
|
| 959 |
+$dbi->register_filter(three_times => sub { $_[0] * 3 });
|
|
| 960 |
+$dbi->apply_filter( |
|
| 961 |
+ 'table1', 'key2' => {out => 'twice', in => 'twice'}
|
|
| 962 |
+); |
|
| 963 |
+$dbi->apply_filter( |
|
| 964 |
+ 'table2', 'key3' => {out => 'three_times', in => 'three_times'}
|
|
| 965 |
+); |
|
| 966 |
+$dbi->insert(table => 'table1', param => {key1 => 5, key2 => 2}, filter => {key2 => undef});
|
|
| 967 |
+$dbi->insert(table => 'table2', param => {key1 => 5, key3 => 6}, filter => {key3 => undef});
|
|
| 968 |
+$result = $dbi->select( |
|
| 969 |
+ table => ['table1', 'table2'], |
|
| 970 |
+ column => ['key2', 'key3'], |
|
| 971 |
+ where => {'table1.key2' => 1, 'table2.key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
|
|
| 972 |
+ |
|
| 973 |
+$result->filter({'key2' => 'twice'});
|
|
| 974 |
+$rows = $result->all; |
|
| 975 |
+is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join");
|
|
| 976 |
+ |
|
| 977 |
+$result = $dbi->select( |
|
| 978 |
+ table => ['table1', 'table2'], |
|
| 979 |
+ column => ['key2', 'key3'], |
|
| 980 |
+ where => {'key2' => 1, 'key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
|
|
| 981 |
+ |
|
| 982 |
+$result->filter({'key2' => 'twice'});
|
|
| 983 |
+$rows = $result->all; |
|
| 984 |
+is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join : omit");
|
|
| 985 |
+ |
|
| 986 |
+test 'each_column'; |
|
| 987 |
+$dbi = DBIx::Custom->connect; |
|
| 988 |
+eval { $dbi->execute("drop table ${q}table$p") };
|
|
| 989 |
+eval { $dbi->execute('drop table table1') };
|
|
| 990 |
+eval { $dbi->execute('drop table table2') };
|
|
| 991 |
+$dbi->execute($create_table1_type); |
|
| 992 |
+$dbi->execute($create_table2); |
|
| 993 |
+ |
|
| 994 |
+$infos = []; |
|
| 995 |
+$dbi->each_column(sub {
|
|
| 996 |
+ my ($self, $table, $column, $cinfo) = @_; |
|
| 997 |
+ |
|
| 998 |
+ if ($table =~ /^table\d/) {
|
|
| 999 |
+ my $info = [$table, $column, $cinfo->{COLUMN_NAME}];
|
|
| 1000 |
+ push @$infos, $info; |
|
| 1001 |
+ } |
|
| 1002 |
+}); |
|
| 1003 |
+$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
|
|
| 1004 |
+is_deeply($infos, |
|
| 1005 |
+ [ |
|
| 1006 |
+ ['table1', 'key1', 'key1'], |
|
| 1007 |
+ ['table1', 'key2', 'key2'], |
|
| 1008 |
+ ['table2', 'key1', 'key1'], |
|
| 1009 |
+ ['table2', 'key3', 'key3'] |
|
| 1010 |
+ ] |
|
| 1011 |
+ |
|
| 1012 |
+); |
|
| 1013 |
+test 'each_table'; |
|
| 1014 |
+$dbi = DBIx::Custom->connect; |
|
| 1015 |
+eval { $dbi->execute('drop table table1') };
|
|
| 1016 |
+eval { $dbi->execute('drop table table2') };
|
|
| 1017 |
+$dbi->execute($create_table2); |
|
| 1018 |
+$dbi->execute($create_table1_type); |
|
| 1019 |
+ |
|
| 1020 |
+$infos = []; |
|
| 1021 |
+$dbi->each_table(sub {
|
|
| 1022 |
+ my ($self, $table, $table_info) = @_; |
|
| 1023 |
+ |
|
| 1024 |
+ if ($table =~ /^table\d/) {
|
|
| 1025 |
+ my $info = [$table, $table_info->{TABLE_NAME}];
|
|
| 1026 |
+ push @$infos, $info; |
|
| 1027 |
+ } |
|
| 1028 |
+}); |
|
| 1029 |
+$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
|
|
| 1030 |
+is_deeply($infos, |
|
| 1031 |
+ [ |
|
| 1032 |
+ ['table1', 'table1'], |
|
| 1033 |
+ ['table2', 'table2'], |
|
| 1034 |
+ ] |
|
| 1035 |
+); |
|
| 1036 |
+ |
|
| 1037 |
+test 'limit'; |
|
| 1038 |
+$dbi = DBIx::Custom->connect; |
|
| 1039 |
+eval { $dbi->execute('drop table table1') };
|
|
| 1040 |
+$dbi->execute($create_table1); |
|
| 1041 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 1042 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4});
|
|
| 1043 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6});
|
|
| 1044 |
+$dbi->register_tag( |
|
| 1045 |
+ limit => sub {
|
|
| 1046 |
+ my ($count, $offset) = @_; |
|
| 1047 |
+ |
|
| 1048 |
+ my $s = ''; |
|
| 1049 |
+ $s .= "limit $count"; |
|
| 1050 |
+ $s .= " offset $offset" if defined $offset; |
|
| 1051 |
+ |
|
| 1052 |
+ return [$s, []]; |
|
| 1053 |
+ } |
|
| 1054 |
+); |
|
| 1055 |
+$rows = $dbi->select( |
|
| 1056 |
+ table => 'table1', |
|
| 1057 |
+ where => {key1 => 1},
|
|
| 1058 |
+ append => "order by key2 {limit 1 0}"
|
|
| 1059 |
+)->all; |
|
| 1060 |
+is_deeply($rows, [{key1 => 1, key2 => 2}]);
|
|
| 1061 |
+$rows = $dbi->select( |
|
| 1062 |
+ table => 'table1', |
|
| 1063 |
+ where => {key1 => 1},
|
|
| 1064 |
+ append => "order by key2 {limit 2 1}"
|
|
| 1065 |
+)->all; |
|
| 1066 |
+is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]);
|
|
| 1067 |
+$rows = $dbi->select( |
|
| 1068 |
+ table => 'table1', |
|
| 1069 |
+ where => {key1 => 1},
|
|
| 1070 |
+ append => "order by key2 {limit 1}"
|
|
| 1071 |
+)->all; |
|
| 1072 |
+is_deeply($rows, [{key1 => 1, key2 => 2}]);
|
|
| 1073 |
+ |
|
| 1074 |
+test 'connect super'; |
|
| 1075 |
+{
|
|
| 1076 |
+ package MyDBI; |
|
| 1077 |
+ |
|
| 1078 |
+ use base 'DBIx::Custom'; |
|
| 1079 |
+ sub connect {
|
|
| 1080 |
+ my $self = shift->SUPER::connect(@_); |
|
| 1081 |
+ |
|
| 1082 |
+ return $self; |
|
| 1083 |
+ } |
|
| 1084 |
+ |
|
| 1085 |
+ sub new {
|
|
| 1086 |
+ my $self = shift->SUPER::new(@_); |
|
| 1087 |
+ |
|
| 1088 |
+ return $self; |
|
| 1089 |
+ } |
|
| 1090 |
+} |
|
| 1091 |
+ |
|
| 1092 |
+$dbi = MyDBI->connect; |
|
| 1093 |
+eval { $dbi->execute('drop table table1') };
|
|
| 1094 |
+$dbi->execute($create_table1); |
|
| 1095 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 1096 |
+is($dbi->select(table => 'table1')->one->{key1}, 1);
|
|
| 1097 |
+ |
|
| 1098 |
+$dbi = MyDBI->new; |
|
| 1099 |
+$dbi->connect; |
|
| 1100 |
+eval { $dbi->execute('drop table table1') };
|
|
| 1101 |
+$dbi->execute($create_table1); |
|
| 1102 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 1103 |
+is($dbi->select(table => 'table1')->one->{key1}, 1);
|
|
| 1104 |
+ |
|
| 1105 |
+{
|
|
| 1106 |
+ package MyDBI2; |
|
| 1107 |
+ |
|
| 1108 |
+ use base 'DBIx::Custom'; |
|
| 1109 |
+ sub connect {
|
|
| 1110 |
+ my $self = shift->SUPER::new(@_); |
|
| 1111 |
+ $self->connect; |
|
| 1112 |
+ |
|
| 1113 |
+ return $self; |
|
| 1114 |
+ } |
|
| 1115 |
+} |
|
| 1116 |
+ |
|
| 1117 |
+$dbi = MyDBI->connect; |
|
| 1118 |
+eval { $dbi->execute('drop table table1') };
|
|
| 1119 |
+$dbi->execute($create_table1); |
|
| 1120 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 1121 |
+is($dbi->select(table => 'table1')->one->{key1}, 1);
|
|
| 1122 |
+ |
|
| 1123 |
+test 'end_filter'; |
|
| 1124 |
+$dbi = DBIx::Custom->connect; |
|
| 1125 |
+eval { $dbi->execute('drop table table1') };
|
|
| 1126 |
+$dbi->execute($create_table1); |
|
| 1127 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 1128 |
+$result = $dbi->select(table => 'table1'); |
|
| 1129 |
+$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
|
|
| 1130 |
+$result->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 });
|
|
| 1131 |
+$row = $result->fetch_first; |
|
| 1132 |
+is_deeply($row, [6, 40]); |
|
| 1133 |
+ |
|
| 1134 |
+$dbi = DBIx::Custom->connect; |
|
| 1135 |
+eval { $dbi->execute('drop table table1') };
|
|
| 1136 |
+$dbi->execute($create_table1); |
|
| 1137 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 1138 |
+$result = $dbi->select(table => 'table1'); |
|
| 1139 |
+$result->filter([qw/key1 key2/] => sub { $_[0] * 2 });
|
|
| 1140 |
+$result->end_filter([[qw/key1 key2/] => sub { $_[0] * 3 }]);
|
|
| 1141 |
+$row = $result->fetch_first; |
|
| 1142 |
+is_deeply($row, [6, 12]); |
|
| 1143 |
+ |
|
| 1144 |
+$dbi = DBIx::Custom->connect; |
|
| 1145 |
+eval { $dbi->execute('drop table table1') };
|
|
| 1146 |
+$dbi->execute($create_table1); |
|
| 1147 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 1148 |
+$result = $dbi->select(table => 'table1'); |
|
| 1149 |
+$result->filter([[qw/key1 key2/] => sub { $_[0] * 2 }]);
|
|
| 1150 |
+$result->end_filter([qw/key1 key2/] => sub { $_[0] * 3 });
|
|
| 1151 |
+$row = $result->fetch_first; |
|
| 1152 |
+is_deeply($row, [6, 12]); |
|
| 1153 |
+ |
|
| 1154 |
+$dbi->register_filter(five_times => sub { $_[0] * 5 });
|
|
| 1155 |
+$result = $dbi->select(table => 'table1'); |
|
| 1156 |
+$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
|
|
| 1157 |
+$result->end_filter({key1 => sub { $_[0] * 3 }, key2 => 'five_times' });
|
|
| 1158 |
+$row = $result->one; |
|
| 1159 |
+is_deeply($row, {key1 => 6, key2 => 40});
|
|
| 1160 |
+ |
|
| 1161 |
+$dbi->register_filter(five_times => sub { $_[0] * 5 });
|
|
| 1162 |
+$dbi->apply_filter('table1',
|
|
| 1163 |
+ key1 => {end => sub { $_[0] * 3 } },
|
|
| 1164 |
+ key2 => {end => 'five_times'}
|
|
| 1165 |
+); |
|
| 1166 |
+$result = $dbi->select(table => 'table1'); |
|
| 1167 |
+$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
|
|
| 1168 |
+$row = $result->one; |
|
| 1169 |
+is_deeply($row, {key1 => 6, key2 => 40}, 'apply_filter');
|
|
| 1170 |
+ |
|
| 1171 |
+$dbi->register_filter(five_times => sub { $_[0] * 5 });
|
|
| 1172 |
+$dbi->apply_filter('table1',
|
|
| 1173 |
+ key1 => {end => sub { $_[0] * 3 } },
|
|
| 1174 |
+ key2 => {end => 'five_times'}
|
|
| 1175 |
+); |
|
| 1176 |
+$result = $dbi->select(table => 'table1'); |
|
| 1177 |
+$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
|
|
| 1178 |
+$result->filter(key1 => undef); |
|
| 1179 |
+$result->end_filter(key1 => undef); |
|
| 1180 |
+$row = $result->one; |
|
| 1181 |
+is_deeply($row, {key1 => 1, key2 => 40}, 'apply_filter overwrite');
|
|
| 1182 |
+ |
|
| 1183 |
+ |
|
| 743 | 1184 |
1; |
| ... | ... |
@@ -68,428 +68,6 @@ my $binary; |
| 68 | 68 |
# Prepare table |
| 69 | 69 |
$dbi = DBIx::Custom->connect; |
| 70 | 70 |
|
| 71 |
-test 'insert'; |
|
| 72 |
-eval { $dbi->execute('drop table table1') };
|
|
| 73 |
-$dbi->execute($create_table1); |
|
| 74 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 75 |
-$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
|
|
| 76 |
-$result = $dbi->execute('select * from table1;');
|
|
| 77 |
-$rows = $result->all; |
|
| 78 |
-is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
|
|
| 79 |
- |
|
| 80 |
-$dbi->execute('delete from table1');
|
|
| 81 |
-$dbi->register_filter( |
|
| 82 |
- twice => sub { $_[0] * 2 },
|
|
| 83 |
- three_times => sub { $_[0] * 3 }
|
|
| 84 |
-); |
|
| 85 |
-$dbi->default_bind_filter('twice');
|
|
| 86 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => 'three_times'});
|
|
| 87 |
-$result = $dbi->execute('select * from table1;');
|
|
| 88 |
-$rows = $result->all; |
|
| 89 |
-is_deeply($rows, [{key1 => 3, key2 => 4}], "filter");
|
|
| 90 |
-$dbi->default_bind_filter(undef); |
|
| 91 |
- |
|
| 92 |
-$dbi->execute('drop table table1');
|
|
| 93 |
-$dbi->execute($create_table1); |
|
| 94 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, append => ' ');
|
|
| 95 |
-$rows = $dbi->select(table => 'table1')->all; |
|
| 96 |
-is_deeply($rows, [{key1 => 1, key2 => 2}], 'insert append');
|
|
| 97 |
- |
|
| 98 |
-eval{$dbi->insert(table => 'table1', noexist => 1)};
|
|
| 99 |
-like($@, qr/noexist/, "invalid"); |
|
| 100 |
- |
|
| 101 |
-eval{$dbi->insert(table => 'table', param => {';' => 1})};
|
|
| 102 |
-like($@, qr/safety/); |
|
| 103 |
- |
|
| 104 |
-$dbi->quote('"');
|
|
| 105 |
-eval { $dbi->execute("drop table ${q}table$p") };
|
|
| 106 |
-$dbi->execute("create table ${q}table$p (${q}select$p)");
|
|
| 107 |
-$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
|
|
| 108 |
-$dbi->insert(table => 'table', param => {select => 1});
|
|
| 109 |
-$result = $dbi->execute("select * from ${q}table$p");
|
|
| 110 |
-$rows = $result->all; |
|
| 111 |
-is_deeply($rows, [{select => 2}], "reserved word");
|
|
| 112 |
- |
|
| 113 |
-eval { $dbi->execute('drop table table1') };
|
|
| 114 |
-$dbi->execute($create_table1); |
|
| 115 |
-$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
|
|
| 116 |
-$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
|
|
| 117 |
-$result = $dbi->execute('select * from table1;');
|
|
| 118 |
-$rows = $result->all; |
|
| 119 |
-is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
|
|
| 120 |
- |
|
| 121 |
-eval { $dbi->execute('drop table table1') };
|
|
| 122 |
-$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
|
|
| 123 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 124 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}, prefix => 'or replace');
|
|
| 125 |
-$result = $dbi->execute('select * from table1;');
|
|
| 126 |
-$rows = $result->all; |
|
| 127 |
-is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
|
|
| 128 |
- |
|
| 129 |
-eval { $dbi->execute('drop table table1') };
|
|
| 130 |
-$dbi->execute($create_table1); |
|
| 131 |
-$dbi->insert(table => 'table1', param => {key1 => \"'1'", key2 => 2});
|
|
| 132 |
-$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
|
|
| 133 |
-$result = $dbi->execute('select * from table1;');
|
|
| 134 |
-$rows = $result->all; |
|
| 135 |
-is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
|
|
| 136 |
- |
|
| 137 |
-test 'update'; |
|
| 138 |
-eval { $dbi->execute('drop table table1') };
|
|
| 139 |
-$dbi->execute($create_table1_2); |
|
| 140 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
|
|
| 141 |
-$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
|
|
| 142 |
-$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1});
|
|
| 143 |
-$result = $dbi->execute('select * from table1;');
|
|
| 144 |
-$rows = $result->all; |
|
| 145 |
-is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
|
|
| 146 |
- {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}],
|
|
| 147 |
- "basic"); |
|
| 148 |
- |
|
| 149 |
-$dbi->execute("delete from table1");
|
|
| 150 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
|
|
| 151 |
-$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
|
|
| 152 |
-$dbi->update(table => 'table1', param => {key2 => 12}, where => {key2 => 2, key3 => 3});
|
|
| 153 |
-$result = $dbi->execute('select * from table1;');
|
|
| 154 |
-$rows = $result->all; |
|
| 155 |
-is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
|
|
| 156 |
- {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}],
|
|
| 157 |
- "update key same as search key"); |
|
| 158 |
- |
|
| 159 |
-$dbi->update(table => 'table1', param => {key2 => [12]}, where => {key2 => 2, key3 => 3});
|
|
| 160 |
-$result = $dbi->execute('select * from table1;');
|
|
| 161 |
-$rows = $result->all; |
|
| 162 |
-is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
|
|
| 163 |
- {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}],
|
|
| 164 |
- "update key same as search key : param is array ref"); |
|
| 165 |
- |
|
| 166 |
-$dbi->execute("delete from table1");
|
|
| 167 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
|
|
| 168 |
-$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
|
|
| 169 |
-$dbi->register_filter(twice => sub { $_[0] * 2 });
|
|
| 170 |
-$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1},
|
|
| 171 |
- filter => {key2 => sub { $_[0] * 2 }});
|
|
| 172 |
-$result = $dbi->execute('select * from table1;');
|
|
| 173 |
-$rows = $result->all; |
|
| 174 |
-is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5},
|
|
| 175 |
- {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}],
|
|
| 176 |
- "filter"); |
|
| 177 |
- |
|
| 178 |
-$result = $dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1}, append => ' ');
|
|
| 179 |
- |
|
| 180 |
-eval{$dbi->update(table => 'table1', where => {key1 => 1}, noexist => 1)};
|
|
| 181 |
-like($@, qr/noexist/, "invalid"); |
|
| 182 |
- |
|
| 183 |
-eval{$dbi->update(table => 'table1')};
|
|
| 184 |
-like($@, qr/where/, "not contain where"); |
|
| 185 |
- |
|
| 186 |
-eval { $dbi->execute('drop table table1') };
|
|
| 187 |
-$dbi->execute($create_table1); |
|
| 188 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 189 |
-$where = $dbi->where; |
|
| 190 |
-$where->clause(['and', 'key1 = :key1', 'key2 = :key2']); |
|
| 191 |
-$where->param({key1 => 1, key2 => 2});
|
|
| 192 |
-$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
|
|
| 193 |
-$result = $dbi->select(table => 'table1'); |
|
| 194 |
-is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
|
|
| 195 |
- |
|
| 196 |
-eval { $dbi->execute('drop table table1') };
|
|
| 197 |
-$dbi->execute($create_table1); |
|
| 198 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 199 |
-$dbi->update( |
|
| 200 |
- table => 'table1', |
|
| 201 |
- param => {key1 => 3},
|
|
| 202 |
- where => [ |
|
| 203 |
- ['and', 'key1 = :key1', 'key2 = :key2'], |
|
| 204 |
- {key1 => 1, key2 => 2}
|
|
| 205 |
- ] |
|
| 206 |
-); |
|
| 207 |
-$result = $dbi->select(table => 'table1'); |
|
| 208 |
-is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
|
|
| 209 |
- |
|
| 210 |
-eval { $dbi->execute('drop table table1') };
|
|
| 211 |
-$dbi->execute($create_table1); |
|
| 212 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 213 |
-$where = $dbi->where; |
|
| 214 |
-$where->clause(['and', 'key2 = :key2']); |
|
| 215 |
-$where->param({key2 => 2});
|
|
| 216 |
-$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
|
|
| 217 |
-$result = $dbi->select(table => 'table1'); |
|
| 218 |
-is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
|
|
| 219 |
- |
|
| 220 |
-eval{$dbi->update(table => 'table1', param => {';' => 1})};
|
|
| 221 |
-like($@, qr/safety/); |
|
| 222 |
- |
|
| 223 |
-eval{$dbi->update(table => 'table1', param => {'key1' => 1}, where => {';' => 1})};
|
|
| 224 |
-like($@, qr/safety/); |
|
| 225 |
- |
|
| 226 |
-eval { $dbi->execute('drop table table1') };
|
|
| 227 |
-$dbi->quote('"');
|
|
| 228 |
-eval { $dbi->execute("drop table ${q}table$p") };
|
|
| 229 |
-$dbi->execute("create table ${q}table$p (${q}select$p, ${q}update$p)");
|
|
| 230 |
-$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
|
|
| 231 |
-$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
|
|
| 232 |
-$dbi->insert(table => 'table', param => {select => 1});
|
|
| 233 |
-$dbi->update(table => 'table', where => {select => 1}, param => {update => 2});
|
|
| 234 |
-$result = $dbi->execute("select * from ${q}table$p");
|
|
| 235 |
-$rows = $result->all; |
|
| 236 |
-is_deeply($rows, [{select => 2, update => 6}], "reserved word");
|
|
| 237 |
- |
|
| 238 |
-eval {$dbi->update_all(table => 'table', param => {';' => 2}) };
|
|
| 239 |
-like($@, qr/safety/); |
|
| 240 |
- |
|
| 241 |
-eval { $dbi->execute("drop table ${q}table$p") };
|
|
| 242 |
-$dbi->reserved_word_quote('"');
|
|
| 243 |
-$dbi->execute("create table ${q}table$p (${q}select$p, ${q}update$p)");
|
|
| 244 |
-$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
|
|
| 245 |
-$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
|
|
| 246 |
-$dbi->insert(table => 'table', param => {select => 1});
|
|
| 247 |
-$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
|
|
| 248 |
-$result = $dbi->execute("select * from ${q}table$p");
|
|
| 249 |
-$rows = $result->all; |
|
| 250 |
-is_deeply($rows, [{select => 2, update => 6}], "reserved word");
|
|
| 251 |
- |
|
| 252 |
-eval { $dbi->execute('drop table table1') };
|
|
| 253 |
-$dbi->execute($create_table1_2); |
|
| 254 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
|
|
| 255 |
-$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
|
|
| 256 |
-$dbi->update({key2 => 11}, table => 'table1', where => {key1 => 1});
|
|
| 257 |
-$result = $dbi->execute('select * from table1;');
|
|
| 258 |
-$rows = $result->all; |
|
| 259 |
-is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
|
|
| 260 |
- {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}],
|
|
| 261 |
- "basic"); |
|
| 262 |
- |
|
| 263 |
-eval { $dbi->execute('drop table table1') };
|
|
| 264 |
-$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
|
|
| 265 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 266 |
-$dbi->update(table => 'table1', param => {key2 => 4},
|
|
| 267 |
- where => {key1 => 1}, prefix => 'or replace');
|
|
| 268 |
-$result = $dbi->execute('select * from table1;');
|
|
| 269 |
-$rows = $result->all; |
|
| 270 |
-is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
|
|
| 271 |
- |
|
| 272 |
-eval { $dbi->execute('drop table table1') };
|
|
| 273 |
-$dbi->execute($create_table1_2); |
|
| 274 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
|
|
| 275 |
-$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
|
|
| 276 |
-$dbi->update(table => 'table1', param => {key2 => \"'11'"}, where => {key1 => 1});
|
|
| 277 |
-$result = $dbi->execute('select * from table1;');
|
|
| 278 |
-$rows = $result->all; |
|
| 279 |
-is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
|
|
| 280 |
- {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}],
|
|
| 281 |
- "basic"); |
|
| 282 |
- |
|
| 283 |
-test 'update_all'; |
|
| 284 |
-eval { $dbi->execute('drop table table1') };
|
|
| 285 |
-$dbi->execute($create_table1_2); |
|
| 286 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
|
|
| 287 |
-$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
|
|
| 288 |
-$dbi->register_filter(twice => sub { $_[0] * 2 });
|
|
| 289 |
-$dbi->update_all(table => 'table1', param => {key2 => 10}, filter => {key2 => 'twice'});
|
|
| 290 |
-$result = $dbi->execute('select * from table1;');
|
|
| 291 |
-$rows = $result->all; |
|
| 292 |
-is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5},
|
|
| 293 |
- {key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}],
|
|
| 294 |
- "filter"); |
|
| 295 |
- |
|
| 296 |
- |
|
| 297 |
-test 'delete'; |
|
| 298 |
-eval { $dbi->execute('drop table table1') };
|
|
| 299 |
-$dbi->execute($create_table1); |
|
| 300 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 301 |
-$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
|
|
| 302 |
-$dbi->delete(table => 'table1', where => {key1 => 1});
|
|
| 303 |
-$result = $dbi->execute('select * from table1;');
|
|
| 304 |
-$rows = $result->all; |
|
| 305 |
-is_deeply($rows, [{key1 => 3, key2 => 4}], "basic");
|
|
| 306 |
- |
|
| 307 |
-$dbi->execute("delete from table1;");
|
|
| 308 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 309 |
-$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
|
|
| 310 |
-$dbi->register_filter(twice => sub { $_[0] * 2 });
|
|
| 311 |
-$dbi->delete(table => 'table1', where => {key2 => 1}, filter => {key2 => 'twice'});
|
|
| 312 |
-$result = $dbi->execute('select * from table1;');
|
|
| 313 |
-$rows = $result->all; |
|
| 314 |
-is_deeply($rows, [{key1 => 3, key2 => 4}], "filter");
|
|
| 315 |
- |
|
| 316 |
-$dbi->delete(table => 'table1', where => {key1 => 1}, append => ' ');
|
|
| 317 |
- |
|
| 318 |
-$dbi->delete_all(table => 'table1'); |
|
| 319 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 320 |
-$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
|
|
| 321 |
-$dbi->delete(table => 'table1', where => {key1 => 1, key2 => 2});
|
|
| 322 |
-$rows = $dbi->select(table => 'table1')->all; |
|
| 323 |
-is_deeply($rows, [{key1 => 3, key2 => 4}], "delete multi key");
|
|
| 324 |
- |
|
| 325 |
-eval{$dbi->delete(table => 'table1', where => {key1 => 1}, noexist => 1)};
|
|
| 326 |
-like($@, qr/noexist/, "invalid"); |
|
| 327 |
- |
|
| 328 |
-eval { $dbi->execute('drop table table1') };
|
|
| 329 |
-$dbi->execute($create_table1); |
|
| 330 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 331 |
-$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
|
|
| 332 |
-$where = $dbi->where; |
|
| 333 |
-$where->clause(['and', 'key1 = :key1', 'key2 = :key2']); |
|
| 334 |
-$where->param({ke1 => 1, key2 => 2});
|
|
| 335 |
-$dbi->delete(table => 'table1', where => $where); |
|
| 336 |
-$result = $dbi->select(table => 'table1'); |
|
| 337 |
-is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where');
|
|
| 338 |
- |
|
| 339 |
-eval { $dbi->execute('drop table table1') };
|
|
| 340 |
-$dbi->execute($create_table1); |
|
| 341 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 342 |
-$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
|
|
| 343 |
-$dbi->delete( |
|
| 344 |
- table => 'table1', |
|
| 345 |
- where => [ |
|
| 346 |
- ['and', 'key1 = :key1', 'key2 = :key2'], |
|
| 347 |
- {ke1 => 1, key2 => 2}
|
|
| 348 |
- ] |
|
| 349 |
-); |
|
| 350 |
-$result = $dbi->select(table => 'table1'); |
|
| 351 |
-is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where');
|
|
| 352 |
- |
|
| 353 |
-eval { $dbi->execute('drop table table1') };
|
|
| 354 |
-$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
|
|
| 355 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 356 |
-$dbi->delete(table => 'table1', where => {key1 => 1}, prefix => ' ');
|
|
| 357 |
-$result = $dbi->execute('select * from table1;');
|
|
| 358 |
-$rows = $result->all; |
|
| 359 |
-is_deeply($rows, [], "basic"); |
|
| 360 |
- |
|
| 361 |
-test 'delete error'; |
|
| 362 |
-eval { $dbi->execute('drop table table1') };
|
|
| 363 |
-$dbi->execute($create_table1); |
|
| 364 |
-eval{$dbi->delete(table => 'table1')};
|
|
| 365 |
-like($@, qr/"where" must be specified/, |
|
| 366 |
- "where key-value pairs not specified"); |
|
| 367 |
- |
|
| 368 |
-eval{$dbi->delete(table => 'table1', where => {';' => 1})};
|
|
| 369 |
-like($@, qr/safety/); |
|
| 370 |
- |
|
| 371 |
-$dbi = DBIx::Custom->connect; |
|
| 372 |
-$dbi->quote('"');
|
|
| 373 |
-eval { $dbi->execute("drop table ${q}table$p") };
|
|
| 374 |
-$dbi->execute("create table ${q}table$p (${q}select$p, ${q}update$p)");
|
|
| 375 |
-$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
|
|
| 376 |
-$dbi->insert(table => 'table', param => {select => 1});
|
|
| 377 |
-$dbi->delete(table => 'table', where => {select => 1});
|
|
| 378 |
-$result = $dbi->execute("select * from ${q}table$p");
|
|
| 379 |
-$rows = $result->all; |
|
| 380 |
-is_deeply($rows, [], "reserved word"); |
|
| 381 |
- |
|
| 382 |
-test 'delete_all'; |
|
| 383 |
-eval { $dbi->execute('drop table table1') };
|
|
| 384 |
-$dbi->execute($create_table1); |
|
| 385 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 386 |
-$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
|
|
| 387 |
-$dbi->delete_all(table => 'table1'); |
|
| 388 |
-$result = $dbi->execute('select * from table1;');
|
|
| 389 |
-$rows = $result->all; |
|
| 390 |
-is_deeply($rows, [], "basic"); |
|
| 391 |
- |
|
| 392 |
- |
|
| 393 |
-test 'select'; |
|
| 394 |
-eval { $dbi->execute('drop table table1') };
|
|
| 395 |
-$dbi->execute($create_table1); |
|
| 396 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 397 |
-$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
|
|
| 398 |
-$rows = $dbi->select(table => 'table1')->all; |
|
| 399 |
-is_deeply($rows, [{key1 => 1, key2 => 2},
|
|
| 400 |
- {key1 => 3, key2 => 4}], "table");
|
|
| 401 |
- |
|
| 402 |
-$rows = $dbi->select(table => 'table1', column => ['key1'])->all; |
|
| 403 |
-is_deeply($rows, [{key1 => 1}, {key1 => 3}], "table and columns and where key");
|
|
| 404 |
- |
|
| 405 |
-$rows = $dbi->select(table => 'table1', where => {key1 => 1})->all;
|
|
| 406 |
-is_deeply($rows, [{key1 => 1, key2 => 2}], "table and columns and where key");
|
|
| 407 |
- |
|
| 408 |
-$rows = $dbi->select(table => 'table1', column => ['key1'], where => {key1 => 3})->all;
|
|
| 409 |
-is_deeply($rows, [{key1 => 3}], "table and columns and where key");
|
|
| 410 |
- |
|
| 411 |
-$rows = $dbi->select(table => 'table1', append => "order by key1 desc limit 1")->all; |
|
| 412 |
-is_deeply($rows, [{key1 => 3, key2 => 4}], "append statement");
|
|
| 413 |
- |
|
| 414 |
-$dbi->register_filter(decrement => sub { $_[0] - 1 });
|
|
| 415 |
-$rows = $dbi->select(table => 'table1', where => {key1 => 2}, filter => {key1 => 'decrement'})
|
|
| 416 |
- ->all; |
|
| 417 |
-is_deeply($rows, [{key1 => 1, key2 => 2}], "filter");
|
|
| 418 |
- |
|
| 419 |
-$dbi->execute($create_table2); |
|
| 420 |
-$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
|
|
| 421 |
-$rows = $dbi->select( |
|
| 422 |
- table => [qw/table1 table2/], |
|
| 423 |
- column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3', |
|
| 424 |
- where => {'table1.key2' => 2},
|
|
| 425 |
- relation => {'table1.key1' => 'table2.key1'}
|
|
| 426 |
-)->all; |
|
| 427 |
-is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : exists where");
|
|
| 428 |
- |
|
| 429 |
-$rows = $dbi->select( |
|
| 430 |
- table => [qw/table1 table2/], |
|
| 431 |
- column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'], |
|
| 432 |
- relation => {'table1.key1' => 'table2.key1'}
|
|
| 433 |
-)->all; |
|
| 434 |
-is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : no exists where");
|
|
| 435 |
- |
|
| 436 |
-eval{$dbi->select(table => 'table1', noexist => 1)};
|
|
| 437 |
-like($@, qr/noexist/, "invalid"); |
|
| 438 |
- |
|
| 439 |
-$dbi = DBIx::Custom->connect; |
|
| 440 |
-$dbi->quote('"');
|
|
| 441 |
-$dbi->execute("create table ${q}table$p (${q}select$p, ${q}update$p)");
|
|
| 442 |
-$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
|
|
| 443 |
-$dbi->insert(table => 'table', param => {select => 1, update => 2});
|
|
| 444 |
-$result = $dbi->select(table => 'table', where => {select => 1});
|
|
| 445 |
-$rows = $result->all; |
|
| 446 |
-is_deeply($rows, [{select => 2, update => 2}], "reserved word");
|
|
| 447 |
- |
|
| 448 |
-test 'fetch filter'; |
|
| 449 |
-eval { $dbi->execute('drop table table1') };
|
|
| 450 |
-$dbi->register_filter( |
|
| 451 |
- twice => sub { $_[0] * 2 },
|
|
| 452 |
- three_times => sub { $_[0] * 3 }
|
|
| 453 |
-); |
|
| 454 |
-$dbi->default_fetch_filter('twice');
|
|
| 455 |
-$dbi->execute($create_table1); |
|
| 456 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 457 |
-$result = $dbi->select(table => 'table1'); |
|
| 458 |
-$result->filter({key1 => 'three_times'});
|
|
| 459 |
-$row = $result->one; |
|
| 460 |
-is_deeply($row, {key1 => 3, key2 => 4}, "default_fetch_filter and filter");
|
|
| 461 |
- |
|
| 462 |
-test 'filters'; |
|
| 463 |
-$dbi = DBIx::Custom->new; |
|
| 464 |
- |
|
| 465 |
-is($dbi->filters->{decode_utf8}->(encode_utf8('あ')),
|
|
| 466 |
- 'あ', "decode_utf8"); |
|
| 467 |
- |
|
| 468 |
-is($dbi->filters->{encode_utf8}->('あ'),
|
|
| 469 |
- encode_utf8('あ'), "encode_utf8");
|
|
| 470 |
- |
|
| 471 |
-test 'transaction'; |
|
| 472 |
-$dbi = DBIx::Custom->connect; |
|
| 473 |
-eval { $dbi->execute('drop table table1') };
|
|
| 474 |
-$dbi->execute($create_table1); |
|
| 475 |
-$dbi->dbh->begin_work; |
|
| 476 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 477 |
-$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
|
|
| 478 |
-$dbi->dbh->commit; |
|
| 479 |
-$result = $dbi->select(table => 'table1'); |
|
| 480 |
-is_deeply(scalar $result->all, [{key1 => 1, key2 => 2}, {key1 => 2, key2 => 3}],
|
|
| 481 |
- "commit"); |
|
| 482 |
- |
|
| 483 |
-$dbi = DBIx::Custom->connect; |
|
| 484 |
-eval { $dbi->execute('drop table table1') };
|
|
| 485 |
-$dbi->execute($create_table1); |
|
| 486 |
-$dbi->dbh->begin_work(0); |
|
| 487 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 488 |
-$dbi->dbh->rollback; |
|
| 489 |
- |
|
| 490 |
-$result = $dbi->select(table => 'table1'); |
|
| 491 |
-ok(! $result->fetch_first, "rollback"); |
|
| 492 |
- |
|
| 493 | 71 |
test 'cache'; |
| 494 | 72 |
eval { $dbi->execute('drop table table1') };
|
| 495 | 73 |
$dbi->cache(1); |