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 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226
|
# sql.py
# Copyright (C) 2005, 2006, 2007 Michael Bayer mike_mp@zzzcomputing.com
#
# This module is part of SQLAlchemy and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
"""Define the base components of SQL expression trees.
All components are derived from a common base class [sqlalchemy.sql#ClauseElement].
Common behaviors are organized based on class hierarchies, in some cases
via mixins.
All object construction from this package occurs via functions which in some
cases will construct composite ``ClauseElement`` structures together, and
in other cases simply return a single ``ClauseElement`` constructed directly.
The function interface affords a more "DSL-ish" feel to constructing SQL expressions
and also allows future class reorganizations.
Even though classes are not constructed directly from the outside, most
classes which have additional public methods are considered to be public (i.e. have no leading underscore).
Other classes which are "semi-public" are marked with a single leading
underscore; these classes usually have few or no public methods and
are less guaranteed to stay the same in future releases.
"""
from sqlalchemy import util, exceptions, logging
from sqlalchemy import types as sqltypes
import string, re, random, sets
__all__ = ['AbstractDialect', 'Alias', 'ClauseElement', 'ClauseParameters',
'ClauseVisitor', 'ColumnCollection', 'ColumnElement',
'Compiled', 'CompoundSelect', 'Executor', 'FromClause', 'Join',
'Select', 'Selectable', 'TableClause', 'alias', 'and_', 'asc',
'between_', 'between', 'bindparam', 'case', 'cast', 'column', 'delete',
'desc', 'distinct', 'except_', 'except_all', 'exists', 'extract', 'func', 'modifier',
'insert', 'intersect', 'intersect_all', 'join', 'literal',
'literal_column', 'not_', 'null', 'or_', 'outerjoin', 'select',
'subquery', 'table', 'text', 'union', 'union_all', 'update',]
# precedence ordering for common operators. if an operator is not present in this list,
# it will be parenthesized when grouped against other operators
PRECEDENCE = {
'FROM':15,
'*':7,
'/':7,
'%':7,
'+':6,
'-':6,
'ILIKE':5,
'NOT ILIKE':5,
'LIKE':5,
'NOT LIKE':5,
'IN':5,
'NOT IN':5,
'IS':5,
'IS NOT':5,
'=':5,
'!=':5,
'>':5,
'<':5,
'>=':5,
'<=':5,
'BETWEEN':5,
'DISTINCT':5,
'NOT':5,
'AND':3,
'OR':2,
',':-1,
'AS':-1,
'EXISTS':0,
'_smallest': -1000,
'_largest': 1000
}
def desc(column):
"""Return a descending ``ORDER BY`` clause element.
E.g.::
order_by = [desc(table1.mycol)]
"""
return _UnaryExpression(column, modifier="DESC")
def asc(column):
"""Return an ascending ``ORDER BY`` clause element.
E.g.::
order_by = [asc(table1.mycol)]
"""
return _UnaryExpression(column, modifier="ASC")
def outerjoin(left, right, onclause=None, **kwargs):
"""Return an ``OUTER JOIN`` clause element.
The returned object is an instance of [sqlalchemy.sql#Join].
Similar functionality is also available via the ``outerjoin()`` method on any
[sqlalchemy.sql#FromClause].
left
The left side of the join.
right
The right side of the join.
onclause
Optional criterion for the ``ON`` clause, is derived from
foreign key relationships established between left and right
otherwise.
To chain joins together, use the ``join()`` or ``outerjoin()``
methods on the resulting ``Join`` object.
"""
return Join(left, right, onclause, isouter = True, **kwargs)
def join(left, right, onclause=None, **kwargs):
"""Return a ``JOIN`` clause element (regular inner join).
The returned object is an instance of [sqlalchemy.sql#Join].
Similar functionality is also available via the ``join()`` method on any
[sqlalchemy.sql#FromClause].
left
The left side of the join.
right
The right side of the join.
onclause
Optional criterion for the ``ON`` clause, is derived from
foreign key relationships established between left and right
otherwise.
To chain joins together, use the ``join()`` or ``outerjoin()``
methods on the resulting ``Join`` object.
"""
return Join(left, right, onclause, **kwargs)
def select(columns=None, whereclause = None, from_obj = [], **kwargs):
"""Returns a ``SELECT`` clause element.
Similar functionality is also available via the ``select()`` method on any
[sqlalchemy.sql#FromClause].
The returned object is an instance of [sqlalchemy.sql#Select].
All arguments which accept ``ClauseElement`` arguments also
accept string arguments, which will be converted as appropriate
into either ``text()`` or ``literal_column()`` constructs.
columns
A list of ``ClauseElement`` objects, typically ``ColumnElement``
objects or subclasses, which will form
the columns clause of the resulting statement. For all
members which are instances of ``Selectable``, the individual
``ColumnElement`` members of the ``Selectable`` will be
added individually to the columns clause. For example, specifying
a ``Table`` instance will result in all the contained ``Column``
objects within to be added to the columns clause.
This argument is not present on the form of ``select()`` available
on ``Table``.
whereclause
A ``ClauseElement`` expression which will be used to form the
``WHERE`` clause.
from_obj
A list of ``ClauseElement`` objects which will be added to the ``FROM``
clause of the resulting statement. Note that "from" objects
are automatically located within the columns and whereclause
ClauseElements. Use this parameter to explicitly specify
"from" objects which are not automatically locatable.
This could include ``Table`` objects that aren't otherwise
present, or ``Join`` objects whose presence will supercede
that of the ``Table`` objects already located in the other
clauses.
\**kwargs
Additional parameters include:
order_by
a scalar or list of ``ClauseElement`` objects
which will comprise the ``ORDER BY`` clause of the resulting
select.
group_by
a list of ``ClauseElement`` objects which will comprise
the ``GROUP BY`` clause of the resulting select.
having
a ``ClauseElement`` that will comprise the ``HAVING``
clause of the resulting select when ``GROUP BY`` is used.
use_labels=False
when ``True``, the statement will be generated using
labels for each column in the columns clause, which qualify
each column with its parent table's (or aliases) name so
that name conflicts between columns in different tables don't
occur. The format of the label is <tablename>_<column>. The
"c" collection of the resulting ``Select`` object will use these
names as well for targeting column members.
distinct=False
when ``True``, applies a ``DISTINCT`` qualifier to the
columns clause of the resulting statement.
for_update=False
when ``True``, applies ``FOR UPDATE`` to the end of the
resulting statement. Certain database dialects also
support alternate values for this parameter, for example
mysql supports "read" which translates to ``LOCK IN SHARE MODE``,
and oracle supports "nowait" which translates to
``FOR UPDATE NOWAIT``.
bind=None
an ``Engine`` or ``Connection`` instance to which the resulting ``Select``
object will be bound. The ``Select`` object will otherwise
automatically bind to whatever ``Connectable`` instances can be located
within its contained ``ClauseElement`` members.
engine=None
deprecated. a synonym for "bind".
limit=None
a numerical value which usually compiles to a ``LIMIT`` expression
in the resulting select. Databases that don't support ``LIMIT``
will attempt to provide similar functionality.
offset=None
a numerical value which usually compiles to an ``OFFSET`` expression
in the resulting select. Databases that don't support ``OFFSET``
will attempt to provide similar functionality.
scalar=False
when ``True``, indicates that the resulting ``Select`` object
is to be used in the "columns" clause of another select statement,
where the evaluated value of the column is the scalar result of
this statement. Normally, placing any ``Selectable`` within the
columns clause of a ``select()`` call will expand the member
columns of the ``Selectable`` individually.
correlate=True
indicates that this ``Select`` object should have its contained
``FromClause`` elements "correlated" to an enclosing ``Select``
object. This means that any ``ClauseElement`` instance within
the "froms" collection of this ``Select`` which is also present
in the "froms" collection of an enclosing select will not be
rendered in the ``FROM`` clause of this select statement.
"""
return Select(columns, whereclause = whereclause, from_obj = from_obj, **kwargs)
def subquery(alias, *args, **kwargs):
"""Return an [sqlalchemy.sql#Alias] object derived from a [sqlalchemy.sql#Select].
name
alias name
\*args, \**kwargs
all other arguments are delivered to the [sqlalchemy.sql#select()] function.
"""
return Select(*args, **kwargs).alias(alias)
def insert(table, values = None, **kwargs):
"""Return an [sqlalchemy.sql#_Insert] clause element.
Similar functionality is available via the ``insert()``
method on [sqlalchemy.schema#Table].
table
The table to be inserted into.
values
A dictionary which specifies the column specifications of the
``INSERT``, and is optional. If left as None, the column
specifications are determined from the bind parameters used
during the compile phase of the ``INSERT`` statement. If the
bind parameters also are None during the compile phase, then the
column specifications will be generated from the full list of
table columns.
If both `values` and compile-time bind parameters are present, the
compile-time bind parameters override the information specified
within `values` on a per-key basis.
The keys within `values` can be either ``Column`` objects or their
string identifiers. Each key may reference one of:
* a literal data value (i.e. string, number, etc.);
* a Column object;
* a SELECT statement.
If a ``SELECT`` statement is specified which references this
``INSERT`` statement's table, the statement will be correlated
against the ``INSERT`` statement.
"""
return _Insert(table, values, **kwargs)
def update(table, whereclause = None, values = None, **kwargs):
"""Return an [sqlalchemy.sql#_Update] clause element.
Similar functionality is available via the ``update()``
method on [sqlalchemy.schema#Table].
table
The table to be updated.
whereclause
A ``ClauseElement`` describing the ``WHERE`` condition of the
``UPDATE`` statement.
values
A dictionary which specifies the ``SET`` conditions of the
``UPDATE``, and is optional. If left as None, the ``SET``
conditions are determined from the bind parameters used during
the compile phase of the ``UPDATE`` statement. If the bind
parameters also are None during the compile phase, then the
``SET`` conditions will be generated from the full list of table
columns.
If both `values` and compile-time bind parameters are present, the
compile-time bind parameters override the information specified
within `values` on a per-key basis.
The keys within `values` can be either ``Column`` objects or their
string identifiers. Each key may reference one of:
* a literal data value (i.e. string, number, etc.);
* a Column object;
* a SELECT statement.
If a ``SELECT`` statement is specified which references this
``UPDATE`` statement's table, the statement will be correlated
against the ``UPDATE`` statement.
"""
return _Update(table, whereclause, values, **kwargs)
def delete(table, whereclause = None, **kwargs):
"""Return a [sqlalchemy.sql#_Delete] clause element.
Similar functionality is available via the ``delete()``
method on [sqlalchemy.schema#Table].
table
The table to be updated.
whereclause
A ``ClauseElement`` describing the ``WHERE`` condition of the
``UPDATE`` statement.
"""
return _Delete(table, whereclause, **kwargs)
def and_(*clauses):
"""Join a list of clauses together using the ``AND`` operator.
The ``&`` operator is also overloaded on all [sqlalchemy.sql#_CompareMixin]
subclasses to produce the same result.
"""
if len(clauses) == 1:
return clauses[0]
return ClauseList(operator='AND', *clauses)
def or_(*clauses):
"""Join a list of clauses together using the ``OR`` operator.
The ``|`` operator is also overloaded on all [sqlalchemy.sql#_CompareMixin]
subclasses to produce the same result.
"""
if len(clauses) == 1:
return clauses[0]
return ClauseList(operator='OR', *clauses)
def not_(clause):
"""Return a negation of the given clause, i.e. ``NOT(clause)``.
The ``~`` operator is also overloaded on all [sqlalchemy.sql#_CompareMixin]
subclasses to produce the same result.
"""
return clause._negate()
def distinct(expr):
"""return a ``DISTINCT`` clause."""
return _UnaryExpression(expr, operator="DISTINCT")
def between(ctest, cleft, cright):
"""Return a ``BETWEEN`` predicate clause.
Equivalent of SQL ``clausetest BETWEEN clauseleft AND clauseright``.
The ``between()`` method on all [sqlalchemy.sql#_CompareMixin] subclasses
provides similar functionality.
"""
return _BinaryExpression(ctest, ClauseList(_literals_as_binds(cleft, type=ctest.type), _literals_as_binds(cright, type=ctest.type), operator='AND', group=False), 'BETWEEN')
def between_(*args, **kwargs):
"""synonym for [sqlalchemy.sql#between()] (deprecated)."""
return between(*args, **kwargs)
def case(whens, value=None, else_=None):
"""Produce a ``CASE`` statement.
whens
A sequence of pairs to be translated into "when / then" clauses.
value
Optional for simple case statements.
else\_
Optional as well, for case defaults.
"""
whenlist = [ClauseList('WHEN', c, 'THEN', r, operator=None) for (c,r) in whens]
if not else_ is None:
whenlist.append(ClauseList('ELSE', else_, operator=None))
if len(whenlist):
type = list(whenlist[-1])[-1].type
else:
type = None
cc = _CalculatedClause(None, 'CASE', value, type=type, operator=None, group_contents=False, *whenlist + ['END'])
return cc
def cast(clause, totype, **kwargs):
"""Return a ``CAST`` function.
Equivalent of SQL ``CAST(clause AS totype)``.
Use with a [sqlalchemy.types#TypeEngine] subclass, i.e::
cast(table.c.unit_price * table.c.qty, Numeric(10,4))
or::
cast(table.c.timestamp, DATE)
"""
return _Cast(clause, totype, **kwargs)
def extract(field, expr):
"""Return the clause ``extract(field FROM expr)``."""
expr = _BinaryExpression(text(field), expr, "FROM")
return func.extract(expr)
def exists(*args, **kwargs):
"""Return an ``EXISTS`` clause as applied to a [sqlalchemy.sql#Select] object.
The resulting [sqlalchemy.sql#_Exists] object can be executed by itself
or used as a subquery within an enclosing select.
\*args, \**kwargs
all arguments are sent directly to the [sqlalchemy.sql#select()] function
to produce a ``SELECT`` statement.
"""
return _Exists(*args, **kwargs)
def union(*selects, **kwargs):
"""Return a ``UNION`` of multiple selectables.
The returned object is an instance of [sqlalchemy.sql#CompoundSelect].
A similar ``union()`` method is available on all [sqlalchemy.sql#FromClause]
subclasses.
\*selects
a list of [sqlalchemy.sql#Select] instances.
\**kwargs
available keyword arguments are the same as those of [sqlalchemy.sql#select()].
"""
return _compound_select('UNION', *selects, **kwargs)
def union_all(*selects, **kwargs):
"""Return a ``UNION ALL`` of multiple selectables.
The returned object is an instance of [sqlalchemy.sql#CompoundSelect].
A similar ``union_all()`` method is available on all [sqlalchemy.sql#FromClause]
subclasses.
\*selects
a list of [sqlalchemy.sql#Select] instances.
\**kwargs
available keyword arguments are the same as those of [sqlalchemy.sql#select()].
"""
return _compound_select('UNION ALL', *selects, **kwargs)
def except_(*selects, **kwargs):
"""Return an ``EXCEPT`` of multiple selectables.
The returned object is an instance of [sqlalchemy.sql#CompoundSelect].
\*selects
a list of [sqlalchemy.sql#Select] instances.
\**kwargs
available keyword arguments are the same as those of [sqlalchemy.sql#select()].
"""
return _compound_select('EXCEPT', *selects, **kwargs)
def except_all(*selects, **kwargs):
"""Return an ``EXCEPT ALL`` of multiple selectables.
The returned object is an instance of [sqlalchemy.sql#CompoundSelect].
\*selects
a list of [sqlalchemy.sql#Select] instances.
\**kwargs
available keyword arguments are the same as those of [sqlalchemy.sql#select()].
"""
return _compound_select('EXCEPT ALL', *selects, **kwargs)
def intersect(*selects, **kwargs):
"""Return an ``INTERSECT`` of multiple selectables.
The returned object is an instance of [sqlalchemy.sql#CompoundSelect].
\*selects
a list of [sqlalchemy.sql#Select] instances.
\**kwargs
available keyword arguments are the same as those of [sqlalchemy.sql#select()].
"""
return _compound_select('INTERSECT', *selects, **kwargs)
def intersect_all(*selects, **kwargs):
"""Return an ``INTERSECT ALL`` of multiple selectables.
The returned object is an instance of [sqlalchemy.sql#CompoundSelect].
\*selects
a list of [sqlalchemy.sql#Select] instances.
\**kwargs
available keyword arguments are the same as those of [sqlalchemy.sql#select()].
"""
return _compound_select('INTERSECT ALL', *selects, **kwargs)
def alias(selectable, alias=None):
"""Return an [sqlalchemy.sql#Alias] object.
An ``Alias`` represents any [sqlalchemy.sql#FromClause] with
an alternate name assigned within SQL, typically using the ``AS``
clause when generated, e.g. ``SELECT * FROM table AS aliasname``.
Similar functionality is available via the ``alias()`` method
available on all ``FromClause`` subclasses.
selectable
any ``FromClause`` subclass, such as a table, select statement, etc..
alias
string name to be assigned as the alias. If ``None``, a random
name will be generated.
"""
return Alias(selectable, alias=alias)
def literal(value, type=None):
"""Return a literal clause, bound to a bind parameter.
Literal clauses are created automatically when non-
``ClauseElement`` objects (such as strings, ints, dates, etc.) are used in
a comparison operation with a [sqlalchemy.sql#_CompareMixin]
subclass, such as a ``Column`` object. Use this function
to force the generation of a literal clause, which will
be created as a [sqlalchemy.sql#_BindParamClause] with a bound
value.
value
the value to be bound. can be any Python object supported by
the underlying DBAPI, or is translatable via the given type
argument.
type
an optional [sqlalchemy.types#TypeEngine] which will provide
bind-parameter translation for this literal.
"""
return _BindParamClause('literal', value, type=type, unique=True)
def label(name, obj):
"""Return a [sqlalchemy.sql#_Label] object for the given [sqlalchemy.sql#ColumnElement].
A label changes the name of an element in the columns clause
of a ``SELECT`` statement, typically via the ``AS`` SQL keyword.
This functionality is more conveniently available via
the ``label()`` method on ``ColumnElement``.
name
label name
obj
a ``ColumnElement``.
"""
return _Label(name, obj)
def column(text, type=None):
"""Return a textual column clause, as would be in the columns
clause of a ``SELECT`` statement.
The object returned is an instance of [sqlalchemy.sql#_ColumnClause],
which represents the "syntactical" portion of the schema-level
[sqlalchemy.schema#Column] object.
text
the name of the column. Quoting rules will be applied to
the clause like any other column name. For textual column
constructs that are not to be quoted, use the [sqlalchemy.sql#literal_column()]
function.
type
an optional [sqlalchemy.types#TypeEngine] object which will provide
result-set translation for this column.
"""
return _ColumnClause(text, type=type)
def literal_column(text, type=None):
"""Return a textual column clause, as would be in the columns
clause of a ``SELECT`` statement.
The object returned is an instance of [sqlalchemy.sql#_ColumnClause],
which represents the "syntactical" portion of the schema-level
[sqlalchemy.schema#Column] object.
text
the name of the column. Quoting rules will not be applied
to the column. For textual column
constructs that should be quoted like any other column
construct, use the [sqlalchemy.sql#column()]
function.
type
an optional [sqlalchemy.types#TypeEngine] object which will provide
result-set translation for this column.
"""
return _ColumnClause(text, type=type, is_literal=True)
def table(name, *columns):
"""Return a [sqlalchemy.sql#Table] object.
This is a primitive version of the [sqlalchemy.schema#Table] object, which
is a subclass of this object.
"""
return TableClause(name, *columns)
def bindparam(key, value=None, type=None, shortname=None, unique=False):
"""Create a bind parameter clause with the given key.
value
a default value for this bind parameter. a bindparam with a value
is called a ``value-based bindparam``.
shortname
an ``alias`` for this bind parameter. usually used to alias the ``key`` and
``label`` of a column, i.e. ``somecolname`` and ``sometable_somecolname``
type
a sqlalchemy.types.TypeEngine object indicating the type of this bind param, will
invoke type-specific bind parameter processing
unique
if True, bind params sharing the same name will have their underlying ``key`` modified
to a uniquely generated name. mostly useful with value-based bind params.
"""
if isinstance(key, _ColumnClause):
return _BindParamClause(key.name, value, type=key.type, shortname=shortname, unique=unique)
else:
return _BindParamClause(key, value, type=type, shortname=shortname, unique=unique)
def text(text, bind=None, engine=None, *args, **kwargs):
"""Create literal text to be inserted into a query.
When constructing a query from a ``select()``, ``update()``,
``insert()`` or ``delete()``, using plain strings for argument
values will usually result in text objects being created
automatically. Use this function when creating textual clauses
outside of other ``ClauseElement`` objects, or optionally wherever
plain text is to be used.
text
The text of the SQL statement to be created. use ``:<param>``
to specify bind parameters; they will be compiled to their
engine-specific format.
bind
An optional connection or engine to be used for this text query.
engine
deprecated. a synonym for 'bind'.
bindparams
A list of ``bindparam()`` instances which can be used to define
the types and/or initial values for the bind parameters within
the textual statement; the keynames of the bindparams must match
those within the text of the statement. The types will be used
for pre-processing on bind values.
typemap
A dictionary mapping the names of columns represented in the
``SELECT`` clause of the textual statement to type objects,
which will be used to perform post-processing on columns within
the result set (for textual statements that produce result
sets).
"""
return _TextClause(text, engine=engine, bind=bind, *args, **kwargs)
def null():
"""Return a ``_Null`` object, which compiles to ``NULL`` in a sql statement."""
return _Null()
class _FunctionGenerator(object):
"""Generate ``_Function`` objects based on getattr calls."""
def __init__(self, **opts):
self.__names = []
self.opts = opts
def __getattr__(self, name):
if name[-1] == '_':
name = name[0:-1]
f = _FunctionGenerator(**self.opts)
f.__names = list(self.__names) + [name]
return f
def __call__(self, *c, **kwargs):
o = self.opts.copy()
o.update(kwargs)
return _Function(self.__names[-1], packagenames=self.__names[0:-1], *c, **o)
func = _FunctionGenerator()
# TODO: use UnaryExpression for this instead ?
modifier = _FunctionGenerator(group=False)
def _compound_select(keyword, *selects, **kwargs):
return CompoundSelect(keyword, *selects, **kwargs)
def _is_literal(element):
return not isinstance(element, ClauseElement)
def _literals_as_text(element):
if _is_literal(element):
return _TextClause(unicode(element))
else:
return element
def _literals_as_binds(element, name='literal', type=None):
if _is_literal(element):
if element is None:
return null()
else:
return _BindParamClause(name, element, shortname=name, type=type, unique=True)
else:
return element
def is_column(col):
return isinstance(col, ColumnElement)
class AbstractDialect(object):
"""Represent the behavior of a particular database.
Used by ``Compiled`` objects."""
pass
class ClauseParameters(object):
"""Represent a dictionary/iterator of bind parameter key names/values.
Tracks the original [sqlalchemy.sql#_BindParamClause] objects as well as the
keys/position of each parameter, and can return parameters as a
dictionary or a list. Will process parameter values according to
the ``TypeEngine`` objects present in the ``_BindParamClause`` instances.
"""
def __init__(self, dialect, positional=None):
super(ClauseParameters, self).__init__()
self.dialect = dialect
self.binds = {}
self.binds_to_names = {}
self.binds_to_values = {}
self.positional = positional or []
def set_parameter(self, bindparam, value, name):
self.binds[bindparam.key] = bindparam
self.binds[name] = bindparam
self.binds_to_names[bindparam] = name
self.binds_to_values[bindparam] = value
def get_original(self, key):
"""Return the given parameter as it was originally placed in
this ``ClauseParameters`` object, without any ``Type``
conversion."""
return self.binds_to_values[self.binds[key]]
def get_processed(self, key):
bind = self.binds[key]
value = self.binds_to_values[bind]
return bind.typeprocess(value, self.dialect)
def keys(self):
return self.binds_to_names.values()
def __getitem__(self, key):
return self.get_processed(key)
def __contains__(self, key):
return key in self.binds
def set_value(self, key, value):
bind = self.binds[key]
self.binds_to_values[bind] = value
def get_original_dict(self):
return dict([(self.binds_to_names[b], self.binds_to_values[b]) for b in self.binds_to_names.keys()])
def get_raw_list(self):
return [self.get_processed(key) for key in self.positional]
def get_raw_dict(self):
d = {}
for k in self.binds_to_names.values():
d[k] = self.get_processed(k)
return d
def __repr__(self):
return self.__class__.__name__ + ":" + repr(self.get_original_dict())
class ClauseVisitor(object):
"""A class that knows how to traverse and visit
``ClauseElements``.
Each ``ClauseElement``'s accept_visitor() method will call a
corresponding visit_XXXX() method here. Traversal of a
hierarchy of ``ClauseElements`` is achieved via the
``traverse()`` method, which is passed the lead
``ClauseElement``.
By default, ``ClauseVisitor`` traverses all elements
fully. Options can be specified at the class level via the
``__traverse_options__`` dictionary which will be passed
to the ``get_children()`` method of each ``ClauseElement``;
these options can indicate modifications to the set of
elements returned, such as to not return column collections
(column_collections=False) or to return Schema-level items
(schema_visitor=True)."""
__traverse_options__ = {}
def traverse(self, obj, stop_on=None):
stack = [obj]
traversal = []
while len(stack) > 0:
t = stack.pop()
if stop_on is None or t not in stop_on:
traversal.insert(0, t)
for c in t.get_children(**self.__traverse_options__):
stack.append(c)
for target in traversal:
v = self
while v is not None:
target.accept_visitor(v)
v = getattr(v, '_next', None)
return obj
def chain(self, visitor):
"""'chain' an additional ClauseVisitor onto this ClauseVisitor.
the chained visitor will receive all visit events after this one."""
tail = self
while getattr(tail, '_next', None) is not None:
tail = tail._next
tail._next = visitor
return self
def visit_column(self, column):
pass
def visit_table(self, table):
pass
def visit_fromclause(self, fromclause):
pass
def visit_bindparam(self, bindparam):
pass
def visit_textclause(self, textclause):
pass
def visit_compound(self, compound):
pass
def visit_compound_select(self, compound):
pass
def visit_binary(self, binary):
pass
def visit_unary(self, unary):
pass
def visit_alias(self, alias):
pass
def visit_select(self, select):
pass
def visit_join(self, join):
pass
def visit_null(self, null):
pass
def visit_clauselist(self, list):
pass
def visit_calculatedclause(self, calcclause):
pass
def visit_grouping(self, gr):
pass
def visit_function(self, func):
pass
def visit_cast(self, cast):
pass
def visit_label(self, label):
pass
def visit_typeclause(self, typeclause):
pass
class LoggingClauseVisitor(ClauseVisitor):
"""extends ClauseVisitor to include debug logging of all traversal.
To install this visitor, set logging.DEBUG for
'sqlalchemy.sql.ClauseVisitor' **before** you import the
sqlalchemy.sql module.
"""
def traverse(self, obj, stop_on=None):
stack = [(obj, "")]
traversal = []
while len(stack) > 0:
(t, indent) = stack.pop()
if stop_on is None or t not in stop_on:
traversal.insert(0, (t, indent))
for c in t.get_children(**self.__traverse_options__):
stack.append((c, indent + " "))
for (target, indent) in traversal:
self.logger.debug(indent + repr(target))
v = self
while v is not None:
target.accept_visitor(v)
v = getattr(v, '_next', None)
return obj
LoggingClauseVisitor.logger = logging.class_logger(ClauseVisitor)
if logging.is_debug_enabled(LoggingClauseVisitor.logger):
ClauseVisitor=LoggingClauseVisitor
class NoColumnVisitor(ClauseVisitor):
"""a ClauseVisitor that will not traverse the exported Column
collections on Table, Alias, Select, and CompoundSelect objects
(i.e. their 'columns' or 'c' attribute).
this is useful because most traversals don't need those columns, or
in the case of ANSICompiler it traverses them explicitly; so
skipping their traversal here greatly cuts down on method call overhead.
"""
__traverse_options__ = {'column_collections':False}
class Executor(object):
"""Interface representing a "thing that can produce Compiled objects
and execute them"."""
def execute_compiled(self, compiled, parameters, echo=None, **kwargs):
"""Execute a Compiled object."""
raise NotImplementedError()
def compiler(self, statement, parameters, **kwargs):
"""Return a Compiled object for the given statement and parameters."""
raise NotImplementedError()
class Compiled(ClauseVisitor):
"""Represent a compiled SQL expression.
The ``__str__`` method of the ``Compiled`` object should produce
the actual text of the statement. ``Compiled`` objects are
specific to their underlying database dialect, and also may
or may not be specific to the columns referenced within a
particular set of bind parameters. In no case should the
``Compiled`` object be dependent on the actual values of those
bind parameters, even though it may reference those values as
defaults.
"""
def __init__(self, dialect, statement, parameters, bind=None, engine=None):
"""Construct a new ``Compiled`` object.
statement
``ClauseElement`` to be compiled.
parameters
Optional dictionary indicating a set of bind parameters
specified with this ``Compiled`` object. These parameters
are the *default* values corresponding to the
``ClauseElement``'s ``_BindParamClauses`` when the
``Compiled`` is executed. In the case of an ``INSERT`` or
``UPDATE`` statement, these parameters will also result in
the creation of new ``_BindParamClause`` objects for each
key and will also affect the generated column list in an
``INSERT`` statement and the ``SET`` clauses of an
``UPDATE`` statement. The keys of the parameter dictionary
can either be the string names of columns or
``_ColumnClause`` objects.
bind
optional engine or connection which will be bound to the
compiled object.
engine
deprecated, a synonym for 'bind'
"""
self.dialect = dialect
self.statement = statement
self.parameters = parameters
self.bind = bind or engine
self.can_execute = statement.supports_execution()
def compile(self):
self.traverse(self.statement)
self.after_compile()
def __str__(self):
"""Return the string text of the generated SQL statement."""
raise NotImplementedError()
def get_params(self, **params):
"""Deprecated. use construct_params(). (supports unicode names)
"""
return self.construct_params(params)
def construct_params(self, params):
"""Return the bind params for this compiled object.
Will start with the default parameters specified when this
``Compiled`` object was first constructed, and will override
those values with those sent via `**params`, which are
key/value pairs. Each key should match one of the
``_BindParamClause`` objects compiled into this object; either
the `key` or `shortname` property of the ``_BindParamClause``.
"""
raise NotImplementedError()
def execute(self, *multiparams, **params):
"""Execute this compiled object."""
e = self.bind
if e is None:
raise exceptions.InvalidRequestError("This Compiled object is not bound to any Engine or Connection.")
return e.execute_compiled(self, *multiparams, **params)
def scalar(self, *multiparams, **params):
"""Execute this compiled object and return the result's scalar value."""
return self.execute(*multiparams, **params).scalar()
class ClauseElement(object):
"""Base class for elements of a programmatically constructed SQL
expression.
"""
def _get_from_objects(self):
"""Return objects represented in this ``ClauseElement`` that
should be added to the ``FROM`` list of a query, when this
``ClauseElement`` is placed in the column clause of a
``Select`` statement.
"""
raise NotImplementedError(repr(self))
def _hide_froms(self):
"""Return a list of ``FROM`` clause elements which this
``ClauseElement`` replaces.
"""
return []
def compare(self, other):
"""Compare this ClauseElement to the given ClauseElement.
Subclasses should override the default behavior, which is a
straight identity comparison.
"""
return self is other
def accept_visitor(self, visitor):
"""Accept a ``ClauseVisitor`` and call the appropriate
``visit_xxx`` method.
"""
raise NotImplementedError(repr(self))
def get_children(self, **kwargs):
"""return immediate child elements of this ``ClauseElement``.
this is used for visit traversal.
\**kwargs may contain flags that change the collection
that is returned, for example to return a subset of items
in order to cut down on larger traversals, or to return
child items from a different context (such as schema-level
collections instead of clause-level)."""
return []
def self_group(self, against=None):
return self
def supports_execution(self):
"""Return True if this clause element represents a complete
executable statement.
"""
return False
def copy_container(self):
"""Return a copy of this ``ClauseElement``, if this
``ClauseElement`` contains other ``ClauseElements``.
If this ``ClauseElement`` is not a container, it should return
self. This is used to create copies of expression trees that
still reference the same *leaf nodes*. The new structure can
then be restructured without affecting the original.
"""
return self
def _find_engine(self):
"""Default strategy for locating an engine within the clause element.
Relies upon a local engine property, or looks in the *from*
objects which ultimately have to contain Tables or
TableClauses.
"""
try:
if self._bind is not None:
return self._bind
except AttributeError:
pass
for f in self._get_from_objects():
if f is self:
continue
engine = f.bind
if engine is not None:
return engine
else:
return None
bind = property(lambda s:s._find_engine(), doc="""Returns the Engine or Connection to which this ClauseElement is bound, or None if none found.""")
engine = bind
def execute(self, *multiparams, **params):
"""Compile and execute this ``ClauseElement``."""
if len(multiparams):
compile_params = multiparams[0]
else:
compile_params = params
return self.compile(bind=self.bind, parameters=compile_params).execute(*multiparams, **params)
def scalar(self, *multiparams, **params):
"""Compile and execute this ``ClauseElement``, returning the
result's scalar representation.
"""
return self.execute(*multiparams, **params).scalar()
def compile(self, bind=None, engine=None, parameters=None, compiler=None, dialect=None):
"""Compile this SQL expression.
Uses the given ``Compiler``, or the given ``AbstractDialect``
or ``Engine`` to create a ``Compiler``. If no `compiler`
arguments are given, tries to use the underlying ``Engine`` this
``ClauseElement`` is bound to to create a ``Compiler``, if any.
Finally, if there is no bound ``Engine``, uses an
``ANSIDialect`` to create a default ``Compiler``.
`parameters` is a dictionary representing the default bind
parameters to be used with the statement. If `parameters` is
a list, it is assumed to be a list of dictionaries and the
first dictionary in the list is used with which to compile
against.
The bind parameters can in some cases determine the output of
the compilation, such as for ``UPDATE`` and ``INSERT``
statements the bind parameters that are present determine the
``SET`` and ``VALUES`` clause of those statements.
"""
if (isinstance(parameters, list) or isinstance(parameters, tuple)):
parameters = parameters[0]
if compiler is None:
if dialect is not None:
compiler = dialect.compiler(self, parameters)
elif bind is not None:
compiler = bind.compiler(self, parameters)
elif engine is not None:
compiler = engine.compiler(self, parameters)
elif self.bind is not None:
compiler = self.bind.compiler(self, parameters)
if compiler is None:
import sqlalchemy.ansisql as ansisql
compiler = ansisql.ANSIDialect().compiler(self, parameters=parameters)
compiler.compile()
return compiler
def __str__(self):
return unicode(self.compile()).encode('ascii', 'backslashreplace')
def __and__(self, other):
return and_(self, other)
def __or__(self, other):
return or_(self, other)
def __invert__(self):
return self._negate()
def _negate(self):
return _UnaryExpression(self.self_group(against="NOT"), operator="NOT", negate=None)
class _CompareMixin(object):
"""Defines comparison operations for ``ClauseElement`` instances.
This is a mixin class that adds the capability to produce ``ClauseElement``
instances based on regular Python operators.
These operations are achieved using Python's operator overload methods
(i.e. ``__eq__()``, ``__ne__()``, etc.
Overridden operators include all comparison operators (i.e. '==', '!=', '<'),
math operators ('+', '-', '*', etc), the '&' and '|' operators which evaluate
to ``AND`` and ``OR`` respectively.
Other methods exist to create additional SQL clauses such as ``IN``, ``LIKE``,
``DISTINCT``, etc.
"""
def __lt__(self, other):
return self._compare('<', other)
def __le__(self, other):
return self._compare('<=', other)
def __eq__(self, other):
return self._compare('=', other)
def __ne__(self, other):
return self._compare('!=', other)
def __gt__(self, other):
return self._compare('>', other)
def __ge__(self, other):
return self._compare('>=', other)
def like(self, other):
"""produce a ``LIKE`` clause."""
return self._compare('LIKE', other)
def in_(self, *other):
"""produce an ``IN`` clause."""
if len(other) == 0:
return _Grouping(case([(self.__eq__(None), text('NULL'))], else_=text('0')).__eq__(text('1')))
elif len(other) == 1:
o = other[0]
if _is_literal(o) or isinstance( o, _CompareMixin):
return self.__eq__( o) #single item -> ==
else:
assert hasattr( o, '_selectable') #better check?
return self._compare( 'IN', o, negate='NOT IN') #single selectable
args = []
for o in other:
if not _is_literal(o):
if not isinstance( o, _CompareMixin):
raise exceptions.InvalidRequestError( "in() function accepts either non-selectable values, or a single selectable: "+repr(o) )
else:
o = self._bind_param(o)
args.append(o)
return self._compare( 'IN', ClauseList(*args).self_group(against='IN'), negate='NOT IN')
def startswith(self, other):
"""produce the clause ``LIKE '<other>%'``"""
perc = isinstance(other,(str,unicode)) and '%' or literal('%',type= sqltypes.String)
return self._compare('LIKE', other + perc)
def endswith(self, other):
"""produce the clause ``LIKE '%<other>'``"""
if isinstance(other,(str,unicode)): po = '%' + other
else:
po = literal('%', type= sqltypes.String) + other
po.type = sqltypes.to_instance( sqltypes.String) #force!
return self._compare('LIKE', po)
def __radd__(self, other):
return self._bind_param(other)._operate('+', self)
def __rsub__(self, other):
return self._bind_param(other)._operate('-', self)
def __rmul__(self, other):
return self._bind_param(other)._operate('*', self)
def __rdiv__(self, other):
return self._bind_param(other)._operate('/', self)
def label(self, name):
"""produce a column label, i.e. ``<columnname> AS <name>``"""
return _Label(name, self, self.type)
def distinct(self):
"""produce a DISTINCT clause, i.e. ``DISTINCT <columnname>``"""
return _UnaryExpression(self, operator="DISTINCT")
def between(self, cleft, cright):
"""produce a BETWEEN clause, i.e. ``<column> BETWEEN <cleft> AND <cright>``"""
return _BinaryExpression(self, ClauseList(self._check_literal(cleft), self._check_literal(cright), operator='AND', group=False), 'BETWEEN')
def op(self, operator):
"""produce a generic operator function.
e.g.
somecolumn.op("*")(5)
produces
somecolumn * 5
operator
a string which will be output as the infix operator
between this ``ClauseElement`` and the expression
passed to the generated function.
"""
return lambda other: self._operate(operator, other)
# and here come the math operators:
def __add__(self, other):
return self._operate('+', other)
def __sub__(self, other):
return self._operate('-', other)
def __mul__(self, other):
return self._operate('*', other)
def __div__(self, other):
return self._operate('/', other)
def __mod__(self, other):
return self._operate('%', other)
def __truediv__(self, other):
return self._operate('/', other)
def _bind_param(self, obj):
return _BindParamClause('literal', obj, shortname=None, type=self.type, unique=True)
def _check_literal(self, other):
if _is_literal(other):
return self._bind_param(other)
else:
return other
def _compare(self, operator, obj, negate=None):
if obj is None or isinstance(obj, _Null):
if operator == '=':
return _BinaryExpression(self._compare_self(), null(), 'IS', negate='IS NOT')
elif operator == '!=':
return _BinaryExpression(self._compare_self(), null(), 'IS NOT', negate='IS')
else:
raise exceptions.ArgumentError("Only '='/'!=' operators can be used with NULL")
else:
obj = self._check_literal(obj)
return _BinaryExpression(self._compare_self(), obj, operator, type=self._compare_type(obj), negate=negate)
def _operate(self, operator, obj):
if _is_literal(obj):
obj = self._bind_param(obj)
return _BinaryExpression(self._compare_self(), obj, operator, type=self._compare_type(obj))
def _compare_self(self):
"""Allow ``ColumnImpl`` to return its ``Column`` object for
usage in ``ClauseElements``, all others to just return self.
"""
return self
def _compare_type(self, obj):
"""Allow subclasses to override the type used in constructing
``_BinaryExpression`` objects.
Default return value is the type of the given object.
"""
return obj.type
class Selectable(ClauseElement):
"""Represent a column list-holding object.
this is the common base class of [sqlalchemy.sql#ColumnElement]
and [sqlalchemy.sql#FromClause]. The reason ``ColumnElement``
is marked as a "list-holding" object is so that it can be treated
similarly to ``FromClause`` in column-selection scenarios; it
contains a list of columns consisting of itself.
"""
columns = util.NotImplProperty("""a [sqlalchemy.sql#ColumnCollection] containing ``ColumnElement`` instances.""")
def _selectable(self):
return self
def accept_visitor(self, visitor):
raise NotImplementedError(repr(self))
def select(self, whereclauses = None, **params):
return select([self], whereclauses, **params)
def _group_parenthesized(self):
"""Indicate if this ``Selectable`` requires parenthesis when
grouped into a compound statement.
"""
return True
class ColumnElement(Selectable, _CompareMixin):
"""Represent an element that is useable within the
"column clause" portion of a ``SELECT`` statement.
This includes columns associated with tables, aliases,
and subqueries, expressions, function calls, SQL keywords
such as ``NULL``, literals, etc. ``ColumnElement`` is the
ultimate base class for all such elements.
``ColumnElement`` supports the ability to be a *proxy* element,
which indicates that the ``ColumnElement`` may be associated with
a ``Selectable`` which was derived from another ``Selectable``.
An example of a "derived" ``Selectable`` is an ``Alias`` of
a ``Table``.
a ``ColumnElement``, by subclassing the ``_CompareMixin`` mixin
class, provides the ability to generate new ``ClauseElement``
objects using Python expressions. See the ``_CompareMixin``
docstring for more details.
"""
primary_key = property(lambda self:getattr(self, '_primary_key', False),
doc=\
"""Primary key flag. Indicates if this ``Column`` represents part or
whole of a primary key for its parent table.
""")
foreign_keys = property(lambda self:getattr(self, '_foreign_keys', []),
doc=\
"""Foreign key accessor. References a list of ``ForeignKey`` objects
which each represent a foreign key placed on this column's ultimate
ancestor.
""")
columns = property(lambda self:[self],
doc=\
"""Columns accessor which returns ``self``, to provide compatibility
with ``Selectable`` objects.
""")
def _one_fkey(self):
if len(self._foreign_keys):
return list(self._foreign_keys)[0]
else:
return None
foreign_key = property(_one_fkey)
def _get_orig_set(self):
try:
return self.__orig_set
except AttributeError:
self.__orig_set = util.Set([self])
return self.__orig_set
def _set_orig_set(self, s):
if len(s) == 0:
s.add(self)
self.__orig_set = s
orig_set = property(_get_orig_set, _set_orig_set,
doc=\
"""A Set containing TableClause-bound, non-proxied ColumnElements
for which this ColumnElement is a proxy. In all cases except
for a column proxied from a Union (i.e. CompoundSelect), this
set will be just one element.
""")
def shares_lineage(self, othercolumn):
"""Return True if the given ``ColumnElement`` has a common
ancestor to this ``ColumnElement``.
"""
for c in self.orig_set:
if c in othercolumn.orig_set:
return True
else:
return False
def _make_proxy(self, selectable, name=None):
"""Create a new ``ColumnElement`` representing this
``ColumnElement`` as it appears in the select list of a
descending selectable.
The default implementation returns a ``_ColumnClause`` if a
name is given, else just returns self.
"""
if name is not None:
co = _ColumnClause(name, selectable)
co.orig_set = self.orig_set
selectable.columns[name]= co
return co
else:
return self
class ColumnCollection(util.OrderedProperties):
"""An ordered dictionary that stores a list of ColumnElement
instances.
Overrides the ``__eq__()`` method to produce SQL clauses between
sets of correlated columns.
"""
def __init__(self, *cols):
super(ColumnCollection, self).__init__()
[self.add(c) for c in cols]
def __str__(self):
return repr([str(c) for c in self])
def add(self, column):
"""Add a column to this collection.
The key attribute of the column will be used as the hash key
for this dictionary.
"""
# Allow an aliased column to replace an unaliased column of the
# same name.
if self.has_key(column.name):
other = self[column.name]
if other.name == other.key:
del self[other.name]
self[column.key] = column
def remove(self, column):
del self[column.key]
def extend(self, iter):
for c in iter:
self.add(c)
def __eq__(self, other):
l = []
for c in other:
for local in self:
if c.shares_lineage(local):
l.append(c==local)
return and_(*l)
def __contains__(self, col):
return self.contains_column(col)
def contains_column(self, col):
# have to use a Set here, because it will compare the identity
# of the column, not just using "==" for comparison which will always return a
# "True" value (i.e. a BinaryClause...)
return col in util.Set(self)
class ColumnSet(util.OrderedSet):
def contains_column(self, col):
return col in self
def extend(self, cols):
for col in cols:
self.add(col)
def __add__(self, other):
return list(self) + list(other)
def __eq__(self, other):
l = []
for c in other:
for local in self:
if c.shares_lineage(local):
l.append(c==local)
return and_(*l)
class FromClause(Selectable):
"""Represent an element that can be used within the ``FROM``
clause of a ``SELECT`` statement.
"""
def __init__(self, name=None):
self.name = name
def _get_from_objects(self):
# this could also be [self], at the moment it doesnt matter to the Select object
return []
def default_order_by(self):
return [self.oid_column]
def accept_visitor(self, visitor):
visitor.visit_fromclause(self)
def count(self, whereclause=None, **params):
if len(self.primary_key):
col = list(self.primary_key)[0]
else:
col = list(self.columns)[0]
return select([func.count(col).label('tbl_row_count')], whereclause, from_obj=[self], **params)
def join(self, right, *args, **kwargs):
return Join(self, right, *args, **kwargs)
def outerjoin(self, right, *args, **kwargs):
return Join(self, right, isouter=True, *args, **kwargs)
def alias(self, name=None):
return Alias(self, name)
def named_with_column(self):
"""True if the name of this FromClause may be prepended to a
column in a generated SQL statement.
"""
return False
def _locate_oid_column(self):
"""Subclasses should override this to return an appropriate OID column."""
return None
def _get_oid_column(self):
if not hasattr(self, '_oid_column'):
self._oid_column = self._locate_oid_column()
return self._oid_column
def _get_all_embedded_columns(self):
ret = []
class FindCols(ClauseVisitor):
def visit_column(self, col):
ret.append(col)
FindCols().traverse(self)
return ret
def corresponding_column(self, column, raiseerr=True, keys_ok=False, require_embedded=False):
"""Given a ``ColumnElement``, return the exported
``ColumnElement`` object from this ``Selectable`` which
corresponds to that original ``Column`` via a common
anscestor column.
column
the target ``ColumnElement`` to be matched
raiseerr
if True, raise an error if the given ``ColumnElement``
could not be matched. if False, non-matches will
return None.
keys_ok
if the ``ColumnElement`` cannot be matched, attempt to
match based on the string "key" property of the column
alone. This makes the search much more liberal.
require_embedded
only return corresponding columns for the given
``ColumnElement``, if the given ``ColumnElement`` is
actually present within a sub-element of this
``FromClause``. Normally the column will match if
it merely shares a common anscestor with one of
the exported columns of this ``FromClause``.
"""
if column in self.c:
return column
if require_embedded and column not in util.Set(self._get_all_embedded_columns()):
if not raiseerr:
return None
else:
raise exceptions.InvalidRequestError("Column instance '%s' is not directly present within selectable '%s'" % (str(column), column.table))
for c in column.orig_set:
try:
return self.original_columns[c]
except KeyError:
pass
else:
if keys_ok:
try:
return self.c[column.name]
except KeyError:
pass
if not raiseerr:
return None
else:
raise exceptions.InvalidRequestError("Given column '%s', attached to table '%s', failed to locate a corresponding column from table '%s'" % (str(column), str(getattr(column, 'table', None)), self.name))
def _get_exported_attribute(self, name):
try:
return getattr(self, name)
except AttributeError:
self._export_columns()
return getattr(self, name)
columns = property(lambda s:s._get_exported_attribute('_columns'))
c = property(lambda s:s._get_exported_attribute('_columns'))
primary_key = property(lambda s:s._get_exported_attribute('_primary_key'))
foreign_keys = property(lambda s:s._get_exported_attribute('_foreign_keys'))
original_columns = property(lambda s:s._get_exported_attribute('_orig_cols'), doc=\
"""A dictionary mapping an original Table-bound
column to a proxied column in this FromClause.
""")
oid_column = property(_get_oid_column)
def _export_columns(self, columns=None):
"""Initialize column collections.
The collections include the primary key, foreign keys, list of
all columns, as well as the *_orig_cols* collection which is a
dictionary used to match Table-bound columns to proxied
columns in this ``FromClause``. The columns in each
collection are *proxied* from the columns returned by the
_exportable_columns method, where a *proxied* column maintains
most or all of the properties of its original column, except
its parent ``Selectable`` is this ``FromClause``.
"""
if hasattr(self, '_columns') and columns is None:
# TODO: put a mutex here ? this is a key place for threading probs
return
self._columns = ColumnCollection()
self._primary_key = ColumnSet()
self._foreign_keys = util.Set()
self._orig_cols = {}
if columns is None:
columns = self._adjusted_exportable_columns()
for co in columns:
cp = self._proxy_column(co)
for ci in cp.orig_set:
cx = self._orig_cols.get(ci)
# TODO: the '=' thing here relates to the order of columns as they are placed in the
# "columns" collection of a CompositeSelect, illustrated in test/sql/selectable.SelectableTest.testunion
# make this relationship less brittle
if cx is None or cp._distance <= cx._distance:
self._orig_cols[ci] = cp
if self.oid_column is not None:
for ci in self.oid_column.orig_set:
self._orig_cols[ci] = self.oid_column
def _adjusted_exportable_columns(self):
"""return the list of ColumnElements represented within this FromClause's _exportable_columns"""
export = self._exportable_columns()
for column in export:
try:
s = column._selectable()
except AttributeError:
continue
for co in s.columns:
yield co
def _exportable_columns(self):
return []
def _proxy_column(self, column):
return column._make_proxy(self)
class _BindParamClause(ClauseElement, _CompareMixin):
"""Represent a bind parameter.
Public constructor is the ``bindparam()`` function.
"""
def __init__(self, key, value, shortname=None, type=None, unique=False):
"""Construct a _BindParamClause.
key
the key for this bind param. Will be used in the generated
SQL statement for dialects that use named parameters. This
value may be modified when part of a compilation operation,
if other ``_BindParamClause`` objects exist with the same
key, or if its length is too long and truncation is
required.
value
Initial value for this bind param. This value may be
overridden by the dictionary of parameters sent to statement
compilation/execution.
shortname
Defaults to the key, a *short name* that will also identify
this bind parameter, similar to an alias. the bind
parameter keys sent to a statement compilation or compiled
execution may match either the key or the shortname of the
corresponding ``_BindParamClause`` objects.
type
A ``TypeEngine`` object that will be used to pre-process the
value corresponding to this ``_BindParamClause`` at
execution time.
unique
if True, the key name of this BindParamClause will be
modified if another ``_BindParamClause`` of the same
name already has been located within the containing
``ClauseElement``.
"""
self.key = key
self.value = value
self.shortname = shortname or key
self.unique = unique
self.type = sqltypes.to_instance(type)
def accept_visitor(self, visitor):
visitor.visit_bindparam(self)
def _get_from_objects(self):
return []
def copy_container(self):
return _BindParamClause(self.key, self.value, self.shortname, self.type, unique=self.unique)
def typeprocess(self, value, dialect):
return self.type.dialect_impl(dialect).convert_bind_param(value, dialect)
def compare(self, other):
"""Compare this ``_BindParamClause`` to the given clause.
Since ``compare()`` is meant to compare statement syntax, this
method returns True if the two ``_BindParamClauses`` have just
the same type.
"""
return isinstance(other, _BindParamClause) and other.type.__class__ == self.type.__class__
def __repr__(self):
return "_BindParamClause(%s, %s, type=%s)" % (repr(self.key), repr(self.value), repr(self.type))
class _TypeClause(ClauseElement):
"""Handle a type keyword in a SQL statement.
Used by the ``Case`` statement.
"""
def __init__(self, type):
self.type = type
def accept_visitor(self, visitor):
visitor.visit_typeclause(self)
def _get_from_objects(self):
return []
class _TextClause(ClauseElement):
"""Represent a literal SQL text fragment.
Public constructor is the ``text()`` function.
"""
def __init__(self, text = "", bind=None, engine=None, bindparams=None, typemap=None):
self._bind = bind or engine
self.bindparams = {}
self.typemap = typemap
if typemap is not None:
for key in typemap.keys():
typemap[key] = sqltypes.to_instance(typemap[key])
def repl(m):
self.bindparams[m.group(1)] = bindparam(m.group(1))
return ":%s" % m.group(1)
# scan the string and search for bind parameter names, add them
# to the list of bindparams
self.text = re.compile(r'(?<!:):([\w_]+)', re.S).sub(repl, text)
if bindparams is not None:
for b in bindparams:
self.bindparams[b.key] = b
def _get_type(self):
if self.typemap is not None and len(self.typemap) == 1:
return list(self.typemap)[0]
else:
return None
type = property(_get_type)
columns = property(lambda s:[])
def get_children(self, **kwargs):
return self.bindparams.values()
def accept_visitor(self, visitor):
visitor.visit_textclause(self)
def _get_from_objects(self):
return []
def supports_execution(self):
return True
class _Null(ColumnElement):
"""Represent the NULL keyword in a SQL statement.
Public constructor is the ``null()`` function.
"""
def __init__(self):
self.type = sqltypes.NULLTYPE
def accept_visitor(self, visitor):
visitor.visit_null(self)
def _get_from_objects(self):
return []
class ClauseList(ClauseElement):
"""Describe a list of clauses, separated by an operator.
By default, is comma-separated, such as a column listing.
"""
def __init__(self, *clauses, **kwargs):
self.clauses = []
self.operator = kwargs.pop('operator', ',')
self.group = kwargs.pop('group', True)
self.group_contents = kwargs.pop('group_contents', True)
for c in clauses:
if c is None: continue
self.append(c)
def __iter__(self):
return iter(self.clauses)
def __len__(self):
return len(self.clauses)
def copy_container(self):
clauses = [clause.copy_container() for clause in self.clauses]
return ClauseList(operator=self.operator, *clauses)
def append(self, clause):
# TODO: not sure if i like the 'group_contents' flag. need to define the difference between
# a ClauseList of ClauseLists, and a "flattened" ClauseList of ClauseLists. flatten() method ?
if self.group_contents:
self.clauses.append(_literals_as_text(clause).self_group(against=self.operator))
else:
self.clauses.append(_literals_as_text(clause))
def get_children(self, **kwargs):
return self.clauses
def accept_visitor(self, visitor):
visitor.visit_clauselist(self)
def _get_from_objects(self):
f = []
for c in self.clauses:
f += c._get_from_objects()
return f
def self_group(self, against=None):
if self.group and self.operator != against and PRECEDENCE.get(self.operator, PRECEDENCE['_smallest']) <= PRECEDENCE.get(against, PRECEDENCE['_largest']):
return _Grouping(self)
else:
return self
def compare(self, other):
"""Compare this ``ClauseList`` to the given ``ClauseList``,
including a comparison of all the clause items.
"""
if not isinstance(other, ClauseList) and len(self.clauses) == 1:
return self.clauses[0].compare(other)
elif isinstance(other, ClauseList) and len(self.clauses) == len(other.clauses):
for i in range(0, len(self.clauses)):
if not self.clauses[i].compare(other.clauses[i]):
return False
else:
return self.operator == other.operator
else:
return False
class _CalculatedClause(ColumnElement):
"""Describe a calculated SQL expression that has a type, like ``CASE``.
Extends ``ColumnElement`` to provide column-level comparison
operators.
"""
def __init__(self, name, *clauses, **kwargs):
self.name = name
self.type = sqltypes.to_instance(kwargs.get('type', None))
self._bind = kwargs.get('bind', kwargs.get('engine', None))
self.group = kwargs.pop('group', True)
self.clauses = ClauseList(operator=kwargs.get('operator', None), group_contents=kwargs.get('group_contents', True), *clauses)
if self.group:
self.clause_expr = self.clauses.self_group()
else:
self.clause_expr = self.clauses
key = property(lambda self:self.name or "_calc_")
def copy_container(self):
clauses = [clause.copy_container() for clause in self.clauses]
return _CalculatedClause(type=self.type, bind=self._bind, *clauses)
def get_children(self, **kwargs):
return self.clause_expr,
def accept_visitor(self, visitor):
visitor.visit_calculatedclause(self)
def _get_from_objects(self):
return self.clauses._get_from_objects()
def _bind_param(self, obj):
return _BindParamClause(self.name, obj, type=self.type, unique=True)
def select(self):
return select([self])
def scalar(self):
return select([self]).scalar()
def execute(self):
return select([self]).execute()
def _compare_type(self, obj):
return self.type
class _Function(_CalculatedClause, FromClause):
"""Describe a SQL function.
Extends ``_CalculatedClause``, turn the *clauselist* into function
arguments, also adds a `packagenames` argument.
"""
def __init__(self, name, *clauses, **kwargs):
self.type = sqltypes.to_instance(kwargs.get('type', None))
self.packagenames = kwargs.get('packagenames', None) or []
kwargs['operator'] = ','
self._engine = kwargs.get('engine', None)
_CalculatedClause.__init__(self, name, **kwargs)
for c in clauses:
self.append(c)
key = property(lambda self:self.name)
def append(self, clause):
self.clauses.append(_literals_as_binds(clause, self.name))
def copy_container(self):
clauses = [clause.copy_container() for clause in self.clauses]
return _Function(self.name, type=self.type, packagenames=self.packagenames, bind=self._bind, *clauses)
def accept_visitor(self, visitor):
visitor.visit_function(self)
class _Cast(ColumnElement):
def __init__(self, clause, totype, **kwargs):
if not hasattr(clause, 'label'):
clause = literal(clause)
self.type = sqltypes.to_instance(totype)
self.clause = clause
self.typeclause = _TypeClause(self.type)
self._distance = 0
def get_children(self, **kwargs):
return self.clause, self.typeclause
def accept_visitor(self, visitor):
visitor.visit_cast(self)
def _get_from_objects(self):
return self.clause._get_from_objects()
def _make_proxy(self, selectable, name=None):
if name is not None:
co = _ColumnClause(name, selectable, type=self.type)
co._distance = self._distance + 1
co.orig_set = self.orig_set
selectable.columns[name]= co
return co
else:
return self
class _UnaryExpression(ColumnElement):
def __init__(self, element, operator=None, modifier=None, type=None, negate=None):
self.operator = operator
self.modifier = modifier
self.element = _literals_as_text(element).self_group(against=self.operator or self.modifier)
self.type = sqltypes.to_instance(type)
self.negate = negate
def copy_container(self):
return self.__class__(self.element.copy_container(), operator=self.operator, modifier=self.modifier, type=self.type, negate=self.negate)
def _get_from_objects(self):
return self.element._get_from_objects()
def get_children(self, **kwargs):
return self.element,
def accept_visitor(self, visitor):
visitor.visit_unary(self)
def compare(self, other):
"""Compare this ``_UnaryExpression`` against the given ``ClauseElement``."""
return (
isinstance(other, _UnaryExpression) and self.operator == other.operator and
self.modifier == other.modifier and
self.element.compare(other.element)
)
def _negate(self):
if self.negate is not None:
return _UnaryExpression(self.element, operator=self.negate, negate=self.operator, modifier=self.modifier, type=self.type)
else:
return super(_UnaryExpression, self)._negate()
def self_group(self, against):
if self.operator and PRECEDENCE.get(self.operator, PRECEDENCE['_smallest']) <= PRECEDENCE.get(against, PRECEDENCE['_largest']):
return _Grouping(self)
else:
return self
class _BinaryExpression(ColumnElement):
"""Represent an expression that is ``LEFT <operator> RIGHT``."""
def __init__(self, left, right, operator, type=None, negate=None):
self.left = _literals_as_text(left).self_group(against=operator)
self.right = _literals_as_text(right).self_group(against=operator)
self.operator = operator
self.type = sqltypes.to_instance(type)
self.negate = negate
def copy_container(self):
return self.__class__(self.left.copy_container(), self.right.copy_container(), self.operator)
def _get_from_objects(self):
return self.left._get_from_objects() + self.right._get_from_objects()
def get_children(self, **kwargs):
return self.left, self.right
def accept_visitor(self, visitor):
visitor.visit_binary(self)
def compare(self, other):
"""Compare this ``_BinaryExpression`` against the given ``_BinaryExpression``."""
return (
isinstance(other, _BinaryExpression) and self.operator == other.operator and
(
self.left.compare(other.left) and self.right.compare(other.right)
or (
self.operator in ['=', '!=', '+', '*'] and
self.left.compare(other.right) and self.right.compare(other.left)
)
)
)
def self_group(self, against=None):
# use small/large defaults for comparison so that unknown operators are always parenthesized
if self.operator != against and (PRECEDENCE.get(self.operator, PRECEDENCE['_smallest']) <= PRECEDENCE.get(against, PRECEDENCE['_largest'])):
return _Grouping(self)
else:
return self
def _negate(self):
if self.negate is not None:
return _BinaryExpression(self.left, self.right, self.negate, negate=self.operator, type=self.type)
else:
return super(_BinaryExpression, self)._negate()
class _Exists(_UnaryExpression):
def __init__(self, *args, **kwargs):
kwargs['correlate'] = True
s = select(*args, **kwargs).self_group()
_UnaryExpression.__init__(self, s, operator="EXISTS")
def _hide_froms(self):
return self._get_from_objects()
class Join(FromClause):
"""represent a ``JOIN`` construct between two ``FromClause``
elements.
the public constructor function for ``Join`` is the module-level
``join()`` function, as well as the ``join()`` method available
off all ``FromClause`` subclasses.
"""
def __init__(self, left, right, onclause=None, isouter = False):
self.left = left._selectable()
self.right = right._selectable()
if onclause is None:
self.onclause = self._match_primaries(self.left, self.right)
else:
self.onclause = onclause
self.isouter = isouter
self.__folded_equivalents = None
self._init_primary_key()
name = property(lambda s: "Join object on " + s.left.name + " " + s.right.name)
encodedname = property(lambda s: s.name.encode('ascii', 'backslashreplace'))
def _init_primary_key(self):
from sqlalchemy import schema
pkcol = util.Set([c for c in self._adjusted_exportable_columns() if c.primary_key])
equivs = {}
def add_equiv(a, b):
for x, y in ((a, b), (b, a)):
if x in equivs:
equivs[x].add(y)
else:
equivs[x] = util.Set([y])
class BinaryVisitor(ClauseVisitor):
def visit_binary(self, binary):
if binary.operator == '=' and isinstance(binary.left, schema.Column) and isinstance(binary.right, schema.Column):
add_equiv(binary.left, binary.right)
BinaryVisitor().traverse(self.onclause)
for col in pkcol:
for fk in col.foreign_keys:
if fk.column in pkcol:
add_equiv(col, fk.column)
omit = util.Set()
for col in pkcol:
p = col
for c in equivs.get(col, util.Set()):
if p.references(c) or (c.primary_key and not p.primary_key):
omit.add(p)
p = c
self.__primary_key = ColumnSet([c for c in self._adjusted_exportable_columns() if c.primary_key and c not in omit])
primary_key = property(lambda s:s.__primary_key)
def _locate_oid_column(self):
return self.left.oid_column
def _exportable_columns(self):
return [c for c in self.left.columns] + [c for c in self.right.columns]
def _proxy_column(self, column):
self._columns[column._label] = column
for f in column.foreign_keys:
self._foreign_keys.add(f)
return column
def _match_primaries(self, primary, secondary):
crit = []
constraints = util.Set()
for fk in secondary.foreign_keys:
if fk.references(primary):
crit.append(primary.corresponding_column(fk.column) == fk.parent)
constraints.add(fk.constraint)
self.foreignkey = fk.parent
if primary is not secondary:
for fk in primary.foreign_keys:
if fk.references(secondary):
crit.append(secondary.corresponding_column(fk.column) == fk.parent)
constraints.add(fk.constraint)
self.foreignkey = fk.parent
if len(crit) == 0:
raise exceptions.ArgumentError("Can't find any foreign key relationships "
"between '%s' and '%s'" % (primary.name, secondary.name))
elif len(constraints) > 1:
raise exceptions.ArgumentError("Can't determine join between '%s' and '%s'; "
"tables have more than one foreign key "
"constraint relationship between them. "
"Please specify the 'onclause' of this "
"join explicitly." % (primary.name, secondary.name))
elif len(crit) == 1:
return (crit[0])
else:
return and_(*crit)
def _group_parenthesized(self):
return True
def _get_folded_equivalents(self, equivs=None):
if self.__folded_equivalents is not None:
return self.__folded_equivalents
if equivs is None:
equivs = util.Set()
class LocateEquivs(NoColumnVisitor):
def visit_binary(self, binary):
if binary.operator == '=' and binary.left.name == binary.right.name:
equivs.add(binary.right)
equivs.add(binary.left)
LocateEquivs().traverse(self.onclause)
collist = []
if isinstance(self.left, Join):
left = self.left._get_folded_equivalents(equivs)
else:
left = list(self.left.columns)
if isinstance(self.right, Join):
right = self.right._get_folded_equivalents(equivs)
else:
right = list(self.right.columns)
used = util.Set()
for c in left + right:
if c in equivs:
if c.name not in used:
collist.append(c)
used.add(c.name)
else:
collist.append(c)
self.__folded_equivalents = collist
return self.__folded_equivalents
folded_equivalents = property(_get_folded_equivalents, doc="Returns the column list of this Join with all equivalently-named, "
"equated columns folded into one column, where 'equated' means they are "
"equated to each other in the ON clause of this join.")
def select(self, whereclause = None, fold_equivalents=False, **kwargs):
"""Create a ``Select`` from this ``Join``.
whereclause
the WHERE criterion that will be sent to the ``select()`` function
fold_equivalents
based on the join criterion of this ``Join``, do not include repeat
column names in the column list of the resulting select, for columns that
are calculated to be "equivalent" based on the join criterion of this
``Join``. this will recursively apply to any joins directly nested by
this one as well.
\**kwargs
all other kwargs are sent to the underlying ``select()`` function.
See the ``select()`` module level function for details.
"""
if fold_equivalents:
collist = self.folded_equivalents
else:
collist = [self.left, self.right]
return select(collist, whereclause, from_obj=[self], **kwargs)
def get_children(self, **kwargs):
return self.left, self.right, self.onclause
def accept_visitor(self, visitor):
visitor.visit_join(self)
engine = property(lambda s:s.left.engine or s.right.engine)
def alias(self, name=None):
"""Create a ``Select`` out of this ``Join`` clause and return an ``Alias`` of it.
The ``Select`` is not correlating.
"""
return self.select(use_labels=True, correlate=False).alias(name)
def _hide_froms(self):
return self.left._get_from_objects() + self.right._get_from_objects()
def _get_from_objects(self):
return [self] + self.onclause._get_from_objects() + self.left._get_from_objects() + self.right._get_from_objects()
class Alias(FromClause):
"""represent an alias, as typically applied to any
table or sub-select within a SQL statement using the
``AS`` keyword (or without the keyword on certain databases
such as Oracle).
this object is constructed from the ``alias()`` module level function
as well as the ``alias()`` method available on all ``FromClause``
subclasses.
"""
def __init__(self, selectable, alias=None):
baseselectable = selectable
while isinstance(baseselectable, Alias):
baseselectable = baseselectable.selectable
self.original = baseselectable
self.selectable = selectable
if alias is None:
if self.original.named_with_column():
alias = getattr(self.original, 'name', None)
if alias is None:
alias = 'anon'
elif len(alias) > 15:
alias = alias[0:15]
alias = alias + "_" + hex(random.randint(0, 65535))[2:]
self.name = alias
self.encodedname = alias.encode('ascii', 'backslashreplace')
self.case_sensitive = getattr(baseselectable, "case_sensitive", True)
def supports_execution(self):
return self.original.supports_execution()
def _locate_oid_column(self):
if self.selectable.oid_column is not None:
return self.selectable.oid_column._make_proxy(self)
else:
return None
def named_with_column(self):
return True
def _exportable_columns(self):
#return self.selectable._exportable_columns()
return self.selectable.columns
def get_children(self, **kwargs):
for c in self.c:
yield c
yield self.selectable
def accept_visitor(self, visitor):
visitor.visit_alias(self)
def _get_from_objects(self):
return [self]
def _group_parenthesized(self):
return False
bind = property(lambda s: s.selectable.bind)
engine = bind
class _Grouping(ColumnElement):
def __init__(self, elem):
self.elem = elem
self.type = getattr(elem, 'type', None)
key = property(lambda s: s.elem.key)
_label = property(lambda s: s.elem._label)
orig_set = property(lambda s:s.elem.orig_set)
def copy_container(self):
return _Grouping(self.elem.copy_container())
def accept_visitor(self, visitor):
visitor.visit_grouping(self)
def get_children(self, **kwargs):
return self.elem,
def _hide_froms(self):
return self.elem._hide_froms()
def _get_from_objects(self):
return self.elem._get_from_objects()
def __getattr__(self, attr):
return getattr(self.elem, attr)
class _Label(ColumnElement):
"""represent a label, as typically applied to any column-level element
using the ``AS`` sql keyword.
this object is constructed from the ``label()`` module level function
as well as the ``label()`` method available on all ``ColumnElement``
subclasses.
"""
def __init__(self, name, obj, type=None):
self.name = name
while isinstance(obj, _Label):
obj = obj.obj
self.obj = obj.self_group(against='AS')
self.case_sensitive = getattr(obj, "case_sensitive", True)
self.type = sqltypes.to_instance(type or getattr(obj, 'type', None))
key = property(lambda s: s.name)
_label = property(lambda s: s.name)
orig_set = property(lambda s:s.obj.orig_set)
def _compare_self(self):
return self.obj
def get_children(self, **kwargs):
return self.obj,
def accept_visitor(self, visitor):
visitor.visit_label(self)
def _get_from_objects(self):
return self.obj._get_from_objects()
def _hide_froms(self):
return self.obj._hide_froms()
def _make_proxy(self, selectable, name = None):
if isinstance(self.obj, Selectable):
return self.obj._make_proxy(selectable, name=self.name)
else:
return column(self.name)._make_proxy(selectable=selectable)
legal_characters = util.Set(string.ascii_letters + string.digits + '_')
class _ColumnClause(ColumnElement):
"""Represents a generic column expression from any textual string.
This includes columns associated with tables, aliases and select
statements, but also any arbitrary text. May or may not be bound
to an underlying ``Selectable``. ``_ColumnClause`` is usually
created publically via the ``column()`` function or the
``column_literal()`` function.
text
the text of the element.
selectable
parent selectable.
type
``TypeEngine`` object which can associate this ``_ColumnClause``
with a type.
case_sensitive
defines whether identifier quoting rules will be applied to the
generated text of this ``_ColumnClause`` so that it is identified in
a case-sensitive manner.
is_literal
if True, the ``_ColumnClause`` is assumed to be an exact expression
that will be delivered to the output with no quoting rules applied
regardless of case sensitive settings. the ``column_literal()`` function is
usually used to create such a ``_ColumnClause``.
"""
def __init__(self, text, selectable=None, type=None, _is_oid=False, case_sensitive=True, is_literal=False):
self.key = self.name = text
self.encodedname = isinstance(self.name, unicode) and self.name.encode('ascii', 'backslashreplace') or self.name
self.table = selectable
self.type = sqltypes.to_instance(type)
self._is_oid = _is_oid
self._distance = 0
self.__label = None
self.case_sensitive = case_sensitive
self.is_literal = is_literal
def _get_label(self):
"""Generate a 'label' for this column.
The label is a product of the parent table name and column
name, and is treated as a unique identifier of this ``Column``
across all ``Tables`` and derived selectables for a particular
metadata collection.
"""
# for a "literal" column, we've no idea what the text is
# therefore no 'label' can be automatically generated
if self.is_literal:
return None
if self.__label is None:
if self.table is not None and self.table.named_with_column():
self.__label = self.table.name + "_" + self.name
counter = 1
while self.table.c.has_key(self.__label):
self.__label = self.__label + "_%d" % counter
counter += 1
else:
self.__label = self.name
self.__label = "".join([x for x in self.__label if x in legal_characters])
return self.__label
is_labeled = property(lambda self:self.name != list(self.orig_set)[0].name)
_label = property(_get_label)
def label(self, name):
# if going off the "__label" property and its None, we have
# no label; return self
if name is None:
return self
else:
return super(_ColumnClause, self).label(name)
def accept_visitor(self, visitor):
visitor.visit_column(self)
def _get_from_objects(self):
if self.table is not None:
return [self.table]
else:
return []
def _bind_param(self, obj):
return _BindParamClause(self._label, obj, shortname = self.name, type=self.type, unique=True)
def _make_proxy(self, selectable, name = None):
# propigate the "is_literal" flag only if we are keeping our name,
# otherwise its considered to be a label
is_literal = self.is_literal and (name is None or name == self.name)
c = _ColumnClause(name or self.name, selectable=selectable, _is_oid=self._is_oid, type=self.type, is_literal=is_literal)
c.orig_set = self.orig_set
c._distance = self._distance + 1
if not self._is_oid:
selectable.columns[c.name] = c
return c
def _compare_type(self, obj):
return self.type
def _group_parenthesized(self):
return False
class TableClause(FromClause):
"""represents a "table" construct.
Note that this represents tables only as another
syntactical construct within SQL expressions; it
does not provide schema-level functionality.
"""
def __init__(self, name, *columns):
super(TableClause, self).__init__(name)
self.name = self.fullname = name
self.encodedname = self.name.encode('ascii', 'backslashreplace')
self._oid_column = _ColumnClause('oid', self, _is_oid=True)
self._export_columns(columns)
def named_with_column(self):
return True
def append_column(self, c):
self._columns[c.name] = c
c.table = self
def _locate_oid_column(self):
return self._oid_column
def _proxy_column(self, c):
self.append_column(c)
return c
def _orig_columns(self):
try:
return self._orig_cols
except AttributeError:
self._orig_cols= {}
for c in self.columns:
for ci in c.orig_set:
self._orig_cols[ci] = c
return self._orig_cols
original_columns = property(_orig_columns)
def get_children(self, column_collections=True, **kwargs):
if column_collections:
return [c for c in self.c]
else:
return []
def accept_visitor(self, visitor):
visitor.visit_table(self)
def _exportable_columns(self):
raise NotImplementedError()
def _group_parenthesized(self):
return False
def count(self, whereclause=None, **params):
if len(self.primary_key):
col = list(self.primary_key)[0]
else:
col = list(self.columns)[0]
return select([func.count(col).label('tbl_row_count')], whereclause, from_obj=[self], **params)
def join(self, right, *args, **kwargs):
return Join(self, right, *args, **kwargs)
def outerjoin(self, right, *args, **kwargs):
return Join(self, right, isouter = True, *args, **kwargs)
def alias(self, name=None):
return Alias(self, name)
def select(self, whereclause = None, **params):
return select([self], whereclause, **params)
def insert(self, values = None):
return insert(self, values=values)
def update(self, whereclause = None, values = None):
return update(self, whereclause, values)
def delete(self, whereclause = None):
return delete(self, whereclause)
def _get_from_objects(self):
return [self]
class _SelectBaseMixin(object):
"""Base class for ``Select`` and ``CompoundSelects``."""
def supports_execution(self):
return True
def order_by(self, *clauses):
if len(clauses) == 1 and clauses[0] is None:
self.order_by_clause = ClauseList()
elif getattr(self, 'order_by_clause', None):
self.order_by_clause = ClauseList(*(list(self.order_by_clause.clauses) + list(clauses)))
else:
self.order_by_clause = ClauseList(*clauses)
def group_by(self, *clauses):
if len(clauses) == 1 and clauses[0] is None:
self.group_by_clause = ClauseList()
elif getattr(self, 'group_by_clause', None):
self.group_by_clause = ClauseList(*(list(clauses)+list(self.group_by_clause.clauses)))
else:
self.group_by_clause = ClauseList(*clauses)
def select(self, whereclauses = None, **params):
return select([self], whereclauses, **params)
def _get_from_objects(self):
if self.is_where or self.is_scalar:
return []
else:
return [self]
class CompoundSelect(_SelectBaseMixin, FromClause):
def __init__(self, keyword, *selects, **kwargs):
_SelectBaseMixin.__init__(self)
self.keyword = keyword
self.use_labels = kwargs.pop('use_labels', False)
self.should_correlate = kwargs.pop('correlate', False)
self.for_update = kwargs.pop('for_update', False)
self.nowait = kwargs.pop('nowait', False)
self.limit = kwargs.pop('limit', None)
self.offset = kwargs.pop('offset', None)
self.is_compound = True
self.is_where = False
self.is_scalar = False
self.is_subquery = False
# unions group from left to right, so don't group first select
self.selects = [n and select.self_group(self) or select for n,select in enumerate(selects)]
# some DBs do not like ORDER BY in the inner queries of a UNION, etc.
for s in selects:
s.order_by(None)
self.group_by(*kwargs.pop('group_by', [None]))
self.order_by(*kwargs.pop('order_by', [None]))
if len(kwargs):
raise TypeError("invalid keyword argument(s) for CompoundSelect: %s" % repr(kwargs.keys()))
self._col_map = {}
name = property(lambda s:s.keyword + " statement")
def self_group(self, against=None):
return _Grouping(self)
def _locate_oid_column(self):
return self.selects[0].oid_column
def _exportable_columns(self):
for s in self.selects:
for c in s.c:
yield c
def _proxy_column(self, column):
if self.use_labels:
col = column._make_proxy(self, name=column._label)
else:
col = column._make_proxy(self)
try:
colset = self._col_map[col.name]
except KeyError:
colset = util.Set()
self._col_map[col.name] = colset
[colset.add(c) for c in col.orig_set]
col.orig_set = colset
return col
def get_children(self, column_collections=True, **kwargs):
return (column_collections and list(self.c) or []) + \
[self.order_by_clause, self.group_by_clause] + list(self.selects)
def accept_visitor(self, visitor):
visitor.visit_compound_select(self)
def _find_engine(self):
for s in self.selects:
e = s._find_engine()
if e:
return e
else:
return None
class Select(_SelectBaseMixin, FromClause):
"""Represent a ``SELECT`` statement, with appendable clauses, as
well as the ability to execute itself and return a result set.
"""
def __init__(self, columns=None, whereclause=None, from_obj=[],
order_by=None, group_by=None, having=None,
use_labels=False, distinct=False, for_update=False,
engine=None, bind=None, limit=None, offset=None, scalar=False,
correlate=True):
"""construct a Select object.
The public constructor for Select is the [sqlalchemy.sql#select()] function;
see that function for argument descriptions.
"""
_SelectBaseMixin.__init__(self)
self.__froms = util.OrderedSet()
self.__hide_froms = util.Set([self])
self.use_labels = use_labels
self.whereclause = None
self.having = None
self._bind = bind or engine
self.limit = limit
self.offset = offset
self.for_update = for_update
self.is_compound = False
# indicates that this select statement should not expand its columns
# into the column clause of an enclosing select, and should instead
# act like a single scalar column
self.is_scalar = scalar
if scalar:
# allow corresponding_column to return None
self.orig_set = util.Set()
# indicates if this select statement, as a subquery, should automatically correlate
# its FROM clause to that of an enclosing select, update, or delete statement.
# note that the "correlate" method can be used to explicitly add a value to be correlated.
self.should_correlate = correlate
# indicates if this select statement is a subquery inside another query
self.is_subquery = False
# indicates if this select statement is in the from clause of another query
self.is_selected_from = False
# indicates if this select statement is a subquery as a criterion
# inside of a WHERE clause
self.is_where = False
self.distinct = distinct
self._raw_columns = []
self.__correlated = {}
self.__correlator = Select._CorrelatedVisitor(self, False)
self.__wherecorrelator = Select._CorrelatedVisitor(self, True)
self.__fromvisitor = Select._FromVisitor(self)
self.order_by_clause = self.group_by_clause = None
if columns is not None:
for c in columns:
self.append_column(c)
if order_by:
order_by = util.to_list(order_by)
if group_by:
group_by = util.to_list(group_by)
self.order_by(*(order_by or [None]))
self.group_by(*(group_by or [None]))
for c in self.order_by_clause:
self.__correlator.traverse(c)
for c in self.group_by_clause:
self.__correlator.traverse(c)
for f in from_obj:
self.append_from(f)
# whereclauses must be appended after the columns/FROM, since it affects
# the correlation of subqueries. see test/sql/select.py SelectTest.testwheresubquery
if whereclause is not None:
self.append_whereclause(whereclause)
if having is not None:
self.append_having(having)
class _CorrelatedVisitor(NoColumnVisitor):
"""Visit a clause, locate any ``Select`` clauses, and tell
them that they should correlate their ``FROM`` list to that of
their parent.
"""
def __init__(self, select, is_where):
NoColumnVisitor.__init__(self)
self.select = select
self.is_where = is_where
def visit_compound_select(self, cs):
self.visit_select(cs)
def visit_column(self, c):
pass
def visit_table(self, c):
pass
def visit_select(self, select):
if select is self.select:
return
select.is_where = self.is_where
select.is_subquery = True
if not select.should_correlate:
return
[select.correlate(x) for x in self.select._Select__froms]
class _FromVisitor(NoColumnVisitor):
def __init__(self, select):
NoColumnVisitor.__init__(self)
self.select = select
def visit_select(self, select):
if select is self.select:
return
select.is_selected_from = True
select.is_subquery = True
def append_column(self, column):
if _is_literal(column):
column = literal_column(str(column))
if isinstance(column, Select) and column.is_scalar:
column = column.self_group(against=',')
self._raw_columns.append(column)
if self.is_scalar and not hasattr(self, 'type'):
self.type = column.type
# if the column is a Select statement itself,
# accept visitor
self.__correlator.traverse(column)
# visit the FROM objects of the column looking for more Selects
for f in column._get_from_objects():
if f is not self:
self.__correlator.traverse(f)
self._process_froms(column, False)
def _make_proxy(self, selectable, name):
if self.is_scalar:
return self._raw_columns[0]._make_proxy(selectable, name)
else:
raise exceptions.InvalidRequestError("Not a scalar select statement")
def label(self, name):
if not self.is_scalar:
raise exceptions.InvalidRequestError("Not a scalar select statement")
else:
return label(name, self)
def _exportable_columns(self):
return [c for c in self._raw_columns if isinstance(c, Selectable)]
def _proxy_column(self, column):
if self.use_labels:
return column._make_proxy(self, name=column._label)
else:
return column._make_proxy(self)
def _process_froms(self, elem, asfrom):
for f in elem._get_from_objects():
self.__fromvisitor.traverse(f)
self.__froms.add(f)
if asfrom:
self.__froms.add(elem)
for f in elem._hide_froms():
self.__hide_froms.add(f)
def self_group(self, against=None):
if isinstance(against, CompoundSelect):
return self
return _Grouping(self)
def append_whereclause(self, whereclause):
self._append_condition('whereclause', whereclause)
def append_having(self, having):
self._append_condition('having', having)
def _append_condition(self, attribute, condition):
if isinstance(condition, basestring):
condition = _TextClause(condition)
self.__wherecorrelator.traverse(condition)
self._process_froms(condition, False)
if getattr(self, attribute) is not None:
setattr(self, attribute, and_(getattr(self, attribute), condition))
else:
setattr(self, attribute, condition)
def correlate(self, from_obj):
"""Given a ``FROM`` object, correlate this ``SELECT`` statement to it.
This basically means the given from object will not come out
in this select statement's ``FROM`` clause when printed.
"""
self.__correlated[from_obj] = from_obj
def append_from(self, fromclause):
if isinstance(fromclause, basestring):
fromclause = FromClause(fromclause)
self.__correlator.traverse(fromclause)
self._process_froms(fromclause, True)
def _locate_oid_column(self):
for f in self.__froms:
if f is self:
# we might be in our own _froms list if a column with us as the parent is attached,
# which includes textual columns.
continue
oid = f.oid_column
if oid is not None:
return oid
else:
return None
def _calc_froms(self):
f = self.__froms.difference(self.__hide_froms)
if (len(f) > 1):
return f.difference(self.__correlated)
else:
return f
froms = property(_calc_froms,
doc="""A collection containing all elements
of the ``FROM`` clause.""")
def get_children(self, column_collections=True, **kwargs):
return (column_collections and list(self.columns) or []) + \
list(self.froms) + \
[x for x in (self.whereclause, self.having, self.order_by_clause, self.group_by_clause) if x is not None]
def accept_visitor(self, visitor):
visitor.visit_select(self)
def union(self, other, **kwargs):
return union(self, other, **kwargs)
def union_all(self, other, **kwargs):
return union_all(self, other, **kwargs)
def _find_engine(self):
"""Try to return a Engine, either explicitly set in this
object, or searched within the from clauses for one.
"""
if self._bind is not None:
return self._bind
for f in self.__froms:
if f is self:
continue
e = f.bind
if e is not None:
self._bind = e
return e
# look through the columns (largely synomous with looking
# through the FROMs except in the case of _CalculatedClause/_Function)
for cc in self._exportable_columns():
for c in cc.columns:
if getattr(c, 'table', None) is self:
continue
e = c.bind
if e is not None:
self._bind = e
return e
return None
class _UpdateBase(ClauseElement):
"""Form the base for ``INSERT``, ``UPDATE``, and ``DELETE`` statements."""
def supports_execution(self):
return True
class _SelectCorrelator(NoColumnVisitor):
def __init__(self, table):
NoColumnVisitor.__init__(self)
self.table = table
def visit_select(self, select):
if select.should_correlate:
select.correlate(self.table)
def _process_whereclause(self, whereclause):
if whereclause is not None:
_UpdateBase._SelectCorrelator(self.table).traverse(whereclause)
return whereclause
def _process_colparams(self, parameters):
"""Receive the *values* of an ``INSERT`` or ``UPDATE``
statement and construct appropriate bind parameters.
"""
if parameters is None:
return None
if isinstance(parameters, list) or isinstance(parameters, tuple):
pp = {}
i = 0
for c in self.table.c:
pp[c.key] = parameters[i]
i +=1
parameters = pp
correlator = _UpdateBase._SelectCorrelator(self.table)
for key in parameters.keys():
value = parameters[key]
if isinstance(value, ClauseElement):
correlator.traverse(value)
elif _is_literal(value):
if _is_literal(key):
col = self.table.c[key]
else:
col = key
try:
parameters[key] = bindparam(col, value, unique=True)
except KeyError:
del parameters[key]
return parameters
def _find_engine(self):
return self.table.bind
class _Insert(_UpdateBase):
def __init__(self, table, values=None):
self.table = table
self.select = None
self.parameters = self._process_colparams(values)
def get_children(self, **kwargs):
if self.select is not None:
return self.select,
else:
return ()
def accept_visitor(self, visitor):
visitor.visit_insert(self)
class _Update(_UpdateBase):
def __init__(self, table, whereclause, values=None):
self.table = table
self.whereclause = self._process_whereclause(whereclause)
self.parameters = self._process_colparams(values)
def get_children(self, **kwargs):
if self.whereclause is not None:
return self.whereclause,
else:
return ()
def accept_visitor(self, visitor):
visitor.visit_update(self)
class _Delete(_UpdateBase):
def __init__(self, table, whereclause):
self.table = table
self.whereclause = self._process_whereclause(whereclause)
def get_children(self, **kwargs):
if self.whereclause is not None:
return self.whereclause,
else:
return ()
def accept_visitor(self, visitor):
visitor.visit_delete(self)
|