cleanup test
|
1 |
use Test::More; |
2 |
use strict; |
|
3 |
use warnings; |
|
4 |
use utf8; |
|
5 |
use Encode qw/encode_utf8 decode_utf8/; |
|
test cleanup
|
6 |
use FindBin; |
cleanup test
|
7 |
use lib "$FindBin::Bin/common"; |
cleanup test
|
8 | |
9 |
BEGIN { |
|
10 |
eval { require DBD::SQLite; 1 } |
|
11 |
or plan skip_all => 'DBD::SQLite required'; |
|
12 |
eval { DBD::SQLite->VERSION >= 1.25 } |
|
13 |
or plan skip_all => 'DBD::SQLite >= 1.25 required'; |
|
14 | ||
15 |
plan 'no_plan'; |
|
16 |
use_ok('DBIx::Custom'); |
|
17 |
} |
|
18 | ||
test cleanup
|
19 |
$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/}; |
cleanup test
|
20 |
sub test { print "# $_[0]\n" } |
21 | ||
test cleanup
|
22 |
use DBIx::Custom; |
test cleanup
|
23 |
{ |
24 |
package DBIx::Custom; |
|
25 |
has dsn => sub { 'dbi:SQLite:dbname=:memory:' } |
|
26 |
} |
|
test cleanup
|
27 |
use MyDBI1; |
28 |
{ |
|
29 |
package MyDBI4; |
|
30 | ||
31 |
use strict; |
|
32 |
use warnings; |
|
33 | ||
34 |
use base 'DBIx::Custom'; |
|
35 | ||
36 |
sub connect { |
|
37 |
my $self = shift->SUPER::connect(@_); |
|
38 |
|
|
39 |
$self->include_model( |
|
40 |
MyModel2 => [ |
|
41 |
'book', |
|
42 |
{class => 'Company', name => 'company'} |
|
43 |
] |
|
44 |
); |
|
45 |
} |
|
46 | ||
47 |
package MyModel2::Base1; |
|
48 | ||
49 |
use strict; |
|
50 |
use warnings; |
|
51 | ||
52 |
use base 'DBIx::Custom::Model'; |
|
53 | ||
54 |
package MyModel2::book; |
|
55 | ||
56 |
use strict; |
|
57 |
use warnings; |
|
58 | ||
59 |
use base 'MyModel2::Base1'; |
|
60 | ||
61 |
sub insert { |
|
62 |
my ($self, $param) = @_; |
|
63 |
|
|
64 |
return $self->SUPER::insert(param => $param); |
|
65 |
} |
|
66 | ||
67 |
sub list { shift->select; } |
|
68 | ||
69 |
package MyModel2::Company; |
|
70 | ||
71 |
use strict; |
|
72 |
use warnings; |
|
73 | ||
74 |
use base 'MyModel2::Base1'; |
|
75 | ||
76 |
sub insert { |
|
77 |
my ($self, $param) = @_; |
|
78 |
|
|
79 |
return $self->SUPER::insert(param => $param); |
|
80 |
} |
|
81 | ||
82 |
sub list { shift->select; } |
|
83 |
} |
|
84 |
{ |
|
85 |
package MyDBI5; |
|
86 | ||
87 |
use strict; |
|
88 |
use warnings; |
|
89 | ||
90 |
use base 'DBIx::Custom'; |
|
91 | ||
92 |
sub connect { |
|
93 |
my $self = shift->SUPER::connect(@_); |
|
94 |
|
|
95 |
$self->include_model('MyModel4'); |
|
96 |
} |
|
97 |
} |
|
98 |
{ |
|
99 |
package MyDBI6; |
|
100 |
|
|
101 |
use base 'DBIx::Custom'; |
|
102 |
|
|
103 |
sub connect { |
|
104 |
my $self = shift->SUPER::connect(@_); |
|
105 |
|
|
106 |
$self->include_model('MyModel5'); |
|
107 |
|
|
108 |
return $self; |
|
109 |
} |
|
110 |
} |
|
111 |
{ |
|
112 |
package MyDBI7; |
|
113 |
|
|
114 |
use base 'DBIx::Custom'; |
|
115 |
|
|
116 |
sub connect { |
|
117 |
my $self = shift->SUPER::connect(@_); |
|
118 |
|
|
119 |
$self->include_model('MyModel6'); |
|
120 |
|
|
121 |
|
|
122 |
return $self; |
|
123 |
} |
|
124 |
} |
|
125 |
{ |
|
126 |
package MyDBI8; |
|
127 |
|
|
128 |
use base 'DBIx::Custom'; |
|
129 |
|
|
130 |
sub connect { |
|
131 |
my $self = shift->SUPER::connect(@_); |
|
132 |
|
|
133 |
$self->include_model('MyModel7'); |
|
134 |
|
|
135 |
return $self; |
|
136 |
} |
|
137 |
} |
|
138 | ||
139 |
{ |
|
140 |
package MyDBI9; |
|
141 |
|
|
142 |
use base 'DBIx::Custom'; |
|
143 |
|
|
144 |
sub connect { |
|
145 |
my $self = shift->SUPER::connect(@_); |
|
146 |
|
|
147 |
$self->include_model('MyModel8')->setup_model; |
|
148 |
|
|
149 |
return $self; |
|
150 |
} |
|
151 |
} |
|
test cleanup
|
152 | |
cleanup test
|
153 |
# Constant |
cleanup test
|
154 |
my $create_table1 = 'create table table1 (key1 char(255), key2 char(255));'; |
155 |
my $create_table1_2 = 'create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));'; |
|
test cleanup
|
156 |
my $create_table2 = 'create table table2 (key1 char(255), key3 char(255));'; |
cleanup test
|
157 |
my $create_table2_2 = "create table table2 (key1, key2, key3)"; |
158 |
my $create_table3 = "create table table3 (key1, key2, key3)"; |
|
test cleanup
|
159 |
my $create_table_reserved = 'create table "table" ("select", "update")'; |
160 | ||
test cleanup
|
161 |
my $q = '"'; |
162 |
my $p = '"'; |
|
cleanup test
|
163 | |
cleanup test
|
164 |
# Variables |
cleanup test
|
165 |
my $builder; |
166 |
my $datas; |
|
cleanup test
|
167 |
my $dbi; |
168 |
my $sth; |
|
169 |
my $source; |
|
170 |
my @sources; |
|
cleanup test
|
171 |
my $select_source; |
172 |
my $insert_source; |
|
173 |
my $update_source; |
|
cleanup test
|
174 |
my $param; |
175 |
my $params; |
|
176 |
my $sql; |
|
177 |
my $result; |
|
178 |
my $row; |
|
179 |
my @rows; |
|
180 |
my $rows; |
|
181 |
my $query; |
|
182 |
my @queries; |
|
183 |
my $select_query; |
|
184 |
my $insert_query; |
|
185 |
my $update_query; |
|
186 |
my $ret_val; |
|
187 |
my $infos; |
|
188 |
my $model; |
|
189 |
my $model2; |
|
190 |
my $where; |
|
191 |
my $update_param; |
|
192 |
my $insert_param; |
|
cleanup test
|
193 |
my $join; |
cleanup test
|
194 |
my $binary; |
cleanup test
|
195 | |
196 |
# Prepare table |
|
test cleanup
|
197 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
198 | |
199 | ||
200 | ||
201 |
test 'model insert id and primary_key option'; |
|
test cleanup
|
202 |
$dbi = MyDBI6->connect; |
cleanup test
|
203 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
204 |
$dbi->execute($create_table1_2); |
cleanup test
|
205 |
$dbi->model('table1')->insert( |
206 |
id => [1, 2], |
|
207 |
param => {key3 => 3} |
|
208 |
); |
|
209 |
$result = $dbi->model('table1')->select; |
|
210 |
$row = $result->one; |
|
211 |
is($row->{key1}, 1); |
|
212 |
is($row->{key2}, 2); |
|
213 |
is($row->{key3}, 3); |
|
214 | ||
test cleanup
|
215 |
$dbi = MyDBI6->connect; |
cleanup test
|
216 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
217 |
$dbi->execute($create_table1_2); |
cleanup test
|
218 |
$dbi->model('table1')->insert( |
219 |
{key3 => 3}, |
|
220 |
id => [1, 2] |
|
221 |
); |
|
222 |
$result = $dbi->model('table1')->select; |
|
223 |
$row = $result->one; |
|
224 |
is($row->{key1}, 1); |
|
225 |
is($row->{key2}, 2); |
|
226 |
is($row->{key3}, 3); |
|
227 | ||
228 |
test 'update and id option'; |
|
test cleanup
|
229 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
230 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
231 |
$dbi->execute($create_table1_2); |
cleanup test
|
232 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
233 |
$dbi->update( |
|
234 |
table => 'table1', |
|
235 |
primary_key => ['key1', 'key2'], |
|
236 |
id => [1, 2], |
|
237 |
param => {key3 => 4} |
|
238 |
); |
|
239 |
is($dbi->select(table => 'table1')->one->{key1}, 1); |
|
240 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
241 |
is($dbi->select(table => 'table1')->one->{key3}, 4); |
|
242 | ||
243 |
$dbi->delete_all(table => 'table1'); |
|
244 |
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3}); |
|
245 |
$dbi->update( |
|
246 |
table => 'table1', |
|
247 |
primary_key => 'key1', |
|
248 |
id => 0, |
|
249 |
param => {key3 => 4} |
|
250 |
); |
|
251 |
is($dbi->select(table => 'table1')->one->{key1}, 0); |
|
252 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
253 |
is($dbi->select(table => 'table1')->one->{key3}, 4); |
|
254 | ||
test cleanup
|
255 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
256 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
257 |
$dbi->execute($create_table1_2); |
cleanup test
|
258 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
259 |
$dbi->update( |
|
260 |
{key3 => 4}, |
|
261 |
table => 'table1', |
|
262 |
primary_key => ['key1', 'key2'], |
|
263 |
id => [1, 2] |
|
264 |
); |
|
265 |
is($dbi->select(table => 'table1')->one->{key1}, 1); |
|
266 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
267 |
is($dbi->select(table => 'table1')->one->{key3}, 4); |
|
268 | ||
269 | ||
270 |
test 'model update and id option'; |
|
test cleanup
|
271 |
$dbi = MyDBI6->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->model('table1')->update( |
|
276 |
id => [1, 2], |
|
277 |
param => {key3 => 4} |
|
278 |
); |
|
279 |
$result = $dbi->model('table1')->select; |
|
280 |
$row = $result->one; |
|
281 |
is($row->{key1}, 1); |
|
282 |
is($row->{key2}, 2); |
|
283 |
is($row->{key3}, 4); |
|
284 | ||
285 | ||
286 |
test 'delete and id option'; |
|
test cleanup
|
287 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
288 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
289 |
$dbi->execute($create_table1_2); |
cleanup test
|
290 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
291 |
$dbi->delete( |
|
292 |
table => 'table1', |
|
293 |
primary_key => ['key1', 'key2'], |
|
294 |
id => [1, 2], |
|
295 |
); |
|
296 |
is_deeply($dbi->select(table => 'table1')->all, []); |
|
297 | ||
298 |
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3}); |
|
299 |
$dbi->delete( |
|
300 |
table => 'table1', |
|
301 |
primary_key => 'key1', |
|
302 |
id => 0, |
|
303 |
); |
|
304 |
is_deeply($dbi->select(table => 'table1')->all, []); |
|
305 | ||
306 | ||
307 |
test 'model delete and id option'; |
|
test cleanup
|
308 |
$dbi = MyDBI6->connect; |
cleanup test
|
309 |
eval { $dbi->execute('drop table table1') }; |
310 |
eval { $dbi->execute('drop table table2') }; |
|
311 |
eval { $dbi->execute('drop table table3') }; |
|
cleanup test
|
312 |
$dbi->execute($create_table1_2); |
cleanup test
|
313 |
$dbi->execute($create_table2_2); |
314 |
$dbi->execute($create_table3); |
|
cleanup test
|
315 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
316 |
$dbi->model('table1')->delete(id => [1, 2]); |
|
317 |
is_deeply($dbi->select(table => 'table1')->all, []); |
|
318 |
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
319 |
$dbi->model('table1_1')->delete(id => [1, 2]); |
|
320 |
is_deeply($dbi->select(table => 'table1')->all, []); |
|
321 |
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
322 |
$dbi->model('table1_3')->delete(id => [1, 2]); |
|
323 |
is_deeply($dbi->select(table => 'table1')->all, []); |
|
324 | ||
325 | ||
326 |
test 'select and id option'; |
|
test cleanup
|
327 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
328 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
329 |
$dbi->execute($create_table1_2); |
cleanup test
|
330 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
331 |
$result = $dbi->select( |
|
332 |
table => 'table1', |
|
333 |
primary_key => ['key1', 'key2'], |
|
334 |
id => [1, 2] |
|
335 |
); |
|
336 |
$row = $result->one; |
|
337 |
is($row->{key1}, 1); |
|
338 |
is($row->{key2}, 2); |
|
339 |
is($row->{key3}, 3); |
|
340 | ||
341 |
$dbi->delete_all(table => 'table1'); |
|
342 |
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3}); |
|
343 |
$result = $dbi->select( |
|
344 |
table => 'table1', |
|
345 |
primary_key => 'key1', |
|
346 |
id => 0, |
|
347 |
); |
|
348 |
$row = $result->one; |
|
349 |
is($row->{key1}, 0); |
|
350 |
is($row->{key2}, 2); |
|
351 |
is($row->{key3}, 3); |
|
352 | ||
353 |
$dbi->delete_all(table => 'table1'); |
|
354 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
355 |
$result = $dbi->select( |
|
356 |
table => 'table1', |
|
357 |
primary_key => ['key1', 'key2'], |
|
358 |
id => [1, 2] |
|
359 |
); |
|
360 |
$row = $result->one; |
|
361 |
is($row->{key1}, 1); |
|
362 |
is($row->{key2}, 2); |
|
363 |
is($row->{key3}, 3); |
|
364 | ||
365 | ||
366 |
test 'model select_at'; |
|
test cleanup
|
367 |
$dbi = MyDBI6->connect; |
cleanup test
|
368 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
369 |
$dbi->execute($create_table1_2); |
cleanup test
|
370 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
371 |
$result = $dbi->model('table1')->select(id => [1, 2]); |
|
372 |
$row = $result->one; |
|
373 |
is($row->{key1}, 1); |
|
374 |
is($row->{key2}, 2); |
|
375 |
is($row->{key3}, 3); |
|
376 | ||
377 |
test 'column separator is default .'; |
|
test cleanup
|
378 |
$dbi = MyDBI7->connect; |
cleanup test
|
379 |
eval { $dbi->execute('drop table table1') }; |
380 |
eval { $dbi->execute('drop table table2') }; |
|
cleanup test
|
381 |
$dbi->execute($create_table1); |
test cleanup
|
382 |
$dbi->execute($create_table2); |
cleanup test
|
383 |
$dbi->setup_model; |
384 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
385 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3}); |
|
386 |
$model = $dbi->model('table1'); |
|
387 |
$result = $model->select( |
|
388 |
column => [$model->column('table2')], |
|
389 |
where => {'table1.key1' => 1} |
|
390 |
); |
|
391 |
is_deeply($result->one, |
|
392 |
{'table2.key1' => 1, 'table2.key3' => 3}); |
|
393 | ||
394 |
$result = $model->select( |
|
395 |
column => [$model->column('table2' => [qw/key1 key3/])], |
|
396 |
where => {'table1.key1' => 1} |
|
397 |
); |
|
398 |
is_deeply($result->one, |
|
399 |
{'table2.key1' => 1, 'table2.key3' => 3}); |
|
400 | ||
401 | ||
cleanup test
|
402 | |
403 |
test 'separator'; |
|
test cleanup
|
404 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
405 |
eval { $dbi->execute('drop table table1') }; |
406 |
eval { $dbi->execute('drop table table2') }; |
|
407 |
$dbi->execute($create_table1); |
|
test cleanup
|
408 |
$dbi->execute($create_table2); |
cleanup test
|
409 | |
410 |
$dbi->create_model( |
|
411 |
table => 'table1', |
|
412 |
join => [ |
|
413 |
'left outer join table2 on table1.key1 = table2.key1' |
|
414 |
], |
|
415 |
primary_key => ['key1'], |
|
cleanup test
|
416 |
); |
cleanup test
|
417 |
$model2 = $dbi->create_model( |
418 |
table => 'table2', |
|
419 |
); |
|
420 |
$dbi->setup_model; |
|
421 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
422 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3}); |
|
423 |
$model = $dbi->model('table1'); |
|
424 |
$result = $model->select( |
|
425 |
column => [ |
|
426 |
$model->mycolumn, |
|
427 |
{table2 => [qw/key1 key3/]} |
|
428 |
], |
|
429 |
where => {'table1.key1' => 1} |
|
430 |
); |
|
431 |
is_deeply($result->one, |
|
432 |
{key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3}); |
|
433 |
is_deeply($model2->select->one, {key1 => 1, key3 => 3}); |
|
cleanup test
|
434 | |
cleanup test
|
435 |
$dbi->separator('__'); |
436 |
$model = $dbi->model('table1'); |
|
437 |
$result = $model->select( |
|
438 |
column => [ |
|
439 |
$model->mycolumn, |
|
440 |
{table2 => [qw/key1 key3/]} |
|
441 |
], |
|
442 |
where => {'table1.key1' => 1} |
|
443 |
); |
|
444 |
is_deeply($result->one, |
|
445 |
{key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3}); |
|
446 |
is_deeply($model2->select->one, {key1 => 1, key3 => 3}); |
|
cleanup test
|
447 | |
cleanup test
|
448 |
$dbi->separator('-'); |
449 |
$model = $dbi->model('table1'); |
|
450 |
$result = $model->select( |
|
451 |
column => [ |
|
452 |
$model->mycolumn, |
|
453 |
{table2 => [qw/key1 key3/]} |
|
454 |
], |
|
455 |
where => {'table1.key1' => 1} |
|
456 |
); |
|
457 |
is_deeply($result->one, |
|
458 |
{key1 => 1, key2 => 2, 'table2-key1' => 1, 'table2-key3' => 3}); |
|
459 |
is_deeply($model2->select->one, {key1 => 1, key3 => 3}); |
|
cleanup test
|
460 | |
cleanup test
|
461 | |
462 |
test 'filter_off'; |
|
test cleanup
|
463 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
464 |
eval { $dbi->execute('drop table table1') }; |
465 |
eval { $dbi->execute('drop table table2') }; |
|
466 |
$dbi->execute($create_table1); |
|
test cleanup
|
467 |
$dbi->execute($create_table2); |
cleanup test
|
468 | |
469 |
$dbi->create_model( |
|
470 |
table => 'table1', |
|
471 |
join => [ |
|
472 |
'left outer join table2 on table1.key1 = table2.key1' |
|
473 |
], |
|
474 |
primary_key => ['key1'], |
|
cleanup test
|
475 |
); |
cleanup test
|
476 |
$dbi->setup_model; |
477 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
478 |
$model = $dbi->model('table1'); |
|
479 |
$result = $model->select(column => 'key1'); |
|
480 |
$result->filter(key1 => sub { $_[0] * 2 }); |
|
481 |
is_deeply($result->one, {key1 => 2}); |
|
cleanup test
|
482 | |
cleanup test
|
483 |
test 'available_datetype'; |
test cleanup
|
484 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
485 |
ok($dbi->can('available_datatype')); |
486 | ||
cleanup test
|
487 | |
cleanup test
|
488 |
test 'select prefix option'; |
test cleanup
|
489 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
490 |
eval { $dbi->execute('drop table table1') }; |
491 |
$dbi->execute($create_table1); |
|
492 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
493 |
$rows = $dbi->select(prefix => 'key1,', column => 'key2', table => 'table1')->all; |
|
494 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "table"); |
|
495 | ||
496 |
test 'map_param'; |
|
497 |
$dbi = DBIx::Custom->connect; |
|
498 |
$param = $dbi->map_param( |
|
499 |
{id => 1, author => 'Ken', price => 1900}, |
|
500 |
id => 'book.id', |
|
501 |
author => ['book.author', sub { '%' . $_[0] . '%' }], |
|
502 |
price => ['book.price', {if => sub { $_[0] eq 1900 }}] |
|
cleanup test
|
503 |
); |
cleanup test
|
504 |
is_deeply($param, {'book.id' => 1, 'book.author' => '%Ken%', |
505 |
'book.price' => 1900}); |
|
506 | ||
507 |
$param = $dbi->map_param( |
|
508 |
{id => 0, author => 0, price => 0}, |
|
509 |
id => 'book.id', |
|
510 |
author => ['book.author', sub { '%' . $_[0] . '%' }], |
|
511 |
price => ['book.price', sub { '%' . $_[0] . '%' }, |
|
512 |
{if => sub { $_[0] eq 0 }}] |
|
cleanup test
|
513 |
); |
cleanup test
|
514 |
is_deeply($param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'}); |
cleanup test
|
515 | |
cleanup test
|
516 |
$param = $dbi->map_param( |
517 |
{id => '', author => '', price => ''}, |
|
518 |
id => 'book.id', |
|
519 |
author => ['book.author', sub { '%' . $_[0] . '%' }], |
|
520 |
price => ['book.price', sub { '%' . $_[0] . '%' }, |
|
521 |
{if => sub { $_[0] eq 1 }}] |
|
cleanup test
|
522 |
); |
cleanup test
|
523 |
is_deeply($param, {}); |
524 | ||
525 |
$param = $dbi->map_param( |
|
526 |
{id => undef, author => undef, price => undef}, |
|
527 |
id => 'book.id', |
|
528 |
price => ['book.price', {if => 'exists'}] |
|
cleanup test
|
529 |
); |
cleanup test
|
530 |
is_deeply($param, {'book.price' => undef}); |
531 | ||
532 |
$param = $dbi->map_param( |
|
533 |
{price => 'a'}, |
|
534 |
id => ['book.id', {if => 'exists'}], |
|
535 |
price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}] |
|
536 |
); |
|
537 |
is_deeply($param, {'book.price' => '%a'}); |
|
cleanup test
|
538 | |
cleanup test
|
539 | |
540 |
test 'table_alias'; |
|
test cleanup
|
541 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
542 |
eval { $dbi->execute('drop table table1') }; |
543 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
cleanup test
|
544 |
$dbi->type_rule( |
545 |
into1 => { |
|
cleanup test
|
546 |
date => sub { uc $_[0] } |
cleanup test
|
547 |
} |
548 |
); |
|
cleanup test
|
549 |
$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => 'a'}, |
550 |
table_alias => {table2 => 'table1'}); |
|
cleanup test
|
551 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
552 |
is($result->one->{key1}, 'A'); |
cleanup test
|
553 | |
554 | ||
cleanup test
|
555 |
test 'order'; |
test cleanup
|
556 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
557 |
eval { $dbi->execute('drop table table1') }; |
558 |
$dbi->execute("create table table1 (key1, key2)"); |
|
559 |
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1'); |
|
560 |
$dbi->insert({key1 => 1, key2 => 3}, table => 'table1'); |
|
561 |
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1'); |
|
562 |
$dbi->insert({key1 => 2, key2 => 4}, table => 'table1'); |
|
563 |
my $order = $dbi->order; |
|
564 |
$order->prepend('key1', 'key2 desc'); |
|
565 |
$result = $dbi->select(table => 'table1', append => "$order"); |
|
566 |
is_deeply($result->all, [{key1 => 1, key2 => 3}, {key1 => 1, key2 => 1}, |
|
567 |
{key1 => 2, key2 => 4}, {key1 => 2, key2 => 2}]); |
|
568 |
$order->prepend('key1 desc'); |
|
569 |
$result = $dbi->select(table => 'table1', append => "$order"); |
|
570 |
is_deeply($result->all, [{key1 => 2, key2 => 4}, {key1 => 2, key2 => 2}, |
|
571 |
{key1 => 1, key2 => 3}, {key1 => 1, key2 => 1}]); |
|
cleanup test
|
572 | |
cleanup test
|
573 |
$order = $dbi->order; |
574 |
$order->prepend(['table1-key1'], [qw/table1-key2 desc/]); |
|
575 |
$result = $dbi->select(table => 'table1', |
|
576 |
column => [[key1 => 'table1-key1'], [key2 => 'table1-key2']], |
|
577 |
append => "$order"); |
|
578 |
is_deeply($result->all, [{'table1-key1' => 1, 'table1-key2' => 3}, |
|
579 |
{'table1-key1' => 1, 'table1-key2' => 1}, |
|
580 |
{'table1-key1' => 2, 'table1-key2' => 4}, |
|
581 |
{'table1-key1' => 2, 'table1-key2' => 2}]); |
|
cleanup test
|
582 | |
cleanup test
|
583 |
test 'tag_parse'; |
test cleanup
|
584 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
585 |
$dbi->tag_parse(0); |
586 |
eval { $dbi->execute('drop table table1') }; |
|
587 |
$dbi->execute("create table table1 (key1, key2)"); |
|
588 |
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1'); |
|
589 |
eval {$dbi->execute("select * from table1 where {= key1}", {key1 => 1})}; |
|
590 |
ok($@); |
|
cleanup test
|
591 | |
cleanup test
|
592 |
test 'last_sql'; |
test cleanup
|
593 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
594 |
eval { $dbi->execute('drop table table1') }; |
595 |
$dbi->execute("create table table1 (key1, key2)"); |
|
596 |
$dbi->execute('select * from table1'); |
|
597 |
is($dbi->last_sql, 'select * from table1;'); |
|
cleanup test
|
598 | |
cleanup test
|
599 |
eval{$dbi->execute("aaa")}; |
600 |
is($dbi->last_sql, 'aaa;'); |
|
cleanup test
|
601 | |
cleanup test
|
602 |
test 'DBIx::Custom header'; |
test cleanup
|
603 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
604 |
eval { $dbi->execute('drop table table1') }; |
605 |
$dbi->execute("create table table1 (key1, key2)"); |
|
606 |
$result = $dbi->execute('select key1 as h1, key2 as h2 from table1'); |
|
607 |
is_deeply($result->header, [qw/h1 h2/]); |
|
cleanup test
|
608 | |
cleanup test
|
609 |
test 'Named placeholder :name(operater) syntax'; |
610 |
$dbi->execute('drop table table1'); |
|
611 |
$dbi->execute($create_table1_2); |
|
612 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
613 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
cleanup test
|
614 | |
cleanup test
|
615 |
$source = "select * from table1 where :key1{=} and :key2{=}"; |
616 |
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2}); |
|
617 |
$rows = $result->all; |
|
618 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]); |
|
cleanup test
|
619 | |
cleanup test
|
620 |
$source = "select * from table1 where :key1{ = } and :key2{=}"; |
621 |
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2}); |
|
622 |
$rows = $result->all; |
|
623 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]); |
|
cleanup test
|
624 | |
cleanup test
|
625 |
$source = "select * from table1 where :key1{<} and :key2{=}"; |
626 |
$result = $dbi->execute($source, param => {key1 => 5, key2 => 2}); |
|
627 |
$rows = $result->all; |
|
628 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]); |
|
cleanup test
|
629 | |
cleanup test
|
630 |
$source = "select * from table1 where :table1.key1{=} and :table1.key2{=}"; |
631 |
$result = $dbi->execute( |
|
632 |
$source, |
|
633 |
param => {'table1.key1' => 1, 'table1.key2' => 1}, |
|
634 |
filter => {'table1.key2' => sub { $_[0] * 2 }} |
|
cleanup test
|
635 |
); |
cleanup test
|
636 |
$rows = $result->all; |
637 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]); |
|
cleanup test
|
638 | |
cleanup test
|
639 |
test 'high perfomance way'; |
640 |
$dbi->execute('drop table table1'); |
|
641 |
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);"); |
|
642 |
$rows = [ |
|
643 |
{ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7}, |
|
644 |
{ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8}, |
|
645 |
]; |
|
646 |
{ |
|
647 |
my $query; |
|
648 |
foreach my $row (@$rows) { |
|
649 |
$query ||= $dbi->insert($row, table => 'table1', query => 1); |
|
650 |
$dbi->execute($query, $row, filter => {ab => sub { $_[0] * 2 }}); |
|
cleanup test
|
651 |
} |
cleanup test
|
652 |
is_deeply($dbi->select(table => 'table1')->all, |
653 |
[ |
|
654 |
{ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7}, |
|
655 |
{ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8}, |
|
656 |
] |
|
657 |
); |
|
658 |
} |
|
659 | ||
660 |
$dbi->execute('drop table table1'); |
|
661 |
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);"); |
|
662 |
$rows = [ |
|
663 |
{ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7}, |
|
664 |
{ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8}, |
|
665 |
]; |
|
666 |
{ |
|
667 |
my $query; |
|
668 |
my $sth; |
|
669 |
foreach my $row (@$rows) { |
|
670 |
$query ||= $dbi->insert($row, table => 'table1', query => 1); |
|
671 |
$sth ||= $query->sth; |
|
672 |
$sth->execute(map { $row->{$_} } sort keys %$row); |
|
cleanup test
|
673 |
} |
cleanup test
|
674 |
is_deeply($dbi->select(table => 'table1')->all, |
675 |
[ |
|
676 |
{ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7}, |
|
677 |
{ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8}, |
|
678 |
] |
|
679 |
); |
|
680 |
} |
|
cleanup test
|
681 | |
cleanup test
|
682 |
test 'result'; |
test cleanup
|
683 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
684 |
eval { $dbi->execute('drop table table1') }; |
685 |
$dbi->execute($create_table1); |
|
686 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
|
687 |
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1'); |
|
688 | ||
cleanup test
|
689 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
690 |
@rows = (); |
691 |
while (my $row = $result->fetch) { |
|
692 |
push @rows, [@$row]; |
|
693 |
} |
|
694 |
is_deeply(\@rows, [[1, 2], [3, 4]]); |
|
cleanup test
|
695 | |
696 |
$result = $dbi->select(table => 'table1'); |
|
cleanup test
|
697 |
@rows = (); |
698 |
while (my $row = $result->fetch_hash) { |
|
699 |
push @rows, {%$row}; |
|
700 |
} |
|
701 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
cleanup test
|
702 | |
703 |
$result = $dbi->select(table => 'table1'); |
|
cleanup test
|
704 |
$row = $result->fetch_first; |
705 |
is_deeply($row, [1, 2], "row"); |
|
706 |
$row = $result->fetch; |
|
707 |
ok(!$row, "finished"); |
|
708 | ||
cleanup test
|
709 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
710 |
$row = $result->fetch_hash_first; |
711 |
is_deeply($row, {key1 => 1, key2 => 2}, "row"); |
|
712 |
$row = $result->fetch_hash; |
|
713 |
ok(!$row, "finished"); |
|
714 | ||
715 |
$dbi->execute('create table table2 (key1, key2);'); |
|
716 |
$result = $dbi->select(table => 'table2'); |
|
717 |
$row = $result->fetch_hash_first; |
|
718 |
ok(!$row, "no row fetch"); |
|
cleanup test
|
719 | |
test cleanup
|
720 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
721 |
eval { $dbi->execute('drop table table1') }; |
722 |
$dbi->execute($create_table1); |
|
723 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
|
724 |
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1'); |
|
725 |
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1'); |
|
726 |
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1'); |
|
727 |
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1'); |
|
cleanup test
|
728 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
729 |
$rows = $result->fetch_multi(2); |
730 |
is_deeply($rows, [[1, 2], |
|
731 |
[3, 4]], "fetch_multi first"); |
|
732 |
$rows = $result->fetch_multi(2); |
|
733 |
is_deeply($rows, [[5, 6], |
|
734 |
[7, 8]], "fetch_multi secound"); |
|
735 |
$rows = $result->fetch_multi(2); |
|
736 |
is_deeply($rows, [[9, 10]], "fetch_multi third"); |
|
737 |
$rows = $result->fetch_multi(2); |
|
738 |
ok(!$rows); |
|
739 | ||
cleanup test
|
740 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
741 |
eval {$result->fetch_multi}; |
742 |
like($@, qr/Row count must be specified/, "Not specified row count"); |
|
cleanup test
|
743 | |
744 |
$result = $dbi->select(table => 'table1'); |
|
cleanup test
|
745 |
$rows = $result->fetch_hash_multi(2); |
746 |
is_deeply($rows, [{key1 => 1, key2 => 2}, |
|
747 |
{key1 => 3, key2 => 4}], "fetch_multi first"); |
|
748 |
$rows = $result->fetch_hash_multi(2); |
|
749 |
is_deeply($rows, [{key1 => 5, key2 => 6}, |
|
750 |
{key1 => 7, key2 => 8}], "fetch_multi secound"); |
|
751 |
$rows = $result->fetch_hash_multi(2); |
|
752 |
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third"); |
|
753 |
$rows = $result->fetch_hash_multi(2); |
|
754 |
ok(!$rows); |
|
755 | ||
cleanup test
|
756 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
757 |
eval {$result->fetch_hash_multi}; |
758 |
like($@, qr/Row count must be specified/, "Not specified row count"); |
|
cleanup test
|
759 | |
test cleanup
|
760 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
761 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
762 |
$dbi->execute($create_table1); |
cleanup test
|
763 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
764 |
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1'); |
|
cleanup test
|
765 | |
cleanup test
|
766 |
test 'fetch_all'; |
767 |
$result = $dbi->select(table => 'table1'); |
|
768 |
$rows = $result->fetch_all; |
|
769 |
is_deeply($rows, [[1, 2], [3, 4]]); |
|
cleanup test
|
770 | |
cleanup test
|
771 |
$result = $dbi->select(table => 'table1'); |
772 |
$rows = $result->fetch_hash_all; |
|
773 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
cleanup test
|
774 | |
cleanup test
|
775 |
$result = $dbi->select(table => 'table1'); |
776 |
$result->dbi->filters({three_times => sub { $_[0] * 3}}); |
|
777 |
$result->filter({key1 => 'three_times'}); |
|
cleanup test
|
778 | |
cleanup test
|
779 |
$rows = $result->fetch_all; |
780 |
is_deeply($rows, [[3, 2], [9, 4]], "array"); |
|
cleanup test
|
781 | |
cleanup test
|
782 |
$result = $dbi->select(table => 'table1'); |
783 |
$result->dbi->filters({three_times => sub { $_[0] * 3}}); |
|
784 |
$result->filter({key1 => 'three_times'}); |
|
785 |
$rows = $result->fetch_hash_all; |
|
786 |
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "hash"); |
|
787 | ||
788 |
test "query_builder"; |
|
789 |
$datas = [ |
|
790 |
# Basic tests |
|
791 |
{ name => 'placeholder basic', |
|
792 |
source => "a {? k1} b {= k2} {<> k3} {> k4} {< k5} {>= k6} {<= k7} {like k8}", , |
|
793 |
sql_expected => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?;", |
|
794 |
columns_expected => [qw/k1 k2 k3 k4 k5 k6 k7 k8/] |
|
795 |
}, |
|
796 |
{ |
|
797 |
name => 'placeholder in', |
|
798 |
source => "{in k1 3};", |
|
799 |
sql_expected => "k1 in (?, ?, ?);", |
|
800 |
columns_expected => [qw/k1 k1 k1/] |
|
801 |
}, |
|
802 |
|
|
803 |
# Table name |
|
804 |
{ |
|
805 |
name => 'placeholder with table name', |
|
806 |
source => "{= a.k1} {= a.k2}", |
|
807 |
sql_expected => "a.k1 = ? a.k2 = ?;", |
|
808 |
columns_expected => [qw/a.k1 a.k2/] |
|
809 |
}, |
|
810 |
{ |
|
811 |
name => 'placeholder in with table name', |
|
812 |
source => "{in a.k1 2} {in b.k2 2}", |
|
813 |
sql_expected => "a.k1 in (?, ?) b.k2 in (?, ?);", |
|
814 |
columns_expected => [qw/a.k1 a.k1 b.k2 b.k2/] |
|
815 |
}, |
|
816 |
{ |
|
817 |
name => 'not contain tag', |
|
818 |
source => "aaa", |
|
819 |
sql_expected => "aaa;", |
|
820 |
columns_expected => [], |
|
821 |
} |
|
822 |
]; |
|
823 | ||
824 |
for (my $i = 0; $i < @$datas; $i++) { |
|
825 |
my $data = $datas->[$i]; |
|
826 |
my $builder = DBIx::Custom->new->query_builder; |
|
827 |
my $query = $builder->build_query($data->{source}); |
|
828 |
is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql"); |
|
829 |
is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns"); |
|
830 |
} |
|
831 | ||
832 |
$builder = DBIx::Custom->new->query_builder; |
|
833 |
$ret_val = $builder->register_tag( |
|
834 |
p => sub { |
|
835 |
my @args = @_; |
|
836 |
|
|
837 |
my $expand = "? $args[0] $args[1]"; |
|
838 |
my $columns = [2]; |
|
839 |
return [$expand, $columns]; |
|
840 |
} |
|
cleanup test
|
841 |
); |
842 | ||
cleanup test
|
843 |
$query = $builder->build_query("{p a b}"); |
844 |
is($query->{sql}, "? a b;", "register_tag sql"); |
|
845 |
is_deeply($query->{columns}, [2], "register_tag columns"); |
|
846 |
isa_ok($ret_val, 'DBIx::Custom::QueryBuilder'); |
|
cleanup test
|
847 | |
cleanup test
|
848 |
$builder = DBIx::Custom->new->query_builder; |
cleanup test
|
849 | |
cleanup test
|
850 |
eval{$builder->build_query('{? }')}; |
851 |
like($@, qr/\QColumn name must be specified in tag "{? }"/, "? not arguments"); |
|
cleanup test
|
852 | |
cleanup test
|
853 |
eval{$builder->build_query("{a }")}; |
854 |
like($@, qr/\QTag "a" is not registered/, "tag not exist"); |
|
cleanup test
|
855 | |
cleanup test
|
856 |
$builder->register_tag({ |
857 |
q => 'string' |
|
858 |
}); |
|
cleanup test
|
859 | |
cleanup test
|
860 |
eval{$builder->build_query("{q}", {})}; |
861 |
like($@, qr/Tag "q" must be sub reference/, "tag not code ref"); |
|
cleanup test
|
862 | |
cleanup test
|
863 |
$builder->register_tag({ |
864 |
r => sub {} |
|
865 |
}); |
|
cleanup test
|
866 | |
cleanup test
|
867 |
eval{$builder->build_query("{r}")}; |
868 |
like($@, qr/\QTag "r" must return [STRING, ARRAY_REFERENCE]/, "tag return noting"); |
|
869 | ||
870 |
$builder->register_tag({ |
|
871 |
s => sub { return ["a", ""]} |
|
872 |
}); |
|
873 | ||
874 |
eval{$builder->build_query("{s}")}; |
|
875 |
like($@, qr/\QTag "s" must return [STRING, ARRAY_REFERENCE]/, "tag return not array columns"); |
|
876 | ||
877 |
$builder->register_tag( |
|
878 |
t => sub {return ["a", []]} |
|
cleanup test
|
879 |
); |
880 | ||
cleanup test
|
881 | |
882 |
test 'General error case'; |
|
883 |
$builder = DBIx::Custom->new->query_builder; |
|
884 |
$builder->register_tag( |
|
885 |
a => sub { |
|
886 |
return ["? ? ?", ['']]; |
|
887 |
} |
|
cleanup test
|
888 |
); |
cleanup test
|
889 |
eval{$builder->build_query("{a}")}; |
890 |
like($@, qr/\QPlaceholder count/, "placeholder count is invalid"); |
|
cleanup test
|
891 | |
cleanup test
|
892 | |
893 |
test 'Default tag Error case'; |
|
894 |
eval{$builder->build_query("{= }")}; |
|
895 |
like($@, qr/Column name must be specified in tag "{= }"/, "basic '=' : key not exist"); |
|
896 | ||
897 |
eval{$builder->build_query("{in }")}; |
|
898 |
like($@, qr/Column name and count of values must be specified in tag "{in }"/, "in : key not exist"); |
|
899 | ||
900 |
eval{$builder->build_query("{in a}")}; |
|
901 |
like($@, qr/\QColumn name and count of values must be specified in tag "{in }"/, |
|
902 |
"in : key not exist"); |
|
903 | ||
904 |
eval{$builder->build_query("{in a r}")}; |
|
905 |
like($@, qr/\QColumn name and count of values must be specified in tag "{in }"/, |
|
906 |
"in : key not exist"); |
|
907 | ||
908 |
test 'variouse source'; |
|
909 |
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d;"; |
|
910 |
$query = $builder->build_query($source); |
|
911 |
is($query->sql, 'a b = ? c { } { = ? } = ? d;', "basic : 1"); |
|
912 | ||
913 |
$source = "abc;"; |
|
914 |
$query = $builder->build_query($source); |
|
915 |
is($query->sql, 'abc;', "basic : 2"); |
|
916 | ||
917 |
$source = "{= a}"; |
|
918 |
$query = $builder->build_query($source); |
|
919 |
is($query->sql, 'a = ?;', "only tag"); |
|
920 | ||
921 |
$source = "000;"; |
|
922 |
$query = $builder->build_query($source); |
|
923 |
is($query->sql, '000;', "contain 0 value"); |
|
924 | ||
925 |
$source = "a {= b} }"; |
|
926 |
eval{$builder->build_query($source)}; |
|
927 |
like($@, qr/unexpected "}"/, "error : 1"); |
|
928 | ||
929 |
$source = "a {= {}"; |
|
930 |
eval{$builder->build_query($source)}; |
|
931 |
like($@, qr/unexpected "{"/, "error : 2"); |
|
932 | ||
test cleanup
|
933 | |
934 | ||
935 | ||
936 | ||
937 | ||
938 | ||
939 | ||
940 | ||
941 | ||
942 | ||
943 | ||
944 | ||
945 | ||
946 | ||
947 | ||
948 | ||
949 | ||
950 | ||
951 | ||
952 | ||
953 | ||
954 | ||
955 | ||
956 | ||
957 | ||
958 | ||
959 |
### a little complex test |
|
cleanup test
|
960 |
test 'select() wrap option'; |
961 |
$dbi = DBIx::Custom->connect; |
|
962 |
eval { $dbi->execute('drop table table1') }; |
|
963 |
$dbi->execute($create_table1); |
|
964 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
965 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3}); |
|
966 |
$rows = $dbi->select( |
|
967 |
table => 'table1', |
|
968 |
column => 'key1', |
|
969 |
wrap => ['select * from (', ') as t where key1 = 1'] |
|
970 |
)->all; |
|
971 |
is_deeply($rows, [{key1 => 1}]); |
|
972 | ||
973 |
eval { |
|
974 |
$dbi->select( |
|
975 |
table => 'table1', |
|
976 |
column => 'key1', |
|
977 |
wrap => 'select * from (' |
|
978 |
) |
|
979 |
}; |
|
980 |
like($@, qr/array/); |
|
981 | ||
cleanup test
|
982 |
test 'dbi method from model'; |
983 |
$dbi = MyDBI9->connect; |
|
984 |
eval { $dbi->execute('drop table table1') }; |
|
985 |
$dbi->execute($create_table1); |
|
986 |
$dbi->setup_model; |
|
987 |
$model = $dbi->model('table1'); |
|
988 |
eval{$model->execute('select * from table1')}; |
|
989 |
ok(!$@); |
|
990 | ||
991 |
test 'column table option'; |
|
992 |
$dbi = MyDBI9->connect; |
|
993 |
eval { $dbi->execute('drop table table1') }; |
|
994 |
$dbi->execute($create_table1); |
|
995 |
eval { $dbi->execute('drop table table2') }; |
|
996 |
$dbi->execute($create_table2); |
|
997 |
$dbi->setup_model; |
|
998 |
$dbi->execute('insert into table1 (key1, key2) values (1, 2);'); |
|
999 |
$dbi->execute('insert into table2 (key1, key3) values (1, 4);'); |
|
1000 |
$model = $dbi->model('table1'); |
|
1001 |
$result = $model->select( |
|
1002 |
column => [ |
|
1003 |
$model->column('table2', {alias => 'table2_alias'}) |
|
1004 |
], |
|
1005 |
where => {'table2_alias.key3' => 4} |
|
1006 |
); |
|
1007 |
is_deeply($result->one, |
|
1008 |
{'table2_alias.key1' => 1, 'table2_alias.key3' => 4}); |
|
1009 | ||
1010 |
$dbi->separator('__'); |
|
1011 |
$result = $model->select( |
|
1012 |
column => [ |
|
1013 |
$model->column('table2', {alias => 'table2_alias'}) |
|
1014 |
], |
|
1015 |
where => {'table2_alias.key3' => 4} |
|
1016 |
); |
|
1017 |
is_deeply($result->one, |
|
1018 |
{'table2_alias__key1' => 1, 'table2_alias__key3' => 4}); |
|
1019 | ||
1020 |
$dbi->separator('-'); |
|
1021 |
$result = $model->select( |
|
1022 |
column => [ |
|
1023 |
$model->column('table2', {alias => 'table2_alias'}) |
|
1024 |
], |
|
1025 |
where => {'table2_alias.key3' => 4} |
|
1026 |
); |
|
1027 |
is_deeply($result->one, |
|
1028 |
{'table2_alias-key1' => 1, 'table2_alias-key3' => 4}); |
|
1029 | ||
1030 |
test 'create_model'; |
|
1031 |
$dbi = DBIx::Custom->connect; |
|
1032 |
eval { $dbi->execute('drop table table1') }; |
|
1033 |
eval { $dbi->execute('drop table table2') }; |
|
1034 |
$dbi->execute($create_table1); |
|
1035 |
$dbi->execute($create_table2); |
|
1036 | ||
1037 |
$dbi->create_model( |
|
1038 |
table => 'table1', |
|
1039 |
join => [ |
|
1040 |
'left outer join table2 on table1.key1 = table2.key1' |
|
1041 |
], |
|
1042 |
primary_key => ['key1'] |
|
1043 |
); |
|
1044 |
$model2 = $dbi->create_model( |
|
1045 |
table => 'table2' |
|
1046 |
); |
|
1047 |
$dbi->create_model( |
|
1048 |
table => 'table3', |
|
1049 |
filter => [ |
|
1050 |
key1 => {in => sub { uc $_[0] }} |
|
1051 |
] |
|
1052 |
); |
|
1053 |
$dbi->setup_model; |
|
1054 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1055 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3}); |
|
1056 |
$model = $dbi->model('table1'); |
|
1057 |
$result = $model->select( |
|
1058 |
column => [$model->mycolumn, $model->column('table2')], |
|
1059 |
where => {'table1.key1' => 1} |
|
1060 |
); |
|
1061 |
is_deeply($result->one, |
|
1062 |
{key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3}); |
|
1063 |
is_deeply($model2->select->one, {key1 => 1, key3 => 3}); |
|
1064 | ||
1065 |
test 'model method'; |
|
1066 |
test 'create_model'; |
|
1067 |
$dbi = DBIx::Custom->connect; |
|
1068 |
eval { $dbi->execute('drop table table2') }; |
|
1069 |
$dbi->execute($create_table2); |
|
1070 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3}); |
|
1071 |
$model = $dbi->create_model( |
|
1072 |
table => 'table2' |
|
1073 |
); |
|
1074 |
$model->method(foo => sub { shift->select(@_) }); |
|
1075 |
is_deeply($model->foo->one, {key1 => 1, key3 => 3}); |
|
1076 | ||
cleanup test
|
1077 |
test 'join'; |
1078 |
$dbi = DBIx::Custom->connect; |
|
1079 |
eval { $dbi->execute('drop table table1') }; |
|
1080 |
$dbi->execute($create_table1); |
|
1081 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1082 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
1083 |
$dbi->execute($create_table2); |
|
1084 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
|
1085 |
$dbi->execute('create table table3 (key3 int, key4 int);'); |
|
1086 |
$dbi->insert(table => 'table3', param => {key3 => 5, key4 => 4}); |
|
1087 |
$rows = $dbi->select( |
|
1088 |
table => 'table1', |
|
1089 |
column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3', |
|
1090 |
where => {'table1.key2' => 2}, |
|
1091 |
join => ['left outer join table2 on table1.key1 = table2.key1'] |
|
1092 |
)->all; |
|
1093 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}]); |
|
1094 | ||
1095 |
$rows = $dbi->select( |
|
1096 |
table => 'table1', |
|
1097 |
where => {'key1' => 1}, |
|
1098 |
join => ['left outer join table2 on table1.key1 = table2.key1'] |
|
1099 |
)->all; |
|
1100 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
1101 | ||
1102 |
eval { |
|
1103 |
$rows = $dbi->select( |
|
1104 |
table => 'table1', |
|
1105 |
column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3', |
|
1106 |
where => {'table1.key2' => 2}, |
|
1107 |
join => {'table1.key1' => 'table2.key1'} |
|
1108 |
); |
|
1109 |
}; |
|
1110 |
like ($@, qr/array/); |
|
1111 | ||
1112 |
$rows = $dbi->select( |
|
1113 |
table => 'table1', |
|
1114 |
where => {'key1' => 1}, |
|
1115 |
join => ['left outer join table2 on table1.key1 = table2.key1', |
|
1116 |
'left outer join table3 on table2.key3 = table3.key3'] |
|
1117 |
)->all; |
|
1118 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
1119 | ||
1120 |
$rows = $dbi->select( |
|
1121 |
column => 'table3.key4 as table3__key4', |
|
1122 |
table => 'table1', |
|
1123 |
where => {'table1.key1' => 1}, |
|
1124 |
join => ['left outer join table2 on table1.key1 = table2.key1', |
|
1125 |
'left outer join table3 on table2.key3 = table3.key3'] |
|
1126 |
)->all; |
|
1127 |
is_deeply($rows, [{table3__key4 => 4}]); |
|
1128 | ||
1129 |
$rows = $dbi->select( |
|
1130 |
column => 'table1.key1 as table1__key1', |
|
1131 |
table => 'table1', |
|
1132 |
where => {'table3.key4' => 4}, |
|
1133 |
join => ['left outer join table2 on table1.key1 = table2.key1', |
|
1134 |
'left outer join table3 on table2.key3 = table3.key3'] |
|
1135 |
)->all; |
|
1136 |
is_deeply($rows, [{table1__key1 => 1}]); |
|
1137 | ||
1138 |
$dbi = DBIx::Custom->connect; |
|
1139 |
$dbi->quote('"'); |
|
1140 |
eval { $dbi->execute('drop table table1') }; |
|
1141 |
$dbi->execute($create_table1); |
|
1142 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1143 |
$dbi->execute($create_table2); |
|
1144 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
|
1145 |
$rows = $dbi->select( |
|
1146 |
table => 'table1', |
|
1147 |
column => '"table1"."key1" as "table1_key1", "table2"."key1" as "table2_key1", "key2", "key3"', |
|
1148 |
where => {'table1.key2' => 2}, |
|
1149 |
join => ['left outer join "table2" on "table1"."key1" = "table2"."key1"'], |
|
1150 |
)->all; |
|
1151 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], |
|
1152 |
'quote'); |
|
1153 | ||
1154 | ||
1155 |
$dbi = DBIx::Custom->connect; |
|
1156 |
eval { $dbi->execute('drop table table1') }; |
|
1157 |
$dbi->execute($create_table1); |
|
1158 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1159 |
$sql = <<"EOS"; |
|
1160 |
left outer join ( |
|
1161 |
select * from table1 as t1 |
|
1162 |
where t1.key2 = ( |
|
1163 |
select max(t2.key2) from table1 as t2 |
|
1164 |
where t1.key1 = t2.key1 |
|
1165 |
) |
|
1166 |
) as latest_table1 on table1.key1 = latest_table1.key1 |
|
1167 |
EOS |
|
1168 |
$join = [$sql]; |
|
1169 |
$rows = $dbi->select( |
|
1170 |
table => 'table1', |
|
1171 |
column => 'latest_table1.key1 as latest_table1__key1', |
|
1172 |
join => $join |
|
1173 |
)->all; |
|
1174 |
is_deeply($rows, [{latest_table1__key1 => 1}]); |
|
1175 | ||
1176 |
$dbi = DBIx::Custom->connect; |
|
1177 |
eval { $dbi->execute('drop table table1') }; |
|
1178 |
eval { $dbi->execute('drop table table2') }; |
|
1179 |
$dbi->execute($create_table1); |
|
1180 |
$dbi->execute($create_table2); |
|
1181 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1182 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4}); |
|
1183 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
|
1184 |
$result = $dbi->select( |
|
1185 |
table => 'table1', |
|
1186 |
join => [ |
|
1187 |
"left outer join table2 on table2.key2 = '4' and table1.key1 = table2.key1" |
|
1188 |
] |
|
1189 |
); |
|
1190 |
is_deeply($result->all, [{key1 => 1, key2 => 2}]); |
|
1191 |
$result = $dbi->select( |
|
1192 |
table => 'table1', |
|
1193 |
column => [{table2 => ['key3']}], |
|
1194 |
join => [ |
|
1195 |
"left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1" |
|
1196 |
] |
|
1197 |
); |
|
1198 |
is_deeply($result->all, [{'table2.key3' => 4}]); |
|
1199 |
$result = $dbi->select( |
|
1200 |
table => 'table1', |
|
1201 |
column => [{table2 => ['key3']}], |
|
1202 |
join => [ |
|
1203 |
"left outer join table2 on table1.key1 = table2.key1 and table2.key3 = '4'" |
|
1204 |
] |
|
1205 |
); |
|
1206 |
is_deeply($result->all, [{'table2.key3' => 4}]); |
|
1207 | ||
1208 |
$dbi = DBIx::Custom->connect; |
|
1209 |
eval { $dbi->execute('drop table table1') }; |
|
1210 |
eval { $dbi->execute('drop table table2') }; |
|
1211 |
$dbi->execute($create_table1); |
|
1212 |
$dbi->execute($create_table2); |
|
1213 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1214 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4}); |
|
1215 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
|
1216 |
$result = $dbi->select( |
|
1217 |
table => 'table1', |
|
1218 |
column => [{table2 => ['key3']}], |
|
1219 |
join => [ |
|
1220 |
{ |
|
1221 |
clause => "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1", |
|
1222 |
table => ['table1', 'table2'] |
|
1223 |
} |
|
1224 |
] |
|
1225 |
); |
|
1226 |
is_deeply($result->all, [{'table2.key3' => 4}]); |
|
1227 | ||
test cleanup
|
1228 | |
1229 |
test 'update_param'; |
|
1230 |
$dbi = DBIx::Custom->connect; |
|
1231 |
eval { $dbi->execute('drop table table1') }; |
|
1232 |
$dbi->execute($create_table1_2); |
|
1233 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
1234 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
1235 | ||
1236 |
$param = {key2 => 11}; |
|
1237 |
$update_param = $dbi->update_param($param); |
|
1238 |
$sql = <<"EOS"; |
|
1239 |
update table1 $update_param |
|
1240 |
where key1 = 1 |
|
1241 |
EOS |
|
1242 |
$dbi->execute($sql, param => $param); |
|
1243 |
$result = $dbi->execute('select * from table1;', table => 'table1'); |
|
1244 |
$rows = $result->all; |
|
1245 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5}, |
|
1246 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
1247 |
"basic"); |
|
1248 | ||
1249 | ||
1250 |
$dbi = DBIx::Custom->connect; |
|
1251 |
eval { $dbi->execute('drop table table1') }; |
|
1252 |
$dbi->execute($create_table1_2); |
|
1253 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
1254 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
1255 | ||
1256 |
$param = {key2 => 11, key3 => 33}; |
|
1257 |
$update_param = $dbi->update_param($param); |
|
1258 |
$sql = <<"EOS"; |
|
1259 |
update table1 $update_param |
|
1260 |
where key1 = 1 |
|
1261 |
EOS |
|
1262 |
$dbi->execute($sql, param => $param); |
|
1263 |
$result = $dbi->execute('select * from table1;', table => 'table1'); |
|
1264 |
$rows = $result->all; |
|
1265 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5}, |
|
1266 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
1267 |
"basic"); |
|
1268 | ||
1269 |
$dbi = DBIx::Custom->connect; |
|
1270 |
eval { $dbi->execute('drop table table1') }; |
|
1271 |
$dbi->execute($create_table1_2); |
|
1272 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
1273 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
1274 | ||
1275 |
$param = {key2 => 11, key3 => 33}; |
|
1276 |
$update_param = $dbi->update_param($param, {no_set => 1}); |
|
1277 |
$sql = <<"EOS"; |
|
1278 |
update table1 set $update_param |
|
1279 |
where key1 = 1 |
|
1280 |
EOS |
|
1281 |
$dbi->execute($sql, param => $param); |
|
1282 |
$result = $dbi->execute('select * from table1;', table => 'table1'); |
|
1283 |
$rows = $result->all; |
|
1284 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5}, |
|
1285 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
1286 |
"update param no_set"); |
|
1287 | ||
1288 |
|
|
1289 |
eval { $dbi->update_param({";" => 1}) }; |
|
1290 |
like($@, qr/not safety/); |
|
1291 | ||
1292 | ||
1293 |
test 'update_param'; |
|
1294 |
$dbi = DBIx::Custom->connect; |
|
1295 |
eval { $dbi->execute('drop table table1') }; |
|
1296 |
$dbi->execute($create_table1_2); |
|
1297 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
1298 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
1299 | ||
1300 |
$param = {key2 => 11}; |
|
1301 |
$update_param = $dbi->assign_param($param); |
|
1302 |
$sql = <<"EOS"; |
|
1303 |
update table1 set $update_param |
|
1304 |
where key1 = 1 |
|
1305 |
EOS |
|
1306 |
$dbi->execute($sql, param => $param, table => 'table1'); |
|
1307 |
$result = $dbi->execute('select * from table1;'); |
|
1308 |
$rows = $result->all; |
|
1309 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5}, |
|
1310 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
1311 |
"basic"); |
|
1312 | ||
1313 | ||
cleanup test
|
1314 |
test 'type option'; # DEPRECATED! |
1315 |
$dbi = DBIx::Custom->connect( |
|
1316 |
data_source => 'dbi:SQLite:dbname=:memory:', |
|
1317 |
dbi_option => { |
|
1318 |
$DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1) |
|
1319 |
} |
|
cleanup test
|
1320 |
); |
cleanup test
|
1321 |
$binary = pack("I3", 1, 2, 3); |
1322 |
eval { $dbi->execute('drop table table1') }; |
|
1323 |
$dbi->execute('create table table1(key1, key2)'); |
|
1324 |
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]); |
|
1325 |
$result = $dbi->select(table => 'table1'); |
|
1326 |
$row = $result->one; |
|
1327 |
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic"); |
|
1328 |
$result = $dbi->execute('select length(key1) as key1_length from table1'); |
|
1329 |
$row = $result->one; |
|
1330 |
is($row->{key1_length}, length $binary); |
|
cleanup test
|
1331 | |
cleanup test
|
1332 |
test 'type_rule from'; |
1333 |
$dbi = DBIx::Custom->connect; |
|
1334 |
$dbi->type_rule( |
|
1335 |
from1 => { |
|
1336 |
date => sub { uc $_[0] } |
|
1337 |
} |
|
cleanup test
|
1338 |
); |
cleanup test
|
1339 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
1340 |
$dbi->insert({key1 => 'a'}, table => 'table1'); |
|
1341 |
$result = $dbi->select(table => 'table1'); |
|
1342 |
is($result->fetch_first->[0], 'A'); |
|
cleanup test
|
1343 | |
cleanup test
|
1344 |
$result = $dbi->select(table => 'table1'); |
1345 |
is($result->one->{key1}, 'A'); |
|
cleanup test
|
1346 | |
cleanup test
|
1347 | |
1348 |
test 'type_rule into'; |
|
test cleanup
|
1349 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1350 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
1351 |
$dbi->type_rule( |
|
1352 |
into1 => { |
|
1353 |
date => sub { uc $_[0] } |
|
1354 |
} |
|
1355 |
); |
|
cleanup test
|
1356 |
$dbi->insert({key1 => 'a'}, table => 'table1'); |
cleanup test
|
1357 |
$result = $dbi->select(table => 'table1'); |
1358 |
is($result->one->{key1}, 'A'); |
|
1359 | ||
test cleanup
|
1360 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1361 |
$dbi->execute("create table table1 (key1 date, key2 datetime)"); |
1362 |
$dbi->type_rule( |
|
1363 |
into1 => [ |
|
1364 |
[qw/date datetime/] => sub { uc $_[0] } |
|
1365 |
] |
|
1366 |
); |
|
1367 |
$dbi->insert({key1 => 'a', key2 => 'b'}, table => 'table1'); |
|
1368 |
$result = $dbi->select(table => 'table1'); |
|
1369 |
$row = $result->one; |
|
1370 |
is($row->{key1}, 'A'); |
|
1371 |
is($row->{key2}, 'B'); |
|
cleanup test
|
1372 | |
test cleanup
|
1373 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1374 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
1375 |
$dbi->insert({key1 => 'a', key2 => 'B'}, table => 'table1'); |
|
1376 |
$dbi->type_rule( |
|
1377 |
into1 => [ |
|
1378 |
[qw/date datetime/] => sub { uc $_[0] } |
|
1379 |
] |
|
1380 |
); |
|
1381 |
$result = $dbi->execute( |
|
1382 |
"select * from table1 where key1 = :key1 and key2 = :table1.key2;", |
|
1383 |
param => {key1 => 'a', 'table1.key2' => 'b'} |
|
1384 |
); |
|
1385 |
$row = $result->one; |
|
1386 |
is($row->{key1}, 'a'); |
|
1387 |
is($row->{key2}, 'B'); |
|
cleanup test
|
1388 | |
test cleanup
|
1389 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1390 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
1391 |
$dbi->insert({key1 => 'A', key2 => 'B'}, table => 'table1'); |
|
1392 |
$dbi->type_rule( |
|
1393 |
into1 => [ |
|
1394 |
[qw/date datetime/] => sub { uc $_[0] } |
|
1395 |
] |
|
1396 |
); |
|
1397 |
$result = $dbi->execute( |
|
1398 |
"select * from table1 where key1 = :key1 and key2 = :table1.key2;", |
|
1399 |
param => {key1 => 'a', 'table1.key2' => 'b'}, |
|
1400 |
table => 'table1' |
|
1401 |
); |
|
1402 |
$row = $result->one; |
|
1403 |
is($row->{key1}, 'A'); |
|
1404 |
is($row->{key2}, 'B'); |
|
cleanup test
|
1405 | |
cleanup test
|
1406 |
$dbi = DBIx::Custom->connect; |
1407 |
$dbi->execute("create table table1 (key1 date, key2 datetime)"); |
|
1408 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
1409 |
$dbi->type_rule( |
|
1410 |
from1 => { |
|
1411 |
date => 'twice', |
|
1412 |
}, |
|
1413 |
into1 => { |
|
1414 |
date => 'twice', |
|
1415 |
} |
|
1416 |
); |
|
1417 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
1418 |
$result = $dbi->select(table => 'table1'); |
|
1419 |
is($result->fetch->[0], 8); |
|
cleanup test
|
1420 | |
cleanup test
|
1421 |
test 'type_rule and filter order'; |
test cleanup
|
1422 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1423 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
1424 |
$dbi->type_rule( |
|
1425 |
into1 => { |
|
1426 |
date => sub { $_[0] . 'b' } |
|
1427 |
}, |
|
1428 |
into2 => { |
|
1429 |
date => sub { $_[0] . 'c' } |
|
1430 |
}, |
|
1431 |
from1 => { |
|
1432 |
date => sub { $_[0] . 'd' } |
|
1433 |
}, |
|
1434 |
from2 => { |
|
1435 |
date => sub { $_[0] . 'e' } |
|
1436 |
} |
|
1437 |
); |
|
1438 |
$dbi->insert({key1 => '1'}, table => 'table1', filter => {key1 => sub { $_[0] . 'a' }}); |
|
1439 |
$result = $dbi->select(table => 'table1'); |
|
1440 |
$result->filter(key1 => sub { $_[0] . 'f' }); |
|
1441 |
is($result->fetch_first->[0], '1abcdef'); |
|
cleanup test
|
1442 | |
cleanup test
|
1443 |
$dbi = DBIx::Custom->connect; |
1444 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1445 |
$dbi->type_rule( |
|
1446 |
from1 => { |
|
1447 |
date => sub { $_[0] . 'p' } |
|
1448 |
}, |
|
1449 |
from2 => { |
|
1450 |
date => sub { $_[0] . 'q' } |
|
1451 |
}, |
|
1452 |
); |
|
1453 |
$dbi->insert({key1 => '1'}, table => 'table1'); |
|
1454 |
$result = $dbi->select(table => 'table1'); |
|
1455 |
$result->type_rule( |
|
1456 |
from1 => { |
|
1457 |
date => sub { $_[0] . 'd' } |
|
1458 |
}, |
|
1459 |
from2 => { |
|
1460 |
date => sub { $_[0] . 'e' } |
|
1461 |
} |
|
1462 |
); |
|
1463 |
$result->filter(key1 => sub { $_[0] . 'f' }); |
|
1464 |
is($result->fetch_first->[0], '1def'); |
|
cleanup test
|
1465 | |
cleanup test
|
1466 |
test 'type_rule_off'; |
1467 |
$dbi = DBIx::Custom->connect; |
|
1468 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1469 |
$dbi->type_rule( |
|
1470 |
from1 => { |
|
1471 |
date => sub { $_[0] * 2 }, |
|
1472 |
}, |
|
1473 |
into1 => { |
|
1474 |
date => sub { $_[0] * 2 }, |
|
1475 |
} |
|
1476 |
); |
|
1477 |
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1); |
|
1478 |
$result = $dbi->select(table => 'table1', type_rule_off => 1); |
|
1479 |
is($result->type_rule_off->fetch->[0], 2); |
|
cleanup test
|
1480 | |
cleanup test
|
1481 |
$dbi = DBIx::Custom->connect; |
1482 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1483 |
$dbi->type_rule( |
|
1484 |
from1 => { |
|
1485 |
date => sub { $_[0] * 2 }, |
|
1486 |
}, |
|
1487 |
into1 => { |
|
1488 |
date => sub { $_[0] * 3 }, |
|
1489 |
} |
|
1490 |
); |
|
1491 |
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1); |
|
1492 |
$result = $dbi->select(table => 'table1', type_rule_off => 1); |
|
1493 |
is($result->one->{key1}, 4); |
|
cleanup test
|
1494 | |
cleanup test
|
1495 |
$dbi = DBIx::Custom->connect; |
1496 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1497 |
$dbi->type_rule( |
|
1498 |
from1 => { |
|
1499 |
date => sub { $_[0] * 2 }, |
|
1500 |
}, |
|
1501 |
into1 => { |
|
1502 |
date => sub { $_[0] * 3 }, |
|
1503 |
} |
|
1504 |
); |
|
1505 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
1506 |
$result = $dbi->select(table => 'table1'); |
|
1507 |
is($result->one->{key1}, 12); |
|
cleanup test
|
1508 | |
cleanup test
|
1509 |
$dbi = DBIx::Custom->connect; |
1510 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1511 |
$dbi->type_rule( |
|
1512 |
from1 => { |
|
1513 |
date => sub { $_[0] * 2 }, |
|
1514 |
}, |
|
1515 |
into1 => { |
|
1516 |
date => sub { $_[0] * 3 }, |
|
1517 |
} |
|
cleanup test
|
1518 |
); |
cleanup test
|
1519 |
$dbi->insert({key1 => 2}, table => 'table1'); |
1520 |
$result = $dbi->select(table => 'table1'); |
|
1521 |
is($result->fetch->[0], 12); |
|
cleanup test
|
1522 | |
cleanup test
|
1523 |
$dbi = DBIx::Custom->connect; |
1524 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1525 |
$dbi->register_filter(ppp => sub { uc $_[0] }); |
|
1526 |
$dbi->type_rule( |
|
1527 |
into1 => { |
|
1528 |
date => 'ppp' |
|
cleanup test
|
1529 |
} |
cleanup test
|
1530 |
); |
1531 |
$dbi->insert({key1 => 'a'}, table => 'table1'); |
|
1532 |
$result = $dbi->select(table => 'table1'); |
|
1533 |
is($result->one->{key1}, 'A'); |
|
cleanup test
|
1534 | |
cleanup test
|
1535 |
eval{$dbi->type_rule( |
1536 |
into1 => { |
|
1537 |
date => 'pp' |
|
cleanup test
|
1538 |
} |
cleanup test
|
1539 |
)}; |
1540 |
like($@, qr/not registered/); |
|
cleanup test
|
1541 | |
test cleanup
|
1542 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1543 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
1544 |
eval { |
|
1545 |
$dbi->type_rule( |
|
1546 |
from1 => { |
|
1547 |
Date => sub { $_[0] * 2 }, |
|
1548 |
} |
|
1549 |
); |
|
1550 |
}; |
|
1551 |
like($@, qr/lower/); |
|
clenup test
|
1552 | |
cleanup test
|
1553 |
eval { |
1554 |
$dbi->type_rule( |
|
1555 |
into1 => { |
|
1556 |
Date => sub { $_[0] * 2 }, |
|
1557 |
} |
|
1558 |
); |
|
1559 |
}; |
|
1560 |
like($@, qr/lower/); |
|
clenup test
|
1561 | |
cleanup test
|
1562 |
$dbi = DBIx::Custom->connect; |
1563 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1564 |
$dbi->type_rule( |
|
1565 |
from1 => { |
|
1566 |
date => sub { $_[0] * 2 }, |
|
1567 |
}, |
|
1568 |
into1 => { |
|
1569 |
date => sub { $_[0] * 3 }, |
|
1570 |
} |
|
1571 |
); |
|
1572 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
clenup test
|
1573 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1574 |
$result->type_rule_off; |
1575 |
is($result->one->{key1}, 6); |
|
clenup test
|
1576 | |
cleanup test
|
1577 |
$dbi = DBIx::Custom->connect; |
1578 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1579 |
$dbi->type_rule( |
|
1580 |
from1 => { |
|
1581 |
date => sub { $_[0] * 2 }, |
|
1582 |
datetime => sub { $_[0] * 4 }, |
|
1583 |
}, |
|
1584 |
); |
|
1585 |
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1'); |
|
clenup test
|
1586 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1587 |
$result->type_rule( |
1588 |
from1 => { |
|
1589 |
date => sub { $_[0] * 3 } |
|
1590 |
} |
|
1591 |
); |
|
1592 |
$row = $result->one; |
|
1593 |
is($row->{key1}, 6); |
|
1594 |
is($row->{key2}, 2); |
|
clenup test
|
1595 | |
1596 |
$result = $dbi->select(table => 'table1'); |
|
cleanup test
|
1597 |
$result->type_rule( |
1598 |
from1 => { |
|
1599 |
date => sub { $_[0] * 3 } |
|
1600 |
} |
|
1601 |
); |
|
1602 |
$row = $result->one; |
|
1603 |
is($row->{key1}, 6); |
|
1604 |
is($row->{key2}, 2); |
|
clenup test
|
1605 | |
1606 |
$result = $dbi->select(table => 'table1'); |
|
cleanup test
|
1607 |
$result->type_rule( |
1608 |
from1 => { |
|
1609 |
date => sub { $_[0] * 3 } |
|
1610 |
} |
|
1611 |
); |
|
1612 |
$row = $result->one; |
|
1613 |
is($row->{key1}, 6); |
|
1614 |
is($row->{key2}, 2); |
|
clenup test
|
1615 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1616 |
$result->type_rule( |
1617 |
from1 => [date => sub { $_[0] * 3 }] |
|
1618 |
); |
|
1619 |
$row = $result->one; |
|
1620 |
is($row->{key1}, 6); |
|
1621 |
is($row->{key2}, 2); |
|
1622 |
$dbi->register_filter(fivetimes => sub { $_[0] * 5}); |
|
clenup test
|
1623 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1624 |
$result->type_rule( |
1625 |
from1 => [date => 'fivetimes'] |
|
1626 |
); |
|
1627 |
$row = $result->one; |
|
1628 |
is($row->{key1}, 10); |
|
1629 |
is($row->{key2}, 2); |
|
clenup test
|
1630 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1631 |
$result->type_rule( |
1632 |
from1 => [date => undef] |
|
1633 |
); |
|
1634 |
$row = $result->one; |
|
1635 |
is($row->{key1}, 2); |
|
1636 |
is($row->{key2}, 2); |
|
clenup test
|
1637 | |
test cleanup
|
1638 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1639 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
1640 |
$dbi->type_rule( |
|
1641 |
from1 => { |
|
1642 |
date => sub { $_[0] * 2 }, |
|
1643 |
}, |
|
1644 |
); |
|
1645 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
clenup test
|
1646 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1647 |
$result->filter(key1 => sub { $_[0] * 3 }); |
1648 |
is($result->one->{key1}, 12); |
|
clenup test
|
1649 | |
cleanup test
|
1650 |
$dbi = DBIx::Custom->connect; |
1651 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1652 |
$dbi->type_rule( |
|
1653 |
from1 => { |
|
1654 |
date => sub { $_[0] * 2 }, |
|
1655 |
}, |
|
1656 |
); |
|
1657 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
clenup test
|
1658 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1659 |
$result->filter(key1 => sub { $_[0] * 3 }); |
1660 |
is($result->fetch->[0], 12); |
|
clenup test
|
1661 | |
cleanup test
|
1662 |
$dbi = DBIx::Custom->connect; |
1663 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1664 |
$dbi->type_rule( |
|
1665 |
into1 => { |
|
1666 |
date => sub { $_[0] . 'b' } |
|
1667 |
}, |
|
1668 |
into2 => { |
|
1669 |
date => sub { $_[0] . 'c' } |
|
1670 |
}, |
|
1671 |
from1 => { |
|
1672 |
date => sub { $_[0] . 'd' } |
|
1673 |
}, |
|
1674 |
from2 => { |
|
1675 |
date => sub { $_[0] . 'e' } |
|
1676 |
} |
|
1677 |
); |
|
1678 |
$dbi->insert({key1 => '1'}, table => 'table1', type_rule_off => 1); |
|
clenup test
|
1679 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1680 |
is($result->type_rule_off->fetch_first->[0], '1'); |
clenup test
|
1681 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1682 |
is($result->type_rule_on->fetch_first->[0], '1de'); |
clenup test
|
1683 | |
cleanup test
|
1684 |
$dbi = DBIx::Custom->connect; |
1685 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1686 |
$dbi->type_rule( |
|
1687 |
into1 => { |
|
1688 |
date => sub { $_[0] . 'b' } |
|
cleanup test
|
1689 |
}, |
cleanup test
|
1690 |
into2 => { |
1691 |
date => sub { $_[0] . 'c' } |
|
cleanup test
|
1692 |
}, |
cleanup test
|
1693 |
from1 => { |
1694 |
date => sub { $_[0] . 'd' } |
|
cleanup test
|
1695 |
}, |
cleanup test
|
1696 |
from2 => { |
1697 |
date => sub { $_[0] . 'e' } |
|
cleanup test
|
1698 |
} |
1699 |
); |
|
cleanup test
|
1700 |
$dbi->insert({key1 => '1'}, table => 'table1', type_rule1_off => 1); |
1701 |
$result = $dbi->select(table => 'table1'); |
|
1702 |
is($result->type_rule1_off->fetch_first->[0], '1ce'); |
|
1703 |
$result = $dbi->select(table => 'table1'); |
|
1704 |
is($result->type_rule1_on->fetch_first->[0], '1cde'); |
|
cleanup test
|
1705 | |
cleanup test
|
1706 |
$dbi = DBIx::Custom->connect; |
1707 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1708 |
$dbi->type_rule( |
|
1709 |
into1 => { |
|
1710 |
date => sub { $_[0] . 'b' } |
|
1711 |
}, |
|
1712 |
into2 => { |
|
1713 |
date => sub { $_[0] . 'c' } |
|
1714 |
}, |
|
1715 |
from1 => { |
|
1716 |
date => sub { $_[0] . 'd' } |
|
1717 |
}, |
|
1718 |
from2 => { |
|
1719 |
date => sub { $_[0] . 'e' } |
|
cleanup test
|
1720 |
} |
1721 |
); |
|
cleanup test
|
1722 |
$dbi->insert({key1 => '1'}, table => 'table1', type_rule2_off => 1); |
1723 |
$result = $dbi->select(table => 'table1'); |
|
1724 |
is($result->type_rule2_off->fetch_first->[0], '1bd'); |
|
1725 |
$result = $dbi->select(table => 'table1'); |
|
1726 |
is($result->type_rule2_on->fetch_first->[0], '1bde'); |
|
test cleanup
|
1727 | |
1728 |
test 'prefix'; |
|
1729 |
$dbi = DBIx::Custom->connect; |
|
1730 |
eval { $dbi->execute('drop table table1') }; |
|
1731 |
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))"); |
|
1732 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1733 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}, prefix => 'or replace'); |
|
1734 |
$result = $dbi->execute('select * from table1;'); |
|
1735 |
$rows = $result->all; |
|
1736 |
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic"); |
|
1737 | ||
1738 |
$dbi = DBIx::Custom->connect; |
|
1739 |
eval { $dbi->execute('drop table table1') }; |
|
1740 |
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))"); |
|
1741 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1742 |
$dbi->update(table => 'table1', param => {key2 => 4}, |
|
1743 |
where => {key1 => 1}, prefix => 'or replace'); |
|
1744 |
$result = $dbi->execute('select * from table1;'); |
|
1745 |
$rows = $result->all; |
|
1746 |
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic"); |
|
1747 | ||
test cleanup
|
1748 |
test 'Model class'; |
1749 |
use MyDBI1; |
|
1750 |
$dbi = MyDBI1->connect; |
|
1751 |
eval { $dbi->execute('drop table book') }; |
|
1752 |
$dbi->execute("create table book (title, author)"); |
|
1753 |
$model = $dbi->model('book'); |
|
1754 |
$model->insert({title => 'a', author => 'b'}); |
|
1755 |
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic'); |
|
1756 |
$dbi->execute("create table company (name)"); |
|
1757 |
$model = $dbi->model('company'); |
|
1758 |
$model->insert({name => 'a'}); |
|
1759 |
is_deeply($model->list->all, [{name => 'a'}], 'basic'); |
|
1760 |
is($dbi->models->{'book'}, $dbi->model('book')); |
|
1761 |
is($dbi->models->{'company'}, $dbi->model('company')); |
|
1762 | ||
1763 |
$dbi = MyDBI4->connect; |
|
1764 |
eval { $dbi->execute('drop table book') }; |
|
1765 |
$dbi->execute("create table book (title, author)"); |
|
1766 |
$model = $dbi->model('book'); |
|
1767 |
$model->insert({title => 'a', author => 'b'}); |
|
1768 |
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic'); |
|
1769 |
$dbi->execute("create table company (name)"); |
|
1770 |
$model = $dbi->model('company'); |
|
1771 |
$model->insert({name => 'a'}); |
|
1772 |
is_deeply($model->list->all, [{name => 'a'}], 'basic'); |
|
test cleanup
|
1773 | |
test cleanup
|
1774 |
$dbi = MyDBI5->connect; |
1775 |
eval { $dbi->execute('drop table company') }; |
|
1776 |
eval { $dbi->execute('drop table table1') }; |
|
1777 |
$dbi->execute("create table company (name)"); |
|
1778 |
$dbi->execute("create table table1 (key1)"); |
|
1779 |
$model = $dbi->model('company'); |
|
1780 |
$model->insert({name => 'a'}); |
|
1781 |
is_deeply($model->list->all, [{name => 'a'}], 'include all model'); |
|
1782 |
$dbi->insert(table => 'table1', param => {key1 => 1}); |
|
1783 |
$model = $dbi->model('book'); |
|
1784 |
is_deeply($model->list->all, [{key1 => 1}], 'include all model'); |
|
test cleanup
|
1785 | |
test cleanup
|
1786 |
test 'primary_key'; |
1787 |
use MyDBI1; |
|
1788 |
$dbi = MyDBI1->connect; |
|
1789 |
$model = $dbi->model('book'); |
|
1790 |
$model->primary_key(['id', 'number']); |
|
1791 |
is_deeply($model->primary_key, ['id', 'number']); |
|
test cleanup
|
1792 | |
test cleanup
|
1793 |
test 'columns'; |
1794 |
use MyDBI1; |
|
1795 |
$dbi = MyDBI1->connect; |
|
1796 |
$model = $dbi->model('book'); |
|
1797 |
$model->columns(['id', 'number']); |
|
1798 |
is_deeply($model->columns, ['id', 'number']); |
|
test cleanup
|
1799 | |
test cleanup
|
1800 |
test 'setup_model'; |
1801 |
use MyDBI1; |
|
1802 |
$dbi = MyDBI1->connect; |
|
1803 |
eval { $dbi->execute('drop table book') }; |
|
1804 |
eval { $dbi->execute('drop table company') }; |
|
1805 |
eval { $dbi->execute('drop table test') }; |
|
test cleanup
|
1806 | |
test cleanup
|
1807 |
$dbi->execute('create table book (id)'); |
1808 |
$dbi->execute('create table company (id, name);'); |
|
1809 |
$dbi->execute('create table test (id, name, primary key (id, name));'); |
|
1810 |
$dbi->setup_model; |
|
1811 |
is_deeply($dbi->model('book')->columns, ['id']); |
|
1812 |
is_deeply($dbi->model('company')->columns, ['id', 'name']); |
|
test cleanup
|
1813 | |
1814 | ||
1815 | ||
1816 | ||
1817 | ||
1818 | ||
1819 | ||
1820 | ||
1821 | ||
1822 | ||
1823 | ||
1824 | ||
1825 | ||
1826 | ||
test cleanup
|
1827 |
### SQLite only test |
1828 |
test 'quote'; |
|
1829 |
$dbi = DBIx::Custom->connect; |
|
1830 |
$dbi->quote('"'); |
|
1831 |
eval { $dbi->execute("drop table ${q}table$p") }; |
|
1832 |
$dbi->execute($create_table_reserved); |
|
1833 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
|
1834 |
$dbi->insert(table => 'table', param => {select => 1}); |
|
1835 |
$dbi->delete(table => 'table', where => {select => 1}); |
|
1836 |
$result = $dbi->execute("select * from ${q}table$p"); |
|
1837 |
$rows = $result->all; |
|
1838 |
is_deeply($rows, [], "reserved word"); |
|
1839 | ||
1840 | ||
1841 | ||
1842 | ||
1843 | ||
1844 | ||
1845 | ||
test cleanup
|
1846 | |
1847 | ||
1848 | ||
1849 | ||
1850 | ||
1851 | ||
1852 | ||
1853 | ||
1854 | ||
1855 | ||
1856 |
# DEPRECATED! test |
|
1857 |
test 'filter __ expression'; |
|
1858 |
$dbi = DBIx::Custom->connect; |
|
1859 |
eval { $dbi->execute('drop table company') }; |
|
1860 |
eval { $dbi->execute('drop table location') }; |
|
1861 |
$dbi->execute('create table company (id, name, location_id)'); |
|
1862 |
$dbi->execute('create table location (id, name)'); |
|
1863 |
$dbi->apply_filter('location', |
|
1864 |
name => {in => sub { uc $_[0] } } |
|
1865 |
); |
|
1866 | ||
1867 |
$dbi->insert(table => 'company', param => {id => 1, name => 'a', location_id => 2}); |
|
1868 |
$dbi->insert(table => 'location', param => {id => 2, name => 'b'}); |
|
1869 | ||
1870 |
$result = $dbi->select( |
|
1871 |
table => ['company', 'location'], relation => {'company.location_id' => 'location.id'}, |
|
1872 |
column => ['location.name as location__name'] |
|
1873 |
); |
|
1874 |
is($result->fetch_first->[0], 'B'); |
|
1875 | ||
1876 |
$result = $dbi->select( |
|
1877 |
table => 'company', relation => {'company.location_id' => 'location.id'}, |
|
1878 |
column => ['location.name as location__name'] |
|
1879 |
); |
|
1880 |
is($result->fetch_first->[0], 'B'); |
|
1881 | ||
1882 |
$result = $dbi->select( |
|
1883 |
table => 'company', relation => {'company.location_id' => 'location.id'}, |
|
1884 |
column => ['location.name as "location.name"'] |
|
1885 |
); |
|
1886 |
is($result->fetch_first->[0], 'B'); |
|
test cleanup
|
1887 | |
1888 |
test 'reserved_word_quote'; |
|
1889 |
$dbi = DBIx::Custom->connect; |
|
1890 |
eval { $dbi->execute("drop table ${q}table$p") }; |
|
1891 |
$dbi->reserved_word_quote('"'); |
|
1892 |
$dbi->execute($create_table_reserved); |
|
1893 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
|
1894 |
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}}); |
|
1895 |
$dbi->insert(table => 'table', param => {select => 1}); |
|
1896 |
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2}); |
|
1897 |
$result = $dbi->execute("select * from ${q}table$p"); |
|
1898 |
$rows = $result->all; |
|
1899 |
is_deeply($rows, [{select => 2, update => 6}], "reserved word"); |