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 char(255), key2 char(255));'; |
155 |
my $create_table1_2 = 'create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));'; |
|
test cleanup
|
156 |
my $create_table2 = 'create table table2 (key1 char(255), key3 char(255));'; |
cleanup test
|
157 |
my $create_table2_2 = "create table table2 (key1, key2, key3)"; |
158 |
my $create_table3 = "create table table3 (key1, key2, key3)"; |
|
test cleanup
|
159 |
my $create_table_reserved = 'create table "table" ("select", "update")'; |
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 | |
199 | ||
200 | ||
201 |
test 'update_param'; |
|
test cleanup
|
202 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
203 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
204 |
$dbi->execute($create_table1_2); |
cleanup test
|
205 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
206 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
207 | ||
208 |
$param = {key2 => 11}; |
|
209 |
$update_param = $dbi->update_param($param); |
|
210 |
$sql = <<"EOS"; |
|
211 |
update table1 $update_param |
|
212 |
where key1 = 1 |
|
213 |
EOS |
|
214 |
$dbi->execute($sql, param => $param); |
|
test cleanup
|
215 |
$result = $dbi->execute('select * from table1;', table => 'table1'); |
cleanup test
|
216 |
$rows = $result->all; |
217 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5}, |
|
218 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
219 |
"basic"); |
|
220 | ||
221 | ||
test cleanup
|
222 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
223 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
224 |
$dbi->execute($create_table1_2); |
cleanup test
|
225 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
226 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
227 | ||
228 |
$param = {key2 => 11, key3 => 33}; |
|
229 |
$update_param = $dbi->update_param($param); |
|
230 |
$sql = <<"EOS"; |
|
231 |
update table1 $update_param |
|
232 |
where key1 = 1 |
|
233 |
EOS |
|
234 |
$dbi->execute($sql, param => $param); |
|
test cleanup
|
235 |
$result = $dbi->execute('select * from table1;', table => 'table1'); |
cleanup test
|
236 |
$rows = $result->all; |
237 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5}, |
|
238 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
239 |
"basic"); |
|
240 | ||
test cleanup
|
241 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
242 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
243 |
$dbi->execute($create_table1_2); |
cleanup test
|
244 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
245 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
246 | ||
247 |
$param = {key2 => 11, key3 => 33}; |
|
248 |
$update_param = $dbi->update_param($param, {no_set => 1}); |
|
249 |
$sql = <<"EOS"; |
|
250 |
update table1 set $update_param |
|
251 |
where key1 = 1 |
|
252 |
EOS |
|
253 |
$dbi->execute($sql, param => $param); |
|
test cleanup
|
254 |
$result = $dbi->execute('select * from table1;', table => 'table1'); |
cleanup test
|
255 |
$rows = $result->all; |
256 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5}, |
|
257 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
258 |
"update param no_set"); |
|
259 | ||
260 |
|
|
261 |
eval { $dbi->update_param({";" => 1}) }; |
|
262 |
like($@, qr/not safety/); |
|
263 | ||
264 | ||
265 |
test 'update_param'; |
|
test cleanup
|
266 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
267 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
268 |
$dbi->execute($create_table1_2); |
cleanup test
|
269 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
270 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
271 | ||
272 |
$param = {key2 => 11}; |
|
273 |
$update_param = $dbi->assign_param($param); |
|
274 |
$sql = <<"EOS"; |
|
275 |
update table1 set $update_param |
|
276 |
where key1 = 1 |
|
277 |
EOS |
|
278 |
$dbi->execute($sql, param => $param, table => 'table1'); |
|
test cleanup
|
279 |
$result = $dbi->execute('select * from table1;'); |
cleanup test
|
280 |
$rows = $result->all; |
281 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5}, |
|
282 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
283 |
"basic"); |
|
284 | ||
285 | ||
286 |
test 'insert_param'; |
|
test cleanup
|
287 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
288 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
289 |
$dbi->execute($create_table1_2); |
cleanup test
|
290 |
$param = {key1 => 1, key2 => 2}; |
291 |
$insert_param = $dbi->insert_param($param); |
|
292 |
$sql = <<"EOS"; |
|
293 |
insert into table1 $insert_param |
|
294 |
EOS |
|
295 |
$dbi->execute($sql, param => $param, table => 'table1'); |
|
296 |
is($dbi->select(table => 'table1')->one->{key1}, 1); |
|
297 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
298 | ||
test cleanup
|
299 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
300 |
$dbi->quote('"'); |
cleanup test
|
301 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
302 |
$dbi->execute($create_table1_2); |
cleanup test
|
303 |
$param = {key1 => 1, key2 => 2}; |
304 |
$insert_param = $dbi->insert_param($param); |
|
305 |
$sql = <<"EOS"; |
|
306 |
insert into table1 $insert_param |
|
307 |
EOS |
|
308 |
$dbi->execute($sql, param => $param, table => 'table1'); |
|
309 |
is($dbi->select(table => 'table1')->one->{key1}, 1); |
|
310 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
311 | ||
312 |
eval { $dbi->insert_param({";" => 1}) }; |
|
313 |
like($@, qr/not safety/); |
|
314 | ||
cleanup test
|
315 | |
316 |
test 'join'; |
|
test cleanup
|
317 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
318 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
319 |
$dbi->execute($create_table1); |
cleanup test
|
320 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
321 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
test cleanup
|
322 |
$dbi->execute($create_table2); |
cleanup test
|
323 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
cleanup test
|
324 |
$dbi->execute('create table table3 (key3 int, key4 int);'); |
cleanup test
|
325 |
$dbi->insert(table => 'table3', param => {key3 => 5, key4 => 4}); |
326 |
$rows = $dbi->select( |
|
327 |
table => 'table1', |
|
328 |
column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3', |
|
329 |
where => {'table1.key2' => 2}, |
|
330 |
join => ['left outer join table2 on table1.key1 = table2.key1'] |
|
331 |
)->all; |
|
332 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}]); |
|
333 | ||
334 |
$rows = $dbi->select( |
|
335 |
table => 'table1', |
|
336 |
where => {'key1' => 1}, |
|
337 |
join => ['left outer join table2 on table1.key1 = table2.key1'] |
|
338 |
)->all; |
|
339 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
340 | ||
341 |
eval { |
|
342 |
$rows = $dbi->select( |
|
343 |
table => 'table1', |
|
344 |
column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3', |
|
345 |
where => {'table1.key2' => 2}, |
|
346 |
join => {'table1.key1' => 'table2.key1'} |
|
347 |
); |
|
348 |
}; |
|
349 |
like ($@, qr/array/); |
|
350 | ||
351 |
$rows = $dbi->select( |
|
352 |
table => 'table1', |
|
353 |
where => {'key1' => 1}, |
|
354 |
join => ['left outer join table2 on table1.key1 = table2.key1', |
|
355 |
'left outer join table3 on table2.key3 = table3.key3'] |
|
356 |
)->all; |
|
357 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
358 | ||
359 |
$rows = $dbi->select( |
|
360 |
column => 'table3.key4 as table3__key4', |
|
361 |
table => 'table1', |
|
362 |
where => {'table1.key1' => 1}, |
|
363 |
join => ['left outer join table2 on table1.key1 = table2.key1', |
|
364 |
'left outer join table3 on table2.key3 = table3.key3'] |
|
365 |
)->all; |
|
366 |
is_deeply($rows, [{table3__key4 => 4}]); |
|
367 | ||
368 |
$rows = $dbi->select( |
|
369 |
column => 'table1.key1 as table1__key1', |
|
370 |
table => 'table1', |
|
371 |
where => {'table3.key4' => 4}, |
|
372 |
join => ['left outer join table2 on table1.key1 = table2.key1', |
|
373 |
'left outer join table3 on table2.key3 = table3.key3'] |
|
374 |
)->all; |
|
375 |
is_deeply($rows, [{table1__key1 => 1}]); |
|
376 | ||
test cleanup
|
377 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
378 |
$dbi->quote('"'); |
cleanup test
|
379 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
380 |
$dbi->execute($create_table1); |
cleanup test
|
381 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
test cleanup
|
382 |
$dbi->execute($create_table2); |
cleanup test
|
383 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
384 |
$rows = $dbi->select( |
|
385 |
table => 'table1', |
|
386 |
column => '"table1"."key1" as "table1_key1", "table2"."key1" as "table2_key1", "key2", "key3"', |
|
387 |
where => {'table1.key2' => 2}, |
|
388 |
join => ['left outer join "table2" on "table1"."key1" = "table2"."key1"'], |
|
389 |
)->all; |
|
390 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], |
|
391 |
'quote'); |
|
392 | ||
cleanup test
|
393 | |
test cleanup
|
394 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
395 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
396 |
$dbi->execute($create_table1); |
cleanup test
|
397 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
398 |
$sql = <<"EOS"; |
|
cleanup test
|
399 |
left outer join ( |
400 |
select * from table1 as t1 |
|
401 |
where t1.key2 = ( |
|
402 |
select max(t2.key2) from table1 as t2 |
|
403 |
where t1.key1 = t2.key1 |
|
404 |
) |
|
405 |
) as latest_table1 on table1.key1 = latest_table1.key1 |
|
406 |
EOS |
|
cleanup test
|
407 |
$join = [$sql]; |
408 |
$rows = $dbi->select( |
|
409 |
table => 'table1', |
|
410 |
column => 'latest_table1.key1 as latest_table1__key1', |
|
411 |
join => $join |
|
412 |
)->all; |
|
413 |
is_deeply($rows, [{latest_table1__key1 => 1}]); |
|
414 | ||
test cleanup
|
415 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
416 |
eval { $dbi->execute('drop table table1') }; |
417 |
eval { $dbi->execute('drop table table2') }; |
|
cleanup test
|
418 |
$dbi->execute($create_table1); |
test cleanup
|
419 |
$dbi->execute($create_table2); |
cleanup test
|
420 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
421 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4}); |
|
422 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
|
423 |
$result = $dbi->select( |
|
424 |
table => 'table1', |
|
425 |
join => [ |
|
426 |
"left outer join table2 on table2.key2 = '4' and table1.key1 = table2.key1" |
|
427 |
] |
|
428 |
); |
|
429 |
is_deeply($result->all, [{key1 => 1, key2 => 2}]); |
|
430 |
$result = $dbi->select( |
|
431 |
table => 'table1', |
|
432 |
column => [{table2 => ['key3']}], |
|
433 |
join => [ |
|
434 |
"left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1" |
|
435 |
] |
|
436 |
); |
|
437 |
is_deeply($result->all, [{'table2.key3' => 4}]); |
|
438 |
$result = $dbi->select( |
|
439 |
table => 'table1', |
|
440 |
column => [{table2 => ['key3']}], |
|
441 |
join => [ |
|
442 |
"left outer join table2 on table1.key1 = table2.key1 and table2.key3 = '4'" |
|
443 |
] |
|
444 |
); |
|
445 |
is_deeply($result->all, [{'table2.key3' => 4}]); |
|
446 | ||
test cleanup
|
447 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
448 |
eval { $dbi->execute('drop table table1') }; |
449 |
eval { $dbi->execute('drop table table2') }; |
|
cleanup test
|
450 |
$dbi->execute($create_table1); |
test cleanup
|
451 |
$dbi->execute($create_table2); |
cleanup test
|
452 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
453 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4}); |
|
454 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
|
455 |
$result = $dbi->select( |
|
456 |
table => 'table1', |
|
457 |
column => [{table2 => ['key3']}], |
|
458 |
join => [ |
|
459 |
{ |
|
460 |
clause => "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1", |
|
461 |
table => ['table1', 'table2'] |
|
462 |
} |
|
463 |
] |
|
464 |
); |
|
465 |
is_deeply($result->all, [{'table2.key3' => 4}]); |
|
466 | ||
467 |
test 'mycolumn'; |
|
test cleanup
|
468 |
$dbi = MyDBI8->connect; |
cleanup test
|
469 |
eval { $dbi->execute('drop table table1') }; |
470 |
eval { $dbi->execute('drop table table2') }; |
|
cleanup test
|
471 |
$dbi->execute($create_table1); |
test cleanup
|
472 |
$dbi->execute($create_table2); |
cleanup test
|
473 |
$dbi->setup_model; |
474 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
475 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3}); |
|
476 |
$model = $dbi->model('table1'); |
|
477 |
$result = $model->select_at( |
|
478 |
column => [ |
|
479 |
$model->mycolumn, |
|
480 |
$model->column('table2') |
|
481 |
] |
|
482 |
); |
|
483 |
is_deeply($result->one, |
|
484 |
{key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3}); |
|
485 | ||
486 |
$result = $model->select_at( |
|
487 |
column => [ |
|
488 |
$model->mycolumn(['key1']), |
|
489 |
$model->column(table2 => ['key1']) |
|
490 |
] |
|
491 |
); |
|
492 |
is_deeply($result->one, |
|
493 |
{key1 => 1, 'table2.key1' => 1}); |
|
494 |
$result = $model->select_at( |
|
495 |
column => [ |
|
496 |
$model->mycolumn(['key1']), |
|
497 |
{table2 => ['key1']} |
|
498 |
] |
|
499 |
); |
|
500 |
is_deeply($result->one, |
|
501 |
{key1 => 1, 'table2.key1' => 1}); |
|
502 | ||
503 |
$result = $model->select_at( |
|
504 |
column => [ |
|
505 |
$model->mycolumn(['key1']), |
|
506 |
['table2.key1', as => 'table2.key1'] |
|
507 |
] |
|
508 |
); |
|
509 |
is_deeply($result->one, |
|
510 |
{key1 => 1, 'table2.key1' => 1}); |
|
511 | ||
512 |
$result = $model->select_at( |
|
513 |
column => [ |
|
514 |
$model->mycolumn(['key1']), |
|
515 |
['table2.key1' => 'table2.key1'] |
|
516 |
] |
|
517 |
); |
|
518 |
is_deeply($result->one, |
|
519 |
{key1 => 1, 'table2.key1' => 1}); |
|
520 | ||
521 |
test 'dbi method from model'; |
|
test cleanup
|
522 |
$dbi = MyDBI9->connect; |
cleanup test
|
523 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
524 |
$dbi->execute($create_table1); |
cleanup test
|
525 |
$model = $dbi->model('table1'); |
526 |
eval{$model->execute('select * from table1')}; |
|
527 |
ok(!$@); |
|
528 | ||
529 |
test 'column table option'; |
|
test cleanup
|
530 |
$dbi = MyDBI9->connect; |
cleanup test
|
531 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
532 |
$dbi->execute($create_table1); |
test cleanup
|
533 |
$dbi->execute($create_table2); |
cleanup test
|
534 |
$dbi->setup_model; |
535 |
$dbi->execute('insert into table1 (key1, key2) values (1, 2);'); |
|
536 |
$dbi->execute('insert into table2 (key1, key3) values (1, 4);'); |
|
537 |
$model = $dbi->model('table1'); |
|
538 |
$result = $model->select( |
|
539 |
column => [ |
|
540 |
$model->column('table2', {alias => 'table2_alias'}) |
|
541 |
], |
|
542 |
where => {'table2_alias.key3' => 4} |
|
543 |
); |
|
544 |
is_deeply($result->one, |
|
545 |
{'table2_alias.key1' => 1, 'table2_alias.key3' => 4}); |
|
546 | ||
547 |
$dbi->separator('__'); |
|
548 |
$result = $model->select( |
|
549 |
column => [ |
|
550 |
$model->column('table2', {alias => 'table2_alias'}) |
|
551 |
], |
|
552 |
where => {'table2_alias.key3' => 4} |
|
553 |
); |
|
554 |
is_deeply($result->one, |
|
555 |
{'table2_alias__key1' => 1, 'table2_alias__key3' => 4}); |
|
556 | ||
557 |
$dbi->separator('-'); |
|
558 |
$result = $model->select( |
|
559 |
column => [ |
|
560 |
$model->column('table2', {alias => 'table2_alias'}) |
|
561 |
], |
|
562 |
where => {'table2_alias.key3' => 4} |
|
563 |
); |
|
564 |
is_deeply($result->one, |
|
565 |
{'table2_alias-key1' => 1, 'table2_alias-key3' => 4}); |
|
566 | ||
567 |
test 'type option'; # DEPRECATED! |
|
568 |
$dbi = DBIx::Custom->connect( |
|
569 |
dbi_option => { |
|
570 |
$DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1) |
|
571 |
} |
|
572 |
); |
|
cleanup test
|
573 |
$binary = pack("I3", 1, 2, 3); |
574 |
eval { $dbi->execute('drop table table1') }; |
|
cleanup test
|
575 |
$dbi->execute('create table table1(key1, key2)'); |
576 |
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]); |
|
577 |
$result = $dbi->select(table => 'table1'); |
|
578 |
$row = $result->one; |
|
579 |
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic"); |
|
580 |
$result = $dbi->execute('select length(key1) as key1_length from table1'); |
|
581 |
$row = $result->one; |
|
582 |
is($row->{key1_length}, length $binary); |
|
583 | ||
584 |
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [['key1'] => DBI::SQL_BLOB]); |
|
585 |
$result = $dbi->select(table => 'table1'); |
|
586 |
$row = $result->one; |
|
587 |
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic"); |
|
588 |
$result = $dbi->execute('select length(key1) as key1_length from table1'); |
|
589 |
$row = $result->one; |
|
590 |
is($row->{key1_length}, length $binary); |
|
591 | ||
592 | ||
593 |
test 'bind_type option'; |
|
594 |
$dbi = DBIx::Custom->connect( |
|
595 |
dbi_option => { |
|
596 |
$DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1) |
|
597 |
} |
|
598 |
); |
|
599 |
$binary = pack("I3", 1, 2, 3); |
|
cleanup test
|
600 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
601 |
$dbi->execute('create table table1(key1, key2)'); |
602 |
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, bind_type => [key1 => DBI::SQL_BLOB]); |
|
603 |
$result = $dbi->select(table => 'table1'); |
|
604 |
$row = $result->one; |
|
605 |
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic"); |
|
606 |
$result = $dbi->execute('select length(key1) as key1_length from table1'); |
|
607 |
$row = $result->one; |
|
608 |
is($row->{key1_length}, length $binary); |
|
609 | ||
610 |
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, bind_type => [['key1'] => DBI::SQL_BLOB]); |
|
611 |
$result = $dbi->select(table => 'table1'); |
|
612 |
$row = $result->one; |
|
613 |
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic"); |
|
614 |
$result = $dbi->execute('select length(key1) as key1_length from table1'); |
|
615 |
$row = $result->one; |
|
616 |
is($row->{key1_length}, length $binary); |
|
617 | ||
618 |
test 'model type attribute'; |
|
619 |
$dbi = DBIx::Custom->connect( |
|
620 |
dbi_option => { |
|
621 |
$DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1) |
|
622 |
} |
|
623 |
); |
|
624 |
$binary = pack("I3", 1, 2, 3); |
|
cleanup test
|
625 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
626 |
$dbi->execute('create table table1(key1, key2)'); |
627 |
$model = $dbi->create_model(table => 'table1', bind_type => [key1 => DBI::SQL_BLOB]); |
|
628 |
$model->insert(param => {key1 => $binary, key2 => 'あ'}); |
|
629 |
$result = $dbi->select(table => 'table1'); |
|
630 |
$row = $result->one; |
|
631 |
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic"); |
|
632 |
$result = $dbi->execute('select length(key1) as key1_length from table1'); |
|
633 |
$row = $result->one; |
|
634 |
is($row->{key1_length}, length $binary); |
|
635 | ||
636 |
test 'create_model'; |
|
test cleanup
|
637 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
638 |
eval { $dbi->execute('drop table table1') }; |
639 |
eval { $dbi->execute('drop table table2') }; |
|
cleanup test
|
640 |
$dbi->execute($create_table1); |
test cleanup
|
641 |
$dbi->execute($create_table2); |
cleanup test
|
642 | |
643 |
$dbi->create_model( |
|
644 |
table => 'table1', |
|
645 |
join => [ |
|
646 |
'left outer join table2 on table1.key1 = table2.key1' |
|
647 |
], |
|
648 |
primary_key => ['key1'] |
|
649 |
); |
|
650 |
$model2 = $dbi->create_model( |
|
651 |
table => 'table2' |
|
652 |
); |
|
653 |
$dbi->create_model( |
|
654 |
table => 'table3', |
|
655 |
filter => [ |
|
656 |
key1 => {in => sub { uc $_[0] }} |
|
657 |
] |
|
658 |
); |
|
659 |
$dbi->setup_model; |
|
660 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
661 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3}); |
|
662 |
$model = $dbi->model('table1'); |
|
663 |
$result = $model->select( |
|
664 |
column => [$model->mycolumn, $model->column('table2')], |
|
665 |
where => {'table1.key1' => 1} |
|
666 |
); |
|
667 |
is_deeply($result->one, |
|
668 |
{key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3}); |
|
669 |
is_deeply($model2->select->one, {key1 => 1, key3 => 3}); |
|
670 | ||
671 |
test 'model method'; |
|
672 |
test 'create_model'; |
|
test cleanup
|
673 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
674 |
eval { $dbi->execute('drop table table2') }; |
test cleanup
|
675 |
$dbi->execute($create_table2); |
cleanup test
|
676 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3}); |
677 |
$model = $dbi->create_model( |
|
678 |
table => 'table2' |
|
679 |
); |
|
680 |
$model->method(foo => sub { shift->select(@_) }); |
|
681 |
is_deeply($model->foo->one, {key1 => 1, key3 => 3}); |
|
682 | ||
683 |
test 'merge_param'; |
|
cleanup test
|
684 |
$dbi = DBIx::Custom->new; |
685 |
$params = [ |
|
686 |
{key1 => 1, key2 => 2, key3 => 3}, |
|
687 |
{key1 => 1, key2 => 2}, |
|
688 |
{key1 => 1} |
|
689 |
]; |
|
690 |
$param = $dbi->merge_param($params->[0], $params->[1], $params->[2]); |
|
691 |
is_deeply($param, {key1 => [1, 1, 1], key2 => [2, 2], key3 => 3}); |
|
692 | ||
693 |
$params = [ |
|
694 |
{key1 => [1, 2], key2 => 1, key3 => [1, 2]}, |
|
695 |
{key1 => [3, 4], key2 => [2, 3], key3 => 3} |
|
696 |
]; |
|
697 |
$param = $dbi->merge_param($params->[0], $params->[1]); |
|
698 |
is_deeply($param, {key1 => [1, 2, 3, 4], key2 => [1, 2, 3], key3 => [1, 2, 3]}); |
|
cleanup test
|
699 | |
700 |
test 'select() param option'; |
|
test cleanup
|
701 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
702 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
703 |
$dbi->execute($create_table1); |
cleanup test
|
704 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
705 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3}); |
|
test cleanup
|
706 |
$dbi->execute($create_table2); |
cleanup test
|
707 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4}); |
708 |
$dbi->insert(table => 'table2', param => {key1 => 2, key3 => 5}); |
|
709 |
$rows = $dbi->select( |
|
710 |
table => 'table1', |
|
711 |
column => 'table1.key1 as table1_key1, key2, key3', |
|
712 |
where => {'table1.key2' => 3}, |
|
713 |
join => ['inner join (select * from table2 where {= table2.key3})' . |
|
714 |
' as table2 on table1.key1 = table2.key1'], |
|
715 |
param => {'table2.key3' => 5} |
|
716 |
)->all; |
|
717 |
is_deeply($rows, [{table1_key1 => 2, key2 => 3, key3 => 5}]); |
|
718 | ||
719 | ||
720 |
test 'select() wrap option'; |
|
test cleanup
|
721 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
722 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
723 |
$dbi->execute($create_table1); |
cleanup test
|
724 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
725 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3}); |
|
726 |
$rows = $dbi->select( |
|
727 |
table => 'table1', |
|
728 |
column => 'key1', |
|
729 |
wrap => ['select * from (', ') as t where key1 = 1'] |
|
730 |
)->all; |
|
731 |
is_deeply($rows, [{key1 => 1}]); |
|
732 | ||
733 |
eval { |
|
734 |
$dbi->select( |
|
735 |
table => 'table1', |
|
736 |
column => 'key1', |
|
737 |
wrap => 'select * from (' |
|
738 |
) |
|
739 |
}; |
|
740 |
like($@, qr/array/); |
|
741 | ||
742 |
test 'select() string where'; |
|
test cleanup
|
743 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
744 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
745 |
$dbi->execute($create_table1); |
cleanup test
|
746 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
747 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3}); |
|
748 |
$rows = $dbi->select( |
|
749 |
table => 'table1', |
|
750 |
where => 'key1 = :key1 and key2 = :key2', |
|
751 |
where_param => {key1 => 1, key2 => 2} |
|
752 |
)->all; |
|
753 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
754 | ||
test cleanup
|
755 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
756 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
757 |
$dbi->execute($create_table1); |
cleanup test
|
758 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
759 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3}); |
|
760 |
$rows = $dbi->select( |
|
761 |
table => 'table1', |
|
762 |
where => [ |
|
763 |
'key1 = :key1 and key2 = :key2', |
|
764 |
{key1 => 1, key2 => 2} |
|
765 |
] |
|
766 |
)->all; |
|
767 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
768 | ||
769 |
test 'delete() string where'; |
|
test cleanup
|
770 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
771 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
772 |
$dbi->execute($create_table1); |
cleanup test
|
773 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
774 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3}); |
|
775 |
$dbi->delete( |
|
776 |
table => 'table1', |
|
777 |
where => 'key1 = :key1 and key2 = :key2', |
|
778 |
where_param => {key1 => 1, key2 => 2} |
|
779 |
); |
|
780 |
$rows = $dbi->select(table => 'table1')->all; |
|
781 |
is_deeply($rows, [{key1 => 2, key2 => 3}]); |
|
782 | ||
test cleanup
|
783 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
784 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
785 |
$dbi->execute($create_table1); |
cleanup test
|
786 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
787 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3}); |
|
788 |
$dbi->delete( |
|
789 |
table => 'table1', |
|
790 |
where => [ |
|
791 |
'key1 = :key1 and key2 = :key2', |
|
792 |
{key1 => 1, key2 => 2} |
|
793 |
] |
|
794 |
); |
|
795 |
$rows = $dbi->select(table => 'table1')->all; |
|
796 |
is_deeply($rows, [{key1 => 2, key2 => 3}]); |
|
797 | ||
798 | ||
799 |
test 'update() string where'; |
|
test cleanup
|
800 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
801 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
802 |
$dbi->execute($create_table1); |
cleanup test
|
803 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
804 |
$dbi->update( |
|
805 |
table => 'table1', |
|
806 |
param => {key1 => 5}, |
|
807 |
where => 'key1 = :key1 and key2 = :key2', |
|
808 |
where_param => {key1 => 1, key2 => 2} |
|
809 |
); |
|
810 |
$rows = $dbi->select(table => 'table1')->all; |
|
811 |
is_deeply($rows, [{key1 => 5, key2 => 2}]); |
|
812 | ||
test cleanup
|
813 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
814 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
815 |
$dbi->execute($create_table1); |
cleanup test
|
816 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
817 |
$dbi->update( |
|
818 |
table => 'table1', |
|
819 |
param => {key1 => 5}, |
|
820 |
where => [ |
|
821 |
'key1 = :key1 and key2 = :key2', |
|
822 |
{key1 => 1, key2 => 2} |
|
823 |
] |
|
824 |
); |
|
825 |
$rows = $dbi->select(table => 'table1')->all; |
|
826 |
is_deeply($rows, [{key1 => 5, key2 => 2}]); |
|
827 | ||
828 |
test 'insert id and primary_key option'; |
|
test cleanup
|
829 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
830 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
831 |
$dbi->execute($create_table1_2); |
cleanup test
|
832 |
$dbi->insert( |
833 |
primary_key => ['key1', 'key2'], |
|
834 |
table => 'table1', |
|
835 |
id => [1, 2], |
|
836 |
param => {key3 => 3} |
|
837 |
); |
|
838 |
is($dbi->select(table => 'table1')->one->{key1}, 1); |
|
839 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
840 |
is($dbi->select(table => 'table1')->one->{key3}, 3); |
|
841 | ||
842 |
$dbi->delete_all(table => 'table1'); |
|
843 |
$dbi->insert( |
|
844 |
primary_key => 'key1', |
|
845 |
table => 'table1', |
|
846 |
id => 0, |
|
847 |
param => {key2 => 2, key3 => 3} |
|
848 |
); |
|
849 | ||
850 |
is($dbi->select(table => 'table1')->one->{key1}, 0); |
|
851 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
852 |
is($dbi->select(table => 'table1')->one->{key3}, 3); |
|
853 | ||
test cleanup
|
854 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
855 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
856 |
$dbi->execute($create_table1_2); |
cleanup test
|
857 |
$dbi->insert( |
858 |
{key3 => 3}, |
|
859 |
primary_key => ['key1', 'key2'], |
|
860 |
table => 'table1', |
|
861 |
id => [1, 2], |
|
862 |
); |
|
863 |
is($dbi->select(table => 'table1')->one->{key1}, 1); |
|
864 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
865 |
is($dbi->select(table => 'table1')->one->{key3}, 3); |
|
866 | ||
867 | ||
868 |
test 'model insert id and primary_key option'; |
|
test cleanup
|
869 |
$dbi = MyDBI6->connect; |
cleanup test
|
870 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
871 |
$dbi->execute($create_table1_2); |
cleanup test
|
872 |
$dbi->model('table1')->insert( |
873 |
id => [1, 2], |
|
874 |
param => {key3 => 3} |
|
875 |
); |
|
876 |
$result = $dbi->model('table1')->select; |
|
877 |
$row = $result->one; |
|
878 |
is($row->{key1}, 1); |
|
879 |
is($row->{key2}, 2); |
|
880 |
is($row->{key3}, 3); |
|
881 | ||
test cleanup
|
882 |
$dbi = MyDBI6->connect; |
cleanup test
|
883 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
884 |
$dbi->execute($create_table1_2); |
cleanup test
|
885 |
$dbi->model('table1')->insert( |
886 |
{key3 => 3}, |
|
887 |
id => [1, 2] |
|
888 |
); |
|
889 |
$result = $dbi->model('table1')->select; |
|
890 |
$row = $result->one; |
|
891 |
is($row->{key1}, 1); |
|
892 |
is($row->{key2}, 2); |
|
893 |
is($row->{key3}, 3); |
|
894 | ||
895 |
test 'update and id option'; |
|
test cleanup
|
896 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
897 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
898 |
$dbi->execute($create_table1_2); |
cleanup test
|
899 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
900 |
$dbi->update( |
|
901 |
table => 'table1', |
|
902 |
primary_key => ['key1', 'key2'], |
|
903 |
id => [1, 2], |
|
904 |
param => {key3 => 4} |
|
905 |
); |
|
906 |
is($dbi->select(table => 'table1')->one->{key1}, 1); |
|
907 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
908 |
is($dbi->select(table => 'table1')->one->{key3}, 4); |
|
909 | ||
910 |
$dbi->delete_all(table => 'table1'); |
|
911 |
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3}); |
|
912 |
$dbi->update( |
|
913 |
table => 'table1', |
|
914 |
primary_key => 'key1', |
|
915 |
id => 0, |
|
916 |
param => {key3 => 4} |
|
917 |
); |
|
918 |
is($dbi->select(table => 'table1')->one->{key1}, 0); |
|
919 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
920 |
is($dbi->select(table => 'table1')->one->{key3}, 4); |
|
921 | ||
test cleanup
|
922 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
923 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
924 |
$dbi->execute($create_table1_2); |
cleanup test
|
925 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
926 |
$dbi->update( |
|
927 |
{key3 => 4}, |
|
928 |
table => 'table1', |
|
929 |
primary_key => ['key1', 'key2'], |
|
930 |
id => [1, 2] |
|
931 |
); |
|
932 |
is($dbi->select(table => 'table1')->one->{key1}, 1); |
|
933 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
934 |
is($dbi->select(table => 'table1')->one->{key3}, 4); |
|
935 | ||
936 | ||
937 |
test 'model update and id option'; |
|
test cleanup
|
938 |
$dbi = MyDBI6->connect; |
cleanup test
|
939 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
940 |
$dbi->execute($create_table1_2); |
cleanup test
|
941 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
942 |
$dbi->model('table1')->update( |
|
943 |
id => [1, 2], |
|
944 |
param => {key3 => 4} |
|
945 |
); |
|
946 |
$result = $dbi->model('table1')->select; |
|
947 |
$row = $result->one; |
|
948 |
is($row->{key1}, 1); |
|
949 |
is($row->{key2}, 2); |
|
950 |
is($row->{key3}, 4); |
|
951 | ||
952 | ||
953 |
test 'delete and id option'; |
|
test cleanup
|
954 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
955 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
956 |
$dbi->execute($create_table1_2); |
cleanup test
|
957 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
958 |
$dbi->delete( |
|
959 |
table => 'table1', |
|
960 |
primary_key => ['key1', 'key2'], |
|
961 |
id => [1, 2], |
|
962 |
); |
|
963 |
is_deeply($dbi->select(table => 'table1')->all, []); |
|
964 | ||
965 |
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3}); |
|
966 |
$dbi->delete( |
|
967 |
table => 'table1', |
|
968 |
primary_key => 'key1', |
|
969 |
id => 0, |
|
970 |
); |
|
971 |
is_deeply($dbi->select(table => 'table1')->all, []); |
|
972 | ||
973 | ||
974 |
test 'model delete and id option'; |
|
test cleanup
|
975 |
$dbi = MyDBI6->connect; |
cleanup test
|
976 |
eval { $dbi->execute('drop table table1') }; |
977 |
eval { $dbi->execute('drop table table2') }; |
|
978 |
eval { $dbi->execute('drop table table3') }; |
|
cleanup test
|
979 |
$dbi->execute($create_table1_2); |
cleanup test
|
980 |
$dbi->execute($create_table2_2); |
981 |
$dbi->execute($create_table3); |
|
cleanup test
|
982 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
983 |
$dbi->model('table1')->delete(id => [1, 2]); |
|
984 |
is_deeply($dbi->select(table => 'table1')->all, []); |
|
985 |
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
986 |
$dbi->model('table1_1')->delete(id => [1, 2]); |
|
987 |
is_deeply($dbi->select(table => 'table1')->all, []); |
|
988 |
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
989 |
$dbi->model('table1_3')->delete(id => [1, 2]); |
|
990 |
is_deeply($dbi->select(table => 'table1')->all, []); |
|
991 | ||
992 | ||
993 |
test 'select and id option'; |
|
test cleanup
|
994 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
995 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
996 |
$dbi->execute($create_table1_2); |
cleanup test
|
997 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
998 |
$result = $dbi->select( |
|
999 |
table => 'table1', |
|
1000 |
primary_key => ['key1', 'key2'], |
|
1001 |
id => [1, 2] |
|
1002 |
); |
|
1003 |
$row = $result->one; |
|
1004 |
is($row->{key1}, 1); |
|
1005 |
is($row->{key2}, 2); |
|
1006 |
is($row->{key3}, 3); |
|
1007 | ||
1008 |
$dbi->delete_all(table => 'table1'); |
|
1009 |
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3}); |
|
1010 |
$result = $dbi->select( |
|
1011 |
table => 'table1', |
|
1012 |
primary_key => 'key1', |
|
1013 |
id => 0, |
|
1014 |
); |
|
1015 |
$row = $result->one; |
|
1016 |
is($row->{key1}, 0); |
|
1017 |
is($row->{key2}, 2); |
|
1018 |
is($row->{key3}, 3); |
|
1019 | ||
1020 |
$dbi->delete_all(table => 'table1'); |
|
1021 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
1022 |
$result = $dbi->select( |
|
1023 |
table => 'table1', |
|
1024 |
primary_key => ['key1', 'key2'], |
|
1025 |
id => [1, 2] |
|
1026 |
); |
|
1027 |
$row = $result->one; |
|
1028 |
is($row->{key1}, 1); |
|
1029 |
is($row->{key2}, 2); |
|
1030 |
is($row->{key3}, 3); |
|
1031 | ||
1032 | ||
1033 |
test 'model select_at'; |
|
test cleanup
|
1034 |
$dbi = MyDBI6->connect; |
cleanup test
|
1035 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
1036 |
$dbi->execute($create_table1_2); |
cleanup test
|
1037 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
1038 |
$result = $dbi->model('table1')->select(id => [1, 2]); |
|
1039 |
$row = $result->one; |
|
1040 |
is($row->{key1}, 1); |
|
1041 |
is($row->{key2}, 2); |
|
1042 |
is($row->{key3}, 3); |
|
1043 | ||
1044 |
test 'column separator is default .'; |
|
test cleanup
|
1045 |
$dbi = MyDBI7->connect; |
cleanup test
|
1046 |
eval { $dbi->execute('drop table table1') }; |
1047 |
eval { $dbi->execute('drop table table2') }; |
|
cleanup test
|
1048 |
$dbi->execute($create_table1); |
test cleanup
|
1049 |
$dbi->execute($create_table2); |
cleanup test
|
1050 |
$dbi->setup_model; |
1051 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1052 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3}); |
|
1053 |
$model = $dbi->model('table1'); |
|
1054 |
$result = $model->select( |
|
1055 |
column => [$model->column('table2')], |
|
1056 |
where => {'table1.key1' => 1} |
|
1057 |
); |
|
1058 |
is_deeply($result->one, |
|
1059 |
{'table2.key1' => 1, 'table2.key3' => 3}); |
|
1060 | ||
1061 |
$result = $model->select( |
|
1062 |
column => [$model->column('table2' => [qw/key1 key3/])], |
|
1063 |
where => {'table1.key1' => 1} |
|
1064 |
); |
|
1065 |
is_deeply($result->one, |
|
1066 |
{'table2.key1' => 1, 'table2.key3' => 3}); |
|
1067 | ||
1068 | ||
cleanup test
|
1069 | |
1070 |
test 'separator'; |
|
test cleanup
|
1071 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1072 |
eval { $dbi->execute('drop table table1') }; |
1073 |
eval { $dbi->execute('drop table table2') }; |
|
1074 |
$dbi->execute($create_table1); |
|
test cleanup
|
1075 |
$dbi->execute($create_table2); |
cleanup test
|
1076 | |
1077 |
$dbi->create_model( |
|
1078 |
table => 'table1', |
|
1079 |
join => [ |
|
1080 |
'left outer join table2 on table1.key1 = table2.key1' |
|
1081 |
], |
|
1082 |
primary_key => ['key1'], |
|
cleanup test
|
1083 |
); |
cleanup test
|
1084 |
$model2 = $dbi->create_model( |
1085 |
table => 'table2', |
|
1086 |
); |
|
1087 |
$dbi->setup_model; |
|
1088 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1089 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3}); |
|
1090 |
$model = $dbi->model('table1'); |
|
1091 |
$result = $model->select( |
|
1092 |
column => [ |
|
1093 |
$model->mycolumn, |
|
1094 |
{table2 => [qw/key1 key3/]} |
|
1095 |
], |
|
1096 |
where => {'table1.key1' => 1} |
|
1097 |
); |
|
1098 |
is_deeply($result->one, |
|
1099 |
{key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3}); |
|
1100 |
is_deeply($model2->select->one, {key1 => 1, key3 => 3}); |
|
cleanup test
|
1101 | |
cleanup test
|
1102 |
$dbi->separator('__'); |
1103 |
$model = $dbi->model('table1'); |
|
1104 |
$result = $model->select( |
|
1105 |
column => [ |
|
1106 |
$model->mycolumn, |
|
1107 |
{table2 => [qw/key1 key3/]} |
|
1108 |
], |
|
1109 |
where => {'table1.key1' => 1} |
|
1110 |
); |
|
1111 |
is_deeply($result->one, |
|
1112 |
{key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3}); |
|
1113 |
is_deeply($model2->select->one, {key1 => 1, key3 => 3}); |
|
cleanup test
|
1114 | |
cleanup test
|
1115 |
$dbi->separator('-'); |
1116 |
$model = $dbi->model('table1'); |
|
1117 |
$result = $model->select( |
|
1118 |
column => [ |
|
1119 |
$model->mycolumn, |
|
1120 |
{table2 => [qw/key1 key3/]} |
|
1121 |
], |
|
1122 |
where => {'table1.key1' => 1} |
|
1123 |
); |
|
1124 |
is_deeply($result->one, |
|
1125 |
{key1 => 1, key2 => 2, 'table2-key1' => 1, 'table2-key3' => 3}); |
|
1126 |
is_deeply($model2->select->one, {key1 => 1, key3 => 3}); |
|
cleanup test
|
1127 | |
cleanup test
|
1128 | |
1129 |
test 'filter_off'; |
|
test cleanup
|
1130 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1131 |
eval { $dbi->execute('drop table table1') }; |
1132 |
eval { $dbi->execute('drop table table2') }; |
|
1133 |
$dbi->execute($create_table1); |
|
test cleanup
|
1134 |
$dbi->execute($create_table2); |
cleanup test
|
1135 | |
1136 |
$dbi->create_model( |
|
1137 |
table => 'table1', |
|
1138 |
join => [ |
|
1139 |
'left outer join table2 on table1.key1 = table2.key1' |
|
1140 |
], |
|
1141 |
primary_key => ['key1'], |
|
cleanup test
|
1142 |
); |
cleanup test
|
1143 |
$dbi->setup_model; |
1144 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1145 |
$model = $dbi->model('table1'); |
|
1146 |
$result = $model->select(column => 'key1'); |
|
1147 |
$result->filter(key1 => sub { $_[0] * 2 }); |
|
1148 |
is_deeply($result->one, {key1 => 2}); |
|
cleanup test
|
1149 | |
cleanup test
|
1150 |
test 'available_datetype'; |
test cleanup
|
1151 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1152 |
ok($dbi->can('available_datatype')); |
1153 | ||
cleanup test
|
1154 | |
cleanup test
|
1155 |
test 'select prefix option'; |
test cleanup
|
1156 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1157 |
eval { $dbi->execute('drop table table1') }; |
1158 |
$dbi->execute($create_table1); |
|
1159 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1160 |
$rows = $dbi->select(prefix => 'key1,', column => 'key2', table => 'table1')->all; |
|
1161 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "table"); |
|
1162 | ||
1163 | ||
1164 |
test 'separator'; |
|
1165 |
$dbi = DBIx::Custom->connect; |
|
1166 |
is($dbi->separator, '.'); |
|
1167 |
$dbi->separator('-'); |
|
1168 |
is($dbi->separator, '-'); |
|
1169 |
$dbi->separator('__'); |
|
1170 |
is($dbi->separator, '__'); |
|
1171 |
eval { $dbi->separator('?') }; |
|
1172 |
like($@, qr/Separator/); |
|
1173 | ||
1174 | ||
1175 |
test 'map_param'; |
|
1176 |
$dbi = DBIx::Custom->connect; |
|
1177 |
$param = $dbi->map_param( |
|
1178 |
{id => 1, author => 'Ken', price => 1900}, |
|
1179 |
id => 'book.id', |
|
1180 |
author => ['book.author', sub { '%' . $_[0] . '%' }], |
|
1181 |
price => ['book.price', {if => sub { $_[0] eq 1900 }}] |
|
cleanup test
|
1182 |
); |
cleanup test
|
1183 |
is_deeply($param, {'book.id' => 1, 'book.author' => '%Ken%', |
1184 |
'book.price' => 1900}); |
|
1185 | ||
1186 |
$param = $dbi->map_param( |
|
1187 |
{id => 0, author => 0, price => 0}, |
|
1188 |
id => 'book.id', |
|
1189 |
author => ['book.author', sub { '%' . $_[0] . '%' }], |
|
1190 |
price => ['book.price', sub { '%' . $_[0] . '%' }, |
|
1191 |
{if => sub { $_[0] eq 0 }}] |
|
cleanup test
|
1192 |
); |
cleanup test
|
1193 |
is_deeply($param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'}); |
cleanup test
|
1194 | |
cleanup test
|
1195 |
$param = $dbi->map_param( |
1196 |
{id => '', author => '', price => ''}, |
|
1197 |
id => 'book.id', |
|
1198 |
author => ['book.author', sub { '%' . $_[0] . '%' }], |
|
1199 |
price => ['book.price', sub { '%' . $_[0] . '%' }, |
|
1200 |
{if => sub { $_[0] eq 1 }}] |
|
cleanup test
|
1201 |
); |
cleanup test
|
1202 |
is_deeply($param, {}); |
1203 | ||
1204 |
$param = $dbi->map_param( |
|
1205 |
{id => undef, author => undef, price => undef}, |
|
1206 |
id => 'book.id', |
|
1207 |
price => ['book.price', {if => 'exists'}] |
|
cleanup test
|
1208 |
); |
cleanup test
|
1209 |
is_deeply($param, {'book.price' => undef}); |
1210 | ||
1211 |
$param = $dbi->map_param( |
|
1212 |
{price => 'a'}, |
|
1213 |
id => ['book.id', {if => 'exists'}], |
|
1214 |
price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}] |
|
1215 |
); |
|
1216 |
is_deeply($param, {'book.price' => '%a'}); |
|
cleanup test
|
1217 | |
cleanup test
|
1218 | |
1219 |
test 'table_alias'; |
|
test cleanup
|
1220 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1221 |
eval { $dbi->execute('drop table table1') }; |
1222 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
cleanup test
|
1223 |
$dbi->type_rule( |
1224 |
into1 => { |
|
cleanup test
|
1225 |
date => sub { uc $_[0] } |
cleanup test
|
1226 |
} |
1227 |
); |
|
cleanup test
|
1228 |
$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => 'a'}, |
1229 |
table_alias => {table2 => 'table1'}); |
|
cleanup test
|
1230 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1231 |
is($result->one->{key1}, 'A'); |
cleanup test
|
1232 | |
1233 | ||
cleanup test
|
1234 |
test 'order'; |
test cleanup
|
1235 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1236 |
eval { $dbi->execute('drop table table1') }; |
1237 |
$dbi->execute("create table table1 (key1, key2)"); |
|
1238 |
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1'); |
|
1239 |
$dbi->insert({key1 => 1, key2 => 3}, table => 'table1'); |
|
1240 |
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1'); |
|
1241 |
$dbi->insert({key1 => 2, key2 => 4}, table => 'table1'); |
|
1242 |
my $order = $dbi->order; |
|
1243 |
$order->prepend('key1', 'key2 desc'); |
|
1244 |
$result = $dbi->select(table => 'table1', append => "$order"); |
|
1245 |
is_deeply($result->all, [{key1 => 1, key2 => 3}, {key1 => 1, key2 => 1}, |
|
1246 |
{key1 => 2, key2 => 4}, {key1 => 2, key2 => 2}]); |
|
1247 |
$order->prepend('key1 desc'); |
|
1248 |
$result = $dbi->select(table => 'table1', append => "$order"); |
|
1249 |
is_deeply($result->all, [{key1 => 2, key2 => 4}, {key1 => 2, key2 => 2}, |
|
1250 |
{key1 => 1, key2 => 3}, {key1 => 1, key2 => 1}]); |
|
cleanup test
|
1251 | |
cleanup test
|
1252 |
$order = $dbi->order; |
1253 |
$order->prepend(['table1-key1'], [qw/table1-key2 desc/]); |
|
1254 |
$result = $dbi->select(table => 'table1', |
|
1255 |
column => [[key1 => 'table1-key1'], [key2 => 'table1-key2']], |
|
1256 |
append => "$order"); |
|
1257 |
is_deeply($result->all, [{'table1-key1' => 1, 'table1-key2' => 3}, |
|
1258 |
{'table1-key1' => 1, 'table1-key2' => 1}, |
|
1259 |
{'table1-key1' => 2, 'table1-key2' => 4}, |
|
1260 |
{'table1-key1' => 2, 'table1-key2' => 2}]); |
|
cleanup test
|
1261 | |
cleanup test
|
1262 |
test 'tag_parse'; |
test cleanup
|
1263 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1264 |
$dbi->tag_parse(0); |
1265 |
eval { $dbi->execute('drop table table1') }; |
|
1266 |
$dbi->execute("create table table1 (key1, key2)"); |
|
1267 |
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1'); |
|
1268 |
eval {$dbi->execute("select * from table1 where {= key1}", {key1 => 1})}; |
|
1269 |
ok($@); |
|
cleanup test
|
1270 | |
cleanup test
|
1271 |
test 'last_sql'; |
test cleanup
|
1272 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1273 |
eval { $dbi->execute('drop table table1') }; |
1274 |
$dbi->execute("create table table1 (key1, key2)"); |
|
1275 |
$dbi->execute('select * from table1'); |
|
1276 |
is($dbi->last_sql, 'select * from table1;'); |
|
cleanup test
|
1277 | |
cleanup test
|
1278 |
eval{$dbi->execute("aaa")}; |
1279 |
is($dbi->last_sql, 'aaa;'); |
|
cleanup test
|
1280 | |
cleanup test
|
1281 |
test 'DBIx::Custom header'; |
test cleanup
|
1282 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1283 |
eval { $dbi->execute('drop table table1') }; |
1284 |
$dbi->execute("create table table1 (key1, key2)"); |
|
1285 |
$result = $dbi->execute('select key1 as h1, key2 as h2 from table1'); |
|
1286 |
is_deeply($result->header, [qw/h1 h2/]); |
|
cleanup test
|
1287 | |
cleanup test
|
1288 |
test 'Named placeholder :name(operater) syntax'; |
1289 |
$dbi->execute('drop table table1'); |
|
1290 |
$dbi->execute($create_table1_2); |
|
1291 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
1292 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
cleanup test
|
1293 | |
cleanup test
|
1294 |
$source = "select * from table1 where :key1{=} and :key2{=}"; |
1295 |
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2}); |
|
1296 |
$rows = $result->all; |
|
1297 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]); |
|
cleanup test
|
1298 | |
cleanup test
|
1299 |
$source = "select * from table1 where :key1{ = } and :key2{=}"; |
1300 |
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2}); |
|
1301 |
$rows = $result->all; |
|
1302 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]); |
|
cleanup test
|
1303 | |
cleanup test
|
1304 |
$source = "select * from table1 where :key1{<} and :key2{=}"; |
1305 |
$result = $dbi->execute($source, param => {key1 => 5, key2 => 2}); |
|
1306 |
$rows = $result->all; |
|
1307 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]); |
|
cleanup test
|
1308 | |
cleanup test
|
1309 |
$source = "select * from table1 where :table1.key1{=} and :table1.key2{=}"; |
1310 |
$result = $dbi->execute( |
|
1311 |
$source, |
|
1312 |
param => {'table1.key1' => 1, 'table1.key2' => 1}, |
|
1313 |
filter => {'table1.key2' => sub { $_[0] * 2 }} |
|
cleanup test
|
1314 |
); |
cleanup test
|
1315 |
$rows = $result->all; |
1316 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]); |
|
cleanup test
|
1317 | |
cleanup test
|
1318 |
test 'high perfomance way'; |
1319 |
$dbi->execute('drop table table1'); |
|
1320 |
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);"); |
|
1321 |
$rows = [ |
|
1322 |
{ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7}, |
|
1323 |
{ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8}, |
|
1324 |
]; |
|
1325 |
{ |
|
1326 |
my $query; |
|
1327 |
foreach my $row (@$rows) { |
|
1328 |
$query ||= $dbi->insert($row, table => 'table1', query => 1); |
|
1329 |
$dbi->execute($query, $row, filter => {ab => sub { $_[0] * 2 }}); |
|
cleanup test
|
1330 |
} |
cleanup test
|
1331 |
is_deeply($dbi->select(table => 'table1')->all, |
1332 |
[ |
|
1333 |
{ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7}, |
|
1334 |
{ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8}, |
|
1335 |
] |
|
1336 |
); |
|
1337 |
} |
|
1338 | ||
1339 |
$dbi->execute('drop table table1'); |
|
1340 |
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);"); |
|
1341 |
$rows = [ |
|
1342 |
{ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7}, |
|
1343 |
{ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8}, |
|
1344 |
]; |
|
1345 |
{ |
|
1346 |
my $query; |
|
1347 |
my $sth; |
|
1348 |
foreach my $row (@$rows) { |
|
1349 |
$query ||= $dbi->insert($row, table => 'table1', query => 1); |
|
1350 |
$sth ||= $query->sth; |
|
1351 |
$sth->execute(map { $row->{$_} } sort keys %$row); |
|
cleanup test
|
1352 |
} |
cleanup test
|
1353 |
is_deeply($dbi->select(table => 'table1')->all, |
1354 |
[ |
|
1355 |
{ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7}, |
|
1356 |
{ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8}, |
|
1357 |
] |
|
1358 |
); |
|
1359 |
} |
|
cleanup test
|
1360 | |
cleanup test
|
1361 |
test 'result'; |
test cleanup
|
1362 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1363 |
eval { $dbi->execute('drop table table1') }; |
1364 |
$dbi->execute($create_table1); |
|
1365 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
|
1366 |
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1'); |
|
1367 | ||
cleanup test
|
1368 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1369 |
@rows = (); |
1370 |
while (my $row = $result->fetch) { |
|
1371 |
push @rows, [@$row]; |
|
1372 |
} |
|
1373 |
is_deeply(\@rows, [[1, 2], [3, 4]]); |
|
cleanup test
|
1374 | |
1375 |
$result = $dbi->select(table => 'table1'); |
|
cleanup test
|
1376 |
@rows = (); |
1377 |
while (my $row = $result->fetch_hash) { |
|
1378 |
push @rows, {%$row}; |
|
1379 |
} |
|
1380 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
cleanup test
|
1381 | |
1382 |
$result = $dbi->select(table => 'table1'); |
|
cleanup test
|
1383 |
$row = $result->fetch_first; |
1384 |
is_deeply($row, [1, 2], "row"); |
|
1385 |
$row = $result->fetch; |
|
1386 |
ok(!$row, "finished"); |
|
1387 | ||
cleanup test
|
1388 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1389 |
$row = $result->fetch_hash_first; |
1390 |
is_deeply($row, {key1 => 1, key2 => 2}, "row"); |
|
1391 |
$row = $result->fetch_hash; |
|
1392 |
ok(!$row, "finished"); |
|
1393 | ||
1394 |
$dbi->execute('create table table2 (key1, key2);'); |
|
1395 |
$result = $dbi->select(table => 'table2'); |
|
1396 |
$row = $result->fetch_hash_first; |
|
1397 |
ok(!$row, "no row fetch"); |
|
cleanup test
|
1398 | |
test cleanup
|
1399 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1400 |
eval { $dbi->execute('drop table table1') }; |
1401 |
$dbi->execute($create_table1); |
|
1402 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
|
1403 |
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1'); |
|
1404 |
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1'); |
|
1405 |
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1'); |
|
1406 |
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1'); |
|
cleanup test
|
1407 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1408 |
$rows = $result->fetch_multi(2); |
1409 |
is_deeply($rows, [[1, 2], |
|
1410 |
[3, 4]], "fetch_multi first"); |
|
1411 |
$rows = $result->fetch_multi(2); |
|
1412 |
is_deeply($rows, [[5, 6], |
|
1413 |
[7, 8]], "fetch_multi secound"); |
|
1414 |
$rows = $result->fetch_multi(2); |
|
1415 |
is_deeply($rows, [[9, 10]], "fetch_multi third"); |
|
1416 |
$rows = $result->fetch_multi(2); |
|
1417 |
ok(!$rows); |
|
1418 | ||
cleanup test
|
1419 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1420 |
eval {$result->fetch_multi}; |
1421 |
like($@, qr/Row count must be specified/, "Not specified row count"); |
|
cleanup test
|
1422 | |
1423 |
$result = $dbi->select(table => 'table1'); |
|
cleanup test
|
1424 |
$rows = $result->fetch_hash_multi(2); |
1425 |
is_deeply($rows, [{key1 => 1, key2 => 2}, |
|
1426 |
{key1 => 3, key2 => 4}], "fetch_multi first"); |
|
1427 |
$rows = $result->fetch_hash_multi(2); |
|
1428 |
is_deeply($rows, [{key1 => 5, key2 => 6}, |
|
1429 |
{key1 => 7, key2 => 8}], "fetch_multi secound"); |
|
1430 |
$rows = $result->fetch_hash_multi(2); |
|
1431 |
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third"); |
|
1432 |
$rows = $result->fetch_hash_multi(2); |
|
1433 |
ok(!$rows); |
|
1434 | ||
cleanup test
|
1435 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1436 |
eval {$result->fetch_hash_multi}; |
1437 |
like($@, qr/Row count must be specified/, "Not specified row count"); |
|
cleanup test
|
1438 | |
test cleanup
|
1439 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1440 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
1441 |
$dbi->execute($create_table1); |
cleanup test
|
1442 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
1443 |
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1'); |
|
cleanup test
|
1444 | |
cleanup test
|
1445 |
test 'fetch_all'; |
1446 |
$result = $dbi->select(table => 'table1'); |
|
1447 |
$rows = $result->fetch_all; |
|
1448 |
is_deeply($rows, [[1, 2], [3, 4]]); |
|
cleanup test
|
1449 | |
cleanup test
|
1450 |
$result = $dbi->select(table => 'table1'); |
1451 |
$rows = $result->fetch_hash_all; |
|
1452 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
cleanup test
|
1453 | |
cleanup test
|
1454 |
$result = $dbi->select(table => 'table1'); |
1455 |
$result->dbi->filters({three_times => sub { $_[0] * 3}}); |
|
1456 |
$result->filter({key1 => 'three_times'}); |
|
cleanup test
|
1457 | |
cleanup test
|
1458 |
$rows = $result->fetch_all; |
1459 |
is_deeply($rows, [[3, 2], [9, 4]], "array"); |
|
cleanup test
|
1460 | |
cleanup test
|
1461 |
$result = $dbi->select(table => 'table1'); |
1462 |
$result->dbi->filters({three_times => sub { $_[0] * 3}}); |
|
1463 |
$result->filter({key1 => 'three_times'}); |
|
1464 |
$rows = $result->fetch_hash_all; |
|
1465 |
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "hash"); |
|
1466 | ||
1467 |
test "query_builder"; |
|
1468 |
$datas = [ |
|
1469 |
# Basic tests |
|
1470 |
{ name => 'placeholder basic', |
|
1471 |
source => "a {? k1} b {= k2} {<> k3} {> k4} {< k5} {>= k6} {<= k7} {like k8}", , |
|
1472 |
sql_expected => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?;", |
|
1473 |
columns_expected => [qw/k1 k2 k3 k4 k5 k6 k7 k8/] |
|
1474 |
}, |
|
1475 |
{ |
|
1476 |
name => 'placeholder in', |
|
1477 |
source => "{in k1 3};", |
|
1478 |
sql_expected => "k1 in (?, ?, ?);", |
|
1479 |
columns_expected => [qw/k1 k1 k1/] |
|
1480 |
}, |
|
1481 |
|
|
1482 |
# Table name |
|
1483 |
{ |
|
1484 |
name => 'placeholder with table name', |
|
1485 |
source => "{= a.k1} {= a.k2}", |
|
1486 |
sql_expected => "a.k1 = ? a.k2 = ?;", |
|
1487 |
columns_expected => [qw/a.k1 a.k2/] |
|
1488 |
}, |
|
1489 |
{ |
|
1490 |
name => 'placeholder in with table name', |
|
1491 |
source => "{in a.k1 2} {in b.k2 2}", |
|
1492 |
sql_expected => "a.k1 in (?, ?) b.k2 in (?, ?);", |
|
1493 |
columns_expected => [qw/a.k1 a.k1 b.k2 b.k2/] |
|
1494 |
}, |
|
1495 |
{ |
|
1496 |
name => 'not contain tag', |
|
1497 |
source => "aaa", |
|
1498 |
sql_expected => "aaa;", |
|
1499 |
columns_expected => [], |
|
1500 |
} |
|
1501 |
]; |
|
1502 | ||
1503 |
for (my $i = 0; $i < @$datas; $i++) { |
|
1504 |
my $data = $datas->[$i]; |
|
1505 |
my $builder = DBIx::Custom->new->query_builder; |
|
1506 |
my $query = $builder->build_query($data->{source}); |
|
1507 |
is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql"); |
|
1508 |
is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns"); |
|
1509 |
} |
|
1510 | ||
1511 |
$builder = DBIx::Custom->new->query_builder; |
|
1512 |
$ret_val = $builder->register_tag( |
|
1513 |
p => sub { |
|
1514 |
my @args = @_; |
|
1515 |
|
|
1516 |
my $expand = "? $args[0] $args[1]"; |
|
1517 |
my $columns = [2]; |
|
1518 |
return [$expand, $columns]; |
|
1519 |
} |
|
cleanup test
|
1520 |
); |
1521 | ||
cleanup test
|
1522 |
$query = $builder->build_query("{p a b}"); |
1523 |
is($query->{sql}, "? a b;", "register_tag sql"); |
|
1524 |
is_deeply($query->{columns}, [2], "register_tag columns"); |
|
1525 |
isa_ok($ret_val, 'DBIx::Custom::QueryBuilder'); |
|
cleanup test
|
1526 | |
cleanup test
|
1527 |
$builder = DBIx::Custom->new->query_builder; |
cleanup test
|
1528 | |
cleanup test
|
1529 |
eval{$builder->build_query('{? }')}; |
1530 |
like($@, qr/\QColumn name must be specified in tag "{? }"/, "? not arguments"); |
|
cleanup test
|
1531 | |
cleanup test
|
1532 |
eval{$builder->build_query("{a }")}; |
1533 |
like($@, qr/\QTag "a" is not registered/, "tag not exist"); |
|
cleanup test
|
1534 | |
cleanup test
|
1535 |
$builder->register_tag({ |
1536 |
q => 'string' |
|
1537 |
}); |
|
cleanup test
|
1538 | |
cleanup test
|
1539 |
eval{$builder->build_query("{q}", {})}; |
1540 |
like($@, qr/Tag "q" must be sub reference/, "tag not code ref"); |
|
cleanup test
|
1541 | |
cleanup test
|
1542 |
$builder->register_tag({ |
1543 |
r => sub {} |
|
1544 |
}); |
|
cleanup test
|
1545 | |
cleanup test
|
1546 |
eval{$builder->build_query("{r}")}; |
1547 |
like($@, qr/\QTag "r" must return [STRING, ARRAY_REFERENCE]/, "tag return noting"); |
|
1548 | ||
1549 |
$builder->register_tag({ |
|
1550 |
s => sub { return ["a", ""]} |
|
1551 |
}); |
|
1552 | ||
1553 |
eval{$builder->build_query("{s}")}; |
|
1554 |
like($@, qr/\QTag "s" must return [STRING, ARRAY_REFERENCE]/, "tag return not array columns"); |
|
1555 | ||
1556 |
$builder->register_tag( |
|
1557 |
t => sub {return ["a", []]} |
|
cleanup test
|
1558 |
); |
1559 | ||
cleanup test
|
1560 | |
1561 |
test 'General error case'; |
|
1562 |
$builder = DBIx::Custom->new->query_builder; |
|
1563 |
$builder->register_tag( |
|
1564 |
a => sub { |
|
1565 |
return ["? ? ?", ['']]; |
|
1566 |
} |
|
cleanup test
|
1567 |
); |
cleanup test
|
1568 |
eval{$builder->build_query("{a}")}; |
1569 |
like($@, qr/\QPlaceholder count/, "placeholder count is invalid"); |
|
cleanup test
|
1570 | |
cleanup test
|
1571 | |
1572 |
test 'Default tag Error case'; |
|
1573 |
eval{$builder->build_query("{= }")}; |
|
1574 |
like($@, qr/Column name must be specified in tag "{= }"/, "basic '=' : key not exist"); |
|
1575 | ||
1576 |
eval{$builder->build_query("{in }")}; |
|
1577 |
like($@, qr/Column name and count of values must be specified in tag "{in }"/, "in : key not exist"); |
|
1578 | ||
1579 |
eval{$builder->build_query("{in a}")}; |
|
1580 |
like($@, qr/\QColumn name and count of values must be specified in tag "{in }"/, |
|
1581 |
"in : key not exist"); |
|
1582 | ||
1583 |
eval{$builder->build_query("{in a r}")}; |
|
1584 |
like($@, qr/\QColumn name and count of values must be specified in tag "{in }"/, |
|
1585 |
"in : key not exist"); |
|
1586 | ||
1587 |
test 'variouse source'; |
|
1588 |
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d;"; |
|
1589 |
$query = $builder->build_query($source); |
|
1590 |
is($query->sql, 'a b = ? c { } { = ? } = ? d;', "basic : 1"); |
|
1591 | ||
1592 |
$source = "abc;"; |
|
1593 |
$query = $builder->build_query($source); |
|
1594 |
is($query->sql, 'abc;', "basic : 2"); |
|
1595 | ||
1596 |
$source = "{= a}"; |
|
1597 |
$query = $builder->build_query($source); |
|
1598 |
is($query->sql, 'a = ?;', "only tag"); |
|
1599 | ||
1600 |
$source = "000;"; |
|
1601 |
$query = $builder->build_query($source); |
|
1602 |
is($query->sql, '000;', "contain 0 value"); |
|
1603 | ||
1604 |
$source = "a {= b} }"; |
|
1605 |
eval{$builder->build_query($source)}; |
|
1606 |
like($@, qr/unexpected "}"/, "error : 1"); |
|
1607 | ||
1608 |
$source = "a {= {}"; |
|
1609 |
eval{$builder->build_query($source)}; |
|
1610 |
like($@, qr/unexpected "{"/, "error : 2"); |
|
1611 | ||
test cleanup
|
1612 | |
1613 | ||
1614 | ||
1615 | ||
1616 | ||
1617 | ||
1618 | ||
1619 | ||
1620 | ||
1621 | ||
1622 | ||
1623 | ||
1624 | ||
1625 | ||
1626 | ||
1627 | ||
1628 | ||
1629 | ||
1630 | ||
1631 | ||
1632 | ||
1633 | ||
1634 | ||
1635 | ||
1636 | ||
1637 | ||
1638 |
### a little complex test |
|
cleanup test
|
1639 |
test 'type option'; # DEPRECATED! |
1640 |
$dbi = DBIx::Custom->connect( |
|
1641 |
data_source => 'dbi:SQLite:dbname=:memory:', |
|
1642 |
dbi_option => { |
|
1643 |
$DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1) |
|
1644 |
} |
|
cleanup test
|
1645 |
); |
cleanup test
|
1646 |
$binary = pack("I3", 1, 2, 3); |
1647 |
eval { $dbi->execute('drop table table1') }; |
|
1648 |
$dbi->execute('create table table1(key1, key2)'); |
|
1649 |
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]); |
|
1650 |
$result = $dbi->select(table => 'table1'); |
|
1651 |
$row = $result->one; |
|
1652 |
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic"); |
|
1653 |
$result = $dbi->execute('select length(key1) as key1_length from table1'); |
|
1654 |
$row = $result->one; |
|
1655 |
is($row->{key1_length}, length $binary); |
|
cleanup test
|
1656 | |
cleanup test
|
1657 |
test 'type_rule from'; |
1658 |
$dbi = DBIx::Custom->connect; |
|
1659 |
$dbi->type_rule( |
|
1660 |
from1 => { |
|
1661 |
date => sub { uc $_[0] } |
|
1662 |
} |
|
cleanup test
|
1663 |
); |
cleanup test
|
1664 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
1665 |
$dbi->insert({key1 => 'a'}, table => 'table1'); |
|
1666 |
$result = $dbi->select(table => 'table1'); |
|
1667 |
is($result->fetch_first->[0], 'A'); |
|
cleanup test
|
1668 | |
cleanup test
|
1669 |
$result = $dbi->select(table => 'table1'); |
1670 |
is($result->one->{key1}, 'A'); |
|
cleanup test
|
1671 | |
cleanup test
|
1672 | |
1673 |
test 'type_rule into'; |
|
test cleanup
|
1674 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1675 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
1676 |
$dbi->type_rule( |
|
1677 |
into1 => { |
|
1678 |
date => sub { uc $_[0] } |
|
1679 |
} |
|
1680 |
); |
|
cleanup test
|
1681 |
$dbi->insert({key1 => 'a'}, table => 'table1'); |
cleanup test
|
1682 |
$result = $dbi->select(table => 'table1'); |
1683 |
is($result->one->{key1}, 'A'); |
|
1684 | ||
test cleanup
|
1685 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1686 |
$dbi->execute("create table table1 (key1 date, key2 datetime)"); |
1687 |
$dbi->type_rule( |
|
1688 |
into1 => [ |
|
1689 |
[qw/date datetime/] => sub { uc $_[0] } |
|
1690 |
] |
|
1691 |
); |
|
1692 |
$dbi->insert({key1 => 'a', key2 => 'b'}, table => 'table1'); |
|
1693 |
$result = $dbi->select(table => 'table1'); |
|
1694 |
$row = $result->one; |
|
1695 |
is($row->{key1}, 'A'); |
|
1696 |
is($row->{key2}, 'B'); |
|
cleanup test
|
1697 | |
test cleanup
|
1698 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1699 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
1700 |
$dbi->insert({key1 => 'a', key2 => 'B'}, table => 'table1'); |
|
1701 |
$dbi->type_rule( |
|
1702 |
into1 => [ |
|
1703 |
[qw/date datetime/] => sub { uc $_[0] } |
|
1704 |
] |
|
1705 |
); |
|
1706 |
$result = $dbi->execute( |
|
1707 |
"select * from table1 where key1 = :key1 and key2 = :table1.key2;", |
|
1708 |
param => {key1 => 'a', 'table1.key2' => 'b'} |
|
1709 |
); |
|
1710 |
$row = $result->one; |
|
1711 |
is($row->{key1}, 'a'); |
|
1712 |
is($row->{key2}, 'B'); |
|
cleanup test
|
1713 | |
test cleanup
|
1714 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1715 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
1716 |
$dbi->insert({key1 => 'A', key2 => 'B'}, table => 'table1'); |
|
1717 |
$dbi->type_rule( |
|
1718 |
into1 => [ |
|
1719 |
[qw/date datetime/] => sub { uc $_[0] } |
|
1720 |
] |
|
1721 |
); |
|
1722 |
$result = $dbi->execute( |
|
1723 |
"select * from table1 where key1 = :key1 and key2 = :table1.key2;", |
|
1724 |
param => {key1 => 'a', 'table1.key2' => 'b'}, |
|
1725 |
table => 'table1' |
|
1726 |
); |
|
1727 |
$row = $result->one; |
|
1728 |
is($row->{key1}, 'A'); |
|
1729 |
is($row->{key2}, 'B'); |
|
cleanup test
|
1730 | |
cleanup test
|
1731 |
$dbi = DBIx::Custom->connect; |
1732 |
$dbi->execute("create table table1 (key1 date, key2 datetime)"); |
|
1733 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
1734 |
$dbi->type_rule( |
|
1735 |
from1 => { |
|
1736 |
date => 'twice', |
|
1737 |
}, |
|
1738 |
into1 => { |
|
1739 |
date => 'twice', |
|
1740 |
} |
|
1741 |
); |
|
1742 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
1743 |
$result = $dbi->select(table => 'table1'); |
|
1744 |
is($result->fetch->[0], 8); |
|
cleanup test
|
1745 | |
cleanup test
|
1746 |
test 'type_rule and filter order'; |
test cleanup
|
1747 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1748 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
1749 |
$dbi->type_rule( |
|
1750 |
into1 => { |
|
1751 |
date => sub { $_[0] . 'b' } |
|
1752 |
}, |
|
1753 |
into2 => { |
|
1754 |
date => sub { $_[0] . 'c' } |
|
1755 |
}, |
|
1756 |
from1 => { |
|
1757 |
date => sub { $_[0] . 'd' } |
|
1758 |
}, |
|
1759 |
from2 => { |
|
1760 |
date => sub { $_[0] . 'e' } |
|
1761 |
} |
|
1762 |
); |
|
1763 |
$dbi->insert({key1 => '1'}, table => 'table1', filter => {key1 => sub { $_[0] . 'a' }}); |
|
1764 |
$result = $dbi->select(table => 'table1'); |
|
1765 |
$result->filter(key1 => sub { $_[0] . 'f' }); |
|
1766 |
is($result->fetch_first->[0], '1abcdef'); |
|
cleanup test
|
1767 | |
cleanup test
|
1768 |
$dbi = DBIx::Custom->connect; |
1769 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1770 |
$dbi->type_rule( |
|
1771 |
from1 => { |
|
1772 |
date => sub { $_[0] . 'p' } |
|
1773 |
}, |
|
1774 |
from2 => { |
|
1775 |
date => sub { $_[0] . 'q' } |
|
1776 |
}, |
|
1777 |
); |
|
1778 |
$dbi->insert({key1 => '1'}, table => 'table1'); |
|
1779 |
$result = $dbi->select(table => 'table1'); |
|
1780 |
$result->type_rule( |
|
1781 |
from1 => { |
|
1782 |
date => sub { $_[0] . 'd' } |
|
1783 |
}, |
|
1784 |
from2 => { |
|
1785 |
date => sub { $_[0] . 'e' } |
|
1786 |
} |
|
1787 |
); |
|
1788 |
$result->filter(key1 => sub { $_[0] . 'f' }); |
|
1789 |
is($result->fetch_first->[0], '1def'); |
|
cleanup test
|
1790 | |
cleanup test
|
1791 |
test 'type_rule_off'; |
1792 |
$dbi = DBIx::Custom->connect; |
|
1793 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1794 |
$dbi->type_rule( |
|
1795 |
from1 => { |
|
1796 |
date => sub { $_[0] * 2 }, |
|
1797 |
}, |
|
1798 |
into1 => { |
|
1799 |
date => sub { $_[0] * 2 }, |
|
1800 |
} |
|
1801 |
); |
|
1802 |
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1); |
|
1803 |
$result = $dbi->select(table => 'table1', type_rule_off => 1); |
|
1804 |
is($result->type_rule_off->fetch->[0], 2); |
|
cleanup test
|
1805 | |
cleanup test
|
1806 |
$dbi = DBIx::Custom->connect; |
1807 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1808 |
$dbi->type_rule( |
|
1809 |
from1 => { |
|
1810 |
date => sub { $_[0] * 2 }, |
|
1811 |
}, |
|
1812 |
into1 => { |
|
1813 |
date => sub { $_[0] * 3 }, |
|
1814 |
} |
|
1815 |
); |
|
1816 |
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1); |
|
1817 |
$result = $dbi->select(table => 'table1', type_rule_off => 1); |
|
1818 |
is($result->one->{key1}, 4); |
|
cleanup test
|
1819 | |
cleanup test
|
1820 |
$dbi = DBIx::Custom->connect; |
1821 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1822 |
$dbi->type_rule( |
|
1823 |
from1 => { |
|
1824 |
date => sub { $_[0] * 2 }, |
|
1825 |
}, |
|
1826 |
into1 => { |
|
1827 |
date => sub { $_[0] * 3 }, |
|
1828 |
} |
|
1829 |
); |
|
1830 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
1831 |
$result = $dbi->select(table => 'table1'); |
|
1832 |
is($result->one->{key1}, 12); |
|
cleanup test
|
1833 | |
cleanup test
|
1834 |
$dbi = DBIx::Custom->connect; |
1835 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1836 |
$dbi->type_rule( |
|
1837 |
from1 => { |
|
1838 |
date => sub { $_[0] * 2 }, |
|
1839 |
}, |
|
1840 |
into1 => { |
|
1841 |
date => sub { $_[0] * 3 }, |
|
1842 |
} |
|
cleanup test
|
1843 |
); |
cleanup test
|
1844 |
$dbi->insert({key1 => 2}, table => 'table1'); |
1845 |
$result = $dbi->select(table => 'table1'); |
|
1846 |
is($result->fetch->[0], 12); |
|
cleanup test
|
1847 | |
cleanup test
|
1848 |
$dbi = DBIx::Custom->connect; |
1849 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1850 |
$dbi->register_filter(ppp => sub { uc $_[0] }); |
|
1851 |
$dbi->type_rule( |
|
1852 |
into1 => { |
|
1853 |
date => 'ppp' |
|
cleanup test
|
1854 |
} |
cleanup test
|
1855 |
); |
1856 |
$dbi->insert({key1 => 'a'}, table => 'table1'); |
|
1857 |
$result = $dbi->select(table => 'table1'); |
|
1858 |
is($result->one->{key1}, 'A'); |
|
cleanup test
|
1859 | |
cleanup test
|
1860 |
eval{$dbi->type_rule( |
1861 |
into1 => { |
|
1862 |
date => 'pp' |
|
cleanup test
|
1863 |
} |
cleanup test
|
1864 |
)}; |
1865 |
like($@, qr/not registered/); |
|
cleanup test
|
1866 | |
test cleanup
|
1867 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1868 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
1869 |
eval { |
|
1870 |
$dbi->type_rule( |
|
1871 |
from1 => { |
|
1872 |
Date => sub { $_[0] * 2 }, |
|
1873 |
} |
|
1874 |
); |
|
1875 |
}; |
|
1876 |
like($@, qr/lower/); |
|
clenup test
|
1877 | |
cleanup test
|
1878 |
eval { |
1879 |
$dbi->type_rule( |
|
1880 |
into1 => { |
|
1881 |
Date => sub { $_[0] * 2 }, |
|
1882 |
} |
|
1883 |
); |
|
1884 |
}; |
|
1885 |
like($@, qr/lower/); |
|
clenup test
|
1886 | |
cleanup test
|
1887 |
$dbi = DBIx::Custom->connect; |
1888 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1889 |
$dbi->type_rule( |
|
1890 |
from1 => { |
|
1891 |
date => sub { $_[0] * 2 }, |
|
1892 |
}, |
|
1893 |
into1 => { |
|
1894 |
date => sub { $_[0] * 3 }, |
|
1895 |
} |
|
1896 |
); |
|
1897 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
clenup test
|
1898 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1899 |
$result->type_rule_off; |
1900 |
is($result->one->{key1}, 6); |
|
clenup test
|
1901 | |
cleanup test
|
1902 |
$dbi = DBIx::Custom->connect; |
1903 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1904 |
$dbi->type_rule( |
|
1905 |
from1 => { |
|
1906 |
date => sub { $_[0] * 2 }, |
|
1907 |
datetime => sub { $_[0] * 4 }, |
|
1908 |
}, |
|
1909 |
); |
|
1910 |
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1'); |
|
clenup test
|
1911 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1912 |
$result->type_rule( |
1913 |
from1 => { |
|
1914 |
date => sub { $_[0] * 3 } |
|
1915 |
} |
|
1916 |
); |
|
1917 |
$row = $result->one; |
|
1918 |
is($row->{key1}, 6); |
|
1919 |
is($row->{key2}, 2); |
|
clenup test
|
1920 | |
1921 |
$result = $dbi->select(table => 'table1'); |
|
cleanup test
|
1922 |
$result->type_rule( |
1923 |
from1 => { |
|
1924 |
date => sub { $_[0] * 3 } |
|
1925 |
} |
|
1926 |
); |
|
1927 |
$row = $result->one; |
|
1928 |
is($row->{key1}, 6); |
|
1929 |
is($row->{key2}, 2); |
|
clenup test
|
1930 | |
1931 |
$result = $dbi->select(table => 'table1'); |
|
cleanup test
|
1932 |
$result->type_rule( |
1933 |
from1 => { |
|
1934 |
date => sub { $_[0] * 3 } |
|
1935 |
} |
|
1936 |
); |
|
1937 |
$row = $result->one; |
|
1938 |
is($row->{key1}, 6); |
|
1939 |
is($row->{key2}, 2); |
|
clenup test
|
1940 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1941 |
$result->type_rule( |
1942 |
from1 => [date => sub { $_[0] * 3 }] |
|
1943 |
); |
|
1944 |
$row = $result->one; |
|
1945 |
is($row->{key1}, 6); |
|
1946 |
is($row->{key2}, 2); |
|
1947 |
$dbi->register_filter(fivetimes => sub { $_[0] * 5}); |
|
clenup test
|
1948 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1949 |
$result->type_rule( |
1950 |
from1 => [date => 'fivetimes'] |
|
1951 |
); |
|
1952 |
$row = $result->one; |
|
1953 |
is($row->{key1}, 10); |
|
1954 |
is($row->{key2}, 2); |
|
clenup test
|
1955 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1956 |
$result->type_rule( |
1957 |
from1 => [date => undef] |
|
1958 |
); |
|
1959 |
$row = $result->one; |
|
1960 |
is($row->{key1}, 2); |
|
1961 |
is($row->{key2}, 2); |
|
clenup test
|
1962 | |
test cleanup
|
1963 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1964 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
1965 |
$dbi->type_rule( |
|
1966 |
from1 => { |
|
1967 |
date => sub { $_[0] * 2 }, |
|
1968 |
}, |
|
1969 |
); |
|
1970 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
clenup test
|
1971 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1972 |
$result->filter(key1 => sub { $_[0] * 3 }); |
1973 |
is($result->one->{key1}, 12); |
|
clenup test
|
1974 | |
cleanup test
|
1975 |
$dbi = DBIx::Custom->connect; |
1976 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1977 |
$dbi->type_rule( |
|
1978 |
from1 => { |
|
1979 |
date => sub { $_[0] * 2 }, |
|
1980 |
}, |
|
1981 |
); |
|
1982 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
clenup test
|
1983 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1984 |
$result->filter(key1 => sub { $_[0] * 3 }); |
1985 |
is($result->fetch->[0], 12); |
|
clenup test
|
1986 | |
cleanup test
|
1987 |
$dbi = DBIx::Custom->connect; |
1988 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1989 |
$dbi->type_rule( |
|
1990 |
into1 => { |
|
1991 |
date => sub { $_[0] . 'b' } |
|
1992 |
}, |
|
1993 |
into2 => { |
|
1994 |
date => sub { $_[0] . 'c' } |
|
1995 |
}, |
|
1996 |
from1 => { |
|
1997 |
date => sub { $_[0] . 'd' } |
|
1998 |
}, |
|
1999 |
from2 => { |
|
2000 |
date => sub { $_[0] . 'e' } |
|
2001 |
} |
|
2002 |
); |
|
2003 |
$dbi->insert({key1 => '1'}, table => 'table1', type_rule_off => 1); |
|
clenup test
|
2004 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
2005 |
is($result->type_rule_off->fetch_first->[0], '1'); |
clenup test
|
2006 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
2007 |
is($result->type_rule_on->fetch_first->[0], '1de'); |
clenup test
|
2008 | |
cleanup test
|
2009 |
$dbi = DBIx::Custom->connect; |
2010 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
2011 |
$dbi->type_rule( |
|
2012 |
into1 => { |
|
2013 |
date => sub { $_[0] . 'b' } |
|
cleanup test
|
2014 |
}, |
cleanup test
|
2015 |
into2 => { |
2016 |
date => sub { $_[0] . 'c' } |
|
cleanup test
|
2017 |
}, |
cleanup test
|
2018 |
from1 => { |
2019 |
date => sub { $_[0] . 'd' } |
|
cleanup test
|
2020 |
}, |
cleanup test
|
2021 |
from2 => { |
2022 |
date => sub { $_[0] . 'e' } |
|
cleanup test
|
2023 |
} |
2024 |
); |
|
cleanup test
|
2025 |
$dbi->insert({key1 => '1'}, table => 'table1', type_rule1_off => 1); |
2026 |
$result = $dbi->select(table => 'table1'); |
|
2027 |
is($result->type_rule1_off->fetch_first->[0], '1ce'); |
|
2028 |
$result = $dbi->select(table => 'table1'); |
|
2029 |
is($result->type_rule1_on->fetch_first->[0], '1cde'); |
|
cleanup test
|
2030 | |
cleanup test
|
2031 |
$dbi = DBIx::Custom->connect; |
2032 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
2033 |
$dbi->type_rule( |
|
2034 |
into1 => { |
|
2035 |
date => sub { $_[0] . 'b' } |
|
2036 |
}, |
|
2037 |
into2 => { |
|
2038 |
date => sub { $_[0] . 'c' } |
|
2039 |
}, |
|
2040 |
from1 => { |
|
2041 |
date => sub { $_[0] . 'd' } |
|
2042 |
}, |
|
2043 |
from2 => { |
|
2044 |
date => sub { $_[0] . 'e' } |
|
cleanup test
|
2045 |
} |
2046 |
); |
|
cleanup test
|
2047 |
$dbi->insert({key1 => '1'}, table => 'table1', type_rule2_off => 1); |
2048 |
$result = $dbi->select(table => 'table1'); |
|
2049 |
is($result->type_rule2_off->fetch_first->[0], '1bd'); |
|
2050 |
$result = $dbi->select(table => 'table1'); |
|
2051 |
is($result->type_rule2_on->fetch_first->[0], '1bde'); |
|
test cleanup
|
2052 | |
2053 |
test 'prefix'; |
|
2054 |
$dbi = DBIx::Custom->connect; |
|
2055 |
eval { $dbi->execute('drop table table1') }; |
|
2056 |
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))"); |
|
2057 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
2058 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}, prefix => 'or replace'); |
|
2059 |
$result = $dbi->execute('select * from table1;'); |
|
2060 |
$rows = $result->all; |
|
2061 |
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic"); |
|
2062 | ||
2063 |
$dbi = DBIx::Custom->connect; |
|
2064 |
eval { $dbi->execute('drop table table1') }; |
|
2065 |
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))"); |
|
2066 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
2067 |
$dbi->update(table => 'table1', param => {key2 => 4}, |
|
2068 |
where => {key1 => 1}, prefix => 'or replace'); |
|
2069 |
$result = $dbi->execute('select * from table1;'); |
|
2070 |
$rows = $result->all; |
|
2071 |
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic"); |
|
2072 | ||
test cleanup
|
2073 |
test 'Model class'; |
2074 |
use MyDBI1; |
|
2075 |
$dbi = MyDBI1->connect; |
|
2076 |
eval { $dbi->execute('drop table book') }; |
|
2077 |
$dbi->execute("create table book (title, author)"); |
|
2078 |
$model = $dbi->model('book'); |
|
2079 |
$model->insert({title => 'a', author => 'b'}); |
|
2080 |
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic'); |
|
2081 |
$dbi->execute("create table company (name)"); |
|
2082 |
$model = $dbi->model('company'); |
|
2083 |
$model->insert({name => 'a'}); |
|
2084 |
is_deeply($model->list->all, [{name => 'a'}], 'basic'); |
|
2085 |
is($dbi->models->{'book'}, $dbi->model('book')); |
|
2086 |
is($dbi->models->{'company'}, $dbi->model('company')); |
|
2087 | ||
2088 |
$dbi = MyDBI4->connect; |
|
2089 |
eval { $dbi->execute('drop table book') }; |
|
2090 |
$dbi->execute("create table book (title, author)"); |
|
2091 |
$model = $dbi->model('book'); |
|
2092 |
$model->insert({title => 'a', author => 'b'}); |
|
2093 |
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic'); |
|
2094 |
$dbi->execute("create table company (name)"); |
|
2095 |
$model = $dbi->model('company'); |
|
2096 |
$model->insert({name => 'a'}); |
|
2097 |
is_deeply($model->list->all, [{name => 'a'}], 'basic'); |
|
test cleanup
|
2098 | |
test cleanup
|
2099 |
$dbi = MyDBI5->connect; |
2100 |
eval { $dbi->execute('drop table company') }; |
|
2101 |
eval { $dbi->execute('drop table table1') }; |
|
2102 |
$dbi->execute("create table company (name)"); |
|
2103 |
$dbi->execute("create table table1 (key1)"); |
|
2104 |
$model = $dbi->model('company'); |
|
2105 |
$model->insert({name => 'a'}); |
|
2106 |
is_deeply($model->list->all, [{name => 'a'}], 'include all model'); |
|
2107 |
$dbi->insert(table => 'table1', param => {key1 => 1}); |
|
2108 |
$model = $dbi->model('book'); |
|
2109 |
is_deeply($model->list->all, [{key1 => 1}], 'include all model'); |
|
test cleanup
|
2110 | |
test cleanup
|
2111 |
test 'primary_key'; |
2112 |
use MyDBI1; |
|
2113 |
$dbi = MyDBI1->connect; |
|
2114 |
$model = $dbi->model('book'); |
|
2115 |
$model->primary_key(['id', 'number']); |
|
2116 |
is_deeply($model->primary_key, ['id', 'number']); |
|
test cleanup
|
2117 | |
test cleanup
|
2118 |
test 'columns'; |
2119 |
use MyDBI1; |
|
2120 |
$dbi = MyDBI1->connect; |
|
2121 |
$model = $dbi->model('book'); |
|
2122 |
$model->columns(['id', 'number']); |
|
2123 |
is_deeply($model->columns, ['id', 'number']); |
|
test cleanup
|
2124 | |
test cleanup
|
2125 |
test 'setup_model'; |
2126 |
use MyDBI1; |
|
2127 |
$dbi = MyDBI1->connect; |
|
2128 |
eval { $dbi->execute('drop table book') }; |
|
2129 |
eval { $dbi->execute('drop table company') }; |
|
2130 |
eval { $dbi->execute('drop table test') }; |
|
test cleanup
|
2131 | |
test cleanup
|
2132 |
$dbi->execute('create table book (id)'); |
2133 |
$dbi->execute('create table company (id, name);'); |
|
2134 |
$dbi->execute('create table test (id, name, primary key (id, name));'); |
|
2135 |
$dbi->setup_model; |
|
2136 |
is_deeply($dbi->model('book')->columns, ['id']); |
|
2137 |
is_deeply($dbi->model('company')->columns, ['id', 'name']); |
|
test cleanup
|
2138 | |
2139 | ||
2140 | ||
2141 | ||
2142 | ||
2143 | ||
2144 | ||
2145 | ||
2146 | ||
2147 | ||
2148 | ||
2149 | ||
2150 | ||
2151 | ||
test cleanup
|
2152 |
### SQLite only test |
2153 |
test 'quote'; |
|
2154 |
$dbi = DBIx::Custom->connect; |
|
2155 |
$dbi->quote('"'); |
|
2156 |
eval { $dbi->execute("drop table ${q}table$p") }; |
|
2157 |
$dbi->execute($create_table_reserved); |
|
2158 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
|
2159 |
$dbi->insert(table => 'table', param => {select => 1}); |
|
2160 |
$dbi->delete(table => 'table', where => {select => 1}); |
|
2161 |
$result = $dbi->execute("select * from ${q}table$p"); |
|
2162 |
$rows = $result->all; |
|
2163 |
is_deeply($rows, [], "reserved word"); |
|
2164 | ||
2165 | ||
2166 | ||
2167 | ||
2168 | ||
2169 | ||
2170 | ||
test cleanup
|
2171 | |
2172 | ||
2173 | ||
2174 | ||
2175 | ||
2176 | ||
2177 | ||
2178 | ||
2179 | ||
2180 | ||
2181 |
# DEPRECATED! test |
|
2182 |
test 'filter __ expression'; |
|
2183 |
$dbi = DBIx::Custom->connect; |
|
2184 |
eval { $dbi->execute('drop table company') }; |
|
2185 |
eval { $dbi->execute('drop table location') }; |
|
2186 |
$dbi->execute('create table company (id, name, location_id)'); |
|
2187 |
$dbi->execute('create table location (id, name)'); |
|
2188 |
$dbi->apply_filter('location', |
|
2189 |
name => {in => sub { uc $_[0] } } |
|
2190 |
); |
|
2191 | ||
2192 |
$dbi->insert(table => 'company', param => {id => 1, name => 'a', location_id => 2}); |
|
2193 |
$dbi->insert(table => 'location', param => {id => 2, name => 'b'}); |
|
2194 | ||
2195 |
$result = $dbi->select( |
|
2196 |
table => ['company', 'location'], relation => {'company.location_id' => 'location.id'}, |
|
2197 |
column => ['location.name as location__name'] |
|
2198 |
); |
|
2199 |
is($result->fetch_first->[0], 'B'); |
|
2200 | ||
2201 |
$result = $dbi->select( |
|
2202 |
table => 'company', relation => {'company.location_id' => 'location.id'}, |
|
2203 |
column => ['location.name as location__name'] |
|
2204 |
); |
|
2205 |
is($result->fetch_first->[0], 'B'); |
|
2206 | ||
2207 |
$result = $dbi->select( |
|
2208 |
table => 'company', relation => {'company.location_id' => 'location.id'}, |
|
2209 |
column => ['location.name as "location.name"'] |
|
2210 |
); |
|
2211 |
is($result->fetch_first->[0], 'B'); |
|
test cleanup
|
2212 | |
2213 |
test 'reserved_word_quote'; |
|
2214 |
$dbi = DBIx::Custom->connect; |
|
2215 |
eval { $dbi->execute("drop table ${q}table$p") }; |
|
2216 |
$dbi->reserved_word_quote('"'); |
|
2217 |
$dbi->execute($create_table_reserved); |
|
2218 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
|
2219 |
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}}); |
|
2220 |
$dbi->insert(table => 'table', param => {select => 1}); |
|
2221 |
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2}); |
|
2222 |
$result = $dbi->execute("select * from ${q}table$p"); |
|
2223 |
$rows = $result->all; |
|
2224 |
is_deeply($rows, [{select => 2, update => 6}], "reserved word"); |