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