/usr/share/perl5/SQL/Statement.pm is in libsql-statement-perl 1.407-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 | package SQL::Statement;
#########################################################################
#
# This module is copyright (c), 2001,2005 by Jeff Zucker.
# This module is copyright (c), 2007-2010 by Jeff Zucker, Jens Rehsack.
# All rights reserved.
#
# It may be freely distributed under the same terms as Perl itself.
#
# See below for help (search for SYNOPSIS)
#########################################################################
use strict;
use warnings FATAL => "all";
use 5.008;
use vars qw($VERSION $DEBUG);
use SQL::Parser ();
use SQL::Eval ();
use SQL::Statement::RAM ();
use SQL::Statement::TermFactory ();
use SQL::Statement::Util ();
use Carp qw(carp croak);
use Clone qw(clone);
use Errno;
use Scalar::Util qw(blessed looks_like_number);
use List::Util qw(first);
use Params::Util qw(_INSTANCE _STRING _ARRAY _ARRAY0 _HASH0 _HASH);
#use locale;
$VERSION = '1.407';
sub new
{
my ( $class, $sql, $flags ) = @_;
# IF USER DEFINED extend_csv IN SCRIPT
# USE THE ANYDATA DIALECT RATHER THAN THE CSV DIALECT
# WITH DBD::CSV
if ( ( defined($main::extend_csv) && $main::extend_csv )
|| ( defined($main::extend_sql) && $main::extend_sql ) )
{
$flags = SQL::Parser->new('AnyData');
}
my $parser = $flags;
my $self = bless( {}, $class );
$flags->{PrintError} = 1 unless defined $flags->{PrintError};
$flags->{text_numbers} = 1 unless defined $flags->{text_numbers};
$flags->{alpha_compare} = 1 unless defined $flags->{alpha_compare};
unless ( blessed($flags) ) # avoid copying stale data from earlier parsing sessions
{
%$self = ( %$self, %{ clone($flags) } );
}
else
{
$self->{$_} = $flags->{$_} for qw(RaiseError PrintError opts);
}
$self->{dlm} = '~';
# Dean Arnold improvement to allow better subclassing
# if (!ref($parser) or (ref($parser) and ref($parser) !~ /^SQL::Parser/)) {
unless ( _INSTANCE( $parser, 'SQL::Parser' ) )
{
my $parser_dialect = $flags->{dialect} || 'AnyData';
$parser_dialect = 'AnyData' if ( $parser_dialect =~ m/^(?:CSV|Excel)$/ );
$parser = SQL::Parser->new( $parser_dialect, $flags );
}
$self->{termFactory} = SQL::Statement::TermFactory->new($self);
$self->{capabilities} = {};
$self->prepare( $sql, $parser );
return $self;
}
sub prepare
{
my ( $self, $sql, $parser ) = @_;
$self->{already_prepared}->{$sql} and return;
# delete earlier preparations, they're overwritten after this prepare run
$self->{already_prepared} = {};
my $rv = $parser->parse($sql);
if ($rv)
{
undef $self->{errstr};
my $parser_struct = clone( $parser->{struct} );
while ( my ( $k, $v ) = each( %{$parser_struct} ) )
{
$self->{$k} = $v;
}
undef $self->{where_terms}; # force rebuild when needed
undef $self->{columns};
undef $self->{splitted_all_cols};
$self->{argnum} = 0;
my $values = $self->{values};
my $param_num = -1;
if ( $self->{limit_clause} )
{
$self->{limit_clause} = SQL::Statement::Limit->new( $self->{limit_clause} );
}
if ( defined( $self->{num_placeholders} ) )
{
for my $i ( 0 .. $self->{num_placeholders} - 1 )
{
$self->{params}->[$i] = SQL::Statement::Param->new($i);
}
}
$self->{tables} = [ map { SQL::Statement::Table->new($_) } @{ $self->{table_names} } ];
if ( $self->{where_clause} && !defined( $self->{where_terms} ) )
{
$self->{where_terms} = $self->{termFactory}->buildCondition( $self->{where_clause} );
#if ( $self->{where_clause}->{combiners} )
#{
# $self->{has_OR} = 1
# if ( first { -1 != index( $_, 'OR' ) } @{ $self->{where_clause}->{combiners} } );
#}
}
++$self->{already_prepared}->{$sql};
return $self;
}
else
{
$self->{errstr} = $parser->errstr;
++$self->{already_prepared}->{$sql};
return;
}
}
sub execute
{
my ( $self, $data, $params ) = @_;
( $self->{NUM_OF_ROWS}, $self->{NUM_OF_FIELDS}, $self->{data} ) = ( 0, 0, [] ) and return 'OEO'
if ( $self->{no_execute} );
$self->{procedure}->{data} = $data if ( $self->{procedure} );
$self->{params} = $params;
my ($command) = $self->command();
return $self->do_err('No command found!') unless ($command);
$self->{where_clause}
and !defined( $self->{where_terms} )
and $self->{where_terms} = $self->{termFactory}->buildCondition( $self->{where_clause} );
( $self->{NUM_OF_ROWS}, $self->{NUM_OF_FIELDS}, $self->{data} ) = $self->$command( $data, $params );
$self->{NAME} =
_ARRAY0( $self->{columns} ) ? [ map { delete $_->{term}->{fastpath}; $_->display_name() } @{ $self->{columns} } ] : [];
# Force closing the tables
$self->{tables} = [ map { SQL::Statement::Table->new($_->{name}) } @{ delete $self->{tables} } ]; # create keen defs
undef $self->{where_terms}; # force rebuild when needed
return unless ( defined( $self->{NUM_OF_ROWS} ) );
return $self->{NUM_OF_ROWS} || '0E0';
}
sub CREATE ($$$)
{
my ( $self, $data, $params ) = @_;
my $names;
# CREATE TABLE AS ...
my $subquery = $self->{subquery};
if ($subquery)
{
my $sth;
# AS IMPORT
if ( $subquery =~ m/^IMPORT/i )
{
$sth = $data->{Database}->prepare("SELECT * FROM $subquery")
or return $self->do_err( $data->{Database}->errstr() );
$sth->execute(@$params) or return $self->do_err( $sth->errstr() );
$names = $sth->{NAME};
}
# AS SELECT
else
{
$sth = $data->{Database}->prepare($subquery)
or return $self->do_err( $data->{Database}->errstr() );
$sth->execute() or return $self->do_err( $sth->errstr() );
$names = $sth->{NAME};
}
$names = $sth->{NAME} unless defined $names;
my $tbl_data = $sth->{sql_stmt}->{data};
my $tbl_name = $self->{org_table_names}->[0] || $self->tables(0)->name;
# my @tbl_cols = map {$_->name} $sth->{sql_stmt}->columns;
#my @tbl_cols=map{$_->name} $sth->{sql_stmt}->columns if $sth->{sql_stmt};
my @tbl_cols;
# @tbl_cols=@{ $sth->{NAME} } unless @tbl_cols;
@tbl_cols = @{$names} unless (@tbl_cols);
my $create_sql = "CREATE TABLE $tbl_name";
$create_sql = "CREATE TEMP TABLE $tbl_name" if ( $self->{is_ram_table} );
my @coldefs = map { "'$_' TEXT" } @tbl_cols;
$create_sql .= '(' . join( ',', @coldefs ) . ')';
$data->{Database}->do($create_sql) or die "Can't do <$create_sql>: " . $data->{Database}->errstr;
my $colstr = ('?,') x @tbl_cols;
my $insert_sql = "INSERT INTO $tbl_name VALUES($colstr)";
my $local_sth = $data->{Database}->prepare($insert_sql);
$local_sth->execute(@$_) for @$tbl_data;
return ( 0, 0 );
}
my ( $eval, $foo ) = $self->open_tables( $data, 1, 1 );
return unless ($eval);
$eval->params($params);
my ( $row, $table, $col ) = ( [], $eval->table( $self->tables(0)->name() ) );
if ( _ARRAY( $table->col_names() ) )
{
return $self->do_err( "Table '" . $self->tables(0)->name() . "' already exists." );
}
foreach $col ( $self->columns() )
{
push( @{$row}, $col->name() );
}
$table->push_names( $data, $row );
return ( 0, 0 );
}
sub CALL
{
my ( $self, $data, $params ) = @_;
# my $dbh = $data->{Database};
# $self->{procedure}->{data} = $data;
my $procTerm = $self->{termFactory}->buildCondition( $self->{procedure} );
( $self->{NUM_OF_ROWS}, $self->{NUM_OF_FIELDS}, $self->{data} ) = $procTerm->value($data);
}
my $enoentstr = "Cannot open .*\(" . Errno::ENOENT . "\)";
my $eabstrstr = "Abstract method .*::open_table called";
my $notblrx = qr/(?:$enoentstr|$eabstrstr)/;
sub DROP ($$$)
{
my ( $self, $data, $params ) = @_;
my $eval;
my @err;
eval {
local $SIG{__WARN__} = sub { push @err, @_ };
($eval) = $self->open_tables( $data, 0, 1 );
};
if ( $self->{ignore_missing_table}
and ( $@ or @err or $self->{errstr} )
and grep { $_ =~ $notblrx } ( @err, $@, $self->{errstr} ) )
{
return ( -1, 0 );
}
return if $self->{errstr};
return $self->do_err( $@ || $err[0] ) if ( $@ || @err );
# return undef unless $eval;
return ( -1, 0 ) unless $eval;
# $eval->params($params);
my ($table) = $eval->table( $self->tables(0)->name() );
$table->drop($data);
#use mylibs; zwarn $self->{sql_stmt};
return ( -1, 0 );
}
sub INSERT ($$$)
{
my ( $self, $data, $params ) = @_;
my ( $eval, $all_cols ) = $self->open_tables( $data, 0, 1 );
return unless ($eval);
$params and $eval->params($params);
$self->verify_columns( $data, $eval, $all_cols ) if ( scalar( $self->columns() ) );
return if ( $self->{errstr} );
my ($table) = $eval->table( $self->tables(0)->name() );
$table->seek( $data, 0, 2 ) unless ( $table->capability('insert_new_row') );
my ( $val, $col, $i, $k );
my ($cNum) = scalar( $self->columns() );
my $param_num = 0;
$cNum
or return $self->do_err("Bad col names in INSERT");
my $maxCol = $#$all_cols;
# INSERT INTO $table (row, ...) VALUES (value, ...), (...)
for ( $k = 0; $k < scalar( @{ $self->{values} } ); ++$k )
{
my ($array) = [];
for ( $i = 0; $i < $cNum; $i++ )
{
$col = $self->columns($i);
$val = $self->row_values( $k, $i );
if ( defined( _INSTANCE( $val, 'SQL::Statement::Param' ) ) )
{
$val = $eval->param( $val->idx() );
}
elsif ( defined( _INSTANCE( $val, 'SQL::Statement::Term' ) ) )
{
$val = $val->value($eval);
}
elsif ( $val and $val->{type} eq 'placeholder' )
{
$val = $eval->param( $param_num++ );
}
elsif ( defined( _HASH($val) ) )
{
$val = $self->{termFactory}->buildCondition($val);
$val = $val->value($eval);
}
else
{
return $self->do_err('Internal error: Unexpected column type');
}
$array->[ $table->column_num( $col->name() ) ] = $val;
}
# Extend row to put values in ALL fields
$#$array < $maxCol and $array->[$maxCol] = undef;
$table->capability('insert_new_row')
? $table->insert_new_row( $data, $array )
: $table->push_row( $data, $array );
}
return ( $k, 0 );
}
sub DELETE ($$$)
{
my ( $self, $data, $params ) = @_;
my ( $eval, $all_cols ) = $self->open_tables( $data, 0, 1 );
return unless $eval;
$eval->params($params);
$self->verify_columns( $data, $eval, $all_cols );
return if ( $self->{errstr} );
my $tname = $self->tables(0)->name();
my ($table) = $eval->table($tname);
my $affected = 0;
my ( @rows, $array );
while ( $array = $table->fetch_row($data) )
{
if ( $self->eval_where( $eval, $tname, $array ) )
{
++$affected;
if ( $table->capability('rowwise_delete') and $table->capability('inplace_delete') )
{
if ( $table->capability('delete_one_row') )
{
$table->delete_one_row( $data, $array );
}
elsif ( $table->capability('delete_current_row') )
{
$table->delete_current_row( $data, $array );
}
}
elsif ( $table->capability('rowwise_delete') )
{
push( @rows, $array );
}
next;
}
push( @rows, $array ) unless ( $table->capability('rowwise_delete') );
}
if ($affected)
{
if ( $table->capability('rowwise_delete') )
{ # @rows is empty in case of inplace_delete capability
foreach my $array (@rows)
{
$table->delete_one_row( $data, $array );
}
}
else
{
# rewrite table without deleted elements
$table->seek( $data, 0, 0 );
foreach my $array (@rows)
{
$table->push_row( $data, $array );
}
$table->truncate($data);
}
}
return ( $affected, 0 );
}
sub UPDATE ($$$)
{
my ( $self, $data, $params ) = @_;
my ( $eval, $all_cols ) = $self->open_tables( $data, 0, 1 );
return unless $eval;
my $valnum = $self->{num_val_placeholders};
my @val_params = splice( @{$params}, 0, $valnum ) if ($valnum);
$self->{params} ||= $params;
$eval->params($params);
$self->verify_columns( $data, $eval, $all_cols );
return if ( $self->{errstr} );
my $tname = $self->tables(0)->name();
my ($table) = $eval->table($tname);
my $affected = 0;
my @rows;
while ( my $array = $table->fetch_row($data) )
{
my $originalValues;
if ( $self->eval_where( $eval, $tname, $array ) )
{
my $valpos = 0;
if ( $table->capability('update_specific_row') )
{
$originalValues = clone($array);
}
for ( my $i = 0; $i < $self->columns(); $i++ )
{
my $val = $self->row_values( 0, $i );
if ( defined( _INSTANCE( $val, 'SQL::Statement::Param' ) ) )
{
$val = $val_params[ $valpos++ ];
}
elsif ( defined( _INSTANCE( $val, 'SQL::Statement::Term' ) ) )
{
$val = $val->value($eval);
}
elsif ( $val and $val->{type} eq 'placeholder' )
{
$val = $val_params[ $valpos++ ];
}
elsif ( defined( _HASH($val) ) )
{
$val = $self->{termFactory}->buildCondition($val);
$val = $val->value($eval);
}
else
{
return $self->do_err('Internal error: Unexpected column type');
}
my $col = $self->columns($i);
$array->[ $table->column_num( $col->name() ) ] = $val;
}
++$affected;
if ( $table->capability('rowwise_update') and $table->capability('inplace_update') )
{
# Martin Fabiani <martin@fabiani.net>:
# the following block is the most important enhancement to SQL::Statement::UPDATE
if ( $table->capability('update_specific_row') )
{
$table->update_specific_row( $data, $array, $originalValues );
}
elsif ( $table->capability('update_one_row') )
{
# NOTE: this prevents from updating index keys
$table->update_one_row( $data, $array );
}
elsif ( $table->capability('update_current_row') )
{
$table->update_current_row( $data, $array );
}
}
elsif ( $table->capability('rowwise_update') )
{
push( @rows, $array ) unless ( $table->capability('update_specific_row') );
push( @rows, [ $array, $originalValues ] )
if ( $table->capability('update_specific_row') );
}
}
push( @rows, $array ) unless ( $table->capability('rowwise_update') );
}
if ($affected)
{
if ( $table->capability('rowwise_update') )
{ # @rows is empty in case of inplace_update capability
foreach my $array (@rows)
{
if ( $table->capability('update_specific_row') )
{
$table->update_specific_row( $data, $array->[0], $array->[1] );
}
elsif ( $table->capability('update_one_row') )
{
$table->update_one_row( $data, $array );
}
}
}
else
{
# rewrite table with updated elements
$table->seek( $data, 0, 0 );
foreach my $array (@rows)
{
$table->push_row( $data, $array );
}
$table->truncate($data);
}
}
return ( $affected, 0 );
}
sub find_join_columns
{
my ( $self, @all_cols ) = @_;
my $display_combine = 'NAMED';
$display_combine = 'NATURAL' if ( -1 != index( $self->{join}->{type}, 'NATURAL' ) );
$display_combine = 'USING' if ( -1 != index( $self->{join}->{clause}, 'USING' ) );
my @display_cols;
my @keycols = ();
@keycols = @{ $self->{join}->{keycols} }
if $self->{join}->{keycols};
foreach (@keycols) { $_ =~ s/\./$self->{dlm}/ }
my %is_key_col;
%is_key_col = map { $_ => 1 } @keycols;
# IF NAMED COLUMNS, USE NAMED COLUMNS
#
if ( $display_combine eq 'NAMED' )
{
@display_cols = $self->columns();
#
# DAA
# need to get to $self's table objects to get the name
#
# @display_cols = map {$_->table . $self->{dlm} . $_->name} @display_cols;
# @display_cols = map {$_->table->{NAME} . $self->{dlm} . $_->name} @display_cols;
my @tbls = $self->tables();
my %tables = ();
$tables{ $_->name() } = $_ foreach (@tbls);
foreach ( 0 .. $#display_cols )
{
$display_cols[$_] = (
$display_cols[$_]->table()
? $tables{ $display_cols[$_]->table() }->name()
: ''
)
. $self->{dlm}
. $display_cols[$_]->name();
}
}
# IF ASTERISKED COLUMNS AND NOT NATURAL OR USING
# USE ALL COLUMNS, IN ORDER OF NAMING OF TABLES
#
elsif ( $display_combine eq 'NONE' )
{
@display_cols = @all_cols;
}
# IF NATURAL, COMBINE ALL SHARED COLUMNS
# IF USING, COMBINE ALL KEY COLUMNS
#
else
{
my %is_natural;
for my $full_col (@all_cols)
{
my ( $table, $col ) = $full_col =~ m/^([^$self->{dlm}]+)$self->{dlm}(.+)$/;
next if ( ( $display_combine eq 'NATURAL' ) and $is_natural{$col} );
next if ( ( $display_combine eq 'USING' ) && $is_natural{$col} && $is_key_col{$col} );
push( @display_cols, $full_col );
$is_natural{$col}++;
}
}
my @shared = ();
my %is_shared;
if ( $self->{join}->{type} =~ m/NATURAL/ )
{
for my $full_col (@all_cols)
{
my ( $table, $col ) = $full_col =~ m/^([^$self->{dlm}]+)$self->{dlm}(.+)$/;
push( @shared, $col ) if ( $is_shared{$col}++ ); # using side-effect of post-inc
}
}
else
{
@shared = @keycols;
}
$self->{join}->{shared_cols} = \@shared;
$self->{join}->{display_cols} = \@display_cols;
}
sub JOIN
{
my ( $self, $data, $params ) = @_;
my ( $eval, $all_cols ) = $self->open_tables( $data, 0, 0 );
return undef unless $eval;
$eval->params($params);
$self->verify_columns( $data, $eval, $all_cols );
return if ( $self->{errstr} );
if ( $self->{join}->{keycols}
and $self->{join}->{table_order}
and ( scalar( @{ $self->{join}->{table_order} } ) == 0 ) )
{
$self->{join}->{table_order} = $self->order_joins( $self->{join}->{keycols} );
$self->{join}->{table_order} = $self->{table_names}
unless ( defined( $self->{join}->{table_order} ) );
}
my @tables = $self->tables;
# GET THE LIST OF QUALIFIED COLUMN NAMES FOR DISPLAY
# *IN ORDER BY NAMING OF TABLES*
#
my @all_cols;
for my $table (@tables)
{
my @cols = @{ $eval->table( $table->{name} )->col_names };
for my $col (@cols)
{
push( @all_cols, $table->{name} . $self->{dlm} . $col );
}
}
$self->find_join_columns(@all_cols);
# JOIN THE TABLES
# *IN ORDER *BY JOINS*
#
@tables = @{ $self->{join}->{table_order} } if ( $self->{join}->{table_order} );
my ( $tableA, $tableB ) = splice( @tables, 0, 2 );
$tableA = $tableA->{name} if ( ref($tableA) );
$tableB = $tableB->{name} if ( ref($tableB) );
my ( $tableAobj, $tableBobj ) = ( $eval->table($tableA), $eval->table($tableB) );
$tableAobj->{NAME} ||= $tableA;
$tableBobj->{NAME} ||= $tableB;
$self->join_2_tables( $data, $params, $tableAobj, $tableBobj );
for my $next_table (@tables)
{
$tableAobj = $self->{join}->{table};
$tableBobj = $eval->table($next_table);
$tableBobj->{NAME} ||= $next_table;
$self->join_2_tables( $data, $params, $tableAobj, $tableBobj );
$self->{cur_table} = $next_table;
}
return $self->SELECT( $data, $params );
}
sub join_2_tables
{
my ( $self, $data, $params, $tableAobj, $tableBobj ) = @_;
my $share_type = 'IMPLICIT';
$share_type = 'NATURAL' if ( -1 != index( $self->{join}->{type}, 'NATURAL' ) );
$share_type = 'USING' if ( -1 != index( $self->{join}->{clause}, 'USING' ) );
$share_type = 'ON' if ( -1 != index( $self->{join}->{clause}, 'ON' ) );
$share_type = 'USING'
if ( ( $share_type eq 'ON' ) && ( scalar( @{ $self->{join}->{keycols} } ) == 1 ) );
my $join_type = 'INNER';
$join_type = 'LEFT' if ( -1 != index( $self->{join}->{type}, 'LEFT' ) );
$join_type = 'RIGHT' if ( -1 != index( $self->{join}->{type}, 'RIGHT' ) );
$join_type = 'FULL' if ( -1 != index( $self->{join}->{type}, 'FULL' ) );
my $right_join = $join_type eq 'RIGHT';
if ($right_join)
{
my $tmpTbl = $tableAobj;
$tableAobj = $tableBobj;
$tableBobj = $tmpTbl;
}
my $tableA = $tableAobj->{NAME};
( 0 != index( $tableA, '"' ) ) and $tableA = lc $tableA;
my $tableB = $tableBobj->{NAME};
( 0 != index( $tableB, '"' ) ) and $tableB = lc $tableB;
my @colsA = @{ $tableAobj->col_names() };
my @colsB = @{ $tableBobj->col_names() };
my ( %isunqualA, %isunqualB, @shared_cols );
$isunqualB{ $colsB[$_] } = 1 for ( 0 .. $#colsB );
my @tmpshared = @{ $self->{join}->{shared_cols} };
if ( $share_type eq 'ON' )
{
$right_join and @tmpshared = reverse @tmpshared;
}
elsif ( $share_type eq 'USING' )
{
foreach my $c (@tmpshared)
{
substr( $c, 0, index( $c, $self->{dlm} ) + 1 ) = '';
push( @shared_cols, $tableA . $self->{dlm} . $c );
push( @shared_cols, $tableB . $self->{dlm} . $c );
}
}
elsif ( $share_type eq 'NATURAL' )
{
for my $c (@colsA)
{
if ( $tableA eq $self->{dlm} . 'tmp' )
{
substr( $c, 0, index( $c, $self->{dlm} ) + 1 ) = '';
}
if ( $isunqualB{$c} )
{
push( @shared_cols, $tableA . $self->{dlm} . $c );
push( @shared_cols, $tableB . $self->{dlm} . $c );
}
}
}
my %whichqual;
if ( $share_type eq 'ON' || $share_type eq 'IMPLICIT' )
{
foreach my $colb (@colsB)
{
$colb = $whichqual{$colb} = $tableB . $self->{dlm} . $colb;
}
}
else
{
@colsB = map { $tableB . $self->{dlm} . $_ } @colsB;
}
my @all_cols = map { $tableA . $self->{dlm} . $_ } @colsA;
@all_cols = $right_join ? ( @colsB, @all_cols ) : ( @all_cols, @colsB );
{
my $str = $self->{dlm} . "tmp" . $self->{dlm};
foreach (@all_cols)
{
my $pos = index( $_, $str );
$pos >= 0 and substr( $_, $pos, length($str) ) = '';
}
}
if ( $tableA eq $self->{dlm} . 'tmp' )
{
foreach my $colA (@colsA)
{
my $c = substr( $colA, index( $colA, $self->{dlm} ) + 1 );
$isunqualA{$c} = $colA;
}
#%isunqualA =
# map { my ($c) = $_ =~ m/^(?:[^$self->{dlm}]+)$self->{dlm}(.+)$/; $c => $_ } @colsA;
}
else
{
foreach my $cola (@colsA)
{
$cola = $isunqualA{$cola} = $tableA . $self->{dlm} . $cola;
}
}
my ( %col_numsA, %col_numsB );
$col_numsA{ $colsA[$_] } = $_ for ( 0 .. $#colsA );
$col_numsB{ $colsB[$_] } = $_ for ( 0 .. $#colsB );
if ( $share_type eq 'ON' || $share_type eq 'IMPLICIT' )
{
%whichqual = ( %whichqual, %isunqualA );
while (@tmpshared)
{
my ( $k1, $k2 ) = splice( @tmpshared, 0, 2 );
# if both keys are in one table, bail out - FIXME: errmsg?
next if ( $isunqualA{$k1} && $isunqualA{$k2} );
next if ( $isunqualB{$k1} && $isunqualB{$k2} );
defined( $whichqual{$k1} ) and $k1 = $whichqual{$k1};
defined( $whichqual{$k2} ) and $k2 = $whichqual{$k2};
if ( defined( $col_numsA{$k1} ) && defined( $col_numsB{$k2} ) )
{
push( @shared_cols, $k1, $k2 );
}
elsif ( defined( $col_numsA{$k2} ) && defined( $col_numsB{$k1} ) )
{
push( @shared_cols, $k2, $k1 );
}
}
}
my %is_shared;
for my $c (@shared_cols)
{
$is_shared{$c} = 1;
defined( $col_numsA{$c} )
or defined( $col_numsB{$c} )
or return $self->do_err("Can't find shared columns!");
}
my ( $posA, $posB ) = ( [], [] );
for my $f (@shared_cols)
{
defined( $col_numsA{$f} ) and push( @{$posA}, $col_numsA{$f} );
defined( $col_numsB{$f} ) and push( @{$posB}, $col_numsB{$f} );
}
my $is_inner_join = $join_type eq "INNER";
#use mylibs; zwarn $self->{join};
# CYCLE THROUGH TABLE B, CREATING A HASH OF ITS VALUES
#
my $hashB = {};
TBLBFETCH: while ( my $array = $tableBobj->fetch_row($data) )
{
my @key_vals = @$array[@$posB];
if ($is_inner_join)
{
defined($_) or next TBLBFETCH for (@key_vals);
}
my $hashkey = join( ' ', @key_vals );
push( @{ $hashB->{$hashkey} }, $array );
}
# CYCLE THROUGH TABLE A
#
my $blankRow;
my $joined_table = [];
my %visited;
TBLAFETCH: while ( my $arrayA = $tableAobj->fetch_row($data) ) # use tbl1st & tbl2nd
{
my @key_vals = @$arrayA[@$posA];
if ($is_inner_join)
{
defined($_) or next TBLAFETCH for (@key_vals);
}
my $hashkey = join( ' ', @key_vals );
my $rowsB = $hashB->{$hashkey};
if ( !defined($rowsB) && ( $join_type ne 'INNER' ) )
{
defined($blankRow) or $blankRow = [ (undef) x scalar(@colsB) ];
$rowsB = [$blankRow];
}
if ( $join_type ne 'UNION' )
{
for my $arrayB ( @{$rowsB} )
{
my $newRow = $right_join ? [ @{$arrayB}, @{$arrayA} ] : [ @{$arrayA}, @{$arrayB} ];
push( @$joined_table, $newRow );
}
}
++$visited{$hashkey};
}
# ADD THE LEFTOVER B ROWS IF NEEDED
#
if ( $join_type eq 'FULL' || $join_type eq 'UNION' )
{
my $st_is_NaturalOrUsing = ( -1 != index( $self->{join}->{type}, 'NATURAL' ) )
|| ( -1 != index( $self->{join}->{clause}, 'USING' ) );
while ( my ( $k, $v ) = each %{$hashB} )
{
next if ( $visited{$k} );
for my $rowB (@$v)
{
my ( @arrayA, @tmpB, $rowhash );
@{$rowhash}{@colsB} = @{$rowB};
for my $c (@all_cols)
{
my ( $table, $col ) = split( $self->{dlm}, $c, 2 );
push( @arrayA, undef ) if ( $table eq $tableA );
push( @tmpB, $rowhash->{$c} ) if ( $table eq $tableB );
}
@arrayA[@$posA] = @tmpB[@$posB] if ($st_is_NaturalOrUsing);
my $newRow = [ @arrayA, @tmpB ];
push( @{$joined_table}, $newRow );
}
}
}
undef $hashB;
undef $tableAobj;
undef $tableBobj;
$self->{join}->{table} =
SQL::Statement::TempTable->new( $self->{dlm} . 'tmp', \@all_cols, $self->{join}->{display_cols}, $joined_table );
return;
}
sub run_functions
{
my ( $self, $data, $params ) = @_;
my ( $eval, $all_cols ) = $self->open_tables( $data, 0, 0 );
my @row = ();
for my $col ( $self->columns() )
{
my $val = $col->value($eval); # FIXME approve
push( @row, $val );
}
return ( 1, scalar @row, [ \@row ] );
}
sub SELECT($$)
{
my ( $self, $data, $params ) = @_;
$self->{params} ||= $params;
defined( _ARRAY( $self->{table_names} ) ) or return $self->run_functions( $data, $params );
my ( $eval, $all_cols, $tableName, $table );
if ( defined( $self->{join} ) )
{
defined $self->{join}->{table} or return $self->JOIN( $data, $params );
$tableName = $self->{dlm} . 'tmp';
$table = $self->{join}->{table};
}
else
{
( $eval, $all_cols ) = $self->open_tables( $data, 0, 0 );
return unless $eval;
$eval->params($params);
$self->verify_columns( $data, $eval, $all_cols );
return if ( $self->{errstr} );
$tableName = $self->tables(0)->name();
$table = $eval->table($tableName);
}
my $rows = [];
# In a loop, build the list of columns to retrieve; this will be
# used both for fetching data and ordering.
my ( $cList, $col, $tbl, $ar, $i, $c );
my $numFields = 0;
my %columns;
my @names;
my %funcs = ();
#
# DAA
#
# lets just disable this and see where it leads...
#
# if ($self->{join}) {
# @names = @{ $table->col_names };
# for my $col(@names) {
# $columns{$tableName}->{"$col"} = $numFields++;
# push(@$cList, $table->column_num($col));
# }
# }
# else {
foreach my $column ( $self->columns() )
{
if ( _INSTANCE( $column, 'SQL::Statement::Param' ) )
{
my $val = $eval->param( $column->idx() );
if ( -1 != ( my $idx = index( $val, '.' ) ) )
{
$col = substr( $val, 0, $idx );
$tbl = substr( $val, $idx + 1 );
}
else
{
$col = $val;
$tbl = $tableName;
}
$tbl ||= '';
$columns{$tbl}->{$col} = $numFields++;
}
else
{
( $col, $tbl ) = ( $column->name(), $column->table() );
$tbl ||= '';
$columns{$tbl}->{ $column->display_name() } = $columns{$tbl}->{$col} = $numFields++;
}
#
# handle functions in select list
#
# DAA
#
# check for a join temp table; if so, check if we can locate
# the column in its delimited set
#
my $cnum =
( ( $tableName eq ( $self->{dlm} . 'tmp' ) ) && ( $tbl ne '' ) )
? $table->column_num( $tbl . $self->{dlm} . $col )
: $table->column_num($col);
if ( !defined $cnum || $column->{function} )
{
$funcs{$col} = $column->{function};
$cnum = $col;
}
push( @$cList, $cnum );
# push(@$cList, $table->column_num($col));
push( @names, $col );
}
# }
$cList = [] unless ( defined($cList) );
if ( $self->{join} )
{
foreach (@names) { $_ =~ s/^[^$self->{dlm}]+$self->{dlm}//; }
}
$self->{NAME} = \@names;
# $self->verify_order_cols($table);
my @order_by = $self->order();
my @extraSortCols = ();
if (@order_by)
{
my $nFields = $numFields;
# It is possible that the user gave an ORDER BY clause with columns
# that are not part of $cList yet. These columns will need to be
# present in the array of arrays for sorting, but will be stripped
# off later.
my $i = -1;
foreach my $column (@order_by)
{
++$i;
( $col, $tbl ) = ( $column->column(), $column->table() );
my $pos;
$tbl ||= $self->colname2table($col);
$tbl ||= '';
if ( $self->{join} )
{
$pos = $table->column_num( $tbl . $self->{dlm} . $col );
defined($pos)
or $pos = $table->column_num( $tbl . '_' . $col );
}
next if ( exists( $columns{$tbl}->{$col} ) );
$pos = $table->column_num($col) unless ( defined($pos) );
push( @extraSortCols, $pos );
$columns{$tbl}->{$col} = $nFields++;
}
}
my $e = $self->{join} ? $table : $eval;
# begin count for limiting if there's a limit clause and no order clause
#
my $limit_count = 0 if ( $self->limit() and !$self->order() );
my $limit = $self->limit();
my $row_count = 0;
my $offset = $self->offset() || 0;
while ( my $array = $table->fetch_row($data) )
{
if ( $self->eval_where( $e, $tableName, $array, \%funcs ) )
{
next if ( defined($limit_count) and ( $row_count++ < $offset ) );
my @row = map { $_->value($e) } $self->columns();
push( @{$rows}, \@row );
# We quit here if there's a limit clause without order clause
# and the limit has been reached
defined($limit_count)
and ( ++$limit_count >= $limit )
and return ( $limit, $numFields, $rows );
}
}
if ( $self->distinct() )
{
my %seen;
@{$rows} = map {
$seen{ join( "\0", ( map { defined($_) ? $_ : '' } @{$_} ) ) }++
? ()
: $_
} @{$rows};
}
if ( $self->{has_set_functions} )
{
my $aggreg;
if ( $self->{group_by} )
{
my @keycols = @{ $self->{colpos} }{ @{ $self->{group_by} } };
$aggreg = SQL::Statement::Group->new( $self, $rows, \@keycols );
}
else
{
$aggreg = SQL::Statement::Aggregate->new( $self, $rows );
}
$rows = $aggreg->calc();
# FIXME re-order if order_by
}
if (@order_by)
{
use sort 'stable';
my @sortCols = map {
my ( $col, $tbl ) = ( $_->column(), $_->table() );
$self->{join} and $table->is_shared($col) and $tbl = 'shared';
$tbl ||= $self->colname2table($col) || '';
( $columns{$tbl}->{$col}, $_->desc() )
} @order_by;
$i = scalar(@sortCols);
do
{
my $desc = $sortCols[ --$i ];
my $colNum = $sortCols[ --$i ];
@{$rows} = sort {
my $result;
$result = _anycmp( $a->[$colNum], $b->[$colNum] );
$desc and $result = -$result;
$result;
} @{$rows};
} while ( $i > 0 );
use sort 'defaults'; # for perl < 5.10.0
}
if ( defined( $self->limit() ) )
{
my $offset = $self->offset() || 0;
my $limit = $self->limit() || 0;
@{$rows} = splice( @{$rows}, $offset, $limit );
}
# Rip off columns that have been added for @extraSortCols only
if (@extraSortCols)
{
foreach my $row ( @{$rows} )
{
splice( @{$row}, $numFields, scalar(@extraSortCols) );
}
}
( scalar( @{$rows} ), $numFields, $rows );
}
sub _anycmp($$;$)
{
my ( $a, $b, $case_fold ) = @_;
if ( !defined($a) || !defined($b) )
{
return defined($a) - defined($b);
}
elsif ( looks_like_number($a) && looks_like_number($b) )
{
return $a <=> $b;
}
else
{
return $case_fold ? lc($a) cmp lc($b) || $a cmp $b : $a cmp $b;
}
}
sub eval_where
{
my ( $self, $eval, $tname, $rowary ) = @_;
return 1 unless ( defined( $self->{where_terms} ) );
$self->{argnum} = 0;
return $self->{where_terms}->value($eval);
}
sub fetch_row
{
my ($self) = @_;
$self->{data} ||= [];
my $row = shift @{ $self->{data} };
return unless $row and scalar @$row;
return $row;
}
no warnings 'once';
*fetch = \&fetch_row;
use warnings;
sub fetch_rows
{
my $self = $_[0];
my $rows = $self->{data} || [];
$self->{data} = [];
return $rows;
}
sub open_table ($$$$$) { croak "Abstract method " . ref( $_[0] ) . "::open_table called" }
sub open_tables
{
my ( $self, $data, $createMode, $lockMode ) = @_;
my @c;
my $t = {};
my @tables = $self->tables();
my $count = -1;
for my $tbl (@tables)
{
++$count;
my $name = $tbl->name();
if ( $name =~ m/^(.+)\.([^\.]+)$/ )
{
my $schema = $1; # ignored
$name = $tbl->{name} = $2;
}
if ( defined( $self->{table_func} ) && defined( $self->{table_func}->{ uc $name } ) )
{
my $u_func = $self->{table_func}->{ uc $name };
$t->{$name} = $self->get_user_func_table( $name, $u_func );
}
elsif (defined( $data->{Database}->{sql_ram_tables} )
&& defined( $data->{Database}->{sql_ram_tables}->{$name} )
&& $data->{Database}->{sql_ram_tables}->{$name} )
{
$t->{$name} = $data->{Database}->{sql_ram_tables}->{$name};
$t->{$name}->seek( $data, 0, 0 );
$t->{$name}->init_table( $data, $name, $createMode, $lockMode )
if ( $t->{$name}->can('init_table') );
}
elsif ( $self->{is_ram_table} )
{
$t->{$name} = $data->{Database}->{sql_ram_tables}->{$name} =
SQL::Statement::RAM::Table->new( $name, [], [] );
}
else
{
undef $@;
eval {
my $open_name = $self->{org_table_names}->[$count];
$t->{$name} = $self->open_table( $data, $open_name, $createMode, $lockMode );
};
my $err = $t->{$name}->{errstr};
return $self->do_err($err) if ($err);
return $self->do_err($@) if ($@);
}
my @cnames;
my $table_cols = $t->{$name}->{org_col_names};
$table_cols = $t->{$name}->{col_names} unless $table_cols;
for my $c (@$table_cols)
{
my $newc = $c =~ m/^"/ ? $c : lc($c);
push( @cnames, $newc );
$self->{ORG_NAME}->{$newc} = $c;
}
#
# set the col_num => col_obj hash for the table
#
my $col_nums;
my $i = 0;
for (@cnames)
{
$col_nums->{$_} = $i++;
}
$t->{$name}->{col_nums} = $col_nums;
$t->{$name}->{col_names} = \@cnames;
my $tcols = $t->{$name}->col_names();
my @newcols;
for (@$tcols)
{
next unless ( defined($_) );
my $ncol = $_;
$ncol = $name . '.' . $ncol unless ( $ncol =~ m/\./ );
push( @newcols, $ncol );
}
@c = ( @c, @newcols );
}
$self->buildColumnObjects( $t, \@tables );
return $self->do_err( $self->{errstr} ) if ( $self->{errstr} );
##################################################
# Patch from Cosimo Streppone <cosimoATcpan.org>
# my $all_cols = $self->{all_cols}
# || [ map {$_->{name} }@{$self->{columns}} ]
# || [];
# @$all_cols = (@$all_cols,@c);
# $self->{all_cols} = $all_cols;
if ( !$self->{all_cols} )
{
my $all_cols = [];
$all_cols = [ map { $_->{name} } @{ $self->{columns} } ];
$all_cols ||= []; # ?
@$all_cols = ( @$all_cols, @c );
$self->{all_cols} = $all_cols;
}
##################################################
return SQL::Eval->new( { 'tables' => $t } ), \@c;
}
sub getColumnObject($)
{
my ( $self, $newcol, $t, $tables ) = @_;
my @columns;
if ( ( $newcol->{type} eq 'column' ) && ( -1 != index( $newcol->{value}, '*' ) ) )
{
my $tbl;
my @tables;
if ( $newcol->{value} =~ m/^(.+)\.\*$/ )
{
$tbl = $1;
return $self->do_err("No table name given in '$newcol->{value}'")
unless ( defined( _STRING($tbl) ) );
@tables = ($tbl);
}
else
{
@tables = map { $_->name() } @{$tables};
}
my $join = defined( _HASH( $self->{join} ) )
&& ( ( -1 != index( $self->{join}->{type}, 'NATURAL' ) )
|| ( -1 != index( $self->{join}->{clause}, 'USING' ) ) );
my %shared_cols;
foreach my $table (@tables)
{
return $self->do_err("Can't find table '$table'") unless ( defined( $t->{$table} ) );
my $tcols = $t->{$table}->{col_names};
return $self->do_err("Couldn't find column names for table '$table'!")
unless ( _ARRAY($tcols) );
foreach my $colName ( @{$tcols} )
{
next if ( $join && $shared_cols{$colName}++ );
my $expcol = [
$colName, # column name
$table, # table name
SQL::Statement::ColumnValue->new( $self, $table . '.' . $colName ), # term
$colName, # display name
$colName,
$newcol,
];
push( @columns, $expcol );
}
}
}
elsif ( ( 'CREATE' eq $self->command() ) || ( 'DROP' eq $self->command() ) )
{
return $self->do_err("Invalid column type '$newcol->{type}'")
unless ( 'column' eq $newcol->{type} );
my $expcol = [
$newcol->{value}, # column name
undef, # table name
undef, # term
$newcol->{value}, # display name
$newcol->{value}, # original name
$newcol, # coldef
];
push( @columns, $expcol );
}
else
{
my $col;
if ( $newcol->{type} eq 'setfunc' )
{
my @cols = $self->getColumnObject( $newcol->{arg}, $t );
if ( 1 == scalar(@cols) )
{
$col = $cols[0]->[2];
}
else
{
# FIXME add '\0' constants between items?
my $colSep = $self->{termFactory}->buildCondition(
{
type => 'string',
value => "\0",
}
);
@cols = map { $_->[2], $colSep } @cols;
pop(@cols);
$col = $self->{termFactory}->buildCondition(
{
type => 'function',
name => 'str_concat',
value => \@cols,
}
);
}
}
else
{
$col = $self->{termFactory}->buildCondition($newcol);
}
my $expcol = [
$newcol->{name} || $newcol->{value}, # column name
undef, # table name
$col, # term
$newcol->{alias} || $newcol->{fullorg}, # display name
$newcol->{fullorg}, # original name
$newcol, # coldef
];
push( @columns, $expcol );
}
return @columns;
}
sub buildColumnObjects($)
{
my ( $self, $t, $tables ) = @_;
defined( _ARRAY0( $self->{column_defs} ) ) or return;
defined( _ARRAY0( $self->{columns} ) ) and return;
$self->{columns} = [];
my $coldefs = $self->{column_defs};
for ( my $i = 0; $i < scalar( @{$coldefs} ); ++$i )
{
my $colentry = $coldefs->[$i];
my @columns = $self->getColumnObject( $colentry, $t, $tables );
return if ( $self->{errstr} );
foreach my $col (@columns)
{
my $expcol = SQL::Statement::Util::Column->new( @{$col} );
push( @{ $self->{columns} }, $expcol );
$self->{column_aliases}->{ $col->[4] } ||= $col->[3];
$self->{colpos}->{ $col->[3] } = scalar( @{ $self->{columns} } ) - 1;
}
}
return;
}
sub verify_expand_column
{
my ( $self, $c, $i, $usr_cols, $is_duplicate, $col_exists ) = @_;
# XXX
defined $self->{ALIASES}->{$c} and $c = $self->{ALIASES}->{$c};
my ( $table, $col, $col_obj );
if ( $c =~ m/(\S+)\.(\S+)/ )
{
$table = $1;
$col = $2;
}
elsif ( ++${$i} >= 0 )
{
$col_obj = $usr_cols->[ ${$i} ];
( $table, $col ) = ( $col_obj->{table}, $col_obj->{name} );
}
else
{
( $table, $col ) = $self->full_qualified_column_name($c);
}
return unless ($col);
my $is_column =
( defined( _INSTANCE( $col_obj, 'SQL::Statement::Util::Column' ) ) and ( $col_obj->{coldef}->{type} eq 'column' ) )
? 1
: 0;
unless ( $is_column and defined($table) )
{
( $table, undef ) = $self->full_qualified_column_name($col);
}
if ( defined( _INSTANCE( $table, 'SQL::Statement::Table' ) ) )
{
$table = $table->name();
}
if ( $is_column and !$table )
{
return $self->do_err("Ambiguous column name '$c'") if ( $is_duplicate->{$c} );
return $self->do_err("No such column '$col'");
$col = $c;
}
elsif ($is_column)
{
my $is_user_def = 1 if ( $self->{opts}->{function_defs}->{$col} );
return $self->do_err("No such column '$table.$col'")
unless ( $col_exists->{"$table.$col"}
or $col_exists->{ "\L$table." . $col }
or $is_user_def );
}
return ( $table, $col ) if ( $is_column or ${$i} < 0 );
return;
}
sub verify_columns
{
my ( $self, $data, $eval, $all_cols ) = @_;
#
# NOTE FOR LATER:
# perhaps cache column names and skip this after first table open
#
$all_cols ||= [];
my @tmp_cols = @{$all_cols};
my @usr_cols = $self->columns();
return $self->do_err('No fetchable columns') if ( 0 == scalar(@usr_cols) );
my ( $cnum, $fully_qualified_cols ) = ( 0, [] );
my @tmpcols = map { $_->{name} } @usr_cols;
my %col_exists = map { $_ => 1 } @tmp_cols;
my ( %is_member, @duplicates, %is_duplicate );
# $_ =~ s/[^.]*\.(.*)/$1/;
foreach (@$all_cols)
{
substr( $_, 0, index( $_, '.' ) + 1 ) = '';
} # XXX we're modifying $all_cols from caller!
@duplicates = grep( $is_member{$_}++, @$all_cols );
%is_duplicate = map { $_ => 1 } @duplicates;
if ( exists( $self->{join} ) && defined( _HASH( $self->{join} ) ) )
{
my $join = $self->{join};
if ( -1 != index( uc $join->{type}, 'NATURAL' ) )
{
%is_duplicate = ();
}
# the following should be probably conditioned on an option,
# but I don't know which --BW
elsif ( 'USING' eq $join->{clause} )
{
my @keys = @{ $join->{keycols} };
delete @is_duplicate{@keys};
}
}
my %set_func_nofunc;
if ( defined( $self->{has_set_functions} ) )
{
my @set_func_nofunc = grep { ( $_->{type} ne 'setfunc' ) } @{ $self->{column_defs} };
%set_func_nofunc = map { ( $_->{alias} || $_->{fullorg} ) => 1 } @set_func_nofunc;
}
my ( $is_fully, $set_fully ) = ( {}, {} );
my $i = -1;
my $num_tables = $self->tables();
for my $c (@tmpcols)
{
my ( $table, $col ) = $self->verify_expand_column( $c, \$i, \@usr_cols, \%is_duplicate, \%col_exists );
return if ( $self->{errstr} );
next unless ( $table && $col );
my $ftc = "$table.$col";
next if ( $table and $col and $is_fully->{$ftc} );
$self->{columns}->[$i]->{name} = $col;
$self->{columns}->[$i]->{table} = $table;
if ( $table and $col )
{
push( @$fully_qualified_cols, $ftc );
++$is_fully->{$ftc};
++$set_fully->{$ftc} if ( $set_func_nofunc{$c} );
}
}
if ( defined( $self->{has_set_functions} ) )
{
if ( defined( _ARRAY( $self->{group_by} ) ) )
{
foreach my $grpby ( @{ $self->{group_by} } )
{
$i = -2;
my ( $table, $col ) = $self->verify_expand_column( $grpby, \$i, \@usr_cols, \%is_duplicate, \%col_exists );
return if ( $self->{errstr} );
$col ||= $grpby;
( $table, $col ) = $self->full_qualified_column_name($col)
if ( defined($col) && !defined($table) );
next unless ( defined($table) && defined($col) );
delete $set_fully->{"$table.$col"};
}
}
if ( defined( _HASH($set_fully) ) )
{
return $self->do_err(
sprintf(
"Column%s '%s' must appear in the GROUP BY clause or be used in an aggregate function",
scalar( keys( %{$set_fully} ) ) > 1 ? 's' : '',
join( "', '", keys( %{$set_fully} ) )
)
);
}
}
if ( $self->{sort_spec_list} )
{
for my $n ( 0 .. scalar @{ $self->{sort_spec_list} } - 1 )
{
defined( _INSTANCE( $self->{sort_spec_list}->[$n], 'SQL::Statement::Order' ) ) and next;
my ( $newcol, $direction ) = each %{ $self->{sort_spec_list}->[$n] };
my $desc = $direction && ( $direction eq "DESC" ); # ($direction || "ASC") eq "DESC";
# XXX parse order by like group by and select list
$i = -2;
my ( $table, $col ) = $self->verify_expand_column( $newcol, \$i, \@usr_cols, \%is_duplicate, \%col_exists );
$self->{errstr} and return;
( $table, $col ) = $self->full_qualified_column_name($newcol)
if ( defined($col) && !defined($table) );
defined($table) and $col = $table . "." . $col;
$self->{sort_spec_list}->[$n] = SQL::Statement::Order->new(
col => SQL::Statement::Util::Column->new(
$col, # column name
$table, # table name
SQL::Statement::ColumnValue->new( $self, $col ), # term
$newcol # display name
),
direction => $direction,
desc => $desc,
);
}
}
return $fully_qualified_cols;
}
sub distinct()
{
my $q = _STRING( $_[0]->{set_quantifier} );
return defined($q) && ( 'DISTINCT' eq $q );
}
sub column_names()
{
my @cols = map { $_->name() } $_[0]->columns();
return @cols;
}
sub command() { return $_[0]->{command} }
sub params(;$)
{
if ( !$_[0]->{params} )
{
return wantarray ? () : 0;
}
return $_[0]->{params}->[ $_[1] ] if ( defined $_[1] );
return wantarray ? @{ $_[0]->{params} } : scalar @{ $_[0]->{params} };
}
sub row_values(;$$)
{
unless ( defined( _ARRAY( $_[0]->{values} ) ) )
{
return wantarray ? () : 0;
}
if ( defined( $_[1] ) )
{
return 0 unless ( defined( $_[0]->{values}->[ $_[1] ] ) );
return $_[0]->{values}->[ $_[1] ]->[ $_[2] ] if ( defined $_[2] );
return wantarray
? map { $_->{value} } @{ $_[0]->{values}->[ $_[1] ] }
: scalar @{ $_[0]->{values}->[ $_[1] ] };
}
else
{
return wantarray
? map {
[ map { $_->{value} } @{$_} ]
} @{ $_[0]->{values} }
: scalar( @{ $_[0]->{values} } );
}
}
#
# $num_of_cols = $stmt->columns() # number of columns
# @cols = $stmt->columns() # array of S::S::Column objects
# $col = $stmt->columns($cnum) # S::S::Column obj for col number $cnum
# $col = $stmt->columns($cname) # S::S::Column obj for col named $cname
#
sub columns
{
my ( $self, $col ) = @_;
if ( !$self->{columns} )
{
return wantarray ? () : 0;
}
if ( defined $col and $col =~ m/^\d+$/ )
{ # arg1 = a number
return $self->{columns}->[$col];
}
elsif ( defined $col )
{ # arg1 = string
for my $c ( @{ $self->{columns} } )
{
return $c if ( $c->name() eq $col );
}
}
return wantarray ? @{ $self->{columns} } : scalar @{ $self->{columns} };
}
sub colname2colnum
{
if ( !$_[0]->{columns} ) { return undef; }
for my $i ( 0 .. $#{ $_[0]->{columns} } )
{
return $i if ( $_[0]->{columns}->[$i]->name() eq $_[1] );
}
return undef;
}
sub colname2table($)
{
my ( $self, $col_name ) = @_;
return undef unless defined $col_name;
my ( $tbl, $col );
if ( $col_name =~ /^(.+)\.(.+)$/ )
{
( $tbl, $col ) = ( $1, $2 );
}
else
{
$col = $col_name;
}
my $found_table;
for my $full_col ( @{ $self->{all_cols} } )
{
my ( $stbl, $scol ) = $full_col =~ /^(.+)\.(.+)$/;
next unless ( $scol || '' ) eq $col;
next if ( defined($tbl) && ( $tbl ne $stbl ) );
$found_table = $stbl;
last;
}
return $found_table;
}
sub full_qualified_column_name($)
{
my ( $self, $col_name ) = @_;
return unless ( defined($col_name) );
# XXX
defined $self->{ALIASES}->{$col_name} and $col_name = $self->{ALIASES}->{$col_name};
my ( $tbl, $col );
unless ( ( $tbl, $col ) = $col_name =~ m/^((?:"[^"]+")|(?:[^.]+))\.(.*)$/ )
{
$col = $col_name;
}
unless ( defined( $self->{splitted_all_cols} ) )
{
my @rc;
for my $full_col ( @{ $self->{all_cols} } )
{
if ( my ( $stbl, $scol ) = $full_col =~ m/^((?:"[^"]+")|(?:[^.]+))\.(.*)$/ )
{
push( @{ $self->{splitted_all_cols} }, [ $stbl, $scol ] );
defined($tbl) and ( $tbl ne $stbl ) and next;
( $scol eq $col ) and @rc = ( $stbl, $scol );
}
}
@rc and return @rc;
}
else
{
for my $splitted_col ( @{ $self->{splitted_all_cols} } )
{
defined($tbl) and ( $tbl ne $splitted_col->[0] ) and next;
( $splitted_col->[1] eq $col ) and return @$splitted_col;
}
}
return ( $tbl, $col );
}
#sub verify_order_cols
#{
# my ( $self, $table ) = @_;
# return unless $self->{sort_spec_list};
# my @ocols = $self->order();
# my @tcols = @{ $table->col_names() };
# my @n_ocols;
#
# for my $colnum ( 0 .. $#ocols )
# {
# my $col = $self->order($colnum);
#
# if ( !defined( $col->table() ) )
# {
# my $cname = $ocols[$colnum]->{col}->name();
# my $tname = $self->colname2table($cname);
# return $self->do_err("No such column '$cname'.") unless ($tname);
# $self->{sort_spec_list}->[$colnum]->{col}->{table} = $tname;
# push( @n_ocols, $tname );
# }
# }
#
# return 1;
#}
sub limit ($) { $_[0]->{limit_clause}->{limit}; }
sub offset ($) { $_[0]->{limit_clause}->{offset}; }
sub order
{
return unless ( defined $_[0]->{sort_spec_list} );
return
defined( $_[1] ) && looks_like_number( $_[1] ) ? $_[0]->{sort_spec_list}->[ $_[1] ]
: wantarray ? @{ $_[0]->{sort_spec_list} }
: scalar @{ $_[0]->{sort_spec_list} };
}
sub tables
{
return
defined( $_[1] ) && looks_like_number( $_[1] ) ? $_[0]->{tables}->[ $_[1] ]
: wantarray ? @{ $_[0]->{tables} }
: scalar @{ $_[0]->{tables} };
}
sub order_joins
{
my ( $self, $links ) = @_;
my ( @new_keycols, @new_links );
for (@$links)
{
my ( $tbl, $col ) = $self->full_qualified_column_name($_);
push( @new_keycols, $tbl . $self->{dlm} . $col );
push( @new_links, $tbl );
}
$self->{join}->{keycols} = $links = \@new_keycols;
# my @tmp = @new_keycols;
# foreach (@tmp) { $_ =~ s/\./$self->{dlm}/g; }
# $self->{join}->{keycols} = \@tmp;
# @$links = \@new_keycols;
my @all_tables;
my %relations;
my %is_table;
while (@new_links)
{
my $t1 = shift(@new_links);
my $t2 = shift(@new_links);
return undef unless ( defined($t1) and defined($t2) );
push @all_tables, $t1 unless ( $is_table{$t1}++ );
push @all_tables, $t2 unless ( $is_table{$t2}++ );
$relations{$t1}{$t2}++;
$relations{$t2}{$t1}++;
}
my @tables = @all_tables;
my @order = shift @tables;
my %is_ordered = ( $order[0] => 1 );
my %visited;
while (@tables)
{
my $t = shift @tables;
my @rels = keys %{ $relations{$t} };
for my $t2 (@rels)
{
next unless $is_ordered{$t2};
push @order, $t;
$is_ordered{$t}++;
last;
}
if ( !$is_ordered{$t} )
{
push( @tables, $t ) if ( $visited{$t}++ < @all_tables );
}
}
if ( @order < @all_tables )
{
my @missing;
my %in_order = map { $_ => 1 } @order;
foreach my $tbl (@all_tables)
{
next if ( $in_order{$tbl} );
push( @missing, $tbl );
}
return $self->do_err( sprintf( 'Unconnected tables (%s) in equijoin statement!', join( ', ', @missing ) ) );
}
$self->{join}->{table_order} = \@order;
return \@order;
}
sub do_err
{
my $self = shift;
my $err = shift;
my $errtype = shift;
my @c = caller 6;
#$err = "[" . $self->{original_string} . "]\n$err\n\n";
# $err = "$err\n\n";
my $prog = $c[1];
my $line = $c[2];
$prog = defined($prog) ? " called from $prog" : '';
$prog .= defined($line) ? " at $line" : '';
$err = "\nExecution ERROR: $err$prog.\n\n";
$self->{errstr} = $err;
carp $err if $self->{PrintError};
croak "$err" if $self->{RaiseError};
return;
}
sub errstr() { return $_[0]->{errstr}; }
sub where_hash() { return $_[0]->{where_clause}; }
sub column_defs() { return $_[0]->{column_defs}; }
sub where()
{
return undef unless $_[0]->{where_terms};
return $_[0]->{where_terms};
}
sub get_user_func_table
{
my ( $self, $name, $u_func ) = @_;
my $term = $self->{termFactory}->buildCondition($u_func);
my @data_aryref = @{ $term->value(undef) };
my $col_names = shift @data_aryref;
# my $tempTable = SQL::Statement::TempTable->new(
# $name, $col_names, $col_names, $data_aryref
# );
my $tempTable = SQL::Statement::RAM::Table->new( $name, $col_names, \@data_aryref );
$tempTable->{all_cols} ||= $col_names;
return $tempTable;
}
sub capability($)
{
my ( $self, $capname ) = @_;
return $self->{capabilities}->{$capname} if ( defined( $self->{capabilities}->{$capname} ) );
return;
}
sub DESTROY
{
my $self = $_[0];
undef $self->{NAME};
undef $self->{ORG_NAME};
undef $self->{all_cols};
undef $self->{already_prepared};
undef $self->{argnum};
undef $self->{col_obj};
undef $self->{column_names};
undef $self->{columns};
undef $self->{cur_table};
undef $self->{data};
undef $self->{group_by};
#undef $self->{has_OR};
undef $self->{join};
undef $self->{limit_clause};
undef $self->{num_placeholders};
undef $self->{num_val_placeholders};
undef $self->{org_table_names};
undef $self->{params};
undef $self->{opts};
undef $self->{procedure};
undef $self->{set_function};
undef $self->{sort_spec_list};
undef $self->{subquery};
undef $self->{tables};
undef $self->{table_names};
undef $self->{table_func};
undef $self->{where_clause};
undef $self->{where_terms};
undef $self->{values};
}
package SQL::Statement::Aggregate;
use Scalar::Util qw(looks_like_number);
use Params::Util qw(_HASH);
use Clone qw(clone);
sub new
{
my ( $class, $owner, $rows ) = @_;
my $self = {
owner => $owner,
records => $rows,
};
return bless( $self, $class );
}
my $empty_agg = {
uniq => [],
count => 0,
sum => undef,
min => undef,
max => undef,
};
sub do_calc()
{
my $self = $_[0];
foreach my $line ( 0 .. ( scalar( @{ $self->{records} } ) - 1 ) )
{
my $row = $self->{records}->[$line];
my $result = $self->getAffectedResult($row);
foreach my $colidx ( 0 .. ( scalar( @{ $self->{owner}->{columns} } ) - 1 ) )
{
my $coldef = $self->{owner}->{columns}->[$colidx]->{coldef};
my $colval = $row->[$colidx];
if ( $coldef->{type} eq 'setfunc' )
{
if ( $coldef->{distinct} eq 'DISTINCT' )
{
next if defined( $result->{uniq}->[$colidx]->{$colval} );
$result->{uniq}->[$colidx]->{$colval} = 1;
}
$result->{agg}->[$colidx] = clone($empty_agg)
unless ( defined( _HASH( $result->{agg}->[$colidx] ) ) );
my $agg = $result->{agg}->[$colidx];
++$agg->{count};
unless ( defined( $agg->{max} )
&& ( SQL::Statement::_anycmp( $colval, $agg->{max} ) < 0 ) )
{
$agg->{max} = $colval;
}
unless ( defined( $agg->{min} )
&& ( SQL::Statement::_anycmp( $colval, $agg->{min} ) > 0 ) )
{
$agg->{min} = $colval;
}
$agg->{sum} += $colval if ( looks_like_number($colval) );
}
else
{
$result->{pure}->[$colidx] = $colval
unless ( defined( $result->{pure}->[$colidx] ) );
}
}
}
}
sub build_row # (\%)
{
my ( $self, $result ) = @_;
my @row;
foreach my $colidx ( 0 .. ( scalar( @{ $self->{owner}->{columns} } ) - 1 ) )
{
my $coldef = $self->{owner}->{columns}->[$colidx]->{coldef};
if ( $coldef->{type} eq 'setfunc' )
{
if ( $coldef->{name} eq 'COUNT' )
{
push( @row, $result->{agg}->[$colidx]->{count} || 0 );
}
elsif ( $coldef->{name} eq 'MAX' )
{
push( @row, $result->{agg}->[$colidx]->{max} );
}
elsif ( $coldef->{name} eq 'MIN' )
{
push( @row, $result->{agg}->[$colidx]->{min} );
}
elsif ( $coldef->{name} eq 'SUM' )
{
push( @row, $result->{agg}->[$colidx]->{sum} );
}
elsif ( $coldef->{name} eq 'AVG' )
{
my $count = $result->{agg}->[$colidx]->{count};
my $sum = $result->{agg}->[$colidx]->{sum};
my $avg = $sum / $count if ( $count && $sum );
push( @row, $avg );
}
else
{
return $self->{owner}->do_err("Invalid SET FUNCTION '$coldef->{name}'");
}
}
else
{
push( @row, $result->{pure}->[$colidx] );
}
}
return \@row;
}
sub calc()
{
my $self = $_[0];
$self->{final_row} = {};
$self->do_calc();
my $final_row = $self->build_row( $self->{final_row} );
return [$final_row];
}
sub getAffectedResult # (\@)
{
return $_[0]->{final_row};
}
package SQL::Statement::Group;
use vars qw(@ISA);
use Params::Util qw(_HASH);
@ISA = qw(SQL::Statement::Aggregate);
sub new
{
my ( $class, $owner, $rows, $keycols ) = @_;
my $self = $class->SUPER::new( $owner, $rows );
$self->{keycols} = $keycols;
return $self;
}
sub calc()
{
my $self = $_[0];
my @final_table;
$self->do_calc();
if ( scalar( keys( %{ $self->{final_rows} } ) ) )
{
foreach my $key ( keys( %{ $self->{final_rows} } ) )
{
my $final_row = $self->build_row( $self->{final_rows}->{$key} );
push( @final_table, $final_row );
}
}
else
{
my $final_row = $self->build_row( {} );
push( @final_table, $final_row );
}
return \@final_table;
}
sub getAffectedResult # (\@)
{
my ( $self, $row ) = @_;
my $rowkey = join( "\0", @$row[ @{ $self->{keycols} } ] );
$self->{final_rows}->{$rowkey} = {}
unless ( defined( _HASH( $self->{final_rows}->{$rowkey} ) ) );
return $self->{final_rows}->{$rowkey};
}
package SQL::Statement::TempTable;
use vars qw(@ISA);
BEGIN
{
require SQL::Eval;
@SQL::Statement::TempTable::ISA = qw(SQL::Eval::Table);
}
sub new
{
my ( $class, $name, $col_names, $table_cols, $table ) = @_;
my %col_nums;
$col_nums{ $col_names->[$_] } = $_ for ( 0 .. scalar @$col_names - 1 );
my @display_order = @col_nums{@$table_cols};
my $self = {
col_names => $col_names,
table_cols => \@display_order,
col_nums => \%col_nums,
table => $table,
NAME => $name,
rowpos => 0,
maxrow => scalar @$table
};
return $class->SUPER::new($self);
}
sub is_shared($) { $_[0]->{is_shared}->{ $_[1] }; }
sub get_pos() { $_[0]->{rowpos} }
sub column_num($)
{
my ( $s, $col ) = @_;
my $new_col = $s->{col_nums}->{$col};
unless ( defined($new_col) )
{
my @tmp = split( '~', $col );
return unless ( 2 == scalar(@tmp) );
$new_col = lc( $tmp[0] ) . '~' . $tmp[1];
$new_col = $s->{col_nums}->{$new_col};
}
return $new_col;
}
sub fetch_row()
{
return $_[0]->{row} =
( $_[0]->{rowpos} >= $_[0]->{maxrow} )
? undef
: $_[0]->{table}->[ $_[0]->{rowpos}++ ];
}
sub column($) { return $_[0]->{row}->[ $_[0]->{col_nums}->{ $_[1] } ]; }
package SQL::Statement::Order;
sub new ($$)
{
my $proto = shift;
my $self = {@_};
bless( $self, ( ref($proto) || $proto ) );
}
sub table ($) { $_[0]->{col}->table(); }
sub column ($) { $_[0]->{col}->display_name(); }
sub desc ($) { $_[0]->{desc}; }
sub direction ($) { $_[0]->{direction}; }
package SQL::Statement::Limit;
sub new ($$)
{
my ( $proto, $self ) = @_;
bless( $self, ( ref($proto) || $proto ) );
}
#sub limit ($) { shift->{limit}; }
#sub offset ($) { shift->{offset}; }
package SQL::Statement::Param;
sub new
{
my ( $class, $idx ) = @_;
my $self = { 'idx' => $idx };
return bless( $self, $class );
}
sub idx ($) { $_[0]->{idx}; }
package SQL::Statement::Table;
sub new
{
my ( $class, $table_name ) = @_;
if ( $table_name !~ m/"/ )
{
$table_name = lc $table_name;
}
my $self = {
name => $table_name,
};
return bless( $self, $class );
}
sub name { $_[0]->{name} }
1;
__END__
=pod
=head1 NAME
SQL::Statement - SQL parsing and processing engine
=head1 SYNOPSIS
# ... depends on what you want to do, see below
=head1 DESCRIPTION
The SQL::Statement module implements a pure Perl SQL parsing and execution
engine. While it by no means implements full ANSI standard, it does support
many features including column and table aliases, built-in and user-defined
functions, implicit and explicit joins, complex nested search conditions,
and other features.
SQL::Statement is a small embeddable Database Management System
(DBMS). This means that it provides all of the services of a simple
DBMS except that instead of a persistent storage mechanism, it has two
things: 1) an in-memory storage mechanism that allows you to prepare,
execute, and fetch from SQL statements using temporary tables and 2) a
set of software sockets where any author can plug in any storage
mechanism.
There are three main uses for SQL::Statement. One or another (hopefully not
all) may be irrelevant for your needs: 1) to access and manipulate data in
CSV, XML, and other formats 2) to build your own DBD for a new data source
3) to parse and examine the structure of SQL statements.
=head1 INSTALLATION
There are no prerequisites for using this as a standalone parser. If
you want to access persistent stored data, you either need to write a
subclass or use one of the DBI DBD drivers. You can install this
module using CPAN.pm, CPANPLUS.pm, PPM, apt-get, or other packaging
tools or you can download the tar.gz file from CPAN and use the
standard perl mantra:
perl Makefile.PL
make
make test
make install
It works fine on all platforms it has been tested on. On Windows, you
can use ppm or with the mantra use nmake, dmake, or make depending on
which is available.
=head1 USAGE
=head2 How can I use SQL::Statement to access and modify data?
SQL::Statement provides the SQL engine for a number of existing DBI drivers
including L<DBD::CSV>, L<DBD::DBM>, L<DBD::AnyData>, L<DBD::Excel>,
L<DBD::Amazon>, and others.
These modules provide access to Comma Separated Values, Fixed Length, XML,
HTML and many other kinds of text files, to Excel Spreadsheets, to BerkeleyDB
and other DBM formats, and to non-traditional data sources like on-the-fly
Amazon searches.
If you are interested in accessing and manipulating persistent data, you may
not really want to use SQL::Statement directly, but use L<DBI> along with
one of the DBDs mentioned above instead. You will be using SQL::Statement, but
under the hood of the DBD. See L<http://dbi.perl.org> for help with DBI and
see L<SQL::Statement::Syntax> for a description of the SQL syntax that
SQL::Statement provides for these modules and see the documentation for
whichever DBD you are using for additional details.
=head2 How can I use it to parse and examine the structure of SQL statements?
SQL::Statement can be used stand-alone (without a subclass and without
DBI) to parse and examine the structure of SQL statements. See
L<SQL::Statement::Structure> for details.
=head2 How can I use it to embed a SQL engine in a DBD or other module?
SQL::Statement is designed to be easily embedded in other modules and is
especially suited for developing new DBI drivers (DBDs).
See L<SQL::Statement::Embed>.
=head2 What SQL Syntax is supported?
SQL::Statement supports a small but powerful subset of SQL commands.
See L<SQL::Statement::Syntax>.
=head2 How can I extend the supported SQL syntax?
You can modify and extend the SQL syntax either by issuing SQL commands or
by subclassing SQL::Statement. See L<SQL::Statement::Syntax>.
=head1 How can I participate in ongoing development?
SQL::Statement is a large module with many potential future directions.
You are invited to help plan, code, test, document, or kibbitz about these
directions. If you want to join the development team, or just hear more
about the development, write Jeff (<jzuckerATcpan.org>) or Jens
(<rehsackATcpan.org>) a note.
=head1 METHODS
The following methods can or must be overridden by derived classes.
=head2 capability
$has_capability = $h->capability('capability_name');
Returns a true value if the specified capability is available.
Currently no capabilities are defined and this is a placeholder for
future use. It is envisioned it will be used like C<<
SQL::Eval::Table::capability >>.
=head2 open_table
The C<< open_table >> method must be overridden by derived classes to provide
the capability of opening data tables. This is a necessity.
Arguments given to open_table call:
=over 4
=item C<< $data >>
The database memo parameter. See L</execute>.
=item C<< $table >>
The name of the table to open as parsed from SQL statement.
=item C<< $createMode >>
A flag indicating the mode (C<< CREATE TABLE ... >>) the table should
be opened with. Set to a true value in create mode.
=item C<< $lockMode >>
A flag indicating whether the table should be opened for writing (any
other than C<< SELECT ... >>). Set to a true value if the table is to
be opened for write access.
=back
The following methods are required to use SQL::Statement in a DBD (for
example).
=head2 new
Instantiates a new SQL::Statement object.
Arguments:
=over 4
=item C<< $sql >>
The SQL statement for later actions.
=item C<< $parser >>
An instance of a L<SQL::Parser> object or flags for it's instantiation.
If omitted, default flags are used.
=back
When the basic initialization is completed,
C<< $self->prepare($sql, $parser) >> is invoked.
=head2 prepare
Prepares SQL::Statement to execute a SQL statement.
Arguments:
=over 4
=item C<< $sql >>
The SQL statement to parse and prepare.
=item C<< $parser >>
Instance of a L<SQL::Parser> object to parse the provided SQL statement.
=back
=head2 execute
Executes a prepared statement.
Arguments:
=over 4
=item C<< $data >>
Memo field passed through to calls of the instantiated C<< $table >>
objects or C<< open_table >> calls. In C<< CREATE >> with subquery,
C<< $data->{Database} >> must be a DBI database handle object.
=item C<< $params >>
Bound params via DBI ...
=back
=head2 errstr
Gives the error string of the last error, if any.
=head2 fetch_row
Fetches the next row from the result data set (implies removing the fetched
row from the result data set).
=head2 fetch_rows
Fetches all (remaining) rows from the result data set.
=begin undocumented
=head2 _anycmp
=head2 buildColumnObjects
=head2 colname2colnum
=head2 colname2table
=head2 column_names
=head2 columns
=head2 command
=head2 distinct
=head2 do_err
=head2 eval_where
=head2 fetch
=head2 find_join_columns
=head2 full_qualified_column_name
=head2 getColumnObject
=head2 get_user_func_table
=head2 join_2_tables
=head2 limit
=head2 offset
=head2 open_tables
=head2 order
=head2 order_joins
=head2 params
=head2 row_values
=head2 run_functions
=head2 tables
=head2 verify_columns
=head2 verify_expand_column
=head2 verify_order_cols
=head2 where
=head2 where_hash
=head2 column_defs
=end undocumented
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc SQL::Statement
You can also look for information at:
=over 4
=item * RT: CPAN's request tracker
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=SQL-Statement>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/SQL-Statement>
=item * CPAN Ratings
L<http://cpanratings.perl.org/s/SQL-Statement>
=item * CPAN Search
L<http://search.cpan.org/dist/SQL-Statement/>
=back
=head2 Where can I go for help?
For questions about installation or usage, please ask on the
dbi-users@perl.org mailing list (see http://dbi.perl.org) or post a
question on PerlMonks (L<http://www.perlmonks.org/>, where Jeff is
known as jZed). Jens does not visit PerlMonks on a regular basis.
If you have a bug report, a patch or a suggestion, please open a new
report ticket at CPAN (but please check previous reports first in case
your issue has already been addressed). You can mail any of the module
maintainers, but you are more assured of an answer by posting to
the dbi-users list or reporting the issue in RT.
Report tickets should contain a detailed description of the bug or
enhancement request and at least an easily verifiable way of
reproducing the issue or fix. Patches are always welcome, too.
=head2 Where can I go for help with a concrete version?
Bugs and feature requests are accepted against the latest version
only. To get patches for earlier versions, you need to get an
agreement with a developer of your choice - who may or not report the
issue and a suggested fix upstream (depends on the license you have
chosen).
=head2 Business support and maintenance
For business support you can contact Jens via his CPAN email
address rehsackATcpan.org. Please keep in mind that business
support is neither available for free nor are you eligible to
receive any support based on the license distributed with this
package.
=head1 ACKNOWLEDGEMENTS
Jochen Wiedmann created the original module as an XS (C) extension in 1998.
Jeff Zucker took over the maintenance in 2001 and rewrote all of the C
portions in Perl and began extending the SQL support. More recently Ilya
Sterin provided help with SQL::Parser, Tim Bunce provided both general and
specific support, Dan Wright and Dean Arnold have contributed extensively
to the code, and dozens of people from around the world have submitted
patches, bug reports, and suggestions.
In 2008 Jens Rehsack took over the maintenance of the extended module
from Jeff. Together with H.Merijn Brand (who has taken DBD::CSV),
Detlef Wartke and Volker Schubbert (especially between 1.16 developer
versions until 1.22) and all submitters of bug reports via RT a lot of
issues have been fixed.
Thanks to all!
If you're interested in helping develop SQL::Statement or want to use it
with your own modules, feel free to contact Jeff or Jens.
=head1 BUGS AND LIMITATIONS
=over 4
=item *
Currently we treat NULL and '' as the same in AnyData/CSV mode -
eventually fix.
=item *
No nested C-style comments allowed as SQL99 says.
=item *
There are some issues regarding combining outer joins with where
clauses.
=item *
Aggregate functions cannot be used in where clause.
=item *
Some SQL commands/features are not supported (most of them cannot by
design), as C<LOCK TABLE>, using indices, sub-selects etc.
Currently the statement for missing features is: I plan to create a
SQL::Statement v2.00 based on a pure Backus-Naur-Form parser and a
fully object oriented command pattern based engine implementation.
When the time is available, I will do it. Until then bugs will be
fixed or other Perl modules under my maintainership will receive my
time. Features which can be added without deep design changes might be
applied earlier - especially when their addition allows studying
effective ways to implement the feature in upcoming 2.00.
=item *
Some people report that SQL::Statement is slower since the XS parts
were implemented in pure Perl. This might be true, but on the other
hand a large number of features have been added including support for
ANSI SQL 99.
For SQL::Statement 1.xx it's not planned to add new XS parts.
=item *
Wildcards are expanded to lower cased identifiers. This might confuse
some people, but it was easier to implement.
The warning in L<DBI> to never trust the case of returned column names
should be read more often. If you need to rely on identifiers, always
use C<sth-E<gt>{NAME_lc}> or C<sth-E<gt>{NAME_uc}> - never rely on
C<sth-E<gt>{NAME}>:
$dbh->{FetchHashKeyName} = 'NAME_lc';
$sth = $dbh->prepare("SELECT FOO, BAR, ID, NAME, BAZ FROM TABLE");
$sth->execute;
$hash_ref = $sth->fetchall_hashref('id');
print "Name for id 42 is $hash_ref->{42}->{name}\n";
See L<DBI/FetchHashKeyName> for more information.
=item *
Unable to use the same table twice with different aliases. B<Workaround>:
Temporary tables: C<< CREATE TEMP TABLE t_foo AS SELECT * FROM foo >>.
Than both tables can be used independently.
=back
Patches to fix bugs/limitations (or a grant to do it) would be
very welcome. Please note, that any patches B<must> successfully pass
all the C<SQL::Statement>, L<DBD::File> and L<DBD::CSV> tests and must
be a general improvement.
=head1 AUTHOR AND COPYRIGHT
Jochen Wiedmann created the original module as an XS (C) extension in 1998.
Jeff Zucker took over the maintenance in 2001 and rewrote all of the C
portions in perl and began extending the SQL support. Since 2008, Jens
Rehsack is the maintainer.
Copyright (c) 2001,2005 by Jeff Zucker: jzuckerATcpan.org
Copyright (c) 2007-2010 by Jens Rehsack: rehsackATcpan.org
Portions Copyright (C) 1998 by Jochen Wiedmann: jwiedATcpan.org
All rights reserved.
You may distribute this module under the terms of either the GNU
General Public License or the Artistic License, as specified in
the Perl README file.
=cut
|