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