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