Showing 2 changed files with 190 additions and 189 deletions
+153
t/common.t
... ...
@@ -3193,6 +3193,159 @@ is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
3193 3193
                   {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
3194 3194
                   "basic");
3195 3195
 
3196
+test 'join';
3197
+$dbi = DBIx::Custom->connect;
3198
+eval { $dbi->execute('drop table table1') };
3199
+$dbi->execute($create_table1);
3200
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3201
+$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
3202
+eval { $dbi->execute('drop table table2') };
3203
+$dbi->execute($create_table2);
3204
+$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
3205
+eval { $dbi->execute('drop table table3') };
3206
+$dbi->execute('create table table3 (key3 int, key4 int);');
3207
+$dbi->insert(table => 'table3', param => {key3 => 5, key4 => 4});
3208
+$rows = $dbi->select(
3209
+    table => 'table1',
3210
+    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
3211
+    where   => {'table1.key2' => 2},
3212
+    join  => ['left outer join table2 on table1.key1 = table2.key1']
3213
+)->all;
3214
+is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}]);
3215
+
3216
+$rows = $dbi->select(
3217
+    table => 'table1',
3218
+    where   => {'key1' => 1},
3219
+    join  => ['left outer join table2 on table1.key1 = table2.key1']
3220
+)->all;
3221
+is_deeply($rows, [{key1 => 1, key2 => 2}]);
3222
+
3223
+eval {
3224
+    $rows = $dbi->select(
3225
+        table => 'table1',
3226
+        column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
3227
+        where   => {'table1.key2' => 2},
3228
+        join  => {'table1.key1' => 'table2.key1'}
3229
+    );
3230
+};
3231
+like ($@, qr/array/);
3232
+
3233
+$rows = $dbi->select(
3234
+    table => 'table1',
3235
+    where   => {'key1' => 1},
3236
+    join  => ['left outer join table2 on table1.key1 = table2.key1',
3237
+              'left outer join table3 on table2.key3 = table3.key3']
3238
+)->all;
3239
+is_deeply($rows, [{key1 => 1, key2 => 2}]);
3240
+
3241
+$rows = $dbi->select(
3242
+    column => 'table3.key4 as table3__key4',
3243
+    table => 'table1',
3244
+    where   => {'table1.key1' => 1},
3245
+    join  => ['left outer join table2 on table1.key1 = table2.key1',
3246
+              'left outer join table3 on table2.key3 = table3.key3']
3247
+)->all;
3248
+is_deeply($rows, [{table3__key4 => 4}]);
3249
+
3250
+$rows = $dbi->select(
3251
+    column => 'table1.key1 as table1__key1',
3252
+    table => 'table1',
3253
+    where   => {'table3.key4' => 4},
3254
+    join  => ['left outer join table2 on table1.key1 = table2.key1',
3255
+              'left outer join table3 on table2.key3 = table3.key3']
3256
+)->all;
3257
+is_deeply($rows, [{table1__key1 => 1}]);
3258
+
3259
+$dbi = DBIx::Custom->connect;
3260
+eval { $dbi->execute('drop table table1') };
3261
+$dbi->execute($create_table1);
3262
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3263
+eval { $dbi->execute('drop table table2') };
3264
+$dbi->execute($create_table2);
3265
+$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
3266
+$rows = $dbi->select(
3267
+    table => 'table1',
3268
+    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",
3269
+    where   => {'table1.key2' => 2},
3270
+    join  => ["left outer join ${q}table2$p on ${q}table1$p.${q}key1$p = ${q}table2$p.${q}key1$p"],
3271
+)->all;
3272
+is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}],
3273
+          'quote');
3274
+
3275
+
3276
+$dbi = DBIx::Custom->connect;
3277
+eval { $dbi->execute('drop table table1') };
3278
+$dbi->execute($create_table1);
3279
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3280
+$sql = <<"EOS";
3281
+left outer join (
3282
+  select * from table1 as t1
3283
+  where t1.key2 = (
3284
+    select max(t2.key2) from table1 as t2
3285
+    where t1.key1 = t2.key1
3286
+  )
3287
+) as latest_table1 on table1.key1 = latest_table1.key1
3288
+EOS
3289
+$join = [$sql];
3290
+$rows = $dbi->select(
3291
+    table => 'table1',
3292
+    column => 'latest_table1.key1 as latest_table1__key1',
3293
+    join  => $join
3294
+)->all;
3295
+is_deeply($rows, [{latest_table1__key1 => 1}]);
3296
+
3297
+$dbi = DBIx::Custom->connect;
3298
+eval { $dbi->execute('drop table table1') };
3299
+eval { $dbi->execute('drop table table2') };
3300
+$dbi->execute($create_table1);
3301
+$dbi->execute($create_table2);
3302
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3303
+$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
3304
+$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
3305
+$result = $dbi->select(
3306
+    table => 'table1',
3307
+    join => [
3308
+        "left outer join table2 on table2.key2 = '4' and table1.key1 = table2.key1"
3309
+    ]
3310
+);
3311
+is_deeply($result->all, [{key1 => 1, key2 => 2}]);
3312
+$result = $dbi->select(
3313
+    table => 'table1',
3314
+    column => [{table2 => ['key3']}],
3315
+    join => [
3316
+        "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1"
3317
+    ]
3318
+);
3319
+is_deeply($result->all, [{'table2.key3' => 4}]);
3320
+$result = $dbi->select(
3321
+    table => 'table1',
3322
+    column => [{table2 => ['key3']}],
3323
+    join => [
3324
+        "left outer join table2 on table1.key1 = table2.key1 and table2.key3 = '4'"
3325
+    ]
3326
+);
3327
+is_deeply($result->all, [{'table2.key3' => 4}]);
3328
+
3329
+$dbi = DBIx::Custom->connect;
3330
+eval { $dbi->execute('drop table table1') };
3331
+eval { $dbi->execute('drop table table2') };
3332
+$dbi->execute($create_table1);
3333
+$dbi->execute($create_table2);
3334
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3335
+$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
3336
+$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
3337
+$result = $dbi->select(
3338
+    table => 'table1',
3339
+    column => [{table2 => ['key3']}],
3340
+    join => [
3341
+        {
3342
+            clause => "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1",
3343
+            table => ['table1', 'table2']
3344
+        }
3345
+    ]
3346
+);
3347
+is_deeply($result->all, [{'table2.key3' => 4}]);
3348
+
3196 3349
 
