Showing 2 changed files with 874 additions and 786 deletions
+1
t/common.t
... ...
@@ -49,6 +49,7 @@ my $where;
49 49
 my $update_param;
50 50
 my $insert_param;
51 51
 my $join;
52
+my $binary;
52 53
 
53 54
 # Drop table
54 55
 eval { $dbi->execute('drop table table1') };
+873 -786
t/sqlite.t
... ...
@@ -60,6 +60,7 @@ my $where;
60 60
 my $update_param;
61 61
 my $insert_param;
62 62
 my $join;
63
+my $binary;
63 64
 
64 65
 # Prepare table
65 66
 $dbi = DBIx::Custom->connect;
... ...
@@ -1759,6 +1760,7 @@ is($dbi->select(table => 'table1')->one->{key3}, 3);
1759 1760
 
1760 1761
 test 'update_at';
1761 1762
 $dbi = DBIx::Custom->connect;
1763
+eval { $dbi->execute('drop table table1') };
1762 1764
 $dbi->execute($create_table1_2);
1763 1765
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1764 1766
 $dbi->update_at(
... ...
@@ -1784,6 +1786,7 @@ is($dbi->select(table => 'table1')->one->{key2}, 2);
1784 1786
 is($dbi->select(table => 'table1')->one->{key3}, 4);
1785 1787
 
1786 1788
 $dbi = DBIx::Custom->connect;
1789
+eval { $dbi->execute('drop table table1') };
1787 1790
 $dbi->execute($create_table1_2);
1788 1791
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1789 1792
 $dbi->update_at(
... ...
@@ -1798,6 +1801,7 @@ is($dbi->select(table => 'table1')->one->{key3}, 4);
1798 1801
 
1799 1802
 test 'select_at';
1800 1803
 $dbi = DBIx::Custom->connect;
1804
+eval { $dbi->execute('drop table table1') };
1801 1805
 $dbi->execute($create_table1_2);
1802 1806
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1803 1807
 $result = $dbi->select_at(
... ...
@@ -1892,6 +1896,9 @@ test 'model delete_at';
1892 1896
     }
1893 1897
 }
1894 1898
 $dbi = MyDBI6->connect;
1899
+eval { $dbi->execute('drop table table1') };
1900
+eval { $dbi->execute('drop table table2') };
1901
+eval { $dbi->execute('drop table table3') };
1895 1902
 $dbi->execute($create_table1_2);
1896 1903
 $dbi->execute("create table table2 (key1, key2, key3)");
1897 1904
 $dbi->execute("create table table3 (key1, key2, key3)");
... ...
@@ -1907,6 +1914,7 @@ is_deeply($dbi->select(table => 'table1')->all, []);
1907 1914
 
1908 1915
 test 'model insert_at';
1909 1916
 $dbi = MyDBI6->connect;
1917
+eval { $dbi->execute('drop table table1') };
1910 1918
 $dbi->execute($create_table1_2);
1911 1919
 $dbi->model('table1')->insert_at(
1912 1920
     where => [1, 2],
... ...
@@ -1920,6 +1928,7 @@ is($row->{key3}, 3);
1920 1928
 
1921 1929
 test 'model update_at';
1922 1930
 $dbi = MyDBI6->connect;
1931
+eval { $dbi->execute('drop table table1') };
1923 1932
 $dbi->execute($create_table1_2);
1924 1933
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1925 1934
 $dbi->model('table1')->update_at(
... ...
@@ -1934,6 +1943,7 @@ is($row->{key3}, 4);
1934 1943
 
1935 1944
 test 'model select_at';
1936 1945
 $dbi = MyDBI6->connect;
1946
+eval { $dbi->execute('drop table table1') };
1937 1947
 $dbi->execute($create_table1_2);
1938 1948
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1939 1949
 $result = $dbi->model('table1')->select_at(where => [1, 2]);
... ...
@@ -1959,6 +1969,8 @@ test 'mycolumn and column';
1959 1969
     }
1960 1970
 }
1961 1971
 $dbi = MyDBI7->connect;
1972
+eval { $dbi->execute('drop table table1') };
1973
+eval { $dbi->execute('drop table table2') };
1962 1974
 $dbi->execute($create_table1);
1963 1975
 $dbi->execute('create table table2 (key1 char(255), key3 char(255));');
1964 1976
 $dbi->separator('__');
... ...
@@ -1975,6 +1987,7 @@ is_deeply($result->one,
1975 1987
 
1976 1988
 test 'update_param';
1977 1989
 $dbi = DBIx::Custom->connect;
1990
+eval { $dbi->execute('drop table table1') };
1978 1991
 $dbi->execute($create_table1_2);
1979 1992
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1980 1993
 $dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
... ...
@@ -1994,6 +2007,7 @@ is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
1994 2007
 
1995 2008
 
1996 2009
 $dbi = DBIx::Custom->connect;
2010
+eval { $dbi->execute('drop table table1') };
1997 2011
 $dbi->execute($create_table1_2);
1998 2012
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1999 2013
 $dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
... ...
@@ -2012,6 +2026,7 @@ is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
2012 2026
                   "basic");
2013 2027
 
2014 2028
 $dbi = DBIx::Custom->connect;
2029
+eval { $dbi->execute('drop table table1') };
2015 2030
 $dbi->execute($create_table1_2);
2016 2031
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
2017 2032
 $dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
... ...
@@ -2036,6 +2051,7 @@ like($@, qr/not safety/);
2036 2051
 
2037 2052
 test 'update_param';
2038 2053
 $dbi = DBIx::Custom->connect;
2054
+eval { $dbi->execute('drop table table1') };
2039 2055
 $dbi->execute($create_table1_2);
2040 2056
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
2041 2057
 $dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
... ...
@@ -2056,6 +2072,7 @@ is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
2056 2072
 
2057 2073
 test 'insert_param';
2058 2074
 $dbi = DBIx::Custom->connect;
2075
+eval { $dbi->execute('drop table table1') };
2059 2076
 $dbi->execute($create_table1_2);
2060 2077
 $param = {key1 => 1, key2 => 2};
2061 2078
 $insert_param = $dbi->insert_param($param);
... ...
@@ -2068,6 +2085,7 @@ is($dbi->select(table => 'table1')->one->{key2}, 2);
2068 2085
 
2069 2086
 $dbi = DBIx::Custom->connect;
2070 2087
 $dbi->quote('"');
2088
+eval { $dbi->execute('drop table table1') };
2071 2089
 $dbi->execute($create_table1_2);
2072 2090
 $param = {key1 => 1, key2 => 2};
2073 2091
 $insert_param = $dbi->insert_param($param);
... ...
@@ -2084,6 +2102,7 @@ like($@, qr/not safety/);
2084 2102
 
2085 2103
 test 'join';
2086 2104
 $dbi = DBIx::Custom->connect;
2105
+eval { $dbi->execute('drop table table1') };
2087 2106
 $dbi->execute($create_table1);
2088 2107
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2089 2108
 $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
... ...
@@ -2144,6 +2163,7 @@ is_deeply($rows, [{table1__key1 => 1}]);
2144 2163
 
2145 2164
 $dbi = DBIx::Custom->connect;
2146 2165
 $dbi->quote('"');
2166
+eval { $dbi->execute('drop table table1') };
2147 2167
 $dbi->execute($create_table1);
2148 2168
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2149 2169
 $dbi->execute('create table table2 (key1 char(255), key3 char(255));');
... ...
@@ -2172,6 +2192,7 @@ is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}],
2172 2192
 }
2173 2193
 
2174 2194
 $dbi = DBIx::Custom->connect;
2195
+eval { $dbi->execute('drop table table1') };
2175 2196
 $dbi->execute($create_table1);
2176 2197
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2177 2198
 $sql = <<"EOS";
... ...
@@ -2192,6 +2213,8 @@ $rows = $dbi->select(
2192 2213
 is_deeply($rows, [{latest_table1__key1 => 1}]);
2193 2214
 
2194 2215
 $dbi = DBIx::Custom->connect;
2216
+eval { $dbi->execute('drop table table1') };
2217
+eval { $dbi->execute('drop table table2') };
2195 2218
 $dbi->execute($create_table1);
2196 2219
 $dbi->execute('create table table2 (key1 char(255), key3 char(255));');
2197 2220
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
... ...
@@ -2222,6 +2245,8 @@ $result = $dbi->select(
2222 2245
 is_deeply($result->all, [{'table2.key3' => 4}]);
2223 2246
 
2224 2247
 $dbi = DBIx::Custom->connect;
2248
+eval { $dbi->execute('drop table table1') };
2249
+eval { $dbi->execute('drop table table2') };
2225 2250
 $dbi->execute($create_table1);
2226 2251
 $dbi->execute('create table table2 (key1 char(255), key3 char(255));');
2227 2252
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
... ...
@@ -2241,6 +2266,8 @@ is_deeply($result->all, [{'table2.key3' => 4}]);
2241 2266
 
2242 2267
 test 'mycolumn';
2243 2268
 $dbi = MyDBI8->connect;
2269
+eval { $dbi->execute('drop table table1') };
2270
+eval { $dbi->execute('drop table table2') };
2244 2271
 $dbi->execute($create_table1);
2245 2272
 $dbi->execute('create table table2 (key1 char(255), key3 char(255));');
2246 2273
 $dbi->setup_model;
... ...
@@ -2306,6 +2333,7 @@ test 'dbi method from model';
2306 2333
     }
2307 2334
 }
2308 2335
 $dbi = MyDBI9->connect;
2336
+eval { $dbi->execute('drop table table1') };
2309 2337
 $dbi->execute($create_table1);
2310 2338
 $model = $dbi->model('table1');
2311 2339
 eval{$model->execute('select * from table1')};
... ...
@@ -2313,6 +2341,7 @@ ok(!$@);
2313 2341
 
2314 2342
 test 'column table option';
2315 2343
 $dbi = MyDBI9->connect;
2344
+eval { $dbi->execute('drop table table1') };
2316 2345
 $dbi->execute($create_table1);
2317 2346
 $dbi->execute('create table table2 (key1 char(255), key3 char(255));');
2318 2347
 $dbi->setup_model;
... ...
@@ -2350,12 +2379,12 @@ is_deeply($result->one,
2350 2379
 
2351 2380
 test 'type option'; # DEPRECATED!
2352 2381
 $dbi = DBIx::Custom->connect(
2353
-    data_source => 'dbi:SQLite:dbname=:memory:',
2354 2382
     dbi_option => {
2355 2383
         $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
2356 2384
     }
2357 2385
 );
2358
-my $binary = pack("I3", 1, 2, 3);
2386
+$binary = pack("I3", 1, 2, 3);
2387
+eval { $dbi->execute('drop table table1') };
2359 2388
 $dbi->execute('create table table1(key1, key2)');
2360 2389
 $dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]);
2361 2390
 $result = $dbi->select(table => 'table1');
... ...
@@ -2376,12 +2405,12 @@ is($row->{key1_length}, length $binary);
2376 2405
 
2377 2406
 test 'bind_type option';
2378 2407
 $dbi = DBIx::Custom->connect(
2379
-    data_source => 'dbi:SQLite:dbname=:memory:',
2380 2408
     dbi_option => {
2381 2409
         $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
2382 2410
     }
2383 2411
 );
2384 2412
 $binary = pack("I3", 1, 2, 3);
2413
+eval { $dbi->execute('drop table table1') };
2385 2414
 $dbi->execute('create table table1(key1, key2)');
2386 2415
 $dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, bind_type => [key1 => DBI::SQL_BLOB]);
2387 2416
 $result = $dbi->select(table => 'table1');
... ...
@@ -2401,12 +2430,12 @@ is($row->{key1_length}, length $binary);
2401 2430
 
