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