removed register_format()
|
1 |
use Test::More; |
2 |
use strict; |
|
3 |
use warnings; |
|
4 | ||
5 |
use utf8; |
|
6 |
use Encode qw/encode_utf8 decode_utf8/; |
|
7 | ||
8 |
BEGIN { |
|
9 |
eval { require DBD::SQLite; 1 } |
|
10 |
or plan skip_all => 'DBD::SQLite required'; |
|
11 |
eval { DBD::SQLite->VERSION >= 1.25 } |
|
12 |
or plan skip_all => 'DBD::SQLite >= 1.25 required'; |
|
13 | ||
14 |
plan 'no_plan'; |
|
15 |
use_ok('DBIx::Custom'); |
|
16 |
} |
|
17 | ||
18 |
# Function for test name |
|
19 |
my $test; |
|
20 |
sub test { |
|
21 |
$test = shift; |
|
22 |
} |
|
23 | ||
24 |
# Constant varialbes for test |
|
25 |
my $CREATE_TABLE = { |
|
26 |
0 => 'create table table1 (key1 char(255), key2 char(255));', |
|
27 |
1 => 'create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));', |
|
28 |
2 => 'create table table2 (key1 char(255), key3 char(255));' |
|
29 |
}; |
|
30 | ||
31 |
my $SELECT_TMPLS = { |
|
32 |
0 => 'select * from table1;' |
|
33 |
}; |
|
34 | ||
35 |
my $DROP_TABLE = { |
|
36 |
0 => 'drop table table1' |
|
37 |
}; |
|
38 | ||
39 |
my $NEW_ARGS = { |
|
40 |
0 => {data_source => 'dbi:SQLite:dbname=:memory:'} |
|
41 |
}; |
|
42 | ||
43 |
# Variables |
|
44 |
my $dbi; |
|
45 |
my $sth; |
|
46 |
my $tmpl; |
|
47 |
my @tmpls; |
|
48 |
my $select_tmpl; |
|
49 |
my $insert_tmpl; |
|
50 |
my $update_tmpl; |
|
51 |
my $params; |
|
52 |
my $sql; |
|
53 |
my $result; |
|
54 |
my $row; |
|
55 |
my @rows; |
|
56 |
my $rows; |
|
57 |
my $query; |
|
58 |
my @queries; |
|
59 |
my $select_query; |
|
60 |
my $insert_query; |
|
61 |
my $update_query; |
|
62 |
my $ret_val; |
|
63 | ||
64 | ||
65 |
test 'disconnect'; |
|
66 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
67 |
$dbi->disconnect; |
|
68 |
ok(!$dbi->dbh, $test); |
|
69 | ||
70 | ||
71 |
test 'connected'; |
|
72 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
73 |
ok($dbi->connected, "$test : connected"); |
|
74 | ||
75 |
# Prepare table |
|
76 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
77 |
$dbi->execute($CREATE_TABLE->{0}); |
|
78 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
79 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
80 | ||
81 |
test 'DBIx::Custom::Result test'; |
|
82 |
$tmpl = "select key1, key2 from table1"; |
|
83 |
$query = $dbi->create_query($tmpl); |
|
84 |
$result = $dbi->execute($query); |
|
85 | ||
86 |
@rows = (); |
|
87 |
while (my $row = $result->fetch) { |
|
88 |
push @rows, [@$row]; |
|
89 |
} |
|
90 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch scalar context"); |
|
91 | ||
92 |
$result = $dbi->execute($query); |
|
93 |
@rows = (); |
|
94 |
while (my @row = $result->fetch) { |
|
95 |
push @rows, [@row]; |
|
96 |
} |
|
97 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch list context"); |
|
98 | ||
99 |
$result = $dbi->execute($query); |
|
100 |
@rows = (); |
|
101 |
while (my $row = $result->fetch_hash) { |
|
102 |
push @rows, {%$row}; |
|
103 |
} |
|
104 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch_hash scalar context"); |
|
105 | ||
106 |
$result = $dbi->execute($query); |
|
107 |
@rows = (); |
|
108 |
while (my %row = $result->fetch_hash) { |
|
109 |
push @rows, {%row}; |
|
110 |
} |
|
111 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch hash list context"); |
|
112 | ||
113 |
$result = $dbi->execute($query); |
|
114 |
$rows = $result->fetch_all; |
|
115 |
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all scalar context"); |
|
116 | ||
117 |
$result = $dbi->execute($query); |
|
118 |
@rows = $result->fetch_all; |
|
119 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_all list context"); |
|
120 | ||
121 |
$result = $dbi->execute($query); |
|
122 |
@rows = $result->fetch_hash_all; |
|
123 |
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_hash_all scalar context"); |
|
124 | ||
125 |
$result = $dbi->execute($query); |
|
126 |
@rows = $result->fetch_all; |
|
127 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_hash_all list context"); |
|
128 | ||
129 | ||
130 |
test 'Insert query return value'; |
|
131 |
$dbi->execute($DROP_TABLE->{0}); |
|
132 |
$dbi->execute($CREATE_TABLE->{0}); |
|
133 |
$tmpl = "insert into table1 {insert key1 key2}"; |
|
134 |
$query = $dbi->create_query($tmpl); |
|
135 |
$ret_val = $dbi->execute($query, param => {key1 => 1, key2 => 2}); |
|
136 |
ok($ret_val, $test); |
|
137 | ||
138 | ||
139 |
test 'Direct query'; |
|
140 |
$dbi->execute($DROP_TABLE->{0}); |
|
141 |
$dbi->execute($CREATE_TABLE->{0}); |
|
142 |
$insert_tmpl = "insert into table1 {insert key1 key2}"; |
|
143 |
$dbi->execute($insert_tmpl, param => {key1 => 1, key2 => 2}); |
|
144 |
$result = $dbi->execute($SELECT_TMPLS->{0}); |
|
145 |
$rows = $result->fetch_hash_all; |
|
146 |
is_deeply($rows, [{key1 => 1, key2 => 2}], $test); |
|
147 | ||
148 |
test 'Filter basic'; |
|
149 |
$dbi->execute($DROP_TABLE->{0}); |
|
150 |
$dbi->execute($CREATE_TABLE->{0}); |
|
151 |
$dbi->register_filter(twice => sub { $_[0] * 2}, |
|
152 |
three_times => sub { $_[0] * 3}); |
|
153 | ||
154 |
$insert_tmpl = "insert into table1 {insert key1 key2};"; |
|
155 |
$insert_query = $dbi->create_query($insert_tmpl); |
|
156 |
$insert_query->filter({key1 => 'twice'}); |
|
157 |
$dbi->execute($insert_query, param => {key1 => 1, key2 => 2}); |
|
158 |
$result = $dbi->execute($SELECT_TMPLS->{0}); |
|
159 |
$rows = $result->filter({key2 => 'three_times'})->fetch_hash_all; |
|
160 |
is_deeply($rows, [{key1 => 2, key2 => 6}], "$test : filter fetch_filter"); |
|
161 |
$dbi->execute($DROP_TABLE->{0}); |
|
162 | ||
163 |
test 'Filter in'; |
|
164 |
$dbi->execute($CREATE_TABLE->{0}); |
|
165 |
$insert_tmpl = "insert into table1 {insert key1 key2};"; |
|
166 |
$insert_query = $dbi->create_query($insert_tmpl); |
|
167 |
$dbi->execute($insert_query, param => {key1 => 2, key2 => 4}); |
|
168 |
$select_tmpl = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}"; |
|
169 |
$select_query = $dbi->create_query($select_tmpl); |
|
170 |
$select_query->filter({'table1.key1' => 'twice'}); |
|
171 |
$result = $dbi->execute($select_query, param => {'table1.key1' => [1,5], 'table1.key2' => [2,4]}); |
|
172 |
$rows = $result->fetch_hash_all; |
|
173 |
is_deeply($rows, [{key1 => 2, key2 => 4}], "$test : filter"); |
|
174 | ||
175 |
test 'DBIx::Custom::SQLTemplate basic tag'; |
|
176 |
$dbi->execute($DROP_TABLE->{0}); |
|
177 |
$dbi->execute($CREATE_TABLE->{1}); |
|
178 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
179 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
180 | ||
181 |
$tmpl = "select * from table1 where {= key1} and {<> key2} and {< key3} and {> key4} and {>= key5};"; |
|
182 |
$query = $dbi->create_query($tmpl); |
|
183 |
$result = $dbi->execute($query, param => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5}); |
|
184 |
$rows = $result->fetch_hash_all; |
|
185 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1"); |
|
186 | ||
187 |
$tmpl = "select * from table1 where {<= key1} and {like key2};"; |
|
188 |
$query = $dbi->create_query($tmpl); |
|
189 |
$result = $dbi->execute($query, param => {key1 => 1, key2 => '%2%'}); |
|
190 |
$rows = $result->fetch_hash_all; |
|
191 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2"); |
|
192 | ||
193 |
test 'DIB::Custom::SQLTemplate in tag'; |
|
194 |
$dbi->execute($DROP_TABLE->{0}); |
|
195 |
$dbi->execute($CREATE_TABLE->{1}); |
|
196 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
197 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
198 | ||
199 |
$tmpl = "select * from table1 where {in key1 2};"; |
|
200 |
$query = $dbi->create_query($tmpl); |
|
201 |
$result = $dbi->execute($query, param => {key1 => [9, 1]}); |
|
202 |
$rows = $result->fetch_hash_all; |
|
203 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic"); |
|
204 | ||
205 |
test 'DBIx::Custom::SQLTemplate insert tag'; |
|
206 |
$dbi->execute("delete from table1"); |
|
207 |
$insert_tmpl = 'insert into table1 {insert key1 key2 key3 key4 key5}'; |
|
208 |
$dbi->execute($insert_tmpl, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
209 | ||
210 |
$result = $dbi->execute($SELECT_TMPLS->{0}); |
|
211 |
$rows = $result->fetch_hash_all; |
|
212 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic"); |
|
213 | ||
214 |
test 'DBIx::Custom::SQLTemplate update tag'; |
|
215 |
$dbi->execute("delete from table1"); |
|
216 |
$insert_tmpl = "insert into table1 {insert key1 key2 key3 key4 key5}"; |
|
217 |
$dbi->execute($insert_tmpl, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
218 |
$dbi->execute($insert_tmpl, param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
219 | ||
220 |
$update_tmpl = 'update table1 {update key1 key2 key3 key4} where {= key5}'; |
|
221 |
$dbi->execute($update_tmpl, param => {key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5}); |
|
222 | ||
223 |
$result = $dbi->execute($SELECT_TMPLS->{0}); |
|
224 |
$rows = $result->fetch_hash_all; |
|
225 |
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5}, |
|
226 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : basic"); |
|
227 | ||
228 |
test 'Error case'; |
|
229 |
eval {DBIx::Custom->connect(data_source => 'dbi:SQLit')}; |
|
230 |
ok($@, "$test : connect error"); |
|
231 | ||
232 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
233 |
eval{$dbi->create_query("{p }")}; |
|
234 |
ok($@, "$test : create_query invalid SQL template"); |
|
235 | ||
236 |
test 'insert'; |
|
237 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
238 |
$dbi->execute($CREATE_TABLE->{0}); |
|
239 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
240 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
241 |
$result = $dbi->execute($SELECT_TMPLS->{0}); |
|
242 |
$rows = $result->fetch_hash_all; |
|
243 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : basic"); |
|
244 | ||
245 |
$dbi->execute('delete from table1'); |
|
246 |
$dbi->register_filter( |
|
247 |
twice => sub { $_[0] * 2 }, |
|
248 |
three_times => sub { $_[0] * 3 } |
|
249 |
); |
|
250 |
$dbi->default_query_filter('twice'); |
|
251 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => 'three_times'}); |
|
252 |
$result = $dbi->execute($SELECT_TMPLS->{0}); |
|
253 |
$rows = $result->fetch_hash_all; |
|
254 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : filter"); |
|
255 |
$dbi->default_query_filter(undef); |
|
256 | ||
257 |
$dbi->execute($DROP_TABLE->{0}); |
|
258 |
$dbi->execute($CREATE_TABLE->{0}); |
|
259 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, append => ' '); |
|
260 |
$rows = $dbi->select(table => 'table1')->fetch_hash_all; |
|
261 |
is_deeply($rows, [{key1 => 1, key2 => 2}], 'insert append'); |
|
262 | ||
263 |
test 'update'; |
|
264 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
265 |
$dbi->execute($CREATE_TABLE->{1}); |
|
266 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
267 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
268 |
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1}); |
|
269 |
$result = $dbi->execute($SELECT_TMPLS->{0}); |
|
270 |
$rows = $result->fetch_hash_all; |
|
271 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5}, |
|
272 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
273 |
"$test : basic"); |
|
274 |
|
|
275 |
$dbi->execute("delete from table1"); |
|
276 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
277 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
278 |
$dbi->update(table => 'table1', param => {key2 => 12}, where => {key2 => 2, key3 => 3}); |
|
279 |
$result = $dbi->execute($SELECT_TMPLS->{0}); |
|
280 |
$rows = $result->fetch_hash_all; |
|
281 |
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5}, |
|
282 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
283 |
"$test : update key same as search key"); |
|
284 | ||
285 |
$dbi->execute("delete from table1"); |
|
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(table => 'table1', param => {key2 => 11}, where => {key1 => 1}, |
|
290 |
filter => {key2 => 'twice'}); |
|
291 |
$result = $dbi->execute($SELECT_TMPLS->{0}); |
|
292 |
$rows = $result->fetch_hash_all; |
|
293 |
is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5}, |
|
294 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
295 |
"$test : filter"); |
|
296 | ||
297 | ||
298 |
$result = $dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1}, append => ' '); |
|
299 | ||
300 |
test 'update_all'; |
|
301 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
302 |
$dbi->execute($CREATE_TABLE->{1}); |
|
303 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
304 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
305 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
306 |
$dbi->update_all(table => 'table1', param => {key2 => 10}, filter => {key2 => 'twice'}); |
|
307 |
$result = $dbi->execute($SELECT_TMPLS->{0}); |
|
308 |
$rows = $result->fetch_hash_all; |
|
309 |
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5}, |
|
310 |
{key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}], |
|
311 |
"$test : filter"); |
|
312 | ||
313 | ||
314 |
test 'delete'; |
|
315 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
316 |
$dbi->execute($CREATE_TABLE->{0}); |
|
317 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
318 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
319 |
$dbi->delete(table => 'table1', where => {key1 => 1}); |
|
320 |
$result = $dbi->execute($SELECT_TMPLS->{0}); |
|
321 |
$rows = $result->fetch_hash_all; |
|
322 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : basic"); |
|
323 | ||
324 |
$dbi->execute("delete from table1;"); |
|
325 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
326 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
327 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
328 |
$dbi->delete(table => 'table1', where => {key2 => 1}, filter => {key2 => 'twice'}); |
|
329 |
$result = $dbi->execute($SELECT_TMPLS->{0}); |
|
330 |
$rows = $result->fetch_hash_all; |
|
331 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : filter"); |
|
332 | ||
333 |
$dbi->delete(table => 'table1', where => {key1 => 1}, append => ' '); |
|
334 | ||
335 |
$dbi->delete_all(table => 'table1'); |
|
336 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
337 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
338 |
$dbi->delete(table => 'table1', where => {key1 => 1, key2 => 2}); |
|
339 |
$rows = $dbi->select(table => 'table1')->fetch_hash_all; |
|
340 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : delete multi key"); |
|
341 | ||
342 |
__END__ |
|
343 | ||
344 |
test 'delete error'; |
|
345 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
346 |
$dbi->execute($CREATE_TABLE->{0}); |
|
347 |
eval{$dbi->delete(table => 'table1')}; |
|
348 |
like($@, qr/Key-value pairs for where clause must be specified to 'delete' second argument/, |
|
349 |
"$test : where key-value pairs not specified"); |
|
350 | ||
351 |
test 'delete_all'; |
|
352 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
353 |
$dbi->execute($CREATE_TABLE->{0}); |
|
354 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
355 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
356 |
$dbi->delete_all(table => 'table1'); |
|
357 |
$result = $dbi->execute($SELECT_TMPLS->{0}); |
|
358 |
$rows = $result->fetch_hash_all; |
|
359 |
is_deeply($rows, [], "$test : basic"); |
|
360 | ||
361 | ||
362 |
test 'select'; |
|
363 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
364 |
$dbi->execute($CREATE_TABLE->{0}); |
|
365 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
366 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
367 |
$rows = $dbi->select(table => 'table1')->fetch_hash_all; |
|
368 |
is_deeply($rows, [{key1 => 1, key2 => 2}, |
|
369 |
{key1 => 3, key2 => 4}], "$test : table"); |
|
370 | ||
371 |
$rows = $dbi->select(table => 'table1', columns => ['key1'])->fetch_hash_all; |
|
372 |
is_deeply($rows, [{key1 => 1}, {key1 => 3}], "$test : table and columns and where key"); |
|
373 | ||
374 |
$rows = $dbi->select(table => 'table1', where => {key1 => 1})->fetch_hash_all; |
|
375 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : table and columns and where key"); |
|
376 | ||
377 |
$rows = $dbi->select(table => 'table1', columns => ['key1'], where => {key1 => 3})->fetch_hash_all; |
|
378 |
is_deeply($rows, [{key1 => 3}], "$test : table and columns and where key"); |
|
379 | ||
380 |
$rows = $dbi->select(table => 'table1', append => "order by key1 desc limit 1")->fetch_hash_all; |
|
381 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : append statement"); |
|
382 | ||
383 |
$dbi->register_filter(decrement => sub { $_[0] - 1 }); |
|
384 |
$rows = $dbi->select(table => 'table1', {where => {key1 => 2}, filter => {key1 => 'decrement'}) |
|
385 |
->fetch_hash_all; |
|
386 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : filter"); |
|
387 | ||
388 |
$dbi->execute($CREATE_TABLE->{2}); |
|
389 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
|
390 |
$rows = $dbi->select( |
|
391 |
table => [qw/table1 table2/], |
|
392 |
columns => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'], |
|
393 |
where => {'table1.key2' => 2}, |
|
394 |
append => "where table1.key1 = table2.key1" |
|
395 |
)->fetch_hash_all; |
|
396 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "$test : join"); |
|
397 | ||
398 |
test 'Cache'; |
|
399 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
400 |
DBIx::Custom->query_cache_max(2); |
|
401 |
$dbi->execute($CREATE_TABLE->{0}); |
|
402 |
delete $DBIx::Custom::CLASS_ATTRS->{_query_caches}; |
|
403 |
delete $DBIx::Custom::CLASS_ATTRS->{_query_cache_keys}; |
|
404 |
$tmpls[0] = "insert into table1 {insert key1 key2}"; |
|
405 |
$queries[0] = $dbi->create_query($tmpls[0]); |
|
406 |
is(DBIx::Custom->_query_caches->{$tmpls[0]}{sql}, $queries[0]->sql, "$test : sql first"); |
|
407 |
is(DBIx::Custom->_query_caches->{$tmpls[0]}{columns}, $queries[0]->columns, "$test : columns first"); |
|
408 |
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls], "$test : cache key first"); |
|
409 | ||
410 |
$tmpls[1] = "select * from table1"; |
|
411 |
$queries[1] = $dbi->create_query($tmpls[1]); |
|
412 |
is(DBIx::Custom->_query_caches->{$tmpls[0]}{sql}, $queries[0]->sql, "$test : sql first"); |
|
413 |
is(DBIx::Custom->_query_caches->{$tmpls[0]}{columns}, $queries[0]->columns, "$test : columns first"); |
|
414 |
is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql second"); |
|
415 |
is(DBIx::Custom->_query_caches->{$tmpls[1]}{columns}, $queries[1]->columns, "$test : columns second"); |
|
416 |
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls], "$test : cache key second"); |
|
417 | ||
418 |
$tmpls[2] = "select key1, key2 from table1"; |
|
419 |
$queries[2] = $dbi->create_query($tmpls[2]); |
|
420 |
ok(!exists DBIx::Custom->_query_caches->{$tmpls[0]}, "$test : cache overflow deleted key"); |
|
421 |
is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql cache overflow deleted key"); |
|
422 |
is(DBIx::Custom->_query_caches->{$tmpls[1]}{columns}, $queries[1]->columns, "$test : columns cache overflow deleted key"); |
|
423 |
is(DBIx::Custom->_query_caches->{$tmpls[2]}{sql}, $queries[2]->sql, "$test : sql cache overflow deleted key"); |
|
424 |
is(DBIx::Custom->_query_caches->{$tmpls[2]}{columns}, $queries[2]->columns, "$test : columns cache overflow deleted key"); |
|
425 |
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls[1, 2]], "$test : cache key third"); |
|
426 | ||
427 |
$queries[1] = $dbi->create_query($tmpls[1]); |
|
428 |
ok(!exists DBIx::Custom->_query_caches->{$tmpls[0]}, "$test : cache overflow deleted key"); |
|
429 |
is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql cache overflow deleted key"); |
|
430 |
is_deeply(DBIx::Custom->_query_caches->{$tmpls[1]}{columns}, $queries[1]->columns, "$test : columns cache overflow deleted key"); |
|
431 |
is(DBIx::Custom->_query_caches->{$tmpls[2]}{sql}, $queries[2]->sql, "$test : sql cache overflow deleted key"); |
|
432 |
is_deeply(DBIx::Custom->_query_caches->{$tmpls[2]}{columns}, $queries[2]->columns, "$test : columns cache overflow deleted key"); |
|
433 |
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls[1, 2]], "$test : cache key third"); |
|
434 | ||
435 |
$query = $dbi->create_query($tmpls[0]); |
|
436 |
$query->filter('aaa'); |
|
437 |
$query = $dbi->create_query($tmpls[0]); |
|
438 |
ok(!$query->filter, "$test : only cached sql and columns"); |
|
439 |
$query->filter('bbb'); |
|
440 |
$query = $dbi->create_query($tmpls[0]); |
|
441 |
ok(!$query->filter, "$test : only cached sql and columns"); |
|
442 | ||
443 |
test 'fetch filter'; |
|
444 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
445 |
$dbi->register_filter( |
|
446 |
twice => sub { $_[0] * 2 }, |
|
447 |
three_times => sub { $_[0] * 3 } |
|
448 |
); |
|
449 |
$dbi->default_fetch_filter('twice'); |
|
450 |
$dbi->execute($CREATE_TABLE->{0}); |
|
451 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
452 |
$result = $dbi->select(table => 'table1'); |
|
453 |
$result->filter({key1 => 'three_times'}); |
|
454 |
$row = $result->fetch_hash_single; |
|
455 |
is_deeply($row, {key1 => 3, key2 => 4}, "$test: default_fetch_filter and filter"); |
|
456 | ||
457 |
test 'filters'; |
|
458 |
$dbi = DBIx::Custom->new; |
|
459 | ||
460 |
ok($dbi->filters->{decode_utf8}->(encode_utf8('あ')), |
|
461 |
'あ', "$test : decode_utf8;); |
|
462 | ||
463 |
is($dbi->filters->{encode_utf8}->('あ'), |
|
464 |
encode_utf8('あ'), "$test : encode_utf8"); |
|
465 |