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