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 |
|
|
add tests
|
23 |
# Constant varialbes for test |
24 |
my $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 |
my $SELECT_TMPL = { |
add tests
|
30 |
0 => 'select * from table1;' |
add tests
|
31 |
}; |
32 |
|
|
add tests
|
33 |
my $DROP_TABLE = { |
cleanup
|
34 |
0 => 'drop table table1' |
35 |
}; |
|
36 |
|
|
add tests
|
37 |
my $NEW_ARGS = { |
38 |
0 => {data_source => 'dbi:SQLite:dbname=:memory:'} |
|
39 |
}; |
|
40 |
|
|
41 |
# Variables for test |
|
cleanup
|
42 |
my $dbi; |
43 |
my $sth; |
|
44 |
my $tmpl; |
|
cleanup#
|
45 |
my $select_tmpl; |
46 |
my $insert_tmpl; |
|
add tests
|
47 |
my $update_tmpl; |
cleanup
|
48 |
my $params; |
49 |
my $sql; |
|
50 |
my $result; |
|
51 |
my @rows; |
|
52 |
my $rows; |
|
add tests
|
53 |
my $query; |
cleanup#
|
54 |
my $select_query; |
55 |
my $insert_query; |
|
add tests
|
56 |
my $update_query; |
add prepare
|
57 |
my $ret_val; |
cleanup#
|
58 |
|
cleanup
|
59 |
|
cleanup
|
60 |
test 'disconnect'; |
add tests
|
61 |
$dbi = DBI::Custom->new($NEW_ARGS->{0}); |
add tests
|
62 |
$dbi->connect; |
63 |
$dbi->disconnect; |
|
64 |
ok(!$dbi->dbh, $test); |
|
add tests
|
65 |
|
add tests
|
66 |
|
cleanup
|
67 |
test 'connected'; |
add tests
|
68 |
$dbi = DBI::Custom->new($NEW_ARGS->{0}); |
cleanup
|
69 |
ok(!$dbi->connected, "$test : not connected"); |
70 |
$dbi->connect; |
|
71 |
ok($dbi->connected, "$test : connected"); |
|
72 |
|
|
add tests
|
73 |
|
74 |
test 'preapare'; |
|
add tests
|
75 |
$dbi = DBI::Custom->new($NEW_ARGS->{0}); |
add tests
|
76 |
$sth = $dbi->prepare($CREATE_TABLE->{0}); |
77 |
ok($sth, "$test : auto connect"); |
|
78 |
$sth->execute; |
|
79 |
$sth = $dbi->prepare($DROP_TABLE->{0}); |
|
80 |
ok($sth, "$test : basic"); |
|
81 |
|
|
82 |
|
|
83 |
test 'do'; |
|
add tests
|
84 |
$dbi = DBI::Custom->new($NEW_ARGS->{0}); |
add tests
|
85 |
$ret_val = $dbi->do($CREATE_TABLE->{0}); |
86 |
ok(defined $ret_val, "$test : auto connect"); |
|
87 |
$ret_val = $dbi->do($DROP_TABLE->{0}); |
|
88 |
ok(defined $ret_val, "$test : basic"); |
|
89 |
|
|
90 |
|
|
cleanup
|
91 |
# Prepare table |
add tests
|
92 |
$dbi = DBI::Custom->new($NEW_ARGS->{0}); |
cleanup
|
93 |
$dbi->connect; |
add prepare
|
94 |
$dbi->do($CREATE_TABLE->{0}); |
95 |
$sth = $dbi->prepare("insert into table1 (key1, key2) values (?, ?);"); |
|
cleanup
|
96 |
$sth->execute(1, 2); |
97 |
$sth->execute(3, 4); |
|
add test
|
98 |
|
add test module
|
99 |
|
add tests
|
100 |
test 'DBI::Custom::Result test'; |
101 |
$tmpl = "select key1, key2 from table1"; |
|
102 |
$query = $dbi->create_query($tmpl); |
|
103 |
$result = $dbi->execute($query); |
|
cleanup
|
104 |
|
105 |
@rows = (); |
|
106 |
while (my $row = $result->fetch) { |
|
107 |
push @rows, [@$row]; |
|
add test module
|
108 |
} |
add tests
|
109 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch scalar context"); |
cleanup
|
110 |
|
add tests
|
111 |
$result = $dbi->execute($query); |
cleanup
|
112 |
@rows = (); |
113 |
while (my @row = $result->fetch) { |
|
114 |
push @rows, [@row]; |
|
add test module
|
115 |
} |
add tests
|
116 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch list context"); |
add test module
|
117 |
|
add tests
|
118 |
$result = $dbi->execute($query); |
cleanup
|
119 |
@rows = (); |
120 |
while (my $row = $result->fetch_hash) { |
|
121 |
push @rows, {%$row}; |
|
add test module
|
122 |
} |
add tests
|
123 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch_hash scalar context"); |
cleanup
|
124 |
|
add tests
|
125 |
$result = $dbi->execute($query); |
cleanup
|
126 |
@rows = (); |
127 |
while (my %row = $result->fetch_hash) { |
|
128 |
push @rows, {%row}; |
|
add test module
|
129 |
} |
add tests
|
130 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch hash list context"); |
add test module
|
131 |
|
add tests
|
132 |
$result = $dbi->execute($query); |
cleanup
|
133 |
$rows = $result->fetch_all; |
add tests
|
134 |
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all scalar context"); |
cleanup
|
135 |
|
add tests
|
136 |
$result = $dbi->execute($query); |
cleanup
|
137 |
@rows = $result->fetch_all; |
add tests
|
138 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_all list context"); |
cleanup
|
139 |
|
add tests
|
140 |
$result = $dbi->execute($query); |
cleanup
|
141 |
@rows = $result->fetch_all_hash; |
add tests
|
142 |
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all_hash scalar context"); |
cleanup
|
143 |
|
add tests
|
144 |
$result = $dbi->execute($query); |
cleanup
|
145 |
@rows = $result->fetch_all; |
add tests
|
146 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_all_hash list context"); |
cleanup
|
147 |
|
add tests
|
148 |
|
add tests
|
149 |
test 'Insert query return value'; |
cleanup
|
150 |
$dbi->do($DROP_TABLE->{0}); |
add tests
|
151 |
$dbi->do($CREATE_TABLE->{0}); |
152 |
$tmpl = "insert into table1 {insert key1 key2}"; |
|
153 |
$query = $dbi->create_query($tmpl); |
|
154 |
$ret_val = $dbi->execute($query, {key1 => 1, key2 => 2}); |
|
155 |
ok($ret_val, $test); |
|
156 |
|
|
add tests
|
157 |
|
158 |
test 'Direct execute'; |
|
cleanup
|
159 |
$dbi->do($DROP_TABLE->{0}); |
add tests
|
160 |
$dbi->do($CREATE_TABLE->{0}); |
161 |
$insert_tmpl = "insert into table1 {insert key1 key2}"; |
|
162 |
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2}, sub { |
|
163 |
my $query = shift; |
|
164 |
$query->bind_filter(sub { |
|
165 |
my ($key, $value) = @_; |
|
166 |
if ($key eq 'key2') { |
|
167 |
return $value + 1; |
|
168 |
} |
|
169 |
return $value; |
|
170 |
}); |
|
171 |
}); |
|
172 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
173 |
$rows = $result->fetch_all_hash; |
|
174 |
is_deeply($rows, [{key1 => 1, key2 => 3}], $test); |
|
175 |
|
|
176 |
|
|
add tests
|
177 |
test 'Filter basic'; |
cleanup
|
178 |
$dbi->do($DROP_TABLE->{0}); |
add tests
|
179 |
$dbi->do($CREATE_TABLE->{0}); |
cleanup#
|
180 |
|
add tests
|
181 |
$insert_tmpl = "insert into table1 {insert key1 key2};"; |
add prepare
|
182 |
$insert_query = $dbi->create_query($insert_tmpl); |
183 |
$insert_query->bind_filter(sub { |
|
add tests
|
184 |
my ($key, $value, $table, $column) = @_; |
185 |
if ($key eq 'key1' && $table eq '' && $column eq 'key1') { |
|
add prepare
|
186 |
return $value * 2; |
cleanup#
|
187 |
} |
188 |
return $value; |
|
189 |
}); |
|
add tests
|
190 |
$dbi->execute($insert_query, {key1 => 1, key2 => 2}); |
191 |
$select_query = $dbi->create_query($SELECT_TMPL->{0}); |
|
add prepare
|
192 |
$select_query->fetch_filter(sub { |
add tests
|
193 |
my ($key, $value, $type, $sth, $i) = @_; |
194 |
if ($key eq 'key2' && $type =~ /char/ && $sth->can('execute') && $i == 1) { |
|
add prepare
|
195 |
return $value * 3; |
196 |
} |
|
197 |
return $value; |
|
198 |
}); |
|
199 |
$result = $dbi->execute($select_query); |
|
200 |
$rows = $result->fetch_all_hash; |
|
add tests
|
201 |
is_deeply($rows, [{key1 => 2, key2 => 6}], "$test : bind_filter fetch_filter"); |
cleanup#
|
202 |
|
add tests
|
203 |
$dbi->do("delete from table1;"); |
204 |
$insert_query->no_bind_filters('key1'); |
|
205 |
$select_query->no_fetch_filters('key2'); |
|
206 |
$dbi->execute($insert_query, {key1 => 1, key2 => 2}); |
|
207 |
$result = $dbi->execute($select_query); |
|
208 |
$rows = $result->fetch_all_hash; |
|
add tests
|
209 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : no_fetch_filters no_bind_filters"); |
add tests
|
210 |
|
cleanup
|
211 |
$dbi->do($DROP_TABLE->{0}); |
add tests
|
212 |
$dbi->do($CREATE_TABLE->{0}); |
213 |
$insert_tmpl = "insert into table1 {insert table1.key1 table1.key2}"; |
|
214 |
$insert_query = $dbi->create_query($insert_tmpl); |
|
215 |
$insert_query->bind_filter(sub { |
|
216 |
my ($key, $value, $table, $column) = @_; |
|
217 |
if ($key eq 'table1.key1' && $table eq 'table1' && $column eq 'key1') { |
|
218 |
return $value * 3; |
|
219 |
} |
|
220 |
return $value; |
|
221 |
}); |
|
222 |
$dbi->execute($insert_query, {table1 => {key1 => 1, key2 => 2}}); |
|
223 |
$select_query = $dbi->create_query($SELECT_TMPL->{0}); |
|
224 |
$result = $dbi->execute($select_query); |
|
225 |
$rows = $result->fetch_all_hash; |
|
226 |
is_deeply($rows, [{key1 => 3, key2 => 2}], "$test : insert with table name"); |
|
227 |
|
|
add tests
|
228 |
test 'Filter in'; |
229 |
$insert_tmpl = "insert into table1 {insert key1 key2};"; |
|
230 |
$insert_query = $dbi->create_query($insert_tmpl); |
|
231 |
$dbi->execute($insert_query, {key1 => 2, key2 => 4}); |
|
232 |
$select_tmpl = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}"; |
|
233 |
$select_query = $dbi->create_query($select_tmpl); |
|
234 |
$select_query->bind_filter(sub { |
|
235 |
my ($key, $value, $table, $column) = @_; |
|
236 |
if ($key eq 'table1.key1' && $table eq 'table1' && $column eq 'key1' || $key eq 'table1.key2') { |
|
237 |
return $value * 2; |
|
238 |
} |
|
239 |
return $value; |
|
240 |
}); |
|
241 |
$result = $dbi->execute($select_query, {table1 => {key1 => [1,5], key2 => [2,5]}}); |
|
242 |
$rows = $result->fetch_all_hash; |
|
243 |
is_deeply($rows, [{key1 => 2, key2 => 4}], "$test : bind_filter"); |
|
244 |
|
|
add tests
|
245 |
|
add tests
|
246 |
test 'DBI::Custom::SQL::Template basic tag'; |
cleanup
|
247 |
$dbi->do($DROP_TABLE->{0}); |
add tests
|
248 |
$dbi->do($CREATE_TABLE->{1}); |
249 |
$sth = $dbi->prepare("insert into table1 (key1, key2, key3, key4, key5) values (?, ?, ?, ?, ?);"); |
|
250 |
$sth->execute(1, 2, 3, 4, 5); |
|
251 |
$sth->execute(6, 7, 8, 9, 10); |
|
add tests
|
252 |
|
add tests
|
253 |
$tmpl = "select * from table1 where {= key1} and {<> key2} and {< key3} and {> key4} and {>= key5};"; |
254 |
$query = $dbi->create_query($tmpl); |
|
255 |
$result = $dbi->execute($query, {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5}); |
|
cleanup
|
256 |
$rows = $result->fetch_all_hash; |
add tests
|
257 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1"); |
cleanup
|
258 |
|
add tests
|
259 |
$tmpl = "select * from table1 where {= table1.key1} and {<> table1.key2} and {< table1.key3} and {> table1.key4} and {>= table1.key5};"; |
260 |
$query = $dbi->create_query($tmpl); |
|
261 |
$result = $dbi->execute($query, {table1 => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5}}); |
|
262 |
$rows = $result->fetch_all_hash; |
|
263 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1 with table"); |
|
264 |
|
|
265 |
$tmpl = "select * from table1 where {= table1.key1} and {<> table1.key2} and {< table1.key3} and {> table1.key4} and {>= table1.key5};"; |
|
266 |
$query = $dbi->create_query($tmpl); |
|
267 |
$result = $dbi->execute($query, {'table1.key1' => 1, 'table1.key2' => 3, 'table1.key3' => 4, 'table1.key4' => 3, 'table1.key5' => 5}); |
|
268 |
$rows = $result->fetch_all_hash; |
|
269 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1 with table dot"); |
|
270 |
|
|
add tests
|
271 |
$tmpl = "select * from table1 where {<= key1} and {like key2};"; |
272 |
$query = $dbi->create_query($tmpl); |
|
273 |
$result = $dbi->execute($query, {key1 => 1, key2 => '%2%'}); |
|
274 |
$rows = $result->fetch_all_hash; |
|
275 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2"); |
|
cleanup
|
276 |
|
add tests
|
277 |
$tmpl = "select * from table1 where {<= table1.key1} and {like table1.key2};"; |
278 |
$query = $dbi->create_query($tmpl); |
|
279 |
$result = $dbi->execute($query, {table1 => {key1 => 1, key2 => '%2%'}}); |
|
280 |
$rows = $result->fetch_all_hash; |
|
281 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2 with table"); |
|
282 |
|
|
283 |
$tmpl = "select * from table1 where {<= table1.key1} and {like table1.key2};"; |
|
284 |
$query = $dbi->create_query($tmpl); |
|
285 |
$result = $dbi->execute($query, {'table1.key1' => 1, 'table1.key2' => '%2%'}); |
|
286 |
$rows = $result->fetch_all_hash; |
|
287 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2 with table dot"); |
|
288 |
|
|
289 |
|
|
add tests
|
290 |
test 'DIB::Custom::SQL::Template in tag'; |
cleanup
|
291 |
$dbi->do($DROP_TABLE->{0}); |
add tests
|
292 |
$dbi->do($CREATE_TABLE->{1}); |
293 |
$sth = $dbi->prepare("insert into table1 (key1, key2, key3, key4, key5) values (?, ?, ?, ?, ?);"); |
|
294 |
$sth->execute(1, 2, 3, 4, 5); |
|
295 |
$sth->execute(6, 7, 8, 9, 10); |
|
296 |
|
|
297 |
$tmpl = "select * from table1 where {in key1 2};"; |
|
298 |
$query = $dbi->create_query($tmpl); |
|
299 |
$result = $dbi->execute($query, {key1 => [9, 1]}); |
|
300 |
$rows = $result->fetch_all_hash; |
|
301 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic"); |
|
302 |
|
|
303 |
$tmpl = "select * from table1 where {in table1.key1 2};"; |
|
304 |
$query = $dbi->create_query($tmpl); |
|
305 |
$result = $dbi->execute($query, {table1 => {key1 => [9, 1]}}); |
|
306 |
$rows = $result->fetch_all_hash; |
|
307 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table"); |
|
add tests
|
308 |
|
add tests
|
309 |
$tmpl = "select * from table1 where {in table1.key1 2};"; |
310 |
$query = $dbi->create_query($tmpl); |
|
311 |
$result = $dbi->execute($query, {'table1.key1' => [9, 1]}); |
|
312 |
$rows = $result->fetch_all_hash; |
|
313 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table dot"); |
|
add tests
|
314 |
|
cleanup
|
315 |
|
add tests
|
316 |
test 'DBI::Custom::SQL::Template insert tag'; |
add tests
|
317 |
$dbi->do("delete from table1"); |
318 |
$insert_tmpl = 'insert into table1 {insert key1 key2 key3 key4 key5}'; |
|
319 |
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
cleanup
|
320 |
|
add tests
|
321 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
add tests
|
322 |
$rows = $result->fetch_all_hash; |
add tests
|
323 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic"); |
cleanup
|
324 |
|
add tests
|
325 |
$dbi->do("delete from table1"); |
326 |
$dbi->execute($insert_tmpl, {'#insert' => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}}); |
|
327 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
328 |
$rows = $result->fetch_all_hash; |
|
add tests
|
329 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : #insert"); |
add tests
|
330 |
|
331 |
$dbi->do("delete from table1"); |
|
332 |
$insert_tmpl = 'insert into table1 {insert table1.key1 table1.key2 table1.key3 table1.key4 table1.key5}'; |
|
333 |
$dbi->execute($insert_tmpl, {table1 => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}}); |
|
334 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
335 |
$rows = $result->fetch_all_hash; |
|
add tests
|
336 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table name"); |
337 |
|
|
add tests
|
338 |
$dbi->do("delete from table1"); |
339 |
$insert_tmpl = 'insert into table1 {insert table1.key1 table1.key2 table1.key3 table1.key4 table1.key5}'; |
|
340 |
$dbi->execute($insert_tmpl, {'table1.key1' => 1, 'table1.key2' => 2, 'table1.key3' => 3, 'table1.key4' => 4, 'table1.key5' => 5}); |
|
341 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
342 |
$rows = $result->fetch_all_hash; |
|
343 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table name dot"); |
|
add tests
|
344 |
|
345 |
$dbi->do("delete from table1"); |
|
346 |
$dbi->execute($insert_tmpl, {'#insert' => {table1 => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}}}); |
|
347 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
348 |
$rows = $result->fetch_all_hash; |
|
add tests
|
349 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : #insert with table name"); |
add tests
|
350 |
|
add tests
|
351 |
$dbi->do("delete from table1"); |
352 |
$dbi->execute($insert_tmpl, {'#insert' => {'table1.key1' => 1, 'table1.key2' => 2, 'table1.key3' => 3, 'table1.key4' => 4, 'table1.key5' => 5}}); |
|
353 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
354 |
$rows = $result->fetch_all_hash; |
|
355 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : #insert with table name dot"); |
|
356 |
|
|
add tests
|
357 |
|
add tests
|
358 |
test 'DBI::Custom::SQL::Template update tag'; |
add tests
|
359 |
$dbi->do("delete from table1"); |
360 |
$insert_tmpl = "insert into table1 {insert key1 key2 key3 key4 key5}"; |
|
361 |
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
362 |
$dbi->execute($insert_tmpl, {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
363 |
|
|
364 |
$update_tmpl = 'update table1 {update key1 key2 key3 key4} where {= key5}'; |
|
365 |
$dbi->execute($update_tmpl, {key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5}); |
|
366 |
|
|
367 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
368 |
$rows = $result->fetch_all_hash; |
|
369 |
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5}, |
|
add tests
|
370 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : basic"); |
add tests
|
371 |
|
add tests
|
372 |
$dbi->execute($update_tmpl, {'#update' => {key1 => 2, key2 => 2, key3 => 2, key4 => 2}, key5 => 5}); |
add tests
|
373 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
374 |
$rows = $result->fetch_all_hash; |
|
add tests
|
375 |
is_deeply($rows, [{key1 => 2, key2 => 2, key3 => 2, key4 => 2, key5 => 5}, |
376 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : #update"); |
|
add tests
|
377 |
|
add tests
|
378 |
$update_tmpl = 'update table1 {update table1.key1 table1.key2 table1.key3 table1.key4} where {= table1.key5}'; |
379 |
$dbi->execute($update_tmpl, {table1 => {key1 => 3, key2 => 3, key3 => 3, key4 => 3, key5 => 5}}); |
|
add tests
|
380 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
381 |
$rows = $result->fetch_all_hash; |
|
add tests
|
382 |
is_deeply($rows, [{key1 => 3, key2 => 3, key3 => 3, key4 => 3, key5 => 5}, |
383 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : with table name"); |
|
add tests
|
384 |
|
add tests
|
385 |
$update_tmpl = 'update table1 {update table1.key1 table1.key2 table1.key3 table1.key4} where {= table1.key5}'; |
386 |
$dbi->execute($update_tmpl, {'table1.key1' => 4, 'table1.key2' => 4, 'table1.key3' => 4, 'table1.key4' => 4, 'table1.key5' => 5}); |
|
add tests
|
387 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
388 |
$rows = $result->fetch_all_hash; |
|
add tests
|
389 |
is_deeply($rows, [{key1 => 4, key2 => 4, key3 => 4, key4 => 4, key5 => 5}, |
add tests
|
390 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : with table name dot"); |
cleanup
|
391 |
|
add tests
|
392 |
$dbi->execute($update_tmpl, {'#update' => {table1 => {key1 => 5, key2 => 5, key3 => 5, key4 => 5}}, table1 => {key5 => 5}}); |
393 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
394 |
$rows = $result->fetch_all_hash; |
|
395 |
is_deeply($rows, [{key1 => 5, key2 => 5, key3 => 5, key4 => 5, key5 => 5}, |
|
add tests
|
396 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : update tag #update with table name"); |
cleanup
|
397 |
|
add tests
|
398 |
$dbi->execute($update_tmpl, {'#update' => {'table1.key1' => 6, 'table1.key2' => 6, 'table1.key3' => 6, 'table1.key4' => 6}, 'table1.key5' => 5}); |
399 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
400 |
$rows = $result->fetch_all_hash; |
|
401 |
is_deeply($rows, [{key1 => 6, key2 => 6, key3 => 6, key4 => 6, key5 => 5}, |
|
add tests
|
402 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : update tag #update with table name dot"); |
add tests
|
403 |
|
cleanup
|
404 |
|
405 |
test 'run_tansaction'; |
|
406 |
$dbi->do($DROP_TABLE->{0}); |
|
407 |
$dbi->do($CREATE_TABLE->{0}); |
|
408 |
$dbi->run_tranzaction(sub { |
|
409 |
$insert_tmpl = 'insert into table1 {insert key1 key2}'; |
|
410 |
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2}); |
|
411 |
$dbi->execute($insert_tmpl, {key1 => 3, key2 => 4}); |
|
412 |
}); |
|
413 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
414 |
$rows = $result->fetch_all_hash; |
|
415 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : commit"); |
|
416 |
|
|
417 |
$dbi->do($DROP_TABLE->{0}); |
|
418 |
$dbi->do($CREATE_TABLE->{0}); |
|
419 |
$dbi->dbh->{RaiseError} = 0; |
|
420 |
eval{ |
|
421 |
$dbi->run_tranzaction(sub { |
|
422 |
$insert_tmpl = 'insert into table1 {insert key1 key2}'; |
|
423 |
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2}); |
|
424 |
die "Fatal Error"; |
|
425 |
$dbi->execute($insert_tmpl, {key1 => 3, key2 => 4}); |
|
426 |
}) |
|
427 |
}; |
|
428 |
like($@, qr/Fatal Error.*Rollback is success/ms, "$test : Rollback success message"); |
|
429 |
ok(!$dbi->dbh->{RaiseError}, "$test : restore RaiseError value"); |
|
430 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
431 |
$rows = $result->fetch_all_hash; |
|
432 |
is_deeply($rows, [], "$test : rollback"); |
|
433 |
|
|
434 |
|
|
add tests
|
435 |
test 'Error case'; |
436 |
$dbi = DBI::Custom->new; |
|
437 |
eval{$dbi->run_tranzaction}; |
|
438 |
like($@, qr/Not yet connect to database/, "$test : Yet Connected"); |
|
439 |
|
|
440 |
$dbi = DBI::Custom->new(data_source => 'dbi:SQLit'); |
|
441 |
eval{$dbi->connect;}; |
|
442 |
ok($@, "$test : connect error"); |
|
443 |
|
|
add tests
|
444 |
$dbi = DBI::Custom->new($NEW_ARGS->{0}); |
add tests
|
445 |
$dbi->connect; |
446 |
$dbi->dbh->{AutoCommit} = 0; |
|
447 |
eval{$dbi->run_tranzaction()}; |
|
448 |
like($@, qr/AutoCommit must be true before tranzaction start/, |
|
449 |
"$test : run_tranzaction auto commit is false"); |
|
450 |
|
|
add tests
|
451 |
$dbi = DBI::Custom->new($NEW_ARGS->{0}); |
add tests
|
452 |
$sql = 'laksjdf'; |
453 |
eval{$dbi->prepare($sql)}; |
|
454 |
like($@, qr/$sql/, "$test : prepare fail"); |
|
455 |
|
|
add tests
|
456 |
$dbi = DBI::Custom->new($NEW_ARGS->{0}); |
add tests
|
457 |
$sql = 'laksjdf'; |
458 |
eval{$dbi->do($sql, qw/1 2 3/)}; |
|
459 |
like($@, qr/$sql/, "$test : do fail"); |
|
460 |
|
|
add tests
|
461 |
$dbi = DBI::Custom->new($NEW_ARGS->{0}); |
add tests
|
462 |
eval{$dbi->create_query("{p }")}; |
463 |
ok($@, "$test : create_query invalid SQL template"); |
|
464 |
|
|
add tests
|
465 |
$dbi = DBI::Custom->new($NEW_ARGS->{0}); |
add tests
|
466 |
$dbi->do($CREATE_TABLE->{0}); |
467 |
$query = $dbi->create_query("select * from table1 where {= key1}"); |
|
468 |
eval{$dbi->execute($query, {key2 => 1})}; |
|
469 |
like($@, qr/Corresponding key is not found in your parameters/, |
|
470 |
"$test : execute corresponding key not found"); |
|
471 |
|
|
add tests
|
472 |
|
473 |
test 'insert'; |
|
474 |
$dbi = DBI::Custom->new($NEW_ARGS->{0}); |
|
475 |
$dbi->do($CREATE_TABLE->{0}); |
|
476 |
$dbi->insert('table1', {key1 => 1, key2 => 2}); |
|
477 |
$dbi->insert('table1', {key1 => 3, key2 => 4}); |
|
478 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
479 |
$rows = $result->fetch_all_hash; |
|
480 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : basic"); |
|
481 |
|
|
482 |
$dbi->do('delete from table1'); |
|
483 |
$dbi->insert('table1', {key1 => 1, key2 => 2}, sub { |
|
484 |
my $query = shift; |
|
485 |
$query->bind_filter(sub { |
|
486 |
my ($key, $value) = @_; |
|
487 |
if ($key eq 'key1') { |
|
488 |
return $value * 3; |
|
489 |
} |
|
490 |
return $value; |
|
491 |
}); |
|
492 |
}); |
|
493 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
494 |
$rows = $result->fetch_all_hash; |
|
495 |
is_deeply($rows, [{key1 => 3, key2 => 2}], "$test : edit_query_callback"); |
|
496 |
|
|
add tests
|
497 |
|
498 |
test 'insert error'; |
|
add tests
|
499 |
eval{$dbi->insert('table1')}; |
add tests
|
500 |
like($@, qr/Key-value pairs for insert must be specified to 'insert' second argument/, "$test : insert key-value not specifed"); |
add tests
|
501 |
|
502 |
eval{$dbi->insert('table1', {key1 => 1, key2 => 2}, 'aaa')}; |
|
503 |
like($@, qr/Query edit callback must be code reference/, "$test : query edit callback not code ref"); |
|
add tests
|
504 |
|
505 |
|
|
506 |
test 'update'; |
|
507 |
$dbi = DBI::Custom->new($NEW_ARGS->{0}); |
|
508 |
$dbi->do($CREATE_TABLE->{1}); |
|
509 |
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
510 |
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
511 |
$dbi->update('table1', {key2 => 11}, {key1 => 1}); |
|
512 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
513 |
$rows = $result->fetch_all_hash; |
|
514 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5}, |
|
515 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
516 |
"$test : basic"); |
|
517 |
|
|
518 |
$dbi->do("delete from table1"); |
|
519 |
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
520 |
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
521 |
$dbi->update('table1', {key2 => 12}, {key2 => 2}); |
|
522 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
523 |
$rows = $result->fetch_all_hash; |
|
524 |
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5}, |
|
525 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
526 |
"$test : update key same as search key"); |
|
527 |
|
|
528 |
$dbi->do("delete from table1"); |
|
529 |
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
530 |
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
531 |
$dbi->update('table1', {key2 => 11}, {key1 => 1}, sub { |
|
532 |
my $query = shift; |
|
533 |
$query->bind_filter(sub { |
|
534 |
my ($key, $value) = @_; |
|
535 |
if ($key eq 'key2') { |
|
536 |
return $value * 2; |
|
537 |
} |
|
538 |
return $value; |
|
539 |
}); |
|
540 |
}); |
|
541 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
542 |
$rows = $result->fetch_all_hash; |
|
543 |
is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5}, |
|
544 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
545 |
"$test : query edit callback"); |
|
546 |
|
|
547 |
|
|
548 |
test 'update error'; |
|
549 |
$dbi = DBI::Custom->new($NEW_ARGS->{0}); |
|
550 |
$dbi->do($CREATE_TABLE->{1}); |
|
551 |
eval{$dbi->update('table1')}; |
|
552 |
like($@, qr/Key-value pairs for update must be specified to 'update' second argument/, |
|
553 |
"$test : update key-value pairs not specified"); |
|
554 |
|
|
555 |
eval{$dbi->update('table1', {key2 => 1})}; |
|
556 |
like($@, qr/Key-value pairs for where clause must be specified to 'update' third argument/, |
|
557 |
"$test : where key-value pairs not specified"); |
|
558 |
|
|
559 |
eval{$dbi->update('table1', {key2 => 1}, {key2 => 3}, 'aaa')}; |
|
560 |
like($@, qr/Query edit callback must be code reference/, |
|
561 |
"$test : query edit callback not code reference"); |
|
562 |
|
|
563 |
|
|
564 |
test 'update_all'; |
|
565 |
$dbi = DBI::Custom->new($NEW_ARGS->{0}); |
|
566 |
$dbi->do($CREATE_TABLE->{1}); |
|
567 |
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
568 |
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
569 |
$dbi->update_all('table1', {key2 => 10}, sub { |
|
570 |
my $query = shift; |
|
571 |
$query->bind_filter(sub { |
|
572 |
my ($key, $value) = @_; |
|
573 |
return $value * 2; |
|
574 |
}) |
|
575 |
}); |
|
576 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
577 |
$rows = $result->fetch_all_hash; |
|
578 |
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5}, |
|
579 |
{key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}], |
|
580 |
"$test : query edit callback"); |
|
581 |
|
|
582 |
|
|
583 |
test 'delete'; |
|
584 |
$dbi = DBI::Custom->new($NEW_ARGS->{0}); |
|
585 |
$dbi->do($CREATE_TABLE->{0}); |
|
586 |
$dbi->insert('table1', {key1 => 1, key2 => 2}); |
|
587 |
$dbi->insert('table1', {key1 => 3, key2 => 4}); |
|
588 |
$dbi->delete('table1', {key1 => 1}); |
|
589 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
590 |
$rows = $result->fetch_all_hash; |
|
591 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : basic"); |
|
592 |
|
|
593 |
$dbi->do("delete from table1;"); |
|
594 |
$dbi->insert('table1', {key1 => 1, key2 => 2}); |
|
595 |
$dbi->insert('table1', {key1 => 3, key2 => 4}); |
|
596 |
$dbi->delete('table1', {key2 => 1}, sub { |
|
597 |
my $query = shift; |
|
598 |
$query->bind_filter(sub { |
|
599 |
my ($key, $value) = @_; |
|
600 |
return $value * 2; |
|
601 |
}); |
|
602 |
}); |
|
603 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
604 |
$rows = $result->fetch_all_hash; |
|
605 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : query edit callback"); |
|
606 |
|
|
607 |
test 'delete error'; |
|
608 |
$dbi = DBI::Custom->new($NEW_ARGS->{0}); |
|
609 |
$dbi->do($CREATE_TABLE->{0}); |
|
610 |
eval{$dbi->delete('table1')}; |
|
611 |
like($@, qr/Key-value pairs for where clause must be specified to 'delete' second argument/, |
|
612 |
"$test : where key-value pairs not specified"); |
|
613 |
|
|
614 |
eval{$dbi->delete('table1', {key1 => 1}, 'aaa')}; |
|
615 |
like($@, qr/Query edit callback must be code reference/, |
|
616 |
"$test : query edit callback not code ref"); |
|
617 |
|
|
618 |
|
|
619 |
test 'delete_all'; |
|
620 |
$dbi = DBI::Custom->new($NEW_ARGS->{0}); |
|
621 |
$dbi->do($CREATE_TABLE->{0}); |
|
622 |
$dbi->insert('table1', {key1 => 1, key2 => 2}); |
|
623 |
$dbi->insert('table1', {key1 => 3, key2 => 4}); |
|
624 |
$dbi->delete_all('table1'); |
|
625 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
626 |
$rows = $result->fetch_all_hash; |
|
627 |
is_deeply($rows, [], "$test : basic"); |
|
628 |
|
|
629 |
|
|
add select
|
630 |
test 'select'; |
631 |
$dbi = DBI::Custom->new($NEW_ARGS->{0}); |
|
632 |
$dbi->do($CREATE_TABLE->{0}); |
|
633 |
$dbi->insert('table1', {key1 => 1, key2 => 2}); |
|
634 |
$dbi->insert('table1', {key1 => 3, key2 => 4}); |
|
add tests
|
635 |
$rows = $dbi->select('table1')->fetch_all_hash; |
add select
|
636 |
is_deeply($rows, [{key1 => 1, key2 => 2}, |
637 |
{key1 => 3, key2 => 4}], "$test : table"); |
|
638 |
|
|
add tests
|
639 |
$rows = $dbi->select('table1', ['key1'])->fetch_all_hash; |
640 |
is_deeply($rows, [{key1 => 1}, {key1 => 3}], "$test : table and columns and where key"); |
|
641 |
|
|
642 |
$rows = $dbi->select('table1', {key1 => 1})->fetch_all_hash; |
|
643 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : table and columns and where key"); |
|
644 |
|
|
645 |
$rows = $dbi->select('table1', ['key1'], {key1 => 3})->fetch_all_hash; |
|
646 |
is_deeply($rows, [{key1 => 3}], "$test : table and columns and where key"); |