cleanup test
|
1 |
use Test::More; |
2 |
use strict; |
|
3 |
use warnings; |
|
4 |
use utf8; |
|
5 |
use Encode qw/encode_utf8 decode_utf8/; |
|
test cleanup
|
6 |
use FindBin; |
cleanup test
|
7 |
use lib "$FindBin::Bin/common"; |
cleanup test
|
8 | |
9 |
BEGIN { |
|
10 |
eval { require DBD::SQLite; 1 } |
|
11 |
or plan skip_all => 'DBD::SQLite required'; |
|
12 |
eval { DBD::SQLite->VERSION >= 1.25 } |
|
13 |
or plan skip_all => 'DBD::SQLite >= 1.25 required'; |
|
14 | ||
15 |
plan 'no_plan'; |
|
16 |
use_ok('DBIx::Custom'); |
|
17 |
} |
|
18 | ||
test cleanup
|
19 |
$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/}; |
cleanup test
|
20 |
sub test { print "# $_[0]\n" } |
21 | ||
test cleanup
|
22 |
use DBIx::Custom; |
test cleanup
|
23 |
{ |
24 |
package DBIx::Custom; |
|
25 |
has dsn => sub { 'dbi:SQLite:dbname=:memory:' } |
|
26 |
} |
|
test cleanup
|
27 |
use MyDBI1; |
28 |
{ |
|
29 |
package MyDBI4; |
|
30 | ||
31 |
use strict; |
|
32 |
use warnings; |
|
33 | ||
34 |
use base 'DBIx::Custom'; |
|
35 | ||
36 |
sub connect { |
|
37 |
my $self = shift->SUPER::connect(@_); |
|
38 |
|
|
39 |
$self->include_model( |
|
40 |
MyModel2 => [ |
|
41 |
'book', |
|
42 |
{class => 'Company', name => 'company'} |
|
43 |
] |
|
44 |
); |
|
45 |
} |
|
46 | ||
47 |
package MyModel2::Base1; |
|
48 | ||
49 |
use strict; |
|
50 |
use warnings; |
|
51 | ||
52 |
use base 'DBIx::Custom::Model'; |
|
53 | ||
54 |
package MyModel2::book; |
|
55 | ||
56 |
use strict; |
|
57 |
use warnings; |
|
58 | ||
59 |
use base 'MyModel2::Base1'; |
|
60 | ||
61 |
sub insert { |
|
62 |
my ($self, $param) = @_; |
|
63 |
|
|
64 |
return $self->SUPER::insert(param => $param); |
|
65 |
} |
|
66 | ||
67 |
sub list { shift->select; } |
|
68 | ||
69 |
package MyModel2::Company; |
|
70 | ||
71 |
use strict; |
|
72 |
use warnings; |
|
73 | ||
74 |
use base 'MyModel2::Base1'; |
|
75 | ||
76 |
sub insert { |
|
77 |
my ($self, $param) = @_; |
|
78 |
|
|
79 |
return $self->SUPER::insert(param => $param); |
|
80 |
} |
|
81 | ||
82 |
sub list { shift->select; } |
|
83 |
} |
|
84 |
{ |
|
85 |
package MyDBI5; |
|
86 | ||
87 |
use strict; |
|
88 |
use warnings; |
|
89 | ||
90 |
use base 'DBIx::Custom'; |
|
91 | ||
92 |
sub connect { |
|
93 |
my $self = shift->SUPER::connect(@_); |
|
94 |
|
|
95 |
$self->include_model('MyModel4'); |
|
96 |
} |
|
97 |
} |
|
98 |
{ |
|
99 |
package MyDBI6; |
|
100 |
|
|
101 |
use base 'DBIx::Custom'; |
|
102 |
|
|
103 |
sub connect { |
|
104 |
my $self = shift->SUPER::connect(@_); |
|
105 |
|
|
106 |
$self->include_model('MyModel5'); |
|
107 |
|
|
108 |
return $self; |
|
109 |
} |
|
110 |
} |
|
111 |
{ |
|
112 |
package MyDBI7; |
|
113 |
|
|
114 |
use base 'DBIx::Custom'; |
|
115 |
|
|
116 |
sub connect { |
|
117 |
my $self = shift->SUPER::connect(@_); |
|
118 |
|
|
119 |
$self->include_model('MyModel6'); |
|
120 |
|
|
121 |
|
|
122 |
return $self; |
|
123 |
} |
|
124 |
} |
|
125 |
{ |
|
126 |
package MyDBI8; |
|
127 |
|
|
128 |
use base 'DBIx::Custom'; |
|
129 |
|
|
130 |
sub connect { |
|
131 |
my $self = shift->SUPER::connect(@_); |
|
132 |
|
|
133 |
$self->include_model('MyModel7'); |
|
134 |
|
|
135 |
return $self; |
|
136 |
} |
|
137 |
} |
|
138 | ||
139 |
{ |
|
140 |
package MyDBI9; |
|
141 |
|
|
142 |
use base 'DBIx::Custom'; |
|
143 |
|
|
144 |
sub connect { |
|
145 |
my $self = shift->SUPER::connect(@_); |
|
146 |
|
|
147 |
$self->include_model('MyModel8')->setup_model; |
|
148 |
|
|
149 |
return $self; |
|
150 |
} |
|
151 |
} |
|
test cleanup
|
152 | |
cleanup test
|
153 |
# Constant |
cleanup test
|
154 |
my $create_table1 = 'create table table1 (key1 varchar, key2 varchar);'; |
155 |
my $create_table1_2 = 'create table table1 (key1 varchar, key2 varchar, key3 varchar, key4 varchar, key5 varchar);'; |
|
156 |
my $create_table2 = 'create table table2 (key1 varchar, key3 varchar);'; |
|
157 |
my $create_table2_2 = "create table table2 (key1 varchar, key2 varchar, key3 varchar)"; |
|
158 |
my $create_table3 = "create table table3 (key1 varchar, key2 varchar, key3 varchar)"; |
|
159 |
my $create_table_reserved = 'create table "table" ("select" varchar, "update" varchar)'; |
|
test cleanup
|
160 | |
test cleanup
|
161 |
my $q = '"'; |
162 |
my $p = '"'; |
|
cleanup test
|
163 | |
cleanup test
|
164 |
# Variables |
cleanup test
|
165 |
my $builder; |
166 |
my $datas; |
|
cleanup test
|
167 |
my $dbi; |
168 |
my $sth; |
|
169 |
my $source; |
|
170 |
my @sources; |
|
cleanup test
|
171 |
my $select_source; |
172 |
my $insert_source; |
|
173 |
my $update_source; |
|
cleanup test
|
174 |
my $param; |
175 |
my $params; |
|
176 |
my $sql; |
|
177 |
my $result; |
|
178 |
my $row; |
|
179 |
my @rows; |
|
180 |
my $rows; |
|
181 |
my $query; |
|
182 |
my @queries; |
|
183 |
my $select_query; |
|
184 |
my $insert_query; |
|
185 |
my $update_query; |
|
186 |
my $ret_val; |
|
187 |
my $infos; |
|
188 |
my $model; |
|
189 |
my $model2; |
|
190 |
my $where; |
|
191 |
my $update_param; |
|
192 |
my $insert_param; |
|
cleanup test
|
193 |
my $join; |
cleanup test
|
194 |
my $binary; |
cleanup test
|
195 | |
196 |
# Prepare table |
|
test cleanup
|
197 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
198 | |
test cleanup
|
199 |
### a little complex test |
cleanup test
|
200 |
test 'table_alias'; |
201 |
$dbi = DBIx::Custom->connect; |
|
202 |
eval { $dbi->execute('drop table table1') }; |
|
203 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
204 |
$dbi->type_rule( |
|
205 |
into1 => { |
|
206 |
date => sub { uc $_[0] } |
|
207 |
} |
|
208 |
); |
|
209 |
$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => 'a'}, |
|
210 |
table_alias => {table2 => 'table1'}); |
|
211 |
$result = $dbi->select(table => 'table1'); |
|
212 |
is($result->one->{key1}, 'A'); |
|
213 | ||
cleanup test
|
214 | |
cleanup test
|
215 |
test 'join'; |
216 |
$dbi = DBIx::Custom->connect; |
|
217 |
eval { $dbi->execute('drop table table1') }; |
|
218 |
$dbi->execute($create_table1); |
|
219 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
220 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
221 |
$dbi->execute($create_table2); |
|
222 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
|
223 |
$dbi->execute('create table table3 (key3 int, key4 int);'); |
|
224 |
$dbi->insert(table => 'table3', param => {key3 => 5, key4 => 4}); |
|
225 |
$rows = $dbi->select( |
|
226 |
table => 'table1', |
|
227 |
column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3', |
|
228 |
where => {'table1.key2' => 2}, |
|
229 |
join => ['left outer join table2 on table1.key1 = table2.key1'] |
|
230 |
)->all; |
|
231 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}]); |
|
232 | ||
233 |
$rows = $dbi->select( |
|
234 |
table => 'table1', |
|
235 |
where => {'key1' => 1}, |
|
236 |
join => ['left outer join table2 on table1.key1 = table2.key1'] |
|
237 |
)->all; |
|
238 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
239 | ||
240 |
eval { |
|
241 |
$rows = $dbi->select( |
|
242 |
table => 'table1', |
|
243 |
column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3', |
|
244 |
where => {'table1.key2' => 2}, |
|
245 |
join => {'table1.key1' => 'table2.key1'} |
|
246 |
); |
|
247 |
}; |
|
248 |
like ($@, qr/array/); |
|
249 | ||
250 |
$rows = $dbi->select( |
|
251 |
table => 'table1', |
|
252 |
where => {'key1' => 1}, |
|
253 |
join => ['left outer join table2 on table1.key1 = table2.key1', |
|
254 |
'left outer join table3 on table2.key3 = table3.key3'] |
|
255 |
)->all; |
|
256 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
257 | ||
258 |
$rows = $dbi->select( |
|
259 |
column => 'table3.key4 as table3__key4', |
|
260 |
table => 'table1', |
|
261 |
where => {'table1.key1' => 1}, |
|
262 |
join => ['left outer join table2 on table1.key1 = table2.key1', |
|
263 |
'left outer join table3 on table2.key3 = table3.key3'] |
|
264 |
)->all; |
|
265 |
is_deeply($rows, [{table3__key4 => 4}]); |
|
266 | ||
267 |
$rows = $dbi->select( |
|
268 |
column => 'table1.key1 as table1__key1', |
|
269 |
table => 'table1', |
|
270 |
where => {'table3.key4' => 4}, |
|
271 |
join => ['left outer join table2 on table1.key1 = table2.key1', |
|
272 |
'left outer join table3 on table2.key3 = table3.key3'] |
|
273 |
)->all; |
|
274 |
is_deeply($rows, [{table1__key1 => 1}]); |
|
275 | ||
276 |
$dbi = DBIx::Custom->connect; |
|
277 |
$dbi->quote('"'); |
|
278 |
eval { $dbi->execute('drop table table1') }; |
|
279 |
$dbi->execute($create_table1); |
|
280 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
281 |
$dbi->execute($create_table2); |
|
282 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
|
283 |
$rows = $dbi->select( |
|
284 |
table => 'table1', |
|
285 |
column => '"table1"."key1" as "table1_key1", "table2"."key1" as "table2_key1", "key2", "key3"', |
|
286 |
where => {'table1.key2' => 2}, |
|
287 |
join => ['left outer join "table2" on "table1"."key1" = "table2"."key1"'], |
|
288 |
)->all; |
|
289 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], |
|
290 |
'quote'); |
|
291 | ||
292 | ||
293 |
$dbi = DBIx::Custom->connect; |
|
294 |
eval { $dbi->execute('drop table table1') }; |
|
295 |
$dbi->execute($create_table1); |
|
296 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
297 |
$sql = <<"EOS"; |
|
298 |
left outer join ( |
|
299 |
select * from table1 as t1 |
|
300 |
where t1.key2 = ( |
|
301 |
select max(t2.key2) from table1 as t2 |
|
302 |
where t1.key1 = t2.key1 |
|
303 |
) |
|
304 |
) as latest_table1 on table1.key1 = latest_table1.key1 |
|
305 |
EOS |
|
306 |
$join = [$sql]; |
|
307 |
$rows = $dbi->select( |
|
308 |
table => 'table1', |
|
309 |
column => 'latest_table1.key1 as latest_table1__key1', |
|
310 |
join => $join |
|
311 |
)->all; |
|
312 |
is_deeply($rows, [{latest_table1__key1 => 1}]); |
|
313 | ||
314 |
$dbi = DBIx::Custom->connect; |
|
315 |
eval { $dbi->execute('drop table table1') }; |
|
316 |
eval { $dbi->execute('drop table table2') }; |
|
317 |
$dbi->execute($create_table1); |
|
318 |
$dbi->execute($create_table2); |
|
319 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
320 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4}); |
|
321 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
|
322 |
$result = $dbi->select( |
|
323 |
table => 'table1', |
|
324 |
join => [ |
|
325 |
"left outer join table2 on table2.key2 = '4' and table1.key1 = table2.key1" |
|
326 |
] |
|
327 |
); |
|
328 |
is_deeply($result->all, [{key1 => 1, key2 => 2}]); |
|
329 |
$result = $dbi->select( |
|
330 |
table => 'table1', |
|
331 |
column => [{table2 => ['key3']}], |
|
332 |
join => [ |
|
333 |
"left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1" |
|
334 |
] |
|
335 |
); |
|
336 |
is_deeply($result->all, [{'table2.key3' => 4}]); |
|
337 |
$result = $dbi->select( |
|
338 |
table => 'table1', |
|
339 |
column => [{table2 => ['key3']}], |
|
340 |
join => [ |
|
341 |
"left outer join table2 on table1.key1 = table2.key1 and table2.key3 = '4'" |
|
342 |
] |
|
343 |
); |
|
344 |
is_deeply($result->all, [{'table2.key3' => 4}]); |
|
345 | ||
346 |
$dbi = DBIx::Custom->connect; |
|
347 |
eval { $dbi->execute('drop table table1') }; |
|
348 |
eval { $dbi->execute('drop table table2') }; |
|
349 |
$dbi->execute($create_table1); |
|
350 |
$dbi->execute($create_table2); |
|
351 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
352 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4}); |
|
353 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
|
354 |
$result = $dbi->select( |
|
355 |
table => 'table1', |
|
356 |
column => [{table2 => ['key3']}], |
|
357 |
join => [ |
|
358 |
{ |
|
359 |
clause => "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1", |
|
360 |
table => ['table1', 'table2'] |
|
361 |
} |
|
362 |
] |
|
363 |
); |
|
364 |
is_deeply($result->all, [{'table2.key3' => 4}]); |
|
365 | ||
test cleanup
|
366 | |
367 |
test 'update_param'; |
|
368 |
$dbi = DBIx::Custom->connect; |
|
369 |
eval { $dbi->execute('drop table table1') }; |
|
370 |
$dbi->execute($create_table1_2); |
|
371 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
372 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
373 | ||
374 |
$param = {key2 => 11}; |
|
375 |
$update_param = $dbi->update_param($param); |
|
376 |
$sql = <<"EOS"; |
|
377 |
update table1 $update_param |
|
378 |
where key1 = 1 |
|
379 |
EOS |
|
380 |
$dbi->execute($sql, param => $param); |
|
381 |
$result = $dbi->execute('select * from table1;', table => 'table1'); |
|
382 |
$rows = $result->all; |
|
383 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5}, |
|
384 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
385 |
"basic"); |
|
386 | ||
387 | ||
388 |
$dbi = DBIx::Custom->connect; |
|
389 |
eval { $dbi->execute('drop table table1') }; |
|
390 |
$dbi->execute($create_table1_2); |
|
391 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
392 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
393 | ||
394 |
$param = {key2 => 11, key3 => 33}; |
|
395 |
$update_param = $dbi->update_param($param); |
|
396 |
$sql = <<"EOS"; |
|
397 |
update table1 $update_param |
|
398 |
where key1 = 1 |
|
399 |
EOS |
|
400 |
$dbi->execute($sql, param => $param); |
|
401 |
$result = $dbi->execute('select * from table1;', table => 'table1'); |
|
402 |
$rows = $result->all; |
|
403 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5}, |
|
404 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
405 |
"basic"); |
|
406 | ||
407 |
$dbi = DBIx::Custom->connect; |
|
408 |
eval { $dbi->execute('drop table table1') }; |
|
409 |
$dbi->execute($create_table1_2); |
|
410 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
411 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
412 | ||
413 |
$param = {key2 => 11, key3 => 33}; |
|
414 |
$update_param = $dbi->update_param($param, {no_set => 1}); |
|
415 |
$sql = <<"EOS"; |
|
416 |
update table1 set $update_param |
|
417 |
where key1 = 1 |
|
418 |
EOS |
|
419 |
$dbi->execute($sql, param => $param); |
|
420 |
$result = $dbi->execute('select * from table1;', table => 'table1'); |
|
421 |
$rows = $result->all; |
|
422 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5}, |
|
423 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
424 |
"update param no_set"); |
|
425 | ||
426 |
|
|
427 |
eval { $dbi->update_param({";" => 1}) }; |
|
428 |
like($@, qr/not safety/); |
|
429 | ||
430 | ||
431 |
test 'update_param'; |
|
432 |
$dbi = DBIx::Custom->connect; |
|
433 |
eval { $dbi->execute('drop table table1') }; |
|
434 |
$dbi->execute($create_table1_2); |
|
435 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
436 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
437 | ||
438 |
$param = {key2 => 11}; |
|
439 |
$update_param = $dbi->assign_param($param); |
|
440 |
$sql = <<"EOS"; |
|
441 |
update table1 set $update_param |
|
442 |
where key1 = 1 |
|
443 |
EOS |
|
444 |
$dbi->execute($sql, param => $param, table => 'table1'); |
|
445 |
$result = $dbi->execute('select * from table1;'); |
|
446 |
$rows = $result->all; |
|
447 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5}, |
|
448 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
449 |
"basic"); |
|
450 | ||
451 | ||
cleanup test
|
452 |
test 'type option'; # DEPRECATED! |
453 |
$dbi = DBIx::Custom->connect( |
|
454 |
data_source => 'dbi:SQLite:dbname=:memory:', |
|
455 |
dbi_option => { |
|
456 |
$DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1) |
|
457 |
} |
|
cleanup test
|
458 |
); |
cleanup test
|
459 |
$binary = pack("I3", 1, 2, 3); |
460 |
eval { $dbi->execute('drop table table1') }; |
|
461 |
$dbi->execute('create table table1(key1, key2)'); |
|
462 |
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]); |
|
463 |
$result = $dbi->select(table => 'table1'); |
|
464 |
$row = $result->one; |
|
465 |
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic"); |
|
466 |
$result = $dbi->execute('select length(key1) as key1_length from table1'); |
|
467 |
$row = $result->one; |
|
468 |
is($row->{key1_length}, length $binary); |
|
cleanup test
|
469 | |
cleanup test
|
470 |
test 'type_rule from'; |
471 |
$dbi = DBIx::Custom->connect; |
|
472 |
$dbi->type_rule( |
|
473 |
from1 => { |
|
474 |
date => sub { uc $_[0] } |
|
475 |
} |
|
cleanup test
|
476 |
); |
cleanup test
|
477 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
478 |
$dbi->insert({key1 => 'a'}, table => 'table1'); |
|
479 |
$result = $dbi->select(table => 'table1'); |
|
480 |
is($result->fetch_first->[0], 'A'); |
|
cleanup test
|
481 | |
cleanup test
|
482 |
$result = $dbi->select(table => 'table1'); |
483 |
is($result->one->{key1}, 'A'); |
|
cleanup test
|
484 | |
cleanup test
|
485 | |
486 |
test 'type_rule into'; |
|
test cleanup
|
487 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
488 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
489 |
$dbi->type_rule( |
|
490 |
into1 => { |
|
491 |
date => sub { uc $_[0] } |
|
492 |
} |
|
493 |
); |
|
cleanup test
|
494 |
$dbi->insert({key1 => 'a'}, table => 'table1'); |
cleanup test
|
495 |
$result = $dbi->select(table => 'table1'); |
496 |
is($result->one->{key1}, 'A'); |
|
497 | ||
test cleanup
|
498 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
499 |
$dbi->execute("create table table1 (key1 date, key2 datetime)"); |
500 |
$dbi->type_rule( |
|
501 |
into1 => [ |
|
502 |
[qw/date datetime/] => sub { uc $_[0] } |
|
503 |
] |
|
504 |
); |
|
505 |
$dbi->insert({key1 => 'a', key2 => 'b'}, table => 'table1'); |
|
506 |
$result = $dbi->select(table => 'table1'); |
|
507 |
$row = $result->one; |
|
508 |
is($row->{key1}, 'A'); |
|
509 |
is($row->{key2}, 'B'); |
|
cleanup test
|
510 | |
test cleanup
|
511 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
512 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
513 |
$dbi->insert({key1 => 'a', key2 => 'B'}, table => 'table1'); |
|
514 |
$dbi->type_rule( |
|
515 |
into1 => [ |
|
516 |
[qw/date datetime/] => sub { uc $_[0] } |
|
517 |
] |
|
518 |
); |
|
519 |
$result = $dbi->execute( |
|
520 |
"select * from table1 where key1 = :key1 and key2 = :table1.key2;", |
|
521 |
param => {key1 => 'a', 'table1.key2' => 'b'} |
|
522 |
); |
|
523 |
$row = $result->one; |
|
524 |
is($row->{key1}, 'a'); |
|
525 |
is($row->{key2}, 'B'); |
|
cleanup test
|
526 | |
test cleanup
|
527 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
528 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
529 |
$dbi->insert({key1 => 'A', key2 => 'B'}, table => 'table1'); |
|
530 |
$dbi->type_rule( |
|
531 |
into1 => [ |
|
532 |
[qw/date datetime/] => sub { uc $_[0] } |
|
533 |
] |
|
534 |
); |
|
535 |
$result = $dbi->execute( |
|
536 |
"select * from table1 where key1 = :key1 and key2 = :table1.key2;", |
|
537 |
param => {key1 => 'a', 'table1.key2' => 'b'}, |
|
538 |
table => 'table1' |
|
539 |
); |
|
540 |
$row = $result->one; |
|
541 |
is($row->{key1}, 'A'); |
|
542 |
is($row->{key2}, 'B'); |
|
cleanup test
|
543 | |
cleanup test
|
544 |
$dbi = DBIx::Custom->connect; |
545 |
$dbi->execute("create table table1 (key1 date, key2 datetime)"); |
|
546 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
547 |
$dbi->type_rule( |
|
548 |
from1 => { |
|
549 |
date => 'twice', |
|
550 |
}, |
|
551 |
into1 => { |
|
552 |
date => 'twice', |
|
553 |
} |
|
554 |
); |
|
555 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
556 |
$result = $dbi->select(table => 'table1'); |
|
557 |
is($result->fetch->[0], 8); |
|
cleanup test
|
558 | |
cleanup test
|
559 |
test 'type_rule and filter order'; |
test cleanup
|
560 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
561 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
562 |
$dbi->type_rule( |
|
563 |
into1 => { |
|
564 |
date => sub { $_[0] . 'b' } |
|
565 |
}, |
|
566 |
into2 => { |
|
567 |
date => sub { $_[0] . 'c' } |
|
568 |
}, |
|
569 |
from1 => { |
|
570 |
date => sub { $_[0] . 'd' } |
|
571 |
}, |
|
572 |
from2 => { |
|
573 |
date => sub { $_[0] . 'e' } |
|
574 |
} |
|
575 |
); |
|
576 |
$dbi->insert({key1 => '1'}, table => 'table1', filter => {key1 => sub { $_[0] . 'a' }}); |
|
577 |
$result = $dbi->select(table => 'table1'); |
|
578 |
$result->filter(key1 => sub { $_[0] . 'f' }); |
|
579 |
is($result->fetch_first->[0], '1abcdef'); |
|
cleanup test
|
580 | |
cleanup test
|
581 |
$dbi = DBIx::Custom->connect; |
582 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
583 |
$dbi->type_rule( |
|
584 |
from1 => { |
|
585 |
date => sub { $_[0] . 'p' } |
|
586 |
}, |
|
587 |
from2 => { |
|
588 |
date => sub { $_[0] . 'q' } |
|
589 |
}, |
|
590 |
); |
|
591 |
$dbi->insert({key1 => '1'}, table => 'table1'); |
|
592 |
$result = $dbi->select(table => 'table1'); |
|
593 |
$result->type_rule( |
|
594 |
from1 => { |
|
595 |
date => sub { $_[0] . 'd' } |
|
596 |
}, |
|
597 |
from2 => { |
|
598 |
date => sub { $_[0] . 'e' } |
|
599 |
} |
|
600 |
); |
|
601 |
$result->filter(key1 => sub { $_[0] . 'f' }); |
|
602 |
is($result->fetch_first->[0], '1def'); |
|
cleanup test
|
603 | |
cleanup test
|
604 |
test 'type_rule_off'; |
605 |
$dbi = DBIx::Custom->connect; |
|
606 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
607 |
$dbi->type_rule( |
|
608 |
from1 => { |
|
609 |
date => sub { $_[0] * 2 }, |
|
610 |
}, |
|
611 |
into1 => { |
|
612 |
date => sub { $_[0] * 2 }, |
|
613 |
} |
|
614 |
); |
|
615 |
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1); |
|
616 |
$result = $dbi->select(table => 'table1', type_rule_off => 1); |
|
617 |
is($result->type_rule_off->fetch->[0], 2); |
|
cleanup test
|
618 | |
cleanup test
|
619 |
$dbi = DBIx::Custom->connect; |
620 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
621 |
$dbi->type_rule( |
|
622 |
from1 => { |
|
623 |
date => sub { $_[0] * 2 }, |
|
624 |
}, |
|
625 |
into1 => { |
|
626 |
date => sub { $_[0] * 3 }, |
|
627 |
} |
|
628 |
); |
|
629 |
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1); |
|
630 |
$result = $dbi->select(table => 'table1', type_rule_off => 1); |
|
631 |
is($result->one->{key1}, 4); |
|
cleanup test
|
632 | |
cleanup test
|
633 |
$dbi = DBIx::Custom->connect; |
634 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
635 |
$dbi->type_rule( |
|
636 |
from1 => { |
|
637 |
date => sub { $_[0] * 2 }, |
|
638 |
}, |
|
639 |
into1 => { |
|
640 |
date => sub { $_[0] * 3 }, |
|
641 |
} |
|
642 |
); |
|
643 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
644 |
$result = $dbi->select(table => 'table1'); |
|
645 |
is($result->one->{key1}, 12); |
|
cleanup test
|
646 | |
cleanup test
|
647 |
$dbi = DBIx::Custom->connect; |
648 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
649 |
$dbi->type_rule( |
|
650 |
from1 => { |
|
651 |
date => sub { $_[0] * 2 }, |
|
652 |
}, |
|
653 |
into1 => { |
|
654 |
date => sub { $_[0] * 3 }, |
|
655 |
} |
|
cleanup test
|
656 |
); |
cleanup test
|
657 |
$dbi->insert({key1 => 2}, table => 'table1'); |
658 |
$result = $dbi->select(table => 'table1'); |
|
659 |
is($result->fetch->[0], 12); |
|
cleanup test
|
660 | |
cleanup test
|
661 |
$dbi = DBIx::Custom->connect; |
662 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
663 |
$dbi->register_filter(ppp => sub { uc $_[0] }); |
|
664 |
$dbi->type_rule( |
|
665 |
into1 => { |
|
666 |
date => 'ppp' |
|
cleanup test
|
667 |
} |
cleanup test
|
668 |
); |
669 |
$dbi->insert({key1 => 'a'}, table => 'table1'); |
|
670 |
$result = $dbi->select(table => 'table1'); |
|
671 |
is($result->one->{key1}, 'A'); |
|
cleanup test
|
672 | |
cleanup test
|
673 |
eval{$dbi->type_rule( |
674 |
into1 => { |
|
675 |
date => 'pp' |
|
cleanup test
|
676 |
} |
cleanup test
|
677 |
)}; |
678 |
like($@, qr/not registered/); |
|
cleanup test
|
679 | |
test cleanup
|
680 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
681 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
682 |
eval { |
|
683 |
$dbi->type_rule( |
|
684 |
from1 => { |
|
685 |
Date => sub { $_[0] * 2 }, |
|
686 |
} |
|
687 |
); |
|
688 |
}; |
|
689 |
like($@, qr/lower/); |
|
clenup test
|
690 | |
cleanup test
|
691 |
eval { |
692 |
$dbi->type_rule( |
|
693 |
into1 => { |
|
694 |
Date => sub { $_[0] * 2 }, |
|
695 |
} |
|
696 |
); |
|
697 |
}; |
|
698 |
like($@, qr/lower/); |
|
clenup test
|
699 | |
cleanup test
|
700 |
$dbi = DBIx::Custom->connect; |
701 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
702 |
$dbi->type_rule( |
|
703 |
from1 => { |
|
704 |
date => sub { $_[0] * 2 }, |
|
705 |
}, |
|
706 |
into1 => { |
|
707 |
date => sub { $_[0] * 3 }, |
|
708 |
} |
|
709 |
); |
|
710 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
clenup test
|
711 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
712 |
$result->type_rule_off; |
713 |
is($result->one->{key1}, 6); |
|
clenup test
|
714 | |
cleanup test
|
715 |
$dbi = DBIx::Custom->connect; |
716 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
717 |
$dbi->type_rule( |
|
718 |
from1 => { |
|
719 |
date => sub { $_[0] * 2 }, |
|
720 |
datetime => sub { $_[0] * 4 }, |
|
721 |
}, |
|
722 |
); |
|
723 |
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1'); |
|
clenup test
|
724 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
725 |
$result->type_rule( |
726 |
from1 => { |
|
727 |
date => sub { $_[0] * 3 } |
|
728 |
} |
|
729 |
); |
|
730 |
$row = $result->one; |
|
731 |
is($row->{key1}, 6); |
|
732 |
is($row->{key2}, 2); |
|
clenup test
|
733 | |
734 |
$result = $dbi->select(table => 'table1'); |
|
cleanup test
|
735 |
$result->type_rule( |
736 |
from1 => { |
|
737 |
date => sub { $_[0] * 3 } |
|
738 |
} |
|
739 |
); |
|
740 |
$row = $result->one; |
|
741 |
is($row->{key1}, 6); |
|
742 |
is($row->{key2}, 2); |
|
clenup test
|
743 | |
744 |
$result = $dbi->select(table => 'table1'); |
|
cleanup test
|
745 |
$result->type_rule( |
746 |
from1 => { |
|
747 |
date => sub { $_[0] * 3 } |
|
748 |
} |
|
749 |
); |
|
750 |
$row = $result->one; |
|
751 |
is($row->{key1}, 6); |
|
752 |
is($row->{key2}, 2); |
|
clenup test
|
753 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
754 |
$result->type_rule( |
755 |
from1 => [date => sub { $_[0] * 3 }] |
|
756 |
); |
|
757 |
$row = $result->one; |
|
758 |
is($row->{key1}, 6); |
|
759 |
is($row->{key2}, 2); |
|
760 |
$dbi->register_filter(fivetimes => sub { $_[0] * 5}); |
|
clenup test
|
761 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
762 |
$result->type_rule( |
763 |
from1 => [date => 'fivetimes'] |
|
764 |
); |
|
765 |
$row = $result->one; |
|
766 |
is($row->{key1}, 10); |
|
767 |
is($row->{key2}, 2); |
|
clenup test
|
768 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
769 |
$result->type_rule( |
770 |
from1 => [date => undef] |
|
771 |
); |
|
772 |
$row = $result->one; |
|
773 |
is($row->{key1}, 2); |
|
774 |
is($row->{key2}, 2); |
|
clenup test
|
775 | |
test cleanup
|
776 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
777 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
778 |
$dbi->type_rule( |
|
779 |
from1 => { |
|
780 |
date => sub { $_[0] * 2 }, |
|
781 |
}, |
|
782 |
); |
|
783 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
clenup test
|
784 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
785 |
$result->filter(key1 => sub { $_[0] * 3 }); |
786 |
is($result->one->{key1}, 12); |
|
clenup test
|
787 | |
cleanup test
|
788 |
$dbi = DBIx::Custom->connect; |
789 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
790 |
$dbi->type_rule( |
|
791 |
from1 => { |
|
792 |
date => sub { $_[0] * 2 }, |
|
793 |
}, |
|
794 |
); |
|
795 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
clenup test
|
796 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
797 |
$result->filter(key1 => sub { $_[0] * 3 }); |
798 |
is($result->fetch->[0], 12); |
|
clenup test
|
799 | |
cleanup test
|
800 |
$dbi = DBIx::Custom->connect; |
801 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
802 |
$dbi->type_rule( |
|
803 |
into1 => { |
|
804 |
date => sub { $_[0] . 'b' } |
|
805 |
}, |
|
806 |
into2 => { |
|
807 |
date => sub { $_[0] . 'c' } |
|
808 |
}, |
|
809 |
from1 => { |
|
810 |
date => sub { $_[0] . 'd' } |
|
811 |
}, |
|
812 |
from2 => { |
|
813 |
date => sub { $_[0] . 'e' } |
|
814 |
} |
|
815 |
); |
|
816 |
$dbi->insert({key1 => '1'}, table => 'table1', type_rule_off => 1); |
|
clenup test
|
817 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
818 |
is($result->type_rule_off->fetch_first->[0], '1'); |
clenup test
|
819 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
820 |
is($result->type_rule_on->fetch_first->[0], '1de'); |
clenup test
|
821 | |
cleanup test
|
822 |
$dbi = DBIx::Custom->connect; |
823 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
824 |
$dbi->type_rule( |
|
825 |
into1 => { |
|
826 |
date => sub { $_[0] . 'b' } |
|
cleanup test
|
827 |
}, |
cleanup test
|
828 |
into2 => { |
829 |
date => sub { $_[0] . 'c' } |
|
cleanup test
|
830 |
}, |
cleanup test
|
831 |
from1 => { |
832 |
date => sub { $_[0] . 'd' } |
|
cleanup test
|
833 |
}, |
cleanup test
|
834 |
from2 => { |
835 |
date => sub { $_[0] . 'e' } |
|
cleanup test
|
836 |
} |
837 |
); |
|
cleanup test
|
838 |
$dbi->insert({key1 => '1'}, table => 'table1', type_rule1_off => 1); |
839 |
$result = $dbi->select(table => 'table1'); |
|
840 |
is($result->type_rule1_off->fetch_first->[0], '1ce'); |
|
841 |
$result = $dbi->select(table => 'table1'); |
|
842 |
is($result->type_rule1_on->fetch_first->[0], '1cde'); |
|
cleanup test
|
843 | |
cleanup test
|
844 |
$dbi = DBIx::Custom->connect; |
845 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
846 |
$dbi->type_rule( |
|
847 |
into1 => { |
|
848 |
date => sub { $_[0] . 'b' } |
|
849 |
}, |
|
850 |
into2 => { |
|
851 |
date => sub { $_[0] . 'c' } |
|
852 |
}, |
|
853 |
from1 => { |
|
854 |
date => sub { $_[0] . 'd' } |
|
855 |
}, |
|
856 |
from2 => { |
|
857 |
date => sub { $_[0] . 'e' } |
|
cleanup test
|
858 |
} |
859 |
); |
|
cleanup test
|
860 |
$dbi->insert({key1 => '1'}, table => 'table1', type_rule2_off => 1); |
861 |
$result = $dbi->select(table => 'table1'); |
|
862 |
is($result->type_rule2_off->fetch_first->[0], '1bd'); |
|
863 |
$result = $dbi->select(table => 'table1'); |
|
864 |
is($result->type_rule2_on->fetch_first->[0], '1bde'); |
|
test cleanup
|
865 | |
866 |
test 'prefix'; |
|
867 |
$dbi = DBIx::Custom->connect; |
|
868 |
eval { $dbi->execute('drop table table1') }; |
|
869 |
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))"); |
|
870 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
871 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}, prefix => 'or replace'); |
|
872 |
$result = $dbi->execute('select * from table1;'); |
|
873 |
$rows = $result->all; |
|
874 |
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic"); |
|
875 | ||
876 |
$dbi = DBIx::Custom->connect; |
|
877 |
eval { $dbi->execute('drop table table1') }; |
|
878 |
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))"); |
|
879 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
880 |
$dbi->update(table => 'table1', param => {key2 => 4}, |
|
881 |
where => {key1 => 1}, prefix => 'or replace'); |
|
882 |
$result = $dbi->execute('select * from table1;'); |
|
883 |
$rows = $result->all; |
|
884 |
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic"); |
|
885 | ||
test cleanup
|
886 |
test 'Model class'; |
887 |
use MyDBI1; |
|
888 |
$dbi = MyDBI1->connect; |
|
889 |
eval { $dbi->execute('drop table book') }; |
|
890 |
$dbi->execute("create table book (title, author)"); |
|
891 |
$model = $dbi->model('book'); |
|
892 |
$model->insert({title => 'a', author => 'b'}); |
|
893 |
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic'); |
|
894 |
$dbi->execute("create table company (name)"); |
|
895 |
$model = $dbi->model('company'); |
|
896 |
$model->insert({name => 'a'}); |
|
897 |
is_deeply($model->list->all, [{name => 'a'}], 'basic'); |
|
898 |
is($dbi->models->{'book'}, $dbi->model('book')); |
|
899 |
is($dbi->models->{'company'}, $dbi->model('company')); |
|
900 | ||
901 |
$dbi = MyDBI4->connect; |
|
902 |
eval { $dbi->execute('drop table book') }; |
|
903 |
$dbi->execute("create table book (title, author)"); |
|
904 |
$model = $dbi->model('book'); |
|
905 |
$model->insert({title => 'a', author => 'b'}); |
|
906 |
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic'); |
|
907 |
$dbi->execute("create table company (name)"); |
|
908 |
$model = $dbi->model('company'); |
|
909 |
$model->insert({name => 'a'}); |
|
910 |
is_deeply($model->list->all, [{name => 'a'}], 'basic'); |
|
test cleanup
|
911 | |
test cleanup
|
912 |
$dbi = MyDBI5->connect; |
913 |
eval { $dbi->execute('drop table company') }; |
|
914 |
eval { $dbi->execute('drop table table1') }; |
|
915 |
$dbi->execute("create table company (name)"); |
|
916 |
$dbi->execute("create table table1 (key1)"); |
|
917 |
$model = $dbi->model('company'); |
|
918 |
$model->insert({name => 'a'}); |
|
919 |
is_deeply($model->list->all, [{name => 'a'}], 'include all model'); |
|
920 |
$dbi->insert(table => 'table1', param => {key1 => 1}); |
|
921 |
$model = $dbi->model('book'); |
|
922 |
is_deeply($model->list->all, [{key1 => 1}], 'include all model'); |
|
test cleanup
|
923 | |
test cleanup
|
924 |
test 'primary_key'; |
925 |
use MyDBI1; |
|
926 |
$dbi = MyDBI1->connect; |
|
927 |
$model = $dbi->model('book'); |
|
928 |
$model->primary_key(['id', 'number']); |
|
929 |
is_deeply($model->primary_key, ['id', 'number']); |
|
test cleanup
|
930 | |
test cleanup
|
931 |
test 'columns'; |
932 |
use MyDBI1; |
|
933 |
$dbi = MyDBI1->connect; |
|
934 |
$model = $dbi->model('book'); |
|
935 |
$model->columns(['id', 'number']); |
|
936 |
is_deeply($model->columns, ['id', 'number']); |
|
test cleanup
|
937 | |
test cleanup
|
938 |
test 'setup_model'; |
939 |
use MyDBI1; |
|
940 |
$dbi = MyDBI1->connect; |
|
941 |
eval { $dbi->execute('drop table book') }; |
|
942 |
eval { $dbi->execute('drop table company') }; |
|
943 |
eval { $dbi->execute('drop table test') }; |
|
test cleanup
|
944 | |
test cleanup
|
945 |
$dbi->execute('create table book (id)'); |
946 |
$dbi->execute('create table company (id, name);'); |
|
947 |
$dbi->execute('create table test (id, name, primary key (id, name));'); |
|
948 |
$dbi->setup_model; |
|
949 |
is_deeply($dbi->model('book')->columns, ['id']); |
|
950 |
is_deeply($dbi->model('company')->columns, ['id', 'name']); |
|
test cleanup
|
951 | |
952 | ||
953 | ||
954 | ||
955 | ||
956 | ||
957 | ||
958 | ||
959 | ||
960 | ||
961 | ||
962 | ||
963 | ||
964 | ||
test cleanup
|
965 |
### SQLite only test |
966 |
test 'quote'; |
|
967 |
$dbi = DBIx::Custom->connect; |
|
968 |
$dbi->quote('"'); |
|
969 |
eval { $dbi->execute("drop table ${q}table$p") }; |
|
970 |
$dbi->execute($create_table_reserved); |
|
971 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
|
972 |
$dbi->insert(table => 'table', param => {select => 1}); |
|
973 |
$dbi->delete(table => 'table', where => {select => 1}); |
|
974 |
$result = $dbi->execute("select * from ${q}table$p"); |
|
975 |
$rows = $result->all; |
|
976 |
is_deeply($rows, [], "reserved word"); |
|
977 | ||
test cleanup
|
978 |
test 'finish statement handle'; |
979 |
$dbi = DBIx::Custom->connect; |
|
980 |
$dbi->execute($create_table1); |
|
981 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
|
982 |
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1'); |
|
983 | ||
984 |
$result = $dbi->select(table => 'table1'); |
|
985 |
$row = $result->fetch_first; |
|
986 |
is_deeply($row, [1, 2], "row"); |
|
987 |
$row = $result->fetch; |
|
988 |
ok(!$row, "finished"); |
|
989 | ||
990 |
$result = $dbi->select(table => 'table1'); |
|
991 |
$row = $result->fetch_hash_first; |
|
992 |
is_deeply($row, {key1 => 1, key2 => 2}, "row"); |
|
993 |
$row = $result->fetch_hash; |
|
994 |
ok(!$row, "finished"); |
|
995 | ||
996 |
$dbi->execute('create table table2 (key1, key2);'); |
|
997 |
$result = $dbi->select(table => 'table2'); |
|
998 |
$row = $result->fetch_hash_first; |
|
999 |
ok(!$row, "no row fetch"); |
|
1000 | ||
1001 |
$dbi = DBIx::Custom->connect; |
|
1002 |
eval { $dbi->execute('drop table table1') }; |
|
1003 |
$dbi->execute($create_table1); |
|
1004 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
|
1005 |
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1'); |
|
1006 |
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1'); |
|
1007 |
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1'); |
|
1008 |
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1'); |
|
1009 |
$result = $dbi->select(table => 'table1'); |
|
1010 |
$rows = $result->fetch_multi(2); |
|
1011 |
is_deeply($rows, [[1, 2], |
|
1012 |
[3, 4]], "fetch_multi first"); |
|
1013 |
$rows = $result->fetch_multi(2); |
|
1014 |
is_deeply($rows, [[5, 6], |
|
1015 |
[7, 8]], "fetch_multi secound"); |
|
1016 |
$rows = $result->fetch_multi(2); |
|
1017 |
is_deeply($rows, [[9, 10]], "fetch_multi third"); |
|
1018 |
$rows = $result->fetch_multi(2); |
|
1019 |
ok(!$rows); |
|
1020 | ||
1021 |
$result = $dbi->select(table => 'table1'); |
|
1022 |
eval {$result->fetch_multi}; |
|
1023 |
like($@, qr/Row count must be specified/, "Not specified row count"); |
|
1024 | ||
1025 |
$result = $dbi->select(table => 'table1'); |
|
1026 |
$rows = $result->fetch_hash_multi(2); |
|
1027 |
is_deeply($rows, [{key1 => 1, key2 => 2}, |
|
1028 |
{key1 => 3, key2 => 4}], "fetch_multi first"); |
|
1029 |
$rows = $result->fetch_hash_multi(2); |
|
1030 |
is_deeply($rows, [{key1 => 5, key2 => 6}, |
|
1031 |
{key1 => 7, key2 => 8}], "fetch_multi secound"); |
|
1032 |
$rows = $result->fetch_hash_multi(2); |
|
1033 |
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third"); |
|
1034 |
$rows = $result->fetch_hash_multi(2); |
|
1035 |
ok(!$rows); |
|
1036 | ||
1037 |
$result = $dbi->select(table => 'table1'); |
|
1038 |
eval {$result->fetch_hash_multi}; |
|
1039 |
like($@, qr/Row count must be specified/, "Not specified row count"); |
|
1040 | ||
test cleanup
|
1041 |
# DEPRECATED! test |
1042 |
test 'filter __ expression'; |
|
1043 |
$dbi = DBIx::Custom->connect; |
|
1044 |
eval { $dbi->execute('drop table company') }; |
|
1045 |
eval { $dbi->execute('drop table location') }; |
|
1046 |
$dbi->execute('create table company (id, name, location_id)'); |
|
1047 |
$dbi->execute('create table location (id, name)'); |
|
1048 |
$dbi->apply_filter('location', |
|
1049 |
name => {in => sub { uc $_[0] } } |
|
1050 |
); |
|
1051 | ||
1052 |
$dbi->insert(table => 'company', param => {id => 1, name => 'a', location_id => 2}); |
|
1053 |
$dbi->insert(table => 'location', param => {id => 2, name => 'b'}); |
|
1054 | ||
1055 |
$result = $dbi->select( |
|
1056 |
table => ['company', 'location'], relation => {'company.location_id' => 'location.id'}, |
|
1057 |
column => ['location.name as location__name'] |
|
1058 |
); |
|
1059 |
is($result->fetch_first->[0], 'B'); |
|
1060 | ||
1061 |
$result = $dbi->select( |
|
1062 |
table => 'company', relation => {'company.location_id' => 'location.id'}, |
|
1063 |
column => ['location.name as location__name'] |
|
1064 |
); |
|
1065 |
is($result->fetch_first->[0], 'B'); |
|
1066 | ||
1067 |
$result = $dbi->select( |
|
1068 |
table => 'company', relation => {'company.location_id' => 'location.id'}, |
|
1069 |
column => ['location.name as "location.name"'] |
|
1070 |
); |
|
1071 |
is($result->fetch_first->[0], 'B'); |
|
test cleanup
|
1072 | |
1073 |
test 'reserved_word_quote'; |
|
1074 |
$dbi = DBIx::Custom->connect; |
|
1075 |
eval { $dbi->execute("drop table ${q}table$p") }; |
|
1076 |
$dbi->reserved_word_quote('"'); |
|
1077 |
$dbi->execute($create_table_reserved); |
|
1078 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
|
1079 |
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}}); |
|
1080 |
$dbi->insert(table => 'table', param => {select => 1}); |
|
1081 |
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2}); |
|
1082 |
$result = $dbi->execute("select * from ${q}table$p"); |
|
1083 |
$rows = $result->all; |
|
1084 |
is_deeply($rows, [{select => 2, update => 6}], "reserved word"); |