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