3197 3350
 
3198 3351
 
+37 -189
t/sqlite.t
... ...
@@ -211,195 +211,6 @@ $dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1'
211 211
 $result = $dbi->select(table => 'table1');
212 212
 is($result->one->{key1}, 'A');
213 213
 
214
-
215
-test 'join';
216
-$dbi = DBIx::Custom->connect;
217
-eval { $dbi->execute('drop table table1') };
218
-$dbi->execute($create_table1);
219
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
220
-$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
221
-$dbi->execute($create_table2);
222
-$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
223
-$dbi->execute('create table table3 (key3 int, key4 int);');
224
-$dbi->insert(table => 'table3', param => {key3 => 5, key4 => 4});
225
-$rows = $dbi->select(
226
-    table => 'table1',
227
-    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
228
-    where   => {'table1.key2' => 2},
229
-    join  => ['left outer join table2 on table1.key1 = table2.key1']
230
-)->all;
231
-is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}]);
232
-
233
-$rows = $dbi->select(
234
-    table => 'table1',
235
-    where   => {'key1' => 1},
236
-    join  => ['left outer join table2 on table1.key1 = table2.key1']
237
-)->all;
238
-is_deeply($rows, [{key1 => 1, key2 => 2}]);
239
-
240
-eval {
241
-    $rows = $dbi->select(
242
-        table => 'table1',
243
-        column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
244
-        where   => {'table1.key2' => 2},
245
-        join  => {'table1.key1' => 'table2.key1'}
246
-    );
247
-};
248
-like ($@, qr/array/);
249
-
250
-$rows = $dbi->select(
251
-    table => 'table1',
252
-    where   => {'key1' => 1},
253
-    join  => ['left outer join table2 on table1.key1 = table2.key1',
254
-              'left outer join table3 on table2.key3 = table3.key3']
255
-)->all;
256
-is_deeply($rows, [{key1 => 1, key2 => 2}]);
257
-
258
-$rows = $dbi->select(
259
-    column => 'table3.key4 as table3__key4',
260
-    table => 'table1',
261
-    where   => {'table1.key1' => 1},
262
-    join  => ['left outer join table2 on table1.key1 = table2.key1',
263
-              'left outer join table3 on table2.key3 = table3.key3']
264
-)->all;
265
-is_deeply($rows, [{table3__key4 => 4}]);
266
-
267
-$rows = $dbi->select(
268
-    column => 'table1.key1 as table1__key1',
269
-    table => 'table1',
270
-    where   => {'table3.key4' => 4},
271
-    join  => ['left outer join table2 on table1.key1 = table2.key1',
272
-              'left outer join table3 on table2.key3 = table3.key3']
273
-)->all;
274
-is_deeply($rows, [{table1__key1 => 1}]);
275
-
276
-$dbi = DBIx::Custom->connect;
277
-$dbi->quote('"');
278
-eval { $dbi->execute('drop table table1') };
279
-$dbi->execute($create_table1);
280
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
281
-$dbi->execute($create_table2);
282
-$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
283
-$rows = $dbi->select(
284
-    table => 'table1',
285
-    column => '"table1"."key1" as "table1_key1", "table2"."key1" as "table2_key1", "key2", "key3"',
286
-    where   => {'table1.key2' => 2},
287
-    join  => ['left outer join "table2" on "table1"."key1" = "table2"."key1"'],
288
-)->all;
289
-is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}],
290
-          'quote');
291
-
292
-
293
-$dbi = DBIx::Custom->connect;
294
-eval { $dbi->execute('drop table table1') };
295
-$dbi->execute($create_table1);
296
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
297
-$sql = <<"EOS";
298
-left outer join (
299
-  select * from table1 as t1
300
-  where t1.key2 = (
301
-    select max(t2.key2) from table1 as t2
302
-    where t1.key1 = t2.key1
303
-  )
304
-) as latest_table1 on table1.key1 = latest_table1.key1
305
-EOS
306
-$join = [$sql];
307
-$rows = $dbi->select(
308
-    table => 'table1',
309
-    column => 'latest_table1.key1 as latest_table1__key1',
310
-    join  => $join
311
-)->all;
312
-is_deeply($rows, [{latest_table1__key1 => 1}]);
313
-
314
-$dbi = DBIx::Custom->connect;
315
-eval { $dbi->execute('drop table table1') };
316
-eval { $dbi->execute('drop table table2') };
317
-$dbi->execute($create_table1);
318
-$dbi->execute($create_table2);
319
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
320
-$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
321
-$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
322
-$result = $dbi->select(
323
-    table => 'table1',
324
-    join => [
325
-        "left outer join table2 on table2.key2 = '4' and table1.key1 = table2.key1"
326
-    ]
327
-);
328
-is_deeply($result->all, [{key1 => 1, key2 => 2}]);
329
-$result = $dbi->select(
330
-    table => 'table1',
331
-    column => [{table2 => ['key3']}],
332
-    join => [
333
-        "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1"
334
-    ]
335
-);
336
-is_deeply($result->all, [{'table2.key3' => 4}]);
337
-$result = $dbi->select(
338
-    table => 'table1',
339
-    column => [{table2 => ['key3']}],
340
-    join => [
341
-        "left outer join table2 on table1.key1 = table2.key1 and table2.key3 = '4'"
342
-    ]
343
-);
344
-is_deeply($result->all, [{'table2.key3' => 4}]);
345
-
346
-$dbi = DBIx::Custom->connect;
347
-eval { $dbi->execute('drop table table1') };
348
-eval { $dbi->execute('drop table table2') };
349
-$dbi->execute($create_table1);
350
-$dbi->execute($create_table2);
351
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
352
-$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
353
-$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
354
-$result = $dbi->select(
355
-    table => 'table1',
356
-    column => [{table2 => ['key3']}],
357
-    join => [
358
-        {
359
-            clause => "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1",
360
-            table => ['table1', 'table2']
361
-        }
362
-    ]
363
-);
364
-is_deeply($result->all, [{'table2.key3' => 4}]);
365
-
366
-
367
-
368
-
369
-test 'type option'; # DEPRECATED!
370
-$dbi = DBIx::Custom->connect(
371
-    data_source => 'dbi:SQLite:dbname=:memory:',
372
-    dbi_option => {
373
-        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
374
-    }
375
-);
376
-$binary = pack("I3", 1, 2, 3);
377
-eval { $dbi->execute('drop table table1') };
378
-$dbi->execute('create table table1(key1, key2)');
379
-$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]);
380
-$result = $dbi->select(table => 'table1');
381
-$row   = $result->one;
382
-is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
383
-$result = $dbi->execute('select length(key1) as key1_length from table1');
384
-$row = $result->one;
385
-is($row->{key1_length}, length $binary);
386
-
387
-test 'type_rule from';
388
-$dbi = DBIx::Custom->connect;
389
-$dbi->type_rule(
390
-    from1 => {
391
-        date => sub { uc $_[0] }
392
-    }
393
-);
394
-$dbi->execute("create table table1 (key1 Date, key2 datetime)");
395
-$dbi->insert({key1 => 'a'}, table => 'table1');
396
-$result = $dbi->select(table => 'table1');
397
-is($result->fetch_first->[0], 'A');
398
-
399
-$result = $dbi->select(table => 'table1');
400
-is($result->one->{key1}, 'A');
401
-
402
-
403 214
 test 'type_rule into';
