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
|
/* connection.c - the connection type
*
* Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de>
*
* This file is part of pysqlite.
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#ifndef Py_BUILD_CORE_BUILTIN
# define Py_BUILD_CORE_MODULE 1
#endif
#include "module.h"
#include "connection.h"
#include "statement.h"
#include "cursor.h"
#include "blob.h"
#include "prepare_protocol.h"
#include "util.h"
#include "pycore_import.h" // _PyImport_GetModuleAttrString()
#include "pycore_modsupport.h" // _PyArg_NoKeywords()
#include "pycore_pyerrors.h" // _PyErr_ChainExceptions1()
#include "pycore_pylifecycle.h" // _Py_IsInterpreterFinalizing()
#include "pycore_weakref.h"
#include <stdbool.h>
#if SQLITE_VERSION_NUMBER >= 3025000
#define HAVE_WINDOW_FUNCTIONS
#endif
static const char *
get_isolation_level(const char *level)
{
assert(level != NULL);
static const char *const allowed_levels[] = {
"",
"DEFERRED",
"IMMEDIATE",
"EXCLUSIVE",
NULL
};
for (int i = 0; allowed_levels[i] != NULL; i++) {
const char *candidate = allowed_levels[i];
if (sqlite3_stricmp(level, candidate) == 0) {
return candidate;
}
}
PyErr_SetString(PyExc_ValueError,
"isolation_level string must be '', 'DEFERRED', "
"'IMMEDIATE', or 'EXCLUSIVE'");
return NULL;
}
static int
isolation_level_converter(PyObject *str_or_none, const char **result)
{
if (Py_IsNone(str_or_none)) {
*result = NULL;
}
else if (PyUnicode_Check(str_or_none)) {
const char *str = _PyUnicode_AsUTF8NoNUL(str_or_none);
if (str == NULL) {
return 0;
}
const char *level = get_isolation_level(str);
if (level == NULL) {
return 0;
}
*result = level;
}
else {
PyErr_SetString(PyExc_TypeError,
"isolation_level must be str or None");
return 0;
}
return 1;
}
static int
autocommit_converter(PyObject *val, enum autocommit_mode *result)
{
if (Py_IsTrue(val)) {
*result = AUTOCOMMIT_ENABLED;
return 1;
}
if (Py_IsFalse(val)) {
*result = AUTOCOMMIT_DISABLED;
return 1;
}
if (PyLong_Check(val) &&
PyLong_AsLong(val) == LEGACY_TRANSACTION_CONTROL)
{
*result = AUTOCOMMIT_LEGACY;
return 1;
}
PyErr_SetString(PyExc_ValueError,
"autocommit must be True, False, or "
"sqlite3.LEGACY_TRANSACTION_CONTROL");
return 0;
}
static int
sqlite3_int64_converter(PyObject *obj, sqlite3_int64 *result)
{
if (!PyLong_Check(obj)) {
PyErr_SetString(PyExc_TypeError, "expected 'int'");
return 0;
}
*result = _pysqlite_long_as_int64(obj);
if (PyErr_Occurred()) {
return 0;
}
return 1;
}
#define clinic_state() (pysqlite_get_state_by_type(Py_TYPE(self)))
#include "clinic/connection.c.h"
#undef clinic_state
/*[clinic input]
module _sqlite3
class _sqlite3.Connection "pysqlite_Connection *" "clinic_state()->ConnectionType"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=67369db2faf80891]*/
static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self);
static void free_callback_context(callback_context *ctx);
static void set_callback_context(callback_context **ctx_pp,
callback_context *ctx);
static int connection_close(pysqlite_Connection *self);
PyObject *_pysqlite_query_execute(pysqlite_Cursor *, int, PyObject *, PyObject *);
static PyObject *
new_statement_cache(pysqlite_Connection *self, pysqlite_state *state,
int maxsize)
{
PyObject *args[] = { NULL, PyLong_FromLong(maxsize), };
if (args[1] == NULL) {
return NULL;
}
PyObject *lru_cache = state->lru_cache;
size_t nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET;
PyObject *inner = PyObject_Vectorcall(lru_cache, args + 1, nargsf, NULL);
Py_DECREF(args[1]);
if (inner == NULL) {
return NULL;
}
args[1] = (PyObject *)self; // Borrowed ref.
nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET;
PyObject *res = PyObject_Vectorcall(inner, args + 1, nargsf, NULL);
Py_DECREF(inner);
return res;
}
static inline int
connection_exec_stmt(pysqlite_Connection *self, const char *sql)
{
int rc;
Py_BEGIN_ALLOW_THREADS
int len = (int)strlen(sql) + 1;
sqlite3_stmt *stmt;
rc = sqlite3_prepare_v2(self->db, sql, len, &stmt, NULL);
if (rc == SQLITE_OK) {
(void)sqlite3_step(stmt);
rc = sqlite3_finalize(stmt);
}
Py_END_ALLOW_THREADS
if (rc != SQLITE_OK) {
(void)_pysqlite_seterror(self->state, self->db);
return -1;
}
return 0;
}
/*[python input]
class IsolationLevel_converter(CConverter):
type = "const char *"
converter = "isolation_level_converter"
class Autocommit_converter(CConverter):
type = "enum autocommit_mode"
converter = "autocommit_converter"
class sqlite3_int64_converter(CConverter):
type = "sqlite3_int64"
converter = "sqlite3_int64_converter"
[python start generated code]*/
/*[python end generated code: output=da39a3ee5e6b4b0d input=dff8760fb1eba6a1]*/
/*[clinic input]
_sqlite3.Connection.__init__ as pysqlite_connection_init
database: object
* [from 3.15]
timeout: double = 5.0
detect_types: int = 0
isolation_level: IsolationLevel = ""
check_same_thread: bool = True
factory: object(c_default='(PyObject*)clinic_state()->ConnectionType') = ConnectionType
cached_statements as cache_size: int = 128
uri: bool = False
*
autocommit: Autocommit(c_default='LEGACY_TRANSACTION_CONTROL') = sqlite3.LEGACY_TRANSACTION_CONTROL
[clinic start generated code]*/
static int
pysqlite_connection_init_impl(pysqlite_Connection *self, PyObject *database,
double timeout, int detect_types,
const char *isolation_level,
int check_same_thread, PyObject *factory,
int cache_size, int uri,
enum autocommit_mode autocommit)
/*[clinic end generated code: output=cba057313ea7712f input=219c3dbecbae7d99]*/
{
if (PySys_Audit("sqlite3.connect", "O", database) < 0) {
return -1;
}
PyObject *bytes;
if (!PyUnicode_FSConverter(database, &bytes)) {
return -1;
}
if (self->initialized) {
self->initialized = 0;
PyTypeObject *tp = Py_TYPE(self);
tp->tp_clear((PyObject *)self);
if (connection_close(self) < 0) {
return -1;
}
}
// Create and configure SQLite database object.
sqlite3 *db;
int rc;
Py_BEGIN_ALLOW_THREADS
rc = sqlite3_open_v2(PyBytes_AS_STRING(bytes), &db,
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
(uri ? SQLITE_OPEN_URI : 0), NULL);
if (rc == SQLITE_OK) {
(void)sqlite3_busy_timeout(db, (int)(timeout*1000));
}
Py_END_ALLOW_THREADS
Py_DECREF(bytes);
if (db == NULL && rc == SQLITE_NOMEM) {
PyErr_NoMemory();
return -1;
}
pysqlite_state *state = pysqlite_get_state_by_type(Py_TYPE(self));
if (rc != SQLITE_OK) {
_pysqlite_seterror(state, db);
goto error;
}
// Create LRU statement cache; returns a new reference.
PyObject *statement_cache = new_statement_cache(self, state, cache_size);
if (statement_cache == NULL) {
goto error;
}
/* Create lists of weak references to cursors and blobs */
PyObject *cursors = PyList_New(0);
if (cursors == NULL) {
Py_DECREF(statement_cache);
goto error;
}
PyObject *blobs = PyList_New(0);
if (blobs == NULL) {
Py_DECREF(statement_cache);
Py_DECREF(cursors);
goto error;
}
// Init connection state members.
self->db = db;
self->state = state;
self->detect_types = detect_types;
self->isolation_level = isolation_level;
self->autocommit = autocommit;
self->check_same_thread = check_same_thread;
self->thread_ident = PyThread_get_thread_ident();
self->statement_cache = statement_cache;
self->cursors = cursors;
self->blobs = blobs;
self->created_cursors = 0;
self->row_factory = Py_NewRef(Py_None);
self->text_factory = Py_NewRef(&PyUnicode_Type);
self->trace_ctx = NULL;
self->progress_ctx = NULL;
self->authorizer_ctx = NULL;
// Borrowed refs
self->Warning = state->Warning;
self->Error = state->Error;
self->InterfaceError = state->InterfaceError;
self->DatabaseError = state->DatabaseError;
self->DataError = state->DataError;
self->OperationalError = state->OperationalError;
self->IntegrityError = state->IntegrityError;
self->InternalError = state->InternalError;
self->ProgrammingError = state->ProgrammingError;
self->NotSupportedError = state->NotSupportedError;
if (PySys_Audit("sqlite3.connect/handle", "O", self) < 0) {
return -1; // Don't goto error; at this point, dealloc will clean up.
}
self->initialized = 1;
if (autocommit == AUTOCOMMIT_DISABLED) {
if (connection_exec_stmt(self, "BEGIN") < 0) {
return -1;
}
}
return 0;
error:
// There are no statements or other SQLite objects attached to the
// database, so sqlite3_close() should always return SQLITE_OK.
rc = sqlite3_close(db);
assert(rc == SQLITE_OK);
return -1;
}
/*[clinic input]
# Create a new destination 'connect' for the docstring and methoddef only.
# This makes it possible to keep the signatures for Connection.__init__ and
# sqlite3.connect() synchronised.
output push
destination connect new file '{dirname}/clinic/_sqlite3.connect.c.h'
# Only output the docstring and the PyMethodDef entry.
output everything suppress
output docstring_definition connect
output methoddef_define connect
# Define the sqlite3.connect function by cloning Connection.__init__.
_sqlite3.connect as pysqlite_connect = _sqlite3.Connection.__init__
Open a connection to the SQLite database file 'database'.
You can use ":memory:" to open a database connection to a database that
resides in RAM instead of on disk.
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=92260edff95d1720]*/
/*[clinic input]
# Restore normal Argument Clinic operation for the rest of this file.
output pop
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b899ba9273edcce7]*/
#define VISIT_CALLBACK_CONTEXT(ctx) \
do { \
if (ctx) { \
Py_VISIT(ctx->callable); \
Py_VISIT(ctx->module); \
} \
} while (0)
static int
connection_traverse(pysqlite_Connection *self, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(self));
Py_VISIT(self->statement_cache);
Py_VISIT(self->cursors);
Py_VISIT(self->blobs);
Py_VISIT(self->row_factory);
Py_VISIT(self->text_factory);
VISIT_CALLBACK_CONTEXT(self->trace_ctx);
VISIT_CALLBACK_CONTEXT(self->progress_ctx);
VISIT_CALLBACK_CONTEXT(self->authorizer_ctx);
#undef VISIT_CALLBACK_CONTEXT
return 0;
}
static inline void
clear_callback_context(callback_context *ctx)
{
if (ctx != NULL) {
Py_CLEAR(ctx->callable);
Py_CLEAR(ctx->module);
}
}
static int
connection_clear(pysqlite_Connection *self)
{
Py_CLEAR(self->statement_cache);
Py_CLEAR(self->cursors);
Py_CLEAR(self->blobs);
Py_CLEAR(self->row_factory);
Py_CLEAR(self->text_factory);
clear_callback_context(self->trace_ctx);
clear_callback_context(self->progress_ctx);
clear_callback_context(self->authorizer_ctx);
return 0;
}
static void
free_callback_contexts(pysqlite_Connection *self)
{
set_callback_context(&self->trace_ctx, NULL);
set_callback_context(&self->progress_ctx, NULL);
set_callback_context(&self->authorizer_ctx, NULL);
}
static void
remove_callbacks(sqlite3 *db)
{
/* None of these APIs can fail, as long as they are given a valid
* database pointer. */
assert(db != NULL);
int rc = sqlite3_trace_v2(db, SQLITE_TRACE_STMT, 0, 0);
assert(rc == SQLITE_OK), (void)rc;
sqlite3_progress_handler(db, 0, 0, (void *)0);
rc = sqlite3_set_authorizer(db, NULL, NULL);
assert(rc == SQLITE_OK), (void)rc;
}
static int
connection_close(pysqlite_Connection *self)
{
if (self->db == NULL) {
return 0;
}
int rc = 0;
if (self->autocommit == AUTOCOMMIT_DISABLED &&
!sqlite3_get_autocommit(self->db))
{
if (connection_exec_stmt(self, "ROLLBACK") < 0) {
rc = -1;
}
}
sqlite3 *db = self->db;
self->db = NULL;
Py_BEGIN_ALLOW_THREADS
/* The v2 close call always returns SQLITE_OK if given a valid database
* pointer (which we do), so we can safely ignore the return value */
(void)sqlite3_close_v2(db);
Py_END_ALLOW_THREADS
free_callback_contexts(self);
return rc;
}
static void
connection_finalize(PyObject *self)
{
pysqlite_Connection *con = (pysqlite_Connection *)self;
PyObject *exc = PyErr_GetRaisedException();
/* If close is implicitly called as a result of interpreter
* tear-down, we must not call back into Python. */
PyInterpreterState *interp = PyInterpreterState_Get();
int teardown = _Py_IsInterpreterFinalizing(interp);
if (teardown && con->db) {
remove_callbacks(con->db);
}
/* Clean up if user has not called .close() explicitly. */
if (con->db) {
if (PyErr_ResourceWarning(self, 1, "unclosed database in %R", self)) {
/* Spurious errors can appear at shutdown */
if (PyErr_ExceptionMatches(PyExc_Warning)) {
PyErr_WriteUnraisable(self);
}
}
}
if (connection_close(con) < 0) {
if (teardown) {
PyErr_Clear();
}
else {
PyErr_WriteUnraisable((PyObject *)self);
}
}
PyErr_SetRaisedException(exc);
}
static void
connection_dealloc(PyObject *self)
{
if (PyObject_CallFinalizerFromDealloc(self) < 0) {
return;
}
PyTypeObject *tp = Py_TYPE(self);
PyObject_GC_UnTrack(self);
tp->tp_clear(self);
tp->tp_free(self);
Py_DECREF(tp);
}
/*[clinic input]
_sqlite3.Connection.cursor as pysqlite_connection_cursor
factory: object = NULL
Return a cursor for the connection.
[clinic start generated code]*/
static PyObject *
pysqlite_connection_cursor_impl(pysqlite_Connection *self, PyObject *factory)
/*[clinic end generated code: output=562432a9e6af2aa1 input=4127345aa091b650]*/
{
PyObject* cursor;
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return NULL;
}
if (factory == NULL) {
factory = (PyObject *)self->state->CursorType;
}
cursor = PyObject_CallOneArg(factory, (PyObject *)self);
if (cursor == NULL)
return NULL;
if (!PyObject_TypeCheck(cursor, self->state->CursorType)) {
PyErr_Format(PyExc_TypeError,
"factory must return a cursor, not %.100s",
Py_TYPE(cursor)->tp_name);
Py_DECREF(cursor);
return NULL;
}
_pysqlite_drop_unused_cursor_references(self);
if (cursor && self->row_factory != Py_None) {
Py_INCREF(self->row_factory);
Py_XSETREF(((pysqlite_Cursor *)cursor)->row_factory, self->row_factory);
}
return cursor;
}
/*[clinic input]
_sqlite3.Connection.blobopen as blobopen
table: str
Table name.
column as col: str
Column name.
row: sqlite3_int64
Row index.
/
*
readonly: bool = False
Open the BLOB without write permissions.
name: str = "main"
Database name.
Open and return a BLOB object.
[clinic start generated code]*/
static PyObject *
blobopen_impl(pysqlite_Connection *self, const char *table, const char *col,
sqlite3_int64 row, int readonly, const char *name)
/*[clinic end generated code: output=6a02d43efb885d1c input=23576bd1108d8774]*/
{
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return NULL;
}
int rc;
sqlite3_blob *blob;
Py_BEGIN_ALLOW_THREADS
rc = sqlite3_blob_open(self->db, name, table, col, row, !readonly, &blob);
Py_END_ALLOW_THREADS
if (rc == SQLITE_MISUSE) {
PyErr_Format(self->state->InterfaceError, sqlite3_errstr(rc));
return NULL;
}
else if (rc != SQLITE_OK) {
_pysqlite_seterror(self->state, self->db);
return NULL;
}
pysqlite_Blob *obj = PyObject_GC_New(pysqlite_Blob, self->state->BlobType);
if (obj == NULL) {
goto error;
}
obj->connection = (pysqlite_Connection *)Py_NewRef(self);
obj->blob = blob;
obj->offset = 0;
obj->in_weakreflist = NULL;
PyObject_GC_Track(obj);
// Add our blob to connection blobs list
PyObject *weakref = PyWeakref_NewRef((PyObject *)obj, NULL);
if (weakref == NULL) {
goto error;
}
rc = PyList_Append(self->blobs, weakref);
Py_DECREF(weakref);
if (rc < 0) {
goto error;
}
return (PyObject *)obj;
error:
Py_XDECREF(obj);
return NULL;
}
/*[clinic input]
_sqlite3.Connection.close as pysqlite_connection_close
Close the database connection.
Any pending transaction is not committed implicitly.
[clinic start generated code]*/
static PyObject *
pysqlite_connection_close_impl(pysqlite_Connection *self)
/*[clinic end generated code: output=a546a0da212c9b97 input=b3ed5b74f6fefc06]*/
{
if (!pysqlite_check_thread(self)) {
return NULL;
}
if (!self->initialized) {
PyTypeObject *tp = Py_TYPE(self);
pysqlite_state *state = pysqlite_get_state_by_type(tp);
PyErr_SetString(state->ProgrammingError,
"Base Connection.__init__ not called.");
return NULL;
}
pysqlite_close_all_blobs(self);
Py_CLEAR(self->statement_cache);
if (connection_close(self) < 0) {
return NULL;
}
Py_RETURN_NONE;
}
/*
* Checks if a connection object is usable (i. e. not closed).
*
* 0 => error; 1 => ok
*/
int pysqlite_check_connection(pysqlite_Connection* con)
{
if (!con->initialized) {
pysqlite_state *state = pysqlite_get_state_by_type(Py_TYPE(con));
PyErr_SetString(state->ProgrammingError,
"Base Connection.__init__ not called.");
return 0;
}
if (!con->db) {
PyErr_SetString(con->state->ProgrammingError,
"Cannot operate on a closed database.");
return 0;
} else {
return 1;
}
}
/*[clinic input]
_sqlite3.Connection.commit as pysqlite_connection_commit
Commit any pending transaction to the database.
If there is no open transaction, this method is a no-op.
[clinic start generated code]*/
static PyObject *
pysqlite_connection_commit_impl(pysqlite_Connection *self)
/*[clinic end generated code: output=3da45579e89407f2 input=c8793c97c3446065]*/
{
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return NULL;
}
if (self->autocommit == AUTOCOMMIT_LEGACY) {
if (!sqlite3_get_autocommit(self->db)) {
if (connection_exec_stmt(self, "COMMIT") < 0) {
return NULL;
}
}
}
else if (self->autocommit == AUTOCOMMIT_DISABLED) {
if (connection_exec_stmt(self, "COMMIT") < 0) {
return NULL;
}
if (connection_exec_stmt(self, "BEGIN") < 0) {
return NULL;
}
}
Py_RETURN_NONE;
}
/*[clinic input]
_sqlite3.Connection.rollback as pysqlite_connection_rollback
Roll back to the start of any pending transaction.
If there is no open transaction, this method is a no-op.
[clinic start generated code]*/
static PyObject *
pysqlite_connection_rollback_impl(pysqlite_Connection *self)
/*[clinic end generated code: output=b66fa0d43e7ef305 input=7f60a2f1076f16b3]*/
{
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return NULL;
}
if (self->autocommit == AUTOCOMMIT_LEGACY) {
if (!sqlite3_get_autocommit(self->db)) {
if (connection_exec_stmt(self, "ROLLBACK") < 0) {
return NULL;
}
}
}
else if (self->autocommit == AUTOCOMMIT_DISABLED) {
if (connection_exec_stmt(self, "ROLLBACK") < 0) {
return NULL;
}
if (connection_exec_stmt(self, "BEGIN") < 0) {
return NULL;
}
}
Py_RETURN_NONE;
}
static int
_pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
{
if (py_val == Py_None) {
sqlite3_result_null(context);
} else if (PyLong_Check(py_val)) {
sqlite_int64 value = _pysqlite_long_as_int64(py_val);
if (value == -1 && PyErr_Occurred())
return -1;
sqlite3_result_int64(context, value);
} else if (PyFloat_Check(py_val)) {
double value = PyFloat_AsDouble(py_val);
if (value == -1 && PyErr_Occurred()) {
return -1;
}
sqlite3_result_double(context, value);
} else if (PyUnicode_Check(py_val)) {
Py_ssize_t sz;
const char *str = PyUnicode_AsUTF8AndSize(py_val, &sz);
if (str == NULL) {
return -1;
}
if (sz > INT_MAX) {
PyErr_SetString(PyExc_OverflowError,
"string is longer than INT_MAX bytes");
return -1;
}
sqlite3_result_text(context, str, (int)sz, SQLITE_TRANSIENT);
} else if (PyObject_CheckBuffer(py_val)) {
Py_buffer view;
if (PyObject_GetBuffer(py_val, &view, PyBUF_SIMPLE) != 0) {
return -1;
}
if (view.len > INT_MAX) {
PyErr_SetString(PyExc_OverflowError,
"BLOB longer than INT_MAX bytes");
PyBuffer_Release(&view);
return -1;
}
sqlite3_result_blob(context, view.buf, (int)view.len, SQLITE_TRANSIENT);
PyBuffer_Release(&view);
} else {
callback_context *ctx = (callback_context *)sqlite3_user_data(context);
PyErr_Format(ctx->state->ProgrammingError,
"User-defined functions cannot return '%s' values to "
"SQLite",
Py_TYPE(py_val)->tp_name);
return -1;
}
return 0;
}
static PyObject *
_pysqlite_build_py_params(sqlite3_context *context, int argc,
sqlite3_value **argv)
{
PyObject* args;
int i;
sqlite3_value* cur_value;
PyObject* cur_py_value;
args = PyTuple_New(argc);
if (!args) {
return NULL;
}
for (i = 0; i < argc; i++) {
cur_value = argv[i];
switch (sqlite3_value_type(argv[i])) {
case SQLITE_INTEGER:
cur_py_value = PyLong_FromLongLong(sqlite3_value_int64(cur_value));
break;
case SQLITE_FLOAT:
cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
break;
case SQLITE_TEXT: {
sqlite3 *db = sqlite3_context_db_handle(context);
const char *text = (const char *)sqlite3_value_text(cur_value);
if (text == NULL && sqlite3_errcode(db) == SQLITE_NOMEM) {
PyErr_NoMemory();
goto error;
}
Py_ssize_t size = sqlite3_value_bytes(cur_value);
cur_py_value = PyUnicode_FromStringAndSize(text, size);
break;
}
case SQLITE_BLOB: {
sqlite3 *db = sqlite3_context_db_handle(context);
const void *blob = sqlite3_value_blob(cur_value);
if (blob == NULL && sqlite3_errcode(db) == SQLITE_NOMEM) {
PyErr_NoMemory();
goto error;
}
Py_ssize_t size = sqlite3_value_bytes(cur_value);
cur_py_value = PyBytes_FromStringAndSize(blob, size);
break;
}
case SQLITE_NULL:
default:
cur_py_value = Py_NewRef(Py_None);
}
if (!cur_py_value) {
goto error;
}
PyTuple_SET_ITEM(args, i, cur_py_value);
}
return args;
error:
Py_DECREF(args);
return NULL;
}
static void
print_or_clear_traceback(callback_context *ctx)
{
assert(ctx != NULL);
assert(ctx->state != NULL);
if (ctx->state->enable_callback_tracebacks) {
PyErr_WriteUnraisable(ctx->callable);
}
else {
PyErr_Clear();
}
}
// Checks the Python exception and sets the appropriate SQLite error code.
static void
set_sqlite_error(sqlite3_context *context, const char *msg)
{
assert(PyErr_Occurred());
if (PyErr_ExceptionMatches(PyExc_MemoryError)) {
sqlite3_result_error_nomem(context);
}
else if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
sqlite3_result_error_toobig(context);
}
else {
sqlite3_result_error(context, msg, -1);
}
callback_context *ctx = (callback_context *)sqlite3_user_data(context);
print_or_clear_traceback(ctx);
}
static void
func_callback(sqlite3_context *context, int argc, sqlite3_value **argv)
{
PyGILState_STATE threadstate = PyGILState_Ensure();
PyObject* args;
PyObject* py_retval = NULL;
int ok;
args = _pysqlite_build_py_params(context, argc, argv);
if (args) {
callback_context *ctx = (callback_context *)sqlite3_user_data(context);
assert(ctx != NULL);
py_retval = PyObject_CallObject(ctx->callable, args);
Py_DECREF(args);
}
ok = 0;
if (py_retval) {
ok = _pysqlite_set_result(context, py_retval) == 0;
Py_DECREF(py_retval);
}
if (!ok) {
set_sqlite_error(context, "user-defined function raised exception");
}
PyGILState_Release(threadstate);
}
static void
step_callback(sqlite3_context *context, int argc, sqlite3_value **params)
{
PyGILState_STATE threadstate = PyGILState_Ensure();
PyObject* args;
PyObject* function_result = NULL;
PyObject** aggregate_instance;
PyObject* stepmethod = NULL;
callback_context *ctx = (callback_context *)sqlite3_user_data(context);
assert(ctx != NULL);
aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
if (aggregate_instance == NULL) {
(void)PyErr_NoMemory();
set_sqlite_error(context, "unable to allocate SQLite aggregate context");
goto error;
}
if (*aggregate_instance == NULL) {
*aggregate_instance = PyObject_CallNoArgs(ctx->callable);
if (!*aggregate_instance) {
set_sqlite_error(context,
"user-defined aggregate's '__init__' method raised error");
goto error;
}
}
stepmethod = PyObject_GetAttr(*aggregate_instance, ctx->state->str_step);
if (!stepmethod) {
set_sqlite_error(context,
"user-defined aggregate's 'step' method not defined");
goto error;
}
args = _pysqlite_build_py_params(context, argc, params);
if (!args) {
goto error;
}
function_result = PyObject_CallObject(stepmethod, args);
Py_DECREF(args);
if (!function_result) {
set_sqlite_error(context,
"user-defined aggregate's 'step' method raised error");
}
error:
Py_XDECREF(stepmethod);
Py_XDECREF(function_result);
PyGILState_Release(threadstate);
}
static void
final_callback(sqlite3_context *context)
{
PyGILState_STATE threadstate = PyGILState_Ensure();
PyObject* function_result;
PyObject** aggregate_instance;
int ok;
aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, 0);
if (aggregate_instance == NULL) {
/* No rows matched the query; the step handler was never called. */
goto error;
}
else if (!*aggregate_instance) {
/* this branch is executed if there was an exception in the aggregate's
* __init__ */
goto error;
}
// Keep the exception (if any) of the last call to step, value, or inverse
PyObject *exc = PyErr_GetRaisedException();
callback_context *ctx = (callback_context *)sqlite3_user_data(context);
assert(ctx != NULL);
function_result = PyObject_CallMethodNoArgs(*aggregate_instance,
ctx->state->str_finalize);
Py_DECREF(*aggregate_instance);
ok = 0;
if (function_result) {
ok = _pysqlite_set_result(context, function_result) == 0;
Py_DECREF(function_result);
}
if (!ok) {
int attr_err = PyErr_ExceptionMatches(PyExc_AttributeError);
_PyErr_ChainExceptions1(exc);
/* Note: contrary to the step, value, and inverse callbacks, SQLite
* does _not_, as of SQLite 3.38.0, propagate errors to sqlite3_step()
* from the finalize callback. This implies that execute*() will not
* raise OperationalError, as it normally would. */
set_sqlite_error(context, attr_err
? "user-defined aggregate's 'finalize' method not defined"
: "user-defined aggregate's 'finalize' method raised error");
}
else {
PyErr_SetRaisedException(exc);
}
error:
PyGILState_Release(threadstate);
}
static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
{
/* we only need to do this once in a while */
if (self->created_cursors++ < 200) {
return;
}
self->created_cursors = 0;
PyObject* new_list = PyList_New(0);
if (!new_list) {
return;
}
for (Py_ssize_t i = 0; i < PyList_Size(self->cursors); i++) {
PyObject* weakref = PyList_GetItem(self->cursors, i);
if (_PyWeakref_IsDead(weakref)) {
continue;
}
if (PyList_Append(new_list, weakref) != 0) {
Py_DECREF(new_list);
return;
}
}
Py_SETREF(self->cursors, new_list);
}
/* Allocate a UDF/callback context structure. In order to ensure that the state
* pointer always outlives the callback context, we make sure it owns a
* reference to the module itself. create_callback_context() is always called
* from connection methods, so we use the defining class to fetch the module
* pointer.
*/
static callback_context *
create_callback_context(PyTypeObject *cls, PyObject *callable)
{
callback_context *ctx = PyMem_Malloc(sizeof(callback_context));
if (ctx != NULL) {
PyObject *module = PyType_GetModule(cls);
ctx->callable = Py_NewRef(callable);
ctx->module = Py_NewRef(module);
ctx->state = pysqlite_get_state(module);
}
return ctx;
}
static void
free_callback_context(callback_context *ctx)
{
assert(ctx != NULL);
Py_XDECREF(ctx->callable);
Py_XDECREF(ctx->module);
PyMem_Free(ctx);
}
static void
set_callback_context(callback_context **ctx_pp, callback_context *ctx)
{
assert(ctx_pp != NULL);
callback_context *tmp = *ctx_pp;
*ctx_pp = ctx;
if (tmp != NULL) {
free_callback_context(tmp);
}
}
static void
destructor_callback(void *ctx)
{
if (ctx != NULL) {
// This function may be called without the GIL held, so we need to
// ensure that we destroy 'ctx' with the GIL held.
PyGILState_STATE gstate = PyGILState_Ensure();
free_callback_context((callback_context *)ctx);
PyGILState_Release(gstate);
}
}
/*[clinic input]
_sqlite3.Connection.create_function as pysqlite_connection_create_function
cls: defining_class
/
name: str
narg: int
func: object
/ [from 3.15]
*
deterministic: bool = False
Creates a new function.
[clinic start generated code]*/
static PyObject *
pysqlite_connection_create_function_impl(pysqlite_Connection *self,
PyTypeObject *cls, const char *name,
int narg, PyObject *func,
int deterministic)
/*[clinic end generated code: output=8a811529287ad240 input=c7c313b0ca8b519e]*/
{
int rc;
int flags = SQLITE_UTF8;
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return NULL;
}
if (deterministic) {
flags |= SQLITE_DETERMINISTIC;
}
callback_context *ctx = create_callback_context(cls, func);
if (ctx == NULL) {
return NULL;
}
rc = sqlite3_create_function_v2(self->db, name, narg, flags, ctx,
func_callback,
NULL,
NULL,
&destructor_callback); // will decref func
if (rc != SQLITE_OK) {
/* Workaround for SQLite bug: no error code or string is available here */
PyErr_SetString(self->OperationalError, "Error creating function");
return NULL;
}
Py_RETURN_NONE;
}
#ifdef HAVE_WINDOW_FUNCTIONS
/*
* Regarding the 'inverse' aggregate callback:
* This method is only required by window aggregate functions, not
* ordinary aggregate function implementations. It is invoked to remove
* a row from the current window. The function arguments, if any,
* correspond to the row being removed.
*/
static void
inverse_callback(sqlite3_context *context, int argc, sqlite3_value **params)
{
PyGILState_STATE gilstate = PyGILState_Ensure();
callback_context *ctx = (callback_context *)sqlite3_user_data(context);
assert(ctx != NULL);
int size = sizeof(PyObject *);
PyObject **cls = (PyObject **)sqlite3_aggregate_context(context, size);
assert(cls != NULL);
assert(*cls != NULL);
PyObject *method = PyObject_GetAttr(*cls, ctx->state->str_inverse);
if (method == NULL) {
set_sqlite_error(context,
"user-defined aggregate's 'inverse' method not defined");
goto exit;
}
PyObject *args = _pysqlite_build_py_params(context, argc, params);
if (args == NULL) {
set_sqlite_error(context,
"unable to build arguments for user-defined aggregate's "
"'inverse' method");
goto exit;
}
PyObject *res = PyObject_CallObject(method, args);
Py_DECREF(args);
if (res == NULL) {
set_sqlite_error(context,
"user-defined aggregate's 'inverse' method raised error");
goto exit;
}
Py_DECREF(res);
exit:
Py_XDECREF(method);
PyGILState_Release(gilstate);
}
/*
* Regarding the 'value' aggregate callback:
* This method is only required by window aggregate functions, not
* ordinary aggregate function implementations. It is invoked to return
* the current value of the aggregate.
*/
static void
value_callback(sqlite3_context *context)
{
PyGILState_STATE gilstate = PyGILState_Ensure();
callback_context *ctx = (callback_context *)sqlite3_user_data(context);
assert(ctx != NULL);
int size = sizeof(PyObject *);
PyObject **cls = (PyObject **)sqlite3_aggregate_context(context, size);
assert(cls != NULL);
assert(*cls != NULL);
PyObject *res = PyObject_CallMethodNoArgs(*cls, ctx->state->str_value);
if (res == NULL) {
int attr_err = PyErr_ExceptionMatches(PyExc_AttributeError);
set_sqlite_error(context, attr_err
? "user-defined aggregate's 'value' method not defined"
: "user-defined aggregate's 'value' method raised error");
}
else {
int rc = _pysqlite_set_result(context, res);
Py_DECREF(res);
if (rc < 0) {
set_sqlite_error(context,
"unable to set result from user-defined aggregate's "
"'value' method");
}
}
PyGILState_Release(gilstate);
}
/*[clinic input]
_sqlite3.Connection.create_window_function as create_window_function
cls: defining_class
name: str
The name of the SQL aggregate window function to be created or
redefined.
num_params: int
The number of arguments the step and inverse methods takes.
aggregate_class: object
A class with step(), finalize(), value(), and inverse() methods.
Set to None to clear the window function.
/
Creates or redefines an aggregate window function. Non-standard.
[clinic start generated code]*/
static PyObject *
create_window_function_impl(pysqlite_Connection *self, PyTypeObject *cls,
const char *name, int num_params,
PyObject *aggregate_class)
/*[clinic end generated code: output=5332cd9464522235 input=46d57a54225b5228]*/
{
if (sqlite3_libversion_number() < 3025000) {
PyErr_SetString(self->NotSupportedError,
"create_window_function() requires "
"SQLite 3.25.0 or higher");
return NULL;
}
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return NULL;
}
int flags = SQLITE_UTF8;
int rc;
if (Py_IsNone(aggregate_class)) {
rc = sqlite3_create_window_function(self->db, name, num_params, flags,
0, 0, 0, 0, 0, 0);
}
else {
callback_context *ctx = create_callback_context(cls, aggregate_class);
if (ctx == NULL) {
return NULL;
}
rc = sqlite3_create_window_function(self->db, name, num_params, flags,
ctx,
&step_callback,
&final_callback,
&value_callback,
&inverse_callback,
&destructor_callback);
}
if (rc != SQLITE_OK) {
// Errors are not set on the database connection, so we cannot
// use _pysqlite_seterror().
PyErr_SetString(self->ProgrammingError, sqlite3_errstr(rc));
return NULL;
}
Py_RETURN_NONE;
}
#endif
/*[clinic input]
_sqlite3.Connection.create_aggregate as pysqlite_connection_create_aggregate
cls: defining_class
/
name: str
n_arg: int
aggregate_class: object
/ [from 3.15]
Creates a new aggregate.
[clinic start generated code]*/
static PyObject *
pysqlite_connection_create_aggregate_impl(pysqlite_Connection *self,
PyTypeObject *cls,
const char *name, int n_arg,
PyObject *aggregate_class)
/*[clinic end generated code: output=1b02d0f0aec7ff96 input=8087056db6eae1cf]*/
{
int rc;
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return NULL;
}
callback_context *ctx = create_callback_context(cls, aggregate_class);
if (ctx == NULL) {
return NULL;
}
rc = sqlite3_create_function_v2(self->db, name, n_arg, SQLITE_UTF8, ctx,
0,
&step_callback,
&final_callback,
&destructor_callback); // will decref func
if (rc != SQLITE_OK) {
/* Workaround for SQLite bug: no error code or string is available here */
PyErr_SetString(self->OperationalError, "Error creating aggregate");
return NULL;
}
Py_RETURN_NONE;
}
static int
authorizer_callback(void *ctx, int action, const char *arg1,
const char *arg2 , const char *dbname,
const char *access_attempt_source)
{
PyGILState_STATE gilstate = PyGILState_Ensure();
PyObject *ret;
int rc = SQLITE_DENY;
assert(ctx != NULL);
PyObject *callable = ((callback_context *)ctx)->callable;
ret = PyObject_CallFunction(callable, "issss", action, arg1, arg2, dbname,
access_attempt_source);
if (ret == NULL) {
print_or_clear_traceback(ctx);
rc = SQLITE_DENY;
}
else {
if (PyLong_Check(ret)) {
rc = PyLong_AsInt(ret);
if (rc == -1 && PyErr_Occurred()) {
print_or_clear_traceback(ctx);
rc = SQLITE_DENY;
}
}
else {
rc = SQLITE_DENY;
}
Py_DECREF(ret);
}
PyGILState_Release(gilstate);
return rc;
}
static int
progress_callback(void *ctx)
{
PyGILState_STATE gilstate = PyGILState_Ensure();
int rc;
PyObject *ret;
assert(ctx != NULL);
PyObject *callable = ((callback_context *)ctx)->callable;
ret = PyObject_CallNoArgs(callable);
if (!ret) {
/* abort query if error occurred */
rc = -1;
}
else {
rc = PyObject_IsTrue(ret);
Py_DECREF(ret);
}
if (rc < 0) {
print_or_clear_traceback(ctx);
}
PyGILState_Release(gilstate);
return rc;
}
/*
* From https://sqlite.org/c3ref/trace_v2.html:
* The integer return value from the callback is currently ignored, though this
* may change in future releases. Callback implementations should return zero
* to ensure future compatibility.
*/
static int
trace_callback(unsigned int type, void *ctx, void *stmt, void *sql)
{
if (type != SQLITE_TRACE_STMT) {
return 0;
}
PyGILState_STATE gilstate = PyGILState_Ensure();
assert(ctx != NULL);
pysqlite_state *state = ((callback_context *)ctx)->state;
assert(state != NULL);
PyObject *py_statement = NULL;
const char *expanded_sql = sqlite3_expanded_sql((sqlite3_stmt *)stmt);
if (expanded_sql == NULL) {
sqlite3 *db = sqlite3_db_handle((sqlite3_stmt *)stmt);
if (sqlite3_errcode(db) == SQLITE_NOMEM) {
(void)PyErr_NoMemory();
goto exit;
}
PyErr_SetString(state->DataError,
"Expanded SQL string exceeds the maximum string length");
print_or_clear_traceback((callback_context *)ctx);
// Fall back to unexpanded sql
py_statement = PyUnicode_FromString((const char *)sql);
}
else {
py_statement = PyUnicode_FromString(expanded_sql);
sqlite3_free((void *)expanded_sql);
}
if (py_statement) {
PyObject *callable = ((callback_context *)ctx)->callable;
PyObject *ret = PyObject_CallOneArg(callable, py_statement);
Py_DECREF(py_statement);
Py_XDECREF(ret);
}
if (PyErr_Occurred()) {
print_or_clear_traceback((callback_context *)ctx);
}
exit:
PyGILState_Release(gilstate);
return 0;
}
/*[clinic input]
_sqlite3.Connection.set_authorizer as pysqlite_connection_set_authorizer
cls: defining_class
authorizer_callback as callable: object
/ [from 3.15]
Set authorizer callback.
[clinic start generated code]*/
static PyObject *
pysqlite_connection_set_authorizer_impl(pysqlite_Connection *self,
PyTypeObject *cls,
PyObject *callable)
/*[clinic end generated code: output=75fa60114fc971c3 input=a52bd4937c588752]*/
{
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return NULL;
}
int rc;
if (callable == Py_None) {
rc = sqlite3_set_authorizer(self->db, NULL, NULL);
set_callback_context(&self->authorizer_ctx, NULL);
}
else {
callback_context *ctx = create_callback_context(cls, callable);
if (ctx == NULL) {
return NULL;
}
rc = sqlite3_set_authorizer(self->db, authorizer_callback, ctx);
set_callback_context(&self->authorizer_ctx, ctx);
}
if (rc != SQLITE_OK) {
PyErr_SetString(self->OperationalError,
"Error setting authorizer callback");
set_callback_context(&self->authorizer_ctx, NULL);
return NULL;
}
Py_RETURN_NONE;
}
/*[clinic input]
_sqlite3.Connection.set_progress_handler as pysqlite_connection_set_progress_handler
cls: defining_class
progress_handler as callable: object
A callable that takes no arguments.
If the callable returns non-zero, the current query is terminated,
and an exception is raised.
/ [from 3.15]
n: int
The number of SQLite virtual machine instructions that are
executed between invocations of 'progress_handler'.
Set progress handler callback.
If 'progress_handler' is None or 'n' is 0, the progress handler is disabled.
[clinic start generated code]*/
static PyObject *
pysqlite_connection_set_progress_handler_impl(pysqlite_Connection *self,
PyTypeObject *cls,
PyObject *callable, int n)
/*[clinic end generated code: output=0739957fd8034a50 input=b4d6e2ef8b4d32f9]*/
{
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return NULL;
}
if (callable == Py_None) {
/* None clears the progress handler previously set */
sqlite3_progress_handler(self->db, 0, 0, (void*)0);
set_callback_context(&self->progress_ctx, NULL);
}
else {
callback_context *ctx = create_callback_context(cls, callable);
if (ctx == NULL) {
return NULL;
}
sqlite3_progress_handler(self->db, n, progress_callback, ctx);
set_callback_context(&self->progress_ctx, ctx);
}
Py_RETURN_NONE;
}
/*[clinic input]
_sqlite3.Connection.set_trace_callback as pysqlite_connection_set_trace_callback
cls: defining_class
trace_callback as callable: object
/ [from 3.15]
Set a trace callback called for each SQL statement (passed as unicode).
[clinic start generated code]*/
static PyObject *
pysqlite_connection_set_trace_callback_impl(pysqlite_Connection *self,
PyTypeObject *cls,
PyObject *callable)
/*[clinic end generated code: output=d91048c03bfcee05 input=d705d592ec03cf28]*/
{
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return NULL;
}
if (callable == Py_None) {
/*
* None clears the trace callback previously set
*
* Ref.
* - https://sqlite.org/c3ref/c_trace.html
* - https://sqlite.org/c3ref/trace_v2.html
*/
sqlite3_trace_v2(self->db, SQLITE_TRACE_STMT, 0, 0);
set_callback_context(&self->trace_ctx, NULL);
}
else {
callback_context *ctx = create_callback_context(cls, callable);
if (ctx == NULL) {
return NULL;
}
sqlite3_trace_v2(self->db, SQLITE_TRACE_STMT, trace_callback, ctx);
set_callback_context(&self->trace_ctx, ctx);
}
Py_RETURN_NONE;
}
#ifdef PY_SQLITE_ENABLE_LOAD_EXTENSION
/*[clinic input]
_sqlite3.Connection.enable_load_extension as pysqlite_connection_enable_load_extension
enable as onoff: bool
/
Enable dynamic loading of SQLite extension modules.
[clinic start generated code]*/
static PyObject *
pysqlite_connection_enable_load_extension_impl(pysqlite_Connection *self,
int onoff)
/*[clinic end generated code: output=9cac37190d388baf input=2a1e87931486380f]*/
{
int rc;
if (PySys_Audit("sqlite3.enable_load_extension",
"OO", self, onoff ? Py_True : Py_False) < 0) {
return NULL;
}
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return NULL;
}
rc = sqlite3_enable_load_extension(self->db, onoff);
if (rc != SQLITE_OK) {
PyErr_SetString(self->OperationalError,
"Error enabling load extension");
return NULL;
} else {
Py_RETURN_NONE;
}
}
/*[clinic input]
_sqlite3.Connection.load_extension as pysqlite_connection_load_extension
name as extension_name: str
/
*
entrypoint: str(accept={str, NoneType}) = None
Load SQLite extension module.
[clinic start generated code]*/
static PyObject *
pysqlite_connection_load_extension_impl(pysqlite_Connection *self,
const char *extension_name,
const char *entrypoint)
/*[clinic end generated code: output=7e61a7add9de0286 input=c36b14ea702e04f5]*/
{
int rc;
char* errmsg;
if (PySys_Audit("sqlite3.load_extension", "Os", self, extension_name) < 0) {
return NULL;
}
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return NULL;
}
rc = sqlite3_load_extension(self->db, extension_name, entrypoint, &errmsg);
if (rc != 0) {
PyErr_SetString(self->OperationalError, errmsg);
return NULL;
} else {
Py_RETURN_NONE;
}
}
#endif
int pysqlite_check_thread(pysqlite_Connection* self)
{
if (self->check_same_thread) {
if (PyThread_get_thread_ident() != self->thread_ident) {
PyErr_Format(self->ProgrammingError,
"SQLite objects created in a thread can only be used in that same thread. "
"The object was created in thread id %lu and this is thread id %lu.",
self->thread_ident, PyThread_get_thread_ident());
return 0;
}
}
return 1;
}
static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
{
if (!pysqlite_check_connection(self)) {
return NULL;
}
if (self->isolation_level != NULL) {
return PyUnicode_FromString(self->isolation_level);
}
Py_RETURN_NONE;
}
static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
{
if (!pysqlite_check_connection(self)) {
return NULL;
}
return PyLong_FromLong(sqlite3_total_changes(self->db));
}
static PyObject* pysqlite_connection_get_in_transaction(pysqlite_Connection* self, void* unused)
{
if (!pysqlite_check_connection(self)) {
return NULL;
}
if (!sqlite3_get_autocommit(self->db)) {
Py_RETURN_TRUE;
}
Py_RETURN_FALSE;
}
static int
pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level, void *Py_UNUSED(ignored))
{
if (isolation_level == NULL) {
PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
return -1;
}
if (Py_IsNone(isolation_level)) {
self->isolation_level = NULL;
// Execute a COMMIT to re-enable autocommit mode
PyObject *res = pysqlite_connection_commit_impl(self);
if (res == NULL) {
return -1;
}
Py_DECREF(res);
return 0;
}
if (!isolation_level_converter(isolation_level, &self->isolation_level)) {
return -1;
}
return 0;
}
static PyObject *
pysqlite_connection_call(pysqlite_Connection *self, PyObject *args,
PyObject *kwargs)
{
PyObject* sql;
pysqlite_Statement* statement;
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return NULL;
}
if (!_PyArg_NoKeywords(MODULE_NAME ".Connection", kwargs))
return NULL;
if (!PyArg_ParseTuple(args, "U", &sql))
return NULL;
statement = pysqlite_statement_create(self, sql);
if (statement == NULL) {
return NULL;
}
return (PyObject*)statement;
}
/*[clinic input]
_sqlite3.Connection.execute as pysqlite_connection_execute
sql: unicode
parameters: object = NULL
/
Executes an SQL statement.
[clinic start generated code]*/
static PyObject *
pysqlite_connection_execute_impl(pysqlite_Connection *self, PyObject *sql,
PyObject *parameters)
/*[clinic end generated code: output=5be05ae01ee17ee4 input=27aa7792681ddba2]*/
{
PyObject* result = 0;
PyObject *cursor = pysqlite_connection_cursor_impl(self, NULL);
if (!cursor) {
goto error;
}
result = _pysqlite_query_execute((pysqlite_Cursor *)cursor, 0, sql, parameters);
if (!result) {
Py_CLEAR(cursor);
}
error:
Py_XDECREF(result);
return cursor;
}
/*[clinic input]
_sqlite3.Connection.executemany as pysqlite_connection_executemany
sql: unicode
parameters: object
/
Repeatedly executes an SQL statement.
[clinic start generated code]*/
static PyObject *
pysqlite_connection_executemany_impl(pysqlite_Connection *self,
PyObject *sql, PyObject *parameters)
/*[clinic end generated code: output=776cd2fd20bfe71f input=495be76551d525db]*/
{
PyObject* result = 0;
PyObject *cursor = pysqlite_connection_cursor_impl(self, NULL);
if (!cursor) {
goto error;
}
result = _pysqlite_query_execute((pysqlite_Cursor *)cursor, 1, sql, parameters);
if (!result) {
Py_CLEAR(cursor);
}
error:
Py_XDECREF(result);
return cursor;
}
/*[clinic input]
_sqlite3.Connection.executescript as pysqlite_connection_executescript
sql_script as script_obj: object
/
Executes multiple SQL statements at once.
[clinic start generated code]*/
static PyObject *
pysqlite_connection_executescript(pysqlite_Connection *self,
PyObject *script_obj)
/*[clinic end generated code: output=4c4f9d77aa0ae37d input=f6e5f1ccfa313db4]*/
{
PyObject* result = 0;
PyObject *cursor = pysqlite_connection_cursor_impl(self, NULL);
if (!cursor) {
goto error;
}
PyObject *meth = self->state->str_executescript; // borrowed ref.
result = PyObject_CallMethodObjArgs(cursor, meth, script_obj, NULL);
if (!result) {
Py_CLEAR(cursor);
}
error:
Py_XDECREF(result);
return cursor;
}
/* ------------------------- COLLATION CODE ------------------------ */
static int
collation_callback(void *context, int text1_length, const void *text1_data,
int text2_length, const void *text2_data)
{
PyGILState_STATE gilstate = PyGILState_Ensure();
PyObject* string1 = 0;
PyObject* string2 = 0;
PyObject* retval = NULL;
long longval;
int result = 0;
/* This callback may be executed multiple times per sqlite3_step(). Bail if
* the previous call failed */
if (PyErr_Occurred()) {
goto finally;
}
string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
if (string1 == NULL) {
goto finally;
}
string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
if (string2 == NULL) {
goto finally;
}
callback_context *ctx = (callback_context *)context;
assert(ctx != NULL);
PyObject *args[] = { NULL, string1, string2 }; // Borrowed refs.
size_t nargsf = 2 | PY_VECTORCALL_ARGUMENTS_OFFSET;
retval = PyObject_Vectorcall(ctx->callable, args + 1, nargsf, NULL);
if (retval == NULL) {
/* execution failed */
goto finally;
}
longval = PyLong_AsLongAndOverflow(retval, &result);
if (longval == -1 && PyErr_Occurred()) {
PyErr_Clear();
result = 0;
}
else if (!result) {
if (longval > 0)
result = 1;
else if (longval < 0)
result = -1;
}
finally:
Py_XDECREF(string1);
Py_XDECREF(string2);
Py_XDECREF(retval);
PyGILState_Release(gilstate);
return result;
}
/*[clinic input]
_sqlite3.Connection.interrupt as pysqlite_connection_interrupt
Abort any pending database operation.
[clinic start generated code]*/
static PyObject *
pysqlite_connection_interrupt_impl(pysqlite_Connection *self)
/*[clinic end generated code: output=f193204bc9e70b47 input=75ad03ade7012859]*/
{
PyObject* retval = NULL;
if (!pysqlite_check_connection(self)) {
goto finally;
}
sqlite3_interrupt(self->db);
retval = Py_NewRef(Py_None);
finally:
return retval;
}
/* Function author: Paul Kippes <kippesp@gmail.com>
* Class method of Connection to call the Python function _iterdump
* of the sqlite3 module.
*/
/*[clinic input]
_sqlite3.Connection.iterdump as pysqlite_connection_iterdump
*
filter: object = None
An optional LIKE pattern for database objects to dump
Returns iterator to the dump of the database in an SQL text format.
[clinic start generated code]*/
static PyObject *
pysqlite_connection_iterdump_impl(pysqlite_Connection *self,
PyObject *filter)
/*[clinic end generated code: output=fd81069c4bdeb6b0 input=4ae6d9a898f108df]*/
{
if (!pysqlite_check_connection(self)) {
return NULL;
}
PyObject *iterdump = _PyImport_GetModuleAttrString(MODULE_NAME ".dump", "_iterdump");
if (!iterdump) {
if (!PyErr_Occurred()) {
PyErr_SetString(self->OperationalError,
"Failed to obtain _iterdump() reference");
}
return NULL;
}
PyObject *args[3] = {NULL, (PyObject *)self, filter};
PyObject *kwnames = Py_BuildValue("(s)", "filter");
if (!kwnames) {
Py_DECREF(iterdump);
return NULL;
}
Py_ssize_t nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET;
PyObject *retval = PyObject_Vectorcall(iterdump, args + 1, nargsf, kwnames);
Py_DECREF(iterdump);
Py_DECREF(kwnames);
return retval;
}
/*[clinic input]
_sqlite3.Connection.backup as pysqlite_connection_backup
target: object(type='pysqlite_Connection *', subclass_of='clinic_state()->ConnectionType')
*
pages: int = -1
progress: object = None
name: str = "main"
sleep: double = 0.250
Makes a backup of the database.
[clinic start generated code]*/
static PyObject *
pysqlite_connection_backup_impl(pysqlite_Connection *self,
pysqlite_Connection *target, int pages,
PyObject *progress, const char *name,
double sleep)
/*[clinic end generated code: output=306a3e6a38c36334 input=c6519d0f59d0fd7f]*/
{
int rc;
int sleep_ms = (int)(sleep * 1000.0);
sqlite3 *bck_conn;
sqlite3_backup *bck_handle;
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return NULL;
}
if (!pysqlite_check_connection(target)) {
return NULL;
}
if (target == self) {
PyErr_SetString(PyExc_ValueError, "target cannot be the same connection instance");
return NULL;
}
if (progress != Py_None && !PyCallable_Check(progress)) {
PyErr_SetString(PyExc_TypeError, "progress argument must be a callable");
return NULL;
}
if (pages == 0) {
pages = -1;
}
bck_conn = target->db;
Py_BEGIN_ALLOW_THREADS
bck_handle = sqlite3_backup_init(bck_conn, "main", self->db, name);
Py_END_ALLOW_THREADS
if (bck_handle == NULL) {
_pysqlite_seterror(self->state, bck_conn);
return NULL;
}
do {
Py_BEGIN_ALLOW_THREADS
rc = sqlite3_backup_step(bck_handle, pages);
Py_END_ALLOW_THREADS
if (progress != Py_None) {
int remaining = sqlite3_backup_remaining(bck_handle);
int pagecount = sqlite3_backup_pagecount(bck_handle);
PyObject *res = PyObject_CallFunction(progress, "iii", rc,
remaining, pagecount);
if (res == NULL) {
/* Callback failed: abort backup and bail. */
Py_BEGIN_ALLOW_THREADS
sqlite3_backup_finish(bck_handle);
Py_END_ALLOW_THREADS
return NULL;
}
Py_DECREF(res);
}
/* Sleep for a while if there are still further pages to copy and
the engine could not make any progress */
if (rc == SQLITE_BUSY || rc == SQLITE_LOCKED) {
Py_BEGIN_ALLOW_THREADS
sqlite3_sleep(sleep_ms);
Py_END_ALLOW_THREADS
}
} while (rc == SQLITE_OK || rc == SQLITE_BUSY || rc == SQLITE_LOCKED);
Py_BEGIN_ALLOW_THREADS
rc = sqlite3_backup_finish(bck_handle);
Py_END_ALLOW_THREADS
if (rc != SQLITE_OK) {
_pysqlite_seterror(self->state, bck_conn);
return NULL;
}
Py_RETURN_NONE;
}
/*[clinic input]
_sqlite3.Connection.create_collation as pysqlite_connection_create_collation
cls: defining_class
name: str
callback as callable: object
/
Creates a collation function.
[clinic start generated code]*/
static PyObject *
pysqlite_connection_create_collation_impl(pysqlite_Connection *self,
PyTypeObject *cls,
const char *name,
PyObject *callable)
/*[clinic end generated code: output=32d339e97869c378 input=f67ecd2e31e61ad3]*/
{
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return NULL;
}
callback_context *ctx = NULL;
int rc;
int flags = SQLITE_UTF8;
if (callable == Py_None) {
rc = sqlite3_create_collation_v2(self->db, name, flags,
NULL, NULL, NULL);
}
else {
if (!PyCallable_Check(callable)) {
PyErr_SetString(PyExc_TypeError, "parameter must be callable");
return NULL;
}
ctx = create_callback_context(cls, callable);
if (ctx == NULL) {
return NULL;
}
rc = sqlite3_create_collation_v2(self->db, name, flags, ctx,
&collation_callback,
&destructor_callback);
}
if (rc != SQLITE_OK) {
/* Unlike other sqlite3_* functions, the destructor callback is _not_
* called if sqlite3_create_collation_v2() fails, so we have to free
* the context before returning.
*/
if (callable != Py_None) {
free_callback_context(ctx);
}
_pysqlite_seterror(self->state, self->db);
return NULL;
}
Py_RETURN_NONE;
}
#ifdef PY_SQLITE_HAVE_SERIALIZE
/*[clinic input]
_sqlite3.Connection.serialize as serialize
*
name: str = "main"
Which database to serialize.
Serialize a database into a byte string.
For an ordinary on-disk database file, the serialization is just a copy of the
disk file. For an in-memory database or a "temp" database, the serialization is
the same sequence of bytes which would be written to disk if that database
were backed up to disk.
[clinic start generated code]*/
static PyObject *
serialize_impl(pysqlite_Connection *self, const char *name)
/*[clinic end generated code: output=97342b0e55239dd3 input=d2eb5194a65abe2b]*/
{
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return NULL;
}
/* If SQLite has a contiguous memory representation of the database, we can
* avoid memory allocations, so we try with the no-copy flag first.
*/
sqlite3_int64 size;
unsigned int flags = SQLITE_SERIALIZE_NOCOPY;
const char *data;
Py_BEGIN_ALLOW_THREADS
data = (const char *)sqlite3_serialize(self->db, name, &size, flags);
if (data == NULL) {
flags &= ~SQLITE_SERIALIZE_NOCOPY;
data = (const char *)sqlite3_serialize(self->db, name, &size, flags);
}
Py_END_ALLOW_THREADS
if (data == NULL) {
PyErr_Format(self->OperationalError, "unable to serialize '%s'",
name);
return NULL;
}
PyObject *res = PyBytes_FromStringAndSize(data, (Py_ssize_t)size);
if (!(flags & SQLITE_SERIALIZE_NOCOPY)) {
sqlite3_free((void *)data);
}
return res;
}
/*[clinic input]
_sqlite3.Connection.deserialize as deserialize
data: Py_buffer(accept={buffer, str})
The serialized database content.
/
*
name: str = "main"
Which database to reopen with the deserialization.
Load a serialized database.
The deserialize interface causes the database connection to disconnect from the
target database, and then reopen it as an in-memory database based on the given
serialized data.
The deserialize interface will fail with SQLITE_BUSY if the database is
currently in a read transaction or is involved in a backup operation.
[clinic start generated code]*/
static PyObject *
deserialize_impl(pysqlite_Connection *self, Py_buffer *data,
const char *name)
/*[clinic end generated code: output=e394c798b98bad89 input=1be4ca1faacf28f2]*/
{
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return NULL;
}
/* Transfer ownership of the buffer to SQLite:
* - Move buffer from Py to SQLite
* - Tell SQLite to free buffer memory
* - Tell SQLite that it is permitted to grow the resulting database
*
* Make sure we don't overflow sqlite3_deserialize(); it accepts a signed
* 64-bit int as its data size argument.
*
* We can safely use sqlite3_malloc64 here, since it was introduced before
* the serialize APIs.
*/
if (data->len > 9223372036854775807) { // (1 << 63) - 1
PyErr_SetString(PyExc_OverflowError, "'data' is too large");
return NULL;
}
sqlite3_int64 size = (sqlite3_int64)data->len;
unsigned char *buf = sqlite3_malloc64(size);
if (buf == NULL) {
return PyErr_NoMemory();
}
const unsigned int flags = SQLITE_DESERIALIZE_FREEONCLOSE |
SQLITE_DESERIALIZE_RESIZEABLE;
int rc;
Py_BEGIN_ALLOW_THREADS
(void)memcpy(buf, data->buf, data->len);
rc = sqlite3_deserialize(self->db, name, buf, size, size, flags);
Py_END_ALLOW_THREADS
if (rc != SQLITE_OK) {
(void)_pysqlite_seterror(self->state, self->db);
return NULL;
}
Py_RETURN_NONE;
}
#endif // PY_SQLITE_HAVE_SERIALIZE
/*[clinic input]
_sqlite3.Connection.__enter__ as pysqlite_connection_enter
Called when the connection is used as a context manager.
Returns itself as a convenience to the caller.
[clinic start generated code]*/
static PyObject *
pysqlite_connection_enter_impl(pysqlite_Connection *self)
/*[clinic end generated code: output=457b09726d3e9dcd input=127d7a4f17e86d8f]*/
{
if (!pysqlite_check_connection(self)) {
return NULL;
}
return Py_NewRef((PyObject *)self);
}
/*[clinic input]
_sqlite3.Connection.__exit__ as pysqlite_connection_exit
type as exc_type: object
value as exc_value: object
traceback as exc_tb: object
/
Called when the connection is used as a context manager.
If there was any exception, a rollback takes place; otherwise we commit.
[clinic start generated code]*/
static PyObject *
pysqlite_connection_exit_impl(pysqlite_Connection *self, PyObject *exc_type,
PyObject *exc_value, PyObject *exc_tb)
/*[clinic end generated code: output=0705200e9321202a input=bd66f1532c9c54a7]*/
{
int commit = 0;
PyObject* result;
if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
commit = 1;
result = pysqlite_connection_commit_impl(self);
}
else {
result = pysqlite_connection_rollback_impl(self);
}
if (result == NULL) {
if (commit) {
/* Commit failed; try to rollback in order to unlock the database.
* If rollback also fails, chain the exceptions. */
PyObject *exc = PyErr_GetRaisedException();
result = pysqlite_connection_rollback_impl(self);
if (result == NULL) {
_PyErr_ChainExceptions1(exc);
}
else {
Py_DECREF(result);
PyErr_SetRaisedException(exc);
}
}
return NULL;
}
Py_DECREF(result);
Py_RETURN_FALSE;
}
/*[clinic input]
_sqlite3.Connection.setlimit as setlimit
category: int
The limit category to be set.
limit: int
The new limit. If the new limit is a negative number, the limit is
unchanged.
/
Set connection run-time limits.
Attempts to increase a limit above its hard upper bound are silently truncated
to the hard upper bound. Regardless of whether or not the limit was changed,
the prior value of the limit is returned.
[clinic start generated code]*/
static PyObject *
setlimit_impl(pysqlite_Connection *self, int category, int limit)
/*[clinic end generated code: output=0d208213f8d68ccd input=9bd469537e195635]*/
{
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return NULL;
}
int old_limit = sqlite3_limit(self->db, category, limit);
if (old_limit < 0) {
PyErr_SetString(self->ProgrammingError, "'category' is out of bounds");
return NULL;
}
return PyLong_FromLong(old_limit);
}
/*[clinic input]
_sqlite3.Connection.getlimit as getlimit
category: int
The limit category to be queried.
/
Get connection run-time limits.
[clinic start generated code]*/
static PyObject *
getlimit_impl(pysqlite_Connection *self, int category)
/*[clinic end generated code: output=7c3f5d11f24cecb1 input=61e0849fb4fb058f]*/
{
return setlimit_impl(self, category, -1);
}
static inline bool
is_int_config(const int op)
{
switch (op) {
case SQLITE_DBCONFIG_ENABLE_FKEY:
case SQLITE_DBCONFIG_ENABLE_TRIGGER:
case SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER:
case SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION:
#if SQLITE_VERSION_NUMBER >= 3016000
case SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE:
#endif
#if SQLITE_VERSION_NUMBER >= 3020000
case SQLITE_DBCONFIG_ENABLE_QPSG:
#endif
#if SQLITE_VERSION_NUMBER >= 3022000
case SQLITE_DBCONFIG_TRIGGER_EQP:
#endif
#if SQLITE_VERSION_NUMBER >= 3024000
case SQLITE_DBCONFIG_RESET_DATABASE:
#endif
#if SQLITE_VERSION_NUMBER >= 3026000
case SQLITE_DBCONFIG_DEFENSIVE:
#endif
#if SQLITE_VERSION_NUMBER >= 3028000
case SQLITE_DBCONFIG_WRITABLE_SCHEMA:
#endif
#if SQLITE_VERSION_NUMBER >= 3029000
case SQLITE_DBCONFIG_DQS_DDL:
case SQLITE_DBCONFIG_DQS_DML:
case SQLITE_DBCONFIG_LEGACY_ALTER_TABLE:
#endif
#if SQLITE_VERSION_NUMBER >= 3030000
case SQLITE_DBCONFIG_ENABLE_VIEW:
#endif
#if SQLITE_VERSION_NUMBER >= 3031000
case SQLITE_DBCONFIG_LEGACY_FILE_FORMAT:
case SQLITE_DBCONFIG_TRUSTED_SCHEMA:
#endif
return true;
default:
return false;
}
}
/*[clinic input]
_sqlite3.Connection.setconfig as setconfig
op: int
The configuration verb; one of the sqlite3.SQLITE_DBCONFIG codes.
enable: bool = True
/
Set a boolean connection configuration option.
[clinic start generated code]*/
static PyObject *
setconfig_impl(pysqlite_Connection *self, int op, int enable)
/*[clinic end generated code: output=c60b13e618aff873 input=a10f1539c2d7da6b]*/
{
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return NULL;
}
if (!is_int_config(op)) {
return PyErr_Format(PyExc_ValueError, "unknown config 'op': %d", op);
}
int actual;
int rc = sqlite3_db_config(self->db, op, enable, &actual);
if (rc != SQLITE_OK) {
(void)_pysqlite_seterror(self->state, self->db);
return NULL;
}
if (enable != actual) {
PyErr_SetString(self->state->OperationalError, "Unable to set config");
return NULL;
}
Py_RETURN_NONE;
}
/*[clinic input]
_sqlite3.Connection.getconfig as getconfig -> bool
op: int
The configuration verb; one of the sqlite3.SQLITE_DBCONFIG codes.
/
Query a boolean connection configuration option.
[clinic start generated code]*/
static int
getconfig_impl(pysqlite_Connection *self, int op)
/*[clinic end generated code: output=25ac05044c7b78a3 input=b0526d7e432e3f2f]*/
{
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return -1;
}
if (!is_int_config(op)) {
PyErr_Format(PyExc_ValueError, "unknown config 'op': %d", op);
return -1;
}
int current;
int rc = sqlite3_db_config(self->db, op, -1, ¤t);
if (rc != SQLITE_OK) {
(void)_pysqlite_seterror(self->state, self->db);
return -1;
}
return current;
}
static PyObject *
get_autocommit(pysqlite_Connection *self, void *Py_UNUSED(ctx))
{
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return NULL;
}
if (self->autocommit == AUTOCOMMIT_ENABLED) {
Py_RETURN_TRUE;
}
if (self->autocommit == AUTOCOMMIT_DISABLED) {
Py_RETURN_FALSE;
}
return PyLong_FromLong(LEGACY_TRANSACTION_CONTROL);
}
static int
set_autocommit(pysqlite_Connection *self, PyObject *val, void *Py_UNUSED(ctx))
{
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return -1;
}
if (!autocommit_converter(val, &self->autocommit)) {
return -1;
}
if (self->autocommit == AUTOCOMMIT_ENABLED) {
if (!sqlite3_get_autocommit(self->db)) {
if (connection_exec_stmt(self, "COMMIT") < 0) {
return -1;
}
}
}
else if (self->autocommit == AUTOCOMMIT_DISABLED) {
if (sqlite3_get_autocommit(self->db)) {
if (connection_exec_stmt(self, "BEGIN") < 0) {
return -1;
}
}
}
return 0;
}
static PyObject *
get_sig(PyObject *self, void *Py_UNUSED(ctx))
{
return PyUnicode_FromString("(sql, /)");
}
static const char connection_doc[] =
PyDoc_STR("SQLite database connection object.");
static PyGetSetDef connection_getset[] = {
{"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
{"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
{"in_transaction", (getter)pysqlite_connection_get_in_transaction, (setter)0},
{"autocommit", (getter)get_autocommit, (setter)set_autocommit},
{"__text_signature__", get_sig, (setter)0},
{NULL}
};
static PyMethodDef connection_methods[] = {
PYSQLITE_CONNECTION_BACKUP_METHODDEF
PYSQLITE_CONNECTION_CLOSE_METHODDEF
PYSQLITE_CONNECTION_COMMIT_METHODDEF
PYSQLITE_CONNECTION_CREATE_AGGREGATE_METHODDEF
PYSQLITE_CONNECTION_CREATE_COLLATION_METHODDEF
PYSQLITE_CONNECTION_CREATE_FUNCTION_METHODDEF
PYSQLITE_CONNECTION_CURSOR_METHODDEF
PYSQLITE_CONNECTION_ENABLE_LOAD_EXTENSION_METHODDEF
PYSQLITE_CONNECTION_ENTER_METHODDEF
PYSQLITE_CONNECTION_EXECUTEMANY_METHODDEF
PYSQLITE_CONNECTION_EXECUTESCRIPT_METHODDEF
PYSQLITE_CONNECTION_EXECUTE_METHODDEF
PYSQLITE_CONNECTION_EXIT_METHODDEF
PYSQLITE_CONNECTION_INTERRUPT_METHODDEF
PYSQLITE_CONNECTION_ITERDUMP_METHODDEF
PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF
PYSQLITE_CONNECTION_ROLLBACK_METHODDEF
PYSQLITE_CONNECTION_SET_AUTHORIZER_METHODDEF
PYSQLITE_CONNECTION_SET_PROGRESS_HANDLER_METHODDEF
PYSQLITE_CONNECTION_SET_TRACE_CALLBACK_METHODDEF
SETLIMIT_METHODDEF
GETLIMIT_METHODDEF
SERIALIZE_METHODDEF
DESERIALIZE_METHODDEF
CREATE_WINDOW_FUNCTION_METHODDEF
BLOBOPEN_METHODDEF
SETCONFIG_METHODDEF
GETCONFIG_METHODDEF
{NULL, NULL}
};
static struct PyMemberDef connection_members[] =
{
{"Warning", _Py_T_OBJECT, offsetof(pysqlite_Connection, Warning), Py_READONLY},
{"Error", _Py_T_OBJECT, offsetof(pysqlite_Connection, Error), Py_READONLY},
{"InterfaceError", _Py_T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), Py_READONLY},
{"DatabaseError", _Py_T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), Py_READONLY},
{"DataError", _Py_T_OBJECT, offsetof(pysqlite_Connection, DataError), Py_READONLY},
{"OperationalError", _Py_T_OBJECT, offsetof(pysqlite_Connection, OperationalError), Py_READONLY},
{"IntegrityError", _Py_T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), Py_READONLY},
{"InternalError", _Py_T_OBJECT, offsetof(pysqlite_Connection, InternalError), Py_READONLY},
{"ProgrammingError", _Py_T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), Py_READONLY},
{"NotSupportedError", _Py_T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), Py_READONLY},
{"row_factory", _Py_T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
{"text_factory", _Py_T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
{NULL}
};
static PyType_Slot connection_slots[] = {
{Py_tp_finalize, connection_finalize},
{Py_tp_dealloc, connection_dealloc},
{Py_tp_doc, (void *)connection_doc},
{Py_tp_methods, connection_methods},
{Py_tp_members, connection_members},
{Py_tp_getset, connection_getset},
{Py_tp_init, pysqlite_connection_init},
{Py_tp_call, pysqlite_connection_call},
{Py_tp_traverse, connection_traverse},
{Py_tp_clear, connection_clear},
{0, NULL},
};
static PyType_Spec connection_spec = {
.name = MODULE_NAME ".Connection",
.basicsize = sizeof(pysqlite_Connection),
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE),
.slots = connection_slots,
};
int
pysqlite_connection_setup_types(PyObject *module)
{
PyObject *type = PyType_FromModuleAndSpec(module, &connection_spec, NULL);
if (type == NULL) {
return -1;
}
pysqlite_state *state = pysqlite_get_state(module);
state->ConnectionType = (PyTypeObject *)type;
return 0;
}
|