add test
|
1 |
use Test::More; |
2 |
use strict; |
|
3 |
use warnings; |
|
4 |
|
|
5 |
BEGIN { |
|
6 |
eval { require DBD::SQLite; 1 } |
|
7 |
or plan skip_all => 'DBD::SQLite required'; |
|
8 |
eval { DBD::SQLite->VERSION >= 1 } |
|
9 |
or plan skip_all => 'DBD::SQLite >= 1.00 required'; |
|
10 |
|
|
11 |
plan 'no_plan'; |
|
12 |
use_ok('DBI::Custom'); |
|
13 |
} |
|
14 |
|
|
cleanup
|
15 |
# Function for test name |
16 |
my $test; |
|
17 |
sub test { |
|
18 |
$test = shift; |
|
19 |
} |
|
add test
|
20 |
|
cleanup
|
21 |
|
22 |
|
|
cleanup#
|
23 |
# Varialbes for test |
add prepare
|
24 |
our $CREATE_TABLE = { |
add tests
|
25 |
0 => 'create table table1 (key1 char(255), key2 char(255));', |
26 |
1 => 'create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));' |
|
add prepare
|
27 |
}; |
28 |
|
|
add tests
|
29 |
our $SELECT_TMPL = { |
add tests
|
30 |
0 => 'select * from table1;' |
add tests
|
31 |
}; |
32 |
|
|
cleanup
|
33 |
our $DROP_TABLE = { |
34 |
0 => 'drop table table1' |
|
35 |
}; |
|
36 |
|
|
cleanup
|
37 |
my $dbi; |
38 |
my $sth; |
|
39 |
my $tmpl; |
|
cleanup#
|
40 |
my $select_tmpl; |
41 |
my $insert_tmpl; |
|
add tests
|
42 |
my $update_tmpl; |
cleanup
|
43 |
my $params; |
44 |
my $sql; |
|
45 |
my $result; |
|
46 |
my @rows; |
|
47 |
my $rows; |
|
add tests
|
48 |
my $query; |
cleanup#
|
49 |
my $select_query; |
50 |
my $insert_query; |
|
add tests
|
51 |
my $update_query; |
add prepare
|
52 |
my $ret_val; |
cleanup#
|
53 |
|
cleanup
|
54 |
|
cleanup
|
55 |
test 'disconnect'; |
add tests
|
56 |
$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:'); |
57 |
$dbi->connect; |
|
58 |
$dbi->disconnect; |
|
59 |
ok(!$dbi->dbh, $test); |
|
add tests
|
60 |
|
cleanup
|
61 |
test 'connected'; |
62 |
$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:'); |
|
63 |
ok(!$dbi->connected, "$test : not connected"); |
|
64 |
$dbi->connect; |
|
65 |
ok($dbi->connected, "$test : connected"); |
|
66 |
|
|
cleanup
|
67 |
# Prepare table |
68 |
$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:'); |
|
69 |
$dbi->connect; |
|
add prepare
|
70 |
$dbi->do($CREATE_TABLE->{0}); |
71 |
$sth = $dbi->prepare("insert into table1 (key1, key2) values (?, ?);"); |
|
cleanup
|
72 |
$sth->execute(1, 2); |
73 |
$sth->execute(3, 4); |
|
add test
|
74 |
|
add test module
|
75 |
|
add tests
|
76 |
test 'DBI::Custom::Result test'; |
77 |
$tmpl = "select key1, key2 from table1"; |
|
78 |
$query = $dbi->create_query($tmpl); |
|
79 |
$result = $dbi->execute($query); |
|
cleanup
|
80 |
|
81 |
@rows = (); |
|
82 |
while (my $row = $result->fetch) { |
|
83 |
push @rows, [@$row]; |
|
add test module
|
84 |
} |
add tests
|
85 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch scalar context"); |
cleanup
|
86 |
|
add tests
|
87 |
$result = $dbi->execute($query); |
cleanup
|
88 |
@rows = (); |
89 |
while (my @row = $result->fetch) { |
|
90 |
push @rows, [@row]; |
|
add test module
|
91 |
} |
add tests
|
92 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch list context"); |
add test module
|
93 |
|
add tests
|
94 |
$result = $dbi->execute($query); |
cleanup
|
95 |
@rows = (); |
96 |
while (my $row = $result->fetch_hash) { |
|
97 |
push @rows, {%$row}; |
|
add test module
|
98 |
} |
add tests
|
99 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch_hash scalar context"); |
cleanup
|
100 |
|
add tests
|
101 |
$result = $dbi->execute($query); |
cleanup
|
102 |
@rows = (); |
103 |
while (my %row = $result->fetch_hash) { |
|
104 |
push @rows, {%row}; |
|
add test module
|
105 |
} |
add tests
|
106 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch hash list context"); |
add test module
|
107 |
|
add tests
|
108 |
$result = $dbi->execute($query); |
cleanup
|
109 |
$rows = $result->fetch_all; |
add tests
|
110 |
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all scalar context"); |
cleanup
|
111 |
|
add tests
|
112 |
$result = $dbi->execute($query); |
cleanup
|
113 |
@rows = $result->fetch_all; |
add tests
|
114 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_all list context"); |
cleanup
|
115 |
|
add tests
|
116 |
$result = $dbi->execute($query); |
cleanup
|
117 |
@rows = $result->fetch_all_hash; |
add tests
|
118 |
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all_hash scalar context"); |
cleanup
|
119 |
|
add tests
|
120 |
$result = $dbi->execute($query); |
cleanup
|
121 |
@rows = $result->fetch_all; |
add tests
|
122 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_all_hash list context"); |
cleanup
|
123 |
|
add tests
|
124 |
|
add tests
|
125 |
test 'Insert query return value'; |
cleanup
|
126 |
$dbi->do($DROP_TABLE->{0}); |
add tests
|
127 |
$dbi->do($CREATE_TABLE->{0}); |
128 |
$tmpl = "insert into table1 {insert key1 key2}"; |
|
129 |
$query = $dbi->create_query($tmpl); |
|
130 |
$ret_val = $dbi->execute($query, {key1 => 1, key2 => 2}); |
|
131 |
ok($ret_val, $test); |
|
132 |
|
|
add tests
|
133 |
|
134 |
test 'Direct execute'; |
|
cleanup
|
135 |
$dbi->do($DROP_TABLE->{0}); |
add tests
|
136 |
$dbi->do($CREATE_TABLE->{0}); |
137 |
$insert_tmpl = "insert into table1 {insert key1 key2}"; |
|
138 |
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2}, sub { |
|
139 |
my $query = shift; |
|
140 |
$query->bind_filter(sub { |
|
141 |
my ($key, $value) = @_; |
|
142 |
if ($key eq 'key2') { |
|
143 |
return $value + 1; |
|
144 |
} |
|
145 |
return $value; |
|
146 |
}); |
|
147 |
}); |
|
148 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
149 |
$rows = $result->fetch_all_hash; |
|
150 |
is_deeply($rows, [{key1 => 1, key2 => 3}], $test); |
|
151 |
|
|
152 |
|
|
add tests
|
153 |
test 'Filter basic'; |
cleanup
|
154 |
$dbi->do($DROP_TABLE->{0}); |
add tests
|
155 |
$dbi->do($CREATE_TABLE->{0}); |
cleanup#
|
156 |
|
add tests
|
157 |
$insert_tmpl = "insert into table1 {insert key1 key2};"; |
add prepare
|
158 |
$insert_query = $dbi->create_query($insert_tmpl); |
159 |
$insert_query->bind_filter(sub { |
|
add tests
|
160 |
my ($key, $value, $table, $column) = @_; |
161 |
if ($key eq 'key1' && $table eq '' && $column eq 'key1') { |
|
add prepare
|
162 |
return $value * 2; |
cleanup#
|
163 |
} |
164 |
return $value; |
|
165 |
}); |
|
add tests
|
166 |
$dbi->execute($insert_query, {key1 => 1, key2 => 2}); |
167 |
$select_query = $dbi->create_query($SELECT_TMPL->{0}); |
|
add prepare
|
168 |
$select_query->fetch_filter(sub { |
add tests
|
169 |
my ($key, $value, $type, $sth, $i) = @_; |
170 |
if ($key eq 'key2' && $type =~ /char/ && $sth->can('execute') && $i == 1) { |
|
add prepare
|
171 |
return $value * 3; |
172 |
} |
|
173 |
return $value; |
|
174 |
}); |
|
175 |
$result = $dbi->execute($select_query); |
|
176 |
$rows = $result->fetch_all_hash; |
|
add tests
|
177 |
is_deeply($rows, [{key1 => 2, key2 => 6}], "$test : bind_filter fetch_filter"); |
cleanup#
|
178 |
|
add tests
|
179 |
$dbi->do("delete from table1;"); |
180 |
$insert_query->no_bind_filters('key1'); |
|
181 |
$select_query->no_fetch_filters('key2'); |
|
182 |
$dbi->execute($insert_query, {key1 => 1, key2 => 2}); |
|
183 |
$result = $dbi->execute($select_query); |
|
184 |
$rows = $result->fetch_all_hash; |
|
add tests
|
185 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : no_fetch_filters no_bind_filters"); |
add tests
|
186 |
|
cleanup
|
187 |
$dbi->do($DROP_TABLE->{0}); |
add tests
|
188 |
$dbi->do($CREATE_TABLE->{0}); |
189 |
$insert_tmpl = "insert into table1 {insert table1.key1 table1.key2}"; |
|
190 |
$insert_query = $dbi->create_query($insert_tmpl); |
|
191 |
$insert_query->bind_filter(sub { |
|
192 |
my ($key, $value, $table, $column) = @_; |
|
193 |
if ($key eq 'table1.key1' && $table eq 'table1' && $column eq 'key1') { |
|
194 |
return $value * 3; |
|
195 |
} |
|
196 |
return $value; |
|
197 |
}); |
|
198 |
$dbi->execute($insert_query, {table1 => {key1 => 1, key2 => 2}}); |
|
199 |
$select_query = $dbi->create_query($SELECT_TMPL->{0}); |
|
200 |
$result = $dbi->execute($select_query); |
|
201 |
$rows = $result->fetch_all_hash; |
|
202 |
is_deeply($rows, [{key1 => 3, key2 => 2}], "$test : insert with table name"); |
|
203 |
|
|
add tests
|
204 |
test 'Filter in'; |
205 |
$insert_tmpl = "insert into table1 {insert key1 key2};"; |
|
206 |
$insert_query = $dbi->create_query($insert_tmpl); |
|
207 |
$dbi->execute($insert_query, {key1 => 2, key2 => 4}); |
|
208 |
$select_tmpl = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}"; |
|
209 |
$select_query = $dbi->create_query($select_tmpl); |
|
210 |
$select_query->bind_filter(sub { |
|
211 |
my ($key, $value, $table, $column) = @_; |
|
212 |
if ($key eq 'table1.key1' && $table eq 'table1' && $column eq 'key1' || $key eq 'table1.key2') { |
|
213 |
return $value * 2; |
|
214 |
} |
|
215 |
return $value; |
|
216 |
}); |
|
217 |
$result = $dbi->execute($select_query, {table1 => {key1 => [1,5], key2 => [2,5]}}); |
|
218 |
$rows = $result->fetch_all_hash; |
|
219 |
is_deeply($rows, [{key1 => 2, key2 => 4}], "$test : bind_filter"); |
|
220 |
|
|
add tests
|
221 |
|
add tests
|
222 |
test 'DBI::Custom::SQL::Template basic tag'; |
cleanup
|
223 |
$dbi->do($DROP_TABLE->{0}); |
add tests
|
224 |
$dbi->do($CREATE_TABLE->{1}); |
225 |
$sth = $dbi->prepare("insert into table1 (key1, key2, key3, key4, key5) values (?, ?, ?, ?, ?);"); |
|
226 |
$sth->execute(1, 2, 3, 4, 5); |
|
227 |
$sth->execute(6, 7, 8, 9, 10); |
|
add tests
|
228 |
|
add tests
|
229 |
$tmpl = "select * from table1 where {= key1} and {<> key2} and {< key3} and {> key4} and {>= key5};"; |
230 |
$query = $dbi->create_query($tmpl); |
|
231 |
$result = $dbi->execute($query, {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5}); |
|
cleanup
|
232 |
$rows = $result->fetch_all_hash; |
add tests
|
233 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1"); |
cleanup
|
234 |
|
add tests
|
235 |
$tmpl = "select * from table1 where {= table1.key1} and {<> table1.key2} and {< table1.key3} and {> table1.key4} and {>= table1.key5};"; |
236 |
$query = $dbi->create_query($tmpl); |
|
237 |
$result = $dbi->execute($query, {table1 => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5}}); |
|
238 |
$rows = $result->fetch_all_hash; |
|
239 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1 with table"); |
|
240 |
|
|
241 |
$tmpl = "select * from table1 where {= table1.key1} and {<> table1.key2} and {< table1.key3} and {> table1.key4} and {>= table1.key5};"; |
|
242 |
$query = $dbi->create_query($tmpl); |
|
243 |
$result = $dbi->execute($query, {'table1.key1' => 1, 'table1.key2' => 3, 'table1.key3' => 4, 'table1.key4' => 3, 'table1.key5' => 5}); |
|
244 |
$rows = $result->fetch_all_hash; |
|
245 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1 with table dot"); |
|
246 |
|
|
add tests
|
247 |
$tmpl = "select * from table1 where {<= key1} and {like key2};"; |
248 |
$query = $dbi->create_query($tmpl); |
|
249 |
$result = $dbi->execute($query, {key1 => 1, key2 => '%2%'}); |
|
250 |
$rows = $result->fetch_all_hash; |
|
251 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2"); |
|
cleanup
|
252 |
|
add tests
|
253 |
$tmpl = "select * from table1 where {<= table1.key1} and {like table1.key2};"; |
254 |
$query = $dbi->create_query($tmpl); |
|
255 |
$result = $dbi->execute($query, {table1 => {key1 => 1, key2 => '%2%'}}); |
|
256 |
$rows = $result->fetch_all_hash; |
|
257 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2 with table"); |
|
258 |
|
|
259 |
$tmpl = "select * from table1 where {<= table1.key1} and {like table1.key2};"; |
|
260 |
$query = $dbi->create_query($tmpl); |
|
261 |
$result = $dbi->execute($query, {'table1.key1' => 1, 'table1.key2' => '%2%'}); |
|
262 |
$rows = $result->fetch_all_hash; |
|
263 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2 with table dot"); |
|
264 |
|
|
265 |
|
|
add tests
|
266 |
test 'DIB::Custom::SQL::Template in tag'; |
cleanup
|
267 |
$dbi->do($DROP_TABLE->{0}); |
add tests
|
268 |
$dbi->do($CREATE_TABLE->{1}); |
269 |
$sth = $dbi->prepare("insert into table1 (key1, key2, key3, key4, key5) values (?, ?, ?, ?, ?);"); |
|
270 |
$sth->execute(1, 2, 3, 4, 5); |
|
271 |
$sth->execute(6, 7, 8, 9, 10); |
|
272 |
|
|
273 |
$tmpl = "select * from table1 where {in key1 2};"; |
|
274 |
$query = $dbi->create_query($tmpl); |
|
275 |
$result = $dbi->execute($query, {key1 => [9, 1]}); |
|
276 |
$rows = $result->fetch_all_hash; |
|
277 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic"); |
|
278 |
|
|
279 |
$tmpl = "select * from table1 where {in table1.key1 2};"; |
|
280 |
$query = $dbi->create_query($tmpl); |
|
281 |
$result = $dbi->execute($query, {table1 => {key1 => [9, 1]}}); |
|
282 |
$rows = $result->fetch_all_hash; |
|
283 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table"); |
|
add tests
|
284 |
|
add tests
|
285 |
$tmpl = "select * from table1 where {in table1.key1 2};"; |
286 |
$query = $dbi->create_query($tmpl); |
|
287 |
$result = $dbi->execute($query, {'table1.key1' => [9, 1]}); |
|
288 |
$rows = $result->fetch_all_hash; |
|
289 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table dot"); |
|
add tests
|
290 |
|
cleanup
|
291 |
|
add tests
|
292 |
test 'DBI::Custom::SQL::Template insert tag'; |
add tests
|
293 |
$dbi->do("delete from table1"); |
294 |
$insert_tmpl = 'insert into table1 {insert key1 key2 key3 key4 key5}'; |
|
295 |
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
cleanup
|
296 |
|
add tests
|
297 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
add tests
|
298 |
$rows = $result->fetch_all_hash; |
add tests
|
299 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic"); |
cleanup
|
300 |
|
add tests
|
301 |
$dbi->do("delete from table1"); |
302 |
$dbi->execute($insert_tmpl, {'#insert' => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}}); |
|
303 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
304 |
$rows = $result->fetch_all_hash; |
|
add tests
|
305 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : #insert"); |
add tests
|
306 |
|
307 |
$dbi->do("delete from table1"); |
|
308 |
$insert_tmpl = 'insert into table1 {insert table1.key1 table1.key2 table1.key3 table1.key4 table1.key5}'; |
|
309 |
$dbi->execute($insert_tmpl, {table1 => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}}); |
|
310 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
311 |
$rows = $result->fetch_all_hash; |
|
add tests
|
312 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table name"); |
313 |
|
|
add tests
|
314 |
$dbi->do("delete from table1"); |
315 |
$insert_tmpl = 'insert into table1 {insert table1.key1 table1.key2 table1.key3 table1.key4 table1.key5}'; |
|
316 |
$dbi->execute($insert_tmpl, {'table1.key1' => 1, 'table1.key2' => 2, 'table1.key3' => 3, 'table1.key4' => 4, 'table1.key5' => 5}); |
|
317 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
318 |
$rows = $result->fetch_all_hash; |
|
319 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table name dot"); |
|
add tests
|
320 |
|
321 |
$dbi->do("delete from table1"); |
|
322 |
$dbi->execute($insert_tmpl, {'#insert' => {table1 => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}}}); |
|
323 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
324 |
$rows = $result->fetch_all_hash; |
|
add tests
|
325 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : #insert with table name"); |
add tests
|
326 |
|
add tests
|
327 |
$dbi->do("delete from table1"); |
328 |
$dbi->execute($insert_tmpl, {'#insert' => {'table1.key1' => 1, 'table1.key2' => 2, 'table1.key3' => 3, 'table1.key4' => 4, 'table1.key5' => 5}}); |
|
329 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
330 |
$rows = $result->fetch_all_hash; |
|
331 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : #insert with table name dot"); |
|
332 |
|
|
add tests
|
333 |
|
add tests
|
334 |
test 'DBI::Custom::SQL::Template update tag'; |
add tests
|
335 |
$dbi->do("delete from table1"); |
336 |
$insert_tmpl = "insert into table1 {insert key1 key2 key3 key4 key5}"; |
|
337 |
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
338 |
$dbi->execute($insert_tmpl, {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
339 |
|
|
340 |
$update_tmpl = 'update table1 {update key1 key2 key3 key4} where {= key5}'; |
|
341 |
$dbi->execute($update_tmpl, {key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5}); |
|
342 |
|
|
343 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
344 |
$rows = $result->fetch_all_hash; |
|
345 |
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5}, |
|
add tests
|
346 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : basic"); |
add tests
|
347 |
|
add tests
|
348 |
$dbi->execute($update_tmpl, {'#update' => {key1 => 2, key2 => 2, key3 => 2, key4 => 2}, key5 => 5}); |
add tests
|
349 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
350 |
$rows = $result->fetch_all_hash; |
|
add tests
|
351 |
is_deeply($rows, [{key1 => 2, key2 => 2, key3 => 2, key4 => 2, key5 => 5}, |
352 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : #update"); |
|
add tests
|
353 |
|
add tests
|
354 |
$update_tmpl = 'update table1 {update table1.key1 table1.key2 table1.key3 table1.key4} where {= table1.key5}'; |
355 |
$dbi->execute($update_tmpl, {table1 => {key1 => 3, key2 => 3, key3 => 3, key4 => 3, key5 => 5}}); |
|
add tests
|
356 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
357 |
$rows = $result->fetch_all_hash; |
|
add tests
|
358 |
is_deeply($rows, [{key1 => 3, key2 => 3, key3 => 3, key4 => 3, key5 => 5}, |
359 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : with table name"); |
|
add tests
|
360 |
|
add tests
|
361 |
$update_tmpl = 'update table1 {update table1.key1 table1.key2 table1.key3 table1.key4} where {= table1.key5}'; |
362 |
$dbi->execute($update_tmpl, {'table1.key1' => 4, 'table1.key2' => 4, 'table1.key3' => 4, 'table1.key4' => 4, 'table1.key5' => 5}); |
|
add tests
|
363 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
364 |
$rows = $result->fetch_all_hash; |
|
add tests
|
365 |
is_deeply($rows, [{key1 => 4, key2 => 4, key3 => 4, key4 => 4, key5 => 5}, |
add tests
|
366 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : with table name dot"); |
cleanup
|
367 |
|
add tests
|
368 |
$dbi->execute($update_tmpl, {'#update' => {table1 => {key1 => 5, key2 => 5, key3 => 5, key4 => 5}}, table1 => {key5 => 5}}); |
369 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
370 |
$rows = $result->fetch_all_hash; |
|
371 |
is_deeply($rows, [{key1 => 5, key2 => 5, key3 => 5, key4 => 5, key5 => 5}, |
|
add tests
|
372 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : update tag #update with table name"); |
cleanup
|
373 |
|
add tests
|
374 |
$dbi->execute($update_tmpl, {'#update' => {'table1.key1' => 6, 'table1.key2' => 6, 'table1.key3' => 6, 'table1.key4' => 6}, 'table1.key5' => 5}); |
375 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
376 |
$rows = $result->fetch_all_hash; |
|
377 |
is_deeply($rows, [{key1 => 6, key2 => 6, key3 => 6, key4 => 6, key5 => 5}, |
|
add tests
|
378 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : update tag #update with table name dot"); |
add tests
|
379 |
|
cleanup
|
380 |
|
381 |
test 'run_tansaction'; |
|
382 |
$dbi->do($DROP_TABLE->{0}); |
|
383 |
$dbi->do($CREATE_TABLE->{0}); |
|
384 |
$dbi->run_tranzaction(sub { |
|
385 |
$insert_tmpl = 'insert into table1 {insert key1 key2}'; |
|
386 |
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2}); |
|
387 |
$dbi->execute($insert_tmpl, {key1 => 3, key2 => 4}); |
|
388 |
}); |
|
389 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
390 |
$rows = $result->fetch_all_hash; |
|
391 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : commit"); |
|
392 |
|
|
393 |
$dbi->do($DROP_TABLE->{0}); |
|
394 |
$dbi->do($CREATE_TABLE->{0}); |
|
395 |
$dbi->dbh->{RaiseError} = 0; |
|
396 |
eval{ |
|
397 |
$dbi->run_tranzaction(sub { |
|
398 |
$insert_tmpl = 'insert into table1 {insert key1 key2}'; |
|
399 |
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2}); |
|
400 |
die "Fatal Error"; |
|
401 |
$dbi->execute($insert_tmpl, {key1 => 3, key2 => 4}); |
|
402 |
}) |
|
403 |
}; |
|
404 |
like($@, qr/Fatal Error.*Rollback is success/ms, "$test : Rollback success message"); |
|
405 |
ok(!$dbi->dbh->{RaiseError}, "$test : restore RaiseError value"); |
|
406 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
407 |
$rows = $result->fetch_all_hash; |
|
408 |
is_deeply($rows, [], "$test : rollback"); |
|
409 |
|
|
410 |
|