2402 2431
 test 'model type attribute';
2403 2432
 $dbi = DBIx::Custom->connect(
2404
-    data_source => 'dbi:SQLite:dbname=:memory:',
2405 2433
     dbi_option => {
2406 2434
         $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
2407 2435
     }
2408 2436
 );
2409 2437
 $binary = pack("I3", 1, 2, 3);
2438
+eval { $dbi->execute('drop table table1') };
2410 2439
 $dbi->execute('create table table1(key1, key2)');
2411 2440
 $model = $dbi->create_model(table => 'table1', bind_type => [key1 => DBI::SQL_BLOB]);
2412 2441
 $model->insert(param => {key1 => $binary, key2 => 'あ'});
... ...
@@ -2419,6 +2448,8 @@ is($row->{key1_length}, length $binary);
2419 2448
 
2420 2449
 test 'create_model';
2421 2450
 $dbi = DBIx::Custom->connect;
2451
+eval { $dbi->execute('drop table table1') };
2452
+eval { $dbi->execute('drop table table2') };
2422 2453
 $dbi->execute($create_table1);
2423 2454
 $dbi->execute('create table table2 (key1 char(255), key3 char(255));');
2424 2455
 
... ...
@@ -2453,6 +2484,7 @@ is_deeply($model2->select->one, {key1 => 1, key3 => 3});
2453 2484
 test 'model method';
2454 2485
 test 'create_model';
2455 2486
 $dbi = DBIx::Custom->connect;
2487
+eval { $dbi->execute('drop table table2') };
2456 2488
 $dbi->execute('create table table2 (key1 char(255), key3 char(255));');