404 215
 $dbi = DBIx::Custom->connect;
405 216
 $dbi->execute("create table table1 (key1 Date, key2 datetime)");
... ...
@@ -954,6 +765,43 @@ eval {$result->fetch_hash_multi};
954 765
 like($@, qr/Row count must be specified/, "Not specified row count");
955 766
 
956 767
 
768
+test 'type option'; # DEPRECATED!
769
+$dbi = DBIx::Custom->connect(
770
+    data_source => 'dbi:SQLite:dbname=:memory:',
771
+    dbi_option => {
772
+        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
773
+    }
774
+);
775
+$binary = pack("I3", 1, 2, 3);
776
+eval { $dbi->execute('drop table table1') };
777
+$dbi->execute('create table table1(key1, key2)');
778
+$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]);
779
+$result = $dbi->select(table => 'table1');
780
+$row   = $result->one;
781
+is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
782
+$result = $dbi->execute('select length(key1) as key1_length from table1');
783
+$row = $result->one;
784
+is($row->{key1_length}, length $binary);
785
+
786
+test 'type_rule from';
787
+$dbi = DBIx::Custom->connect;
788
+$dbi->type_rule(
789
+    from1 => {
790
+        date => sub { uc $_[0] }
791
+    }
792
+);
793
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
794
+$dbi->insert({key1 => 'a'}, table => 'table1');
795
+$result = $dbi->select(table => 'table1');
796
+is($result->fetch_first->[0], 'A');
797
+
798
+$result = $dbi->select(table => 'table1');
799
+is($result->one->{key1}, 'A');
800
+
801
+
802
+
803
+
804
+
957 805
 # DEPRECATED! test
958 806
 test 'filter __ expression';
959 807
 $dbi = DBIx::Custom->connect;