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 |
|
add tests
|
61 |
|
cleanup
|
62 |
test 'connected'; |
63 |
$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:'); |
|
64 |
ok(!$dbi->connected, "$test : not connected"); |
|
65 |
$dbi->connect; |
|
66 |
ok($dbi->connected, "$test : connected"); |
|
67 |
|
|
add tests
|
68 |
|
69 |
test 'preapare'; |
|
70 |
$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:'); |
|
71 |
$sth = $dbi->prepare($CREATE_TABLE->{0}); |
|
72 |
ok($sth, "$test : auto connect"); |
|
73 |
$sth->execute; |
|
74 |
$sth = $dbi->prepare($DROP_TABLE->{0}); |
|
75 |
ok($sth, "$test : basic"); |
|
76 |
|
|
77 |
|
|
78 |
test 'do'; |
|
79 |
$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:'); |
|
80 |
$ret_val = $dbi->do($CREATE_TABLE->{0}); |
|
81 |
ok(defined $ret_val, "$test : auto connect"); |
|
82 |
$ret_val = $dbi->do($DROP_TABLE->{0}); |
|
83 |
ok(defined $ret_val, "$test : basic"); |
|
84 |
|
|
85 |
|
|
cleanup
|
86 |
# Prepare table |
87 |
$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:'); |
|
88 |
$dbi->connect; |
|
add prepare
|
89 |
$dbi->do($CREATE_TABLE->{0}); |
90 |
$sth = $dbi->prepare("insert into table1 (key1, key2) values (?, ?);"); |
|
cleanup
|
91 |
$sth->execute(1, 2); |
92 |
$sth->execute(3, 4); |
|
add test
|
93 |
|
add test module
|
94 |
|
add tests
|
95 |
test 'DBI::Custom::Result test'; |
96 |
$tmpl = "select key1, key2 from table1"; |
|
97 |
$query = $dbi->create_query($tmpl); |
|
98 |
$result = $dbi->execute($query); |
|
cleanup
|
99 |
|
100 |
@rows = (); |
|
101 |
while (my $row = $result->fetch) { |
|
102 |
push @rows, [@$row]; |
|
add test module
|
103 |
} |
add tests
|
104 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch scalar context"); |
cleanup
|
105 |
|
add tests
|
106 |
$result = $dbi->execute($query); |
cleanup
|
107 |
@rows = (); |
108 |
while (my @row = $result->fetch) { |
|
109 |
push @rows, [@row]; |
|
add test module
|
110 |
} |
add tests
|
111 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch list context"); |
add test module
|
112 |
|
add tests
|
113 |
$result = $dbi->execute($query); |
cleanup
|
114 |
@rows = (); |
115 |
while (my $row = $result->fetch_hash) { |
|
116 |
push @rows, {%$row}; |
|
add test module
|
117 |
} |
add tests
|
118 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch_hash scalar context"); |
cleanup
|
119 |
|
add tests
|
120 |
$result = $dbi->execute($query); |
cleanup
|
121 |
@rows = (); |
122 |
while (my %row = $result->fetch_hash) { |
|
123 |
push @rows, {%row}; |
|
add test module
|
124 |
} |
add tests
|
125 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch hash list context"); |
add test module
|
126 |
|
add tests
|
127 |
$result = $dbi->execute($query); |
cleanup
|
128 |
$rows = $result->fetch_all; |
add tests
|
129 |
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all scalar context"); |
cleanup
|
130 |
|
add tests
|
131 |
$result = $dbi->execute($query); |
cleanup
|
132 |
@rows = $result->fetch_all; |
add tests
|
133 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_all list context"); |
cleanup
|
134 |
|
add tests
|
135 |
$result = $dbi->execute($query); |
cleanup
|
136 |
@rows = $result->fetch_all_hash; |
add tests
|
137 |
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all_hash scalar context"); |
cleanup
|
138 |
|
add tests
|
139 |
$result = $dbi->execute($query); |
cleanup
|
140 |
@rows = $result->fetch_all; |
add tests
|
141 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_all_hash list context"); |
cleanup
|
142 |
|
add tests
|
143 |
|
add tests
|
144 |
test 'Insert query return value'; |
cleanup
|
145 |
$dbi->do($DROP_TABLE->{0}); |
add tests
|
146 |
$dbi->do($CREATE_TABLE->{0}); |
147 |
$tmpl = "insert into table1 {insert key1 key2}"; |
|
148 |
$query = $dbi->create_query($tmpl); |
|
149 |
$ret_val = $dbi->execute($query, {key1 => 1, key2 => 2}); |
|
150 |
ok($ret_val, $test); |
|
151 |
|
|
add tests
|
152 |
|
153 |
test 'Direct execute'; |
|
cleanup
|
154 |
$dbi->do($DROP_TABLE->{0}); |
add tests
|
155 |
$dbi->do($CREATE_TABLE->{0}); |
156 |
$insert_tmpl = "insert into table1 {insert key1 key2}"; |
|
157 |
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2}, sub { |
|
158 |
my $query = shift; |
|
159 |
$query->bind_filter(sub { |
|
160 |
my ($key, $value) = @_; |
|
161 |
if ($key eq 'key2') { |
|
162 |
return $value + 1; |
|
163 |
} |
|
164 |
return $value; |
|
165 |
}); |
|
166 |
}); |
|
167 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
168 |
$rows = $result->fetch_all_hash; |
|
169 |
is_deeply($rows, [{key1 => 1, key2 => 3}], $test); |
|
170 |
|
|
171 |
|
|
add tests
|
172 |
test 'Filter basic'; |
cleanup
|
173 |
$dbi->do($DROP_TABLE->{0}); |
add tests
|
174 |
$dbi->do($CREATE_TABLE->{0}); |
cleanup#
|
175 |
|
add tests
|
176 |
$insert_tmpl = "insert into table1 {insert key1 key2};"; |
add prepare
|
177 |
$insert_query = $dbi->create_query($insert_tmpl); |
178 |
$insert_query->bind_filter(sub { |
|
add tests
|
179 |
my ($key, $value, $table, $column) = @_; |
180 |
if ($key eq 'key1' && $table eq '' && $column eq 'key1') { |
|
add prepare
|
181 |
return $value * 2; |
cleanup#
|
182 |
} |
183 |
return $value; |
|
184 |
}); |
|
add tests
|
185 |
$dbi->execute($insert_query, {key1 => 1, key2 => 2}); |
186 |
$select_query = $dbi->create_query($SELECT_TMPL->{0}); |
|
add prepare
|
187 |
$select_query->fetch_filter(sub { |
add tests
|
188 |
my ($key, $value, $type, $sth, $i) = @_; |
189 |
if ($key eq 'key2' && $type =~ /char/ && $sth->can('execute') && $i == 1) { |
|
add prepare
|
190 |
return $value * 3; |
191 |
} |
|
192 |
return $value; |
|
193 |
}); |
|
194 |
$result = $dbi->execute($select_query); |
|
195 |
$rows = $result->fetch_all_hash; |
|
add tests
|
196 |
is_deeply($rows, [{key1 => 2, key2 => 6}], "$test : bind_filter fetch_filter"); |
cleanup#
|
197 |
|
add tests
|
198 |
$dbi->do("delete from table1;"); |
199 |
$insert_query->no_bind_filters('key1'); |
|
200 |
$select_query->no_fetch_filters('key2'); |
|
201 |
$dbi->execute($insert_query, {key1 => 1, key2 => 2}); |
|
202 |
$result = $dbi->execute($select_query); |
|
203 |
$rows = $result->fetch_all_hash; |
|
add tests
|
204 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : no_fetch_filters no_bind_filters"); |
add tests
|
205 |
|
cleanup
|
206 |
$dbi->do($DROP_TABLE->{0}); |
add tests
|
207 |
$dbi->do($CREATE_TABLE->{0}); |
208 |
$insert_tmpl = "insert into table1 {insert table1.key1 table1.key2}"; |
|
209 |
$insert_query = $dbi->create_query($insert_tmpl); |
|
210 |
$insert_query->bind_filter(sub { |
|
211 |
my ($key, $value, $table, $column) = @_; |
|
212 |
if ($key eq 'table1.key1' && $table eq 'table1' && $column eq 'key1') { |
|
213 |
return $value * 3; |
|
214 |
} |
|
215 |
return $value; |
|
216 |
}); |
|
217 |
$dbi->execute($insert_query, {table1 => {key1 => 1, key2 => 2}}); |
|
218 |
$select_query = $dbi->create_query($SELECT_TMPL->{0}); |
|
219 |
$result = $dbi->execute($select_query); |
|
220 |
$rows = $result->fetch_all_hash; |
|
221 |
is_deeply($rows, [{key1 => 3, key2 => 2}], "$test : insert with table name"); |
|
222 |
|
|
add tests
|
223 |
test 'Filter in'; |
224 |
$insert_tmpl = "insert into table1 {insert key1 key2};"; |
|
225 |
$insert_query = $dbi->create_query($insert_tmpl); |
|
226 |
$dbi->execute($insert_query, {key1 => 2, key2 => 4}); |
|
227 |
$select_tmpl = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}"; |
|
228 |
$select_query = $dbi->create_query($select_tmpl); |
|
229 |
$select_query->bind_filter(sub { |
|
230 |
my ($key, $value, $table, $column) = @_; |
|
231 |
if ($key eq 'table1.key1' && $table eq 'table1' && $column eq 'key1' || $key eq 'table1.key2') { |
|
232 |
return $value * 2; |
|
233 |
} |
|
234 |
return $value; |
|
235 |
}); |
|
236 |
$result = $dbi->execute($select_query, {table1 => {key1 => [1,5], key2 => [2,5]}}); |
|
237 |
$rows = $result->fetch_all_hash; |
|
238 |
is_deeply($rows, [{key1 => 2, key2 => 4}], "$test : bind_filter"); |
|
239 |
|
|
add tests
|
240 |
|
add tests
|
241 |
test 'DBI::Custom::SQL::Template basic tag'; |
cleanup
|
242 |
$dbi->do($DROP_TABLE->{0}); |
add tests
|
243 |
$dbi->do($CREATE_TABLE->{1}); |
244 |
$sth = $dbi->prepare("insert into table1 (key1, key2, key3, key4, key5) values (?, ?, ?, ?, ?);"); |
|
245 |
$sth->execute(1, 2, 3, 4, 5); |
|
246 |
$sth->execute(6, 7, 8, 9, 10); |
|
add tests
|
247 |
|
add tests
|
248 |
$tmpl = "select * from table1 where {= key1} and {<> key2} and {< key3} and {> key4} and {>= key5};"; |
249 |
$query = $dbi->create_query($tmpl); |
|
250 |
$result = $dbi->execute($query, {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5}); |
|
cleanup
|
251 |
$rows = $result->fetch_all_hash; |
add tests
|
252 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1"); |
cleanup
|
253 |
|
add tests
|
254 |
$tmpl = "select * from table1 where {= table1.key1} and {<> table1.key2} and {< table1.key3} and {> table1.key4} and {>= table1.key5};"; |
255 |
$query = $dbi->create_query($tmpl); |
|
256 |
$result = $dbi->execute($query, {table1 => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5}}); |
|
257 |
$rows = $result->fetch_all_hash; |
|
258 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1 with table"); |
|
259 |
|
|
260 |
$tmpl = "select * from table1 where {= table1.key1} and {<> table1.key2} and {< table1.key3} and {> table1.key4} and {>= table1.key5};"; |
|
261 |
$query = $dbi->create_query($tmpl); |
|
262 |
$result = $dbi->execute($query, {'table1.key1' => 1, 'table1.key2' => 3, 'table1.key3' => 4, 'table1.key4' => 3, 'table1.key5' => 5}); |
|
263 |
$rows = $result->fetch_all_hash; |
|
264 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1 with table dot"); |
|
265 |
|
|
add tests
|
266 |
$tmpl = "select * from table1 where {<= key1} and {like key2};"; |
267 |
$query = $dbi->create_query($tmpl); |
|
268 |
$result = $dbi->execute($query, {key1 => 1, key2 => '%2%'}); |
|
269 |
$rows = $result->fetch_all_hash; |
|
270 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2"); |
|
cleanup
|
271 |
|
add tests
|
272 |
$tmpl = "select * from table1 where {<= table1.key1} and {like table1.key2};"; |
273 |
$query = $dbi->create_query($tmpl); |
|
274 |
$result = $dbi->execute($query, {table1 => {key1 => 1, key2 => '%2%'}}); |
|
275 |
$rows = $result->fetch_all_hash; |
|
276 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2 with table"); |
|
277 |
|
|
278 |
$tmpl = "select * from table1 where {<= table1.key1} and {like table1.key2};"; |
|
279 |
$query = $dbi->create_query($tmpl); |
|
280 |
$result = $dbi->execute($query, {'table1.key1' => 1, 'table1.key2' => '%2%'}); |
|
281 |
$rows = $result->fetch_all_hash; |
|
282 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2 with table dot"); |
|
283 |
|
|
284 |
|
|
add tests
|
285 |
test 'DIB::Custom::SQL::Template in tag'; |
cleanup
|
286 |
$dbi->do($DROP_TABLE->{0}); |
add tests
|
287 |
$dbi->do($CREATE_TABLE->{1}); |
288 |
$sth = $dbi->prepare("insert into table1 (key1, key2, key3, key4, key5) values (?, ?, ?, ?, ?);"); |
|
289 |
$sth->execute(1, 2, 3, 4, 5); |
|
290 |
$sth->execute(6, 7, 8, 9, 10); |
|
291 |
|
|
292 |
$tmpl = "select * from table1 where {in key1 2};"; |
|
293 |
$query = $dbi->create_query($tmpl); |
|
294 |
$result = $dbi->execute($query, {key1 => [9, 1]}); |
|
295 |
$rows = $result->fetch_all_hash; |
|
296 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic"); |
|
297 |
|
|
298 |
$tmpl = "select * from table1 where {in table1.key1 2};"; |
|
299 |
$query = $dbi->create_query($tmpl); |
|
300 |
$result = $dbi->execute($query, {table1 => {key1 => [9, 1]}}); |
|
301 |
$rows = $result->fetch_all_hash; |
|
302 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table"); |
|
add tests
|
303 |
|
add tests
|
304 |
$tmpl = "select * from table1 where {in table1.key1 2};"; |
305 |
$query = $dbi->create_query($tmpl); |
|
306 |
$result = $dbi->execute($query, {'table1.key1' => [9, 1]}); |
|
307 |
$rows = $result->fetch_all_hash; |
|
308 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table dot"); |
|
add tests
|
309 |
|
cleanup
|
310 |
|
add tests
|
311 |
test 'DBI::Custom::SQL::Template insert tag'; |
add tests
|
312 |
$dbi->do("delete from table1"); |
313 |
$insert_tmpl = 'insert into table1 {insert key1 key2 key3 key4 key5}'; |
|
314 |
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
cleanup
|
315 |
|
add tests
|
316 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
add tests
|
317 |
$rows = $result->fetch_all_hash; |
add tests
|
318 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic"); |
cleanup
|
319 |
|
add tests
|
320 |
$dbi->do("delete from table1"); |
321 |
$dbi->execute($insert_tmpl, {'#insert' => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}}); |
|
322 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
323 |
$rows = $result->fetch_all_hash; |
|
add tests
|
324 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : #insert"); |
add tests
|
325 |
|
326 |
$dbi->do("delete from table1"); |
|
327 |
$insert_tmpl = 'insert into table1 {insert table1.key1 table1.key2 table1.key3 table1.key4 table1.key5}'; |
|
328 |
$dbi->execute($insert_tmpl, {table1 => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}}); |
|
329 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
330 |
$rows = $result->fetch_all_hash; |
|
add tests
|
331 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table name"); |
332 |
|
|
add tests
|
333 |
$dbi->do("delete from table1"); |
334 |
$insert_tmpl = 'insert into table1 {insert table1.key1 table1.key2 table1.key3 table1.key4 table1.key5}'; |
|
335 |
$dbi->execute($insert_tmpl, {'table1.key1' => 1, 'table1.key2' => 2, 'table1.key3' => 3, 'table1.key4' => 4, 'table1.key5' => 5}); |
|
336 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
337 |
$rows = $result->fetch_all_hash; |
|
338 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table name dot"); |
|
add tests
|
339 |
|
340 |
$dbi->do("delete from table1"); |
|
341 |
$dbi->execute($insert_tmpl, {'#insert' => {table1 => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}}}); |
|
342 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
343 |
$rows = $result->fetch_all_hash; |
|
add tests
|
344 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : #insert with table name"); |
add tests
|
345 |
|
add tests
|
346 |
$dbi->do("delete from table1"); |
347 |
$dbi->execute($insert_tmpl, {'#insert' => {'table1.key1' => 1, 'table1.key2' => 2, 'table1.key3' => 3, 'table1.key4' => 4, 'table1.key5' => 5}}); |
|
348 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
349 |
$rows = $result->fetch_all_hash; |
|
350 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : #insert with table name dot"); |
|
351 |
|
|
add tests
|
352 |
|
add tests
|
353 |
test 'DBI::Custom::SQL::Template update tag'; |
add tests
|
354 |
$dbi->do("delete from table1"); |
355 |
$insert_tmpl = "insert into table1 {insert key1 key2 key3 key4 key5}"; |
|
356 |
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
357 |
$dbi->execute($insert_tmpl, {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
358 |
|
|
359 |
$update_tmpl = 'update table1 {update key1 key2 key3 key4} where {= key5}'; |
|
360 |
$dbi->execute($update_tmpl, {key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5}); |
|
361 |
|
|
362 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
363 |
$rows = $result->fetch_all_hash; |
|
364 |
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5}, |
|
add tests
|
365 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : basic"); |
add tests
|
366 |
|
add tests
|
367 |
$dbi->execute($update_tmpl, {'#update' => {key1 => 2, key2 => 2, key3 => 2, key4 => 2}, key5 => 5}); |
add tests
|
368 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
369 |
$rows = $result->fetch_all_hash; |
|
add tests
|
370 |
is_deeply($rows, [{key1 => 2, key2 => 2, key3 => 2, key4 => 2, key5 => 5}, |
371 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : #update"); |
|
add tests
|
372 |
|
add tests
|
373 |
$update_tmpl = 'update table1 {update table1.key1 table1.key2 table1.key3 table1.key4} where {= table1.key5}'; |
374 |
$dbi->execute($update_tmpl, {table1 => {key1 => 3, key2 => 3, key3 => 3, key4 => 3, key5 => 5}}); |
|
add tests
|
375 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
376 |
$rows = $result->fetch_all_hash; |
|
add tests
|
377 |
is_deeply($rows, [{key1 => 3, key2 => 3, key3 => 3, key4 => 3, key5 => 5}, |
378 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : with table name"); |
|
add tests
|
379 |
|
add tests
|
380 |
$update_tmpl = 'update table1 {update table1.key1 table1.key2 table1.key3 table1.key4} where {= table1.key5}'; |
381 |
$dbi->execute($update_tmpl, {'table1.key1' => 4, 'table1.key2' => 4, 'table1.key3' => 4, 'table1.key4' => 4, 'table1.key5' => 5}); |
|
add tests
|
382 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
383 |
$rows = $result->fetch_all_hash; |
|
add tests
|
384 |
is_deeply($rows, [{key1 => 4, key2 => 4, key3 => 4, key4 => 4, key5 => 5}, |
add tests
|
385 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : with table name dot"); |
cleanup
|
386 |
|
add tests
|
387 |
$dbi->execute($update_tmpl, {'#update' => {table1 => {key1 => 5, key2 => 5, key3 => 5, key4 => 5}}, table1 => {key5 => 5}}); |
388 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
389 |
$rows = $result->fetch_all_hash; |
|
390 |
is_deeply($rows, [{key1 => 5, key2 => 5, key3 => 5, key4 => 5, key5 => 5}, |
|
add tests
|
391 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : update tag #update with table name"); |
cleanup
|
392 |
|
add tests
|
393 |
$dbi->execute($update_tmpl, {'#update' => {'table1.key1' => 6, 'table1.key2' => 6, 'table1.key3' => 6, 'table1.key4' => 6}, 'table1.key5' => 5}); |
394 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
395 |
$rows = $result->fetch_all_hash; |
|
396 |
is_deeply($rows, [{key1 => 6, key2 => 6, key3 => 6, key4 => 6, key5 => 5}, |
|
add tests
|
397 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : update tag #update with table name dot"); |
add tests
|
398 |
|
cleanup
|
399 |
|
400 |
test 'run_tansaction'; |
|
401 |
$dbi->do($DROP_TABLE->{0}); |
|
402 |
$dbi->do($CREATE_TABLE->{0}); |
|
403 |
$dbi->run_tranzaction(sub { |
|
404 |
$insert_tmpl = 'insert into table1 {insert key1 key2}'; |
|
405 |
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2}); |
|
406 |
$dbi->execute($insert_tmpl, {key1 => 3, key2 => 4}); |
|
407 |
}); |
|
408 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
409 |
$rows = $result->fetch_all_hash; |
|
410 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : commit"); |
|
411 |
|
|
412 |
$dbi->do($DROP_TABLE->{0}); |
|
413 |
$dbi->do($CREATE_TABLE->{0}); |
|
414 |
$dbi->dbh->{RaiseError} = 0; |
|
415 |
eval{ |
|
416 |
$dbi->run_tranzaction(sub { |
|
417 |
$insert_tmpl = 'insert into table1 {insert key1 key2}'; |
|
418 |
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2}); |
|
419 |
die "Fatal Error"; |
|
420 |
$dbi->execute($insert_tmpl, {key1 => 3, key2 => 4}); |
|
421 |
}) |
|
422 |
}; |
|
423 |
like($@, qr/Fatal Error.*Rollback is success/ms, "$test : Rollback success message"); |
|
424 |
ok(!$dbi->dbh->{RaiseError}, "$test : restore RaiseError value"); |
|
425 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
426 |
$rows = $result->fetch_all_hash; |
|
427 |
is_deeply($rows, [], "$test : rollback"); |
|
428 |
|
|
429 |
|
|
add tests
|
430 |
test 'Error case'; |
431 |
$dbi = DBI::Custom->new; |
|
432 |
eval{$dbi->run_tranzaction}; |
|
433 |
like($@, qr/Not yet connect to database/, "$test : Yet Connected"); |
|
434 |
|
|
435 |
$dbi = DBI::Custom->new(data_source => 'dbi:SQLit'); |
|
436 |
eval{$dbi->connect;}; |
|
437 |
ok($@, "$test : connect error"); |
|
438 |
|
|
439 |
$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:'); |
|
440 |
$dbi->connect; |
|
441 |
$dbi->dbh->{AutoCommit} = 0; |
|
442 |
eval{$dbi->run_tranzaction()}; |
|
443 |
like($@, qr/AutoCommit must be true before tranzaction start/, |
|
444 |
"$test : run_tranzaction auto commit is false"); |
|
445 |
|
|
446 |
$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:'); |
|
447 |
$sql = 'laksjdf'; |
|
448 |
eval{$dbi->prepare($sql)}; |
|
449 |
like($@, qr/$sql/, "$test : prepare fail"); |
|
450 |
|
|
451 |
$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:'); |
|
452 |
$sql = 'laksjdf'; |
|
453 |
eval{$dbi->do($sql, qw/1 2 3/)}; |
|
454 |
like($@, qr/$sql/, "$test : do fail"); |
|
455 |
|
|
456 |
$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:'); |
|
457 |
eval{$dbi->create_query("{p }")}; |
|
458 |
ok($@, "$test : create_query invalid SQL template"); |
|
459 |
|
|
460 |
$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:'); |
|
461 |
$dbi->do($CREATE_TABLE->{0}); |
|
462 |
$query = $dbi->create_query("select * from table1 where {= key1}"); |
|
463 |
eval{$dbi->execute($query, {key2 => 1})}; |
|
464 |
like($@, qr/Corresponding key is not found in your parameters/, |
|
465 |
"$test : execute corresponding key not found"); |
|
466 |
|