2457 2489
 $dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2458 2490
 $model = $dbi->create_model(
... ...
@@ -2480,6 +2512,7 @@ is_deeply($param, {key1 => [1, 2, 3, 4], key2 => [1, 2, 3], key3 => [1, 2, 3]});
2480 2512
 
2481 2513
 test 'select() param option';
2482 2514
 $dbi = DBIx::Custom->connect;
2515
+eval { $dbi->execute('drop table table1') };
2483 2516
 $dbi->execute($create_table1);
2484 2517
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2485 2518
 $dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
... ...
@@ -2499,6 +2532,7 @@ is_deeply($rows, [{table1_key1 => 2, key2 => 3, key3 => 5}]);
2499 2532
 
2500 2533
 test 'select() wrap option';
2501 2534
 $dbi = DBIx::Custom->connect;
2535
+eval { $dbi->execute('drop table table1') };
2502 2536
 $dbi->execute($create_table1);
2503 2537
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2504 2538
 $dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
... ...
@@ -2520,6 +2554,7 @@ like($@, qr/array/);
2520 2554
 
2521 2555
 test 'select() string where';
2522 2556
 $dbi = DBIx::Custom->connect;
2557
+eval { $dbi->execute('drop table table1') };
2523 2558
 $dbi->execute($create_table1);
2524 2559
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2525 2560
 $dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
... ...
@@ -2531,6 +2566,7 @@ $rows = $dbi->select(
2531 2566
 is_deeply($rows, [{key1 => 1, key2 => 2}]);
2532 2567
 
2533 2568
 $dbi = DBIx::Custom->connect;
2569
+eval { $dbi->execute('drop table table1') };
2534 2570
 $dbi->execute($create_table1);
2535 2571
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2536 2572
 $dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
... ...
@@ -2545,6 +2581,7 @@ is_deeply($rows, [{key1 => 1, key2 => 2}]);
2545 2581
 
2546 2582
 test 'delete() string where';
2547 2583
 $dbi = DBIx::Custom->connect;
2584
+eval { $dbi->execute('drop table table1') };
2548 2585
 $dbi->execute($create_table1);
2549 2586
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2550 2587
 $dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
... ...
@@ -2557,6 +2594,7 @@ $rows = $dbi->select(table => 'table1')->all;
2557 2594
 is_deeply($rows, [{key1 => 2, key2 => 3}]);
2558 2595
 
2559 2596
 $dbi = DBIx::Custom->connect;
2597
+eval { $dbi->execute('drop table table1') };
2560 2598
 $dbi->execute($create_table1);
2561 2599
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2562 2600
 $dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
... ...
@@ -2573,6 +2611,7 @@ is_deeply($rows, [{key1 => 2, key2 => 3}]);
2573 2611
 
2574 2612
 test 'update() string where';
2575 2613
 $dbi = DBIx::Custom->connect;
2614
+eval { $dbi->execute('drop table table1') };
2576 2615
 $dbi->execute($create_table1);
2577 2616
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2578 2617
 $dbi->update(
... ...
@@ -2585,6 +2624,7 @@ $rows = $dbi->select(table => 'table1')->all;
2585 2624
 is_deeply($rows, [{key1 => 5, key2 => 2}]);
2586 2625
 
2587 2626
 $dbi = DBIx::Custom->connect;
2627
+eval { $dbi->execute('drop table table1') };
2588 2628
 $dbi->execute($create_table1);
2589 2629
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2590 2630
 $dbi->update(
... ...
@@ -2600,6 +2640,7 @@ is_deeply($rows, [{key1 => 5, key2 => 2}]);
2600 2640
 
2601 2641
 test 'insert id and primary_key option';
2602 2642
 $dbi = DBIx::Custom->connect;
2643
+eval { $dbi->execute('drop table table1') };
2603 2644
 $dbi->execute($create_table1_2);
2604 2645
 $dbi->insert(
2605 2646
     primary_key => ['key1', 'key2'], 
... ...
@@ -2624,6 +2665,7 @@ is($dbi->select(table => 'table1')->one->{key2}, 2);
2624 2665
 is($dbi->select(table => 'table1')->one->{key3}, 3);
2625 2666
 
2626 2667
 $dbi = DBIx::Custom->connect;
2668
+eval { $dbi->execute('drop table table1') };
2627 2669
 $dbi->execute($create_table1_2);
2628 2670
 $dbi->insert(
2629 2671
     {key3 => 3},
... ...
@@ -2638,6 +2680,7 @@ is($dbi->select(table => 'table1')->one->{key3}, 3);
2638 2680
 
2639 2681
 test 'model insert id and primary_key option';
2640 2682
 $dbi = MyDBI6->connect;
2683
+eval { $dbi->execute('drop table table1') };
2641 2684
 $dbi->execute($create_table1_2);
2642 2685
 $dbi->model('table1')->insert(
2643 2686
     id => [1, 2],
... ...
@@ -2650,6 +2693,7 @@ is($row->{key2}, 2);
2650 2693
 is($row->{key3}, 3);
2651 2694
 
2652 2695
 $dbi = MyDBI6->connect;
2696
+eval { $dbi->execute('drop table table1') };
2653 2697
 $dbi->execute($create_table1_2);
2654 2698
 $dbi->model('table1')->insert(
2655 2699
     {key3 => 3},
... ...
@@ -2663,6 +2707,7 @@ is($row->{key3}, 3);
2663 2707
 
2664 2708
 test 'update and id option';
2665 2709
 $dbi = DBIx::Custom->connect;
2710
+eval { $dbi->execute('drop table table1') };
2666 2711
 $dbi->execute($create_table1_2);
2667 2712
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2668 2713
 $dbi->update(
... ...
@@ -2688,6 +2733,7 @@ is($dbi->select(table => 'table1')->one->{key2}, 2);
2688 2733
 is($dbi->select(table => 'table1')->one->{key3}, 4);
2689 2734
 
2690 2735
 $dbi = DBIx::Custom->connect;
2736
+eval { $dbi->execute('drop table table1') };
2691 2737
 $dbi->execute($create_table1_2);
2692 2738
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2693 2739
 $dbi->update(
... ...
@@ -2703,6 +2749,7 @@ is($dbi->select(table => 'table1')->one->{key3}, 4);
2703 2749
 
2704 2750
 test 'model update and id option';
2705 2751
 $dbi = MyDBI6->connect;
2752
+eval { $dbi->execute('drop table table1') };
2706 2753
 $dbi->execute($create_table1_2);
2707 2754
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2708 2755
 $dbi->model('table1')->update(
... ...
@@ -2718,6 +2765,7 @@ is($row->{key3}, 4);
2718 2765
 
2719 2766
 test 'delete and id option';
2720 2767
 $dbi = DBIx::Custom->connect;
2768
+eval { $dbi->execute('drop table table1') };
2721 2769
 $dbi->execute($create_table1_2);
2722 2770
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2723 2771
 $dbi->delete(
... ...
@@ -2738,6 +2786,9 @@ is_deeply($dbi->select(table => 'table1')->all, []);
2738 2786
 
2739 2787
 test 'model delete and id option';
2740 2788
 $dbi = MyDBI6->connect;
2789
+eval { $dbi->execute('drop table table1') };
2790
+eval { $dbi->execute('drop table table2') };
2791
+eval { $dbi->execute('drop table table3') };
2741 2792
 $dbi->execute($create_table1_2);
2742 2793
 $dbi->execute("create table table2 (key1, key2, key3)");
2743 2794
 $dbi->execute("create table table3 (key1, key2, key3)");
... ...
@@ -2754,6 +2805,7 @@ is_deeply($dbi->select(table => 'table1')->all, []);
2754 2805
 
2755 2806
 test 'select and id option';
2756 2807
 $dbi = DBIx::Custom->connect;
2808
+eval { $dbi->execute('drop table table1') };
2757 2809
 $dbi->execute($create_table1_2);
2758 2810
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2759 2811
 $result = $dbi->select(
... ...
@@ -2793,6 +2845,7 @@ is($row->{key3}, 3);
2793 2845
 
2794 2846
 test 'model select_at';
2795 2847
 $dbi = MyDBI6->connect;
2848
+eval { $dbi->execute('drop table table1') };
2796 2849
 $dbi->execute($create_table1_2);
2797 2850
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2798 2851
 $result = $dbi->model('table1')->select(id => [1, 2]);
... ...
@@ -2803,6 +2856,8 @@ is($row->{key3}, 3);
2803 2856
 
2804 2857
 test 'column separator is default .';
2805 2858
 $dbi = MyDBI7->connect;
2859
+eval { $dbi->execute('drop table table1') };
2860
+eval { $dbi->execute('drop table table2') };
2806 2861
 $dbi->execute($create_table1);
2807 2862
 $dbi->execute('create table table2 (key1 char(255), key3 char(255));');
2808 2863
 $dbi->setup_model;
... ...
@@ -2824,547 +2879,585 @@ is_deeply($result->one,
2824 2879
           {'table2.key1' => 1, 'table2.key3' => 3});
2825 2880
 
2826 2881
 
2827
-test 'type_rule from';
2882
+
2883
+test 'separator';
2828 2884
 $dbi = DBIx::Custom->connect;
2829
-$dbi->type_rule(
2830
-    from1 => {
2831
-        date => sub { uc $_[0] }
2832
-    }
2885
+eval { $dbi->execute('drop table table1') };
2886
+eval { $dbi->execute('drop table table2') };
2887
+$dbi->execute($create_table1);
2888
+$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
2889
+
2890
+$dbi->create_model(
2891
+    table => 'table1',
2892
+    join => [
2893
+       'left outer join table2 on table1.key1 = table2.key1'
2894
+    ],
2895
+    primary_key => ['key1'],
2833 2896
 );
2834
-$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2835
-$dbi->insert({key1 => 'a'}, table => 'table1');
2836
-$result = $dbi->select(table => 'table1');
2837
-is($result->fetch_first->[0], 'A');
2897
+$model2 = $dbi->create_model(
2898
+    table => 'table2',
2899
+);
2900
+$dbi->setup_model;
2901
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2902
+$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2903
+$model = $dbi->model('table1');
2904
+$result = $model->select(
2905
+    column => [
2906
+        $model->mycolumn,
2907
+        {table2 => [qw/key1 key3/]}
2908
+    ],
2909
+    where => {'table1.key1' => 1}
2910
+);
2911
+is_deeply($result->one,
2912
+          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
2913
+is_deeply($model2->select->one, {key1 => 1, key3 => 3});
2838 2914
 
2839
-$result = $dbi->select(table => 'table1');
2840
-is($result->one->{key1}, 'A');
2915
+$dbi->separator('__');
2916
+$model = $dbi->model('table1');
2917
+$result = $model->select(
2918
+    column => [
2919
+        $model->mycolumn,
2920
+        {table2 => [qw/key1 key3/]}
2921
+    ],
2922
+    where => {'table1.key1' => 1}
2923
+);
2924
+is_deeply($result->one,
2925
+          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
2926
+is_deeply($model2->select->one, {key1 => 1, key3 => 3});
2841 2927
 
2928
+$dbi->separator('-');
2929
+$model = $dbi->model('table1');
2930
+$result = $model->select(
2931
+    column => [
2932
+        $model->mycolumn,
2933
+        {table2 => [qw/key1 key3/]}
2934
+    ],
2935
+    where => {'table1.key1' => 1}
2936
+);
2937
+is_deeply($result->one,
2938
+          {key1 => 1, key2 => 2, 'table2-key1' => 1, 'table2-key3' => 3});
2939
+is_deeply($model2->select->one, {key1 => 1, key3 => 3});
2842 2940
 
2843
-test 'type_rule into';
2941
+
2942
+test 'filter_off';
2844 2943
 $dbi = DBIx::Custom->connect;
2845
-$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2846
-$dbi->type_rule(
2847
-    into1 => {
2848
-        date => sub { uc $_[0] }
2849
-    }
2944
+eval { $dbi->execute('drop table table1') };
2945
+eval { $dbi->execute('drop table table2') };
2946
+$dbi->execute($create_table1);
2947
+$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
2948
+
2949
+$dbi->create_model(
2950
+    table => 'table1',
2951
+    join => [
2952
+       'left outer join table2 on table1.key1 = table2.key1'
2953
+    ],
2954
+    primary_key => ['key1'],
2850 2955
 );
2851
-$dbi->insert({key1 => 'a'}, table => 'table1');
2852
-$result = $dbi->select(table => 'table1');
2853
-is($result->one->{key1}, 'A');
2956
+$dbi->setup_model;
2957
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2958
+$model = $dbi->model('table1');
2959
+$result = $model->select(column => 'key1');
2960
+$result->filter(key1 => sub { $_[0] * 2 });
2961
+is_deeply($result->one, {key1 => 2});
2854 2962
 
2963
+test 'available_datetype';
2855 2964
 $dbi = DBIx::Custom->connect;
2856
-$dbi->execute("create table table1 (key1 date, key2 datetime)");
2857
-$dbi->type_rule(
2858
-    into1 => [
2859
-         [qw/date datetime/] => sub { uc $_[0] }
2860
-    ]
2861
-);
2862
-$dbi->insert({key1 => 'a', key2 => 'b'}, table => 'table1');
2863
-$result = $dbi->select(table => 'table1');
2864
-$row = $result->one;
2865
-is($row->{key1}, 'A');
2866
-is($row->{key2}, 'B');
2965
+ok($dbi->can('available_datatype'));
2966
+
2867 2967
 
2968
+test 'select prefix option';
2868 2969
 $dbi = DBIx::Custom->connect;
2869
-$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2870
-$dbi->insert({key1 => 'a', key2 => 'B'}, table => 'table1');
2871
-$dbi->type_rule(
2872
-    into1 => [
2873
-        [qw/date datetime/] => sub { uc $_[0] }
2874
-    ]
2970
+eval { $dbi->execute('drop table table1') };
2971
+$dbi->execute($create_table1);
2972
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2973
+$rows = $dbi->select(prefix => 'key1,', column => 'key2', table => 'table1')->all;
2974
+is_deeply($rows, [{key1 => 1, key2 => 2}], "table");
2975
+
2976
+
2977
+test 'separator';
2978
+$dbi = DBIx::Custom->connect;
2979
+is($dbi->separator, '.');
2980
+$dbi->separator('-');
2981
+is($dbi->separator, '-');
2982
+$dbi->separator('__');
2983
+is($dbi->separator, '__');
2984
+eval { $dbi->separator('?') };
2985
+like($@, qr/Separator/);
2986
+
2987
+
2988
+test 'map_param';
2989
+$dbi = DBIx::Custom->connect;
2990
+$param = $dbi->map_param(
2991
+    {id => 1, author => 'Ken', price => 1900},
2992
+    id => 'book.id',
2993
+    author => ['book.author', sub { '%' . $_[0] . '%' }],
2994
+    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
2875 2995
 );
2876
-$result = $dbi->execute(
2877
-    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
2878
-    param => {key1 => 'a', 'table1.key2' => 'b'}
2996
+is_deeply($param, {'book.id' => 1, 'book.author' => '%Ken%',
2997
+  'book.price' => 1900});
2998
+
2999
+$param = $dbi->map_param(
3000
+    {id => 0, author => 0, price => 0},
3001
+    id => 'book.id',
3002
+    author => ['book.author', sub { '%' . $_[0] . '%' }],
3003
+    price => ['book.price', sub { '%' . $_[0] . '%' },
3004
+      {if => sub { $_[0] eq 0 }}]
2879 3005
 );
2880
-$row = $result->one;
2881
-is($row->{key1}, 'a');
2882
-is($row->{key2}, 'B');
3006
+is_deeply($param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
2883 3007
 
2884
-$dbi = DBIx::Custom->connect;
2885
-$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2886
-$dbi->insert({key1 => 'A', key2 => 'B'}, table => 'table1');
2887
-$dbi->type_rule(
2888
-    into1 => [
2889
-        [qw/date datetime/] => sub { uc $_[0] }
2890
-    ]
3008
+$param = $dbi->map_param(
3009
+    {id => '', author => '', price => ''},
3010
+    id => 'book.id',
3011
+    author => ['book.author', sub { '%' . $_[0] . '%' }],
3012
+    price => ['book.price', sub { '%' . $_[0] . '%' },
3013
+      {if => sub { $_[0] eq 1 }}]
2891 3014
 );
2892
-$result = $dbi->execute(
2893
-    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
2894
-    param => {key1 => 'a', 'table1.key2' => 'b'},
2895
-    table => 'table1'
3015
+is_deeply($param, {});
3016
+
3017
+$param = $dbi->map_param(
3018
+    {id => undef, author => undef, price => undef},
3019
+    id => 'book.id',
3020
+    price => ['book.price', {if => 'exists'}]
2896 3021
 );
2897
-$row = $result->one;
2898
-is($row->{key1}, 'A');
2899
-is($row->{key2}, 'B');
3022
+is_deeply($param, {'book.price' => undef});
3023
+
3024
+$param = $dbi->map_param(
3025
+    {price => 'a'},
3026
+    id => ['book.id', {if => 'exists'}],
3027
+    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
3028
+);
3029
+is_deeply($param, {'book.price' => '%a'});
2900 3030
 
3031
+
3032
+test 'table_alias';
2901 3033
 $dbi = DBIx::Custom->connect;
2902
-$dbi->execute("create table table1 (key1 date, key2 datetime)");
2903
-$dbi->register_filter(twice => sub { $_[0] * 2 });
3034
+eval { $dbi->execute('drop table table1') };
3035
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2904 3036
 $dbi->type_rule(
2905
-    from1 => {
2906
-        date => 'twice',
2907
-    },
2908 3037
     into1 => {
2909
-        date => 'twice',
3038
+        date => sub { uc $_[0] }
2910 3039
     }
2911 3040
 );
2912
-$dbi->insert({key1 => 2}, table => 'table1');
3041
+$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => 'a'},
3042
+  table_alias => {table2 => 'table1'});
2913 3043
 $result = $dbi->select(table => 'table1');
2914
-is($result->fetch->[0], 8);
3044
+is($result->one->{key1}, 'A');
2915 3045
 
2916
-test 'type_rule and filter order';
2917
-$dbi = DBIx::Custom->connect;
2918
-$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2919
-$dbi->type_rule(
2920
-    into1 => {
2921
-        date => sub { $_[0] . 'b' }
2922
-    },
2923
-    into2 => {
2924
-        date => sub { $_[0] . 'c' }
2925
-    },
2926
-    from1 => {
2927
-        date => sub { $_[0] . 'd' }
2928
-    },
2929
-    from2 => {
2930
-        date => sub { $_[0] . 'e' }
2931
-    }
2932
-);
2933
-$dbi->insert({key1 => '1'}, table => 'table1', filter => {key1 => sub { $_[0] . 'a' }});
2934
-$result = $dbi->select(table => 'table1');
2935
-$result->filter(key1 => sub { $_[0] . 'f' });
2936
-is($result->fetch_first->[0], '1abcdef');
2937 3046
 
3047
+test 'order';
2938 3048
 $dbi = DBIx::Custom->connect;
2939
-$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2940
-$dbi->type_rule(
2941
-    from1 => {
2942
-        date => sub { $_[0] . 'p' }
2943
-    },
2944
-    from2 => {
2945
-        date => sub { $_[0] . 'q' }
2946
-    },
2947
-);
2948
-$dbi->insert({key1 => '1'}, table => 'table1');
2949
-$result = $dbi->select(table => 'table1');
2950
-$result->type_rule(
2951
-    from1 => {
2952
-        date => sub { $_[0] . 'd' }
2953
-    },
2954
-    from2 => {
2955
-        date => sub { $_[0] . 'e' }
2956
-    }
2957
-);
2958
-$result->filter(key1 => sub { $_[0] . 'f' });
2959
-is($result->fetch_first->[0], '1def');
3049
+eval { $dbi->execute('drop table table1') };
3050
+$dbi->execute("create table table1 (key1, key2)");
3051
+$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
3052
+$dbi->insert({key1 => 1, key2 => 3}, table => 'table1');
3053
+$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
3054
+$dbi->insert({key1 => 2, key2 => 4}, table => 'table1');
3055
+my $order = $dbi->order;
3056
+$order->prepend('key1', 'key2 desc');
3057
+$result = $dbi->select(table => 'table1', append => "$order");
3058
+is_deeply($result->all, [{key1 => 1, key2 => 3}, {key1 => 1, key2 => 1},
3059
+  {key1 => 2, key2 => 4}, {key1 => 2, key2 => 2}]);
3060
+$order->prepend('key1 desc');
3061
+$result = $dbi->select(table => 'table1', append => "$order");
3062
+is_deeply($result->all, [{key1 => 2, key2 => 4}, {key1 => 2, key2 => 2},
3063
+  {key1 => 1, key2 => 3}, {key1 => 1, key2 => 1}]);
2960 3064
 
2961
-test 'type_rule_off';
2962
-$dbi = DBIx::Custom->connect;
2963
-$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2964
-$dbi->type_rule(
2965
-    from1 => {
2966
-        date => sub { $_[0] * 2 },
2967
-    },
2968
-    into1 => {
2969
-        date => sub { $_[0] * 2 },
2970
-    }
2971
-);
2972
-$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
2973
-$result = $dbi->select(table => 'table1', type_rule_off => 1);
2974
-is($result->type_rule_off->fetch->[0], 2);
3065
+$order = $dbi->order;
3066
+$order->prepend(['table1-key1'], [qw/table1-key2 desc/]);
3067
+$result = $dbi->select(table => 'table1',
3068
+  column => [[key1 => 'table1-key1'], [key2 => 'table1-key2']],
3069
+  append => "$order");
3070
+is_deeply($result->all, [{'table1-key1' => 1, 'table1-key2' => 3},
3071
+  {'table1-key1' => 1, 'table1-key2' => 1},
3072
+  {'table1-key1' => 2, 'table1-key2' => 4},
3073
+  {'table1-key1' => 2, 'table1-key2' => 2}]);
2975 3074
 
3075
+test 'tag_parse';
2976 3076
 $dbi = DBIx::Custom->connect;
2977
-$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2978
-$dbi->type_rule(
2979
-    from1 => {
2980
-        date => sub { $_[0] * 2 },
2981
-    },
2982
-    into1 => {
2983
-        date => sub { $_[0] * 3 },
2984
-    }
2985
-);
2986
-$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
2987
-$result = $dbi->select(table => 'table1', type_rule_off => 1);
2988
-is($result->one->{key1}, 4);
3077
+$dbi->tag_parse(0);
3078
+eval { $dbi->execute('drop table table1') };
3079
+$dbi->execute("create table table1 (key1, key2)");
3080
+$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
3081
+eval {$dbi->execute("select * from table1 where {= key1}", {key1 => 1})};
3082
+ok($@);
2989 3083
 
3084
+test 'last_sql';
2990 3085
 $dbi = DBIx::Custom->connect;
2991
-$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2992
-$dbi->type_rule(
2993
-    from1 => {
2994
-        date => sub { $_[0] * 2 },
2995
-    },
2996
-    into1 => {
2997
-        date => sub { $_[0] * 3 },
2998
-    }
2999
-);
3000
-$dbi->insert({key1 => 2}, table => 'table1');
3001
-$result = $dbi->select(table => 'table1');
3002
-is($result->one->{key1}, 12);
3086
+eval { $dbi->execute('drop table table1') };
3087
+$dbi->execute("create table table1 (key1, key2)");
3088
+$dbi->execute('select * from table1');
3089
+is($dbi->last_sql, 'select * from table1;');
3003 3090
 
3004
-$dbi = DBIx::Custom->connect;
3005
-$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3006
-$dbi->type_rule(
3007
-    from1 => {
3008
-        date => sub { $_[0] * 2 },
3009
-    },
3010
-    into1 => {
3011
-        date => sub { $_[0] * 3 },
3012
-    }
3013
-);
3014
-$dbi->insert({key1 => 2}, table => 'table1');
3015
-$result = $dbi->select(table => 'table1');
3016
-is($result->fetch->[0], 12);
3091
+eval{$dbi->execute("aaa")};
3092
+is($dbi->last_sql, 'aaa;');
3017 3093
 
3094
+test 'DBIx::Custom header';
3018 3095
 $dbi = DBIx::Custom->connect;
3019
-$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3020
-$dbi->register_filter(ppp => sub { uc $_[0] });
3021
-$dbi->type_rule(
3022
-    into1 => {
3023
-        date => 'ppp'
3024
-    }
3025
-);
3026
-$dbi->insert({key1 => 'a'}, table => 'table1');
3027
-$result = $dbi->select(table => 'table1');
3028
-is($result->one->{key1}, 'A');
3096
+eval { $dbi->execute('drop table table1') };
3097
+$dbi->execute("create table table1 (key1, key2)");
3098
+$result = $dbi->execute('select key1 as h1, key2 as h2 from table1');
3099
+is_deeply($result->header, [qw/h1 h2/]);
3029 3100
 
3030
-eval{$dbi->type_rule(
3031
-    into1 => {
3032
-        date => 'pp'
3033
-    }
3034
-)};
3035
-like($@, qr/not registered/);
3101
+test 'Named placeholder :name(operater) syntax';
3102
+$dbi->execute('drop table table1');
3103
+$dbi->execute($create_table1_2);
3104
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
3105
+$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
3036 3106
 
3037
-$dbi = DBIx::Custom->connect;
3038
-$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3039
-eval {
3040
-    $dbi->type_rule(
3041
-        from1 => {
3042
-            Date => sub { $_[0] * 2 },
3043
-        }
3044
-    );
3045
-};
3046
-like($@, qr/lower/);
3107
+$source = "select * from table1 where :key1{=} and :key2{=}";
3108
+$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
3109
+$rows = $result->all;
3110
+is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3047 3111
 
3048
-eval {
3049
-    $dbi->type_rule(
3050
-        into1 => {
3051
-            Date => sub { $_[0] * 2 },
3052
-        }
3053
-    );
3054
-};
3055
-like($@, qr/lower/);
3112
+$source = "select * from table1 where :key1{ = } and :key2{=}";
3113
+$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
3114
+$rows = $result->all;
3115
+is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3056 3116
 
3057
-$dbi = DBIx::Custom->connect;
3058
-$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3059
-$dbi->type_rule(
3060
-    from1 => {
3061
-        date => sub { $_[0] * 2 },
3062
-    },
3063
-    into1 => {
3064
-        date => sub { $_[0] * 3 },
3065
-    }
3066
-);
3067
-$dbi->insert({key1 => 2}, table => 'table1');
3068
-$result = $dbi->select(table => 'table1');
3069
-$result->type_rule_off;
3070
-is($result->one->{key1}, 6);
3117
+$source = "select * from table1 where :key1{<} and :key2{=}";
3118
+$result = $dbi->execute($source, param => {key1 => 5, key2 => 2});
3119
+$rows = $result->all;
3120
+is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3071 3121
 
3072
-$dbi = DBIx::Custom->connect;
3073
-$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3074
-$dbi->type_rule(
3075
-    from1 => {
3076
-        date => sub { $_[0] * 2 },
3077
-        datetime => sub { $_[0] * 4 },
3078
-    },
3079
-);
3080
-$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
3081
-$result = $dbi->select(table => 'table1');
3082
-$result->type_rule(
3083
-    from1 => {
3084
-        date => sub { $_[0] * 3 }
3085
-    }
3122
+$source = "select * from table1 where :table1.key1{=} and :table1.key2{=}";
3123
+$result = $dbi->execute(
3124
+    $source,
3125
+    param => {'table1.key1' => 1, 'table1.key2' => 1},
3126
+    filter => {'table1.key2' => sub { $_[0] * 2 }}
3086 3127
 );
3087
-$row = $result->one;
3088
-is($row->{key1}, 6);
3089
-is($row->{key2}, 2);
3128
+$rows = $result->all;
3129
+is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3090 3130
 
3091
-$result = $dbi->select(table => 'table1');
3092
-$result->type_rule(
3093
-    from1 => {
3094
-        date => sub { $_[0] * 3 }
3131
+test 'high perfomance way';
3132
+$dbi->execute('drop table table1');
3133
+$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
3134
+$rows = [
3135
+    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3136
+    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3137
+];
3138
+{
3139
+    my $query;
3140
+    foreach my $row (@$rows) {
3141
+      $query ||= $dbi->insert($row, table => 'table1', query => 1);
3142
+      $dbi->execute($query, $row, filter => {ab => sub { $_[0] * 2 }});
3095 3143
     }
3096
-);
3097
-$row = $result->one;
3098
-is($row->{key1}, 6);
3099
-is($row->{key2}, 2);
3100
-
3101
-$result = $dbi->select(table => 'table1');
3102
-$result->type_rule(
3103
-    from1 => {
3104
-        date => sub { $_[0] * 3 }
3144
+    is_deeply($dbi->select(table => 'table1')->all,
3145
+      [
3146
+          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3147
+          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3148
+      ]
3149
+    );
3150
+}
3151
+
3152
+$dbi->execute('drop table table1');
3153
+$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
3154
+$rows = [
3155
+    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3156
+    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3157
+];
3158
+{
3159
+    my $query;
3160
+    my $sth;
3161
+    foreach my $row (@$rows) {
3162
+      $query ||= $dbi->insert($row, table => 'table1', query => 1);
3163
+      $sth ||= $query->sth;
3164
+      $sth->execute(map { $row->{$_} } sort keys %$row);
3105 3165
     }
3106
-);
3107
-$row = $result->one;
3108
-is($row->{key1}, 6);
3109
-is($row->{key2}, 2);
3110
-$result = $dbi->select(table => 'table1');
3111
-$result->type_rule(
3112
-    from1 => [date => sub { $_[0] * 3 }]
3113
-);
3114
-$row = $result->one;
3115
-is($row->{key1}, 6);
3116
-is($row->{key2}, 2);
3117
-$dbi->register_filter(fivetimes => sub { $_[0] * 5});
3118
-$result = $dbi->select(table => 'table1');
3119
-$result->type_rule(
3120
-    from1 => [date => 'fivetimes']
3121
-);
3122
-$row = $result->one;
3123
-is($row->{key1}, 10);
3124
-is($row->{key2}, 2);
3125
-$result = $dbi->select(table => 'table1');
3126
-$result->type_rule(
3127
-    from1 => [date => undef]
3128
-);
3129
-$row = $result->one;
3130
-is($row->{key1}, 2);
3131
-is($row->{key2}, 2);
3166
+    is_deeply($dbi->select(table => 'table1')->all,
3167
+      [
3168
+          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3169
+          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3170
+      ]
3171
+    );
3172
+}
3132 3173
 
3174
+test 'result';
3133 3175
 $dbi = DBIx::Custom->connect;
3134
-$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3135
-$dbi->type_rule(
3136
-    from1 => {
3137
-        date => sub { $_[0] * 2 },
3138
-    },
3139
-);
3140
-$dbi->insert({key1 => 2}, table => 'table1');
3176
+eval { $dbi->execute('drop table table1') };
3177
+$dbi->execute($create_table1);
3178
+$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3179
+$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3180
+
3141 3181
 $result = $dbi->select(table => 'table1');
3142
-$result->filter(key1 => sub { $_[0] * 3 });
3143
-is($result->one->{key1}, 12);
3182
+@rows = ();
3183
+while (my $row = $result->fetch) {
3184
+    push @rows, [@$row];
3185
+}
3186
+is_deeply(\@rows, [[1, 2], [3, 4]]);
3144 3187
 
3145
-$dbi = DBIx::Custom->connect;
3146
-$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3147
-$dbi->type_rule(
3148
-    from1 => {
3149
-        date => sub { $_[0] * 2 },
3150
-    },
3151
-);
3152
-$dbi->insert({key1 => 2}, table => 'table1');
3153 3188
 $result = $dbi->select(table => 'table1');
3154
-$result->filter(key1 => sub { $_[0] * 3 });
3155
-is($result->fetch->[0], 12);
3189
+@rows = ();
3190
+while (my $row = $result->fetch_hash) {
3191
+    push @rows, {%$row};
3192
+}
3193
+is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
3156 3194
 
3157
-$dbi = DBIx::Custom->connect;
3158
-$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3159
-$dbi->type_rule(
3160
-    into1 => {
3161
-        date => sub { $_[0] . 'b' }
3162
-    },
3163
-    into2 => {
3164
-        date => sub { $_[0] . 'c' }
3165
-    },
3166
-    from1 => {
3167
-        date => sub { $_[0] . 'd' }
3168
-    },
3169
-    from2 => {
3170
-        date => sub { $_[0] . 'e' }
3171
-    }
3172
-);
3173
-$dbi->insert({key1 => '1'}, table => 'table1', type_rule_off => 1);
3174 3195
 $result = $dbi->select(table => 'table1');
3175
-is($result->type_rule_off->fetch_first->[0], '1');
3196
+$row = $result->fetch_first;
3197
+is_deeply($row, [1, 2], "row");
3198
+$row = $result->fetch;
3199
+ok(!$row, "finished");
3200
+
3176 3201
 $result = $dbi->select(table => 'table1');
3177
-is($result->type_rule_on->fetch_first->[0], '1de');
3202
+$row = $result->fetch_hash_first;
3203
+is_deeply($row, {key1 => 1, key2 => 2}, "row");
3204
+$row = $result->fetch_hash;
3205
+ok(!$row, "finished");
3206
+
3207
+$dbi->execute('create table table2 (key1, key2);');
3208
+$result = $dbi->select(table => 'table2');
3209
+$row = $result->fetch_hash_first;
3210
+ok(!$row, "no row fetch");
3178 3211
 
3179 3212
 $dbi = DBIx::Custom->connect;
3180
-$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3181
-$dbi->type_rule(
3182
-    into1 => {
3183
-        date => sub { $_[0] . 'b' }
3184
-    },
3185
-    into2 => {
3186
-        date => sub { $_[0] . 'c' }
3187
-    },
3188
-    from1 => {
3189
-        date => sub { $_[0] . 'd' }
3190
-    },
3191
-    from2 => {
3192
-        date => sub { $_[0] . 'e' }
3193
-    }
3194
-);
3195
-$dbi->insert({key1 => '1'}, table => 'table1', type_rule1_off => 1);
3213
+eval { $dbi->execute('drop table table1') };
3214
+$dbi->execute($create_table1);
3215
+$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3216
+$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3217
+$dbi->insert({key1 => 5, key2 => 6}, table => 'table1');
3218
+$dbi->insert({key1 => 7, key2 => 8}, table => 'table1');
3219
+$dbi->insert({key1 => 9, key2 => 10}, table => 'table1');
3196 3220
 $result = $dbi->select(table => 'table1');
3197
-is($result->type_rule1_off->fetch_first->[0], '1ce');
3221
+$rows = $result->fetch_multi(2);
3222
+is_deeply($rows, [[1, 2],
3223
+                  [3, 4]], "fetch_multi first");
3224
+$rows = $result->fetch_multi(2);
3225
+is_deeply($rows, [[5, 6],
3226
+                  [7, 8]], "fetch_multi secound");
3227
+$rows = $result->fetch_multi(2);
3228
+is_deeply($rows, [[9, 10]], "fetch_multi third");
3229
+$rows = $result->fetch_multi(2);
3230
+ok(!$rows);
3231
+
3198 3232
 $result = $dbi->select(table => 'table1');
3199
-is($result->type_rule1_on->fetch_first->[0], '1cde');
3233
+eval {$result->fetch_multi};
3234
+like($@, qr/Row count must be specified/, "Not specified row count");
3200 3235
 
3201
-$dbi = DBIx::Custom->connect;
3202
-$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3203
-$dbi->type_rule(
3204
-    into1 => {
3205
-        date => sub { $_[0] . 'b' }
3206
-    },
3207
-    into2 => {
3208
-        date => sub { $_[0] . 'c' }
3209
-    },
3210
-    from1 => {
3211
-        date => sub { $_[0] . 'd' }
3212
-    },
3213
-    from2 => {
3214
-        date => sub { $_[0] . 'e' }
3215
-    }
3216
-);
3217
-$dbi->insert({key1 => '1'}, table => 'table1', type_rule2_off => 1);
3218 3236
 $result = $dbi->select(table => 'table1');
3219
-is($result->type_rule2_off->fetch_first->[0], '1bd');
3237
+$rows = $result->fetch_hash_multi(2);
3238
+is_deeply($rows, [{key1 => 1, key2 => 2},
3239
+                  {key1 => 3, key2 => 4}], "fetch_multi first");
3240
+$rows = $result->fetch_hash_multi(2);
3241
+is_deeply($rows, [{key1 => 5, key2 => 6},
3242
+                  {key1 => 7, key2 => 8}], "fetch_multi secound");
3243
+$rows = $result->fetch_hash_multi(2);
3244
+is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third");
3245
+$rows = $result->fetch_hash_multi(2);
3246
+ok(!$rows);
3247
+
3220 3248
 $result = $dbi->select(table => 'table1');
3221
-is($result->type_rule2_on->fetch_first->[0], '1bde');
3249
+eval {$result->fetch_hash_multi};
3250
+like($@, qr/Row count must be specified/, "Not specified row count");
3222 3251
 
3223
-test 'separator';
3224 3252
 $dbi = DBIx::Custom->connect;
3253
+eval { $dbi->execute('drop table table1') };
3225 3254
 $dbi->execute($create_table1);
3226
-$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
3227
-
3228
-$dbi->create_model(
3229
-    table => 'table1',
3230
-    join => [
3231
-       'left outer join table2 on table1.key1 = table2.key1'
3232
-    ],
3233
-    primary_key => ['key1'],
3234
-);
3235
-$model2 = $dbi->create_model(
3236
-    table => 'table2',
3237
-);
3238
-$dbi->setup_model;
3239
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3240
-$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
3241
-$model = $dbi->model('table1');
3242
-$result = $model->select(
3243
-    column => [
3244
-        $model->mycolumn,
3245
-        {table2 => [qw/key1 key3/]}
3246
-    ],
3247
-    where => {'table1.key1' => 1}
3248
-);
3249
-is_deeply($result->one,
3250
-          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
3251
-is_deeply($model2->select->one, {key1 => 1, key3 => 3});
3255
+$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3256
+$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3252 3257
 
3253
-$dbi->separator('__');
3254
-$model = $dbi->model('table1');
3255
-$result = $model->select(
3256
-    column => [
3257
-        $model->mycolumn,
3258
-        {table2 => [qw/key1 key3/]}
3259
-    ],
3260
-    where => {'table1.key1' => 1}
3261
-);
3262
-is_deeply($result->one,
3263
-          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
3264
-is_deeply($model2->select->one, {key1 => 1, key3 => 3});
3258
+test 'fetch_all';
3259
+$result = $dbi->select(table => 'table1');
3260
+$rows = $result->fetch_all;
3261
+is_deeply($rows, [[1, 2], [3, 4]]);
3265 3262
 
3266
-$dbi->separator('-');
3267
-$model = $dbi->model('table1');
3268
-$result = $model->select(
3269
-    column => [
3270
-        $model->mycolumn,
3271
-        {table2 => [qw/key1 key3/]}
3272
-    ],
3273
-    where => {'table1.key1' => 1}
3274
-);
3275
-is_deeply($result->one,
3276
-          {key1 => 1, key2 => 2, 'table2-key1' => 1, 'table2-key3' => 3});
3277
-is_deeply($model2->select->one, {key1 => 1, key3 => 3});
3263
+$result = $dbi->select(table => 'table1');
3264
+$rows = $result->fetch_hash_all;
3265
+is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
3278 3266
 
3267
+$result = $dbi->select(table => 'table1');
3268
+$result->dbi->filters({three_times => sub { $_[0] * 3}});
3269
+$result->filter({key1 => 'three_times'});
3279 3270
 
3280
-test 'filter_off';
3281
-$dbi = DBIx::Custom->connect;
3282
-$dbi->execute($create_table1);
3283
-$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
3271
+$rows = $result->fetch_all;
3272
+is_deeply($rows, [[3, 2], [9, 4]], "array");
3284 3273
 
3285
-$dbi->create_model(
3286
-    table => 'table1',
3287
-    join => [
3288
-       'left outer join table2 on table1.key1 = table2.key1'
3289
-    ],
3290
-    primary_key => ['key1'],
3274
+$result = $dbi->select(table => 'table1');
3275
+$result->dbi->filters({three_times => sub { $_[0] * 3}});
3276
+$result->filter({key1 => 'three_times'});
3277
+$rows = $result->fetch_hash_all;
3278
+is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "hash");
3279
+
3280
+test "query_builder";
3281
+$datas = [
3282
+    # Basic tests
3283
+    {   name            => 'placeholder basic',
3284
+        source            => "a {?  k1} b {=  k2} {<> k3} {>  k4} {<  k5} {>= k6} {<= k7} {like k8}", ,
3285
+        sql_expected    => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?;",
3286
+        columns_expected   => [qw/k1 k2 k3 k4 k5 k6 k7 k8/]
3287
+    },
3288
+    {
3289
+        name            => 'placeholder in',
3290
+        source            => "{in k1 3};",
3291
+        sql_expected    => "k1 in (?, ?, ?);",
3292
+        columns_expected   => [qw/k1 k1 k1/]
3293
+    },
3294
+    
3295
+    # Table name
3296
+    {
3297
+        name            => 'placeholder with table name',
3298
+        source            => "{= a.k1} {= a.k2}",
3299
+        sql_expected    => "a.k1 = ? a.k2 = ?;",
3300
+        columns_expected  => [qw/a.k1 a.k2/]
3301
+    },
3302
+    {   
3303
+        name            => 'placeholder in with table name',
3304
+        source            => "{in a.k1 2} {in b.k2 2}",
3305
+        sql_expected    => "a.k1 in (?, ?) b.k2 in (?, ?);",
3306
+        columns_expected  => [qw/a.k1 a.k1 b.k2 b.k2/]
3307
+    },
3308
+    {
3309
+        name            => 'not contain tag',
3310
+        source            => "aaa",
3311
+        sql_expected    => "aaa;",
3312
+        columns_expected  => [],
3313
+    }
3314
+];
3315
+
3316
+for (my $i = 0; $i < @$datas; $i++) {
3317
+    my $data = $datas->[$i];
3318
+    my $builder = DBIx::Custom->new->query_builder;
3319
+    my $query = $builder->build_query($data->{source});
3320
+    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
3321
+    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
3322
+}
3323
+
3324
+$builder = DBIx::Custom->new->query_builder;
3325
+$ret_val = $builder->register_tag(
3326
+    p => sub {
3327
+        my @args = @_;
3328
+        
3329
+        my $expand    = "? $args[0] $args[1]";
3330
+        my $columns = [2];
3331
+        return [$expand, $columns];
3332
+    }
3291 3333
 );
3292
-$dbi->setup_model;
3293
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3294
-$model = $dbi->model('table1');
3295
-$result = $model->select(column => 'key1');
3296
-$result->filter(key1 => sub { $_[0] * 2 });
3297
-is_deeply($result->one, {key1 => 2});
3298 3334
 
3299
-test 'available_datetype';
3300
-$dbi = DBIx::Custom->connect;
3301
-ok($dbi->can('available_datatype'));
3335
+$query = $builder->build_query("{p a b}");
3336
+is($query->{sql}, "? a b;", "register_tag sql");
3337
+is_deeply($query->{columns}, [2], "register_tag columns");
3338
+isa_ok($ret_val, 'DBIx::Custom::QueryBuilder');
3302 3339
 
3340
+$builder = DBIx::Custom->new->query_builder;
3303 3341
 
3304
-test 'select prefix option';
3305
-$dbi = DBIx::Custom->connect;
3306
-$dbi->execute($create_table1);
3307
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3308
-$rows = $dbi->select(prefix => 'key1,', column => 'key2', table => 'table1')->all;
3309
-is_deeply($rows, [{key1 => 1, key2 => 2}], "table");
3342
+eval{$builder->build_query('{? }')};
3343
+like($@, qr/\QColumn name must be specified in tag "{? }"/, "? not arguments");
3310 3344
 
3345
+eval{$builder->build_query("{a }")};
3346
+like($@, qr/\QTag "a" is not registered/, "tag not exist");
3311 3347
 
3312
-test 'separator';
3313
-$dbi = DBIx::Custom->connect;
3314
-is($dbi->separator, '.');
3315
-$dbi->separator('-');
3316
-is($dbi->separator, '-');
3317
-$dbi->separator('__');
3318
-is($dbi->separator, '__');
3319
-eval { $dbi->separator('?') };
3320
-like($@, qr/Separator/);
3348
+$builder->register_tag({
3349
+    q => 'string'
3350
+});
3321 3351
 
3352
+eval{$builder->build_query("{q}", {})};
3353
+like($@, qr/Tag "q" must be sub reference/, "tag not code ref");
3322 3354
 
3323
-test 'map_param';
3324
-$dbi = DBIx::Custom->connect;
3325
-$param = $dbi->map_param(
3326
-    {id => 1, author => 'Ken', price => 1900},
3327
-    id => 'book.id',
3328
-    author => ['book.author', sub { '%' . $_[0] . '%' }],
3329
-    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
3330
-);
3331
-is_deeply($param, {'book.id' => 1, 'book.author' => '%Ken%',
3332
-  'book.price' => 1900});
3355
+$builder->register_tag({
3356
+   r => sub {} 
3357
+});
3333 3358
 
3334
-$param = $dbi->map_param(
3335
-    {id => 0, author => 0, price => 0},
3336
-    id => 'book.id',
3337
-    author => ['book.author', sub { '%' . $_[0] . '%' }],
3338
-    price => ['book.price', sub { '%' . $_[0] . '%' },
3339
-      {if => sub { $_[0] eq 0 }}]
3359
+eval{$builder->build_query("{r}")};
3360
+like($@, qr/\QTag "r" must return [STRING, ARRAY_REFERENCE]/, "tag return noting");
3361
+
3362
+$builder->register_tag({
3363
+   s => sub { return ["a", ""]} 
3364
+});
3365
+
3366
+eval{$builder->build_query("{s}")};
3367
+like($@, qr/\QTag "s" must return [STRING, ARRAY_REFERENCE]/, "tag return not array columns");
3368
+
3369
+$builder->register_tag(
3370
+    t => sub {return ["a", []]}
3340 3371
 );
3341
-is_deeply($param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
3342 3372
 
3343
-$param = $dbi->map_param(
3344
-    {id => '', author => '', price => ''},
3345
-    id => 'book.id',
3346
-    author => ['book.author', sub { '%' . $_[0] . '%' }],
3347
-    price => ['book.price', sub { '%' . $_[0] . '%' },
3348
-      {if => sub { $_[0] eq 1 }}]
3373
+
3374
+test 'General error case';
3375
+$builder = DBIx::Custom->new->query_builder;
3376
+$builder->register_tag(
3377
+    a => sub {
3378
+        return ["? ? ?", ['']];
3379
+    }
3349 3380
 );
3350
-is_deeply($param, {});
3381
+eval{$builder->build_query("{a}")};
3382
+like($@, qr/\QPlaceholder count/, "placeholder count is invalid");
3351 3383
 
3352
-$param = $dbi->map_param(
3353
-    {id => undef, author => undef, price => undef},
3354
-    id => 'book.id',
3355
-    price => ['book.price', {if => 'exists'}]
3384
+
3385
+test 'Default tag Error case';
3386
+eval{$builder->build_query("{= }")};
3387
+like($@, qr/Column name must be specified in tag "{= }"/, "basic '=' : key not exist");
3388
+
3389
+eval{$builder->build_query("{in }")};
3390
+like($@, qr/Column name and count of values must be specified in tag "{in }"/, "in : key not exist");
3391
+
3392
+eval{$builder->build_query("{in a}")};
3393
+like($@, qr/\QColumn name and count of values must be specified in tag "{in }"/,
3394
+     "in : key not exist");
3395
+
3396
+eval{$builder->build_query("{in a r}")};
3397
+like($@, qr/\QColumn name and count of values must be specified in tag "{in }"/,
3398
+     "in : key not exist");
3399
+
3400
+test 'variouse source';
3401
+$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d;";
3402
+$query = $builder->build_query($source);
3403
+is($query->sql, 'a b = ? c { } { = ? } = ? d;', "basic : 1");
3404
+
3405
+$source = "abc;";
3406
+$query = $builder->build_query($source);
3407
+is($query->sql, 'abc;', "basic : 2");
3408
+
3409
+$source = "{= a}";
3410
+$query = $builder->build_query($source);
3411
+is($query->sql, 'a = ?;', "only tag");
3412
+
3413
+$source = "000;";
3414
+$query = $builder->build_query($source);
3415
+is($query->sql, '000;', "contain 0 value");
3416
+
3417
+$source = "a {= b} }";
3418
+eval{$builder->build_query($source)};
3419
+like($@, qr/unexpected "}"/, "error : 1");
3420
+
3421
+$source = "a {= {}";
3422
+eval{$builder->build_query($source)};
3423
+like($@, qr/unexpected "{"/, "error : 2");
3424
+
3425
+### SQLite test
3426
+test 'type option'; # DEPRECATED!
3427
+$dbi = DBIx::Custom->connect(
3428
+    data_source => 'dbi:SQLite:dbname=:memory:',
3429
+    dbi_option => {
3430
+        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
3431
+    }
3356 3432
 );
3357
-is_deeply($param, {'book.price' => undef});
3433
+$binary = pack("I3", 1, 2, 3);
3434
+eval { $dbi->execute('drop table table1') };
3435
+$dbi->execute('create table table1(key1, key2)');
3436
+$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]);
3437
+$result = $dbi->select(table => 'table1');
3438
+$row   = $result->one;
3439
+is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
3440
+$result = $dbi->execute('select length(key1) as key1_length from table1');
3441
+$row = $result->one;
3442
+is($row->{key1_length}, length $binary);
3358 3443
 
3359
-$param = $dbi->map_param(
3360
-    {price => 'a'},
3361
-    id => ['book.id', {if => 'exists'}],
3362
-    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
3444
+test 'type_rule from';
3445
+$dbi = DBIx::Custom->connect;
3446
+$dbi->type_rule(
3447
+    from1 => {
3448
+        date => sub { uc $_[0] }
3449
+    }
3363 3450
 );
3364
-is_deeply($param, {'book.price' => '%a'});
3451
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3452
+$dbi->insert({key1 => 'a'}, table => 'table1');
3453
+$result = $dbi->select(table => 'table1');
3454
+is($result->fetch_first->[0], 'A');
3365 3455
 
3456
+$result = $dbi->select(table => 'table1');
3457
+is($result->one->{key1}, 'A');
3366 3458
 
3367
-test 'table_alias';
3459
+
3460
+test 'type_rule into';
3368 3461
 $dbi = DBIx::Custom->connect;
3369 3462
 $dbi->execute("create table table1 (key1 Date, key2 datetime)");
3370 3463
 $dbi->type_rule(
... ...
@@ -3372,380 +3465,374 @@ $dbi->type_rule(
3372 3465
         date => sub { uc $_[0] }
3373 3466
     }
3374 3467
 );
3375
-$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => 'a'},
3376
-  table_alias => {table2 => 'table1'});
3468
+$dbi->insert({key1 => 'a'}, table => 'table1');
3377 3469
 $result = $dbi->select(table => 'table1');
3378 3470
 is($result->one->{key1}, 'A');
3379 3471
 
3380
-
3381
-test 'order';
3382 3472
 $dbi = DBIx::Custom->connect;
3383
-$dbi->execute("create table table1 (key1, key2)");
3384
-$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
3385
-$dbi->insert({key1 => 1, key2 => 3}, table => 'table1');
3386
-$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
3387
-$dbi->insert({key1 => 2, key2 => 4}, table => 'table1');
3388
-my $order = $dbi->order;
3389
-$order->prepend('key1', 'key2 desc');
3390
-$result = $dbi->select(table => 'table1', append => "$order");
3391
-is_deeply($result->all, [{key1 => 1, key2 => 3}, {key1 => 1, key2 => 1},
3392
-  {key1 => 2, key2 => 4}, {key1 => 2, key2 => 2}]);
3393
-$order->prepend('key1 desc');
3394
-$result = $dbi->select(table => 'table1', append => "$order");
3395
-is_deeply($result->all, [{key1 => 2, key2 => 4}, {key1 => 2, key2 => 2},
3396
-  {key1 => 1, key2 => 3}, {key1 => 1, key2 => 1}]);
3397
-
3398
-$order = $dbi->order;
3399
-$order->prepend(['table1-key1'], [qw/table1-key2 desc/]);
3400
-$result = $dbi->select(table => 'table1',
3401
-  column => [[key1 => 'table1-key1'], [key2 => 'table1-key2']],
3402
-  append => "$order");
3403
-is_deeply($result->all, [{'table1-key1' => 1, 'table1-key2' => 3},
3404
-  {'table1-key1' => 1, 'table1-key2' => 1},
3405
-  {'table1-key1' => 2, 'table1-key2' => 4},
3406
-  {'table1-key1' => 2, 'table1-key2' => 2}]);
3473
+$dbi->execute("create table table1 (key1 date, key2 datetime)");
3474
+$dbi->type_rule(
3475
+    into1 => [
3476
+         [qw/date datetime/] => sub { uc $_[0] }
3477
+    ]
3478
+);
3479
+$dbi->insert({key1 => 'a', key2 => 'b'}, table => 'table1');
3480
+$result = $dbi->select(table => 'table1');
3481
+$row = $result->one;
3482
+is($row->{key1}, 'A');
3483
+is($row->{key2}, 'B');
3407 3484
 
3408
-test 'tag_parse';
3409 3485
 $dbi = DBIx::Custom->connect;
3410
-$dbi->tag_parse(0);
3411
-$dbi->execute("create table table1 (key1, key2)");
3412
-$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
3413
-eval {$dbi->execute("select * from table1 where {= key1}", {key1 => 1})};
3414
-ok($@);
3486
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3487
+$dbi->insert({key1 => 'a', key2 => 'B'}, table => 'table1');
3488
+$dbi->type_rule(
3489
+    into1 => [
3490
+        [qw/date datetime/] => sub { uc $_[0] }
3491
+    ]
3492
+);
3493
+$result = $dbi->execute(
3494
+    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
3495
+    param => {key1 => 'a', 'table1.key2' => 'b'}
3496
+);
3497
+$row = $result->one;
3498
+is($row->{key1}, 'a');
3499
+is($row->{key2}, 'B');
3415 3500
 
3416
-test 'last_sql';
3417 3501
 $dbi = DBIx::Custom->connect;
3418
-$dbi->execute("create table table1 (key1, key2)");
3419
-$dbi->execute('select * from table1');
3420
-is($dbi->last_sql, 'select * from table1;');
3502
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3503
+$dbi->insert({key1 => 'A', key2 => 'B'}, table => 'table1');
3504
+$dbi->type_rule(
3505
+    into1 => [
3506
+        [qw/date datetime/] => sub { uc $_[0] }
3507
+    ]
3508
+);
3509
+$result = $dbi->execute(
3510
+    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
3511
+    param => {key1 => 'a', 'table1.key2' => 'b'},
3512
+    table => 'table1'
3513
+);
3514
+$row = $result->one;
3515
+is($row->{key1}, 'A');
3516
+is($row->{key2}, 'B');
3421 3517
 
3422
-eval{$dbi->execute("aaa")};
3423
-is($dbi->last_sql, 'aaa;');
3518
+$dbi = DBIx::Custom->connect;
3519
+$dbi->execute("create table table1 (key1 date, key2 datetime)");
3520
+$dbi->register_filter(twice => sub { $_[0] * 2 });
3521
+$dbi->type_rule(
3522
+    from1 => {
3523
+        date => 'twice',
3524
+    },
3525
+    into1 => {
3526
+        date => 'twice',
3527
+    }
3528
+);
3529
+$dbi->insert({key1 => 2}, table => 'table1');
3530
+$result = $dbi->select(table => 'table1');
3531
+is($result->fetch->[0], 8);
3424 3532
 
3425
-test 'DBIx::Custom header';
3533
+test 'type_rule and filter order';
3426 3534
 $dbi = DBIx::Custom->connect;
3427
-$dbi->execute("create table table1 (key1, key2)");
3428
-$result = $dbi->execute('select key1 as h1, key2 as h2 from table1');
3429
-is_deeply($result->header, [qw/h1 h2/]);
3535
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3536
+$dbi->type_rule(
3537
+    into1 => {
3538
+        date => sub { $_[0] . 'b' }
3539
+    },
3540
+    into2 => {
3541
+        date => sub { $_[0] . 'c' }
3542
+    },
3543
+    from1 => {
3544
+        date => sub { $_[0] . 'd' }
3545
+    },
3546
+    from2 => {
3547
+        date => sub { $_[0] . 'e' }
3548
+    }
3549
+);
3550
+$dbi->insert({key1 => '1'}, table => 'table1', filter => {key1 => sub { $_[0] . 'a' }});
3551
+$result = $dbi->select(table => 'table1');
3552
+$result->filter(key1 => sub { $_[0] . 'f' });
3553
+is($result->fetch_first->[0], '1abcdef');
3430 3554
 
3431
-test 'Named placeholder :name(operater) syntax';
3432
-$dbi->execute('drop table table1');
3433
-$dbi->execute($create_table1_2);
3434
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
3435
-$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
3555
+$dbi = DBIx::Custom->connect;
3556
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3557
+$dbi->type_rule(
3558
+    from1 => {
3559
+        date => sub { $_[0] . 'p' }
3560
+    },
3561
+    from2 => {
3562
+        date => sub { $_[0] . 'q' }
3563
+    },
3564
+);
3565
+$dbi->insert({key1 => '1'}, table => 'table1');
3566
+$result = $dbi->select(table => 'table1');
3567
+$result->type_rule(
3568
+    from1 => {
3569
+        date => sub { $_[0] . 'd' }
3570
+    },
3571
+    from2 => {
3572
+        date => sub { $_[0] . 'e' }
3573
+    }
3574
+);
3575
+$result->filter(key1 => sub { $_[0] . 'f' });
3576
+is($result->fetch_first->[0], '1def');
3436 3577
 
3437
-$source = "select * from table1 where :key1{=} and :key2{=}";
3438
-$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
3439
-$rows = $result->all;
3440
-is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3578
+test 'type_rule_off';
3579
+$dbi = DBIx::Custom->connect;
3580
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3581
+$dbi->type_rule(
3582
+    from1 => {
3583
+        date => sub { $_[0] * 2 },
3584
+    },
3585
+    into1 => {
3586
+        date => sub { $_[0] * 2 },
3587
+    }
3588
+);
3589
+$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
3590
+$result = $dbi->select(table => 'table1', type_rule_off => 1);
3591
+is($result->type_rule_off->fetch->[0], 2);
3441 3592
 
3442
-$source = "select * from table1 where :key1{ = } and :key2{=}";
3443
-$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
3444
-$rows = $result->all;
3445
-is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3593
+$dbi = DBIx::Custom->connect;
3594
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3595
+$dbi->type_rule(
3596
+    from1 => {
3597
+        date => sub { $_[0] * 2 },
3598
+    },
3599
+    into1 => {
3600
+        date => sub { $_[0] * 3 },
3601
+    }
3602
+);
3603
+$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
3604
+$result = $dbi->select(table => 'table1', type_rule_off => 1);
3605
+is($result->one->{key1}, 4);
3446 3606
 
3447
-$source = "select * from table1 where :key1{<} and :key2{=}";
3448
-$result = $dbi->execute($source, param => {key1 => 5, key2 => 2});
3449
-$rows = $result->all;
3450
-is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3607
+$dbi = DBIx::Custom->connect;
3608
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3609
+$dbi->type_rule(
3610
+    from1 => {
3611
+        date => sub { $_[0] * 2 },
3612
+    },
3613
+    into1 => {
3614
+        date => sub { $_[0] * 3 },
3615
+    }
3616
+);
3617
+$dbi->insert({key1 => 2}, table => 'table1');
3618
+$result = $dbi->select(table => 'table1');
3619
+is($result->one->{key1}, 12);
3451 3620
 
3452
-$source = "select * from table1 where :table1.key1{=} and :table1.key2{=}";
3453
-$result = $dbi->execute(
3454
-    $source,
3455
-    param => {'table1.key1' => 1, 'table1.key2' => 1},
3456
-    filter => {'table1.key2' => sub { $_[0] * 2 }}
3621
+$dbi = DBIx::Custom->connect;
3622
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3623
+$dbi->type_rule(
3624
+    from1 => {
3625
+        date => sub { $_[0] * 2 },
3626
+    },
3627
+    into1 => {
3628
+        date => sub { $_[0] * 3 },
3629
+    }
3457 3630
 );
3458
-$rows = $result->all;
3459
-is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3631
+$dbi->insert({key1 => 2}, table => 'table1');
3632
+$result = $dbi->select(table => 'table1');
3633
+is($result->fetch->[0], 12);
3460 3634
 
3461
-test 'high perfomance way';
3462
-$dbi->execute('drop table table1');
3463
-$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
3464
-$rows = [
3465
-    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3466
-    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3467
-];
3468
-{
3469
-    my $query;
3470
-    foreach my $row (@$rows) {
3471
-      $query ||= $dbi->insert($row, table => 'table1', query => 1);
3472
-      $dbi->execute($query, $row, filter => {ab => sub { $_[0] * 2 }});
3635
+$dbi = DBIx::Custom->connect;
3636
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3637
+$dbi->register_filter(ppp => sub { uc $_[0] });
3638
+$dbi->type_rule(
3639
+    into1 => {
3640
+        date => 'ppp'
3473 3641
     }
3474
-    is_deeply($dbi->select(table => 'table1')->all,
3475
-      [
3476
-          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3477
-          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3478
-      ]
3479
-    );
3480
-}
3642
+);
3643
+$dbi->insert({key1 => 'a'}, table => 'table1');
3644
+$result = $dbi->select(table => 'table1');
3645
+is($result->one->{key1}, 'A');
3481 3646
 
3482
-$dbi->execute('drop table table1');
3483
-$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
3484
-$rows = [
3485
-    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3486
-    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3487
-];
3488
-{
3489
-    my $query;
3490
-    my $sth;
3491
-    foreach my $row (@$rows) {
3492
-      $query ||= $dbi->insert($row, table => 'table1', query => 1);
3493
-      $sth ||= $query->sth;
3494
-      $sth->execute(map { $row->{$_} } sort keys %$row);
3647
+eval{$dbi->type_rule(
3648
+    into1 => {
3649
+        date => 'pp'
3495 3650
     }
3496
-    is_deeply($dbi->select(table => 'table1')->all,
3497
-      [
3498
-          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3499
-          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3500
-      ]
3501
-    );
3502
-}
3651
+)};
3652
+like($@, qr/not registered/);
3503 3653
 
3504
-test 'result';
3505 3654
 $dbi = DBIx::Custom->connect;
3506
-$dbi->execute($create_table1);
3507
-$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3508
-$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3655
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3656
+eval {
3657
+    $dbi->type_rule(
3658
+        from1 => {
3659
+            Date => sub { $_[0] * 2 },
3660
+        }
3661
+    );
3662
+};
3663
+like($@, qr/lower/);
3509 3664
 
3510
-$result = $dbi->select(table => 'table1');
3511
-@rows = ();
3512
-while (my $row = $result->fetch) {
3513
-    push @rows, [@$row];
3514
-}
3515
-is_deeply(\@rows, [[1, 2], [3, 4]]);
3665
+eval {
3666
+    $dbi->type_rule(
3667
+        into1 => {
3668
+            Date => sub { $_[0] * 2 },
3669
+        }
3670
+    );
3671
+};
3672
+like($@, qr/lower/);
3516 3673
 
3674
+$dbi = DBIx::Custom->connect;
3675
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3676
+$dbi->type_rule(
3677
+    from1 => {
3678
+        date => sub { $_[0] * 2 },
3679
+    },
3680
+    into1 => {
3681
+        date => sub { $_[0] * 3 },
3682
+    }
3683
+);
3684
+$dbi->insert({key1 => 2}, table => 'table1');
3517 3685
 $result = $dbi->select(table => 'table1');
3518
-@rows = ();
3519
-while (my $row = $result->fetch_hash) {
3520
-    push @rows, {%$row};
3521
-}
3522
-is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
3686
+$result->type_rule_off;
3687
+is($result->one->{key1}, 6);
3523 3688
 
3689
+$dbi = DBIx::Custom->connect;
3690
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3691
+$dbi->type_rule(
3692
+    from1 => {
3693
+        date => sub { $_[0] * 2 },
3694
+        datetime => sub { $_[0] * 4 },
3695
+    },
3696
+);
3697
+$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
3524 3698
 $result = $dbi->select(table => 'table1');
3525
-$row = $result->fetch_first;
3526
-is_deeply($row, [1, 2], "row");
3527
-$row = $result->fetch;
3528
-ok(!$row, "finished");
3699
+$result->type_rule(
3700
+    from1 => {
3701
+        date => sub { $_[0] * 3 }
3702
+    }
3703
+);
3704
+$row = $result->one;
3705
+is($row->{key1}, 6);
3706
+is($row->{key2}, 2);
3529 3707
 
3530 3708
 $result = $dbi->select(table => 'table1');
3531
-$row = $result->fetch_hash_first;
3532
-is_deeply($row, {key1 => 1, key2 => 2}, "row");
3533
-$row = $result->fetch_hash;
3534
-ok(!$row, "finished");
3535
-
3536
-$dbi->execute('create table table2 (key1, key2);');
3537
-$result = $dbi->select(table => 'table2');
3538
-$row = $result->fetch_hash_first;
3539
-ok(!$row, "no row fetch");
3709
+$result->type_rule(
3710
+    from1 => {
3711
+        date => sub { $_[0] * 3 }
3712
+    }
3713
+);
3714
+$row = $result->one;
3715
+is($row->{key1}, 6);
3716
+is($row->{key2}, 2);
3540 3717
 
3541
-$dbi = DBIx::Custom->connect;
3542
-$dbi->execute($create_table1);
3543
-$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3544
-$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3545
-$dbi->insert({key1 => 5, key2 => 6}, table => 'table1');
3546
-$dbi->insert({key1 => 7, key2 => 8}, table => 'table1');
3547
-$dbi->insert({key1 => 9, key2 => 10}, table => 'table1');
3548 3718
 $result = $dbi->select(table => 'table1');
3549
-$rows = $result->fetch_multi(2);
3550
-is_deeply($rows, [[1, 2],
3551
-                  [3, 4]], "fetch_multi first");
3552
-$rows = $result->fetch_multi(2);
3553
-is_deeply($rows, [[5, 6],
3554
-                  [7, 8]], "fetch_multi secound");
3555
-$rows = $result->fetch_multi(2);
3556
-is_deeply($rows, [[9, 10]], "fetch_multi third");
3557
-$rows = $result->fetch_multi(2);
3558
-ok(!$rows);
3559
-
3719
+$result->type_rule(
3720
+    from1 => {
3721
+        date => sub { $_[0] * 3 }
3722
+    }
3723
+);
3724
+$row = $result->one;
3725
+is($row->{key1}, 6);
3726
+is($row->{key2}, 2);
3560 3727
 $result = $dbi->select(table => 'table1');
3561
-eval {$result->fetch_multi};
3562
-like($@, qr/Row count must be specified/, "Not specified row count");
3563
-
3728
+$result->type_rule(
3729
+    from1 => [date => sub { $_[0] * 3 }]
3730
+);
3731
+$row = $result->one;
3732
+is($row->{key1}, 6);
3733
+is($row->{key2}, 2);
3734
+$dbi->register_filter(fivetimes => sub { $_[0] * 5});
3564 3735
 $result = $dbi->select(table => 'table1');
3565
-$rows = $result->fetch_hash_multi(2);
3566
-is_deeply($rows, [{key1 => 1, key2 => 2},
3567
-                  {key1 => 3, key2 => 4}], "fetch_multi first");
3568
-$rows = $result->fetch_hash_multi(2);
3569
-is_deeply($rows, [{key1 => 5, key2 => 6},
3570
-                  {key1 => 7, key2 => 8}], "fetch_multi secound");
3571
-$rows = $result->fetch_hash_multi(2);
3572
-is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third");
3573
-$rows = $result->fetch_hash_multi(2);
3574
-ok(!$rows);
3575
-
3736
+$result->type_rule(
3737
+    from1 => [date => 'fivetimes']
3738
+);
3739
+$row = $result->one;
3740
+is($row->{key1}, 10);
3741
+is($row->{key2}, 2);
3576 3742
 $result = $dbi->select(table => 'table1');
3577
-eval {$result->fetch_hash_multi};
3578
-like($@, qr/Row count must be specified/, "Not specified row count");
3743
+$result->type_rule(
3744
+    from1 => [date => undef]
3745
+);
3746
+$row = $result->one;
3747
+is($row->{key1}, 2);
3748
+is($row->{key2}, 2);
3579 3749
 
3580 3750
 $dbi = DBIx::Custom->connect;
3581
-$dbi->execute($create_table1);
3582
-$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3583
-$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3584
-
3585
-test 'fetch_all';
3751
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3752
+$dbi->type_rule(
3753
+    from1 => {
3754
+        date => sub { $_[0] * 2 },
3755
+    },
3756
+);
3757
+$dbi->insert({key1 => 2}, table => 'table1');
3586 3758
 $result = $dbi->select(table => 'table1');
3587
-$rows = $result->fetch_all;
3588
-is_deeply($rows, [[1, 2], [3, 4]]);
3759
+$result->filter(key1 => sub { $_[0] * 3 });
3760
+is($result->one->{key1}, 12);
3589 3761
 
3762
+$dbi = DBIx::Custom->connect;
3763
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3764
+$dbi->type_rule(
3765
+    from1 => {
3766
+        date => sub { $_[0] * 2 },
3767
+    },
3768
+);
3769
+$dbi->insert({key1 => 2}, table => 'table1');
3590 3770
 $result = $dbi->select(table => 'table1');
3591
-$rows = $result->fetch_hash_all;
3592
-is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
3771
+$result->filter(key1 => sub { $_[0] * 3 });
3772
+is($result->fetch->[0], 12);
3593 3773
 
3774
+$dbi = DBIx::Custom->connect;
3775
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3776
+$dbi->type_rule(
3777
+    into1 => {
3778
+        date => sub { $_[0] . 'b' }
3779
+    },
3780
+    into2 => {
3781
+        date => sub { $_[0] . 'c' }
3782
+    },
3783
+    from1 => {
3784
+        date => sub { $_[0] . 'd' }
3785
+    },
3786
+    from2 => {
3787
+        date => sub { $_[0] . 'e' }
3788
+    }
3789
+);
3790
+$dbi->insert({key1 => '1'}, table => 'table1', type_rule_off => 1);
3594 3791
 $result = $dbi->select(table => 'table1');
3595
-$result->dbi->filters({three_times => sub { $_[0] * 3}});
3596
-$result->filter({key1 => 'three_times'});
3597
-
3598
-$rows = $result->fetch_all;
3599
-is_deeply($rows, [[3, 2], [9, 4]], "array");
3600
-
3792
+is($result->type_rule_off->fetch_first->[0], '1');
3601 3793
 $result = $dbi->select(table => 'table1');
3602
-$result->dbi->filters({three_times => sub { $_[0] * 3}});
3603
-$result->filter({key1 => 'three_times'});
3604
-$rows = $result->fetch_hash_all;
3605
-is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "hash");
3794
+is($result->type_rule_on->fetch_first->[0], '1de');
3606 3795
 
3607
-test "query_builder";
3608
-$datas = [
3609
-    # Basic tests
3610
-    {   name            => 'placeholder basic',
3611
-        source            => "a {?  k1} b {=  k2} {<> k3} {>  k4} {<  k5} {>= k6} {<= k7} {like k8}", ,
3612
-        sql_expected    => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?;",
3613
-        columns_expected   => [qw/k1 k2 k3 k4 k5 k6 k7 k8/]
3614
-    },
3615
-    {
3616
-        name            => 'placeholder in',
3617
-        source            => "{in k1 3};",
3618
-        sql_expected    => "k1 in (?, ?, ?);",
3619
-        columns_expected   => [qw/k1 k1 k1/]
3796
+$dbi = DBIx::Custom->connect;
3797
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3798
+$dbi->type_rule(
3799
+    into1 => {
3800
+        date => sub { $_[0] . 'b' }
3620 3801
     },
3621
-    
3622
-    # Table name
3623
-    {
3624
-        name            => 'placeholder with table name',
3625
-        source            => "{= a.k1} {= a.k2}",
3626
-        sql_expected    => "a.k1 = ? a.k2 = ?;",
3627
-        columns_expected  => [qw/a.k1 a.k2/]
3802
+    into2 => {
3803
+        date => sub { $_[0] . 'c' }
3628 3804
     },
3629
-    {   
3630
-        name            => 'placeholder in with table name',
3631
-        source            => "{in a.k1 2} {in b.k2 2}",
3632
-        sql_expected    => "a.k1 in (?, ?) b.k2 in (?, ?);",
3633
-        columns_expected  => [qw/a.k1 a.k1 b.k2 b.k2/]
3805
+    from1 => {
3806
+        date => sub { $_[0] . 'd' }
3634 3807
     },
3635
-    {
3636
-        name            => 'not contain tag',
3637
-        source            => "aaa",
3638
-        sql_expected    => "aaa;",
3639
-        columns_expected  => [],
3640
-    }
3641
-];
3642
-
3643
-for (my $i = 0; $i < @$datas; $i++) {
3644
-    my $data = $datas->[$i];
3645
-    my $builder = DBIx::Custom->new->query_builder;
3646
-    my $query = $builder->build_query($data->{source});
3647
-    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
3648
-    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
3649
-}
3650
-
3651
-$builder = DBIx::Custom->new->query_builder;
3652
-$ret_val = $builder->register_tag(
3653
-    p => sub {
3654
-        my @args = @_;
3655
-        
3656
-        my $expand    = "? $args[0] $args[1]";
3657
-        my $columns = [2];
3658
-        return [$expand, $columns];
3808
+    from2 => {
3809
+        date => sub { $_[0] . 'e' }
3659 3810
     }
3660 3811
 );
3812
+$dbi->insert({key1 => '1'}, table => 'table1', type_rule1_off => 1);
3813
+$result = $dbi->select(table => 'table1');
3814
+is($result->type_rule1_off->fetch_first->[0], '1ce');
3815
+$result = $dbi->select(table => 'table1');
3816
+is($result->type_rule1_on->fetch_first->[0], '1cde');
3661 3817
 
3662
-$query = $builder->build_query("{p a b}");
3663
-is($query->{sql}, "? a b;", "register_tag sql");
3664
-is_deeply($query->{columns}, [2], "register_tag columns");
3665
-isa_ok($ret_val, 'DBIx::Custom::QueryBuilder');
3666
-
3667
-$builder = DBIx::Custom->new->query_builder;
3668
-
3669
-eval{$builder->build_query('{? }')};
3670
-like($@, qr/\QColumn name must be specified in tag "{? }"/, "? not arguments");
3671
-
3672
-eval{$builder->build_query("{a }")};
3673
-like($@, qr/\QTag "a" is not registered/, "tag not exist");
3674
-
3675
-$builder->register_tag({
3676
-    q => 'string'
3677
-});
3678
-
3679
-eval{$builder->build_query("{q}", {})};
3680
-like($@, qr/Tag "q" must be sub reference/, "tag not code ref");
3681
-
3682
-$builder->register_tag({
3683
-   r => sub {} 
3684
-});
3685
-
3686
-eval{$builder->build_query("{r}")};
3687
-like($@, qr/\QTag "r" must return [STRING, ARRAY_REFERENCE]/, "tag return noting");
3688
-
3689
-$builder->register_tag({
3690
-   s => sub { return ["a", ""]} 
3691
-});
3692
-
3693
-eval{$builder->build_query("{s}")};
3694
-like($@, qr/\QTag "s" must return [STRING, ARRAY_REFERENCE]/, "tag return not array columns");
3695
-
3696
-$builder->register_tag(
3697
-    t => sub {return ["a", []]}
3698
-);
3699
-
3700
-
3701
-test 'General error case';
3702
-$builder = DBIx::Custom->new->query_builder;
3703
-$builder->register_tag(
3704
-    a => sub {
3705
-        return ["? ? ?", ['']];
3818
+$dbi = DBIx::Custom->connect;
3819
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3820
+$dbi->type_rule(
3821
+    into1 => {
3822
+        date => sub { $_[0] . 'b' }
3823
+    },
3824
+    into2 => {
3825
+        date => sub { $_[0] . 'c' }
3826
+    },
3827
+    from1 => {
3828
+        date => sub { $_[0] . 'd' }
3829
+    },
3830
+    from2 => {
3831
+        date => sub { $_[0] . 'e' }
3706 3832
     }
3707 3833
 );
3708
-eval{$builder->build_query("{a}")};
3709
-like($@, qr/\QPlaceholder count/, "placeholder count is invalid");
3710
-
3711
-
3712
-test 'Default tag Error case';
3713
-eval{$builder->build_query("{= }")};
3714
-like($@, qr/Column name must be specified in tag "{= }"/, "basic '=' : key not exist");
3715
-
3716
-eval{$builder->build_query("{in }")};
3717
-like($@, qr/Column name and count of values must be specified in tag "{in }"/, "in : key not exist");
3718
-
3719
-eval{$builder->build_query("{in a}")};
3720
-like($@, qr/\QColumn name and count of values must be specified in tag "{in }"/,
3721
-     "in : key not exist");
3722
-
3723
-eval{$builder->build_query("{in a r}")};
3724
-like($@, qr/\QColumn name and count of values must be specified in tag "{in }"/,
3725
-     "in : key not exist");
3726
-
3727
-test 'variouse source';
3728
-$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d;";
3729
-$query = $builder->build_query($source);
3730
-is($query->sql, 'a b = ? c { } { = ? } = ? d;', "basic : 1");
3731
-
3732
-$source = "abc;";
3733
-$query = $builder->build_query($source);
3734
-is($query->sql, 'abc;', "basic : 2");
3735
-
3736
-$source = "{= a}";
3737
-$query = $builder->build_query($source);
3738
-is($query->sql, 'a = ?;', "only tag");
3739
-
3740
-$source = "000;";
3741
-$query = $builder->build_query($source);
3742
-is($query->sql, '000;', "contain 0 value");
3743
-
3744
-$source = "a {= b} }";
3745
-eval{$builder->build_query($source)};
3746
-like($@, qr/unexpected "}"/, "error : 1");
3747
-
3748
-$source = "a {= {}";
3749
-eval{$builder->build_query($source)};
3750
-like($@, qr/unexpected "{"/, "error : 2");
3751
-
3834
+$dbi->insert({key1 => '1'}, table => 'table1', type_rule2_off => 1);
3835
+$result = $dbi->select(table => 'table1');
3836
+is($result->type_rule2_off->fetch_first->[0], '1bd');
3837
+$result = $dbi->select(table => 'table1');
3838
+is($result->type_rule2_on->fetch_first->[0], '1bde');