... | ... |
@@ -1,7 +1,6 @@ |
1 | 1 |
use Test::More; |
2 | 2 |
use strict; |
3 | 3 |
use warnings; |
4 |
-use DBI qw/:sql_types/; |
|
5 | 4 |
|
6 | 5 |
BEGIN { |
7 | 6 |
eval { require DBD::SQLite; 1 } |
... | ... |
@@ -24,11 +23,16 @@ our $CREATE_TABLE = { |
24 | 23 |
0 => 'create table table1 (key1 char(255), key2 char(255));' |
25 | 24 |
}; |
26 | 25 |
|
26 |
+our $SELECT_TMPL = { |
|
27 |
+ 0 => 'select key1, key2 from table1;' |
|
28 |
+}; |
|
29 |
+ |
|
27 | 30 |
my $dbi; |
28 | 31 |
my $sth; |
29 | 32 |
my $tmpl; |
30 | 33 |
my $select_tmpl; |
31 | 34 |
my $insert_tmpl; |
35 |
+my $update_tmpl; |
|
32 | 36 |
my $params; |
33 | 37 |
my $sql; |
34 | 38 |
my $result; |
... | ... |
@@ -37,10 +41,12 @@ my $rows; |
37 | 41 |
my $query; |
38 | 42 |
my $select_query; |
39 | 43 |
my $insert_query; |
44 |
+my $update_query; |
|
40 | 45 |
my $ret_val; |
41 | 46 |
|
42 | 47 |
|
43 | 48 |
|
49 |
+ |
|
44 | 50 |
# Prepare table |
45 | 51 |
$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:'); |
46 | 52 |
$dbi->connect; |
... | ... |
@@ -105,11 +111,19 @@ $result = $dbi->execute($query); |
105 | 111 |
@rows = $result->fetch_all; |
106 | 112 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_all_hash list context"); |
107 | 113 |
|
114 |
+test 'Insert query return value'; |
|
115 |
+$dbi->reconnect; |
|
116 |
+$dbi->do($CREATE_TABLE->{0}); |
|
117 |
+$tmpl = "insert into table1 {insert key1 key2}"; |
|
118 |
+$query = $dbi->create_query($tmpl); |
|
119 |
+$ret_val = $dbi->execute($query, {key1 => 1, key2 => 2}); |
|
120 |
+ok($ret_val, $test); |
|
121 |
+ |
|
108 | 122 |
test 'Filter'; |
109 | 123 |
$dbi->reconnect; |
110 | 124 |
$dbi->do($CREATE_TABLE->{0}); |
111 | 125 |
|
112 |
-$insert_tmpl = "insert into table1 {insert_values key1 key2};"; |
|
126 |
+$insert_tmpl = "insert into table1 {insert key1 key2};"; |
|
113 | 127 |
$insert_query = $dbi->create_query($insert_tmpl); |
114 | 128 |
$insert_query->bind_filter(sub { |
115 | 129 |
my ($key, $value, $table, $column) = @_; |
... | ... |
@@ -119,11 +133,9 @@ $insert_query->bind_filter(sub { |
119 | 133 |
return $value; |
120 | 134 |
}); |
121 | 135 |
|
122 |
-$ret_val = $dbi->execute($insert_query, {key1 => 1, key2 => 2}); |
|
123 |
-ok($ret_val, "Insert success return value"); |
|
136 |
+$dbi->execute($insert_query, {key1 => 1, key2 => 2}); |
|
124 | 137 |
|
125 |
-$select_tmpl = "select key1, key2 from table1"; |
|
126 |
-$select_query = $dbi->create_query($select_tmpl); |
|
138 |
+$select_query = $dbi->create_query($SELECT_TMPL->{0}); |
|
127 | 139 |
$select_query->fetch_filter(sub { |
128 | 140 |
my ($key, $value, $type, $sth, $i) = @_; |
129 | 141 |
if ($key eq 'key2' && $type =~ /char/ && $sth->can('execute') && $i == 1) { |
... | ... |
@@ -136,6 +148,27 @@ $result = $dbi->execute($select_query); |
136 | 148 |
$rows = $result->fetch_all_hash; |
137 | 149 |
is_deeply($rows, [{key1 => 2, key2 => 6}], "$test : bind_filter fetch_filter"); |
138 | 150 |
|
151 |
+$dbi->reconnect; |
|
152 |
+$dbi->do($CREATE_TABLE->{0}); |
|
153 |
+$insert_tmpl = "insert into table1 {insert table1.key1 table1.key2}"; |
|
154 |
+ |
|
155 |
+$insert_query = $dbi->create_query($insert_tmpl); |
|
156 |
+$insert_query->bind_filter(sub { |
|
157 |
+ my ($key, $value, $table, $column) = @_; |
|
158 |
+ if ($key eq 'table1.key1' && $table eq 'table1' && $column eq 'key1') { |
|
159 |
+ return $value * 3; |
|
160 |
+ } |
|
161 |
+ return $value; |
|
162 |
+}); |
|
163 |
+ |
|
164 |
+$dbi->execute($insert_query, {table1 => {key1 => 1, key2 => 2}}); |
|
165 |
+ |
|
166 |
+$select_query = $dbi->create_query($SELECT_TMPL->{0}); |
|
167 |
+$result = $dbi->execute($select_query); |
|
168 |
+$rows = $result->fetch_all_hash; |
|
169 |
+is_deeply($rows, [{key1 => 3, key2 => 2}], "$test : insert with table name"); |
|
170 |
+ |
|
171 |
+ |
|
139 | 172 |
__END__ |
140 | 173 |
|
141 | 174 |
$dbi->fetch_filter(sub { |
... | ... |
@@ -191,8 +224,8 @@ is_deeply(\@bind, ['g'], 'sql template bind2' ); |
191 | 224 |
|
192 | 225 |
# Insert values |
193 | 226 |
$dbi = DBI::Custom->new; |
194 |
-$tmpl = "insert into table {insert_values}"; |
|
195 |
-$params = {insert_values => {key1 => 'a', key2 => 'b'}}; |
|
227 |
+$tmpl = "insert into table {insert}"; |
|
228 |
+$params = {insert => {key1 => 'a', key2 => 'b'}}; |
|
196 | 229 |
|
197 | 230 |
$dbi->filters(filter => sub { |
198 | 231 |
my ($key, $value) = @_; |
... | ... |
@@ -208,8 +241,8 @@ is_deeply(\@bind, ['A', 'b'], 'sql template bind' ); |
208 | 241 |
|
209 | 242 |
# Update set |
210 | 243 |
$dbi = DBI::Custom->new; |
211 |
-$tmpl = "update table {update_set}"; |
|
212 |
-$params = {update_set => {key1 => 'a', key2 => 'b'}}; |
|
244 |
+$tmpl = "update table {update}"; |
|
245 |
+$params = {update => {key1 => 'a', key2 => 'b'}}; |
|
213 | 246 |
|
214 | 247 |
$dbi->filters(filter => sub { |
215 | 248 |
my ($key, $value) = @_; |