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; |
cleanup
|
6 |
use Scalar::Util 'isweak'; |
cleanup test
|
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 | ||
finished oracle test
|
16 |
$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/}; |
17 |
sub test { print "# $_[0]\n" } |
|
18 | ||
cleanup test
|
19 |
# Constant |
20 |
my $table1 = $dbi->table1; |
|
21 |
my $table2 = $dbi->table2; |
|
22 |
my $table2_alias = $dbi->table2_alias; |
|
23 |
my $table3 = $dbi->table3; |
|
24 |
my $key1 = $dbi->key1; |
|
25 |
my $key2 = $dbi->key2; |
|
26 |
my $key3 = $dbi->key3; |
|
27 |
my $key4 = $dbi->key4; |
|
28 |
my $key5 = $dbi->key5; |
|
29 |
my $key6 = $dbi->key6; |
|
30 |
my $key7 = $dbi->key7; |
|
31 |
my $key8 = $dbi->key8; |
|
32 |
my $key9 = $dbi->key9; |
|
33 |
my $key10 = $dbi->key10; |
|
34 |
my $create_table1 = $dbi->create_table1; |
|
35 |
my $create_table1_2 = $dbi->create_table1_2; |
|
36 |
my $create_table1_type = $dbi->create_table1_type; |
|
37 |
my $create_table1_highperformance = $dbi->create_table1_highperformance; |
|
38 |
my $create_table2 = $dbi->create_table2; |
|
39 |
my $create_table2_2 = $dbi->create_table2_2; |
|
40 |
my $create_table3 = $dbi->create_table3; |
|
41 |
my $create_table_reserved = $dbi->create_table_reserved; |
|
42 |
my $q = substr($dbi->quote, 0, 1); |
|
43 |
my $p = substr($dbi->quote, 1, 1) || $q; |
|
44 |
my $date_typename = $dbi->date_typename; |
|
45 |
my $datetime_typename = $dbi->datetime_typename; |
|
46 |
my $date_datatype = $dbi->date_datatype; |
|
47 |
my $datetime_datatype = $dbi->datetime_datatype; |
|
48 | ||
49 |
# Variables |
|
50 |
my $builder; |
|
51 |
my $datas; |
|
52 |
my $sth; |
|
53 |
my $source; |
|
54 |
my @sources; |
|
55 |
my $select_source; |
|
56 |
my $insert_source; |
|
57 |
my $update_source; |
|
58 |
my $param; |
|
59 |
my $params; |
|
60 |
my $sql; |
|
61 |
my $result; |
|
62 |
my $row; |
|
63 |
my @rows; |
|
64 |
my $rows; |
|
65 |
my $query; |
|
66 |
my @queries; |
|
67 |
my $select_query; |
|
68 |
my $insert_query; |
|
69 |
my $update_query; |
|
70 |
my $ret_val; |
|
71 |
my $infos; |
|
72 |
my $model; |
|
73 |
my $model2; |
|
74 |
my $where; |
|
75 |
my $update_param; |
|
76 |
my $insert_param; |
|
77 |
my $join; |
|
78 |
my $binary; |
|
79 | ||
test cleanup
|
80 |
require MyDBI1; |
test cleanup
|
81 |
{ |
82 |
package MyDBI4; |
|
83 | ||
84 |
use strict; |
|
85 |
use warnings; |
|
86 | ||
87 |
use base 'DBIx::Custom'; |
|
88 | ||
89 |
sub connect { |
|
90 |
my $self = shift->SUPER::connect(@_); |
|
91 |
|
|
92 |
$self->include_model( |
|
93 |
MyModel2 => [ |
|
cleanup test
|
94 |
$table1, |
95 |
{class => $table2, name => $table2} |
|
test cleanup
|
96 |
] |
97 |
); |
|
98 |
} |
|
99 | ||
100 |
package MyModel2::Base1; |
|
101 | ||
102 |
use strict; |
|
103 |
use warnings; |
|
104 | ||
105 |
use base 'DBIx::Custom::Model'; |
|
106 | ||
test cleanup
|
107 |
package MyModel2::table1; |
test cleanup
|
108 | |
109 |
use strict; |
|
110 |
use warnings; |
|
111 | ||
112 |
use base 'MyModel2::Base1'; |
|
113 | ||
114 |
sub insert { |
|
115 |
my ($self, $param) = @_; |
|
116 |
|
|
117 |
return $self->SUPER::insert(param => $param); |
|
118 |
} |
|
119 | ||
120 |
sub list { shift->select; } |
|
121 | ||
test cleanup
|
122 |
package MyModel2::table2; |
test cleanup
|
123 | |
124 |
use strict; |
|
125 |
use warnings; |
|
126 | ||
127 |
use base 'MyModel2::Base1'; |
|
128 | ||
129 |
sub insert { |
|
130 |
my ($self, $param) = @_; |
|
131 |
|
|
132 |
return $self->SUPER::insert(param => $param); |
|
133 |
} |
|
134 | ||
135 |
sub list { shift->select; } |
|
test cleanup
|
136 | |
137 |
package MyModel2::TABLE1; |
|
138 | ||
139 |
use strict; |
|
140 |
use warnings; |
|
141 | ||
142 |
use base 'MyModel2::Base1'; |
|
143 | ||
144 |
sub insert { |
|
145 |
my ($self, $param) = @_; |
|
146 |
|
|
147 |
return $self->SUPER::insert(param => $param); |
|
148 |
} |
|
149 | ||
150 |
sub list { shift->select; } |
|
151 | ||
152 |
package MyModel2::TABLE2; |
|
153 | ||
154 |
use strict; |
|
155 |
use warnings; |
|
156 | ||
157 |
use base 'MyModel2::Base1'; |
|
158 | ||
159 |
sub insert { |
|
160 |
my ($self, $param) = @_; |
|
161 |
|
|
162 |
return $self->SUPER::insert(param => $param); |
|
163 |
} |
|
164 | ||
165 |
sub list { shift->select; } |
|
test cleanup
|
166 |
} |
167 |
{ |
|
168 |
package MyDBI5; |
|
169 | ||
170 |
use strict; |
|
171 |
use warnings; |
|
172 | ||
173 |
use base 'DBIx::Custom'; |
|
174 | ||
175 |
sub connect { |
|
176 |
my $self = shift->SUPER::connect(@_); |
|
177 |
|
|
178 |
$self->include_model('MyModel4'); |
|
179 |
} |
|
180 |
} |
|
181 |
{ |
|
182 |
package MyDBI6; |
|
183 |
|
|
184 |
use base 'DBIx::Custom'; |
|
185 |
|
|
186 |
sub connect { |
|
187 |
my $self = shift->SUPER::connect(@_); |
|
188 |
|
|
189 |
$self->include_model('MyModel5'); |
|
190 |
|
|
191 |
return $self; |
|
192 |
} |
|
193 |
} |
|
194 |
{ |
|
195 |
package MyDBI7; |
|
196 |
|
|
197 |
use base 'DBIx::Custom'; |
|
198 |
|
|
199 |
sub connect { |
|
200 |
my $self = shift->SUPER::connect(@_); |
|
201 |
|
|
202 |
$self->include_model('MyModel6'); |
|
203 |
|
|
204 |
|
|
205 |
return $self; |
|
206 |
} |
|
207 |
} |
|
208 |
{ |
|
209 |
package MyDBI8; |
|
210 |
|
|
211 |
use base 'DBIx::Custom'; |
|
212 |
|
|
213 |
sub connect { |
|
214 |
my $self = shift->SUPER::connect(@_); |
|
215 |
|
|
216 |
$self->include_model('MyModel7'); |
|
217 |
|
|
218 |
return $self; |
|
219 |
} |
|
220 |
} |
|
221 | ||
222 |
{ |
|
223 |
package MyDBI9; |
|
224 |
|
|
225 |
use base 'DBIx::Custom'; |
|
226 |
|
|
227 |
sub connect { |
|
228 |
my $self = shift->SUPER::connect(@_); |
|
229 |
|
|
cleanup test
|
230 |
$self->include_model('MyModel8'); |
test cleanup
|
231 |
|
232 |
return $self; |
|
233 |
} |
|
234 |
} |
|
235 | ||
cleanup test
|
236 |
# Create table |
test cleanup
|
237 |
$dbi = DBIx::Custom->connect; |
test cleanup in progress
|
238 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
239 |
$dbi->execute($create_table1); |
240 |
$model = $dbi->create_model(table => $table1); |
|
241 |
$model->insert({$key1 => 1, $key2 => 2}); |
|
242 |
is_deeply($model->select->all, [{$key1 => 1, $key2 => 2}]); |
|
cleanup
|
243 | |
cleanup test
|
244 |
test 'DBIx::Custom::Result test'; |
245 |
$dbi->delete_all(table => $table1); |
|
246 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
|
247 |
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4}); |
|
248 |
$source = "select $key1, $key2 from $table1"; |
|
249 |
$query = $dbi->create_query($source); |
|
250 |
$result = $dbi->execute($query); |
|
cleanup
|
251 | |
cleanup test
|
252 |
@rows = (); |
253 |
while (my $row = $result->fetch) { |
|
254 |
push @rows, [@$row]; |
|
255 |
} |
|
256 |
is_deeply(\@rows, [[1, 2], [3, 4]], "fetch"); |
|
cleanup
|
257 | |
cleanup test
|
258 |
$result = $dbi->execute($query); |
259 |
@rows = (); |
|
260 |
while (my $row = $result->fetch_hash) { |
|
261 |
push @rows, {%$row}; |
|
262 |
} |
|
263 |
is_deeply(\@rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], "fetch_hash"); |
|
test cleanup
|
264 | |
cleanup test
|
265 |
$result = $dbi->execute($query); |
266 |
$rows = $result->fetch_all; |
|
267 |
is_deeply($rows, [[1, 2], [3, 4]], "fetch_all"); |
|
test cleanup
|
268 | |
cleanup test
|
269 |
$result = $dbi->execute($query); |
270 |
$rows = $result->fetch_hash_all; |
|
271 |
is_deeply($rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], "all"); |
|
test cleanup
|
272 | |
cleanup test
|
273 |
test 'Insert query return value'; |
274 |
$source = "insert into $table1 {insert_param $key1 $key2}"; |
|
275 |
$query = $dbi->execute($source, {}, query => 1); |
|
276 |
$ret_val = $dbi->execute($query, param => {$key1 => 1, $key2 => 2}); |
|
277 |
ok($ret_val); |
|
test cleanup
|
278 | |
cleanup test
|
279 |
test 'Direct query'; |
280 |
$dbi->delete_all(table => $table1); |
|
281 |
$insert_source = "insert into $table1 {insert_param $key1 $key2}"; |
|
282 |
$dbi->execute($insert_source, param => {$key1 => 1, $key2 => 2}); |
|
283 |
$result = $dbi->execute("select * from $table1"); |
|
284 |
$rows = $result->all; |
|
285 |
is_deeply($rows, [{$key1 => 1, $key2 => 2}]); |
|
cleanup test
|
286 | |
287 |
test 'Filter basic'; |
|
test cleanup in progress
|
288 |
$dbi->delete_all(table => $table1); |
cleanup test
|
289 |
$dbi->register_filter(twice => sub { $_[0] * 2}, |
290 |
three_times => sub { $_[0] * 3}); |
|
291 | ||
cleanup test
|
292 |
$insert_source = "insert into $table1 {insert_param $key1 $key2}"; |
cleanup test
|
293 |
$insert_query = $dbi->execute($insert_source, {}, query => 1); |
test cleanup in progress
|
294 |
$insert_query->filter({$key1 => 'twice'}); |
295 |
$dbi->execute($insert_query, param => {$key1 => 1, $key2 => 2}); |
|
cleanup test
|
296 |
$result = $dbi->execute("select * from $table1"); |
test cleanup in progress
|
297 |
$rows = $result->filter({$key2 => 'three_times'})->all; |
298 |
is_deeply($rows, [{$key1 => 2, $key2 => 6}], "filter fetch_filter"); |
|
cleanup test
|
299 | |
300 |
test 'Filter in'; |
|
test cleanup in progress
|
301 |
$dbi->delete_all(table => $table1); |
cleanup test
|
302 |
$insert_source = "insert into $table1 {insert_param $key1 $key2}"; |
cleanup test
|
303 |
$insert_query = $dbi->execute($insert_source, {}, query => 1); |
test cleanup in progress
|
304 |
$dbi->execute($insert_query, param => {$key1 => 2, $key2 => 4}); |
305 |
$select_source = "select * from $table1 where {in $table1.$key1 2} and {in $table1.$key2 2}"; |
|
cleanup test
|
306 |
$select_query = $dbi->execute($select_source,{}, query => 1); |
finishied sqlite test cleanu...
|
307 |
$select_query->filter({"$table1.$key1" => 'twice'}); |
test cleanup in progress
|
308 |
$result = $dbi->execute($select_query, param => {"$table1.$key1" => [1,5], "$table1.$key2" => [2,4]}); |
cleanup test
|
309 |
$rows = $result->all; |
test cleanup in progress
|
310 |
is_deeply($rows, [{$key1 => 2, $key2 => 4}], "filter"); |
cleanup test
|
311 | |
cleanup test
|
312 |
test 'DBIx::Custom::SQLTemplate basic tag'; |
finishied sqlite test cleanu...
|
313 |
$dbi->execute("drop table $table1"); |
cleanup test
|
314 |
$dbi->execute($create_table1_2); |
test cleanup in progress
|
315 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}); |
316 |
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10}); |
|
cleanup test
|
317 | |
cleanup test
|
318 |
$source = "select * from $table1 where $key1 = :$key1 and {<> $key2} and {< $key3} and {> $key4} and {>= $key5}"; |
cleanup test
|
319 |
$query = $dbi->execute($source, {}, query => 1); |
test cleanup in progress
|
320 |
$result = $dbi->execute($query, param => {$key1 => 1, $key2 => 3, $key3 => 4, $key4 => 3, $key5 => 5}); |
cleanup test
|
321 |
$rows = $result->all; |
test cleanup in progress
|
322 |
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}], "basic tag1"); |
cleanup test
|
323 | |
cleanup test
|
324 |
$source = "select * from $table1 where $key1 = :$key1 and {<> $key2} and {< $key3} and {> $key4} and {>= $key5}"; |
cleanup test
|
325 |
$query = $dbi->execute($source, {}, query => 1); |
test cleanup in progress
|
326 |
$result = $dbi->execute($query, {$key1 => 1, $key2 => 3, $key3 => 4, $key4 => 3, $key5 => 5}); |
cleanup test
|
327 |
$rows = $result->all; |
test cleanup in progress
|
328 |
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}], "basic tag1"); |
cleanup test
|
329 | |
cleanup test
|
330 |
$source = "select * from $table1 where {<= $key1} and {like $key2}"; |
cleanup test
|
331 |
$query = $dbi->execute($source, {}, query => 1); |
test cleanup in progress
|
332 |
$result = $dbi->execute($query, param => {$key1 => 1, $key2 => '%2%'}); |
cleanup test
|
333 |
$rows = $result->all; |
test cleanup in progress
|
334 |
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}], "basic tag2"); |
cleanup test
|
335 | |
336 |
test 'DIB::Custom::SQLTemplate in tag'; |
|
finishied sqlite test cleanu...
|
337 |
$dbi->execute("drop table $table1"); |
cleanup test
|
338 |
$dbi->execute($create_table1_2); |
test cleanup in progress
|
339 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}); |
340 |
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10}); |
|
cleanup test
|
341 | |
cleanup test
|
342 |
$source = "select * from $table1 where {in $key1 2}"; |
cleanup test
|
343 |
$query = $dbi->execute($source, {}, query => 1); |
test cleanup in progress
|
344 |
$result = $dbi->execute($query, param => {$key1 => [9, 1]}); |
cleanup test
|
345 |
$rows = $result->all; |
test cleanup in progress
|
346 |
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}], "basic"); |
cleanup test
|
347 | |
348 |
test 'DBIx::Custom::SQLTemplate insert tag'; |
|
test cleanup in progress
|
349 |
$dbi->delete_all(table => $table1); |
350 |
$insert_source = "insert into $table1 {insert_param $key1 $key2 $key3 $key4 $key5}"; |
|
351 |
$dbi->execute($insert_source, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}); |
|
cleanup test
|
352 | |
cleanup test
|
353 |
$result = $dbi->execute("select * from $table1"); |
cleanup test
|
354 |
$rows = $result->all; |
test cleanup in progress
|
355 |
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}], "basic"); |
cleanup test
|
356 | |
357 |
test 'DBIx::Custom::SQLTemplate update tag'; |
|
test cleanup in progress
|
358 |
$dbi->delete_all(table => $table1); |
359 |
$insert_source = "insert into $table1 {insert_param $key1 $key2 $key3 $key4 $key5}"; |
|
360 |
$dbi->execute($insert_source, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}); |
|
361 |
$dbi->execute($insert_source, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10}); |
|
cleanup test
|
362 | |
test cleanup in progress
|
363 |
$update_source = "update $table1 {update_param $key1 $key2 $key3 $key4} where {= $key5}"; |
364 |
$dbi->execute($update_source, param => {$key1 => 1, $key2 => 1, $key3 => 1, $key4 => 1, $key5 => 5}); |
|
cleanup test
|
365 | |
cleanup test
|
366 |
$result = $dbi->execute("select * from $table1 order by $key1"); |
cleanup test
|
367 |
$rows = $result->all; |
test cleanup in progress
|
368 |
is_deeply($rows, [{$key1 => 1, $key2 => 1, $key3 => 1, $key4 => 1, $key5 => 5}, |
369 |
{$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10}], "basic"); |
|
cleanup test
|
370 | |
cleanup test
|
371 |
test 'Named placeholder'; |
finishied sqlite test cleanu...
|
372 |
$dbi->execute("drop table $table1"); |
cleanup test
|
373 |
$dbi->execute($create_table1_2); |
test cleanup in progress
|
374 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}); |
375 |
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10}); |
|
cleanup test
|
376 | |
test cleanup in progress
|
377 |
$source = "select * from $table1 where $key1 = :$key1 and $key2 = :$key2"; |
378 |
$result = $dbi->execute($source, param => {$key1 => 1, $key2 => 2}); |
|
cleanup test
|
379 |
$rows = $result->all; |
test cleanup in progress
|
380 |
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}]); |
cleanup test
|
381 | |
test cleanup in progress
|
382 |
$source = "select * from $table1 where $key1 = \n:$key1\n and $key2 = :$key2"; |
383 |
$result = $dbi->execute($source, param => {$key1 => 1, $key2 => 2}); |
|
cleanup test
|
384 |
$rows = $result->all; |
test cleanup in progress
|
385 |
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}]); |
cleanup test
|
386 | |
test cleanup in progress
|
387 |
$source = "select * from $table1 where $key1 = :$key1 or $key1 = :$key1"; |
388 |
$result = $dbi->execute($source, param => {$key1 => [1, 2]}); |
|
cleanup test
|
389 |
$rows = $result->all; |
test cleanup in progress
|
390 |
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}]); |
cleanup test
|
391 | |
test cleanup in progress
|
392 |
$source = "select * from $table1 where $key1 = :$table1.$key1 and $key2 = :$table1.$key2"; |
cleanup test
|
393 |
$result = $dbi->execute( |
394 |
$source, |
|
test cleanup in progress
|
395 |
param => {"$table1.$key1" => 1, "$table1.$key2" => 1}, |
finishied sqlite test cleanu...
|
396 |
filter => {"$table1.$key2" => sub { $_[0] * 2 }} |
cleanup test
|
397 |
); |
398 |
$rows = $result->all; |
|
test cleanup in progress
|
399 |
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}]); |
cleanup test
|
400 | |
finishied sqlite test cleanu...
|
401 |
$dbi->execute("drop table $table1"); |
cleanup test
|
402 |
$dbi->execute($create_table1); |
test cleanup in progress
|
403 |
$dbi->insert(table => $table1, param => {$key1 => '2011-10-14 12:19:18', $key2 => 2}); |
404 |
$source = "select * from $table1 where $key1 = '2011-10-14 12:19:18' and $key2 = :$key2"; |
|
cleanup test
|
405 |
$result = $dbi->execute( |
406 |
$source, |
|
test cleanup in progress
|
407 |
param => {$key2 => 2}, |
cleanup test
|
408 |
); |
409 | ||
410 |
$rows = $result->all; |
|
test cleanup in progress
|
411 |
like($rows->[0]->{$key1}, qr/2011-10-14 12:19:18/); |
412 |
is($rows->[0]->{$key2}, 2); |
|
cleanup test
|
413 | |
test cleanup in progress
|
414 |
$dbi->delete_all(table => $table1); |
415 |
$dbi->insert(table => $table1, param => {$key1 => 'a:b c:d', $key2 => 2}); |
|
416 |
$source = "select * from $table1 where $key1 = 'a\\:b c\\:d' and $key2 = :$key2"; |
|
cleanup test
|
417 |
$result = $dbi->execute( |
418 |
$source, |
|
test cleanup in progress
|
419 |
param => {$key2 => 2}, |
cleanup test
|
420 |
); |
421 |
$rows = $result->all; |
|
test cleanup in progress
|
422 |
is_deeply($rows, [{$key1 => 'a:b c:d', $key2 => 2}]); |
cleanup test
|
423 | |
test cleanup
|
424 |
test 'Error case'; |
425 |
eval {DBIx::Custom->connect(dsn => 'dbi:SQLit')}; |
|
426 |
ok($@, "connect error"); |
|
427 | ||
428 |
eval{$dbi->execute("{p }", {}, query => 1)}; |
|
429 |
ok($@, "create_query invalid SQL template"); |
|
added EXPERIMENTAL DBIx::Cus...
|
430 | |
cleanup test
|
431 |
test 'insert'; |
finishied sqlite test cleanu...
|
432 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
433 |
$dbi->execute($create_table1); |
test cleanup in progress
|
434 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
435 |
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4}); |
|
cleanup test
|
436 |
$result = $dbi->execute("select * from $table1"); |
cleanup test
|
437 |
$rows = $result->all; |
test cleanup in progress
|
438 |
is_deeply($rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], "basic"); |
cleanup test
|
439 | |
finishied sqlite test cleanu...
|
440 |
$dbi->execute("delete from $table1"); |
cleanup test
|
441 |
$dbi->register_filter( |
442 |
twice => sub { $_[0] * 2 }, |
|
443 |
three_times => sub { $_[0] * 3 } |
|
444 |
); |
|
445 |
$dbi->default_bind_filter('twice'); |
|
test cleanup in progress
|
446 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}, filter => {$key1 => 'three_times'}); |
cleanup test
|
447 |
$result = $dbi->execute("select * from $table1"); |
cleanup test
|
448 |
$rows = $result->all; |
test cleanup in progress
|
449 |
is_deeply($rows, [{$key1 => 3, $key2 => 4}], "filter"); |
cleanup test
|
450 |
$dbi->default_bind_filter(undef); |
451 | ||
finishied sqlite test cleanu...
|
452 |
$dbi->execute("drop table $table1"); |
cleanup test
|
453 |
$dbi->execute($create_table1); |
test cleanup in progress
|
454 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}, append => ' '); |
455 |
$rows = $dbi->select(table => $table1)->all; |
|
456 |
is_deeply($rows, [{$key1 => 1, $key2 => 2}], 'insert append'); |
|
cleanup test
|
457 | |
test cleanup in progress
|
458 |
eval{$dbi->insert(table => $table1, noexist => 1)}; |
cleanup test
|
459 |
like($@, qr/noexist/, "invalid"); |
460 | ||
461 |
eval{$dbi->insert(table => 'table', param => {';' => 1})}; |
|
462 |
like($@, qr/safety/); |
|
463 | ||
cleanup test
|
464 |
eval { $dbi->execute("drop table ${q}table$p") }; |
test cleanup
|
465 |
$dbi->execute($create_table_reserved); |
cleanup test
|
466 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
467 |
$dbi->insert(table => 'table', param => {select => 1}); |
|
cleanup test
|
468 |
$result = $dbi->execute("select * from ${q}table$p"); |
cleanup test
|
469 |
$rows = $result->all; |
test cleanup
|
470 |
is_deeply($rows, [{select => 2, update => undef}], "reserved word"); |
cleanup test
|
471 | |
finishied sqlite test cleanu...
|
472 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
473 |
$dbi->execute($create_table1); |
test cleanup in progress
|
474 |
$dbi->insert({$key1 => 1, $key2 => 2}, table => $table1); |
475 |
$dbi->insert({$key1 => 3, $key2 => 4}, table => $table1); |
|
cleanup test
|
476 |
$result = $dbi->execute("select * from $table1"); |
cleanup test
|
477 |
$rows = $result->all; |
test cleanup in progress
|
478 |
is_deeply($rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], "basic"); |
cleanup test
|
479 | |
finishied sqlite test cleanu...
|
480 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
481 |
$dbi->execute($create_table1); |
test cleanup in progress
|
482 |
$dbi->insert(table => $table1, param => {$key1 => \"'1'", $key2 => 2}); |
483 |
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4}); |
|
cleanup test
|
484 |
$result = $dbi->execute("select * from $table1"); |
cleanup test
|
485 |
$rows = $result->all; |
test cleanup in progress
|
486 |
is_deeply($rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], "basic"); |
cleanup test
|
487 | |
test cleanup
|
488 |
test 'update'; |
finishied sqlite test cleanu...
|
489 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
490 |
$dbi->execute($create_table1_2); |
test cleanup in progress
|
491 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}); |
492 |
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10}); |
|
493 |
$dbi->update(table => $table1, param => {$key2 => 11}, where => {$key1 => 1}); |
|
cleanup test
|
494 |
$result = $dbi->execute("select * from $table1 order by $key1"); |
test cleanup
|
495 |
$rows = $result->all; |
test cleanup in progress
|
496 |
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5}, |
497 |
{$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10}], |
|
test cleanup
|
498 |
"basic"); |
499 |
|
|
test cleanup in progress
|
500 |
$dbi->execute("delete from $table1"); |
501 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}); |
|
502 |
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10}); |
|
503 |
$dbi->update(table => $table1, param => {$key2 => 12}, where => {$key2 => 2, $key3 => 3}); |
|
cleanup test
|
504 |
$result = $dbi->execute("select * from $table1 order by $key1"); |
test cleanup
|
505 |
$rows = $result->all; |
test cleanup in progress
|
506 |
is_deeply($rows, [{$key1 => 1, $key2 => 12, $key3 => 3, $key4 => 4, $key5 => 5}, |
507 |
{$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10}], |
|
test cleanup
|
508 |
"update key same as search key"); |
509 | ||
test cleanup in progress
|
510 |
$dbi->update(table => $table1, param => {$key2 => [12]}, where => {$key2 => 2, $key3 => 3}); |
cleanup test
|
511 |
$result = $dbi->execute("select * from $table1 order by $key1"); |
test cleanup
|
512 |
$rows = $result->all; |
test cleanup in progress
|
513 |
is_deeply($rows, [{$key1 => 1, $key2 => 12, $key3 => 3, $key4 => 4, $key5 => 5}, |
514 |
{$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10}], |
|
test cleanup
|
515 |
"update key same as search key : param is array ref"); |
516 | ||
test cleanup in progress
|
517 |
$dbi->execute("delete from $table1"); |
518 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}); |
|
519 |
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10}); |
|
test cleanup
|
520 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
test cleanup in progress
|
521 |
$dbi->update(table => $table1, param => {$key2 => 11}, where => {$key1 => 1}, |
522 |
filter => {$key2 => sub { $_[0] * 2 }}); |
|
cleanup test
|
523 |
$result = $dbi->execute("select * from $table1 order by $key1"); |
test cleanup
|
524 |
$rows = $result->all; |
test cleanup in progress
|
525 |
is_deeply($rows, [{$key1 => 1, $key2 => 22, $key3 => 3, $key4 => 4, $key5 => 5}, |
526 |
{$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10}], |
|
test cleanup
|
527 |
"filter"); |
528 | ||
test cleanup in progress
|
529 |
$result = $dbi->update(table => $table1, param => {$key2 => 11}, where => {$key1 => 1}, append => ' '); |
test cleanup
|
530 | |
test cleanup in progress
|
531 |
eval{$dbi->update(table => $table1, where => {$key1 => 1}, noexist => 1)}; |
test cleanup
|
532 |
like($@, qr/noexist/, "invalid"); |
533 | ||
test cleanup in progress
|
534 |
eval{$dbi->update(table => $table1)}; |
test cleanup
|
535 |
like($@, qr/where/, "not contain where"); |
536 | ||
finishied sqlite test cleanu...
|
537 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
538 |
$dbi->execute($create_table1); |
test cleanup in progress
|
539 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
test cleanup
|
540 |
$where = $dbi->where; |
test cleanup in progress
|
541 |
$where->clause(['and', "$key1 = :$key1", "$key2 = :$key2"]); |
542 |
$where->param({$key1 => 1, $key2 => 2}); |
|
543 |
$dbi->update(table => $table1, param => {$key1 => 3}, where => $where); |
|
544 |
$result = $dbi->select(table => $table1); |
|
545 |
is_deeply($result->all, [{$key1 => 3, $key2 => 2}], 'update() where'); |
|
test cleanup
|
546 | |
finishied sqlite test cleanu...
|
547 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
548 |
$dbi->execute($create_table1); |
test cleanup in progress
|
549 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
test cleanup
|
550 |
$dbi->update( |
test cleanup in progress
|
551 |
table => $table1, |
552 |
param => {$key1 => 3}, |
|
test cleanup
|
553 |
where => [ |
test cleanup in progress
|
554 |
['and', "$key1 = :$key1", "$key2 = :$key2"], |
555 |
{$key1 => 1, $key2 => 2} |
|
test cleanup
|
556 |
] |
557 |
); |
|
test cleanup in progress
|
558 |
$result = $dbi->select(table => $table1); |
559 |
is_deeply($result->all, [{$key1 => 3, $key2 => 2}], 'update() where'); |
|
test cleanup
|
560 | |
finishied sqlite test cleanu...
|
561 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
562 |
$dbi->execute($create_table1); |
test cleanup in progress
|
563 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
test cleanup
|
564 |
$where = $dbi->where; |
test cleanup in progress
|
565 |
$where->clause(['and', "$key2 = :$key2"]); |
566 |
$where->param({$key2 => 2}); |
|
567 |
$dbi->update(table => $table1, param => {$key1 => 3}, where => $where); |
|
568 |
$result = $dbi->select(table => $table1); |
|
569 |
is_deeply($result->all, [{$key1 => 3, $key2 => 2}], 'update() where'); |
|
test cleanup
|
570 | |
test cleanup in progress
|
571 |
eval{$dbi->update(table => $table1, param => {';' => 1})}; |
test cleanup
|
572 |
like($@, qr/safety/); |
573 | ||
test cleanup in progress
|
574 |
eval{$dbi->update(table => $table1, param => {$key1 => 1}, where => {';' => 1})}; |
test cleanup
|
575 |
like($@, qr/safety/); |
576 | ||
finishied sqlite test cleanu...
|
577 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
578 |
eval { $dbi->execute("drop table ${q}table$p") }; |
test cleanup
|
579 |
$dbi->execute($create_table_reserved); |
test cleanup
|
580 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
581 |
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}}); |
|
582 |
$dbi->insert(table => 'table', param => {select => 1}); |
|
583 |
$dbi->update(table => 'table', where => {select => 1}, param => {update => 2}); |
|
584 |
$result = $dbi->execute("select * from ${q}table$p"); |
|
585 |
$rows = $result->all; |
|
586 |
is_deeply($rows, [{select => 2, update => 6}], "reserved word"); |
|
587 | ||
588 |
eval {$dbi->update_all(table => 'table', param => {';' => 2}) }; |
|
589 |
like($@, qr/safety/); |
|
590 | ||
591 |
eval { $dbi->execute("drop table ${q}table$p") }; |
|
test cleanup
|
592 |
$dbi->execute($create_table_reserved); |
test cleanup
|
593 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
594 |
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}}); |
|
595 |
$dbi->insert(table => 'table', param => {select => 1}); |
|
596 |
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2}); |
|
597 |
$result = $dbi->execute("select * from ${q}table$p"); |
|
598 |
$rows = $result->all; |
|
599 |
is_deeply($rows, [{select => 2, update => 6}], "reserved word"); |
|
600 | ||
finishied sqlite test cleanu...
|
601 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
602 |
$dbi->execute($create_table1_2); |
test cleanup in progress
|
603 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}); |
604 |
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10}); |
|
605 |
$dbi->update({$key2 => 11}, table => $table1, where => {$key1 => 1}); |
|
cleanup test
|
606 |
$result = $dbi->execute("select * from $table1 order by $key1"); |
test cleanup
|
607 |
$rows = $result->all; |
test cleanup in progress
|
608 |
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5}, |
609 |
{$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10}], |
|
test cleanup
|
610 |
"basic"); |
611 | ||
finishied sqlite test cleanu...
|
612 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
613 |
$dbi->execute($create_table1_2); |
test cleanup in progress
|
614 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}); |
615 |
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10}); |
|
616 |
$dbi->update(table => $table1, param => {$key2 => \"'11'"}, where => {$key1 => 1}); |
|
cleanup test
|
617 |
$result = $dbi->execute("select * from $table1 order by $key1"); |
test cleanup
|
618 |
$rows = $result->all; |
test cleanup in progress
|
619 |
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5}, |
620 |
{$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10}], |
|
test cleanup
|
621 |
"basic"); |
622 | ||
623 |
test 'update_all'; |
|
finishied sqlite test cleanu...
|
624 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
625 |
$dbi->execute($create_table1_2); |
test cleanup in progress
|
626 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}); |
627 |
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10}); |
|
test cleanup
|
628 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
test cleanup in progress
|
629 |
$dbi->update_all(table => $table1, param => {$key2 => 10}, filter => {$key2 => 'twice'}); |
cleanup test
|
630 |
$result = $dbi->execute("select * from $table1"); |
test cleanup
|
631 |
$rows = $result->all; |
test cleanup in progress
|
632 |
is_deeply($rows, [{$key1 => 1, $key2 => 20, $key3 => 3, $key4 => 4, $key5 => 5}, |
633 |
{$key1 => 6, $key2 => 20, $key3 => 8, $key4 => 9, $key5 => 10}], |
|
test cleanup
|
634 |
"filter"); |
635 | ||
636 | ||
637 |
test 'delete'; |
|
finishied sqlite test cleanu...
|
638 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
639 |
$dbi->execute($create_table1); |
test cleanup in progress
|
640 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
641 |
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4}); |
|
642 |
$dbi->delete(table => $table1, where => {$key1 => 1}); |
|
cleanup test
|
643 |
$result = $dbi->execute("select * from $table1"); |
test cleanup
|
644 |
$rows = $result->all; |
test cleanup in progress
|
645 |
is_deeply($rows, [{$key1 => 3, $key2 => 4}], "basic"); |
test cleanup
|
646 | |
cleanup test
|
647 |
$dbi->execute("delete from $table1"); |
test cleanup in progress
|
648 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
649 |
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4}); |
|
test cleanup
|
650 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
test cleanup in progress
|
651 |
$dbi->delete(table => $table1, where => {$key2 => 1}, filter => {$key2 => 'twice'}); |
cleanup test
|
652 |
$result = $dbi->execute("select * from $table1"); |
test cleanup
|
653 |
$rows = $result->all; |
test cleanup in progress
|
654 |
is_deeply($rows, [{$key1 => 3, $key2 => 4}], "filter"); |
test cleanup
|
655 | |
test cleanup in progress
|
656 |
$dbi->delete(table => $table1, where => {$key1 => 1}, append => ' '); |
test cleanup
|
657 | |
test cleanup in progress
|
658 |
$dbi->delete_all(table => $table1); |
659 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
|
660 |
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4}); |
|
661 |
$dbi->delete(table => $table1, where => {$key1 => 1, $key2 => 2}); |
|
662 |
$rows = $dbi->select(table => $table1)->all; |
|
663 |
is_deeply($rows, [{$key1 => 3, $key2 => 4}], "delete multi key"); |
|
test cleanup
|
664 | |
test cleanup in progress
|
665 |
eval{$dbi->delete(table => $table1, where => {$key1 => 1}, noexist => 1)}; |
test cleanup
|
666 |
like($@, qr/noexist/, "invalid"); |
667 | ||
finishied sqlite test cleanu...
|
668 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
669 |
$dbi->execute($create_table1); |
test cleanup in progress
|
670 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
671 |
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4}); |
|
test cleanup
|
672 |
$where = $dbi->where; |
test cleanup in progress
|
673 |
$where->clause(['and', "$key1 = :$key1", "$key2 = :$key2"]); |
674 |
$where->param({ke1 => 1, $key2 => 2}); |
|
675 |
$dbi->delete(table => $table1, where => $where); |
|
676 |
$result = $dbi->select(table => $table1); |
|
677 |
is_deeply($result->all, [{$key1 => 3, $key2 => 4}], 'delete() where'); |
|
test cleanup
|
678 | |
finishied sqlite test cleanu...
|
679 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
680 |
$dbi->execute($create_table1); |
test cleanup in progress
|
681 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
682 |
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4}); |
|
test cleanup
|
683 |
$dbi->delete( |
test cleanup in progress
|
684 |
table => $table1, |
test cleanup
|
685 |
where => [ |
test cleanup in progress
|
686 |
['and', "$key1 = :$key1", "$key2 = :$key2"], |
687 |
{ke1 => 1, $key2 => 2} |
|
test cleanup
|
688 |
] |
689 |
); |
|
test cleanup in progress
|
690 |
$result = $dbi->select(table => $table1); |
691 |
is_deeply($result->all, [{$key1 => 3, $key2 => 4}], 'delete() where'); |
|
test cleanup
|
692 | |
finishied sqlite test cleanu...
|
693 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
694 |
$dbi->execute($create_table1); |
test cleanup in progress
|
695 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
696 |
$dbi->delete(table => $table1, where => {$key1 => 1}, prefix => ' '); |
|
cleanup test
|
697 |
$result = $dbi->execute("select * from $table1"); |
test cleanup
|
698 |
$rows = $result->all; |
699 |
is_deeply($rows, [], "basic"); |
|
700 | ||
701 |
test 'delete error'; |
|
finishied sqlite test cleanu...
|
702 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
703 |
$dbi->execute($create_table1); |
test cleanup in progress
|
704 |
eval{$dbi->delete(table => $table1)}; |
test cleanup
|
705 |
like($@, qr/"where" must be specified/, |
706 |
"where key-value pairs not specified"); |
|
707 | ||
test cleanup in progress
|
708 |
eval{$dbi->delete(table => $table1, where => {';' => 1})}; |
test cleanup
|
709 |
like($@, qr/safety/); |
710 | ||
test cleanup
|
711 |
$dbi = undef; |
test cleanup
|
712 |
$dbi = DBIx::Custom->connect; |
713 |
eval { $dbi->execute("drop table ${q}table$p") }; |
|
test cleanup
|
714 |
$dbi->execute($create_table_reserved); |
test cleanup
|
715 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
716 |
$dbi->insert(table => 'table', param => {select => 1}); |
|
717 |
$dbi->delete(table => 'table', where => {select => 1}); |
|
718 |
$result = $dbi->execute("select * from ${q}table$p"); |
|
719 |
$rows = $result->all; |
|
720 |
is_deeply($rows, [], "reserved word"); |
|
721 | ||
722 |
test 'delete_all'; |
|
finishied sqlite test cleanu...
|
723 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
724 |
$dbi->execute($create_table1); |
test cleanup in progress
|
725 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
726 |
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4}); |
|
727 |
$dbi->delete_all(table => $table1); |
|
cleanup test
|
728 |
$result = $dbi->execute("select * from $table1"); |
test cleanup
|
729 |
$rows = $result->all; |
730 |
is_deeply($rows, [], "basic"); |
|
731 | ||
732 | ||
733 |
test 'select'; |
|
finishied sqlite test cleanu...
|
734 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
735 |
$dbi->execute($create_table1); |
test cleanup in progress
|
736 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
737 |
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4}); |
|
738 |
$rows = $dbi->select(table => $table1)->all; |
|
739 |
is_deeply($rows, [{$key1 => 1, $key2 => 2}, |
|
740 |
{$key1 => 3, $key2 => 4}], "table"); |
|
test cleanup
|
741 | |
test cleanup in progress
|
742 |
$rows = $dbi->select(table => $table1, column => [$key1])->all; |
743 |
is_deeply($rows, [{$key1 => 1}, {$key1 => 3}], "table and columns and where key"); |
|
test cleanup
|
744 | |
test cleanup in progress
|
745 |
$rows = $dbi->select(table => $table1, where => {$key1 => 1})->all; |
746 |
is_deeply($rows, [{$key1 => 1, $key2 => 2}], "table and columns and where key"); |
|
test cleanup
|
747 | |
test cleanup in progress
|
748 |
$rows = $dbi->select(table => $table1, column => [$key1], where => {$key1 => 3})->all; |
749 |
is_deeply($rows, [{$key1 => 3}], "table and columns and where key"); |
|
test cleanup
|
750 | |
751 |
$dbi->register_filter(decrement => sub { $_[0] - 1 }); |
|
test cleanup in progress
|
752 |
$rows = $dbi->select(table => $table1, where => {$key1 => 2}, filter => {$key1 => 'decrement'}) |
test cleanup
|
753 |
->all; |
test cleanup in progress
|
754 |
is_deeply($rows, [{$key1 => 1, $key2 => 2}], "filter"); |
test cleanup
|
755 | |
test cleanup in progress
|
756 |
eval { $dbi->execute("drop table $table2") }; |
test cleanup
|
757 |
$dbi->execute($create_table2); |
test cleanup in progress
|
758 |
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5}); |
test cleanup
|
759 |
$rows = $dbi->select( |
test cleanup in progress
|
760 |
table => [$table1, $table2], |
761 |
column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3", |
|
762 |
where => {"$table1.$key2" => 2}, |
|
763 |
relation => {"$table1.$key1" => "$table2.$key1"} |
|
test cleanup
|
764 |
)->all; |
test cleanup in progress
|
765 |
is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}], "relation : exists where"); |
test cleanup
|
766 | |
767 |
$rows = $dbi->select( |
|
test cleanup in progress
|
768 |
table => [$table1, $table2], |
finishied sqlite test cleanu...
|
769 |
column => ["$table1.$key1 as ${table1}_$key1", "${table2}.$key1 as ${table2}_$key1", $key2, $key3], |
test cleanup in progress
|
770 |
relation => {"$table1.$key1" => "$table2.$key1"} |
test cleanup
|
771 |
)->all; |
test cleanup in progress
|
772 |
is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}], "relation : no exists where"); |
test cleanup
|
773 | |
test cleanup in progress
|
774 |
eval{$dbi->select(table => $table1, noexist => 1)}; |
test cleanup
|
775 |
like($@, qr/noexist/, "invalid"); |
776 | ||
777 |
$dbi = DBIx::Custom->connect; |
|
test cleanup
|
778 |
eval { $dbi->execute("drop table ${q}table$p") }; |
779 |
$dbi->execute($create_table_reserved); |
|
test cleanup
|
780 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
781 |
$dbi->insert(table => 'table', param => {select => 1, update => 2}); |
|
782 |
$result = $dbi->select(table => 'table', where => {select => 1}); |
|
783 |
$rows = $result->all; |
|
784 |
is_deeply($rows, [{select => 2, update => 2}], "reserved word"); |
|
785 | ||
786 |
test 'fetch filter'; |
|
finishied sqlite test cleanu...
|
787 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
788 |
$dbi->register_filter( |
789 |
twice => sub { $_[0] * 2 }, |
|
790 |
three_times => sub { $_[0] * 3 } |
|
791 |
); |
|
792 |
$dbi->default_fetch_filter('twice'); |
|
793 |
$dbi->execute($create_table1); |
|
test cleanup in progress
|
794 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
795 |
$result = $dbi->select(table => $table1); |
|
796 |
$result->filter({$key1 => 'three_times'}); |
|
test cleanup
|
797 |
$row = $result->one; |
test cleanup in progress
|
798 |
is_deeply($row, {$key1 => 3, $key2 => 4}, "default_fetch_filter and filter"); |
test cleanup
|
799 | |
800 |
test 'filters'; |
|
801 |
$dbi = DBIx::Custom->new; |
|
802 | ||
803 |
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')), |
|
804 |
'あ', "decode_utf8"); |
|
805 | ||
806 |
is($dbi->filters->{encode_utf8}->('あ'), |
|
807 |
encode_utf8('あ'), "encode_utf8"); |
|
808 | ||
cleanup test
|
809 |
test 'transaction1'; |
test cleanup
|
810 |
$dbi = DBIx::Custom->connect; |
finishied sqlite test cleanu...
|
811 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
812 |
$dbi->execute($create_table1); |
fixed transaction test
|
813 |
$dbi->begin_work; |
814 |
$dbi->dbh->{AutoCommit} = 0; |
|
test cleanup in progress
|
815 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
fixed transaction test
|
816 |
$dbi->rollback; |
817 |
$dbi->dbh->{AutoCommit} = 1; |
|
818 | ||
test cleanup in progress
|
819 |
$result = $dbi->select(table => $table1); |
fixed transaction test
|
820 |
ok(! $result->fetch_first, "rollback"); |
821 | ||
test cleanup
|
822 | |
823 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
824 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
825 |
$dbi->execute($create_table1); |
fixed transaction test
|
826 |
$dbi->begin_work; |
827 |
$dbi->dbh->{AutoCommit} = 0; |
|
test cleanup in progress
|
828 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
829 |
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3}); |
|
fixed transaction test
|
830 |
$dbi->commit; |
831 |
$dbi->dbh->{AutoCommit} = 1; |
|
test cleanup in progress
|
832 |
$result = $dbi->select(table => $table1); |
833 |
is_deeply(scalar $result->all, [{$key1 => 1, $key2 => 2}, {$key1 => 2, $key2 => 3}], |
|
fixed transaction test
|
834 |
"commit"); |
test cleanup
|
835 | |
836 |
test 'execute'; |
|
finishied sqlite test cleanu...
|
837 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
838 |
$dbi->execute($create_table1); |
839 |
{ |
|
840 |
local $Carp::Verbose = 0; |
|
finishied sqlite test cleanu...
|
841 |
eval{$dbi->execute("select * frm $table1")}; |
cleanup test
|
842 |
like($@, qr/\Qselect * frm $table1/, "fail prepare"); |
test cleanup
|
843 |
like($@, qr/\.t /, "fail : not verbose"); |
844 |
} |
|
845 |
{ |
|
846 |
local $Carp::Verbose = 1; |
|
finishied sqlite test cleanu...
|
847 |
eval{$dbi->execute("select * frm $table1")}; |
test cleanup
|
848 |
like($@, qr/Custom.*\.t /s, "fail : verbose"); |
849 |
} |
|
850 | ||
test cleanup in progress
|
851 |
eval{$dbi->execute('select * from $table1', no_exists => 1)}; |
test cleanup
|
852 |
like($@, qr/wrong/, "invald SQL"); |
853 | ||
test cleanup in progress
|
854 |
$query = $dbi->execute("select * from $table1 where $key1 = :$key1", {}, query => 1); |
test cleanup
|
855 |
$dbi->dbh->disconnect; |
test cleanup in progress
|
856 |
eval{$dbi->execute($query, param => {$key1 => {a => 1}})}; |
test cleanup
|
857 |
ok($@, "execute fail"); |
858 | ||
859 |
{ |
|
860 |
local $Carp::Verbose = 0; |
|
test cleanup in progress
|
861 |
eval{$dbi->execute("select * from $table1 where {0 $key1}", {}, query => 1)}; |
test cleanup
|
862 |
like($@, qr/\Q.t /, "caller spec : not vebose"); |
863 |
} |
|
864 |
{ |
|
865 |
local $Carp::Verbose = 1; |
|
test cleanup in progress
|
866 |
eval{$dbi->execute("select * from $table1 where {0 $key1}", {}, query => 1)}; |
test cleanup
|
867 |
like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose"); |
868 |
} |
|
869 | ||
870 | ||
cleanup test
|
871 |
test 'transaction2'; |
test cleanup
|
872 |
$dbi = DBIx::Custom->connect; |
finishied sqlite test cleanu...
|
873 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
874 |
$dbi->execute($create_table1); |
875 | ||
876 |
$dbi->begin_work; |
|
877 | ||
878 |
eval { |
|
test cleanup in progress
|
879 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
test cleanup
|
880 |
die "Error"; |
test cleanup in progress
|
881 |
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4}); |
test cleanup
|
882 |
}; |
883 | ||
884 |
$dbi->rollback if $@; |
|
885 | ||
test cleanup in progress
|
886 |
$result = $dbi->select(table => $table1); |
test cleanup
|
887 |
$rows = $result->all; |
888 |
is_deeply($rows, [], "rollback"); |
|
889 | ||
890 |
$dbi->begin_work; |
|
891 | ||
892 |
eval { |
|
test cleanup in progress
|
893 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
894 |
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4}); |
|
test cleanup
|
895 |
}; |
896 | ||
897 |
$dbi->commit unless $@; |
|
898 | ||
test cleanup in progress
|
899 |
$result = $dbi->select(table => $table1); |
test cleanup
|
900 |
$rows = $result->all; |
test cleanup in progress
|
901 |
is_deeply($rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], "commit"); |
test cleanup
|
902 | |
903 |
$dbi->dbh->{AutoCommit} = 0; |
|
904 |
eval{ $dbi->begin_work }; |
|
905 |
ok($@, "exception"); |
|
906 |
$dbi->dbh->{AutoCommit} = 1; |
|
added EXPERIMENTAL DBIx::Cus...
|
907 | |
test cleanup
|
908 |
test 'cache'; |
finishied sqlite test cleanu...
|
909 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
910 |
$dbi->cache(1); |
911 |
$dbi->execute($create_table1); |
|
cleanup test
|
912 |
$source = "select * from $table1 where $key1 = :$key1 and $key2 = :$key2"; |
test cleanup
|
913 |
$dbi->execute($source, {}, query => 1); |
914 |
is_deeply($dbi->{_cached}->{$source}, |
|
cleanup test
|
915 |
{sql => "select * from $table1 where $key1 = ? and $key2 = ?", columns => [$key1, $key2], tables => []}, "cache"); |
test cleanup
|
916 | |
finishied sqlite test cleanu...
|
917 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
918 |
$dbi->execute($create_table1); |
919 |
$dbi->{_cached} = {}; |
|
920 |
$dbi->cache(0); |
|
test cleanup in progress
|
921 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
test cleanup
|
922 |
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache'); |
923 | ||
924 |
test 'execute'; |
|
finishied sqlite test cleanu...
|
925 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
926 |
$dbi->execute($create_table1); |
927 |
{ |
|
928 |
local $Carp::Verbose = 0; |
|
finishied sqlite test cleanu...
|
929 |
eval{$dbi->execute("select * frm $table1")}; |
cleanup test
|
930 |
like($@, qr/\Qselect * frm $table1/, "fail prepare"); |
test cleanup
|
931 |
like($@, qr/\.t /, "fail : not verbose"); |
932 |
} |
|
933 |
{ |
|
934 |
local $Carp::Verbose = 1; |
|
test cleanup in progress
|
935 |
eval{$dbi->execute('select * frm $table1')}; |
test cleanup
|
936 |
like($@, qr/Custom.*\.t /s, "fail : verbose"); |
937 |
} |
|
938 | ||
test cleanup in progress
|
939 |
eval{$dbi->execute('select * from $table1', no_exists => 1)}; |
test cleanup
|
940 |
like($@, qr/wrong/, "invald SQL"); |
941 | ||
test cleanup in progress
|
942 |
$query = $dbi->execute("select * from $table1 where $key1 = :$key1", {}, query => 1); |
test cleanup
|
943 |
$dbi->dbh->disconnect; |
test cleanup in progress
|
944 |
eval{$dbi->execute($query, param => {$key1 => {a => 1}})}; |
test cleanup
|
945 |
ok($@, "execute fail"); |
946 | ||
947 |
{ |
|
948 |
local $Carp::Verbose = 0; |
|
test cleanup in progress
|
949 |
eval{$dbi->execute("select * from $table1 where {0 $key1}", {}, query => 1)}; |
test cleanup
|
950 |
like($@, qr/\Q.t /, "caller spec : not vebose"); |
951 |
} |
|
952 |
{ |
|
953 |
local $Carp::Verbose = 1; |
|
test cleanup in progress
|
954 |
eval{$dbi->execute("select * from $table1 where {0 $key1}", {}, query => 1)}; |
test cleanup
|
955 |
like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose"); |
956 |
} |
|
957 | ||
958 |
test 'method'; |
|
959 |
$dbi->method( |
|
960 |
one => sub { 1 } |
|
961 |
); |
|
962 |
$dbi->method( |
|
963 |
two => sub { 2 } |
|
964 |
); |
|
965 |
$dbi->method({ |
|
966 |
twice => sub { |
|
967 |
my $self = shift; |
|
968 |
return $_[0] * 2; |
|
969 |
} |
|
970 |
}); |
|
971 | ||
972 |
is($dbi->one, 1, "first"); |
|
973 |
is($dbi->two, 2, "second"); |
|
974 |
is($dbi->twice(5), 10 , "second"); |
|
975 | ||
976 |
eval {$dbi->XXXXXX}; |
|
977 |
ok($@, "not exists"); |
|
978 | ||
979 |
test 'out filter'; |
|
980 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
981 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
982 |
$dbi->execute($create_table1); |
983 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
984 |
$dbi->register_filter(three_times => sub { $_[0] * 3}); |
|
985 |
$dbi->apply_filter( |
|
test cleanup in progress
|
986 |
$table1, $key1 => {out => 'twice', in => 'three_times'}, |
987 |
$key2 => {out => 'three_times', in => 'twice'}); |
|
988 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
|
cleanup test
|
989 |
$result = $dbi->execute("select * from $table1"); |
test cleanup
|
990 |
$row = $result->fetch_hash_first; |
test cleanup in progress
|
991 |
is_deeply($row, {$key1 => 2, $key2 => 6}, "insert"); |
992 |
$result = $dbi->select(table => $table1); |
|
test cleanup
|
993 |
$row = $result->one; |
test cleanup in progress
|
994 |
is_deeply($row, {$key1 => 6, $key2 => 12}, "insert"); |
test cleanup
|
995 | |
996 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
997 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
998 |
$dbi->execute($create_table1); |
999 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
1000 |
$dbi->register_filter(three_times => sub { $_[0] * 3}); |
|
1001 |
$dbi->apply_filter( |
|
test cleanup in progress
|
1002 |
$table1, $key1 => {out => 'twice', in => 'three_times'}, |
1003 |
$key2 => {out => 'three_times', in => 'twice'}); |
|
test cleanup
|
1004 |
$dbi->apply_filter( |
test cleanup in progress
|
1005 |
$table1, $key1 => {out => undef} |
test cleanup
|
1006 |
); |
test cleanup in progress
|
1007 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
cleanup test
|
1008 |
$result = $dbi->execute("select * from $table1"); |
test cleanup
|
1009 |
$row = $result->one; |
test cleanup in progress
|
1010 |
is_deeply($row, {$key1 => 1, $key2 => 6}, "insert"); |
test cleanup
|
1011 | |
1012 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
1013 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
1014 |
$dbi->execute($create_table1); |
1015 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
1016 |
$dbi->apply_filter( |
|
test cleanup in progress
|
1017 |
$table1, $key1 => {out => 'twice', in => 'twice'} |
test cleanup
|
1018 |
); |
test cleanup in progress
|
1019 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}, filter => {$key1 => undef}); |
1020 |
$dbi->update(table => $table1, param => {$key1 => 2}, where => {$key2 => 2}); |
|
cleanup test
|
1021 |
$result = $dbi->execute("select * from $table1"); |
test cleanup
|
1022 |
$row = $result->one; |
test cleanup in progress
|
1023 |
is_deeply($row, {$key1 => 4, $key2 => 2}, "update"); |
test cleanup
|
1024 | |
1025 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
1026 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
1027 |
$dbi->execute($create_table1); |
1028 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
1029 |
$dbi->apply_filter( |
|
test cleanup in progress
|
1030 |
$table1, $key1 => {out => 'twice', in => 'twice'} |
test cleanup
|
1031 |
); |
test cleanup in progress
|
1032 |
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 2}, filter => {$key1=> undef}); |
1033 |
$dbi->delete(table => $table1, where => {$key1 => 1}); |
|
cleanup test
|
1034 |
$result = $dbi->execute("select * from $table1"); |
test cleanup
|
1035 |
$rows = $result->all; |
1036 |
is_deeply($rows, [], "delete"); |
|
1037 | ||
1038 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
1039 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
1040 |
$dbi->execute($create_table1); |
1041 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
1042 |
$dbi->apply_filter( |
|
test cleanup in progress
|
1043 |
$table1, $key1 => {out => 'twice', in => 'twice'} |
test cleanup
|
1044 |
); |
test cleanup in progress
|
1045 |
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 2}, filter => {$key1 => undef}); |
1046 |
$result = $dbi->select(table => $table1, where => {$key1 => 1}); |
|
1047 |
$result->filter({$key2 => 'twice'}); |
|
test cleanup
|
1048 |
$rows = $result->all; |
test cleanup in progress
|
1049 |
is_deeply($rows, [{$key1 => 4, $key2 => 4}], "select"); |
test cleanup
|
1050 | |
1051 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
1052 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
1053 |
$dbi->execute($create_table1); |
1054 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
1055 |
$dbi->apply_filter( |
|
test cleanup in progress
|
1056 |
$table1, $key1 => {out => 'twice', in => 'twice'} |
test cleanup
|
1057 |
); |
test cleanup in progress
|
1058 |
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 2}, filter => {$key1 => undef}); |
cleanup test
|
1059 |
$result = $dbi->execute("select * from $table1 where $key1 = :$key1 and $key2 = :$key2", |
test cleanup in progress
|
1060 |
param => {$key1 => 1, $key2 => 2}, |
1061 |
table => [$table1]); |
|
test cleanup
|
1062 |
$rows = $result->all; |
test cleanup in progress
|
1063 |
is_deeply($rows, [{$key1 => 4, $key2 => 2}], "execute"); |
test cleanup
|
1064 | |
1065 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
1066 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
1067 |
$dbi->execute($create_table1); |
1068 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
1069 |
$dbi->apply_filter( |
|
test cleanup in progress
|
1070 |
$table1, $key1 => {out => 'twice', in => 'twice'} |
test cleanup
|
1071 |
); |
test cleanup in progress
|
1072 |
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 2}, filter => {$key1 => undef}); |
cleanup test
|
1073 |
$result = $dbi->execute("select * from {table $table1} where $key1 = :$key1 and $key2 = :$key2", |
test cleanup in progress
|
1074 |
param => {$key1 => 1, $key2 => 2}); |
test cleanup
|
1075 |
$rows = $result->all; |
test cleanup in progress
|
1076 |
is_deeply($rows, [{$key1 => 4, $key2 => 2}], "execute table tag"); |
test cleanup
|
1077 | |
1078 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
1079 |
eval { $dbi->execute("drop table $table1") }; |
1080 |
eval { $dbi->execute("drop table $table2") }; |
|
test cleanup
|
1081 |
$dbi->execute($create_table1); |
1082 |
$dbi->execute($create_table2); |
|
1083 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
1084 |
$dbi->register_filter(three_times => sub { $_[0] * 3 }); |
|
1085 |
$dbi->apply_filter( |
|
cleanup test
|
1086 |
$table1, $key2 => {out => 'twice', in => 'twice'} |
1087 |
); |
|
1088 |
$dbi->apply_filter( |
|
1089 |
$table2, $key3 => {out => 'three_times', in => 'three_times'} |
|
test cleanup
|
1090 |
); |
cleanup test
|
1091 |
$dbi->insert(table => $table1, param => {$key1 => 5, $key2 => 2}, filter => {$key2 => undef}); |
1092 |
$dbi->insert(table => $table2, param => {$key1 => 5, $key3 => 6}, filter => {$key3 => undef}); |
|
1093 |
$result = $dbi->select( |
|
1094 |
table => [$table1, $table2], |
|
1095 |
column => [$key2, $key3], |
|
1096 |
where => {"$table1.$key2" => 1, "$table2.$key3" => 2}, relation => {"$table1.$key1" => "$table2.$key1"}); |
|
1097 | ||
1098 |
$result->filter({$key2 => 'twice'}); |
|
1099 |
$rows = $result->all; |
|
1100 |
is_deeply($rows, [{$key2 => 4, $key3 => 18}], "select : join"); |
|
1101 | ||
1102 |
$result = $dbi->select( |
|
1103 |
table => [$table1, $table2], |
|
1104 |
column => [$key2, $key3], |
|
1105 |
where => {$key2 => 1, $key3 => 2}, relation => {"$table1.$key1" => "$table2.$key1"}); |
|
1106 | ||
1107 |
$result->filter({$key2 => 'twice'}); |
|
1108 |
$rows = $result->all; |
|
1109 |
is_deeply($rows, [{$key2 => 4, $key3 => 18}], "select : join : omit"); |
|
test cleanup
|
1110 | |
1111 |
test 'connect super'; |
|
test cleanup
|
1112 |
$dbi = DBIx::Custom->connect; |
finishied sqlite test cleanu...
|
1113 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
1114 |
$dbi->execute($create_table1); |
test cleanup in progress
|
1115 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
1116 |
is($dbi->select(table => $table1)->one->{$key1}, 1); |
|
test cleanup
|
1117 | |
test cleanup
|
1118 |
$dbi = DBIx::Custom->new; |
test cleanup
|
1119 |
$dbi->connect; |
finishied sqlite test cleanu...
|
1120 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
1121 |
$dbi->execute($create_table1); |
test cleanup in progress
|
1122 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
1123 |
is($dbi->select(table => $table1)->one->{$key1}, 1); |
|
test cleanup
|
1124 | |
test cleanup
|
1125 |
$dbi = DBIx::Custom->connect; |
finishied sqlite test cleanu...
|
1126 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
1127 |
$dbi->execute($create_table1); |
test cleanup in progress
|
1128 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
1129 |
is($dbi->select(table => $table1)->one->{$key1}, 1); |
|
test cleanup
|
1130 | |
1131 |
test 'end_filter'; |
|
1132 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
1133 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
1134 |
$dbi->execute($create_table1); |
test cleanup in progress
|
1135 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
1136 |
$result = $dbi->select(table => $table1); |
|
1137 |
$result->filter($key1 => sub { $_[0] * 2 }, $key2 => sub { $_[0] * 4 }); |
|
1138 |
$result->end_filter($key1 => sub { $_[0] * 3 }, $key2 => sub { $_[0] * 5 }); |
|
test cleanup
|
1139 |
$row = $result->fetch_first; |
1140 |
is_deeply($row, [6, 40]); |
|
1141 | ||
1142 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
1143 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
1144 |
$dbi->execute($create_table1); |
test cleanup in progress
|
1145 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
1146 |
$result = $dbi->select(table => $table1); |
|
finishied sqlite test cleanu...
|
1147 |
$result->filter([$key1, $key2] => sub { $_[0] * 2 }); |
1148 |
$result->end_filter([[$key1, $key2] => sub { $_[0] * 3 }]); |
|
test cleanup
|
1149 |
$row = $result->fetch_first; |
1150 |
is_deeply($row, [6, 12]); |
|
1151 | ||
1152 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
1153 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
1154 |
$dbi->execute($create_table1); |
test cleanup in progress
|
1155 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
1156 |
$result = $dbi->select(table => $table1); |
|
finishied sqlite test cleanu...
|
1157 |
$result->filter([[$key1, $key2] => sub { $_[0] * 2 }]); |
1158 |
$result->end_filter([$key1, $key2] => sub { $_[0] * 3 }); |
|
test cleanup
|
1159 |
$row = $result->fetch_first; |
1160 |
is_deeply($row, [6, 12]); |
|
1161 | ||
1162 |
$dbi->register_filter(five_times => sub { $_[0] * 5 }); |
|
test cleanup in progress
|
1163 |
$result = $dbi->select(table => $table1); |
1164 |
$result->filter($key1 => sub { $_[0] * 2 }, $key2 => sub { $_[0] * 4 }); |
|
1165 |
$result->end_filter({$key1 => sub { $_[0] * 3 }, $key2 => 'five_times' }); |
|
test cleanup
|
1166 |
$row = $result->one; |
test cleanup in progress
|
1167 |
is_deeply($row, {$key1 => 6, $key2 => 40}); |
test cleanup
|
1168 | |
1169 |
$dbi->register_filter(five_times => sub { $_[0] * 5 }); |
|
test cleanup in progress
|
1170 |
$dbi->apply_filter($table1, |
1171 |
$key1 => {end => sub { $_[0] * 3 } }, |
|
1172 |
$key2 => {end => 'five_times'} |
|
test cleanup
|
1173 |
); |
test cleanup in progress
|
1174 |
$result = $dbi->select(table => $table1); |
1175 |
$result->filter($key1 => sub { $_[0] * 2 }, $key2 => sub { $_[0] * 4 }); |
|
test cleanup
|
1176 |
$row = $result->one; |
test cleanup in progress
|
1177 |
is_deeply($row, {$key1 => 6, $key2 => 40}, 'apply_filter'); |
test cleanup
|
1178 | |
1179 |
$dbi->register_filter(five_times => sub { $_[0] * 5 }); |
|
test cleanup in progress
|
1180 |
$dbi->apply_filter($table1, |
1181 |
$key1 => {end => sub { $_[0] * 3 } }, |
|
1182 |
$key2 => {end => 'five_times'} |
|
1183 |
); |
|
1184 |
$result = $dbi->select(table => $table1); |
|
1185 |
$result->filter($key1 => sub { $_[0] * 2 }, $key2 => sub { $_[0] * 4 }); |
|
1186 |
$result->filter($key1 => undef); |
|
1187 |
$result->end_filter($key1 => undef); |
|
test cleanup
|
1188 |
$row = $result->one; |
test cleanup in progress
|
1189 |
is_deeply($row, {$key1 => 1, $key2 => 40}, 'apply_filter overwrite'); |
test cleanup
|
1190 | |
test cleanup
|
1191 |
test 'remove_end_filter and remove_filter'; |
1192 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
1193 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
1194 |
$dbi->execute($create_table1); |
test cleanup in progress
|
1195 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
1196 |
$result = $dbi->select(table => $table1); |
|
test cleanup
|
1197 |
$row = $result |
test cleanup in progress
|
1198 |
->filter($key1 => sub { $_[0] * 2 }, $key2 => sub { $_[0] * 4 }) |
test cleanup
|
1199 |
->remove_filter |
test cleanup in progress
|
1200 |
->end_filter($key1 => sub { $_[0] * 3 }, $key2 => sub { $_[0] * 5 }) |
test cleanup
|
1201 |
->remove_end_filter |
1202 |
->fetch_first; |
|
1203 |
is_deeply($row, [1, 2]); |
|
1204 | ||
1205 |
test 'empty where select'; |
|
1206 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
1207 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
1208 |
$dbi->execute($create_table1); |
test cleanup in progress
|
1209 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
1210 |
$result = $dbi->select(table => $table1, where => {}); |
|
test cleanup
|
1211 |
$row = $result->one; |
test cleanup in progress
|
1212 |
is_deeply($row, {$key1 => 1, $key2 => 2}); |
test cleanup
|
1213 | |
1214 |
test 'select query option'; |
|
1215 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
1216 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
1217 |
$dbi->execute($create_table1); |
test cleanup in progress
|
1218 |
$query = $dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}, query => 1); |
test cleanup
|
1219 |
is(ref $query, 'DBIx::Custom::Query'); |
test cleanup in progress
|
1220 |
$query = $dbi->update(table => $table1, where => {$key1 => 1}, param => {$key2 => 2}, query => 1); |
test cleanup
|
1221 |
is(ref $query, 'DBIx::Custom::Query'); |
test cleanup in progress
|
1222 |
$query = $dbi->delete(table => $table1, where => {$key1 => 1}, query => 1); |
test cleanup
|
1223 |
is(ref $query, 'DBIx::Custom::Query'); |
test cleanup in progress
|
1224 |
$query = $dbi->select(table => $table1, where => {$key1 => 1, $key2 => 2}, query => 1); |
test cleanup
|
1225 |
is(ref $query, 'DBIx::Custom::Query'); |
1226 | ||
1227 |
test 'where'; |
|
1228 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
1229 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
1230 |
$dbi->execute($create_table1); |
test cleanup in progress
|
1231 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
1232 |
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4}); |
|
1233 |
$where = $dbi->where->clause(['and', "$key1 = :$key1", "$key2 = :$key2"]); |
|
1234 |
is("$where", "where ( $key1 = :$key1 and $key2 = :$key2 )", 'no param'); |
|
test cleanup
|
1235 | |
1236 |
$where = $dbi->where |
|
test cleanup in progress
|
1237 |
->clause(['and', "$key1 = :$key1", "$key2 = :$key2"]) |
1238 |
->param({$key1 => 1}); |
|
test cleanup
|
1239 | |
1240 |
$result = $dbi->select( |
|
test cleanup in progress
|
1241 |
table => $table1, |
test cleanup
|
1242 |
where => $where |
1243 |
); |
|
1244 |
$row = $result->all; |
|
test cleanup in progress
|
1245 |
is_deeply($row, [{$key1 => 1, $key2 => 2}]); |
test cleanup
|
1246 | |
1247 |
$result = $dbi->select( |
|
test cleanup in progress
|
1248 |
table => $table1, |
test cleanup
|
1249 |
where => [ |
test cleanup in progress
|
1250 |
['and', "$key1 = :$key1", "$key2 = :$key2"], |
1251 |
{$key1 => 1} |
|
test cleanup
|
1252 |
] |
1253 |
); |
|
1254 |
$row = $result->all; |
|
test cleanup in progress
|
1255 |
is_deeply($row, [{$key1 => 1, $key2 => 2}]); |
test cleanup
|
1256 | |
1257 |
$where = $dbi->where |
|
test cleanup in progress
|
1258 |
->clause(['and', "$key1 = :$key1", "$key2 = :$key2"]) |
1259 |
->param({$key1 => 1, $key2 => 2}); |
|
test cleanup
|
1260 |
$result = $dbi->select( |
test cleanup in progress
|
1261 |
table => $table1, |
test cleanup
|
1262 |
where => $where |
1263 |
); |
|
1264 |
$row = $result->all; |
|
test cleanup in progress
|
1265 |
is_deeply($row, [{$key1 => 1, $key2 => 2}]); |
test cleanup
|
1266 | |
1267 |
$where = $dbi->where |
|
test cleanup in progress
|
1268 |
->clause(['and', "$key1 = :$key1", "$key2 = :$key2"]) |
test cleanup
|
1269 |
->param({}); |
1270 |
$result = $dbi->select( |
|
test cleanup in progress
|
1271 |
table => $table1, |
test cleanup
|
1272 |
where => $where, |
1273 |
); |
|
1274 |
$row = $result->all; |
|
test cleanup in progress
|
1275 |
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]); |
test cleanup
|
1276 | |
1277 |
$where = $dbi->where |
|
test cleanup in progress
|
1278 |
->clause(['and', ['or', "$key1 > :$key1", "$key1 < :$key1"], "$key2 = :$key2"]) |
1279 |
->param({$key1 => [0, 3], $key2 => 2}); |
|
test cleanup
|
1280 |
$result = $dbi->select( |
test cleanup in progress
|
1281 |
table => $table1, |
test cleanup
|
1282 |
where => $where, |
1283 |
); |
|
1284 |
$row = $result->all; |
|
test cleanup in progress
|
1285 |
is_deeply($row, [{$key1 => 1, $key2 => 2}]); |
test cleanup
|
1286 | |
1287 |
$where = $dbi->where; |
|
1288 |
$result = $dbi->select( |
|
test cleanup in progress
|
1289 |
table => $table1, |
test cleanup
|
1290 |
where => $where |
1291 |
); |
|
1292 |
$row = $result->all; |
|
test cleanup in progress
|
1293 |
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]); |
test cleanup
|
1294 | |
1295 |
eval { |
|
1296 |
$where = $dbi->where |
|
1297 |
->clause(['uuu']); |
|
1298 |
$result = $dbi->select( |
|
test cleanup in progress
|
1299 |
table => $table1, |
test cleanup
|
1300 |
where => $where |
1301 |
); |
|
1302 |
}; |
|
1303 |
ok($@); |
|
1304 | ||
1305 |
$where = $dbi->where; |
|
1306 |
is("$where", ''); |
|
1307 | ||
1308 |
$where = $dbi->where |
|
test cleanup in progress
|
1309 |
->clause(['or', ("$key1 = :$key1") x 2]) |
1310 |
->param({$key1 => [1, 3]}); |
|
test cleanup
|
1311 |
$result = $dbi->select( |
test cleanup in progress
|
1312 |
table => $table1, |
test cleanup
|
1313 |
where => $where, |
1314 |
); |
|
1315 |
$row = $result->all; |
|
test cleanup in progress
|
1316 |
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]); |
test cleanup
|
1317 | |
1318 |
$where = $dbi->where |
|
test cleanup in progress
|
1319 |
->clause(['or', ("$key1 = :$key1") x 2]) |
1320 |
->param({$key1 => [1]}); |
|
test cleanup
|
1321 |
$result = $dbi->select( |
test cleanup in progress
|
1322 |
table => $table1, |
test cleanup
|
1323 |
where => $where, |
1324 |
); |
|
1325 |
$row = $result->all; |
|
test cleanup in progress
|
1326 |
is_deeply($row, [{$key1 => 1, $key2 => 2}]); |
test cleanup
|
1327 | |
1328 |
$where = $dbi->where |
|
test cleanup in progress
|
1329 |
->clause(['or', ("$key1 = :$key1") x 2]) |
1330 |
->param({$key1 => 1}); |
|
test cleanup
|
1331 |
$result = $dbi->select( |
test cleanup in progress
|
1332 |
table => $table1, |
test cleanup
|
1333 |
where => $where, |
1334 |
); |
|
1335 |
$row = $result->all; |
|
test cleanup in progress
|
1336 |
is_deeply($row, [{$key1 => 1, $key2 => 2}]); |
test cleanup
|
1337 | |
1338 |
$where = $dbi->where |
|
finishied sqlite test cleanu...
|
1339 |
->clause("$key1 = :$key1") |
test cleanup in progress
|
1340 |
->param({$key1 => 1}); |
test cleanup
|
1341 |
$result = $dbi->select( |
test cleanup in progress
|
1342 |
table => $table1, |
test cleanup
|
1343 |
where => $where, |
1344 |
); |
|
1345 |
$row = $result->all; |
|
test cleanup in progress
|
1346 |
is_deeply($row, [{$key1 => 1, $key2 => 2}]); |
test cleanup
|
1347 | |
1348 |
$where = $dbi->where |
|
test cleanup in progress
|
1349 |
->clause("$key1 = :$key1 $key2 = :$key2") |
1350 |
->param({$key1 => 1}); |
|
test cleanup
|
1351 |
eval{$where->to_string}; |
1352 |
like($@, qr/one column/); |
|
1353 | ||
1354 |
$where = $dbi->where |
|
test cleanup in progress
|
1355 |
->clause(['or', ("$key1 = :$key1") x 3]) |
1356 |
->param({$key1 => [$dbi->not_exists, 1, 3]}); |
|
test cleanup
|
1357 |
$result = $dbi->select( |
test cleanup in progress
|
1358 |
table => $table1, |
test cleanup
|
1359 |
where => $where, |
1360 |
); |
|
1361 |
$row = $result->all; |
|
test cleanup in progress
|
1362 |
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], 'not_exists'); |
test cleanup
|
1363 | |
1364 |
$where = $dbi->where |
|
test cleanup in progress
|
1365 |
->clause(['or', ("$key1 = :$key1") x 3]) |
1366 |
->param({$key1 => [1, $dbi->not_exists, 3]}); |
|
test cleanup
|
1367 |
$result = $dbi->select( |
test cleanup in progress
|
1368 |
table => $table1, |
test cleanup
|
1369 |
where => $where, |
1370 |
); |
|
1371 |
$row = $result->all; |
|
test cleanup in progress
|
1372 |
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], 'not_exists'); |
test cleanup
|
1373 | |
1374 |
$where = $dbi->where |
|
test cleanup in progress
|
1375 |
->clause(['or', ("$key1 = :$key1") x 3]) |
1376 |
->param({$key1 => [1, 3, $dbi->not_exists]}); |
|
test cleanup
|
1377 |
$result = $dbi->select( |
test cleanup in progress
|
1378 |
table => $table1, |
test cleanup
|
1379 |
where => $where, |
1380 |
); |
|
1381 |
$row = $result->all; |
|
test cleanup in progress
|
1382 |
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], 'not_exists'); |
test cleanup
|
1383 | |
1384 |
$where = $dbi->where |
|
test cleanup in progress
|
1385 |
->clause(['or', ("$key1 = :$key1") x 3]) |
1386 |
->param({$key1 => [1, $dbi->not_exists, $dbi->not_exists]}); |
|
test cleanup
|
1387 |
$result = $dbi->select( |
test cleanup in progress
|
1388 |
table => $table1, |
test cleanup
|
1389 |
where => $where, |
1390 |
); |
|
1391 |
$row = $result->all; |
|
test cleanup in progress
|
1392 |
is_deeply($row, [{$key1 => 1, $key2 => 2}], 'not_exists'); |
test cleanup
|
1393 | |
1394 |
$where = $dbi->where |
|
test cleanup in progress
|
1395 |
->clause(['or', ("$key1 = :$key1") x 3]) |
1396 |
->param({$key1 => [$dbi->not_exists, 1, $dbi->not_exists]}); |
|
test cleanup
|
1397 |
$result = $dbi->select( |
test cleanup in progress
|
1398 |
table => $table1, |
test cleanup
|
1399 |
where => $where, |
1400 |
); |
|
1401 |
$row = $result->all; |
|
test cleanup in progress
|
1402 |
is_deeply($row, [{$key1 => 1, $key2 => 2}], 'not_exists'); |
test cleanup
|
1403 | |
1404 |
$where = $dbi->where |
|
test cleanup in progress
|
1405 |
->clause(['or', ("$key1 = :$key1") x 3]) |
1406 |
->param({$key1 => [$dbi->not_exists, $dbi->not_exists, 1]}); |
|
test cleanup
|
1407 |
$result = $dbi->select( |
test cleanup in progress
|
1408 |
table => $table1, |
test cleanup
|
1409 |
where => $where, |
1410 |
); |
|
1411 |
$row = $result->all; |
|
test cleanup in progress
|
1412 |
is_deeply($row, [{$key1 => 1, $key2 => 2}], 'not_exists'); |
test cleanup
|
1413 | |
1414 |
$where = $dbi->where |
|
test cleanup in progress
|
1415 |
->clause(['or', ("$key1 = :$key1") x 3]) |
1416 |
->param({$key1 => [$dbi->not_exists, $dbi->not_exists, $dbi->not_exists]}); |
|
test cleanup
|
1417 |
$result = $dbi->select( |
test cleanup in progress
|
1418 |
table => $table1, |
test cleanup
|
1419 |
where => $where, |
1420 |
); |
|
1421 |
$row = $result->all; |
|
test cleanup in progress
|
1422 |
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], 'not_exists'); |
test cleanup
|
1423 | |
1424 |
$where = $dbi->where |
|
test cleanup in progress
|
1425 |
->clause(['or', ("$key1 = :$key1") x 3]) |
1426 |
->param({$key1 => []}); |
|
test cleanup
|
1427 |
$result = $dbi->select( |
test cleanup in progress
|
1428 |
table => $table1, |
test cleanup
|
1429 |
where => $where, |
1430 |
); |
|
1431 |
$row = $result->all; |
|
test cleanup in progress
|
1432 |
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], 'not_exists'); |
test cleanup
|
1433 | |
1434 |
$where = $dbi->where |
|
test cleanup in progress
|
1435 |
->clause(['and', "{> $key1}", "{< $key1}" ]) |
1436 |
->param({$key1 => [2, $dbi->not_exists]}); |
|
test cleanup
|
1437 |
$result = $dbi->select( |
test cleanup in progress
|
1438 |
table => $table1, |
test cleanup
|
1439 |
where => $where, |
1440 |
); |
|
1441 |
$row = $result->all; |
|
test cleanup in progress
|
1442 |
is_deeply($row, [{$key1 => 3, $key2 => 4}], 'not_exists'); |
test cleanup
|
1443 | |
1444 |
$where = $dbi->where |
|
test cleanup in progress
|
1445 |
->clause(['and', "{> $key1}", "{< $key1}" ]) |
1446 |
->param({$key1 => [$dbi->not_exists, 2]}); |
|
test cleanup
|
1447 |
$result = $dbi->select( |
test cleanup in progress
|
1448 |
table => $table1, |
test cleanup
|
1449 |
where => $where, |
1450 |
); |
|
1451 |
$row = $result->all; |
|
test cleanup in progress
|
1452 |
is_deeply($row, [{$key1 => 1, $key2 => 2}], 'not_exists'); |
test cleanup
|
1453 | |
1454 |
$where = $dbi->where |
|
test cleanup in progress
|
1455 |
->clause(['and', "{> $key1}", "{< $key1}" ]) |
1456 |
->param({$key1 => [$dbi->not_exists, $dbi->not_exists]}); |
|
test cleanup
|
1457 |
$result = $dbi->select( |
test cleanup in progress
|
1458 |
table => $table1, |
test cleanup
|
1459 |
where => $where, |
1460 |
); |
|
1461 |
$row = $result->all; |
|
test cleanup in progress
|
1462 |
is_deeply($row, [{$key1 => 1, $key2 => 2},{$key1 => 3, $key2 => 4}], 'not_exists'); |
test cleanup
|
1463 | |
1464 |
$where = $dbi->where |
|
test cleanup in progress
|
1465 |
->clause(['and', "{> $key1}", "{< $key1}" ]) |
1466 |
->param({$key1 => [0, 2]}); |
|
test cleanup
|
1467 |
$result = $dbi->select( |
test cleanup in progress
|
1468 |
table => $table1, |
test cleanup
|
1469 |
where => $where, |
1470 |
); |
|
1471 |
$row = $result->all; |
|
test cleanup in progress
|
1472 |
is_deeply($row, [{$key1 => 1, $key2 => 2}], 'not_exists'); |
test cleanup
|
1473 | |
1474 |
$where = $dbi->where |
|
test cleanup in progress
|
1475 |
->clause(['and',"$key1 is not null", "$key2 is not null" ]); |
test cleanup
|
1476 |
$result = $dbi->select( |
test cleanup in progress
|
1477 |
table => $table1, |
test cleanup
|
1478 |
where => $where, |
1479 |
); |
|
1480 |
$row = $result->all; |
|
test cleanup in progress
|
1481 |
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}], 'not_exists'); |
test cleanup
|
1482 | |
1483 |
eval {$dbi->where(ppp => 1) }; |
|
1484 |
like($@, qr/invalid/); |
|
1485 | ||
1486 |
$where = $dbi->where( |
|
test cleanup in progress
|
1487 |
clause => ['and', ['or'], ['and', "$key1 = :$key1", "$key2 = :$key2"]], |
1488 |
param => {$key1 => 1, $key2 => 2} |
|
test cleanup
|
1489 |
); |
1490 |
$result = $dbi->select( |
|
test cleanup in progress
|
1491 |
table => $table1, |
test cleanup
|
1492 |
where => $where, |
1493 |
); |
|
1494 |
$row = $result->all; |
|
test cleanup in progress
|
1495 |
is_deeply($row, [{$key1 => 1, $key2 => 2}]); |
test cleanup
|
1496 | |
1497 | ||
1498 |
$where = $dbi->where( |
|
test cleanup in progress
|
1499 |
clause => ['and', ['or'], ['or', ":$key1", ":$key2"]], |
test cleanup
|
1500 |
param => {} |
1501 |
); |
|
1502 |
$result = $dbi->select( |
|
test cleanup in progress
|
1503 |
table => $table1, |
test cleanup
|
1504 |
where => $where, |
1505 |
); |
|
1506 |
$row = $result->all; |
|
test cleanup in progress
|
1507 |
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]); |
test cleanup
|
1508 | |
1509 |
$where = $dbi->where; |
|
test cleanup in progress
|
1510 |
$where->clause(['and', ":${key1}{=}"]); |
1511 |
$where->param({$key1 => undef}); |
|
1512 |
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1}); |
|
test cleanup
|
1513 |
$row = $result->all; |
test cleanup in progress
|
1514 |
is_deeply($row, [{$key1 => 1, $key2 => 2}]); |
test cleanup
|
1515 | |
1516 |
$where = $dbi->where; |
|
test cleanup in progress
|
1517 |
$where->clause(['and', ":${key1}{=}"]); |
1518 |
$where->param({$key1 => undef}); |
|
test cleanup
|
1519 |
$where->if('defined'); |
1520 |
$where->map; |
|
test cleanup in progress
|
1521 |
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1}); |
test cleanup
|
1522 |
$row = $result->all; |
test cleanup in progress
|
1523 |
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]); |
test cleanup
|
1524 | |
1525 |
$where = $dbi->where; |
|
test cleanup in progress
|
1526 |
$where->clause(['or', ":${key1}{=}", ":${key1}{=}"]); |
1527 |
$where->param({$key1 => [undef, undef]}); |
|
1528 |
$result = $dbi->execute("select * from $table1 $where", {$key1 => [1, 0]}); |
|
test cleanup
|
1529 |
$row = $result->all; |
test cleanup in progress
|
1530 |
is_deeply($row, [{$key1 => 1, $key2 => 2}]); |
1531 |
$result = $dbi->execute("select * from $table1 $where", {$key1 => [0, 1]}); |
|
test cleanup
|
1532 |
$row = $result->all; |
test cleanup in progress
|
1533 |
is_deeply($row, [{$key1 => 1, $key2 => 2}]); |
test cleanup
|
1534 | |
1535 |
$where = $dbi->where; |
|
test cleanup in progress
|
1536 |
$where->clause(['and', ":${key1}{=}"]); |
1537 |
$where->param({$key1 => [undef, undef]}); |
|
test cleanup
|
1538 |
$where->if('defined'); |
1539 |
$where->map; |
|
test cleanup in progress
|
1540 |
$result = $dbi->execute("select * from $table1 $where", {$key1 => [1, 0]}); |
test cleanup
|
1541 |
$row = $result->all; |
test cleanup in progress
|
1542 |
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]); |
1543 |
$result = $dbi->execute("select * from $table1 $where", {$key1 => [0, 1]}); |
|
test cleanup
|
1544 |
$row = $result->all; |
test cleanup in progress
|
1545 |
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]); |
test cleanup
|
1546 | |
1547 |
$where = $dbi->where; |
|
test cleanup in progress
|
1548 |
$where->clause(['and', ":${key1}{=}"]); |
1549 |
$where->param({$key1 => 0}); |
|
test cleanup
|
1550 |
$where->if('length'); |
1551 |
$where->map; |
|
test cleanup in progress
|
1552 |
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1}); |
test cleanup
|
1553 |
$row = $result->all; |
test cleanup in progress
|
1554 |
is_deeply($row, [{$key1 => 1, $key2 => 2}]); |
test cleanup
|
1555 | |
1556 |
$where = $dbi->where; |
|
test cleanup in progress
|
1557 |
$where->clause(['and', ":${key1}{=}"]); |
1558 |
$where->param({$key1 => ''}); |
|
test cleanup
|
1559 |
$where->if('length'); |
1560 |
$where->map; |
|
test cleanup in progress
|
1561 |
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1}); |
test cleanup
|
1562 |
$row = $result->all; |
test cleanup in progress
|
1563 |
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]); |
test cleanup
|
1564 | |
1565 |
$where = $dbi->where; |
|
test cleanup in progress
|
1566 |
$where->clause(['and', ":${key1}{=}"]); |
1567 |
$where->param({$key1 => 5}); |
|
test cleanup
|
1568 |
$where->if(sub { ($_[0] || '') eq 5 }); |
1569 |
$where->map; |
|
test cleanup in progress
|
1570 |
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1}); |
test cleanup
|
1571 |
$row = $result->all; |
test cleanup in progress
|
1572 |
is_deeply($row, [{$key1 => 1, $key2 => 2}]); |
test cleanup
|
1573 | |
1574 |
$where = $dbi->where; |
|
test cleanup in progress
|
1575 |
$where->clause(['and', ":${key1}{=}"]); |
1576 |
$where->param({$key1 => 7}); |
|
test cleanup
|
1577 |
$where->if(sub { ($_[0] || '') eq 5 }); |
1578 |
$where->map; |
|
test cleanup in progress
|
1579 |
$result = $dbi->execute("select * from $table1 $where", {$key1 => 1}); |
test cleanup
|
1580 |
$row = $result->all; |
test cleanup in progress
|
1581 |
is_deeply($row, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]); |
test cleanup
|
1582 | |
1583 |
$where = $dbi->where; |
|
1584 |
$where->param({id => 1, author => 'Ken', price => 1900}); |
|
finishied sqlite test cleanu...
|
1585 |
$where->map(id => "$table1.id", |
1586 |
author => ["$table1.author", sub { '%' . $_[0] . '%' }], |
|
1587 |
price => ["$table1.price", {if => sub { $_[0] eq 1900 }}] |
|
test cleanup
|
1588 |
); |
test cleanup in progress
|
1589 |
is_deeply($where->param, {"$table1.id" => 1, "$table1.author" => '%Ken%', |
finishied sqlite test cleanu...
|
1590 |
"$table1.price" => 1900}); |
test cleanup
|
1591 | |
1592 |
$where = $dbi->where; |
|
1593 |
$where->param({id => 0, author => 0, price => 0}); |
|
1594 |
$where->map( |
|
finishied sqlite test cleanu...
|
1595 |
id => "$table1.id", |
1596 |
author => ["$table1.author", sub { '%' . $_[0] . '%' }], |
|
1597 |
price => ["$table1.price", sub { '%' . $_[0] . '%' }, |
|
test cleanup
|
1598 |
{if => sub { $_[0] eq 0 }}] |
1599 |
); |
|
test cleanup in progress
|
1600 |
is_deeply($where->param, {"$table1.id" => 0, "$table1.author" => '%0%', "$table1.price" => '%0%'}); |
test cleanup
|
1601 | |
1602 |
$where = $dbi->where; |
|
1603 |
$where->param({id => '', author => '', price => ''}); |
|
1604 |
$where->if('length'); |
|
1605 |
$where->map( |
|
finishied sqlite test cleanu...
|
1606 |
id => "$table1.id", |
1607 |
author => ["$table1.author", sub { '%' . $_[0] . '%' }], |
|
1608 |
price => ["$table1.price", sub { '%' . $_[0] . '%' }, |
|
test cleanup
|
1609 |
{if => sub { $_[0] eq 1 }}] |
1610 |
); |
|
1611 |
is_deeply($where->param, {}); |
|
1612 | ||
1613 |
$where = $dbi->where; |
|
1614 |
$where->param({id => undef, author => undef, price => undef}); |
|
1615 |
$where->if('length'); |
|
1616 |
$where->map( |
|
finishied sqlite test cleanu...
|
1617 |
id => "$table1.id", |
1618 |
price => ["$table1.price", {if => 'exists'}] |
|
test cleanup
|
1619 |
); |
finishied sqlite test cleanu...
|
1620 |
is_deeply($where->param, {"$table1.price" => undef}); |
test cleanup
|
1621 | |
1622 |
$where = $dbi->where; |
|
1623 |
$where->param({price => 'a'}); |
|
1624 |
$where->if('length'); |
|
1625 |
$where->map( |
|
finishied sqlite test cleanu...
|
1626 |
id => ["$table1.id", {if => 'exists'}], |
1627 |
price => ["$table1.price", sub { '%' . $_[0] }, {if => 'exists'}] |
|
test cleanup
|
1628 |
); |
finishied sqlite test cleanu...
|
1629 |
is_deeply($where->param, {"$table1.price" => '%a'}); |
test cleanup
|
1630 | |
1631 |
$where = $dbi->where; |
|
1632 |
$where->param({id => [1, 2], author => 'Ken', price => 1900}); |
|
1633 |
$where->map( |
|
finishied sqlite test cleanu...
|
1634 |
id => "$table1.id", |
1635 |
author => ["$table1.author", sub { '%' . $_[0] . '%' }], |
|
1636 |
price => ["$table1.price", {if => sub { $_[0] eq 1900 }}] |
|
test cleanup
|
1637 |
); |
test cleanup in progress
|
1638 |
is_deeply($where->param, {"$table1.id" => [1, 2], "$table1.author" => '%Ken%', |
finishied sqlite test cleanu...
|
1639 |
"$table1.price" => 1900}); |
test cleanup
|
1640 | |
1641 |
$where = $dbi->where; |
|
1642 |
$where->if('length'); |
|
1643 |
$where->param({id => ['', ''], author => 'Ken', price => 1900}); |
|
1644 |
$where->map( |
|
finishied sqlite test cleanu...
|
1645 |
id => "$table1.id", |
1646 |
author => ["$table1.author", sub { '%' . $_[0] . '%' }], |
|
1647 |
price => ["$table1.price", {if => sub { $_[0] eq 1900 }}] |
|
test cleanup
|
1648 |
); |
test cleanup in progress
|
1649 |
is_deeply($where->param, {"$table1.id" => [$dbi->not_exists, $dbi->not_exists], "$table1.author" => '%Ken%', |
finishied sqlite test cleanu...
|
1650 |
"$table1.price" => 1900}); |
test cleanup
|
1651 | |
1652 |
$where = $dbi->where; |
|
1653 |
$where->param({id => ['', ''], author => 'Ken', price => 1900}); |
|
1654 |
$where->map( |
|
finishied sqlite test cleanu...
|
1655 |
id => ["$table1.id", {if => 'length'}], |
1656 |
author => ["$table1.author", sub { '%' . $_[0] . '%' }, {if => 'defined'}], |
|
1657 |
price => ["$table1.price", {if => sub { $_[0] eq 1900 }}] |
|
test cleanup
|
1658 |
); |
test cleanup in progress
|
1659 |
is_deeply($where->param, {"$table1.id" => [$dbi->not_exists, $dbi->not_exists], "$table1.author" => '%Ken%', |
finishied sqlite test cleanu...
|
1660 |
"$table1.price" => 1900}); |
test cleanup
|
1661 | |
1662 |
test 'register_tag_processor'; |
|
1663 |
$dbi = DBIx::Custom->connect; |
|
1664 |
$dbi->register_tag_processor( |
|
1665 |
a => sub { 1 } |
|
1666 |
); |
|
test cleanup
|
1667 |
is($dbi->{_tags}->{a}->(), 1); |
test cleanup
|
1668 | |
1669 |
test 'register_tag'; |
|
1670 |
$dbi = DBIx::Custom->connect; |
|
1671 |
$dbi->register_tag( |
|
1672 |
b => sub { 2 } |
|
1673 |
); |
|
test cleanup
|
1674 |
is($dbi->{_tags}->{b}->(), 2); |
test cleanup
|
1675 | |
1676 |
test 'table not specify exception'; |
|
1677 |
$dbi = DBIx::Custom->connect; |
|
1678 |
eval {$dbi->insert}; |
|
1679 |
like($@, qr/table/); |
|
1680 |
eval {$dbi->update}; |
|
1681 |
like($@, qr/table/); |
|
1682 |
eval {$dbi->delete}; |
|
1683 |
like($@, qr/table/); |
|
1684 |
eval {$dbi->select}; |
|
1685 |
like($@, qr/table/); |
|
test cleanup
|
1686 | |
test cleanup
|
1687 |
test 'more tests'; |
1688 |
$dbi = DBIx::Custom->connect; |
|
1689 |
eval{$dbi->apply_filter('table', 'column', [])}; |
|
1690 |
like($@, qr/apply_filter/); |
|
1691 | ||
1692 |
eval{$dbi->apply_filter('table', 'column', {outer => 2})}; |
|
1693 |
like($@, qr/apply_filter/); |
|
1694 | ||
1695 |
$dbi->apply_filter( |
|
1696 | ||
1697 |
); |
|
1698 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
1699 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
1700 |
$dbi->execute($create_table1); |
test cleanup in progress
|
1701 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
1702 |
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4}); |
|
1703 |
$dbi->apply_filter($table1, $key2, |
|
test cleanup
|
1704 |
{in => sub { $_[0] * 3 }, out => sub { $_[0] * 2 }}); |
test cleanup in progress
|
1705 |
$rows = $dbi->select(table => $table1, where => {$key2 => 1})->all; |
1706 |
is_deeply($rows, [{$key1 => 1, $key2 => 6}]); |
|
test cleanup
|
1707 | |
1708 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
1709 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
1710 |
$dbi->execute($create_table1); |
test cleanup in progress
|
1711 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
1712 |
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4}); |
|
1713 |
$dbi->apply_filter($table1, $key2, {}); |
|
1714 |
$rows = $dbi->select(table => $table1, where => {$key2 => 2})->all; |
|
1715 |
is_deeply($rows, [{$key1 => 1, $key2 => 2}]); |
|
test cleanup
|
1716 | |
1717 |
$dbi = DBIx::Custom->connect; |
|
test cleanup in progress
|
1718 |
eval {$dbi->apply_filter($table1, $key2, {out => 'no'})}; |
test cleanup
|
1719 |
like($@, qr/not registered/); |
test cleanup in progress
|
1720 |
eval {$dbi->apply_filter($table1, $key2, {in => 'no'})}; |
test cleanup
|
1721 |
like($@, qr/not registered/); |
1722 |
$dbi->method({one => sub { 1 }}); |
|
1723 |
is($dbi->one, 1); |
|
1724 | ||
1725 |
eval{DBIx::Custom->connect(dsn => undef)}; |
|
1726 |
like($@, qr/_connect/); |
|
1727 | ||
1728 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
1729 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
1730 |
$dbi->execute($create_table1); |
1731 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
test cleanup in progress
|
1732 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}, |
1733 |
filter => {$key1 => 'twice'}); |
|
1734 |
$row = $dbi->select(table => $table1)->one; |
|
1735 |
is_deeply($row, {$key1 => 2, $key2 => 2}); |
|
1736 |
eval {$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}, |
|
1737 |
filter => {$key1 => 'no'}) }; |
|
test cleanup
|
1738 |
like($@, qr//); |
1739 | ||
1740 |
$dbi->register_filter(one => sub { }); |
|
1741 |
$dbi->default_fetch_filter('one'); |
|
1742 |
ok($dbi->default_fetch_filter); |
|
1743 |
$dbi->default_bind_filter('one'); |
|
1744 |
ok($dbi->default_bind_filter); |
|
1745 |
eval{$dbi->default_fetch_filter('no')}; |
|
1746 |
like($@, qr/not registered/); |
|
1747 |
eval{$dbi->default_bind_filter('no')}; |
|
1748 |
like($@, qr/not registered/); |
|
1749 |
$dbi->default_bind_filter(undef); |
|
1750 |
ok(!defined $dbi->default_bind_filter); |
|
1751 |
$dbi->default_fetch_filter(undef); |
|
1752 |
ok(!defined $dbi->default_fetch_filter); |
|
test cleanup in progress
|
1753 |
eval {$dbi->execute("select * from $table1 {} {= author") }; |
test cleanup
|
1754 |
like($@, qr/Tag not finished/); |
1755 | ||
1756 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
1757 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
1758 |
$dbi->execute($create_table1); |
1759 |
$dbi->register_filter(one => sub { 1 }); |
|
test cleanup in progress
|
1760 |
$result = $dbi->select(table => $table1); |
1761 |
eval {$result->filter($key1 => 'no')}; |
|
test cleanup
|
1762 |
like($@, qr/not registered/); |
test cleanup in progress
|
1763 |
eval {$result->end_filter($key1 => 'no')}; |
test cleanup
|
1764 |
like($@, qr/not registered/); |
1765 |
$result->default_filter(undef); |
|
1766 |
ok(!defined $result->default_filter); |
|
1767 |
$result->default_filter('one'); |
|
1768 |
is($result->default_filter->(), 1); |
|
1769 | ||
1770 |
test 'dbi_option'; |
|
1771 |
$dbi = DBIx::Custom->connect(dbi_option => {PrintError => 1}); |
|
1772 |
ok($dbi->dbh->{PrintError}); |
|
1773 |
$dbi = DBIx::Custom->connect(dbi_options => {PrintError => 1}); |
|
1774 |
ok($dbi->dbh->{PrintError}); |
|
1775 | ||
1776 |
test 'DBIx::Custom::Result stash()'; |
|
1777 |
$result = DBIx::Custom::Result->new; |
|
1778 |
is_deeply($result->stash, {}, 'default'); |
|
1779 |
$result->stash->{foo} = 1; |
|
1780 |
is($result->stash->{foo}, 1, 'get and set'); |
|
test cleanup
|
1781 | |
cleanup test
|
1782 |
test 'delete_at'; |
1783 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
1784 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
1785 |
$dbi->execute($create_table1_2); |
test cleanup in progress
|
1786 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3}); |
cleanup test
|
1787 |
$dbi->delete_at( |
test cleanup in progress
|
1788 |
table => $table1, |
1789 |
primary_key => [$key1, $key2], |
|
cleanup test
|
1790 |
where => [1, 2], |
1791 |
); |
|
test cleanup in progress
|
1792 |
is_deeply($dbi->select(table => $table1)->all, []); |
cleanup test
|
1793 | |
test cleanup in progress
|
1794 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3}); |
cleanup test
|
1795 |
$dbi->delete_at( |
test cleanup in progress
|
1796 |
table => $table1, |
1797 |
primary_key => $key1, |
|
cleanup test
|
1798 |
where => 1, |
1799 |
); |
|
test cleanup in progress
|
1800 |
is_deeply($dbi->select(table => $table1)->all, []); |
cleanup test
|
1801 | |
1802 |
test 'insert_at'; |
|
1803 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
1804 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
1805 |
$dbi->execute($create_table1_2); |
1806 |
$dbi->insert_at( |
|
test cleanup in progress
|
1807 |
primary_key => [$key1, $key2], |
1808 |
table => $table1, |
|
cleanup test
|
1809 |
where => [1, 2], |
test cleanup in progress
|
1810 |
param => {$key3 => 3} |
cleanup test
|
1811 |
); |
test cleanup in progress
|
1812 |
is($dbi->select(table => $table1)->one->{$key1}, 1); |
1813 |
is($dbi->select(table => $table1)->one->{$key2}, 2); |
|
1814 |
is($dbi->select(table => $table1)->one->{$key3}, 3); |
|
cleanup test
|
1815 | |
test cleanup in progress
|
1816 |
$dbi->delete_all(table => $table1); |
1817 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3}); |
|
cleanup test
|
1818 |
$dbi->insert_at( |
test cleanup in progress
|
1819 |
primary_key => $key1, |
1820 |
table => $table1, |
|
cleanup test
|
1821 |
where => 1, |
test cleanup in progress
|
1822 |
param => {$key2 => 2, $key3 => 3} |
cleanup test
|
1823 |
); |
1824 | ||
test cleanup in progress
|
1825 |
is($dbi->select(table => $table1)->one->{$key1}, 1); |
1826 |
is($dbi->select(table => $table1)->one->{$key2}, 2); |
|
1827 |
is($dbi->select(table => $table1)->one->{$key3}, 3); |
|
cleanup test
|
1828 | |
1829 |
eval { |
|
1830 |
$dbi->insert_at( |
|
test cleanup in progress
|
1831 |
table => $table1, |
1832 |
primary_key => [$key1, $key2], |
|
cleanup test
|
1833 |
where => {}, |
test cleanup in progress
|
1834 |
param => {$key1 => 1, $key2 => 2, $key3 => 3}, |
cleanup test
|
1835 |
); |
1836 |
}; |
|
1837 |
like($@, qr/must be/); |
|
1838 | ||
1839 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
1840 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
1841 |
$dbi->execute($create_table1_2); |
1842 |
$dbi->insert_at( |
|
test cleanup in progress
|
1843 |
{$key3 => 3}, |
1844 |
primary_key => [$key1, $key2], |
|
1845 |
table => $table1, |
|
cleanup test
|
1846 |
where => [1, 2], |
1847 |
); |
|
test cleanup in progress
|
1848 |
is($dbi->select(table => $table1)->one->{$key1}, 1); |
1849 |
is($dbi->select(table => $table1)->one->{$key2}, 2); |
|
1850 |
is($dbi->select(table => $table1)->one->{$key3}, 3); |
|
cleanup test
|
1851 | |
1852 |
test 'update_at'; |
|
1853 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
1854 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
1855 |
$dbi->execute($create_table1_2); |
test cleanup in progress
|
1856 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3}); |
cleanup test
|
1857 |
$dbi->update_at( |
test cleanup in progress
|
1858 |
table => $table1, |
1859 |
primary_key => [$key1, $key2], |
|
cleanup test
|
1860 |
where => [1, 2], |
test cleanup in progress
|
1861 |
param => {$key3 => 4} |
cleanup test
|
1862 |
); |
test cleanup in progress
|
1863 |
is($dbi->select(table => $table1)->one->{$key1}, 1); |
1864 |
is($dbi->select(table => $table1)->one->{$key2}, 2); |
|
1865 |
is($dbi->select(table => $table1)->one->{$key3}, 4); |
|
cleanup test
|
1866 | |
test cleanup in progress
|
1867 |
$dbi->delete_all(table => $table1); |
1868 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3}); |
|
cleanup test
|
1869 |
$dbi->update_at( |
test cleanup in progress
|
1870 |
table => $table1, |
1871 |
primary_key => $key1, |
|
cleanup test
|
1872 |
where => 1, |
test cleanup in progress
|
1873 |
param => {$key3 => 4} |
cleanup test
|
1874 |
); |
test cleanup in progress
|
1875 |
is($dbi->select(table => $table1)->one->{$key1}, 1); |
1876 |
is($dbi->select(table => $table1)->one->{$key2}, 2); |
|
1877 |
is($dbi->select(table => $table1)->one->{$key3}, 4); |
|
cleanup test
|
1878 | |
1879 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
1880 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
1881 |
$dbi->execute($create_table1_2); |
test cleanup in progress
|
1882 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3}); |
cleanup test
|
1883 |
$dbi->update_at( |
test cleanup in progress
|
1884 |
{$key3 => 4}, |
1885 |
table => $table1, |
|
1886 |
primary_key => [$key1, $key2], |
|
cleanup test
|
1887 |
where => [1, 2] |
1888 |
); |
|
test cleanup in progress
|
1889 |
is($dbi->select(table => $table1)->one->{$key1}, 1); |
1890 |
is($dbi->select(table => $table1)->one->{$key2}, 2); |
|
1891 |
is($dbi->select(table => $table1)->one->{$key3}, 4); |
|
cleanup test
|
1892 | |
1893 |
test 'select_at'; |
|
1894 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
1895 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
1896 |
$dbi->execute($create_table1_2); |
test cleanup in progress
|
1897 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3}); |
cleanup test
|
1898 |
$result = $dbi->select_at( |
test cleanup in progress
|
1899 |
table => $table1, |
1900 |
primary_key => [$key1, $key2], |
|
cleanup test
|
1901 |
where => [1, 2] |
1902 |
); |
|
1903 |
$row = $result->one; |
|
test cleanup in progress
|
1904 |
is($row->{$key1}, 1); |
1905 |
is($row->{$key2}, 2); |
|
1906 |
is($row->{$key3}, 3); |
|
cleanup test
|
1907 | |
test cleanup in progress
|
1908 |
$dbi->delete_all(table => $table1); |
1909 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3}); |
|
cleanup test
|
1910 |
$result = $dbi->select_at( |
test cleanup in progress
|
1911 |
table => $table1, |
1912 |
primary_key => $key1, |
|
cleanup test
|
1913 |
where => 1, |
1914 |
); |
|
1915 |
$row = $result->one; |
|
test cleanup in progress
|
1916 |
is($row->{$key1}, 1); |
1917 |
is($row->{$key2}, 2); |
|
1918 |
is($row->{$key3}, 3); |
|
cleanup test
|
1919 | |
test cleanup in progress
|
1920 |
$dbi->delete_all(table => $table1); |
1921 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3}); |
|
cleanup test
|
1922 |
$result = $dbi->select_at( |
test cleanup in progress
|
1923 |
table => $table1, |
1924 |
primary_key => [$key1, $key2], |
|
cleanup test
|
1925 |
where => [1, 2] |
1926 |
); |
|
1927 |
$row = $result->one; |
|
test cleanup in progress
|
1928 |
is($row->{$key1}, 1); |
1929 |
is($row->{$key2}, 2); |
|
1930 |
is($row->{$key3}, 3); |
|
cleanup test
|
1931 | |
1932 |
eval { |
|
1933 |
$result = $dbi->select_at( |
|
test cleanup in progress
|
1934 |
table => $table1, |
1935 |
primary_key => [$key1, $key2], |
|
cleanup test
|
1936 |
where => {}, |
1937 |
); |
|
1938 |
}; |
|
1939 |
like($@, qr/must be/); |
|
1940 | ||
1941 |
eval { |
|
1942 |
$result = $dbi->select_at( |
|
test cleanup in progress
|
1943 |
table => $table1, |
1944 |
primary_key => [$key1, $key2], |
|
cleanup test
|
1945 |
where => [1], |
1946 |
); |
|
1947 |
}; |
|
1948 |
like($@, qr/same/); |
|
1949 | ||
1950 |
eval { |
|
1951 |
$result = $dbi->update_at( |
|
test cleanup in progress
|
1952 |
table => $table1, |
1953 |
primary_key => [$key1, $key2], |
|
cleanup test
|
1954 |
where => {}, |
test cleanup in progress
|
1955 |
param => {$key1 => 1, $key2 => 2}, |
cleanup test
|
1956 |
); |
1957 |
}; |
|
1958 |
like($@, qr/must be/); |
|
1959 | ||
1960 |
eval { |
|
1961 |
$result = $dbi->delete_at( |
|
test cleanup in progress
|
1962 |
table => $table1, |
1963 |
primary_key => [$key1, $key2], |
|
cleanup test
|
1964 |
where => {}, |
1965 |
); |
|
1966 |
}; |
|
1967 |
like($@, qr/must be/); |
|
1968 | ||
1969 |
test 'model delete_at'; |
|
1970 |
$dbi = MyDBI6->connect; |
|
finishied sqlite test cleanu...
|
1971 |
eval { $dbi->execute("drop table $table1") }; |
1972 |
eval { $dbi->execute("drop table $table2") }; |
|
1973 |
eval { $dbi->execute("drop table $table3") }; |
|
cleanup test
|
1974 |
$dbi->execute($create_table1_2); |
1975 |
$dbi->execute($create_table2_2); |
|
1976 |
$dbi->execute($create_table3); |
|
test cleanup in progress
|
1977 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3}); |
1978 |
$dbi->model($table1)->delete_at(where => [1, 2]); |
|
1979 |
is_deeply($dbi->select(table => $table1)->all, []); |
|
1980 |
$dbi->insert(table => $table2, param => {$key1 => 1, $key2 => 2, $key3 => 3}); |
|
1981 |
$dbi->model($table1)->delete_at(where => [1, 2]); |
|
1982 |
is_deeply($dbi->select(table => $table1)->all, []); |
|
1983 |
$dbi->insert(table => $table3, param => {$key1 => 1, $key2 => 2, $key3 => 3}); |
|
1984 |
$dbi->model($table3)->delete_at(where => [1, 2]); |
|
1985 |
is_deeply($dbi->select(table => $table3)->all, []); |
|
cleanup test
|
1986 | |
1987 |
test 'model insert_at'; |
|
1988 |
$dbi = MyDBI6->connect; |
|
finishied sqlite test cleanu...
|
1989 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
1990 |
$dbi->execute($create_table1_2); |
test cleanup in progress
|
1991 |
$dbi->model($table1)->insert_at( |
cleanup test
|
1992 |
where => [1, 2], |
test cleanup in progress
|
1993 |
param => {$key3 => 3} |
cleanup test
|
1994 |
); |
test cleanup in progress
|
1995 |
$result = $dbi->model($table1)->select; |
cleanup test
|
1996 |
$row = $result->one; |
test cleanup in progress
|
1997 |
is($row->{$key1}, 1); |
1998 |
is($row->{$key2}, 2); |
|
1999 |
is($row->{$key3}, 3); |
|
cleanup test
|
2000 | |
2001 |
test 'model update_at'; |
|
2002 |
$dbi = MyDBI6->connect; |
|
finishied sqlite test cleanu...
|
2003 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
2004 |
$dbi->execute($create_table1_2); |
test cleanup in progress
|
2005 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3}); |
2006 |
$dbi->model($table1)->update_at( |
|
cleanup test
|
2007 |
where => [1, 2], |
test cleanup in progress
|
2008 |
param => {$key3 => 4} |
cleanup test
|
2009 |
); |
test cleanup in progress
|
2010 |
$result = $dbi->model($table1)->select; |
cleanup test
|
2011 |
$row = $result->one; |
test cleanup in progress
|
2012 |
is($row->{$key1}, 1); |
2013 |
is($row->{$key2}, 2); |
|
2014 |
is($row->{$key3}, 4); |
|
cleanup test
|
2015 | |
2016 |
test 'model select_at'; |
|
2017 |
$dbi = MyDBI6->connect; |
|
finishied sqlite test cleanu...
|
2018 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
2019 |
$dbi->execute($create_table1_2); |
test cleanup in progress
|
2020 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3}); |
2021 |
$result = $dbi->model($table1)->select_at(where => [1, 2]); |
|
cleanup test
|
2022 |
$row = $result->one; |
test cleanup in progress
|
2023 |
is($row->{$key1}, 1); |
2024 |
is($row->{$key2}, 2); |
|
2025 |
is($row->{$key3}, 3); |
|
cleanup test
|
2026 | |
2027 | ||
2028 |
test 'mycolumn and column'; |
|
2029 |
$dbi = MyDBI7->connect; |
|
finishied sqlite test cleanu...
|
2030 |
eval { $dbi->execute("drop table $table1") }; |
2031 |
eval { $dbi->execute("drop table $table2") }; |
|
cleanup test
|
2032 |
$dbi->execute($create_table1); |
2033 |
$dbi->execute($create_table2); |
|
2034 |
$dbi->separator('__'); |
|
2035 |
$dbi->setup_model; |
|
test cleanup in progress
|
2036 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
2037 |
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3}); |
|
2038 |
$model = $dbi->model($table1); |
|
cleanup test
|
2039 |
$result = $model->select( |
test cleanup in progress
|
2040 |
column => [$model->mycolumn, $model->column($table2)], |
finishied sqlite test cleanu...
|
2041 |
where => {"$table1.$key1" => 1} |
cleanup test
|
2042 |
); |
2043 |
is_deeply($result->one, |
|
test cleanup in progress
|
2044 |
{$key1 => 1, $key2 => 2, "${table2}__$key1" => 1, "${table2}__$key3" => 3}); |
test cleanup
|
2045 | |
test cleanup
|
2046 |
test 'insert_param'; |
2047 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
2048 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
2049 |
$dbi->execute($create_table1_2); |
test cleanup in progress
|
2050 |
$param = {$key1 => 1, $key2 => 2}; |
test cleanup
|
2051 |
$insert_param = $dbi->insert_param($param); |
2052 |
$sql = <<"EOS"; |
|
test cleanup in progress
|
2053 |
insert into $table1 $insert_param |
test cleanup
|
2054 |
EOS |
test cleanup in progress
|
2055 |
$dbi->execute($sql, param => $param, table => $table1); |
2056 |
is($dbi->select(table => $table1)->one->{$key1}, 1); |
|
2057 |
is($dbi->select(table => $table1)->one->{$key2}, 2); |
|
test cleanup
|
2058 | |
2059 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
2060 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
2061 |
$dbi->execute($create_table1_2); |
test cleanup in progress
|
2062 |
$param = {$key1 => 1, $key2 => 2}; |
test cleanup
|
2063 |
$insert_param = $dbi->insert_param($param); |
2064 |
$sql = <<"EOS"; |
|
test cleanup in progress
|
2065 |
insert into $table1 $insert_param |
test cleanup
|
2066 |
EOS |
test cleanup in progress
|
2067 |
$dbi->execute($sql, param => $param, table => $table1); |
2068 |
is($dbi->select(table => $table1)->one->{$key1}, 1); |
|
2069 |
is($dbi->select(table => $table1)->one->{$key2}, 2); |
|
test cleanup
|
2070 | |
2071 |
eval { $dbi->insert_param({";" => 1}) }; |
|
2072 |
like($@, qr/not safety/); |
|
test cleanup
|
2073 | |
cleanup test
|
2074 |
test 'mycolumn'; |
2075 |
$dbi = MyDBI8->connect; |
|
finishied sqlite test cleanu...
|
2076 |
eval { $dbi->execute("drop table $table1") }; |
2077 |
eval { $dbi->execute("drop table $table2") }; |
|
cleanup test
|
2078 |
$dbi->execute($create_table1); |
2079 |
$dbi->execute($create_table2); |
|
2080 |
$dbi->setup_model; |
|
test cleanup in progress
|
2081 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
2082 |
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3}); |
|
2083 |
$model = $dbi->model($table1); |
|
cleanup test
|
2084 |
$result = $model->select_at( |
2085 |
column => [ |
|
2086 |
$model->mycolumn, |
|
test cleanup in progress
|
2087 |
$model->column($table2) |
cleanup test
|
2088 |
] |
2089 |
); |
|
2090 |
is_deeply($result->one, |
|
test cleanup in progress
|
2091 |
{$key1 => 1, $key2 => 2, "$table2.$key1" => 1, "$table2.$key3" => 3}); |
cleanup test
|
2092 | |
2093 |
$result = $model->select_at( |
|
2094 |
column => [ |
|
test cleanup in progress
|
2095 |
$model->mycolumn([$key1]), |
2096 |
$model->column($table2 => [$key1]) |
|
cleanup test
|
2097 |
] |
2098 |
); |
|
2099 |
is_deeply($result->one, |
|
finishied sqlite test cleanu...
|
2100 |
{$key1 => 1, "$table2.$key1" => 1}); |
cleanup test
|
2101 |
$result = $model->select_at( |
2102 |
column => [ |
|
test cleanup in progress
|
2103 |
$model->mycolumn([$key1]), |
2104 |
{$table2 => [$key1]} |
|
cleanup test
|
2105 |
] |
2106 |
); |
|
2107 |
is_deeply($result->one, |
|
finishied sqlite test cleanu...
|
2108 |
{$key1 => 1, "$table2.$key1" => 1}); |
cleanup test
|
2109 | |
2110 |
$result = $model->select_at( |
|
2111 |
column => [ |
|
test cleanup in progress
|
2112 |
$model->mycolumn([$key1]), |
2113 |
["$table2.$key1", as => "$table2.$key1"] |
|
cleanup test
|
2114 |
] |
2115 |
); |
|
2116 |
is_deeply($result->one, |
|
finishied sqlite test cleanu...
|
2117 |
{$key1 => 1, "$table2.$key1" => 1}); |
cleanup test
|
2118 | |
2119 |
$result = $model->select_at( |
|
2120 |
column => [ |
|
test cleanup in progress
|
2121 |
$model->mycolumn([$key1]), |
2122 |
["$table2.$key1" => "$table2.$key1"] |
|
cleanup test
|
2123 |
] |
2124 |
); |
|
2125 |
is_deeply($result->one, |
|
finishied sqlite test cleanu...
|
2126 |
{$key1 => 1, "$table2.$key1" => 1}); |
cleanup test
|
2127 | |
cleanup test
|
2128 |
test 'merge_param'; |
2129 |
$dbi = DBIx::Custom->new; |
|
2130 |
$params = [ |
|
test cleanup in progress
|
2131 |
{$key1 => 1, $key2 => 2, $key3 => 3}, |
2132 |
{$key1 => 1, $key2 => 2}, |
|
2133 |
{$key1 => 1} |
|
cleanup test
|
2134 |
]; |
2135 |
$param = $dbi->merge_param($params->[0], $params->[1], $params->[2]); |
|
test cleanup in progress
|
2136 |
is_deeply($param, {$key1 => [1, 1, 1], $key2 => [2, 2], $key3 => 3}); |
cleanup test
|
2137 | |
2138 |
$params = [ |
|
test cleanup in progress
|
2139 |
{$key1 => [1, 2], $key2 => 1, $key3 => [1, 2]}, |
2140 |
{$key1 => [3, 4], $key2 => [2, 3], $key3 => 3} |
|
cleanup test
|
2141 |
]; |
2142 |
$param = $dbi->merge_param($params->[0], $params->[1]); |
|
test cleanup in progress
|
2143 |
is_deeply($param, {$key1 => [1, 2, 3, 4], $key2 => [1, 2, 3], $key3 => [1, 2, 3]}); |
cleanup test
|
2144 | |
2145 |
test 'select() param option'; |
|
2146 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
2147 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
2148 |
$dbi->execute($create_table1); |
test cleanup in progress
|
2149 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
2150 |
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3}); |
|
finishied sqlite test cleanu...
|
2151 |
eval { $dbi->execute("drop table $table2") }; |
cleanup test
|
2152 |
$dbi->execute($create_table2); |
test cleanup in progress
|
2153 |
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 4}); |
2154 |
$dbi->insert(table => $table2, param => {$key1 => 2, $key3 => 5}); |
|
cleanup test
|
2155 |
$rows = $dbi->select( |
test cleanup in progress
|
2156 |
table => $table1, |
2157 |
column => "$table1.$key1 as ${table1}_$key1, $key2, $key3", |
|
finishied sqlite test cleanu...
|
2158 |
where => {"$table1.$key2" => 3}, |
test cleanup in progress
|
2159 |
join => ["inner join (select * from $table2 where {= $table2.$key3})" . |
test cleanup
|
2160 |
" $table2 on $table1.$key1 = $table2.$key1"], |
finishied sqlite test cleanu...
|
2161 |
param => {"$table2.$key3" => 5} |
cleanup test
|
2162 |
)->all; |
test cleanup in progress
|
2163 |
is_deeply($rows, [{"${table1}_$key1" => 2, $key2 => 3, $key3 => 5}]); |
cleanup test
|
2164 | |
2165 |
test 'select() string where'; |
|
2166 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
2167 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
2168 |
$dbi->execute($create_table1); |
test cleanup in progress
|
2169 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
2170 |
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3}); |
|
cleanup test
|
2171 |
$rows = $dbi->select( |
test cleanup in progress
|
2172 |
table => $table1, |
2173 |
where => "$key1 = :$key1 and $key2 = :$key2", |
|
2174 |
where_param => {$key1 => 1, $key2 => 2} |
|
cleanup test
|
2175 |
)->all; |
test cleanup in progress
|
2176 |
is_deeply($rows, [{$key1 => 1, $key2 => 2}]); |
cleanup test
|
2177 | |
2178 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
2179 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
2180 |
$dbi->execute($create_table1); |
test cleanup in progress
|
2181 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
2182 |
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3}); |
|
cleanup test
|
2183 |
$rows = $dbi->select( |
test cleanup in progress
|
2184 |
table => $table1, |
cleanup test
|
2185 |
where => [ |
test cleanup in progress
|
2186 |
"$key1 = :$key1 and $key2 = :$key2", |
2187 |
{$key1 => 1, $key2 => 2} |
|
cleanup test
|
2188 |
] |
2189 |
)->all; |
|
test cleanup in progress
|
2190 |
is_deeply($rows, [{$key1 => 1, $key2 => 2}]); |
cleanup test
|
2191 | |
2192 |
test 'delete() string where'; |
|
2193 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
2194 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
2195 |
$dbi->execute($create_table1); |
test cleanup in progress
|
2196 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
2197 |
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3}); |
|
cleanup test
|
2198 |
$dbi->delete( |
test cleanup in progress
|
2199 |
table => $table1, |
2200 |
where => "$key1 = :$key1 and $key2 = :$key2", |
|
2201 |
where_param => {$key1 => 1, $key2 => 2} |
|
cleanup test
|
2202 |
); |
test cleanup in progress
|
2203 |
$rows = $dbi->select(table => $table1)->all; |
2204 |
is_deeply($rows, [{$key1 => 2, $key2 => 3}]); |
|
cleanup test
|
2205 | |
2206 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
2207 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
2208 |
$dbi->execute($create_table1); |
test cleanup in progress
|
2209 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
2210 |
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3}); |
|
cleanup test
|
2211 |
$dbi->delete( |
test cleanup in progress
|
2212 |
table => $table1, |
cleanup test
|
2213 |
where => [ |
test cleanup in progress
|
2214 |
"$key1 = :$key1 and $key2 = :$key2", |
2215 |
{$key1 => 1, $key2 => 2} |
|
cleanup test
|
2216 |
] |
2217 |
); |
|
test cleanup in progress
|
2218 |
$rows = $dbi->select(table => $table1)->all; |
2219 |
is_deeply($rows, [{$key1 => 2, $key2 => 3}]); |
|
cleanup test
|
2220 | |
2221 | ||
2222 |
test 'update() string where'; |
|
2223 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
2224 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
2225 |
$dbi->execute($create_table1); |
test cleanup in progress
|
2226 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
cleanup test
|
2227 |
$dbi->update( |
test cleanup in progress
|
2228 |
table => $table1, |
2229 |
param => {$key1 => 5}, |
|
2230 |
where => "$key1 = :$key1 and $key2 = :$key2", |
|
2231 |
where_param => {$key1 => 1, $key2 => 2} |
|
cleanup test
|
2232 |
); |
test cleanup in progress
|
2233 |
$rows = $dbi->select(table => $table1)->all; |
2234 |
is_deeply($rows, [{$key1 => 5, $key2 => 2}]); |
|
cleanup test
|
2235 | |
2236 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
2237 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
2238 |
$dbi->execute($create_table1); |
test cleanup in progress
|
2239 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
cleanup test
|
2240 |
$dbi->update( |
test cleanup in progress
|
2241 |
table => $table1, |
2242 |
param => {$key1 => 5}, |
|
cleanup test
|
2243 |
where => [ |
test cleanup in progress
|
2244 |
"$key1 = :$key1 and $key2 = :$key2", |
2245 |
{$key1 => 1, $key2 => 2} |
|
cleanup test
|
2246 |
] |
2247 |
); |
|
test cleanup in progress
|
2248 |
$rows = $dbi->select(table => $table1)->all; |
2249 |
is_deeply($rows, [{$key1 => 5, $key2 => 2}]); |
|
cleanup test
|
2250 | |
2251 |
test 'insert id and primary_key option'; |
|
2252 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
2253 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
2254 |
$dbi->execute($create_table1_2); |
2255 |
$dbi->insert( |
|
test cleanup in progress
|
2256 |
primary_key => [$key1, $key2], |
2257 |
table => $table1, |
|
cleanup test
|
2258 |
id => [1, 2], |
test cleanup in progress
|
2259 |
param => {$key3 => 3} |
cleanup test
|
2260 |
); |
test cleanup in progress
|
2261 |
is($dbi->select(table => $table1)->one->{$key1}, 1); |
2262 |
is($dbi->select(table => $table1)->one->{$key2}, 2); |
|
2263 |
is($dbi->select(table => $table1)->one->{$key3}, 3); |
|
cleanup test
|
2264 | |
test cleanup in progress
|
2265 |
$dbi->delete_all(table => $table1); |
cleanup test
|
2266 |
$dbi->insert( |
test cleanup in progress
|
2267 |
primary_key => $key1, |
2268 |
table => $table1, |
|
cleanup test
|
2269 |
id => 0, |
test cleanup in progress
|
2270 |
param => {$key2 => 2, $key3 => 3} |
cleanup test
|
2271 |
); |
2272 | ||
test cleanup in progress
|
2273 |
is($dbi->select(table => $table1)->one->{$key1}, 0); |
2274 |
is($dbi->select(table => $table1)->one->{$key2}, 2); |
|
2275 |
is($dbi->select(table => $table1)->one->{$key3}, 3); |
|
cleanup test
|
2276 | |
2277 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
2278 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
2279 |
$dbi->execute($create_table1_2); |
2280 |
$dbi->insert( |
|
test cleanup in progress
|
2281 |
{$key3 => 3}, |
2282 |
primary_key => [$key1, $key2], |
|
2283 |
table => $table1, |
|
cleanup test
|
2284 |
id => [1, 2], |
2285 |
); |
|
test cleanup in progress
|
2286 |
is($dbi->select(table => $table1)->one->{$key1}, 1); |
2287 |
is($dbi->select(table => $table1)->one->{$key2}, 2); |
|
2288 |
is($dbi->select(table => $table1)->one->{$key3}, 3); |
|
test cleanup
|
2289 | |
cleanup test
|
2290 |
test 'model insert id and primary_key option'; |
2291 |
$dbi = MyDBI6->connect; |
|
finishied sqlite test cleanu...
|
2292 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
2293 |
$dbi->execute($create_table1_2); |
test cleanup in progress
|
2294 |
$dbi->model($table1)->insert( |
cleanup test
|
2295 |
id => [1, 2], |
test cleanup in progress
|
2296 |
param => {$key3 => 3} |
cleanup test
|
2297 |
); |
test cleanup in progress
|
2298 |
$result = $dbi->model($table1)->select; |
cleanup test
|
2299 |
$row = $result->one; |
test cleanup in progress
|
2300 |
is($row->{$key1}, 1); |
2301 |
is($row->{$key2}, 2); |
|
2302 |
is($row->{$key3}, 3); |
|
cleanup test
|
2303 | |
2304 |
$dbi = MyDBI6->connect; |
|
finishied sqlite test cleanu...
|
2305 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
2306 |
$dbi->execute($create_table1_2); |
test cleanup in progress
|
2307 |
$dbi->model($table1)->insert( |
2308 |
{$key3 => 3}, |
|
cleanup test
|
2309 |
id => [1, 2] |
2310 |
); |
|
test cleanup in progress
|
2311 |
$result = $dbi->model($table1)->select; |
cleanup test
|
2312 |
$row = $result->one; |
test cleanup in progress
|
2313 |
is($row->{$key1}, 1); |
2314 |
is($row->{$key2}, 2); |
|
2315 |
is($row->{$key3}, 3); |
|
cleanup test
|
2316 | |
2317 |
test 'update and id option'; |
|
2318 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
2319 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
2320 |
$dbi->execute($create_table1_2); |
test cleanup in progress
|
2321 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3}); |
cleanup test
|
2322 |
$dbi->update( |
test cleanup in progress
|
2323 |
table => $table1, |
2324 |
primary_key => [$key1, $key2], |
|
cleanup test
|
2325 |
id => [1, 2], |
test cleanup in progress
|
2326 |
param => {$key3 => 4} |
cleanup test
|
2327 |
); |
test cleanup in progress
|
2328 |
is($dbi->select(table => $table1)->one->{$key1}, 1); |
2329 |
is($dbi->select(table => $table1)->one->{$key2}, 2); |
|
2330 |
is($dbi->select(table => $table1)->one->{$key3}, 4); |
|
cleanup test
|
2331 | |
test cleanup in progress
|
2332 |
$dbi->delete_all(table => $table1); |
2333 |
$dbi->insert(table => $table1, param => {$key1 => 0, $key2 => 2, $key3 => 3}); |
|
cleanup test
|
2334 |
$dbi->update( |
test cleanup in progress
|
2335 |
table => $table1, |
2336 |
primary_key => $key1, |
|
cleanup test
|
2337 |
id => 0, |
test cleanup in progress
|
2338 |
param => {$key3 => 4} |
cleanup test
|
2339 |
); |
test cleanup in progress
|
2340 |
is($dbi->select(table => $table1)->one->{$key1}, 0); |
2341 |
is($dbi->select(table => $table1)->one->{$key2}, 2); |
|
2342 |
is($dbi->select(table => $table1)->one->{$key3}, 4); |
|
cleanup test
|
2343 | |
2344 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
2345 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
2346 |
$dbi->execute($create_table1_2); |
test cleanup in progress
|
2347 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3}); |
cleanup test
|
2348 |
$dbi->update( |
test cleanup in progress
|
2349 |
{$key3 => 4}, |
2350 |
table => $table1, |
|
2351 |
primary_key => [$key1, $key2], |
|
cleanup test
|
2352 |
id => [1, 2] |
2353 |
); |
|
test cleanup in progress
|
2354 |
is($dbi->select(table => $table1)->one->{$key1}, 1); |
2355 |
is($dbi->select(table => $table1)->one->{$key2}, 2); |
|
2356 |
is($dbi->select(table => $table1)->one->{$key3}, 4); |
|
cleanup test
|
2357 | |
2358 | ||
2359 |
test 'model update and id option'; |
|
2360 |
$dbi = MyDBI6->connect; |
|
finishied sqlite test cleanu...
|
2361 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
2362 |
$dbi->execute($create_table1_2); |
test cleanup in progress
|
2363 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3}); |
2364 |
$dbi->model($table1)->update( |
|
cleanup test
|
2365 |
id => [1, 2], |
test cleanup in progress
|
2366 |
param => {$key3 => 4} |
cleanup test
|
2367 |
); |
test cleanup in progress
|
2368 |
$result = $dbi->model($table1)->select; |
cleanup test
|
2369 |
$row = $result->one; |
test cleanup in progress
|
2370 |
is($row->{$key1}, 1); |
2371 |
is($row->{$key2}, 2); |
|
2372 |
is($row->{$key3}, 4); |
|
cleanup test
|
2373 | |
2374 | ||
2375 |
test 'delete and id option'; |
|
2376 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
2377 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
2378 |
$dbi->execute($create_table1_2); |
test cleanup in progress
|
2379 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3}); |
cleanup test
|
2380 |
$dbi->delete( |
test cleanup in progress
|
2381 |
table => $table1, |
2382 |
primary_key => [$key1, $key2], |
|
cleanup test
|
2383 |
id => [1, 2], |
2384 |
); |
|
test cleanup in progress
|
2385 |
is_deeply($dbi->select(table => $table1)->all, []); |
cleanup test
|
2386 | |
test cleanup in progress
|
2387 |
$dbi->insert(table => $table1, param => {$key1 => 0, $key2 => 2, $key3 => 3}); |
cleanup test
|
2388 |
$dbi->delete( |
test cleanup in progress
|
2389 |
table => $table1, |
2390 |
primary_key => $key1, |
|
cleanup test
|
2391 |
id => 0, |
2392 |
); |
|
test cleanup in progress
|
2393 |
is_deeply($dbi->select(table => $table1)->all, []); |
cleanup test
|
2394 | |
2395 | ||
2396 |
test 'model delete and id option'; |
|
2397 |
$dbi = MyDBI6->connect; |
|
finishied sqlite test cleanu...
|
2398 |
eval { $dbi->execute("drop table $table1") }; |
2399 |
eval { $dbi->execute("drop table $table2") }; |
|
2400 |
eval { $dbi->execute("drop table $table3") }; |
|
cleanup test
|
2401 |
$dbi->execute($create_table1_2); |
2402 |
$dbi->execute($create_table2_2); |
|
2403 |
$dbi->execute($create_table3); |
|
test cleanup in progress
|
2404 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3}); |
2405 |
$dbi->model($table1)->delete(id => [1, 2]); |
|
2406 |
is_deeply($dbi->select(table => $table1)->all, []); |
|
2407 |
$dbi->insert(table => $table2, param => {$key1 => 1, $key2 => 2, $key3 => 3}); |
|
2408 |
$dbi->model($table1)->delete(id => [1, 2]); |
|
2409 |
is_deeply($dbi->select(table => $table1)->all, []); |
|
2410 |
$dbi->insert(table => $table3, param => {$key1 => 1, $key2 => 2, $key3 => 3}); |
|
2411 |
$dbi->model($table3)->delete(id => [1, 2]); |
|
2412 |
is_deeply($dbi->select(table => $table3)->all, []); |
|
cleanup test
|
2413 | |
2414 | ||
2415 |
test 'select and id option'; |
|
2416 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
2417 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
2418 |
$dbi->execute($create_table1_2); |
test cleanup in progress
|
2419 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3}); |
cleanup test
|
2420 |
$result = $dbi->select( |
test cleanup in progress
|
2421 |
table => $table1, |
2422 |
primary_key => [$key1, $key2], |
|
cleanup test
|
2423 |
id => [1, 2] |
2424 |
); |
|
2425 |
$row = $result->one; |
|
test cleanup in progress
|
2426 |
is($row->{$key1}, 1); |
2427 |
is($row->{$key2}, 2); |
|
2428 |
is($row->{$key3}, 3); |
|
cleanup test
|
2429 | |
test cleanup in progress
|
2430 |
$dbi->delete_all(table => $table1); |
2431 |
$dbi->insert(table => $table1, param => {$key1 => 0, $key2 => 2, $key3 => 3}); |
|
cleanup test
|
2432 |
$result = $dbi->select( |
test cleanup in progress
|
2433 |
table => $table1, |
2434 |
primary_key => $key1, |
|
cleanup test
|
2435 |
id => 0, |
2436 |
); |
|
2437 |
$row = $result->one; |
|
test cleanup in progress
|
2438 |
is($row->{$key1}, 0); |
2439 |
is($row->{$key2}, 2); |
|
2440 |
is($row->{$key3}, 3); |
|
cleanup test
|
2441 | |
test cleanup in progress
|
2442 |
$dbi->delete_all(table => $table1); |
2443 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3}); |
|
cleanup test
|
2444 |
$result = $dbi->select( |
test cleanup in progress
|
2445 |
table => $table1, |
2446 |
primary_key => [$key1, $key2], |
|
cleanup test
|
2447 |
id => [1, 2] |
2448 |
); |
|
2449 |
$row = $result->one; |
|
test cleanup in progress
|
2450 |
is($row->{$key1}, 1); |
2451 |
is($row->{$key2}, 2); |
|
2452 |
is($row->{$key3}, 3); |
|
cleanup test
|
2453 | |
2454 | ||
2455 |
test 'model select_at'; |
|
2456 |
$dbi = MyDBI6->connect; |
|
finishied sqlite test cleanu...
|
2457 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
2458 |
$dbi->execute($create_table1_2); |
test cleanup in progress
|
2459 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3}); |
2460 |
$result = $dbi->model($table1)->select(id => [1, 2]); |
|
cleanup test
|
2461 |
$row = $result->one; |
test cleanup in progress
|
2462 |
is($row->{$key1}, 1); |
2463 |
is($row->{$key2}, 2); |
|
2464 |
is($row->{$key3}, 3); |
|
cleanup test
|
2465 | |
2466 |
test 'column separator is default .'; |
|
2467 |
$dbi = MyDBI7->connect; |
|
finishied sqlite test cleanu...
|
2468 |
eval { $dbi->execute("drop table $table1") }; |
2469 |
eval { $dbi->execute("drop table $table2") }; |
|
cleanup test
|
2470 |
$dbi->execute($create_table1); |
2471 |
$dbi->execute($create_table2); |
|
2472 |
$dbi->setup_model; |
|
test cleanup in progress
|
2473 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
2474 |
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3}); |
|
2475 |
$model = $dbi->model($table1); |
|
cleanup test
|
2476 |
$result = $model->select( |
test cleanup in progress
|
2477 |
column => [$model->column($table2)], |
finishied sqlite test cleanu...
|
2478 |
where => {"$table1.$key1" => 1} |
cleanup test
|
2479 |
); |
2480 |
is_deeply($result->one, |
|
test cleanup in progress
|
2481 |
{"$table2.$key1" => 1, "$table2.$key3" => 3}); |
cleanup test
|
2482 | |
2483 |
$result = $model->select( |
|
finishied sqlite test cleanu...
|
2484 |
column => [$model->column($table2 => [$key1, $key3])], |
2485 |
where => {"$table1.$key1" => 1} |
|
cleanup test
|
2486 |
); |
2487 |
is_deeply($result->one, |
|
test cleanup in progress
|
2488 |
{"$table2.$key1" => 1, "$table2.$key3" => 3}); |
test cleanup
|
2489 | |
cleanup test
|
2490 |
test 'separator'; |
2491 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
2492 |
eval { $dbi->execute("drop table $table1") }; |
2493 |
eval { $dbi->execute("drop table $table2") }; |
|
cleanup test
|
2494 |
$dbi->execute($create_table1); |
2495 |
$dbi->execute($create_table2); |
|
test cleanup
|
2496 | |
cleanup test
|
2497 |
$dbi->create_model( |
test cleanup in progress
|
2498 |
table => $table1, |
cleanup test
|
2499 |
join => [ |
test cleanup in progress
|
2500 |
"left outer join $table2 on $table1.$key1 = $table2.$key1" |
cleanup test
|
2501 |
], |
test cleanup in progress
|
2502 |
primary_key => [$key1], |
cleanup test
|
2503 |
); |
2504 |
$model2 = $dbi->create_model( |
|
test cleanup in progress
|
2505 |
table => $table2, |
cleanup test
|
2506 |
); |
2507 |
$dbi->setup_model; |
|
test cleanup in progress
|
2508 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
2509 |
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3}); |
|
2510 |
$model = $dbi->model($table1); |
|
cleanup test
|
2511 |
$result = $model->select( |
2512 |
column => [ |
|
2513 |
$model->mycolumn, |
|
finishied sqlite test cleanu...
|
2514 |
{$table2 => [$key1, $key3]} |
cleanup test
|
2515 |
], |
finishied sqlite test cleanu...
|
2516 |
where => {"$table1.$key1" => 1} |
cleanup test
|
2517 |
); |
2518 |
is_deeply($result->one, |
|
test cleanup in progress
|
2519 |
{$key1 => 1, $key2 => 2, "$table2.$key1" => 1, "$table2.$key3" => 3}); |
2520 |
is_deeply($model2->select->one, {$key1 => 1, $key3 => 3}); |
|
test cleanup
|
2521 | |
cleanup test
|
2522 |
$dbi->separator('__'); |
test cleanup in progress
|
2523 |
$model = $dbi->model($table1); |
cleanup test
|
2524 |
$result = $model->select( |
2525 |
column => [ |
|
2526 |
$model->mycolumn, |
|
finishied sqlite test cleanu...
|
2527 |
{$table2 => [$key1, $key3]} |
cleanup test
|
2528 |
], |
finishied sqlite test cleanu...
|
2529 |
where => {"$table1.$key1" => 1} |
cleanup test
|
2530 |
); |
2531 |
is_deeply($result->one, |
|
test cleanup in progress
|
2532 |
{$key1 => 1, $key2 => 2, "${table2}__$key1" => 1, "${table2}__$key3" => 3}); |
2533 |
is_deeply($model2->select->one, {$key1 => 1, $key3 => 3}); |
|
test cleanup
|
2534 | |
cleanup test
|
2535 |
$dbi->separator('-'); |
test cleanup in progress
|
2536 |
$model = $dbi->model($table1); |
cleanup test
|
2537 |
$result = $model->select( |
2538 |
column => [ |
|
2539 |
$model->mycolumn, |
|
finishied sqlite test cleanu...
|
2540 |
{$table2 => [$key1, $key3]} |
cleanup test
|
2541 |
], |
finishied sqlite test cleanu...
|
2542 |
where => {"$table1.$key1" => 1} |
cleanup test
|
2543 |
); |
2544 |
is_deeply($result->one, |
|
test cleanup in progress
|
2545 |
{$key1 => 1, $key2 => 2, "$table2-$key1" => 1, "$table2-$key3" => 3}); |
2546 |
is_deeply($model2->select->one, {$key1 => 1, $key3 => 3}); |
|
test cleanup
|
2547 | |
2548 | ||
cleanup test
|
2549 |
test 'filter_off'; |
2550 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
2551 |
eval { $dbi->execute("drop table $table1") }; |
2552 |
eval { $dbi->execute("drop table $table2") }; |
|
cleanup test
|
2553 |
$dbi->execute($create_table1); |
2554 |
$dbi->execute($create_table2); |
|
test cleanup
|
2555 | |
cleanup test
|
2556 |
$dbi->create_model( |
test cleanup in progress
|
2557 |
table => $table1, |
cleanup test
|
2558 |
join => [ |
test cleanup in progress
|
2559 |
"left outer join $table2 on $table1.$key1 = $table2.$key1" |
cleanup test
|
2560 |
], |
test cleanup in progress
|
2561 |
primary_key => [$key1], |
cleanup test
|
2562 |
); |
2563 |
$dbi->setup_model; |
|
test cleanup in progress
|
2564 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
2565 |
$model = $dbi->model($table1); |
|
2566 |
$result = $model->select(column => $key1); |
|
2567 |
$result->filter($key1 => sub { $_[0] * 2 }); |
|
2568 |
is_deeply($result->one, {$key1 => 2}); |
|
test cleanup
|
2569 | |
cleanup test
|
2570 |
test 'available_datetype'; |
2571 |
$dbi = DBIx::Custom->connect; |
|
2572 |
ok($dbi->can('available_datatype')); |
|
test cleanup
|
2573 | |
2574 | ||
cleanup test
|
2575 |
test 'select prefix option'; |
2576 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
2577 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
2578 |
$dbi->execute($create_table1); |
test cleanup in progress
|
2579 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
finishied sqlite test cleanu...
|
2580 |
$rows = $dbi->select(prefix => "$key1,", column => $key2, table => $table1)->all; |
test cleanup in progress
|
2581 |
is_deeply($rows, [{$key1 => 1, $key2 => 2}], "table"); |
test cleanup
|
2582 | |
cleanup test
|
2583 |
test 'map_param'; |
2584 |
$dbi = DBIx::Custom->connect; |
|
2585 |
$param = $dbi->map_param( |
|
2586 |
{id => 1, author => 'Ken', price => 1900}, |
|
finishied sqlite test cleanu...
|
2587 |
id => "$table1.id", |
2588 |
author => ["$table1.author", sub { '%' . $_[0] . '%' }], |
|
2589 |
price => ["$table1.price", {if => sub { $_[0] eq 1900 }}] |
|
cleanup test
|
2590 |
); |
test cleanup in progress
|
2591 |
is_deeply($param, {"$table1.id" => 1, "$table1.author" => '%Ken%', |
finishied sqlite test cleanu...
|
2592 |
"$table1.price" => 1900}); |
test cleanup
|
2593 | |
cleanup test
|
2594 |
$param = $dbi->map_param( |
2595 |
{id => 0, author => 0, price => 0}, |
|
finishied sqlite test cleanu...
|
2596 |
id => "$table1.id", |
2597 |
author => ["$table1.author", sub { '%' . $_[0] . '%' }], |
|
2598 |
price => ["$table1.price", sub { '%' . $_[0] . '%' }, |
|
cleanup test
|
2599 |
{if => sub { $_[0] eq 0 }}] |
2600 |
); |
|
test cleanup in progress
|
2601 |
is_deeply($param, {"$table1.id" => 0, "$table1.author" => '%0%', "$table1.price" => '%0%'}); |
test cleanup
|
2602 | |
cleanup test
|
2603 |
$param = $dbi->map_param( |
2604 |
{id => '', author => '', price => ''}, |
|
finishied sqlite test cleanu...
|
2605 |
id => "$table1.id", |
2606 |
author => ["$table1.author", sub { '%' . $_[0] . '%' }], |
|
2607 |
price => ["$table1.price", sub { '%' . $_[0] . '%' }, |
|
cleanup test
|
2608 |
{if => sub { $_[0] eq 1 }}] |
2609 |
); |
|
2610 |
is_deeply($param, {}); |
|
test cleanup
|
2611 | |
cleanup test
|
2612 |
$param = $dbi->map_param( |
2613 |
{id => undef, author => undef, price => undef}, |
|
finishied sqlite test cleanu...
|
2614 |
id => "$table1.id", |
2615 |
price => ["$table1.price", {if => 'exists'}] |
|
cleanup test
|
2616 |
); |
finishied sqlite test cleanu...
|
2617 |
is_deeply($param, {"$table1.price" => undef}); |
test cleanup
|
2618 | |
cleanup test
|
2619 |
$param = $dbi->map_param( |
2620 |
{price => 'a'}, |
|
finishied sqlite test cleanu...
|
2621 |
id => ["$table1.id", {if => 'exists'}], |
2622 |
price => ["$table1.price", sub { '%' . $_[0] }, {if => 'exists'}] |
|
cleanup test
|
2623 |
); |
finishied sqlite test cleanu...
|
2624 |
is_deeply($param, {"$table1.price" => '%a'}); |
test cleanup
|
2625 | |
test cleanup
|
2626 |
test 'order'; |
2627 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
2628 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
2629 |
$dbi->execute($create_table1); |
test cleanup in progress
|
2630 |
$dbi->insert({$key1 => 1, $key2 => 1}, table => $table1); |
2631 |
$dbi->insert({$key1 => 1, $key2 => 3}, table => $table1); |
|
2632 |
$dbi->insert({$key1 => 2, $key2 => 2}, table => $table1); |
|
2633 |
$dbi->insert({$key1 => 2, $key2 => 4}, table => $table1); |
|
test cleanup
|
2634 |
my $order = $dbi->order; |
finishied sqlite test cleanu...
|
2635 |
$order->prepend($key1, "$key2 desc"); |
2636 |
$result = $dbi->select(table => $table1, append => $order); |
|
test cleanup in progress
|
2637 |
is_deeply($result->all, [{$key1 => 1, $key2 => 3}, {$key1 => 1, $key2 => 1}, |
2638 |
{$key1 => 2, $key2 => 4}, {$key1 => 2, $key2 => 2}]); |
|
finishied sqlite test cleanu...
|
2639 |
$order->prepend("$key1 desc"); |
2640 |
$result = $dbi->select(table => $table1, append => $order); |
|
test cleanup in progress
|
2641 |
is_deeply($result->all, [{$key1 => 2, $key2 => 4}, {$key1 => 2, $key2 => 2}, |
2642 |
{$key1 => 1, $key2 => 3}, {$key1 => 1, $key2 => 1}]); |
|
test cleanup
|
2643 | |
2644 |
$order = $dbi->order; |
|
finishied sqlite test cleanu...
|
2645 |
$order->prepend(["$table1-$key1"], ["$table1-$key2", 'desc']); |
test cleanup in progress
|
2646 |
$result = $dbi->select(table => $table1, |
2647 |
column => [[$key1 => "$table1-$key1"], [$key2 => "$table1-$key2"]], |
|
finishied sqlite test cleanu...
|
2648 |
append => $order); |
test cleanup in progress
|
2649 |
is_deeply($result->all, [{"$table1-$key1" => 1, "$table1-$key2" => 3}, |
2650 |
{"$table1-$key1" => 1, "$table1-$key2" => 1}, |
|
2651 |
{"$table1-$key1" => 2, "$table1-$key2" => 4}, |
|
2652 |
{"$table1-$key1" => 2, "$table1-$key2" => 2}]); |
|
test cleanup
|
2653 | |
2654 |
test 'tag_parse'; |
|
2655 |
$dbi = DBIx::Custom->connect; |
|
2656 |
$dbi->tag_parse(0); |
|
finishied sqlite test cleanu...
|
2657 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
2658 |
$dbi->execute($create_table1); |
test cleanup in progress
|
2659 |
$dbi->insert({$key1 => 1, $key2 => 1}, table => $table1); |
2660 |
eval {$dbi->execute("select * from $table1 where {= $key1}", {$key1 => 1})}; |
|
test cleanup
|
2661 |
ok($@); |
2662 | ||
2663 |
test 'last_sql'; |
|
2664 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
2665 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
2666 |
$dbi->execute($create_table1); |
finishied sqlite test cleanu...
|
2667 |
$dbi->execute("select * from $table1"); |
cleanup test
|
2668 |
is($dbi->last_sql, "select * from $table1"); |
test cleanup
|
2669 | |
2670 |
eval{$dbi->execute("aaa")}; |
|
cleanup test
|
2671 |
is($dbi->last_sql, 'aaa'); |
test cleanup
|
2672 | |
2673 |
test 'DBIx::Custom header'; |
|
2674 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
2675 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
2676 |
$dbi->execute($create_table1); |
test cleanup in progress
|
2677 |
$result = $dbi->execute("select $key1 as h1, $key2 as h2 from $table1"); |
test cleanup
|
2678 |
is_deeply([map { lc } @{$result->header}], [qw/h1 h2/]); |
test cleanup
|
2679 | |
2680 |
test 'Named placeholder :name(operater) syntax'; |
|
finishied sqlite test cleanu...
|
2681 |
$dbi->execute("drop table $table1"); |
test cleanup
|
2682 |
$dbi->execute($create_table1_2); |
test cleanup in progress
|
2683 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}); |
2684 |
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10}); |
|
test cleanup
|
2685 | |
test cleanup in progress
|
2686 |
$source = "select * from $table1 where :${key1}{=} and :${key2}{=}"; |
2687 |
$result = $dbi->execute($source, param => {$key1 => 1, $key2 => 2}); |
|
test cleanup
|
2688 |
$rows = $result->all; |
test cleanup in progress
|
2689 |
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}]); |
test cleanup
|
2690 | |
test cleanup in progress
|
2691 |
$source = "select * from $table1 where :${key1}{ = } and :${key2}{=}"; |
2692 |
$result = $dbi->execute($source, param => {$key1 => 1, $key2 => 2}); |
|
test cleanup
|
2693 |
$rows = $result->all; |
test cleanup in progress
|
2694 |
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}]); |
test cleanup
|
2695 | |
test cleanup in progress
|
2696 |
$source = "select * from $table1 where :${key1}{<} and :${key2}{=}"; |
2697 |
$result = $dbi->execute($source, param => {$key1 => 5, $key2 => 2}); |
|
test cleanup
|
2698 |
$rows = $result->all; |
test cleanup in progress
|
2699 |
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}]); |
test cleanup
|
2700 | |
test cleanup in progress
|
2701 |
$source = "select * from $table1 where :$table1.${key1}{=} and :$table1.${key2}{=}"; |
test cleanup
|
2702 |
$result = $dbi->execute( |
2703 |
$source, |
|
test cleanup in progress
|
2704 |
param => {"$table1.$key1" => 1, "$table1.$key2" => 1}, |
finishied sqlite test cleanu...
|
2705 |
filter => {"$table1.$key2" => sub { $_[0] * 2 }} |
test cleanup
|
2706 |
); |
2707 |
$rows = $result->all; |
|
test cleanup in progress
|
2708 |
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}]); |
test cleanup
|
2709 | |
2710 |
test 'high perfomance way'; |
|
finishied sqlite test cleanu...
|
2711 |
$dbi->execute("drop table $table1"); |
test cleanup
|
2712 |
$dbi->execute($create_table1_highperformance); |
2713 |
$rows = [ |
|
test cleanup in progress
|
2714 |
{$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7}, |
2715 |
{$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8}, |
|
test cleanup
|
2716 |
]; |
2717 |
{ |
|
2718 |
my $query; |
|
2719 |
foreach my $row (@$rows) { |
|
test cleanup in progress
|
2720 |
$query ||= $dbi->insert($row, table => $table1, query => 1); |
2721 |
$dbi->execute($query, $row, filter => {$key7 => sub { $_[0] * 2 }}); |
|
test cleanup
|
2722 |
} |
test cleanup in progress
|
2723 |
is_deeply($dbi->select(table => $table1)->all, |
test cleanup
|
2724 |
[ |
test cleanup in progress
|
2725 |
{$key7 => 2, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7}, |
2726 |
{$key7 => 2, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8}, |
|
test cleanup
|
2727 |
] |
2728 |
); |
|
2729 |
} |
|
2730 | ||
finishied sqlite test cleanu...
|
2731 |
$dbi->execute("drop table $table1"); |
test cleanup
|
2732 |
$dbi->execute($create_table1_highperformance); |
2733 |
$rows = [ |
|
test cleanup in progress
|
2734 |
{$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7}, |
2735 |
{$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8}, |
|
test cleanup
|
2736 |
]; |
2737 |
{ |
|
2738 |
my $query; |
|
2739 |
my $sth; |
|
2740 |
foreach my $row (@$rows) { |
|
test cleanup in progress
|
2741 |
$query ||= $dbi->insert($row, table => $table1, query => 1); |
test cleanup
|
2742 |
$sth ||= $query->sth; |
2743 |
$sth->execute(map { $row->{$_} } sort keys %$row); |
|
2744 |
} |
|
test cleanup in progress
|
2745 |
is_deeply($dbi->select(table => $table1)->all, |
test cleanup
|
2746 |
[ |
test cleanup in progress
|
2747 |
{$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 7}, |
2748 |
{$key7 => 1, $key6 => 2, $key5 => 3, $key4 => 4, $key3 => 5, $key2 => 6, $key1 => 8}, |
|
test cleanup
|
2749 |
] |
2750 |
); |
|
2751 |
} |
|
2752 | ||
2753 |
test 'result'; |
|
2754 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
2755 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
2756 |
$dbi->execute($create_table1); |
test cleanup in progress
|
2757 |
$dbi->insert({$key1 => 1, $key2 => 2}, table => $table1); |
2758 |
$dbi->insert({$key1 => 3, $key2 => 4}, table => $table1); |
|
test cleanup
|
2759 | |
test cleanup in progress
|
2760 |
$result = $dbi->select(table => $table1); |
test cleanup
|
2761 |
@rows = (); |
2762 |
while (my $row = $result->fetch) { |
|
2763 |
push @rows, [@$row]; |
|
2764 |
} |
|
2765 |
is_deeply(\@rows, [[1, 2], [3, 4]]); |
|
2766 | ||
test cleanup in progress
|
2767 |
$result = $dbi->select(table => $table1); |
test cleanup
|
2768 |
@rows = (); |
2769 |
while (my $row = $result->fetch_hash) { |
|
2770 |
push @rows, {%$row}; |
|
2771 |
} |
|
test cleanup in progress
|
2772 |
is_deeply(\@rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]); |
test cleanup
|
2773 | |
2774 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
2775 |
eval { $dbi->execute("drop table $table1") }; |
test cleanup
|
2776 |
$dbi->execute($create_table1); |
test cleanup in progress
|
2777 |
$dbi->insert({$key1 => 1, $key2 => 2}, table => $table1); |
2778 |
$dbi->insert({$key1 => 3, $key2 => 4}, table => $table1); |
|
test cleanup
|
2779 | |
2780 |
test 'fetch_all'; |
|
test cleanup in progress
|
2781 |
$result = $dbi->select(table => $table1); |
test cleanup
|
2782 |
$rows = $result->fetch_all; |
2783 |
is_deeply($rows, [[1, 2], [3, 4]]); |
|
2784 | ||
test cleanup in progress
|
2785 |
$result = $dbi->select(table => $table1); |
test cleanup
|
2786 |
$rows = $result->fetch_hash_all; |
test cleanup in progress
|
2787 |
is_deeply($rows, [{$key1 => 1, $key2 => 2}, {$key1 => 3, $key2 => 4}]); |
test cleanup
|
2788 | |
test cleanup in progress
|
2789 |
$result = $dbi->select(table => $table1); |
test cleanup
|
2790 |
$result->dbi->filters({three_times => sub { $_[0] * 3}}); |
test cleanup in progress
|
2791 |
$result->filter({$key1 => 'three_times'}); |
test cleanup
|
2792 | |
2793 |
$rows = $result->fetch_all; |
|
2794 |
is_deeply($rows, [[3, 2], [9, 4]], "array"); |
|
2795 | ||
test cleanup in progress
|
2796 |
$result = $dbi->select(table => $table1); |
test cleanup
|
2797 |
$result->dbi->filters({three_times => sub { $_[0] * 3}}); |
test cleanup in progress
|
2798 |
$result->filter({$key1 => 'three_times'}); |
test cleanup
|
2799 |
$rows = $result->fetch_hash_all; |
test cleanup in progress
|
2800 |
is_deeply($rows, [{$key1 => 3, $key2 => 2}, {$key1 => 9, $key2 => 4}], "hash"); |
test cleanup
|
2801 | |
2802 |
test "query_builder"; |
|
2803 |
$datas = [ |
|
2804 |
# Basic tests |
|
2805 |
{ name => 'placeholder basic', |
|
2806 |
source => "a {? k1} b {= k2} {<> k3} {> k4} {< k5} {>= k6} {<= k7} {like k8}", , |
|
cleanup test
|
2807 |
sql_expected => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?", |
test cleanup
|
2808 |
columns_expected => [qw/k1 k2 k3 k4 k5 k6 k7 k8/] |
2809 |
}, |
|
2810 |
{ |
|
2811 |
name => 'placeholder in', |
|
cleanup test
|
2812 |
source => "{in k1 3}", |
2813 |
sql_expected => "k1 in (?, ?, ?)", |
|
test cleanup
|
2814 |
columns_expected => [qw/k1 k1 k1/] |
2815 |
}, |
|
2816 |
|
|
2817 |
# Table name |
|
2818 |
{ |
|
2819 |
name => 'placeholder with table name', |
|
2820 |
source => "{= a.k1} {= a.k2}", |
|
cleanup test
|
2821 |
sql_expected => "a.k1 = ? a.k2 = ?", |
test cleanup
|
2822 |
columns_expected => [qw/a.k1 a.k2/] |
2823 |
}, |
|
2824 |
{ |
|
2825 |
name => 'placeholder in with table name', |
|
2826 |
source => "{in a.k1 2} {in b.k2 2}", |
|
cleanup test
|
2827 |
sql_expected => "a.k1 in (?, ?) b.k2 in (?, ?)", |
test cleanup
|
2828 |
columns_expected => [qw/a.k1 a.k1 b.k2 b.k2/] |
2829 |
}, |
|
2830 |
{ |
|
2831 |
name => 'not contain tag', |
|
2832 |
source => "aaa", |
|
cleanup test
|
2833 |
sql_expected => "aaa", |
test cleanup
|
2834 |
columns_expected => [], |
2835 |
} |
|
2836 |
]; |
|
2837 | ||
2838 |
for (my $i = 0; $i < @$datas; $i++) { |
|
2839 |
my $data = $datas->[$i]; |
|
cleanup
|
2840 |
my $dbi = DBIx::Custom->new; |
2841 |
my $builder = $dbi->query_builder; |
|
test cleanup
|
2842 |
my $query = $builder->build_query($data->{source}); |
2843 |
is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql"); |
|
2844 |
is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns"); |
|
2845 |
} |
|
2846 | ||
cleanup
|
2847 |
$dbi = DBIx::Custom->new; |
2848 |
$builder = $dbi->query_builder; |
|
2849 |
$dbi->register_tag( |
|
test cleanup
|
2850 |
p => sub { |
2851 |
my @args = @_; |
|
2852 |
|
|
2853 |
my $expand = "? $args[0] $args[1]"; |
|
2854 |
my $columns = [2]; |
|
2855 |
return [$expand, $columns]; |
|
2856 |
} |
|
2857 |
); |
|
2858 | ||
2859 |
$query = $builder->build_query("{p a b}"); |
|
cleanup test
|
2860 |
is($query->{sql}, "? a b", "register_tag sql"); |
test cleanup
|
2861 |
is_deeply($query->{columns}, [2], "register_tag columns"); |
2862 | ||
2863 |
eval{$builder->build_query('{? }')}; |
|
2864 |
like($@, qr/\QColumn name must be specified in tag "{? }"/, "? not arguments"); |
|
2865 | ||
2866 |
eval{$builder->build_query("{a }")}; |
|
2867 |
like($@, qr/\QTag "a" is not registered/, "tag not exist"); |
|
2868 | ||
cleanup
|
2869 |
$dbi->register_tag({ |
test cleanup
|
2870 |
q => 'string' |
2871 |
}); |
|
2872 | ||
2873 |
eval{$builder->build_query("{q}", {})}; |
|
2874 |
like($@, qr/Tag "q" must be sub reference/, "tag not code ref"); |
|
2875 | ||
cleanup
|
2876 |
$dbi->register_tag({ |
test cleanup
|
2877 |
r => sub {} |
2878 |
}); |
|
2879 | ||
2880 |
eval{$builder->build_query("{r}")}; |
|
2881 |
like($@, qr/\QTag "r" must return [STRING, ARRAY_REFERENCE]/, "tag return noting"); |
|
2882 | ||
cleanup
|
2883 |
$dbi->register_tag({ |
test cleanup
|
2884 |
s => sub { return ["a", ""]} |
2885 |
}); |
|
2886 | ||
2887 |
eval{$builder->build_query("{s}")}; |
|
2888 |
like($@, qr/\QTag "s" must return [STRING, ARRAY_REFERENCE]/, "tag return not array columns"); |
|
2889 | ||
cleanup
|
2890 |
$dbi->register_tag( |
test cleanup
|
2891 |
t => sub {return ["a", []]} |
2892 |
); |
|
2893 | ||
2894 | ||
cleanup
|
2895 |
$dbi->register_tag( |
test cleanup
|
2896 |
a => sub { |
2897 |
return ["? ? ?", ['']]; |
|
2898 |
} |
|
2899 |
); |
|
2900 |
eval{$builder->build_query("{a}")}; |
|
2901 |
like($@, qr/\QPlaceholder count/, "placeholder count is invalid"); |
|
2902 | ||
2903 | ||
cleanup test
|
2904 |
test 'Default tag Error case'; |
2905 |
eval{$builder->build_query("{= }")}; |
|
2906 |
like($@, qr/Column name must be specified in tag "{= }"/, "basic '=' : key not exist"); |
|
2907 | ||
2908 |
eval{$builder->build_query("{in }")}; |
|
2909 |
like($@, qr/Column name and count of values must be specified in tag "{in }"/, "in : key not exist"); |
|
2910 | ||
2911 |
eval{$builder->build_query("{in a}")}; |
|
2912 |
like($@, qr/\QColumn name and count of values must be specified in tag "{in }"/, |
|
2913 |
"in : key not exist"); |
|
2914 | ||
2915 |
eval{$builder->build_query("{in a r}")}; |
|
2916 |
like($@, qr/\QColumn name and count of values must be specified in tag "{in }"/, |
|
2917 |
"in : key not exist"); |
|
2918 | ||
2919 |
test 'variouse source'; |
|
2920 |
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d"; |
|
2921 |
$query = $builder->build_query($source); |
|
2922 |
is($query->sql, 'a b = ? c { } { = ? } = ? d', "basic : 1"); |
|
2923 | ||
2924 |
$source = "abc"; |
|
2925 |
$query = $builder->build_query($source); |
|
2926 |
is($query->sql, 'abc', "basic : 2"); |
|
2927 | ||
2928 |
$source = "{= a}"; |
|
2929 |
$query = $builder->build_query($source); |
|
2930 |
is($query->sql, 'a = ?', "only tag"); |
|
2931 | ||
2932 |
$source = "000"; |
|
2933 |
$query = $builder->build_query($source); |
|
2934 |
is($query->sql, '000', "contain 0 value"); |
|
2935 | ||
2936 |
$source = "a {= b} }"; |
|
2937 |
eval{$builder->build_query($source)}; |
|
2938 |
like($@, qr/unexpected "}"/, "error : 1"); |
|
2939 | ||
2940 |
$source = "a {= {}"; |
|
2941 |
eval{$builder->build_query($source)}; |
|
2942 |
like($@, qr/unexpected "{"/, "error : 2"); |
|
2943 | ||
2944 |
test 'select() wrap option'; |
|
2945 |
$dbi = DBIx::Custom->connect; |
|
2946 |
eval { $dbi->execute("drop table $table1") }; |
|
2947 |
$dbi->execute($create_table1); |
|
2948 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
|
2949 |
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3}); |
|
2950 |
$rows = $dbi->select( |
|
2951 |
table => $table1, |
|
2952 |
column => $key1, |
|
test cleanup
|
2953 |
wrap => ["select * from (", ") t where $key1 = 1"] |
cleanup test
|
2954 |
)->all; |
2955 |
is_deeply($rows, [{$key1 => 1}]); |
|
2956 | ||
2957 |
eval { |
|
2958 |
$dbi->select( |
|
2959 |
table => $table1, |
|
2960 |
column => $key1, |
|
2961 |
wrap => 'select * from (' |
|
2962 |
) |
|
2963 |
}; |
|
2964 |
like($@, qr/array/); |
|
2965 | ||
2966 |
test 'select() sqlfilter option'; |
|
2967 |
$dbi = DBIx::Custom->connect; |
|
2968 |
eval { $dbi->execute("drop table $table1") }; |
|
2969 |
$dbi->execute($create_table1); |
|
2970 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
|
2971 |
$dbi->insert(table => $table1, param => {$key1 => 2, $key2 => 3}); |
|
2972 |
$rows = $dbi->select( |
|
2973 |
table => $table1, |
|
2974 |
column => $key1, |
|
2975 |
sqlfilter => sub { |
|
2976 |
my $sql = shift; |
|
test cleanup
|
2977 |
$sql = "select * from ( $sql ) t where $key1 = 1"; |
cleanup test
|
2978 |
return $sql; |
2979 |
} |
|
2980 |
)->all; |
|
2981 |
is_deeply($rows, [{$key1 => 1}]); |
|
2982 | ||
2983 |
test 'dbi method from model'; |
|
2984 |
$dbi = MyDBI9->connect; |
|
2985 |
eval { $dbi->execute("drop table $table1") }; |
|
2986 |
$dbi->execute($create_table1); |
|
2987 |
$dbi->setup_model; |
|
2988 |
$model = $dbi->model($table1); |
|
2989 |
eval{$model->execute("select * from $table1")}; |
|
2990 |
ok(!$@); |
|
2991 | ||
2992 |
test 'column table option'; |
|
2993 |
$dbi = MyDBI9->connect; |
|
2994 |
eval { $dbi->execute("drop table $table1") }; |
|
2995 |
$dbi->execute($create_table1); |
|
2996 |
eval { $dbi->execute("drop table $table2") }; |
|
2997 |
$dbi->execute($create_table2); |
|
2998 |
$dbi->setup_model; |
|
2999 |
$dbi->execute("insert into $table1 ($key1, $key2) values (1, 2)"); |
|
3000 |
$dbi->execute("insert into $table2 ($key1, $key3) values (1, 4)"); |
|
3001 |
$model = $dbi->model($table1); |
|
3002 |
$result = $model->select( |
|
3003 |
column => [ |
|
3004 |
$model->column($table2, {alias => $table2_alias}) |
|
3005 |
], |
|
3006 |
where => {"$table2_alias.$key3" => 4} |
|
3007 |
); |
|
3008 |
is_deeply($result->one, |
|
3009 |
{"$table2_alias.$key1" => 1, "$table2_alias.$key3" => 4}); |
|
3010 | ||
3011 |
$dbi->separator('__'); |
|
3012 |
$result = $model->select( |
|
3013 |
column => [ |
|
3014 |
$model->column($table2, {alias => $table2_alias}) |
|
3015 |
], |
|
3016 |
where => {"$table2_alias.$key3" => 4} |
|
3017 |
); |
|
3018 |
is_deeply($result->one, |
|
3019 |
{"${table2_alias}__$key1" => 1, "${table2_alias}__$key3" => 4}); |
|
3020 | ||
3021 |
$dbi->separator('-'); |
|
3022 |
$result = $model->select( |
|
3023 |
column => [ |
|
3024 |
$model->column($table2, {alias => $table2_alias}) |
|
3025 |
], |
|
3026 |
where => {"$table2_alias.$key3" => 4} |
|
3027 |
); |
|
3028 |
is_deeply($result->one, |
|
3029 |
{"$table2_alias-$key1" => 1, "$table2_alias-$key3" => 4}); |
|
3030 | ||
3031 |
test 'create_model'; |
|
3032 |
$dbi = DBIx::Custom->connect; |
|
3033 |
eval { $dbi->execute("drop table $table1") }; |
|
3034 |
eval { $dbi->execute("drop table $table2") }; |
|
3035 |
$dbi->execute($create_table1); |
|
3036 |
$dbi->execute($create_table2); |
|
3037 | ||
3038 |
$dbi->create_model( |
|
3039 |
table => $table1, |
|
3040 |
join => [ |
|
3041 |
"left outer join $table2 on $table1.$key1 = $table2.$key1" |
|
3042 |
], |
|
3043 |
primary_key => [$key1] |
|
3044 |
); |
|
3045 |
$model2 = $dbi->create_model( |
|
3046 |
table => $table2 |
|
3047 |
); |
|
3048 |
$dbi->create_model( |
|
3049 |
table => $table3, |
|
3050 |
filter => [ |
|
3051 |
$key1 => {in => sub { uc $_[0] }} |
|
3052 |
] |
|
3053 |
); |
|
3054 |
$dbi->setup_model; |
|
3055 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
|
3056 |
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3}); |
|
3057 |
$model = $dbi->model($table1); |
|
3058 |
$result = $model->select( |
|
3059 |
column => [$model->mycolumn, $model->column($table2)], |
|
3060 |
where => {"$table1.$key1" => 1} |
|
3061 |
); |
|
3062 |
is_deeply($result->one, |
|
3063 |
{$key1 => 1, $key2 => 2, "$table2.$key1" => 1, "$table2.$key3" => 3}); |
|
3064 |
is_deeply($model2->select->one, {$key1 => 1, $key3 => 3}); |
|
3065 | ||
3066 |
test 'model method'; |
|
3067 |
$dbi = DBIx::Custom->connect; |
|
3068 |
eval { $dbi->execute("drop table $table2") }; |
|
3069 |
$dbi->execute($create_table2); |
|
3070 |
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 3}); |
|
3071 |
$model = $dbi->create_model( |
|
3072 |
table => $table2 |
|
3073 |
); |
|
3074 |
$model->method(foo => sub { shift->select(@_) }); |
|
3075 |
is_deeply($model->foo->one, {$key1 => 1, $key3 => 3}); |
|
3076 | ||
3077 |
test 'update_param'; |
|
3078 |
$dbi = DBIx::Custom->connect; |
|
3079 |
eval { $dbi->execute("drop table $table1") }; |
|
3080 |
$dbi->execute($create_table1_2); |
|
3081 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}); |
|
3082 |
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10}); |
|
3083 | ||
3084 |
$param = {$key2 => 11}; |
|
3085 |
$update_param = $dbi->update_param($param); |
|
3086 |
$sql = <<"EOS"; |
|
3087 |
update $table1 $update_param |
|
3088 |
where $key1 = 1 |
|
3089 |
EOS |
|
3090 |
$dbi->execute($sql, param => $param); |
|
3091 |
$result = $dbi->execute("select * from $table1 order by $key1", table => $table1); |
|
3092 |
$rows = $result->all; |
|
3093 |
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5}, |
|
3094 |
{$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10}], |
|
3095 |
"basic"); |
|
3096 | ||
3097 | ||
3098 |
$dbi = DBIx::Custom->connect; |
|
3099 |
eval { $dbi->execute("drop table $table1") }; |
|
3100 |
$dbi->execute($create_table1_2); |
|
3101 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}); |
|
3102 |
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10}); |
|
3103 | ||
3104 |
$param = {$key2 => 11, $key3 => 33}; |
|
3105 |
$update_param = $dbi->update_param($param); |
|
3106 |
$sql = <<"EOS"; |
|
3107 |
update $table1 $update_param |
|
3108 |
where $key1 = 1 |
|
3109 |
EOS |
|
3110 |
$dbi->execute($sql, param => $param); |
|
3111 |
$result = $dbi->execute("select * from $table1 order by $key1", table => $table1); |
|
3112 |
$rows = $result->all; |
|
3113 |
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 33, $key4 => 4, $key5 => 5}, |
|
3114 |
{$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10}], |
|
3115 |
"basic"); |
|
3116 | ||
3117 |
$dbi = DBIx::Custom->connect; |
|
3118 |
eval { $dbi->execute("drop table $table1") }; |
|
3119 |
$dbi->execute($create_table1_2); |
|
3120 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}); |
|
3121 |
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10}); |
|
3122 | ||
3123 |
$param = {$key2 => 11, $key3 => 33}; |
|
3124 |
$update_param = $dbi->update_param($param, {no_set => 1}); |
|
3125 |
$sql = <<"EOS"; |
|
3126 |
update $table1 set $update_param |
|
3127 |
where $key1 = 1 |
|
3128 |
EOS |
|
3129 |
$dbi->execute($sql, param => $param); |
|
3130 |
$result = $dbi->execute("select * from $table1 order by $key1", table => $table1); |
|
3131 |
$rows = $result->all; |
|
3132 |
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 33, $key4 => 4, $key5 => 5}, |
|
3133 |
{$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10}], |
|
3134 |
"update param no_set"); |
|
3135 | ||
3136 |
|
|
3137 |
eval { $dbi->update_param({";" => 1}) }; |
|
3138 |
like($@, qr/not safety/); |
|
3139 | ||
test cleanup
|
3140 | |
cleanup test
|
3141 |
test 'update_param'; |
3142 |
$dbi = DBIx::Custom->connect; |
|
3143 |
eval { $dbi->execute("drop table $table1") }; |
|
3144 |
$dbi->execute($create_table1_2); |
|
3145 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}); |
|
3146 |
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10}); |
|
test cleanup
|
3147 | |
cleanup test
|
3148 |
$param = {$key2 => 11}; |
3149 |
$update_param = $dbi->assign_param($param); |
|
3150 |
$sql = <<"EOS"; |
|
3151 |
update $table1 set $update_param |
|
3152 |
where $key1 = 1 |
|
3153 |
EOS |
|
3154 |
$dbi->execute($sql, param => $param, table => $table1); |
|
3155 |
$result = $dbi->execute("select * from $table1 order by $key1"); |
|
3156 |
$rows = $result->all; |
|
3157 |
is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5}, |
|
3158 |
{$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10}], |
|
3159 |
"basic"); |
|
test cleanup
|
3160 | |
cleanup test
|
3161 |
test 'Model class'; |
3162 |
$dbi = MyDBI1->connect; |
|
3163 |
eval { $dbi->execute("drop table $table1") }; |
|
3164 |
$dbi->execute($create_table1); |
|
3165 |
$model = $dbi->model($table1); |
|
3166 |
$model->insert({$key1 => 'a', $key2 => 'b'}); |
|
3167 |
is_deeply($model->list->all, [{$key1 => 'a', $key2 => 'b'}], 'basic'); |
|
3168 |
eval { $dbi->execute("drop table $table2") }; |
|
3169 |
$dbi->execute($create_table2); |
|
3170 |
$model = $dbi->model($table2); |
|
3171 |
$model->insert({$key1 => 'a'}); |
|
3172 |
is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'basic'); |
|
3173 |
is($dbi->models->{$table1}, $dbi->model($table1)); |
|
3174 |
is($dbi->models->{$table2}, $dbi->model($table2)); |
|
3175 | ||
3176 |
$dbi = MyDBI4->connect; |
|
3177 |
eval { $dbi->execute("drop table $table1") }; |
|
3178 |
$dbi->execute($create_table1); |
|
3179 |
$model = $dbi->model($table1); |
|
3180 |
$model->insert({$key1 => 'a', $key2 => 'b'}); |
|
3181 |
is_deeply($model->list->all, [{$key1 => 'a', $key2 => 'b'}], 'basic'); |
|
3182 |
eval { $dbi->execute("drop table $table2") }; |
|
3183 |
$dbi->execute($create_table2); |
|
3184 |
$model = $dbi->model($table2); |
|
3185 |
$model->insert({$key1 => 'a'}); |
|
3186 |
is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'basic'); |
|
3187 | ||
3188 |
$dbi = MyDBI5->connect; |
|
3189 |
eval { $dbi->execute("drop table $table1") }; |
|
3190 |
eval { $dbi->execute("drop table $table2") }; |
|
3191 |
$dbi->execute($create_table1); |
|
3192 |
$dbi->execute($create_table2); |
|
3193 |
$model = $dbi->model($table2); |
|
3194 |
$model->insert({$key1 => 'a'}); |
|
3195 |
is_deeply($model->list->all, [{$key1 => 'a', $key3 => undef}], 'include all model'); |
|
3196 |
$dbi->insert(table => $table1, param => {$key1 => 1}); |
|
3197 |
$model = $dbi->model($table1); |
|
3198 |
is_deeply($model->list->all, [{$key1 => 1, $key2 => undef}], 'include all model'); |
|
3199 | ||
3200 |
test 'primary_key'; |
|
3201 |
$dbi = MyDBI1->connect; |
|
3202 |
$model = $dbi->model($table1); |
|
3203 |
$model->primary_key([$key1, $key2]); |
|
3204 |
is_deeply($model->primary_key, [$key1, $key2]); |
|
3205 | ||
3206 |
test 'columns'; |
|
3207 |
$dbi = MyDBI1->connect; |
|
3208 |
$model = $dbi->model($table1); |
|
3209 |
$model->columns([$key1, $key2]); |
|
3210 |
is_deeply($model->columns, [$key1, $key2]); |
|
cleanup test
|
3211 | |
cleanup test
|
3212 |
test 'setup_model'; |
3213 |
$dbi = MyDBI1->connect; |
|
finishied sqlite test cleanu...
|
3214 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
3215 |
eval { $dbi->execute("drop table $table2") }; |
added EXPERIMENTAL execute m...
|
3216 | |
cleanup test
|
3217 |
$dbi->execute($create_table1); |
cleanup test
|
3218 |
$dbi->execute($create_table2); |
cleanup test
|
3219 |
$dbi->setup_model; |
cleanup test
|
3220 |
is_deeply([sort @{$dbi->model($table1)->columns}], [$key1, $key2]); |
3221 |
is_deeply([sort @{$dbi->model($table2)->columns}], [$key1, $key3]); |
|
cleanup test
|
3222 | |
cleanup test
|
3223 |
test 'each_column'; |
3224 |
$dbi = DBIx::Custom->connect; |
|
3225 |
eval { $dbi->execute("drop table ${q}table$p") }; |
|
finishied sqlite test cleanu...
|
3226 |
eval { $dbi->execute("drop table $table1") }; |
3227 |
eval { $dbi->execute("drop table $table2") }; |
|
cleanup test
|
3228 |
eval { $dbi->execute("drop table $table3") }; |
3229 |
$dbi->execute($create_table1_type); |
|
cleanup test
|
3230 |
$dbi->execute($create_table2); |
cleanup test
|
3231 | |
3232 |
$infos = []; |
|
3233 |
$dbi->each_column(sub { |
|
3234 |
my ($self, $table, $column, $cinfo) = @_; |
|
3235 |
|
|
3236 |
if ($table =~ /^table\d/i) { |
|
3237 |
my $info = [$table, $column, $cinfo->{COLUMN_NAME}]; |
|
3238 |
push @$infos, $info; |
|
3239 |
} |
|
3240 |
}); |
|
3241 |
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos]; |
|
3242 |
is_deeply($infos, |
|
3243 |
[ |
|
3244 |
[$table1, $key1, $key1], |
|
3245 |
[$table1, $key2, $key2], |
|
3246 |
[$table2, $key1, $key1], |
|
3247 |
[$table2, $key3, $key3] |
|
3248 |
] |
|
3249 |
|
|
cleanup test
|
3250 |
); |
cleanup test
|
3251 |
test 'each_table'; |
3252 |
$dbi = DBIx::Custom->connect; |
|
3253 |
eval { $dbi->execute("drop table $table1") }; |
|
3254 |
eval { $dbi->execute("drop table $table2") }; |
|
3255 |
$dbi->execute($create_table2); |
|
3256 |
$dbi->execute($create_table1_type); |
|
cleanup test
|
3257 | |
cleanup test
|
3258 |
$infos = []; |
3259 |
$dbi->each_table(sub { |
|
3260 |
my ($self, $table, $table_info) = @_; |
|
3261 |
|
|
3262 |
if ($table =~ /^table\d/i) { |
|
3263 |
my $info = [$table, $table_info->{TABLE_NAME}]; |
|
3264 |
push @$infos, $info; |
|
3265 |
} |
|
3266 |
}); |
|
3267 |
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos]; |
|
3268 |
is_deeply($infos, |
|
3269 |
[ |
|
3270 |
[$table1, $table1], |
|
3271 |
[$table2, $table2], |
|
3272 |
] |
|
cleanup test
|
3273 |
); |
3274 | ||
cleanup test
|
3275 |
test 'type_rule into'; |
3276 |
eval { $dbi->execute("drop table $table1") }; |
|
3277 |
$dbi->execute($create_table1_type); |
|
3278 |
$dbi = DBIx::Custom->connect; |
|
3279 |
eval { $dbi->execute("drop table $table1") }; |
|
3280 |
$dbi->execute($create_table1_type); |
|
3281 | ||
3282 |
$dbi->type_rule( |
|
3283 |
into1 => { |
|
3284 |
$date_typename => sub { '2010-' . $_[0] } |
|
3285 |
} |
|
cleanup test
|
3286 |
); |
cleanup test
|
3287 |
$dbi->insert({$key1 => '01-01'}, table => $table1); |
3288 |
$result = $dbi->select(table => $table1); |
|
3289 |
like($result->one->{$key1}, qr/^2010-01-01/); |
|
cleanup test
|
3290 | |
3291 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
3292 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
3293 |
$dbi->execute($create_table1_type); |
3294 |
$dbi->type_rule( |
|
3295 |
into1 => [ |
|
3296 |
[$date_typename, $datetime_typename] => sub { |
|
3297 |
my $value = shift; |
|
3298 |
$value =~ s/02/03/g; |
|
3299 |
return $value; |
|
3300 |
} |
|
3301 |
] |
|
3302 |
); |
|
3303 |
$dbi->insert({$key1 => '2010-01-02', $key2 => '2010-01-01 01:01:02'}, table => $table1); |
|
3304 |
$result = $dbi->select(table => $table1); |
|
3305 |
$row = $result->one; |
|
3306 |
like($row->{$key1}, qr/^2010-01-03/); |
|
3307 |
like($row->{$key2}, qr/^2010-01-01 01:01:03/); |
|
cleanup test
|
3308 | |
cleanup test
|
3309 |
$dbi = DBIx::Custom->connect; |
3310 |
eval { $dbi->execute("drop table $table1") }; |
|
3311 |
$dbi->execute($create_table1_type); |
|
3312 |
$dbi->insert({$key1 => '2010-01-03', $key2 => '2010-01-01 01:01:03'}, table => $table1); |
|
3313 |
$dbi->type_rule( |
|
3314 |
into1 => [ |
|
3315 |
[$date_typename, $datetime_typename] => sub { |
|
3316 |
my $value = shift; |
|
3317 |
$value =~ s/02/03/g; |
|
3318 |
return $value; |
|
3319 |
} |
|
3320 |
] |
|
cleanup test
|
3321 |
); |
cleanup test
|
3322 |
$result = $dbi->execute( |
3323 |
"select * from $table1 where $key1 = :$key1 and $key2 = :$table1.$key2", |
|
3324 |
param => {$key1 => '2010-01-03', "$table1.$key2" => '2010-01-01 01:01:02'} |
|
cleanup test
|
3325 |
); |
cleanup test
|
3326 |
$row = $result->one; |
3327 |
like($row->{$key1}, qr/^2010-01-03/); |
|
3328 |
like($row->{$key2}, qr/^2010-01-01 01:01:03/); |
|
3329 | ||
3330 |
$dbi = DBIx::Custom->connect; |
|
3331 |
eval { $dbi->execute("drop table $table1") }; |
|
3332 |
$dbi->execute($create_table1_type); |
|
3333 |
$dbi->insert({$key1 => '2010-01-03', $key2 => '2010-01-01 01:01:03'}, table => $table1); |
|
3334 |
$dbi->type_rule( |
|
3335 |
into1 => [ |
|
3336 |
[$date_typename, $datetime_typename] => sub { |
|
3337 |
my $value = shift; |
|
3338 |
$value =~ s/02/03/g; |
|
3339 |
return $value; |
|
3340 |
} |
|
cleanup test
|
3341 |
] |
3342 |
); |
|
cleanup test
|
3343 |
$result = $dbi->execute( |
3344 |
"select * from $table1 where $key1 = :$key1 and $key2 = :$table1.$key2", |
|
3345 |
param => {$key1 => '2010-01-02', "$table1.$key2" => '2010-01-01 01:01:02'}, |
|
3346 |
table => $table1 |
|
3347 |
); |
|
3348 |
$row = $result->one; |
|
3349 |
like($row->{$key1}, qr/^2010-01-03/); |
|
3350 |
like($row->{$key2}, qr/2010-01-01 01:01:03/); |
|
3351 | ||
3352 |
$dbi = DBIx::Custom->connect; |
|
3353 |
eval { $dbi->execute("drop table $table1") }; |
|
3354 |
$dbi->execute($create_table1_type); |
|
3355 |
$dbi->register_filter(convert => sub { |
|
3356 |
my $value = shift || ''; |
|
3357 |
$value =~ s/02/03/; |
|
3358 |
return $value; |
|
3359 |
}); |
|
3360 |
$dbi->type_rule( |
|
3361 |
from1 => { |
|
3362 |
$date_datatype => 'convert', |
|
3363 |
}, |
|
3364 |
into1 => { |
|
3365 |
$date_typename => 'convert', |
|
3366 |
} |
|
3367 |
); |
|
3368 |
$dbi->insert({$key1 => '2010-02-02'}, table => $table1); |
|
3369 |
$result = $dbi->select(table => $table1); |
|
3370 |
like($result->fetch->[0], qr/^2010-03-03/); |
|
3371 | ||
3372 |
test 'type_rule and filter order'; |
|
3373 |
$dbi = DBIx::Custom->connect; |
|
3374 |
eval { $dbi->execute("drop table $table1") }; |
|
3375 |
$dbi->execute($create_table1_type); |
|
3376 |
$dbi->type_rule( |
|
3377 |
into1 => { |
|
3378 |
$date_typename => sub { my $v = shift || ''; $v =~ s/4/5/; return $v } |
|
3379 |
}, |
|
3380 |
into2 => { |
|
3381 |
$date_typename => sub { my $v = shift || ''; $v =~ s/5/6/; return $v } |
|
3382 |
}, |
|
3383 |
from1 => { |
|
3384 |
$date_datatype => sub { my $v = shift || ''; $v =~ s/6/7/; return $v } |
|
3385 |
}, |
|
3386 |
from2 => { |
|
3387 |
$date_datatype => sub { my $v = shift || ''; $v =~ s/7/8/; return $v } |
|
3388 |
} |
|
3389 |
); |
|
3390 |
$dbi->insert({$key1 => '2010-01-03'}, |
|
3391 |
table => $table1, filter => {$key1 => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }}); |
|
3392 |
$result = $dbi->select(table => $table1); |
|
3393 |
$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/8/9/; return $v }); |
|
3394 |
like($result->fetch_first->[0], qr/^2010-01-09/); |
|
3395 | ||
3396 | ||
3397 |
$dbi = DBIx::Custom->connect; |
|
3398 |
eval { $dbi->execute("drop table $table1") }; |
|
3399 |
$dbi->execute($create_table1_type); |
|
3400 |
$dbi->type_rule( |
|
3401 |
from1 => { |
|
3402 |
$date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v } |
|
3403 |
}, |
|
3404 |
from2 => { |
|
3405 |
$date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v } |
|
3406 |
}, |
|
3407 |
); |
|
3408 |
$dbi->insert({$key1 => '2010-01-03'}, table => $table1); |
|
3409 |
$result = $dbi->select(table => $table1); |
|
3410 |
$result->type_rule( |
|
3411 |
from1 => { |
|
3412 |
$date_datatype => sub { my $v = shift || ''; $v =~ s/3/6/; return $v } |
|
3413 |
}, |
|
3414 |
from2 => { |
|
3415 |
$date_datatype => sub { my $v = shift || ''; $v =~ s/6/8/; return $v } |
|
3416 |
} |
|
cleanup test
|
3417 |
); |
cleanup test
|
3418 |
$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/8/9/; return $v }); |
3419 |
like($result->fetch_first->[0], qr/^2010-01-09/); |
|
cleanup test
|
3420 | |
cleanup test
|
3421 |
test 'type_rule_off'; |
cleanup test
|
3422 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
3423 |
eval { $dbi->execute("drop table $table1") }; |
3424 |
$dbi->execute($create_table1_type); |
|
3425 |
$dbi->type_rule( |
|
3426 |
from1 => { |
|
3427 |
$date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v } |
|
3428 |
}, |
|
3429 |
into1 => { |
|
3430 |
$date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v } |
|
3431 |
} |
|
cleanup test
|
3432 |
); |
cleanup test
|
3433 |
$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule_off => 1); |
3434 |
$result = $dbi->select(table => $table1, type_rule_off => 1); |
|
3435 |
like($result->type_rule_off->fetch->[0], qr/^2010-01-03/); |
|
cleanup test
|
3436 | |
cleanup test
|
3437 |
$dbi = DBIx::Custom->connect; |
finishied sqlite test cleanu...
|
3438 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
3439 |
$dbi->execute($create_table1_type); |
3440 |
$dbi->type_rule( |
|
3441 |
from1 => { |
|
3442 |
$date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v } |
|
3443 |
}, |
|
3444 |
into1 => { |
|
3445 |
$date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v } |
|
3446 |
} |
|
3447 |
); |
|
3448 |
$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule_off => 1); |
|
3449 |
$result = $dbi->select(table => $table1, type_rule_off => 1); |
|
3450 |
like($result->one->{$key1}, qr/^2010-01-04/); |
|
cleanup test
|
3451 | |
3452 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
3453 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
3454 |
$dbi->execute($create_table1_type); |
3455 |
$dbi->type_rule( |
|
3456 |
from1 => { |
|
3457 |
$date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v } |
|
3458 |
}, |
|
3459 |
into1 => { |
|
3460 |
$date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v } |
|
3461 |
} |
|
3462 |
); |
|
3463 |
$dbi->insert({$key1 => '2010-01-03'}, table => $table1); |
|
3464 |
$result = $dbi->select(table => $table1); |
|
3465 |
like($result->one->{$key1}, qr/^2010-01-05/); |
|
cleanup test
|
3466 | |
3467 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
3468 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
3469 |
$dbi->execute($create_table1_type); |
3470 |
$dbi->type_rule( |
|
3471 |
from1 => { |
|
3472 |
$date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v } |
|
3473 |
}, |
|
3474 |
into1 => { |
|
3475 |
$date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v } |
|
3476 |
} |
|
3477 |
); |
|
3478 |
$dbi->insert({$key1 => '2010-01-03'}, table => $table1); |
|
3479 |
$result = $dbi->select(table => $table1); |
|
3480 |
like($result->fetch->[0], qr/2010-01-05/); |
|
cleanup test
|
3481 | |
3482 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
3483 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
3484 |
$dbi->execute($create_table1_type); |
3485 |
$dbi->register_filter(ppp => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }); |
|
3486 |
$dbi->type_rule( |
|
3487 |
into1 => { |
|
3488 |
$date_typename => 'ppp' |
|
3489 |
} |
|
3490 |
); |
|
3491 |
$dbi->insert({$key1 => '2010-01-03'}, table => $table1); |
|
3492 |
$result = $dbi->select(table => $table1); |
|
3493 |
like($result->one->{$key1}, qr/^2010-01-04/); |
|
cleanup test
|
3494 | |
cleanup test
|
3495 |
eval{$dbi->type_rule( |
3496 |
into1 => { |
|
3497 |
$date_typename => 'pp' |
|
3498 |
} |
|
3499 |
)}; |
|
3500 |
like($@, qr/not registered/); |
|
cleanup test
|
3501 | |
cleanup test
|
3502 |
$dbi = DBIx::Custom->connect; |
finishied sqlite test cleanu...
|
3503 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
3504 |
$dbi->execute($create_table1_type); |
cleanup test
|
3505 |
eval { |
cleanup test
|
3506 |
$dbi->type_rule( |
3507 |
from1 => { |
|
3508 |
Date => sub { $_[0] * 2 }, |
|
3509 |
} |
|
cleanup test
|
3510 |
); |
3511 |
}; |
|
cleanup test
|
3512 |
like($@, qr/lower/); |
cleanup test
|
3513 | |
cleanup test
|
3514 |
eval { |
3515 |
$dbi->type_rule( |
|
3516 |
into1 => { |
|
3517 |
Date => sub { $_[0] * 2 }, |
|
3518 |
} |
|
3519 |
); |
|
3520 |
}; |
|
3521 |
like($@, qr/lower/); |
|
cleanup test
|
3522 | |
3523 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
3524 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
3525 |
$dbi->execute($create_table1_type); |
3526 |
$dbi->type_rule( |
|
3527 |
from1 => { |
|
3528 |
$date_datatype => sub { my $v = shift || ''; $v =~ s/4/5/; return $v } |
|
3529 |
}, |
|
3530 |
into1 => { |
|
3531 |
$date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v } |
|
3532 |
} |
|
3533 |
); |
|
3534 |
$dbi->insert({$key1 => '2010-01-03'}, table => $table1); |
|
3535 |
$result = $dbi->select(table => $table1); |
|
3536 |
$result->type_rule_off; |
|
3537 |
like($result->one->{$key1}, qr/^2010-01-04/); |
|
cleanup test
|
3538 | |
3539 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
3540 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
3541 |
$dbi->execute($create_table1_type); |
3542 |
$dbi->type_rule( |
|
3543 |
from1 => { |
|
3544 |
$date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }, |
|
3545 |
$datetime_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v } |
|
3546 |
}, |
|
cleanup test
|
3547 |
); |
cleanup test
|
3548 |
$dbi->insert({$key1 => '2010-01-03', $key2 => '2010-01-01 01:01:03'}, table => $table1); |
3549 |
$result = $dbi->select(table => $table1); |
|
3550 |
$result->type_rule( |
|
3551 |
from1 => { |
|
3552 |
$date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v } |
|
3553 |
} |
|
cleanup test
|
3554 |
); |
cleanup test
|
3555 |
$row = $result->one; |
3556 |
like($row->{$key1}, qr/^2010-01-05/); |
|
3557 |
like($row->{$key2}, qr/^2010-01-01 01:01:03/); |
|
3558 | ||
3559 |
$result = $dbi->select(table => $table1); |
|
3560 |
$result->type_rule( |
|
3561 |
from1 => { |
|
3562 |
$date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v } |
|
3563 |
} |
|
cleanup test
|
3564 |
); |
cleanup test
|
3565 |
$row = $result->one; |
3566 |
like($row->{$key1}, qr/2010-01-05/); |
|
3567 |
like($row->{$key2}, qr/2010-01-01 01:01:03/); |
|
3568 | ||
3569 |
$result = $dbi->select(table => $table1); |
|
3570 |
$result->type_rule( |
|
3571 |
from1 => { |
|
3572 |
$date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v } |
|
3573 |
} |
|
3574 |
); |
|
3575 |
$row = $result->one; |
|
3576 |
like($row->{$key1}, qr/2010-01-05/); |
|
3577 |
like($row->{$key2}, qr/2010-01-01 01:01:03/); |
|
3578 | ||
3579 |
$result = $dbi->select(table => $table1); |
|
3580 |
$result->type_rule( |
|
3581 |
from1 => [$date_datatype => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }] |
|
3582 |
); |
|
3583 |
$row = $result->one; |
|
3584 |
like($row->{$key1}, qr/2010-01-05/); |
|
3585 |
like($row->{$key2}, qr/2010-01-01 01:01:03/); |
|
3586 | ||
3587 |
$dbi->register_filter(five => sub { my $v = shift || ''; $v =~ s/3/5/; return $v }); |
|
3588 |
$result = $dbi->select(table => $table1); |
|
3589 |
$result->type_rule( |
|
3590 |
from1 => [$date_datatype => 'five'] |
|
3591 |
); |
|
3592 |
$row = $result->one; |
|
3593 |
like($row->{$key1}, qr/^2010-01-05/); |
|
3594 |
like($row->{$key2}, qr/^2010-01-01 01:01:03/); |
|
3595 | ||
3596 |
$result = $dbi->select(table => $table1); |
|
3597 |
$result->type_rule( |
|
3598 |
from1 => [$date_datatype => undef] |
|
3599 |
); |
|
3600 |
$row = $result->one; |
|
3601 |
like($row->{$key1}, qr/^2010-01-03/); |
|
3602 |
like($row->{$key2}, qr/^2010-01-01 01:01:03/); |
|
cleanup test
|
3603 | |
3604 |
$dbi = DBIx::Custom->connect; |
|
finishied sqlite test cleanu...
|
3605 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
3606 |
$dbi->execute($create_table1_type); |
3607 |
$dbi->type_rule( |
|
3608 |
from1 => { |
|
3609 |
$date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v }, |
|
3610 |
}, |
|
cleanup test
|
3611 |
); |
cleanup test
|
3612 |
$dbi->insert({$key1 => '2010-01-03'}, table => $table1); |
3613 |
$result = $dbi->select(table => $table1); |
|
3614 |
$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }); |
|
3615 |
like($result->one->{$key1}, qr/^2010-01-05/); |
|
cleanup test
|
3616 | |
cleanup test
|
3617 |
$dbi = DBIx::Custom->connect; |
finishied sqlite test cleanu...
|
3618 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
3619 |
$dbi->execute($create_table1_type); |
3620 |
$dbi->type_rule( |
|
3621 |
from1 => { |
|
3622 |
$date_datatype => sub { my $v = shift || ''; $v =~ s/3/4/; return $v } |
|
3623 |
}, |
|
3624 |
); |
|
3625 |
$dbi->insert({$key1 => '2010-01-03'}, table => $table1); |
|
3626 |
$result = $dbi->select(table => $table1); |
|
3627 |
$result->filter($key1 => sub { my $v = shift || ''; $v =~ s/4/5/; return $v }); |
|
3628 |
like($result->fetch->[0], qr/^2010-01-05/); |
|
test cleanup
|
3629 | |
cleanup test
|
3630 |
$dbi = DBIx::Custom->connect; |
finishied sqlite test cleanu...
|
3631 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
3632 |
$dbi->execute($create_table1_type); |
3633 |
$dbi->type_rule( |
|
3634 |
into1 => { |
|
3635 |
$date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v } |
|
3636 |
}, |
|
3637 |
into2 => { |
|
3638 |
$date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v } |
|
3639 |
}, |
|
3640 |
from1 => { |
|
3641 |
$date_datatype => sub { my $v = shift || ''; $v =~ s/3/6/; return $v } |
|
3642 |
}, |
|
3643 |
from2 => { |
|
3644 |
$date_datatype => sub { my $v = shift || ''; $v =~ s/(3|6)/7/; return $v } |
|
3645 |
} |
|
3646 |
); |
|
3647 |
$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule_off => 1); |
|
3648 |
$result = $dbi->select(table => $table1); |
|
3649 |
like($result->type_rule_off->fetch_first->[0], qr/^2010-01-03/); |
|
3650 |
$result = $dbi->select(table => $table1); |
|
3651 |
like($result->type_rule_on->fetch_first->[0], qr/^2010-01-07/); |
|
test cleanup
|
3652 | |
cleanup test
|
3653 |
$dbi = DBIx::Custom->connect; |
finishied sqlite test cleanu...
|
3654 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
3655 |
$dbi->execute($create_table1_type); |
3656 |
$dbi->type_rule( |
|
3657 |
into1 => { |
|
3658 |
$date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v } |
|
3659 |
}, |
|
3660 |
into2 => { |
|
3661 |
$date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v } |
|
3662 |
}, |
|
3663 |
from1 => { |
|
3664 |
$date_datatype => sub { my $v = shift || ''; $v =~ s/(3|5)/6/; return $v } |
|
3665 |
}, |
|
3666 |
from2 => { |
|
3667 |
$date_datatype => sub { my $v = shift || ''; $v =~ s/6/7/; return $v } |
|
3668 |
} |
|
3669 |
); |
|
3670 |
$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule1_off => 1); |
|
3671 |
$result = $dbi->select(table => $table1); |
|
3672 |
like($result->type_rule1_off->fetch_first->[0], qr/^2010-01-05/); |
|
3673 |
$result = $dbi->select(table => $table1); |
|
3674 |
like($result->type_rule1_on->fetch_first->[0], qr/^2010-01-07/); |
|
test cleanup
|
3675 | |
cleanup test
|
3676 |
$dbi = DBIx::Custom->connect; |
finishied sqlite test cleanu...
|
3677 |
eval { $dbi->execute("drop table $table1") }; |
cleanup test
|
3678 |
$dbi->execute($create_table1_type); |
3679 |
$dbi->type_rule( |
|
3680 |
into1 => { |
|
3681 |
$date_typename => sub { my $v = shift || ''; $v =~ s/3/5/; return $v } |
|
3682 |
}, |
|
3683 |
into2 => { |
|
3684 |
$date_typename => sub { my $v = shift || ''; $v =~ s/3/4/; return $v } |
|
3685 |
}, |
|
3686 |
from1 => { |
|
3687 |
$date_datatype => sub { my $v = shift || ''; $v =~ s/5/6/; return $v } |
|
3688 |
}, |
|
3689 |
from2 => { |
|
3690 |
$date_datatype => sub { my $v = shift || ''; $v =~ s/(3|6)/7/; return $v } |
|
3691 |
} |
|
3692 |
); |
|
3693 |
$dbi->insert({$key1 => '2010-01-03'}, table => $table1, type_rule2_off => 1); |
|
3694 |
$result = $dbi->select(table => $table1); |
|
3695 |
like($result->type_rule2_off->fetch_first->[0], qr/^2010-01-06/); |
|
3696 |
$result = $dbi->select(table => $table1); |
|
3697 |
like($result->type_rule2_on->fetch_first->[0], qr/^2010-01-07/); |
|
test cleanup
|
3698 | |
added memory leak check test
|
3699 |
test 'join'; |
3700 |
$dbi = DBIx::Custom->connect; |
|
3701 |
eval { $dbi->execute("drop table $table1") }; |
|
3702 |
$dbi->execute($create_table1); |
|
3703 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
|
3704 |
$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4}); |
|
3705 |
eval { $dbi->execute("drop table $table2") }; |
|
3706 |
$dbi->execute($create_table2); |
|
3707 |
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5}); |
|
3708 |
eval { $dbi->execute("drop table $table3") }; |
|
3709 |
$dbi->execute("create table $table3 ($key3 int, $key4 int)"); |
|
3710 |
$dbi->insert(table => $table3, param => {$key3 => 5, $key4 => 4}); |
|
3711 |
$rows = $dbi->select( |
|
3712 |
table => $table1, |
|
3713 |
column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3", |
|
3714 |
where => {"$table1.$key2" => 2}, |
|
3715 |
join => ["left outer join $table2 on $table1.$key1 = $table2.$key1"] |
|
3716 |
)->all; |
|
3717 |
is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}]); |
|
3718 | ||
3719 |
$rows = $dbi->select( |
|
3720 |
table => $table1, |
|
3721 |
where => {$key1 => 1}, |
|
3722 |
join => ["left outer join $table2 on $table1.$key1 = $table2.$key1"] |
|
3723 |
)->all; |
|
3724 |
is_deeply($rows, [{$key1 => 1, $key2 => 2}]); |
|
3725 | ||
3726 |
eval { |
|
3727 |
$rows = $dbi->select( |
|
3728 |
table => $table1, |
|
3729 |
column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3", |
|
3730 |
where => {"$table1.$key2" => 2}, |
|
3731 |
join => {"$table1.$key1" => "$table2.$key1"} |
|
3732 |
); |
|
3733 |
}; |
|
3734 |
like ($@, qr/array/); |
|
3735 | ||
3736 |
$rows = $dbi->select( |
|
3737 |
table => $table1, |
|
3738 |
where => {$key1 => 1}, |
|
3739 |
join => ["left outer join $table2 on $table1.$key1 = $table2.$key1", |
|
3740 |
"left outer join $table3 on $table2.$key3 = $table3.$key3"] |
|
3741 |
)->all; |
|
3742 |
is_deeply($rows, [{$key1 => 1, $key2 => 2}]); |
|
3743 | ||
3744 |
$rows = $dbi->select( |
|
3745 |
column => "$table3.$key4 as ${table3}__$key4", |
|
3746 |
table => $table1, |
|
3747 |
where => {"$table1.$key1" => 1}, |
|
3748 |
join => ["left outer join $table2 on $table1.$key1 = $table2.$key1", |
|
3749 |
"left outer join $table3 on $table2.$key3 = $table3.$key3"] |
|
3750 |
)->all; |
|
3751 |
is_deeply($rows, [{"${table3}__$key4" => 4}]); |
|
3752 | ||
3753 |
$rows = $dbi->select( |
|
3754 |
column => "$table1.$key1 as ${table1}__$key1", |
|
3755 |
table => $table1, |
|
3756 |
where => {"$table3.$key4" => 4}, |
|
3757 |
join => ["left outer join $table2 on $table1.$key1 = $table2.$key1", |
|
3758 |
"left outer join $table3 on $table2.$key3 = $table3.$key3"] |
|
3759 |
)->all; |
|
3760 |
is_deeply($rows, [{"${table1}__$key1" => 1}]); |
|
3761 | ||
3762 |
$dbi = DBIx::Custom->connect; |
|
3763 |
eval { $dbi->execute("drop table $table1") }; |
|
3764 |
$dbi->execute($create_table1); |
|
3765 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
|
3766 |
eval { $dbi->execute("drop table $table2") }; |
|
3767 |
$dbi->execute($create_table2); |
|
3768 |
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5}); |
|
3769 |
$rows = $dbi->select( |
|
3770 |
table => $table1, |
|
3771 |
column => "${q}$table1$p.${q}$key1$p as ${q}${table1}_$key1$p, ${q}$table2$p.${q}$key1$p as ${q}${table2}_$key1$p, ${q}$key2$p, ${q}$key3$p", |
|
3772 |
where => {"$table1.$key2" => 2}, |
|
3773 |
join => ["left outer join ${q}$table2$p on ${q}$table1$p.${q}$key1$p = ${q}$table2$p.${q}$key1$p"], |
|
3774 |
)->all; |
|
3775 |
is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}], |
|
3776 |
'quote'); |
|
3777 | ||
3778 | ||
3779 |
$dbi = DBIx::Custom->connect; |
|
3780 |
eval { $dbi->execute("drop table $table1") }; |
|
3781 |
$dbi->execute($create_table1); |
|
3782 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
|
3783 |
$sql = <<"EOS"; |
|
3784 |
left outer join ( |
|
3785 |
select * from $table1 t1 |
|
3786 |
where t1.$key2 = ( |
|
3787 |
select max(t2.$key2) from $table1 t2 |
|
3788 |
where t1.$key1 = t2.$key1 |
|
3789 |
) |
|
3790 |
) $table3 on $table1.$key1 = $table3.$key1 |
|
3791 |
EOS |
|
3792 |
$join = [$sql]; |
|
3793 |
$rows = $dbi->select( |
|
3794 |
table => $table1, |
|
3795 |
column => "$table3.$key1 as ${table3}__$key1", |
|
3796 |
join => $join |
|
3797 |
)->all; |
|
3798 |
is_deeply($rows, [{"${table3}__$key1" => 1}]); |
|
3799 | ||
3800 |
$dbi = DBIx::Custom->connect; |
|
3801 |
eval { $dbi->execute("drop table $table1") }; |
|
3802 |
eval { $dbi->execute("drop table $table2") }; |
|
3803 |
$dbi->execute($create_table1); |
|
3804 |
$dbi->execute($create_table2); |
|
3805 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
|
3806 |
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 4}); |
|
3807 |
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5}); |
|
3808 |
$result = $dbi->select( |
|
3809 |
table => $table1, |
|
3810 |
join => [ |
|
3811 |
"left outer join $table2 on $table2.$key2 = '4' and $table1.$key1 = $table2.$key1" |
|
3812 |
] |
|
3813 |
); |
|
3814 |
is_deeply($result->all, [{$key1 => 1, $key2 => 2}]); |
|
3815 |
$result = $dbi->select( |
|
3816 |
table => $table1, |
|
3817 |
column => [{$table2 => [$key3]}], |
|
3818 |
join => [ |
|
3819 |
"left outer join $table2 on $table2.$key3 = '4' and $table1.$key1 = $table2.$key1" |
|
3820 |
] |
|
3821 |
); |
|
3822 |
is_deeply($result->all, [{"$table2.$key3" => 4}]); |
|
3823 |
$result = $dbi->select( |
|
3824 |
table => $table1, |
|
3825 |
column => [{$table2 => [$key3]}], |
|
3826 |
join => [ |
|
3827 |
"left outer join $table2 on $table1.$key1 = $table2.$key1 and $table2.$key3 = '4'" |
|
3828 |
] |
|
3829 |
); |
|
3830 |
is_deeply($result->all, [{"$table2.$key3" => 4}]); |
|
3831 | ||
3832 |
$dbi = DBIx::Custom->connect; |
|
3833 |
eval { $dbi->execute("drop table $table1") }; |
|
3834 |
eval { $dbi->execute("drop table $table2") }; |
|
3835 |
$dbi->execute($create_table1); |
|
3836 |
$dbi->execute($create_table2); |
|
3837 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
|
3838 |
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 4}); |
|
3839 |
$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5}); |
|
3840 |
$result = $dbi->select( |
|
3841 |
table => $table1, |
|
3842 |
column => [{$table2 => [$key3]}], |
|
3843 |
join => [ |
|
3844 |
{ |
|
3845 |
clause => "left outer join $table2 on $table2.$key3 = '4' and $table1.$key1 = $table2.$key1", |
|
3846 |
table => [$table1, $table2] |
|
3847 |
} |
|
3848 |
] |
|
3849 |
); |
|
3850 |
is_deeply($result->all, [{"$table2.$key3" => 4}]); |
|
3851 | ||
3852 |
test 'columns'; |
|
3853 |
$dbi = MyDBI1->connect; |
|
3854 |
$model = $dbi->model($table1); |
|
3855 | ||
cleanup test
|
3856 | |
cleanup test
|
3857 |
1; |