... | ... |
@@ -13,6 +13,9 @@ plan skip_all => $ENV{DBIX_CUSTOM_SKIP_MESSAGE} || 'common.t is always skipped' |
13 | 13 |
|
14 | 14 |
plan 'no_plan'; |
15 | 15 |
|
16 |
+$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/}; |
|
17 |
+sub test { print "# $_[0]\n" } |
|
18 |
+ |
|
16 | 19 |
# Constant |
17 | 20 |
my $table1 = $dbi->table1; |
18 | 21 |
my $table2 = $dbi->table2; |
... | ... |
@@ -230,8 +233,164 @@ require MyDBI1; |
230 | 233 |
} |
231 | 234 |
} |
232 | 235 |
|
233 |
-$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/}; |
|
234 |
-sub test { print "# $_[0]\n" } |
|
236 |
+test 'join'; |
|
237 |
+$dbi = DBIx::Custom->connect; |
|
238 |
+eval { $dbi->execute("drop table $table1") }; |
|
239 |
+$dbi->execute($create_table1); |
|
240 |
+$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
|
241 |
+$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4}); |
|
242 |
+eval { $dbi->execute("drop table $table2") }; |
|
243 |
+$dbi->execute($create_table2); |
|
244 |
+$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5}); |
|
245 |
+eval { $dbi->execute("drop table $table3") }; |
|
246 |
+$dbi->execute("create table $table3 ($key3 int, $key4 int)"); |
|
247 |
+$dbi->insert(table => $table3, param => {$key3 => 5, $key4 => 4}); |
|
248 |
+$rows = $dbi->select( |
|
249 |
+ table => $table1, |
|
250 |
+ column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3", |
|
251 |
+ where => {"$table1.$key2" => 2}, |
|
252 |
+ join => ["left outer join $table2 on $table1.$key1 = $table2.$key1"] |
|
253 |
+)->all; |
|
254 |
+is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}]); |
|
255 |
+ |
|
256 |
+$rows = $dbi->select( |
|
257 |
+ table => $table1, |
|
258 |
+ where => {$key1 => 1}, |
|
259 |
+ join => ["left outer join $table2 on $table1.$key1 = $table2.$key1"] |
|
260 |
+)->all; |
|
261 |
+is_deeply($rows, [{$key1 => 1, $key2 => 2}]); |
|
262 |
+ |
|
263 |
+eval { |
|
264 |
+ $rows = $dbi->select( |
|
265 |
+ table => $table1, |
|
266 |
+ column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3", |
|
267 |
+ where => {"$table1.$key2" => 2}, |
|
268 |
+ join => {"$table1.$key1" => "$table2.$key1"} |
|
269 |
+ ); |
|
270 |
+}; |
|
271 |
+like ($@, qr/array/); |
|
272 |
+ |
|
273 |
+$rows = $dbi->select( |
|
274 |
+ table => $table1, |
|
275 |
+ where => {$key1 => 1}, |
|
276 |
+ join => ["left outer join $table2 on $table1.$key1 = $table2.$key1", |
|
277 |
+ "left outer join $table3 on $table2.$key3 = $table3.$key3"] |
|
278 |
+)->all; |
|
279 |
+is_deeply($rows, [{$key1 => 1, $key2 => 2}]); |
|
280 |
+ |
|
281 |
+$rows = $dbi->select( |
|
282 |
+ column => "$table3.$key4 as ${table3}__$key4", |
|
283 |
+ table => $table1, |
|
284 |
+ where => {"$table1.$key1" => 1}, |
|
285 |
+ join => ["left outer join $table2 on $table1.$key1 = $table2.$key1", |
|
286 |
+ "left outer join $table3 on $table2.$key3 = $table3.$key3"] |
|
287 |
+)->all; |
|
288 |
+is_deeply($rows, [{"${table3}__$key4" => 4}]); |
|
289 |
+ |
|
290 |
+$rows = $dbi->select( |
|
291 |
+ column => "$table1.$key1 as ${table1}__$key1", |
|
292 |
+ table => $table1, |
|
293 |
+ where => {"$table3.$key4" => 4}, |
|
294 |
+ join => ["left outer join $table2 on $table1.$key1 = $table2.$key1", |
|
295 |
+ "left outer join $table3 on $table2.$key3 = $table3.$key3"] |
|
296 |
+)->all; |
|
297 |
+is_deeply($rows, [{"${table1}__$key1" => 1}]); |
|
298 |
+ |
|
299 |
+$dbi = DBIx::Custom->connect; |
|
300 |
+eval { $dbi->execute("drop table $table1") }; |
|
301 |
+$dbi->execute($create_table1); |
|
302 |
+$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
|
303 |
+eval { $dbi->execute("drop table $table2") }; |
|
304 |
+$dbi->execute($create_table2); |
|
305 |
+$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5}); |
|
306 |
+$rows = $dbi->select( |
|
307 |
+ table => $table1, |
|
308 |
+ 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", |
|
309 |
+ where => {"$table1.$key2" => 2}, |
|
310 |
+ join => ["left outer join ${q}$table2$p on ${q}$table1$p.${q}$key1$p = ${q}$table2$p.${q}$key1$p"], |
|
311 |
+)->all; |
|
312 |
+is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}], |
|
313 |
+ 'quote'); |
|
314 |
+ |
|
315 |
+ |
|
316 |
+$dbi = DBIx::Custom->connect; |
|
317 |
+eval { $dbi->execute("drop table $table1") }; |
|
318 |
+$dbi->execute($create_table1); |
|
319 |
+$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
|
320 |
+$sql = <<"EOS"; |
|
321 |
+left outer join ( |
|
322 |
+ select * from $table1 t1 |
|
323 |
+ where t1.$key2 = ( |
|
324 |
+ select max(t2.$key2) from $table1 t2 |
|
325 |
+ where t1.$key1 = t2.$key1 |
|
326 |
+ ) |
|
327 |
+) $table3 on $table1.$key1 = $table3.$key1 |
|
328 |
+EOS |
|
329 |
+$join = [$sql]; |
|
330 |
+$rows = $dbi->select( |
|
331 |
+ table => $table1, |
|
332 |
+ column => "$table3.$key1 as ${table3}__$key1", |
|
333 |
+ join => $join |
|
334 |
+)->all; |
|
335 |
+$DB::single = 1; |
|
336 |
+is_deeply($rows, [{"${table3}__$key1" => 1}]); |
|
337 |
+ |
|
338 |
+$dbi = DBIx::Custom->connect; |
|
339 |
+eval { $dbi->execute("drop table $table1") }; |
|
340 |
+eval { $dbi->execute("drop table $table2") }; |
|
341 |
+$dbi->execute($create_table1); |
|
342 |
+$dbi->execute($create_table2); |
|
343 |
+$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
|
344 |
+$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 4}); |
|
345 |
+$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5}); |
|
346 |
+$result = $dbi->select( |
|
347 |
+ table => $table1, |
|
348 |
+ join => [ |
|
349 |
+ "left outer join $table2 on $table2.$key2 = '4' and $table1.$key1 = $table2.$key1" |
|
350 |
+ ] |
|
351 |
+); |
|
352 |
+is_deeply($result->all, [{$key1 => 1, $key2 => 2}]); |
|
353 |
+$result = $dbi->select( |
|
354 |
+ table => $table1, |
|
355 |
+ column => [{$table2 => [$key3]}], |
|
356 |
+ join => [ |
|
357 |
+ "left outer join $table2 on $table2.$key3 = '4' and $table1.$key1 = $table2.$key1" |
|
358 |
+ ] |
|
359 |
+); |
|
360 |
+is_deeply($result->all, [{"$table2.$key3" => 4}]); |
|
361 |
+$result = $dbi->select( |
|
362 |
+ table => $table1, |
|
363 |
+ column => [{$table2 => [$key3]}], |
|
364 |
+ join => [ |
|
365 |
+ "left outer join $table2 on $table1.$key1 = $table2.$key1 and $table2.$key3 = '4'" |
|
366 |
+ ] |
|
367 |
+); |
|
368 |
+is_deeply($result->all, [{"$table2.$key3" => 4}]); |
|
369 |
+ |
|
370 |
+$dbi = DBIx::Custom->connect; |
|
371 |
+eval { $dbi->execute("drop table $table1") }; |
|
372 |
+eval { $dbi->execute("drop table $table2") }; |
|
373 |
+$dbi->execute($create_table1); |
|
374 |
+$dbi->execute($create_table2); |
|
375 |
+$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
|
376 |
+$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 4}); |
|
377 |
+$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5}); |
|
378 |
+$result = $dbi->select( |
|
379 |
+ table => $table1, |
|
380 |
+ column => [{$table2 => [$key3]}], |
|
381 |
+ join => [ |
|
382 |
+ { |
|
383 |
+ clause => "left outer join $table2 on $table2.$key3 = '4' and $table1.$key1 = $table2.$key1", |
|
384 |
+ table => [$table1, $table2] |
|
385 |
+ } |
|
386 |
+ ] |
|
387 |
+); |
|
388 |
+is_deeply($result->all, [{"$table2.$key3" => 4}]); |
|
389 |
+ |
|
390 |
+test 'columns'; |
|
391 |
+$DB::single = 1; |
|
392 |
+$dbi = MyDBI1->connect; |
|
393 |
+$model = $dbi->model($table1); |
|
235 | 394 |
|
236 | 395 |
# Create table |
237 | 396 |
$dbi = DBIx::Custom->connect; |
... | ... |
@@ -1966,12 +2125,6 @@ eval { |
1966 | 2125 |
}; |
1967 | 2126 |
like($@, qr/must be/); |
1968 | 2127 |
|
1969 |
-test 'columns'; |
|
1970 |
-$DB::single = 1; |
|
1971 |
-$dbi = MyDBI1->connect; |
|
1972 |
-$model = $dbi->model($table1); |
|
1973 |
- |
|
1974 |
- |
|
1975 | 2128 |
test 'model delete_at'; |
1976 | 2129 |
$dbi = MyDBI6->connect; |
1977 | 2130 |
eval { $dbi->execute("drop table $table1") }; |
... | ... |
@@ -3164,159 +3317,6 @@ is_deeply($rows, [{$key1 => 1, $key2 => 11, $key3 => 3, $key4 => 4, $key5 => 5}, |
3164 | 3317 |
{$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10}], |
3165 | 3318 |
"basic"); |
3166 | 3319 |
|
3167 |
-test 'join'; |
|
3168 |
-$dbi = DBIx::Custom->connect; |
|
3169 |
-eval { $dbi->execute("drop table $table1") }; |
|
3170 |
-$dbi->execute($create_table1); |
|
3171 |
-$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
|
3172 |
-$dbi->insert(table => $table1, param => {$key1 => 3, $key2 => 4}); |
|
3173 |
-eval { $dbi->execute("drop table $table2") }; |
|
3174 |
-$dbi->execute($create_table2); |
|
3175 |
-$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5}); |
|
3176 |
-eval { $dbi->execute("drop table $table3") }; |
|
3177 |
-$dbi->execute("create table $table3 ($key3 int, $key4 int)"); |
|
3178 |
-$dbi->insert(table => $table3, param => {$key3 => 5, $key4 => 4}); |
|
3179 |
-$rows = $dbi->select( |
|
3180 |
- table => $table1, |
|
3181 |
- column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3", |
|
3182 |
- where => {"$table1.$key2" => 2}, |
|
3183 |
- join => ["left outer join $table2 on $table1.$key1 = $table2.$key1"] |
|
3184 |
-)->all; |
|
3185 |
-is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}]); |
|
3186 |
- |
|
3187 |
-$rows = $dbi->select( |
|
3188 |
- table => $table1, |
|
3189 |
- where => {$key1 => 1}, |
|
3190 |
- join => ["left outer join $table2 on $table1.$key1 = $table2.$key1"] |
|
3191 |
-)->all; |
|
3192 |
-is_deeply($rows, [{$key1 => 1, $key2 => 2}]); |
|
3193 |
- |
|
3194 |
-eval { |
|
3195 |
- $rows = $dbi->select( |
|
3196 |
- table => $table1, |
|
3197 |
- column => "$table1.$key1 as ${table1}_$key1, $table2.$key1 as ${table2}_$key1, $key2, $key3", |
|
3198 |
- where => {"$table1.$key2" => 2}, |
|
3199 |
- join => {"$table1.$key1" => "$table2.$key1"} |
|
3200 |
- ); |
|
3201 |
-}; |
|
3202 |
-like ($@, qr/array/); |
|
3203 |
- |
|
3204 |
-$rows = $dbi->select( |
|
3205 |
- table => $table1, |
|
3206 |
- where => {$key1 => 1}, |
|
3207 |
- join => ["left outer join $table2 on $table1.$key1 = $table2.$key1", |
|
3208 |
- "left outer join $table3 on $table2.$key3 = $table3.$key3"] |
|
3209 |
-)->all; |
|
3210 |
-is_deeply($rows, [{$key1 => 1, $key2 => 2}]); |
|
3211 |
- |
|
3212 |
-$rows = $dbi->select( |
|
3213 |
- column => "$table3.$key4 as ${table3}__$key4", |
|
3214 |
- table => $table1, |
|
3215 |
- where => {"$table1.$key1" => 1}, |
|
3216 |
- join => ["left outer join $table2 on $table1.$key1 = $table2.$key1", |
|
3217 |
- "left outer join $table3 on $table2.$key3 = $table3.$key3"] |
|
3218 |
-)->all; |
|
3219 |
-is_deeply($rows, [{"${table3}__$key4" => 4}]); |
|
3220 |
- |
|
3221 |
-$rows = $dbi->select( |
|
3222 |
- column => "$table1.$key1 as ${table1}__$key1", |
|
3223 |
- table => $table1, |
|
3224 |
- where => {"$table3.$key4" => 4}, |
|
3225 |
- join => ["left outer join $table2 on $table1.$key1 = $table2.$key1", |
|
3226 |
- "left outer join $table3 on $table2.$key3 = $table3.$key3"] |
|
3227 |
-)->all; |
|
3228 |
-is_deeply($rows, [{"${table1}__$key1" => 1}]); |
|
3229 |
- |
|
3230 |
-$dbi = DBIx::Custom->connect; |
|
3231 |
-eval { $dbi->execute("drop table $table1") }; |
|
3232 |
-$dbi->execute($create_table1); |
|
3233 |
-$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
|
3234 |
-eval { $dbi->execute("drop table $table2") }; |
|
3235 |
-$dbi->execute($create_table2); |
|
3236 |
-$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5}); |
|
3237 |
-$rows = $dbi->select( |
|
3238 |
- table => $table1, |
|
3239 |
- 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", |
|
3240 |
- where => {"$table1.$key2" => 2}, |
|
3241 |
- join => ["left outer join ${q}$table2$p on ${q}$table1$p.${q}$key1$p = ${q}$table2$p.${q}$key1$p"], |
|
3242 |
-)->all; |
|
3243 |
-is_deeply($rows, [{"${table1}_$key1" => 1, "${table2}_$key1" => 1, $key2 => 2, $key3 => 5}], |
|
3244 |
- 'quote'); |
|
3245 |
- |
|
3246 |
- |
|
3247 |
-$dbi = DBIx::Custom->connect; |
|
3248 |
-eval { $dbi->execute("drop table $table1") }; |
|
3249 |
-$dbi->execute($create_table1); |
|
3250 |
-$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
|
3251 |
-$sql = <<"EOS"; |
|
3252 |
-left outer join ( |
|
3253 |
- select * from $table1 t1 |
|
3254 |
- where t1.$key2 = ( |
|
3255 |
- select max(t2.$key2) from $table1 t2 |
|
3256 |
- where t1.$key1 = t2.$key1 |
|
3257 |
- ) |
|
3258 |
-) latest_$table1 on $table1.$key1 = latest_$table1.$key1 |
|
3259 |
-EOS |
|
3260 |
-$join = [$sql]; |
|
3261 |
-$rows = $dbi->select( |
|
3262 |
- table => $table1, |
|
3263 |
- column => "latest_$table1.$key1 as latest_${table1}__$key1", |
|
3264 |
- join => $join |
|
3265 |
-)->all; |
|
3266 |
-is_deeply($rows, [{"latest_${table1}__$key1" => 1}]); |
|
3267 |
- |
|
3268 |
-$dbi = DBIx::Custom->connect; |
|
3269 |
-eval { $dbi->execute("drop table $table1") }; |
|
3270 |
-eval { $dbi->execute("drop table $table2") }; |
|
3271 |
-$dbi->execute($create_table1); |
|
3272 |
-$dbi->execute($create_table2); |
|
3273 |
-$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
|
3274 |
-$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 4}); |
|
3275 |
-$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5}); |
|
3276 |
-$result = $dbi->select( |
|
3277 |
- table => $table1, |
|
3278 |
- join => [ |
|
3279 |
- "left outer join $table2 on $table2.$key2 = '4' and $table1.$key1 = $table2.$key1" |
|
3280 |
- ] |
|
3281 |
-); |
|
3282 |
-is_deeply($result->all, [{$key1 => 1, $key2 => 2}]); |
|
3283 |
-$result = $dbi->select( |
|
3284 |
- table => $table1, |
|
3285 |
- column => [{$table2 => [$key3]}], |
|
3286 |
- join => [ |
|
3287 |
- "left outer join $table2 on $table2.$key3 = '4' and $table1.$key1 = $table2.$key1" |
|
3288 |
- ] |
|
3289 |
-); |
|
3290 |
-is_deeply($result->all, [{"$table2.$key3" => 4}]); |
|
3291 |
-$result = $dbi->select( |
|
3292 |
- table => $table1, |
|
3293 |
- column => [{$table2 => [$key3]}], |
|
3294 |
- join => [ |
|
3295 |
- "left outer join $table2 on $table1.$key1 = $table2.$key1 and $table2.$key3 = '4'" |
|
3296 |
- ] |
|
3297 |
-); |
|
3298 |
-is_deeply($result->all, [{"$table2.$key3" => 4}]); |
|
3299 |
- |
|
3300 |
-$dbi = DBIx::Custom->connect; |
|
3301 |
-eval { $dbi->execute("drop table $table1") }; |
|
3302 |
-eval { $dbi->execute("drop table $table2") }; |
|
3303 |
-$dbi->execute($create_table1); |
|
3304 |
-$dbi->execute($create_table2); |
|
3305 |
-$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}); |
|
3306 |
-$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 4}); |
|
3307 |
-$dbi->insert(table => $table2, param => {$key1 => 1, $key3 => 5}); |
|
3308 |
-$result = $dbi->select( |
|
3309 |
- table => $table1, |
|
3310 |
- column => [{$table2 => [$key3]}], |
|
3311 |
- join => [ |
|
3312 |
- { |
|
3313 |
- clause => "left outer join $table2 on $table2.$key3 = '4' and $table1.$key1 = $table2.$key1", |
|
3314 |
- table => [$table1, $table2] |
|
3315 |
- } |
|
3316 |
- ] |
|
3317 |
-); |
|
3318 |
-is_deeply($result->all, [{"$table2.$key3" => 4}]); |
|
3319 |
- |
|
3320 | 3320 |
test 'Model class'; |
3321 | 3321 |
$dbi = MyDBI1->connect; |
3322 | 3322 |
eval { $dbi->execute("drop table $table1") }; |
... | ... |
@@ -10,10 +10,8 @@ sub connect { |
10 | 10 |
|
11 | 11 |
$self->include_model( |
12 | 12 |
MyModel1 => [ |
13 |
- 'table1', |
|
14 |
- 'table2', |
|
15 |
- 'TABLE1', |
|
16 |
- 'TABLE2' |
|
13 |
+ $self->table1, |
|
14 |
+ $self->table2 |
|
17 | 15 |
] |
18 | 16 |
); |
19 | 17 |
} |
... | ... |
@@ -10,10 +10,8 @@ sub connect { |
10 | 10 |
|
11 | 11 |
$self->include_model( |
12 | 12 |
MyModel1 => [ |
13 |
- 'table1', |
|
14 |
- 'table2', |
|
15 |
- 'TABLE1', |
|
16 |
- 'TABLE2' |
|
13 |
+ $self->table1, |
|
14 |
+ $self->table2 |
|
17 | 15 |
] |
18 | 16 |
); |
19 | 17 |
} |