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