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
|
/*
+----------------------------------------------------------------------+
| PHP Version 4 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2003 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.0 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_0.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Wez Furlong <wez@thebrainroom.com> |
| Tal Peer <tal@php.net> |
| Marcus Boerger <helly@php.net> |
+----------------------------------------------------------------------+
$Id$
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#define PHP_SQLITE_MODULE_VERSION "1.0.3"
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_sqlite.h"
#if HAVE_TIME_H
# include <time.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sqlite.h>
#ifndef safe_emalloc
# define safe_emalloc(a,b,c) emalloc((a)*(b)+(c))
#endif
#ifndef ZEND_ENGINE_2
# define OnUpdateLong OnUpdateInt
#endif
ZEND_DECLARE_MODULE_GLOBALS(sqlite)
extern int sqlite_encode_binary(const unsigned char *in, int n, unsigned char *out);
extern int sqlite_decode_binary(const unsigned char *in, unsigned char *out);
static unsigned char arg3_force_ref[] = {3, BYREF_NONE, BYREF_NONE, BYREF_FORCE };
static int le_sqlite_db, le_sqlite_result, le_sqlite_pdb;
static inline void php_sqlite_strtoupper(char *s)
{
while (*s!='\0') {
*s = toupper(*s);
s++;
}
}
static inline void php_sqlite_strtolower(char *s)
{
while (*s!='\0') {
*s = tolower(*s);
s++;
}
}
/* {{{ PHP_INI
*/
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY_EX("sqlite.assoc_case", "0", PHP_INI_ALL, OnUpdateLong, assoc_case, zend_sqlite_globals, sqlite_globals, display_link_numbers)
PHP_INI_END()
/* }}} */
#define DB_FROM_ZVAL(db, zv) ZEND_FETCH_RESOURCE2(db, struct php_sqlite_db *, zv, -1, "sqlite database", le_sqlite_db, le_sqlite_pdb)
#define PHP_SQLITE_EMPTY_QUERY \
if (!sql_len) { \
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot execute empty query."); \
RETURN_FALSE; \
}
struct php_sqlite_result {
struct php_sqlite_db *db;
sqlite_vm *vm;
int buffered;
int ncolumns;
int nrows;
int curr_row;
char **col_names;
int alloc_rows;
char **table;
int mode;
};
struct php_sqlite_db {
sqlite *db;
int last_err_code;
int is_persistent;
int rsrc_id;
HashTable callbacks;
};
struct php_sqlite_agg_functions {
struct php_sqlite_db *db;
int is_valid;
zval *step;
zval *fini;
};
enum { PHPSQLITE_ASSOC = 1, PHPSQLITE_NUM = 2, PHPSQLITE_BOTH = PHPSQLITE_ASSOC|PHPSQLITE_NUM };
function_entry sqlite_functions[] = {
PHP_FE(sqlite_open, arg3_force_ref)
PHP_FE(sqlite_popen, arg3_force_ref)
PHP_FE(sqlite_close, NULL)
PHP_FE(sqlite_query, NULL)
PHP_FE(sqlite_exec, NULL)
PHP_FE(sqlite_array_query, NULL)
PHP_FE(sqlite_single_query, NULL)
PHP_FE(sqlite_fetch_array, NULL)
PHP_FE(sqlite_fetch_single, NULL)
PHP_FALIAS(sqlite_fetch_string, sqlite_fetch_single, NULL)
PHP_FE(sqlite_fetch_all, NULL)
PHP_FE(sqlite_current, NULL)
PHP_FE(sqlite_column, NULL)
PHP_FE(sqlite_libversion, NULL)
PHP_FE(sqlite_libencoding, NULL)
PHP_FE(sqlite_changes, NULL)
PHP_FE(sqlite_last_insert_rowid, NULL)
PHP_FE(sqlite_num_rows, NULL)
PHP_FE(sqlite_num_fields, NULL)
PHP_FE(sqlite_field_name, NULL)
PHP_FE(sqlite_seek, NULL)
PHP_FE(sqlite_rewind, NULL)
PHP_FE(sqlite_next, NULL)
PHP_FE(sqlite_has_more, NULL)
PHP_FE(sqlite_escape_string, NULL)
PHP_FE(sqlite_busy_timeout, NULL)
PHP_FE(sqlite_last_error, NULL)
PHP_FE(sqlite_error_string, NULL)
PHP_FE(sqlite_unbuffered_query, NULL)
PHP_FE(sqlite_create_aggregate, NULL)
PHP_FE(sqlite_create_function, NULL)
PHP_FE(sqlite_udf_encode_binary, NULL)
PHP_FE(sqlite_udf_decode_binary, NULL)
{NULL, NULL, NULL}
};
zend_module_entry sqlite_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"sqlite",
sqlite_functions,
PHP_MINIT(sqlite),
NULL,
NULL,
PHP_RSHUTDOWN(sqlite),
PHP_MINFO(sqlite),
#if ZEND_MODULE_API_NO >= 20010901
PHP_SQLITE_MODULE_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_SQLITE
ZEND_GET_MODULE(sqlite)
#endif
static int php_sqlite_callback_invalidator(struct php_sqlite_agg_functions *funcs TSRMLS_DC)
{
if (!funcs->is_valid) {
return 0;
}
if (funcs->step) {
zval_ptr_dtor(&funcs->step);
funcs->step = NULL;
}
if (funcs->fini) {
zval_ptr_dtor(&funcs->fini);
funcs->fini = NULL;
}
funcs->is_valid = 0;
return 0;
}
static void php_sqlite_callback_dtor(void *pDest)
{
struct php_sqlite_agg_functions *funcs = (struct php_sqlite_agg_functions*)pDest;
if (funcs->is_valid) {
TSRMLS_FETCH();
php_sqlite_callback_invalidator(funcs TSRMLS_CC);
}
}
static ZEND_RSRC_DTOR_FUNC(php_sqlite_db_dtor)
{
if (rsrc->ptr) {
struct php_sqlite_db *db = (struct php_sqlite_db*)rsrc->ptr;
sqlite_close(db->db);
zend_hash_destroy(&db->callbacks);
pefree(db, db->is_persistent);
rsrc->ptr = NULL;
}
}
static void real_result_dtor(struct php_sqlite_result *res TSRMLS_DC)
{
int i, j, base;
if (res->vm) {
sqlite_finalize(res->vm, NULL);
}
if (res->table) {
if (!res->buffered && res->nrows) {
res->nrows = 1; /* only one row is stored */
}
for (i = 0; i < res->nrows; i++) {
base = i * res->ncolumns;
for (j = 0; j < res->ncolumns; j++) {
if (res->table[base + j] != NULL) {
efree(res->table[base + j]);
}
}
}
efree(res->table);
}
if (res->col_names) {
for (j = 0; j < res->ncolumns; j++) {
efree(res->col_names[j]);
}
efree(res->col_names);
}
if (res->db) {
zend_list_delete(res->db->rsrc_id);
}
efree(res);
}
static ZEND_RSRC_DTOR_FUNC(php_sqlite_result_dtor)
{
struct php_sqlite_result *res = (struct php_sqlite_result *)rsrc->ptr;
real_result_dtor(res TSRMLS_CC);
}
static int php_sqlite_forget_persistent_id_numbers(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
struct php_sqlite_db *db = (struct php_sqlite_db*)rsrc->ptr;
if (Z_TYPE_P(rsrc) != le_sqlite_pdb) {
return 0;
}
/* prevent bad mojo if someone tries to use a previously registered function in the next request */
zend_hash_apply(&db->callbacks, (apply_func_t)php_sqlite_callback_invalidator TSRMLS_CC);
db->rsrc_id = FAILURE;
/* don't leave pending commits hanging around */
sqlite_exec(db->db, "ROLLBACK", NULL, NULL, NULL);
return 0;
}
PHP_RSHUTDOWN_FUNCTION(sqlite)
{
zend_hash_apply(&EG(persistent_list), (apply_func_t)php_sqlite_forget_persistent_id_numbers TSRMLS_CC);
return SUCCESS;
}
/* {{{ PHP Function interface */
static void php_sqlite_generic_function_callback(sqlite_func *func, int argc, const char **argv)
{
zval *retval = NULL;
zval ***zargs = NULL;
zval funcname;
int i, res;
char *callable = NULL, *errbuf=NULL;
TSRMLS_FETCH();
/* sanity check the args */
if (argc == 0) {
sqlite_set_result_error(func, "not enough parameters", -1);
return;
}
ZVAL_STRING(&funcname, (char*)argv[0], 0);
if (!zend_is_callable(&funcname, 0, &callable)) {
spprintf(&errbuf, 0, "function `%s' is not callable", callable);
sqlite_set_result_error(func, errbuf, -1);
efree(errbuf);
efree(callable);
return;
}
efree(callable);
if (argc > 1) {
zargs = (zval ***)safe_emalloc((argc - 1), sizeof(zval **), 0);
for (i = 0; i < argc-1; i++) {
zargs[i] = emalloc(sizeof(zval *));
MAKE_STD_ZVAL(*zargs[i]);
ZVAL_STRING(*zargs[i], (char*)argv[i+1], 1);
}
}
res = call_user_function_ex(EG(function_table),
NULL,
&funcname,
&retval,
argc-1,
zargs,
0, NULL TSRMLS_CC);
if (res == SUCCESS) {
if (retval == NULL) {
sqlite_set_result_string(func, NULL, 0);
} else {
switch (Z_TYPE_P(retval)) {
case IS_STRING:
sqlite_set_result_string(func, Z_STRVAL_P(retval), Z_STRLEN_P(retval));
break;
case IS_LONG:
case IS_BOOL:
sqlite_set_result_int(func, Z_LVAL_P(retval));
break;
case IS_DOUBLE:
sqlite_set_result_double(func, Z_DVAL_P(retval));
break;
case IS_NULL:
default:
sqlite_set_result_string(func, NULL, 0);
}
}
} else {
sqlite_set_result_error(func, "call_user_function_ex failed", -1);
}
if (retval) {
zval_ptr_dtor(&retval);
}
if (zargs) {
for (i = 0; i < argc-1; i++) {
zval_ptr_dtor(zargs[i]);
efree(zargs[i]);
}
efree(zargs);
}
}
/* }}} */
/* {{{ callback for sqlite_create_function */
static void php_sqlite_function_callback(sqlite_func *func, int argc, const char **argv)
{
zval *retval = NULL;
zval ***zargs = NULL;
int i, res;
struct php_sqlite_agg_functions *funcs = sqlite_user_data(func);
TSRMLS_FETCH();
if (!funcs->is_valid) {
sqlite_set_result_error(func, "this function has not been correctly defined for this request", -1);
return;
}
if (argc > 0) {
zargs = (zval ***)safe_emalloc(argc, sizeof(zval **), 0);
for (i = 0; i < argc; i++) {
zargs[i] = emalloc(sizeof(zval *));
MAKE_STD_ZVAL(*zargs[i]);
if (argv[i] == NULL) {
ZVAL_NULL(*zargs[i]);
} else {
ZVAL_STRING(*zargs[i], (char*)argv[i], 1);
}
}
}
res = call_user_function_ex(EG(function_table),
NULL,
funcs->step,
&retval,
argc,
zargs,
0, NULL TSRMLS_CC);
if (res == SUCCESS) {
if (retval == NULL) {
sqlite_set_result_string(func, NULL, 0);
} else {
switch (Z_TYPE_P(retval)) {
case IS_STRING:
/* TODO: for binary results, need to encode the string */
sqlite_set_result_string(func, Z_STRVAL_P(retval), Z_STRLEN_P(retval));
break;
case IS_LONG:
case IS_BOOL:
sqlite_set_result_int(func, Z_LVAL_P(retval));
break;
case IS_DOUBLE:
sqlite_set_result_double(func, Z_DVAL_P(retval));
break;
case IS_NULL:
default:
sqlite_set_result_string(func, NULL, 0);
}
}
} else {
sqlite_set_result_error(func, "call_user_function_ex failed", -1);
}
if (retval) {
zval_ptr_dtor(&retval);
}
if (zargs) {
for (i = 0; i < argc; i++) {
zval_ptr_dtor(zargs[i]);
efree(zargs[i]);
}
efree(zargs);
}
}
/* }}} */
/* {{{ callback for sqlite_create_aggregate: step function */
static void php_sqlite_agg_step_function_callback(sqlite_func *func, int argc, const char **argv)
{
zval *retval = NULL;
zval ***zargs;
zval **context_p;
int i, res, zargc;
struct php_sqlite_agg_functions *funcs = sqlite_user_data(func);
TSRMLS_FETCH();
if (!funcs->is_valid) {
sqlite_set_result_error(func, "this function has not been correctly defined for this request", -1);
return;
}
/* sanity check the args */
if (argc < 1) {
return;
}
zargc = argc + 1;
zargs = (zval ***)safe_emalloc(zargc, sizeof(zval **), 0);
/* first arg is always the context zval */
context_p = (zval **)sqlite_aggregate_context(func, sizeof(*context_p));
if (*context_p == NULL) {
MAKE_STD_ZVAL(*context_p);
(*context_p)->is_ref = 1;
Z_TYPE_PP(context_p) = IS_NULL;
}
zargs[0] = context_p;
/* copy the other args */
for (i = 0; i < argc; i++) {
zargs[i+1] = emalloc(sizeof(zval *));
MAKE_STD_ZVAL(*zargs[i+1]);
if (argv[i] == NULL) {
ZVAL_NULL(*zargs[i+1]);
} else {
ZVAL_STRING(*zargs[i+1], (char*)argv[i], 1);
}
}
res = call_user_function_ex(EG(function_table),
NULL,
funcs->step,
&retval,
zargc,
zargs,
0, NULL TSRMLS_CC);
if (res != SUCCESS) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "call_user_function_ex failed");
}
if (retval) {
zval_ptr_dtor(&retval);
}
if (zargs) {
for (i = 1; i < zargc; i++) {
zval_ptr_dtor(zargs[i]);
efree(zargs[i]);
}
efree(zargs);
}
}
/* }}} */
/* {{{ callback for sqlite_create_aggregate: finalize function */
static void php_sqlite_agg_fini_function_callback(sqlite_func *func)
{
zval *retval = NULL;
int res;
struct php_sqlite_agg_functions *funcs = sqlite_user_data(func);
zval **context_p;
TSRMLS_FETCH();
if (!funcs->is_valid) {
sqlite_set_result_error(func, "this function has not been correctly defined for this request", -1);
return;
}
context_p = (zval **)sqlite_aggregate_context(func, sizeof(*context_p));
res = call_user_function_ex(EG(function_table),
NULL,
funcs->fini,
&retval,
1,
&context_p,
0, NULL TSRMLS_CC);
if (res == SUCCESS) {
if (retval == NULL) {
sqlite_set_result_string(func, NULL, 0);
} else {
switch (Z_TYPE_P(retval)) {
case IS_STRING:
/* TODO: for binary results, need to encode the string */
sqlite_set_result_string(func, Z_STRVAL_P(retval), Z_STRLEN_P(retval));
break;
case IS_LONG:
case IS_BOOL:
sqlite_set_result_int(func, Z_LVAL_P(retval));
break;
case IS_DOUBLE:
sqlite_set_result_double(func, Z_DVAL_P(retval));
break;
case IS_NULL:
default:
sqlite_set_result_string(func, NULL, 0);
}
}
} else {
sqlite_set_result_error(func, "call_user_function_ex failed", -1);
}
if (retval) {
zval_ptr_dtor(&retval);
}
zval_ptr_dtor(context_p);
}
/* }}} */
/* {{{ Authorization Callback */
static int php_sqlite_authorizer(void *autharg, int access_type, const char *arg3, const char *arg4,
const char *arg5, const char *arg6)
{
switch (access_type) {
case SQLITE_COPY:
if (strncmp(arg4, ":memory:", sizeof(":memory:") - 1)) {
TSRMLS_FETCH();
if (PG(safe_mode) && (!php_checkuid(arg4, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
return SQLITE_DENY;
}
if (php_check_open_basedir(arg4 TSRMLS_CC)) {
return SQLITE_DENY;
}
}
return SQLITE_OK;
#ifdef SQLITE_ATTACH
case SQLITE_ATTACH:
if (strncmp(arg3, ":memory:", sizeof(":memory:") - 1)) {
TSRMLS_FETCH();
if (PG(safe_mode) && (!php_checkuid(arg3, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
return SQLITE_DENY;
}
if (php_check_open_basedir(arg3 TSRMLS_CC)) {
return SQLITE_DENY;
}
}
return SQLITE_OK;
#endif
default:
/* access allowed */
return SQLITE_OK;
}
}
/* }}} */
static int init_sqlite_globals(zend_sqlite_globals *g)
{
g->assoc_case = 0;
return SUCCESS;
}
PHP_MINIT_FUNCTION(sqlite)
{
ZEND_INIT_MODULE_GLOBALS(sqlite, init_sqlite_globals, NULL);
REGISTER_INI_ENTRIES();
le_sqlite_db = zend_register_list_destructors_ex(php_sqlite_db_dtor, NULL, "sqlite database", module_number);
le_sqlite_pdb = zend_register_list_destructors_ex(NULL, php_sqlite_db_dtor, "sqlite database (persistent)", module_number);
le_sqlite_result = zend_register_list_destructors_ex(php_sqlite_result_dtor, NULL, "sqlite result", module_number);
REGISTER_LONG_CONSTANT("SQLITE_BOTH", PHPSQLITE_BOTH, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SQLITE_NUM", PHPSQLITE_NUM, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SQLITE_ASSOC", PHPSQLITE_ASSOC, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SQLITE_OK", SQLITE_OK, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SQLITE_ERROR", SQLITE_ERROR, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SQLITE_INTERNAL", SQLITE_INTERNAL, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SQLITE_PERM", SQLITE_PERM, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SQLITE_ABORT", SQLITE_ABORT, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SQLITE_BUSY", SQLITE_BUSY, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SQLITE_LOCKED", SQLITE_LOCKED, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SQLITE_NOMEM", SQLITE_NOMEM, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SQLITE_READONLY", SQLITE_READONLY, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SQLITE_INTERRUPT", SQLITE_INTERRUPT, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SQLITE_IOERR", SQLITE_IOERR, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SQLITE_CORRUPT", SQLITE_CORRUPT, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SQLITE_NOTFOUND", SQLITE_NOTFOUND, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SQLITE_FULL", SQLITE_FULL, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SQLITE_CANTOPEN", SQLITE_CANTOPEN, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SQLITE_PROTOCOL", SQLITE_PROTOCOL, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SQLITE_EMPTY", SQLITE_EMPTY, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SQLITE_SCHEMA", SQLITE_SCHEMA, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SQLITE_TOOBIG", SQLITE_TOOBIG, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SQLITE_CONSTRAINT", SQLITE_CONSTRAINT, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SQLITE_MISMATCH", SQLITE_MISMATCH, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SQLITE_MISUSE", SQLITE_MISUSE, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SQLITE_NOLFS", SQLITE_NOLFS, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SQLITE_AUTH", SQLITE_AUTH, CONST_CS|CONST_PERSISTENT);
#ifdef SQLITE_FORMAT
REGISTER_LONG_CONSTANT("SQLITE_FORMAT", SQLITE_FORMAT, CONST_CS|CONST_PERSISTENT);
#endif
#ifdef SQLITE_RANGE
REGISTER_LONG_CONSTANT("SQLITE_RANGE", SQLITE_RANGE, CONST_CS|CONST_PERSISTENT);
#endif
#ifdef SQLITE_NOTADB
REGISTER_LONG_CONSTANT("SQLITE_NOTADB", SQLITE_NOTADB, CONST_CS|CONST_PERSISTENT);
#endif
REGISTER_LONG_CONSTANT("SQLITE_ROW", SQLITE_ROW, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SQLITE_DONE", SQLITE_DONE, CONST_CS|CONST_PERSISTENT);
return SUCCESS;
}
PHP_MINFO_FUNCTION(sqlite)
{
php_info_print_table_start();
php_info_print_table_header(2, "SQLite support", "enabled");
php_info_print_table_row(2, "PECL Module version", PHP_SQLITE_MODULE_VERSION " $Id$");
php_info_print_table_row(2, "SQLite Library", sqlite_libversion());
php_info_print_table_row(2, "SQLite Encoding", sqlite_libencoding());
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
static struct php_sqlite_db *php_sqlite_open(char *filename, int mode, char *persistent_id, zval *return_value, zval *errmsg TSRMLS_DC)
{
char *errtext = NULL;
sqlite *sdb = NULL;
struct php_sqlite_db *db = NULL;
sdb = sqlite_open(filename, mode, &errtext);
if (sdb == NULL) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", errtext);
if (errmsg) {
ZVAL_STRING(errmsg, errtext, 1);
}
sqlite_freemem(errtext);
RETVAL_FALSE;
return NULL;
}
db = (struct php_sqlite_db *)pemalloc(sizeof(struct php_sqlite_db), persistent_id ? 1 : 0);
db->is_persistent = persistent_id ? 1 : 0;
db->last_err_code = SQLITE_OK;
db->db = sdb;
zend_hash_init(&db->callbacks, 0, NULL, php_sqlite_callback_dtor, db->is_persistent);
/* register the PHP functions */
sqlite_create_function(sdb, "php", -1, php_sqlite_generic_function_callback, 0);
/* set default busy handler; keep retrying up until 1 minute has passed,
* then fail with a busy status code */
sqlite_busy_timeout(sdb, 60000);
/* authorizer hook so we can enforce safe mode
* Note: the declaration of php_sqlite_authorizer is correct for 2.8.2 of libsqlite,
* and IS backwards binary compatible with earlier versions */
sqlite_set_authorizer(sdb, php_sqlite_authorizer, NULL);
db->rsrc_id = ZEND_REGISTER_RESOURCE(return_value, db, persistent_id ? le_sqlite_pdb : le_sqlite_db);
if (persistent_id) {
list_entry le;
Z_TYPE(le) = le_sqlite_pdb;
le.ptr = db;
if (FAILURE == zend_hash_update(&EG(persistent_list), persistent_id,
strlen(persistent_id)+1,
(void *)&le, sizeof(le), NULL)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to register persistent resource");
}
}
return db;
}
/* {{{ proto resource sqlite_popen(string filename [, int mode, string &errmessage])
Opens a persistent handle to an SQLite database. Will create the database if it does not exist */
PHP_FUNCTION(sqlite_popen)
{
int mode = 0666;
char *filename, *fullpath, *hashkey;
long filename_len, hashkeylen;
zval *errmsg = NULL;
struct php_sqlite_db *db = NULL;
list_entry *le;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lz/",
&filename, &filename_len, &mode, &errmsg)) {
return;
}
if (errmsg) {
zval_dtor(errmsg);
}
if (strncmp(filename, ":memory:", sizeof(":memory:") - 1)) {
/* resolve the fully-qualified path name to use as the hash key */
fullpath = expand_filepath(filename, NULL TSRMLS_CC);
if (PG(safe_mode) && (!php_checkuid(fullpath, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
RETURN_FALSE;
}
if (php_check_open_basedir(fullpath TSRMLS_CC)) {
RETURN_FALSE;
}
} else {
fullpath = estrndup(filename, filename_len);
}
hashkeylen = spprintf(&hashkey, 0, "sqlite_pdb_%s:%d", fullpath, mode);
/* do we have an existing persistent connection ? */
if (SUCCESS == zend_hash_find(&EG(persistent_list), hashkey, hashkeylen+1, (void*)&le)) {
if (Z_TYPE_P(le) == le_sqlite_pdb) {
db = (struct php_sqlite_db*)le->ptr;
if (db->rsrc_id == FAILURE) {
/* give it a valid resource id for this request */
db->rsrc_id = ZEND_REGISTER_RESOURCE(return_value, db, le_sqlite_pdb);
} else {
int type;
/* sanity check to ensure that the resource is still a valid regular resource
* number */
if (_zend_list_find(db->rsrc_id, &type TSRMLS_CC) == db) {
/* already accessed this request; map it */
zend_list_addref(db->rsrc_id);
ZVAL_RESOURCE(return_value, db->rsrc_id);
} else {
db->rsrc_id = ZEND_REGISTER_RESOURCE(return_value, db, le_sqlite_pdb);
}
}
/* all set */
efree(fullpath);
efree(hashkey);
return;
}
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Some other type of persistent resource is using this hash key!?");
RETURN_FALSE;
}
/* now we need to open the database */
php_sqlite_open(fullpath, mode, hashkey, return_value, errmsg TSRMLS_CC);
efree(fullpath);
efree(hashkey);
}
/* }}} */
/* {{{ proto resource sqlite_open(string filename [, int mode, string &errmessage])
Opens an SQLite database. Will create the database if it does not exist */
PHP_FUNCTION(sqlite_open)
{
int mode = 0666;
char *filename, *fullpath = NULL;
long filename_len;
zval *errmsg = NULL;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lz/",
&filename, &filename_len, &mode, &errmsg)) {
return;
}
if (errmsg) {
zval_dtor(errmsg);
}
if (strncmp(filename, ":memory:", sizeof(":memory:") - 1)) {
/* resolve the fully-qualified path name to use as the hash key */
fullpath = expand_filepath(filename, NULL TSRMLS_CC);
if (PG(safe_mode) && (!php_checkuid(fullpath, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
efree(fullpath);
RETURN_FALSE;
}
if (php_check_open_basedir(fullpath TSRMLS_CC)) {
efree(fullpath);
RETURN_FALSE;
}
}
php_sqlite_open(fullpath ? fullpath : filename, mode, NULL, return_value, errmsg TSRMLS_CC);
if (fullpath) {
efree(fullpath);
}
}
/* }}} */
/* {{{ proto void sqlite_busy_timeout(resource db, int ms)
Set busy timeout duration. If ms <= 0, all busy handlers are disabled */
PHP_FUNCTION(sqlite_busy_timeout)
{
zval *zdb;
struct php_sqlite_db *db;
long ms;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &zdb, &ms)) {
return;
}
DB_FROM_ZVAL(db, &zdb);
sqlite_busy_timeout(db->db, ms);
}
/* }}} */
/* {{{ proto void sqlite_close(resource db)
Closes an open sqlite database */
PHP_FUNCTION(sqlite_close)
{
zval *zdb;
struct php_sqlite_db *db;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zdb)) {
return;
}
DB_FROM_ZVAL(db, &zdb);
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zdb)) {
return;
}
DB_FROM_ZVAL(db, &zdb);
zend_list_delete(Z_RESVAL_P(zdb));
}
/* }}} */
/* {{{ php_sqlite_fetch */
int php_sqlite_fetch(struct php_sqlite_result *rres TSRMLS_DC)
{
const char **rowdata, **colnames;
int ret, i, base;
char *errtext = NULL, *colname;
next_row:
ret = sqlite_step(rres->vm, &rres->ncolumns, &rowdata, &colnames);
if (!rres->nrows) {
/* first row - lets copy the column names */
rres->col_names = safe_emalloc(rres->ncolumns, sizeof(char *), 0);
for (i = 0; i < rres->ncolumns; i++) {
colname = (char*)colnames[i];
if (SQLITE_G(assoc_case) == 1) {
php_sqlite_strtoupper(colname);
} else if (SQLITE_G(assoc_case) == 2) {
php_sqlite_strtolower(colname);
}
rres->col_names[i] = estrdup(colname);
}
if (!rres->buffered) {
/* non buffered mode - also fetch memory for on single row */
rres->table = safe_emalloc(rres->ncolumns, sizeof(char *), 0);
}
}
switch (ret) {
case SQLITE_ROW:
if (rres->buffered) {
/* add the row to our collection */
if (rres->nrows + 1 >= rres->alloc_rows) {
rres->alloc_rows = rres->alloc_rows ? rres->alloc_rows * 2 : 16;
rres->table = erealloc(rres->table, rres->alloc_rows * rres->ncolumns * sizeof(char *));
}
base = rres->nrows * rres->ncolumns;
for (i = 0; i < rres->ncolumns; i++) {
if (rowdata[i]) {
rres->table[base + i] = estrdup(rowdata[i]);
} else {
rres->table[base + i] = NULL;
}
}
rres->nrows++;
goto next_row;
} else {
/* non buffered: only fetch one row but first free data if not first row */
if (rres->nrows++) {
for (i = 0; i < rres->ncolumns; i++) {
if (rres->table[i]) {
efree(rres->table[i]);
}
}
}
for (i = 0; i < rres->ncolumns; i++) {
if (rowdata[i]) {
rres->table[i] = estrdup(rowdata[i]);
} else {
rres->table[i] = NULL;
}
}
}
ret = SQLITE_OK;
break;
case SQLITE_BUSY:
case SQLITE_ERROR:
case SQLITE_MISUSE:
case SQLITE_DONE:
default:
if (rres->vm) {
ret = sqlite_finalize(rres->vm, &errtext);
}
rres->vm = NULL;
if (ret != SQLITE_OK) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", errtext);
sqlite_freemem(errtext);
}
break;
}
rres->db->last_err_code = ret;
return ret;
}
/* }}} */
/* {{{ sqlite_query */
void sqlite_query(struct php_sqlite_db *db, char *sql, long sql_len, int mode, int buffered, zval *return_value, struct php_sqlite_result *rres TSRMLS_DC)
{
struct php_sqlite_result res;
int ret;
char *errtext = NULL;
const char *tail;
memset(&res, 0, sizeof(res));
res.buffered = buffered;
res.mode = mode;
ret = sqlite_compile(db->db, sql, &tail, &res.vm, &errtext);
db->last_err_code = ret;
if (ret != SQLITE_OK) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", errtext);
sqlite_freemem(errtext);
goto terminate;
} else if (!res.vm) { /* empty query */
terminate:
if (return_value) {
RETURN_FALSE;
} else {
efree(rres);
return;
}
}
if (!rres) {
rres = (struct php_sqlite_result*)emalloc(sizeof(*rres));
}
memcpy(rres, &res, sizeof(*rres));
rres->db = db;
zend_list_addref(db->rsrc_id);
/* now the result set is ready for stepping: get first row */
if (php_sqlite_fetch(rres TSRMLS_CC) != SQLITE_OK) {
real_result_dtor(rres TSRMLS_CC);
if (return_value) {
RETURN_FALSE;
} else {
return;
}
}
rres->curr_row = 0;
if (return_value) {
ZEND_REGISTER_RESOURCE(return_value, rres, le_sqlite_result);
}
}
/* }}} */
/* {{{ proto resource sqlite_unbuffered_query(string query, resource db [ , int result_type ])
Execute a query that does not prefetch and buffer all data */
PHP_FUNCTION(sqlite_unbuffered_query)
{
zval *zdb;
struct php_sqlite_db *db;
char *sql;
long sql_len;
int mode = PHPSQLITE_BOTH;
char *errtext = NULL;
if (FAILURE == zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET,
ZEND_NUM_ARGS() TSRMLS_CC, "sr|l", &sql, &sql_len, &zdb, &mode) &&
FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|l", &zdb, &sql, &sql_len, &mode)) {
return;
}
DB_FROM_ZVAL(db, &zdb);
PHP_SQLITE_EMPTY_QUERY;
/* avoid doing work if we can */
if (!return_value_used) {
db->last_err_code = sqlite_exec(db->db, sql, NULL, NULL, &errtext);
if (db->last_err_code != SQLITE_OK) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", errtext);
sqlite_freemem(errtext);
}
return;
}
sqlite_query(db, sql, sql_len, mode, 0, return_value, NULL TSRMLS_CC);
}
/* }}} */
/* {{{ proto resource sqlite_query(string query, resource db [ , int result_type ])
Executes a query against a given database and returns a result handle */
PHP_FUNCTION(sqlite_query)
{
zval *zdb;
struct php_sqlite_db *db;
char *sql;
long sql_len;
int mode = PHPSQLITE_BOTH;
char *errtext = NULL;
if (FAILURE == zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET,
ZEND_NUM_ARGS() TSRMLS_CC, "sr|l", &sql, &sql_len, &zdb, &mode) &&
FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|l", &zdb, &sql, &sql_len, &mode)) {
return;
}
DB_FROM_ZVAL(db, &zdb);
PHP_SQLITE_EMPTY_QUERY;
/* avoid doing work if we can */
if (!return_value_used) {
db->last_err_code = sqlite_exec(db->db, sql, NULL, NULL, &errtext);
if (db->last_err_code != SQLITE_OK) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", errtext);
sqlite_freemem(errtext);
}
return;
}
sqlite_query(db, sql, sql_len, mode, 1, return_value, NULL TSRMLS_CC);
}
/* }}} */
/* {{{ proto boolean sqlite_exec(string query, resource db)
Executes a result-less query against a given database */
PHP_FUNCTION(sqlite_exec)
{
zval *zdb;
struct php_sqlite_db *db;
char *sql;
long sql_len;
char *errtext = NULL;
if (FAILURE == zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET,
ZEND_NUM_ARGS() TSRMLS_CC, "sr", &sql, &sql_len, &zdb) &&
FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &zdb, &sql, &sql_len)) {
return;
}
DB_FROM_ZVAL(db, &zdb);
PHP_SQLITE_EMPTY_QUERY;
db->last_err_code = sqlite_exec(db->db, sql, NULL, NULL, &errtext);
if (db->last_err_code != SQLITE_OK) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", errtext);
sqlite_freemem(errtext);
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ php_sqlite_fetch_array */
static void php_sqlite_fetch_array(struct php_sqlite_result *res, int mode, zend_bool decode_binary, int move_next, zval *return_value TSRMLS_DC)
{
int j, buffered = res->buffered;
const char **rowdata, **colnames;
/* check range of the row */
if (res->curr_row >= res->nrows) {
/* no more */
RETURN_FALSE;
}
colnames = (const char**)res->col_names;
if (res->buffered) {
rowdata = (const char**)&res->table[res->curr_row * res->ncolumns];
} else {
rowdata = (const char**)res->table;
}
/* now populate the result */
array_init(return_value);
for (j = 0; j < res->ncolumns; j++) {
zval *decoded;
MAKE_STD_ZVAL(decoded);
if (rowdata[j] == NULL) {
ZVAL_NULL(decoded);
} else if (decode_binary && rowdata[j][0] == '\x01') {
Z_STRVAL_P(decoded) = emalloc(strlen(rowdata[j]));
Z_STRLEN_P(decoded) = sqlite_decode_binary(rowdata[j]+1, Z_STRVAL_P(decoded));
Z_STRVAL_P(decoded)[Z_STRLEN_P(decoded)] = '\0';
Z_TYPE_P(decoded) = IS_STRING;
if (!buffered) {
efree((char*)rowdata[j]);
rowdata[j] = NULL;
}
} else {
ZVAL_STRING(decoded, (char*)rowdata[j], buffered);
if (!buffered) {
rowdata[j] = NULL;
}
}
if (mode & PHPSQLITE_NUM) {
if (mode & PHPSQLITE_ASSOC) {
add_index_zval(return_value, j, decoded);
ZVAL_ADDREF(decoded);
add_assoc_zval(return_value, (char*)colnames[j], decoded);
} else {
add_next_index_zval(return_value, decoded);
}
} else {
add_assoc_zval(return_value, (char*)colnames[j], decoded);
}
}
if (move_next) {
if (!res->buffered) {
/* non buffered: fetch next row */
php_sqlite_fetch(res TSRMLS_CC);
}
/* advance the row pointer */
res->curr_row++;
}
}
/* }}} */
/* {{{ php_sqlite_fetch_column */
static void php_sqlite_fetch_column(struct php_sqlite_result *res, zval *which, zend_bool decode_binary, zval *return_value TSRMLS_DC)
{
int j;
const char **rowdata, **colnames;
/* check range of the row */
if (res->curr_row >= res->nrows) {
/* no more */
RETURN_FALSE;
}
colnames = (const char**)res->col_names;
if (Z_TYPE_P(which) == IS_LONG) {
j = Z_LVAL_P(which);
} else {
convert_to_string_ex(&which);
for (j = 0; j < res->ncolumns; j++) {
if (!strcasecmp((char*)colnames[j], Z_STRVAL_P(which))) {
break;
}
}
}
if (j < 0 || j >= res->ncolumns) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "No such column %d", j);
RETURN_FALSE;
}
if (res->buffered) {
rowdata = (const char**)&res->table[res->curr_row * res->ncolumns];
} else {
rowdata = (const char**)res->table;
}
if (rowdata[j] == NULL) {
RETURN_NULL();
} else if (decode_binary && rowdata[j][0] == '\x01') {
int l = strlen(rowdata[j]);
char *decoded = emalloc(l);
l = sqlite_decode_binary(rowdata[j]+1, decoded);
decoded[l] = '\0';
RETVAL_STRINGL(decoded, l, 0);
if (!res->buffered) {
efree((char*)rowdata[j]);
rowdata[j] = NULL;
}
} else {
RETVAL_STRING((char*)rowdata[j], res->buffered);
if (!res->buffered) {
rowdata[j] = NULL;
}
}
}
/* }}} */
/* {{{ proto array sqlite_fetch_all(resource result [, int result_type, bool decode_binary])
Fetches all rows from a result set as an array */
PHP_FUNCTION(sqlite_fetch_all)
{
zval *zres, *ent;
int mode = PHPSQLITE_BOTH;
zend_bool decode_binary = 1;
struct php_sqlite_result *res;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|lb", &zres, &mode, &decode_binary)) {
return;
}
ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);
if (ZEND_NUM_ARGS() < 2) {
mode = res->mode;
}
if (res->curr_row >= res->nrows && res->nrows) {
if (!res->buffered) {
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "One or more rowsets were already returned");
} else {
res->curr_row = 0;
}
}
array_init(return_value);
while (res->curr_row < res->nrows) {
MAKE_STD_ZVAL(ent);
php_sqlite_fetch_array(res, mode, decode_binary, 1, ent TSRMLS_CC);
add_next_index_zval(return_value, ent);
}
}
/* }}} */
/* {{{ proto array sqlite_fetch_array(resource result [, int result_type, bool decode_binary])
Fetches the next row from a result set as an array */
PHP_FUNCTION(sqlite_fetch_array)
{
zval *zres;
int mode = PHPSQLITE_BOTH;
zend_bool decode_binary = 1;
struct php_sqlite_result *res;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|lb", &zres, &mode, &decode_binary)) {
return;
}
ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);
if (ZEND_NUM_ARGS() < 2) {
mode = res->mode;
}
php_sqlite_fetch_array(res, mode, decode_binary, 1, return_value TSRMLS_CC);
}
/* }}} */
/* {{{ proto array sqlite_array_query(resource db, string query [ , int result_type, bool decode_binary ])
Executes a query against a given database and returns an array */
PHP_FUNCTION(sqlite_array_query)
{
zval *zdb, *ent;
struct php_sqlite_db *db;
struct php_sqlite_result *rres;
char *sql;
long sql_len;
int mode = PHPSQLITE_BOTH;
char *errtext = NULL;
zend_bool decode_binary = 1;
if (FAILURE == zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET,
ZEND_NUM_ARGS() TSRMLS_CC, "sr|lb", &sql, &sql_len, &zdb, &mode, &decode_binary) &&
FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|lb", &zdb, &sql, &sql_len, &mode, &decode_binary)) {
return;
}
DB_FROM_ZVAL(db, &zdb);
PHP_SQLITE_EMPTY_QUERY;
/* avoid doing work if we can */
if (!return_value_used) {
db->last_err_code = sqlite_exec(db->db, sql, NULL, NULL, &errtext);
if (db->last_err_code != SQLITE_OK) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", errtext);
sqlite_freemem(errtext);
}
return;
}
rres = (struct php_sqlite_result *)emalloc(sizeof(*rres));
sqlite_query(db, sql, sql_len, mode, 0, NULL, rres TSRMLS_CC);
if (db->last_err_code != SQLITE_OK) {
/* no need to free rres, as it will be freed by sqlite_query() for us */
RETURN_FALSE;
}
array_init(return_value);
while (rres->curr_row < rres->nrows) {
MAKE_STD_ZVAL(ent);
php_sqlite_fetch_array(rres, mode, decode_binary, 1, ent TSRMLS_CC);
add_next_index_zval(return_value, ent);
}
real_result_dtor(rres TSRMLS_CC);
}
/* }}} */
/* {{{ php_sqlite_fetch_single */
static void php_sqlite_fetch_single(struct php_sqlite_result *res, zend_bool decode_binary, zval *return_value TSRMLS_DC)
{
const char **rowdata;
char *decoded;
int decoded_len;
/* check range of the row */
if (res->curr_row >= res->nrows) {
/* no more */
RETURN_FALSE;
}
if (res->buffered) {
rowdata = (const char**)&res->table[res->curr_row * res->ncolumns];
} else {
rowdata = (const char**)res->table;
}
if (decode_binary && rowdata[0] != NULL && rowdata[0][0] == '\x01') {
decoded = emalloc(strlen(rowdata[0]));
decoded_len = sqlite_decode_binary(rowdata[0]+1, decoded);
if (!res->buffered) {
efree((char*)rowdata[0]);
rowdata[0] = NULL;
}
} else if (rowdata[0]) {
decoded_len = strlen((char*)rowdata[0]);
if (res->buffered) {
decoded = estrndup((char*)rowdata[0], decoded_len);
} else {
decoded = (char*)rowdata[0];
rowdata[0] = NULL;
}
} else {
decoded = NULL;
decoded_len = 0;
}
if (!res->buffered) {
/* non buffered: fetch next row */
php_sqlite_fetch(res TSRMLS_CC);
}
/* advance the row pointer */
res->curr_row++;
if (decoded == NULL) {
RETURN_NULL();
} else {
RETURN_STRINGL(decoded, decoded_len, 0);
}
}
/* }}} */
/* {{{ proto array sqlite_single_query(resource db, string query [ , bool single_row, bool decode_binary ])
Executes a query against a given database and returns an array */
PHP_FUNCTION(sqlite_single_query)
{
zval *zdb, *ent;
struct php_sqlite_db *db;
struct php_sqlite_result *rres;
char *sql;
long sql_len;
char *errtext = NULL;
zend_bool decode_binary = 1;
zend_bool srow = 1;
if (FAILURE == zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET,
ZEND_NUM_ARGS() TSRMLS_CC, "sr|bb", &sql, &sql_len, &zdb, &srow, &decode_binary) &&
FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|bb", &zdb, &sql, &sql_len, &srow, &decode_binary)) {
return;
}
DB_FROM_ZVAL(db, &zdb);
PHP_SQLITE_EMPTY_QUERY;
/* avoid doing work if we can */
if (!return_value_used) {
db->last_err_code = sqlite_exec(db->db, sql, NULL, NULL, &errtext);
if (db->last_err_code != SQLITE_OK) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", errtext);
sqlite_freemem(errtext);
}
return;
}
rres = (struct php_sqlite_result *)emalloc(sizeof(*rres));
sqlite_query(db, sql, sql_len, PHPSQLITE_NUM, 0, NULL, rres TSRMLS_CC);
if (db->last_err_code != SQLITE_OK) {
/* no need to free rres, as it will be freed by sqlite_query() for us */
RETURN_FALSE;
}
if (!srow) {
array_init(return_value);
}
while (rres->curr_row < rres->nrows) {
MAKE_STD_ZVAL(ent);
php_sqlite_fetch_single(rres, decode_binary, ent TSRMLS_CC);
/* if set and we only have 1 row in the result set, return the result as a string. */
if (srow) {
if (rres->curr_row == 1 && rres->curr_row >= rres->nrows) {
*return_value = *ent;
zval_copy_ctor(return_value);
zval_dtor(ent);
FREE_ZVAL(ent);
break;
} else {
srow = 0;
array_init(return_value);
}
}
add_next_index_zval(return_value, ent);
}
real_result_dtor(rres TSRMLS_CC);
}
/* }}} */
/* {{{ proto string sqlite_fetch_array(resource result [, bool decode_binary])
Fetches first column of a result set as a string */
PHP_FUNCTION(sqlite_fetch_single)
{
zval *zres;
zend_bool decode_binary = 1;
struct php_sqlite_result *res;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|b", &zres, &decode_binary)) {
return;
}
ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);
php_sqlite_fetch_single(res, decode_binary, return_value TSRMLS_CC);
}
/* }}} */
/* {{{ proto array sqlite_fetch_array(resource result [, int result_type, bool decode_binary])
Fetches the current row from a result set as an array */
PHP_FUNCTION(sqlite_current)
{
zval *zres;
int mode = PHPSQLITE_BOTH;
zend_bool decode_binary = 1;
struct php_sqlite_result *res;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|lb", &zres, &mode, &decode_binary)) {
return;
}
ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);
if (ZEND_NUM_ARGS() < 2) {
mode = res->mode;
}
php_sqlite_fetch_array(res, mode, decode_binary, 0, return_value TSRMLS_CC);
}
/* }}} */
/* {{{ proto mixed sqlite_column(resource result, mixed index_or_name [, bool decode_binary])
Fetches a column from the current row of a result set */
PHP_FUNCTION(sqlite_column)
{
zval *zres;
zval *which;
zend_bool decode_binary = 1;
struct php_sqlite_result *res;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz|b", &zres, &which, &decode_binary)) {
return;
}
ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);
php_sqlite_fetch_column(res, which, decode_binary, return_value TSRMLS_CC);
}
/* }}} */
/* {{{ proto string sqlite_libversion()
Returns the version of the linked SQLite library */
PHP_FUNCTION(sqlite_libversion)
{
if (ZEND_NUM_ARGS() != 0) {
WRONG_PARAM_COUNT;
}
RETURN_STRING((char*)sqlite_libversion(), 1);
}
/* }}} */
/* {{{ proto string sqlite_libencoding()
Returns the encoding (iso8859 or UTF-8) of the linked SQLite library */
PHP_FUNCTION(sqlite_libencoding)
{
if (ZEND_NUM_ARGS() != 0) {
WRONG_PARAM_COUNT;
}
RETURN_STRING((char*)sqlite_libencoding(), 1);
}
/* }}} */
/* {{{ proto int sqlite_changes(resource db)
Returns the number of rows that were changed by the most recent SQL statement */
PHP_FUNCTION(sqlite_changes)
{
zval *zdb;
struct php_sqlite_db *db;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zdb)) {
return;
}
DB_FROM_ZVAL(db, &zdb);
RETURN_LONG(sqlite_changes(db->db));
}
/* }}} */
/* {{{ proto int sqlite_last_insert_rowid(resource db)
Returns the rowid of the most recently inserted row */
PHP_FUNCTION(sqlite_last_insert_rowid)
{
zval *zdb;
struct php_sqlite_db *db;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zdb)) {
return;
}
DB_FROM_ZVAL(db, &zdb);
RETURN_LONG(sqlite_last_insert_rowid(db->db));
}
/* }}} */
/* {{{ proto int sqlite_num_rows(resource result)
Returns the number of rows in a result set */
PHP_FUNCTION(sqlite_num_rows)
{
zval *zres;
struct php_sqlite_result *res;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zres)) {
return;
}
ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);
if (res->buffered) {
RETURN_LONG(res->nrows);
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Row count is not available for unbuffered queries");
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto bool sqlite_has_more(resource result)
Returns whether or not more rows are available */
PHP_FUNCTION(sqlite_has_more)
{
zval *zres;
struct php_sqlite_result *res;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zres)) {
return;
}
ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);
RETURN_BOOL(res->nrows && res->curr_row < res->nrows); /* curr_row may be -1 */
}
/* }}} */
/* {{{ proto int sqlite_num_fields(resource result)
Returns the number of fields in a result set */
PHP_FUNCTION(sqlite_num_fields)
{
zval *zres;
struct php_sqlite_result *res;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zres)) {
return;
}
ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);
RETURN_LONG(res->ncolumns);
}
/* }}} */
/* {{{ proto string sqlite_field_name(resource result, int field)
Returns the name of a particular field */
PHP_FUNCTION(sqlite_field_name)
{
zval *zres;
struct php_sqlite_result *res;
int field;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &zres, &field)) {
return;
}
ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);
if (field < 0 || field >= res->ncolumns) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "field %d out of range", field);
RETURN_FALSE;
}
RETURN_STRING(res->col_names[field], 1);
}
/* }}} */
/* {{{ proto bool sqlite_seek(resource result, int row)
Seek to a particular row number */
PHP_FUNCTION(sqlite_seek)
{
zval *zres;
struct php_sqlite_result *res;
int row;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &zres, &row)) {
return;
}
ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);
if (!res->buffered) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot seek an unbuffered result set");
RETURN_FALSE;
}
if (row < 0 || row >= res->nrows) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "row %d out of range", row);
RETURN_FALSE;
}
res->curr_row = row;
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool sqlite_rewind(resource result)
Seek to first row number */
PHP_FUNCTION(sqlite_rewind)
{
zval *zres;
struct php_sqlite_result *res;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zres)) {
return;
}
ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);
if (!res->buffered) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot seek an unbuffered result set");
RETURN_FALSE;
}
if (!res->nrows) {
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "no rows received");
RETURN_FALSE;
}
res->curr_row = 0;
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool sqlite_next(resource result)
Seek to next row number */
PHP_FUNCTION(sqlite_next)
{
zval *zres;
struct php_sqlite_result *res;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zres)) {
return;
}
ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);
if (!res->buffered && res->vm) {
php_sqlite_fetch(res TSRMLS_CC);
}
if (res->curr_row >= res->nrows) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "no more rows available");
RETURN_FALSE;
}
res->curr_row++;
RETURN_TRUE;
}
/* }}} */
/* {{{ proto string sqlite_escape_string(string item)
Escapes a string for use as a query parameter */
PHP_FUNCTION(sqlite_escape_string)
{
char *string = NULL;
long stringlen;
char *ret;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &string, &stringlen)) {
return;
}
if (stringlen && (string[0] == '\x01' || memchr(string, '\0', stringlen) != NULL)) {
/* binary string */
int enclen;
ret = emalloc( 1 + 5 + stringlen * ((float) 256 / (float) 253) );
ret[0] = '\x01';
enclen = sqlite_encode_binary((const unsigned char*)string, stringlen, ret+1);
RETVAL_STRINGL(ret, enclen+1, 0);
} else {
ret = sqlite_mprintf("%q", string);
if (ret) {
RETVAL_STRING(ret, 1);
sqlite_freemem(ret);
}
}
}
/* }}} */
/* {{{ proto int sqlite_last_error(resource db)
Returns the error code of the last error for a database */
PHP_FUNCTION(sqlite_last_error)
{
zval *zdb;
struct php_sqlite_db *db;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zdb)) {
return;
}
DB_FROM_ZVAL(db, &zdb);
RETURN_LONG(db->last_err_code);
}
/* }}} */
/* {{{ proto string sqlite_error_string(int error_code)
Returns the textual description of an error code */
PHP_FUNCTION(sqlite_error_string)
{
long code;
const char *msg;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &code)) {
return;
}
msg = sqlite_error_string(code);
if (msg) {
RETURN_STRING((char*)msg, 1);
} else {
RETURN_NULL();
}
}
/* }}} */
/* manages duplicate registrations of a particular function, and
* also handles the case where the db is using a persistent connection */
enum callback_prep_t { DO_REG, SKIP_REG, ERR };
static enum callback_prep_t prep_callback_struct(struct php_sqlite_db *db, int is_agg,
char *funcname,
zval *step, zval *fini, struct php_sqlite_agg_functions **funcs)
{
struct php_sqlite_agg_functions *alloc_funcs, func_tmp;
char *hashkey;
int hashkeylen;
enum callback_prep_t ret;
hashkeylen = spprintf(&hashkey, 0, "%s-%s", is_agg ? "agg" : "reg", funcname);
/* is it already registered ? */
if (SUCCESS == zend_hash_find(&db->callbacks, hashkey, hashkeylen+1, (void*)&alloc_funcs)) {
/* override the previous definition */
if (alloc_funcs->is_valid) {
/* release these */
if (alloc_funcs->step) {
zval_ptr_dtor(&alloc_funcs->step);
alloc_funcs->step = NULL;
}
if (alloc_funcs->fini) {
zval_ptr_dtor(&alloc_funcs->fini);
alloc_funcs->fini = NULL;
}
}
ret = SKIP_REG;
} else {
/* add a new one */
func_tmp.db = db;
ret = SUCCESS == zend_hash_update(&db->callbacks, hashkey, hashkeylen+1,
(void*)&func_tmp, sizeof(func_tmp), (void**)&alloc_funcs) ? DO_REG : ERR;
}
efree(hashkey);
MAKE_STD_ZVAL(alloc_funcs->step);
*(alloc_funcs->step) = *step;
zval_copy_ctor(alloc_funcs->step);
if (is_agg) {
MAKE_STD_ZVAL(alloc_funcs->fini);
*(alloc_funcs->fini) = *fini;
zval_copy_ctor(alloc_funcs->fini);
} else {
alloc_funcs->fini = NULL;
}
alloc_funcs->is_valid = 1;
*funcs = alloc_funcs;
return ret;
}
/* {{{ proto bool sqlite_create_aggregate(resource db, string funcname, mixed step_func, mixed finalize_func[, long num_args])
Registers an aggregated function for queries*/
PHP_FUNCTION(sqlite_create_aggregate)
{
char *funcname = NULL;
long funcname_len;
zval *zstep, *zfinal, *zdb;
struct php_sqlite_db *db;
struct php_sqlite_agg_functions *funcs;
char *callable = NULL;
long num_args = -1;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rszz|l", &zdb, &funcname, &funcname_len, &zstep, &zfinal, &num_args)) {
return;
}
DB_FROM_ZVAL(db, &zdb);
if (!zend_is_callable(zstep, 0, &callable)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "step function `%s' is not callable", callable);
efree(callable);
return;
}
efree(callable);
if (!zend_is_callable(zfinal, 0, &callable)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "finalize function `%s' is not callable", callable);
efree(callable);
return;
}
efree(callable);
if (prep_callback_struct(db, 1, funcname, zstep, zfinal, &funcs) == DO_REG) {
sqlite_create_aggregate(db->db, funcname, num_args,
php_sqlite_agg_step_function_callback,
php_sqlite_agg_fini_function_callback, funcs);
}
}
/* }}} */
/* {{{ proto bool sqlite_create_function(resource db, string funcname, mixed callback[, long num_args])
Registers a "regular" function for queries */
PHP_FUNCTION(sqlite_create_function)
{
char *funcname = NULL;
long funcname_len;
zval *zcall, *zdb;
struct php_sqlite_db *db;
struct php_sqlite_agg_functions *funcs;
char *callable = NULL;
long num_args = -1;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsz|l", &zdb, &funcname, &funcname_len, &zcall, &num_args)) {
return;
}
DB_FROM_ZVAL(db, &zdb);
if (!zend_is_callable(zcall, 0, &callable)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "function `%s' is not callable", callable);
efree(callable);
return;
}
efree(callable);
if (prep_callback_struct(db, 0, funcname, zcall, NULL, &funcs) == DO_REG) {
sqlite_create_function(db->db, funcname, num_args, php_sqlite_function_callback, funcs);
}
}
/* }}} */
/* {{{ proto string sqlite_udf_encode_binary(string data)
Apply binary encoding (if required) to a string to return from an UDF */
PHP_FUNCTION(sqlite_udf_encode_binary)
{
char *data = NULL;
long datalen;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!", &data, &datalen)) {
return;
}
if (data == NULL) {
RETURN_NULL();
}
if (datalen && (data[0] == '\x01' || memchr(data, '\0', datalen) != NULL)) {
/* binary string */
int enclen;
char *ret;
ret = emalloc( 1 + 5 + datalen * ((float) 256 / (float) 253) );
ret[0] = '\x01';
enclen = sqlite_encode_binary((const unsigned char*)data, datalen, ret+1);
RETVAL_STRINGL(ret, enclen+1, 0);
} else {
RETVAL_STRINGL(data, datalen, 1);
}
}
/* }}} */
/* {{{ proto string sqlite_udf_decode_binary(string data)
Decode binary encoding on a string parameter passed to an UDF */
PHP_FUNCTION(sqlite_udf_decode_binary)
{
char *data = NULL;
long datalen;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!", &data, &datalen)) {
return;
}
if (data == NULL) {
RETURN_NULL();
}
if (datalen && data[0] == '\x01') {
/* encoded string */
int enclen;
char *ret;
ret = emalloc(datalen);
enclen = sqlite_decode_binary((const unsigned char*)data+1, ret);
ret[enclen] = '\0';
RETVAL_STRINGL(ret, enclen, 0);
} else {
RETVAL_STRINGL(data, datalen, 1);
}
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/
|