You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2885 lines
76 KiB

23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
22 years ago
22 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2004 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.0 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_0.txt. |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Wez Furlong <wez@thebrainroom.com> |
  16. | Tal Peer <tal@php.net> |
  17. | Marcus Boerger <helly@php.net> |
  18. +----------------------------------------------------------------------+
  19. $Id$
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include "config.h"
  23. #endif
  24. #define PHP_SQLITE_MODULE_VERSION "1.1-dev"
  25. #include "php.h"
  26. #include "php_ini.h"
  27. #include "ext/standard/info.h"
  28. #include "ext/session/php_session.h"
  29. #include "php_sqlite.h"
  30. #if HAVE_TIME_H
  31. # include <time.h>
  32. #endif
  33. #if HAVE_UNISTD_H
  34. #include <unistd.h>
  35. #endif
  36. #include <sqlite.h>
  37. #include "zend_default_classes.h"
  38. #ifndef safe_emalloc
  39. # define safe_emalloc(a,b,c) emalloc((a)*(b)+(c))
  40. #endif
  41. #ifndef ZEND_ENGINE_2
  42. # define OnUpdateLong OnUpdateInt
  43. #endif
  44. ZEND_DECLARE_MODULE_GLOBALS(sqlite)
  45. #if HAVE_PHP_SESSION
  46. extern ps_module ps_mod_sqlite;
  47. #define ps_sqlite_ptr &ps_mod_sqlite
  48. #endif
  49. extern int sqlite_encode_binary(const unsigned char *in, int n, unsigned char *out);
  50. extern int sqlite_decode_binary(const unsigned char *in, unsigned char *out);
  51. #define php_sqlite_encode_binary(in, n, out) sqlite_encode_binary((const unsigned char *)in, n, (unsigned char *)out)
  52. #define php_sqlite_decode_binary(in, out) sqlite_decode_binary((const unsigned char *)in, (unsigned char *)out)
  53. static int le_sqlite_db, le_sqlite_result, le_sqlite_pdb;
  54. static inline void php_sqlite_strtoupper(char *s)
  55. {
  56. while (*s!='\0') {
  57. *s = toupper(*s);
  58. s++;
  59. }
  60. }
  61. static inline void php_sqlite_strtolower(char *s)
  62. {
  63. while (*s!='\0') {
  64. *s = tolower(*s);
  65. s++;
  66. }
  67. }
  68. /* {{{ PHP_INI
  69. */
  70. PHP_INI_BEGIN()
  71. STD_PHP_INI_ENTRY_EX("sqlite.assoc_case", "0", PHP_INI_ALL, OnUpdateLong, assoc_case, zend_sqlite_globals, sqlite_globals, display_link_numbers)
  72. PHP_INI_END()
  73. /* }}} */
  74. #define DB_FROM_ZVAL(db, zv) ZEND_FETCH_RESOURCE2(db, struct php_sqlite_db *, zv, -1, "sqlite database", le_sqlite_db, le_sqlite_pdb)
  75. #define DB_FROM_OBJECT(db, object) \
  76. { \
  77. sqlite_object *obj = (sqlite_object*) zend_object_store_get_object(object TSRMLS_CC); \
  78. db = obj->u.db; \
  79. if (!db) { \
  80. php_error_docref(NULL TSRMLS_CC, E_WARNING, "The database wasn't opened"); \
  81. RETURN_NULL(); \
  82. } \
  83. }
  84. #define RES_FROM_OBJECT(res, object) \
  85. { \
  86. sqlite_object *obj = (sqlite_object*) zend_object_store_get_object(object TSRMLS_CC); \
  87. res = obj->u.res; \
  88. if (!res) { \
  89. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No result set available"); \
  90. RETURN_NULL(); \
  91. } \
  92. }
  93. #define SQLITE_THROW(message) \
  94. PG(suppress_errors) = 0; \
  95. EG(exception) = zend_throw_exception(sqlite_ce_exception, message, 0 TSRMLS_CC);
  96. struct php_sqlite_result {
  97. struct php_sqlite_db *db;
  98. sqlite_vm *vm;
  99. int buffered;
  100. int ncolumns;
  101. int nrows;
  102. int curr_row;
  103. char **col_names;
  104. int alloc_rows;
  105. char **table;
  106. int mode;
  107. };
  108. struct php_sqlite_db {
  109. sqlite *db;
  110. int last_err_code;
  111. zend_bool is_persistent;
  112. int rsrc_id;
  113. HashTable callbacks;
  114. };
  115. struct php_sqlite_agg_functions {
  116. struct php_sqlite_db *db;
  117. int is_valid;
  118. zval *step;
  119. zval *fini;
  120. };
  121. 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);
  122. static int php_sqlite_fetch(struct php_sqlite_result *rres TSRMLS_DC);
  123. enum { PHPSQLITE_ASSOC = 1, PHPSQLITE_NUM = 2, PHPSQLITE_BOTH = PHPSQLITE_ASSOC|PHPSQLITE_NUM };
  124. function_entry sqlite_functions[] = {
  125. PHP_FE(sqlite_open, third_arg_force_ref)
  126. PHP_FE(sqlite_popen, third_arg_force_ref)
  127. PHP_FE(sqlite_close, NULL)
  128. PHP_FE(sqlite_query, NULL)
  129. PHP_FE(sqlite_array_query, NULL)
  130. PHP_FE(sqlite_single_query, NULL)
  131. PHP_FE(sqlite_fetch_array, NULL)
  132. PHP_FE(sqlite_fetch_object, NULL)
  133. PHP_FE(sqlite_fetch_single, NULL)
  134. PHP_FALIAS(sqlite_fetch_string, sqlite_fetch_single, NULL)
  135. PHP_FE(sqlite_fetch_all, NULL)
  136. PHP_FE(sqlite_current, NULL)
  137. PHP_FE(sqlite_column, NULL)
  138. PHP_FE(sqlite_libversion, NULL)
  139. PHP_FE(sqlite_libencoding, NULL)
  140. PHP_FE(sqlite_changes, NULL)
  141. PHP_FE(sqlite_last_insert_rowid, NULL)
  142. PHP_FE(sqlite_num_rows, NULL)
  143. PHP_FE(sqlite_num_fields, NULL)
  144. PHP_FE(sqlite_field_name, NULL)
  145. PHP_FE(sqlite_seek, NULL)
  146. PHP_FE(sqlite_rewind, NULL)
  147. PHP_FE(sqlite_next, NULL)
  148. PHP_FE(sqlite_prev, NULL)
  149. PHP_FE(sqlite_has_more, NULL)
  150. PHP_FE(sqlite_has_prev, NULL)
  151. PHP_FE(sqlite_escape_string, NULL)
  152. PHP_FE(sqlite_busy_timeout, NULL)
  153. PHP_FE(sqlite_last_error, NULL)
  154. PHP_FE(sqlite_error_string, NULL)
  155. PHP_FE(sqlite_unbuffered_query, NULL)
  156. PHP_FE(sqlite_create_aggregate, NULL)
  157. PHP_FE(sqlite_create_function, NULL)
  158. PHP_FE(sqlite_factory, third_arg_force_ref)
  159. PHP_FE(sqlite_udf_encode_binary, NULL)
  160. PHP_FE(sqlite_udf_decode_binary, NULL)
  161. PHP_FE(sqlite_fetch_column_types, NULL)
  162. {NULL, NULL, NULL}
  163. };
  164. function_entry sqlite_funcs_db[] = {
  165. PHP_ME_MAPPING(sqlite_db, sqlite_open, NULL)
  166. /* PHP_ME_MAPPING(close, sqlite_close, NULL)*/
  167. PHP_ME_MAPPING(query, sqlite_query, NULL)
  168. PHP_ME_MAPPING(array_query, sqlite_array_query, NULL)
  169. PHP_ME_MAPPING(single_query, sqlite_single_query, NULL)
  170. PHP_ME_MAPPING(unbuffered_query, sqlite_unbuffered_query, NULL)
  171. PHP_ME_MAPPING(last_insert_rowid, sqlite_last_insert_rowid, NULL)
  172. PHP_ME_MAPPING(changes, sqlite_changes, NULL)
  173. PHP_ME_MAPPING(create_aggregate, sqlite_create_aggregate, NULL)
  174. PHP_ME_MAPPING(create_function, sqlite_create_function, NULL)
  175. PHP_ME_MAPPING(busy_timeout, sqlite_busy_timeout, NULL)
  176. PHP_ME_MAPPING(last_error, sqlite_last_error, NULL)
  177. PHP_ME_MAPPING(fetch_column_types, sqlite_fetch_column_types, NULL)
  178. /* PHP_ME_MAPPING(error_string, sqlite_error_string, NULL) static */
  179. /* PHP_ME_MAPPING(escape_string, sqlite_escape_string, NULL) static */
  180. {NULL, NULL, NULL}
  181. };
  182. function_entry sqlite_funcs_query[] = {
  183. PHP_ME_MAPPING(fetch_array, sqlite_fetch_array, NULL)
  184. PHP_ME_MAPPING(fetch_object, sqlite_fetch_object, NULL)
  185. PHP_ME_MAPPING(fetch_single, sqlite_fetch_single, NULL)
  186. PHP_ME_MAPPING(fetch_all, sqlite_fetch_all, NULL)
  187. PHP_ME_MAPPING(column, sqlite_column, NULL)
  188. PHP_ME_MAPPING(num_fields, sqlite_num_fields, NULL)
  189. PHP_ME_MAPPING(field_name, sqlite_field_name, NULL)
  190. /* spl_forward */
  191. PHP_ME_MAPPING(current, sqlite_current, NULL)
  192. PHP_ME_MAPPING(next, sqlite_next, NULL)
  193. PHP_ME_MAPPING(hasmore, sqlite_has_more, NULL)
  194. /* spl_sequence */
  195. PHP_ME_MAPPING(rewind, sqlite_rewind, NULL)
  196. /* additional */
  197. PHP_ME_MAPPING(prev, sqlite_prev, NULL)
  198. PHP_ME_MAPPING(hasprev, sqlite_has_prev, NULL)
  199. PHP_ME_MAPPING(num_rows, sqlite_num_rows, NULL)
  200. PHP_ME_MAPPING(seek, sqlite_seek, NULL)
  201. {NULL, NULL, NULL}
  202. };
  203. function_entry sqlite_funcs_ub_query[] = {
  204. PHP_ME_MAPPING(fetch_array, sqlite_fetch_array, NULL)
  205. PHP_ME_MAPPING(fetch_object, sqlite_fetch_object, NULL)
  206. PHP_ME_MAPPING(fetch_single, sqlite_fetch_single, NULL)
  207. PHP_ME_MAPPING(fetch_all, sqlite_fetch_all, NULL)
  208. PHP_ME_MAPPING(column, sqlite_column, NULL)
  209. PHP_ME_MAPPING(num_fields, sqlite_num_fields, NULL)
  210. PHP_ME_MAPPING(field_name, sqlite_field_name, NULL)
  211. /* spl_forward */
  212. PHP_ME_MAPPING(current, sqlite_current, NULL)
  213. PHP_ME_MAPPING(next, sqlite_next, NULL)
  214. PHP_ME_MAPPING(hasmore, sqlite_has_more, NULL)
  215. {NULL, NULL, NULL}
  216. };
  217. function_entry sqlite_funcs_exception[] = {
  218. {NULL, NULL, NULL}
  219. };
  220. zend_module_entry sqlite_module_entry = {
  221. #if ZEND_MODULE_API_NO >= 20010901
  222. STANDARD_MODULE_HEADER,
  223. #endif
  224. "sqlite",
  225. sqlite_functions,
  226. PHP_MINIT(sqlite),
  227. NULL,
  228. PHP_RINIT(sqlite),
  229. PHP_RSHUTDOWN(sqlite),
  230. PHP_MINFO(sqlite),
  231. #if ZEND_MODULE_API_NO >= 20010901
  232. PHP_SQLITE_MODULE_VERSION,
  233. #endif
  234. STANDARD_MODULE_PROPERTIES
  235. };
  236. #ifdef COMPILE_DL_SQLITE
  237. ZEND_GET_MODULE(sqlite)
  238. # ifdef PHP_WIN32
  239. # include "zend_arg_defs.c"
  240. # endif
  241. #endif
  242. static int php_sqlite_callback_invalidator(struct php_sqlite_agg_functions *funcs TSRMLS_DC)
  243. {
  244. if (!funcs->is_valid) {
  245. return 0;
  246. }
  247. if (funcs->step) {
  248. zval_ptr_dtor(&funcs->step);
  249. funcs->step = NULL;
  250. }
  251. if (funcs->fini) {
  252. zval_ptr_dtor(&funcs->fini);
  253. funcs->fini = NULL;
  254. }
  255. funcs->is_valid = 0;
  256. return 0;
  257. }
  258. static void php_sqlite_callback_dtor(void *pDest)
  259. {
  260. struct php_sqlite_agg_functions *funcs = (struct php_sqlite_agg_functions*)pDest;
  261. if (funcs->is_valid) {
  262. TSRMLS_FETCH();
  263. php_sqlite_callback_invalidator(funcs TSRMLS_CC);
  264. }
  265. }
  266. static ZEND_RSRC_DTOR_FUNC(php_sqlite_db_dtor)
  267. {
  268. if (rsrc->ptr) {
  269. struct php_sqlite_db *db = (struct php_sqlite_db*)rsrc->ptr;
  270. sqlite_close(db->db);
  271. zend_hash_destroy(&db->callbacks);
  272. pefree(db, db->is_persistent);
  273. rsrc->ptr = NULL;
  274. }
  275. }
  276. static void real_result_dtor(struct php_sqlite_result *res TSRMLS_DC)
  277. {
  278. int i, j, base;
  279. if (res->vm) {
  280. sqlite_finalize(res->vm, NULL);
  281. }
  282. if (res->table) {
  283. if (!res->buffered && res->nrows) {
  284. res->nrows = 1; /* only one row is stored */
  285. }
  286. for (i = 0; i < res->nrows; i++) {
  287. base = i * res->ncolumns;
  288. for (j = 0; j < res->ncolumns; j++) {
  289. if (res->table[base + j] != NULL) {
  290. efree(res->table[base + j]);
  291. }
  292. }
  293. }
  294. efree(res->table);
  295. }
  296. if (res->col_names) {
  297. for (j = 0; j < res->ncolumns; j++) {
  298. efree(res->col_names[j]);
  299. }
  300. efree(res->col_names);
  301. }
  302. zend_list_delete(res->db->rsrc_id);
  303. efree(res);
  304. }
  305. static ZEND_RSRC_DTOR_FUNC(php_sqlite_result_dtor)
  306. {
  307. struct php_sqlite_result *res = (struct php_sqlite_result *)rsrc->ptr;
  308. real_result_dtor(res TSRMLS_CC);
  309. }
  310. static int php_sqlite_forget_persistent_id_numbers(zend_rsrc_list_entry *rsrc TSRMLS_DC)
  311. {
  312. struct php_sqlite_db *db = (struct php_sqlite_db*)rsrc->ptr;
  313. /* prevent bad mojo if someone tries to use a previously registered function in the next request */
  314. zend_hash_apply(&db->callbacks, (apply_func_t)php_sqlite_callback_invalidator TSRMLS_CC);
  315. if (Z_TYPE_P(rsrc) != le_sqlite_pdb) {
  316. return 0;
  317. }
  318. db->rsrc_id = FAILURE;
  319. /* don't leave pending commits hanging around */
  320. sqlite_exec(db->db, "ROLLBACK", NULL, NULL, NULL);
  321. return 0;
  322. }
  323. PHP_RSHUTDOWN_FUNCTION(sqlite)
  324. {
  325. zend_hash_apply(&EG(persistent_list), (apply_func_t)php_sqlite_forget_persistent_id_numbers TSRMLS_CC);
  326. return SUCCESS;
  327. }
  328. /* {{{ PHP Function interface */
  329. static void php_sqlite_generic_function_callback(sqlite_func *func, int argc, const char **argv)
  330. {
  331. zval *retval = NULL;
  332. zval ***zargs = NULL;
  333. zval funcname;
  334. int i, res;
  335. char *callable = NULL, *errbuf=NULL;
  336. TSRMLS_FETCH();
  337. /* sanity check the args */
  338. if (argc == 0) {
  339. sqlite_set_result_error(func, "not enough parameters", -1);
  340. return;
  341. }
  342. ZVAL_STRING(&funcname, (char*)argv[0], 1);
  343. if (!zend_make_callable(&funcname, &callable TSRMLS_CC)) {
  344. spprintf(&errbuf, 0, "function `%s' is not a function name", callable);
  345. sqlite_set_result_error(func, errbuf, -1);
  346. efree(errbuf);
  347. efree(callable);
  348. zval_dtor(&funcname);
  349. return;
  350. }
  351. if (argc > 1) {
  352. zargs = (zval ***)safe_emalloc((argc - 1), sizeof(zval **), 0);
  353. for (i = 0; i < argc-1; i++) {
  354. zargs[i] = emalloc(sizeof(zval *));
  355. MAKE_STD_ZVAL(*zargs[i]);
  356. ZVAL_STRING(*zargs[i], (char*)argv[i+1], 1);
  357. }
  358. }
  359. res = call_user_function_ex(EG(function_table),
  360. NULL,
  361. &funcname,
  362. &retval,
  363. argc-1,
  364. zargs,
  365. 0, NULL TSRMLS_CC);
  366. zval_dtor(&funcname);
  367. if (res == SUCCESS) {
  368. if (retval == NULL) {
  369. sqlite_set_result_string(func, NULL, 0);
  370. } else {
  371. switch (Z_TYPE_P(retval)) {
  372. case IS_STRING:
  373. sqlite_set_result_string(func, Z_STRVAL_P(retval), Z_STRLEN_P(retval));
  374. break;
  375. case IS_LONG:
  376. case IS_BOOL:
  377. sqlite_set_result_int(func, Z_LVAL_P(retval));
  378. break;
  379. case IS_DOUBLE:
  380. sqlite_set_result_double(func, Z_DVAL_P(retval));
  381. break;
  382. case IS_NULL:
  383. default:
  384. sqlite_set_result_string(func, NULL, 0);
  385. }
  386. }
  387. } else {
  388. char *errbuf;
  389. spprintf(&errbuf, 0, "call_user_function_ex failed for function %s()", callable);
  390. sqlite_set_result_error(func, errbuf, -1);
  391. efree(errbuf);
  392. }
  393. efree(callable);
  394. if (retval) {
  395. zval_ptr_dtor(&retval);
  396. }
  397. if (zargs) {
  398. for (i = 0; i < argc-1; i++) {
  399. zval_ptr_dtor(zargs[i]);
  400. efree(zargs[i]);
  401. }
  402. efree(zargs);
  403. }
  404. }
  405. /* }}} */
  406. /* {{{ callback for sqlite_create_function */
  407. static void php_sqlite_function_callback(sqlite_func *func, int argc, const char **argv)
  408. {
  409. zval *retval = NULL;
  410. zval ***zargs = NULL;
  411. int i, res;
  412. struct php_sqlite_agg_functions *funcs = sqlite_user_data(func);
  413. TSRMLS_FETCH();
  414. if (!funcs->is_valid) {
  415. sqlite_set_result_error(func, "this function has not been correctly defined for this request", -1);
  416. return;
  417. }
  418. if (argc > 0) {
  419. zargs = (zval ***)safe_emalloc(argc, sizeof(zval **), 0);
  420. for (i = 0; i < argc; i++) {
  421. zargs[i] = emalloc(sizeof(zval *));
  422. MAKE_STD_ZVAL(*zargs[i]);
  423. if (argv[i] == NULL) {
  424. ZVAL_NULL(*zargs[i]);
  425. } else {
  426. ZVAL_STRING(*zargs[i], (char*)argv[i], 1);
  427. }
  428. }
  429. }
  430. res = call_user_function_ex(EG(function_table),
  431. NULL,
  432. funcs->step,
  433. &retval,
  434. argc,
  435. zargs,
  436. 0, NULL TSRMLS_CC);
  437. if (res == SUCCESS) {
  438. if (retval == NULL) {
  439. sqlite_set_result_string(func, NULL, 0);
  440. } else {
  441. switch (Z_TYPE_P(retval)) {
  442. case IS_STRING:
  443. /* TODO: for binary results, need to encode the string */
  444. sqlite_set_result_string(func, Z_STRVAL_P(retval), Z_STRLEN_P(retval));
  445. break;
  446. case IS_LONG:
  447. case IS_BOOL:
  448. sqlite_set_result_int(func, Z_LVAL_P(retval));
  449. break;
  450. case IS_DOUBLE:
  451. sqlite_set_result_double(func, Z_DVAL_P(retval));
  452. break;
  453. case IS_NULL:
  454. default:
  455. sqlite_set_result_string(func, NULL, 0);
  456. }
  457. }
  458. } else {
  459. sqlite_set_result_error(func, "call_user_function_ex failed", -1);
  460. }
  461. if (retval) {
  462. zval_ptr_dtor(&retval);
  463. }
  464. if (zargs) {
  465. for (i = 0; i < argc; i++) {
  466. zval_ptr_dtor(zargs[i]);
  467. efree(zargs[i]);
  468. }
  469. efree(zargs);
  470. }
  471. }
  472. /* }}} */
  473. /* {{{ callback for sqlite_create_aggregate: step function */
  474. static void php_sqlite_agg_step_function_callback(sqlite_func *func, int argc, const char **argv)
  475. {
  476. zval *retval = NULL;
  477. zval ***zargs;
  478. zval **context_p;
  479. int i, res, zargc;
  480. struct php_sqlite_agg_functions *funcs = sqlite_user_data(func);
  481. TSRMLS_FETCH();
  482. if (!funcs->is_valid) {
  483. sqlite_set_result_error(func, "this function has not been correctly defined for this request", -1);
  484. return;
  485. }
  486. /* sanity check the args */
  487. if (argc < 1) {
  488. return;
  489. }
  490. zargc = argc + 1;
  491. zargs = (zval ***)safe_emalloc(zargc, sizeof(zval **), 0);
  492. /* first arg is always the context zval */
  493. context_p = (zval **)sqlite_aggregate_context(func, sizeof(*context_p));
  494. if (*context_p == NULL) {
  495. MAKE_STD_ZVAL(*context_p);
  496. (*context_p)->is_ref = 1;
  497. Z_TYPE_PP(context_p) = IS_NULL;
  498. }
  499. zargs[0] = context_p;
  500. /* copy the other args */
  501. for (i = 0; i < argc; i++) {
  502. zargs[i+1] = emalloc(sizeof(zval *));
  503. MAKE_STD_ZVAL(*zargs[i+1]);
  504. if (argv[i] == NULL) {
  505. ZVAL_NULL(*zargs[i+1]);
  506. } else {
  507. ZVAL_STRING(*zargs[i+1], (char*)argv[i], 1);
  508. }
  509. }
  510. res = call_user_function_ex(EG(function_table),
  511. NULL,
  512. funcs->step,
  513. &retval,
  514. zargc,
  515. zargs,
  516. 0, NULL TSRMLS_CC);
  517. if (res != SUCCESS) {
  518. php_error_docref(NULL TSRMLS_CC, E_WARNING, "call_user_function_ex failed");
  519. }
  520. if (retval) {
  521. zval_ptr_dtor(&retval);
  522. }
  523. if (zargs) {
  524. for (i = 1; i < zargc; i++) {
  525. zval_ptr_dtor(zargs[i]);
  526. efree(zargs[i]);
  527. }
  528. efree(zargs);
  529. }
  530. }
  531. /* }}} */
  532. /* {{{ callback for sqlite_create_aggregate: finalize function */
  533. static void php_sqlite_agg_fini_function_callback(sqlite_func *func)
  534. {
  535. zval *retval = NULL;
  536. int res;
  537. struct php_sqlite_agg_functions *funcs = sqlite_user_data(func);
  538. zval **context_p;
  539. TSRMLS_FETCH();
  540. if (!funcs->is_valid) {
  541. sqlite_set_result_error(func, "this function has not been correctly defined for this request", -1);
  542. return;
  543. }
  544. context_p = (zval **)sqlite_aggregate_context(func, sizeof(*context_p));
  545. res = call_user_function_ex(EG(function_table),
  546. NULL,
  547. funcs->fini,
  548. &retval,
  549. 1,
  550. &context_p,
  551. 0, NULL TSRMLS_CC);
  552. if (res == SUCCESS) {
  553. if (retval == NULL) {
  554. sqlite_set_result_string(func, NULL, 0);
  555. } else {
  556. switch (Z_TYPE_P(retval)) {
  557. case IS_STRING:
  558. /* TODO: for binary results, need to encode the string */
  559. sqlite_set_result_string(func, Z_STRVAL_P(retval), Z_STRLEN_P(retval));
  560. break;
  561. case IS_LONG:
  562. case IS_BOOL:
  563. sqlite_set_result_int(func, Z_LVAL_P(retval));
  564. break;
  565. case IS_DOUBLE:
  566. sqlite_set_result_double(func, Z_DVAL_P(retval));
  567. break;
  568. case IS_NULL:
  569. default:
  570. sqlite_set_result_string(func, NULL, 0);
  571. }
  572. }
  573. } else {
  574. sqlite_set_result_error(func, "call_user_function_ex failed", -1);
  575. }
  576. if (retval) {
  577. zval_ptr_dtor(&retval);
  578. }
  579. zval_ptr_dtor(context_p);
  580. }
  581. /* }}} */
  582. /* {{{ Authorization Callback */
  583. static int php_sqlite_authorizer(void *autharg, int access_type, const char *arg3, const char *arg4,
  584. const char *arg5, const char *arg6)
  585. {
  586. switch (access_type) {
  587. case SQLITE_COPY:
  588. {
  589. TSRMLS_FETCH();
  590. if (PG(safe_mode) && (!php_checkuid(arg4, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
  591. return SQLITE_DENY;
  592. }
  593. if (php_check_open_basedir(arg4 TSRMLS_CC)) {
  594. return SQLITE_DENY;
  595. }
  596. }
  597. return SQLITE_OK;
  598. #ifdef SQLITE_ATTACH
  599. case SQLITE_ATTACH:
  600. {
  601. TSRMLS_FETCH();
  602. if (PG(safe_mode) && (!php_checkuid(arg3, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
  603. return SQLITE_DENY;
  604. }
  605. if (php_check_open_basedir(arg3 TSRMLS_CC)) {
  606. return SQLITE_DENY;
  607. }
  608. }
  609. return SQLITE_OK;
  610. #endif
  611. default:
  612. /* access allowed */
  613. return SQLITE_OK;
  614. }
  615. }
  616. /* }}} */
  617. /* {{{ OO init/structure stuff */
  618. #define REGISTER_SQLITE_CLASS(name, parent) \
  619. { \
  620. zend_class_entry ce; \
  621. INIT_CLASS_ENTRY(ce, "sqlite_" # name, sqlite_funcs_ ## name); \
  622. ce.create_object = sqlite_object_new_ ## name; \
  623. sqlite_ce_ ## name = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \
  624. memcpy(&sqlite_object_handlers_ ## name, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \
  625. sqlite_object_handlers_ ## name.clone_obj = NULL; \
  626. sqlite_ce_ ## name->ce_flags |= ZEND_ACC_FINAL_CLASS; \
  627. }
  628. zend_class_entry *sqlite_ce_db, *sqlite_ce_exception;
  629. zend_class_entry *sqlite_ce_query, *sqlite_ce_ub_query;
  630. static zend_object_handlers sqlite_object_handlers_db;
  631. static zend_object_handlers sqlite_object_handlers_query;
  632. static zend_object_handlers sqlite_object_handlers_ub_query;
  633. static zend_object_handlers sqlite_object_handlers_exception;
  634. typedef enum {
  635. is_db,
  636. is_result
  637. } sqlite_obj_type;
  638. typedef struct _sqlite_object {
  639. zend_object std;
  640. sqlite_obj_type type;
  641. union {
  642. struct php_sqlite_db *db;
  643. struct php_sqlite_result *res;
  644. void *ptr;
  645. } u;
  646. } sqlite_object;
  647. static int sqlite_free_persistent(list_entry *le, void *ptr TSRMLS_DC)
  648. {
  649. return le->ptr == ptr ? ZEND_HASH_APPLY_REMOVE : ZEND_HASH_APPLY_KEEP;
  650. }
  651. static void sqlite_object_dtor(void *object, zend_object_handle handle TSRMLS_DC)
  652. {
  653. sqlite_object *intern = (sqlite_object *)object;
  654. zend_hash_destroy(intern->std.properties);
  655. FREE_HASHTABLE(intern->std.properties);
  656. if (intern->u.ptr) {
  657. if (intern->type == is_db) {
  658. if (intern->u.db->rsrc_id) {
  659. zend_list_delete(intern->u.db->rsrc_id);
  660. zend_hash_apply_with_argument(&EG(persistent_list), (apply_func_arg_t) sqlite_free_persistent, &intern->u.ptr TSRMLS_CC);
  661. }
  662. } else {
  663. real_result_dtor(intern->u.res TSRMLS_CC);
  664. }
  665. }
  666. efree(object);
  667. }
  668. static void sqlite_object_new(zend_class_entry *class_type, zend_object_handlers *handlers, zend_object_value *retval TSRMLS_DC)
  669. {
  670. sqlite_object *intern;
  671. zval *tmp;
  672. intern = emalloc(sizeof(sqlite_object));
  673. memset(intern, 0, sizeof(sqlite_object));
  674. intern->std.ce = class_type;
  675. ALLOC_HASHTABLE(intern->std.properties);
  676. zend_hash_init(intern->std.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
  677. zend_hash_copy(intern->std.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
  678. retval->handle = zend_objects_store_put(intern, sqlite_object_dtor, NULL TSRMLS_CC);
  679. retval->handlers = handlers;
  680. }
  681. static zend_object_value sqlite_object_new_db(zend_class_entry *class_type TSRMLS_DC)
  682. {
  683. zend_object_value retval;
  684. sqlite_object_new(class_type, &sqlite_object_handlers_db, &retval TSRMLS_CC);
  685. return retval;
  686. }
  687. static zend_object_value sqlite_object_new_query(zend_class_entry *class_type TSRMLS_DC)
  688. {
  689. zend_object_value retval;
  690. sqlite_object_new(class_type, &sqlite_object_handlers_query, &retval TSRMLS_CC);
  691. return retval;
  692. }
  693. static zend_object_value sqlite_object_new_ub_query(zend_class_entry *class_type TSRMLS_DC)
  694. {
  695. zend_object_value retval;
  696. sqlite_object_new(class_type, &sqlite_object_handlers_ub_query, &retval TSRMLS_CC);
  697. return retval;
  698. }
  699. static zend_object_value sqlite_object_new_exception(zend_class_entry *class_type TSRMLS_DC)
  700. {
  701. zend_object_value retval;
  702. sqlite_object_new(class_type, &sqlite_object_handlers_exception, &retval TSRMLS_CC);
  703. return retval;
  704. }
  705. #define SQLITE_REGISTER_OBJECT(_type, _object, _ptr) \
  706. { \
  707. sqlite_object *obj; \
  708. obj = (sqlite_object*)zend_object_store_get_object(_object TSRMLS_CC); \
  709. obj->type = is_ ## _type; \
  710. obj->u._type = _ptr; \
  711. }
  712. static zend_class_entry *sqlite_get_ce_query(zval *object TSRMLS_DC)
  713. {
  714. return sqlite_ce_query;
  715. }
  716. static zend_class_entry *sqlite_get_ce_ub_query(zval *object TSRMLS_DC)
  717. {
  718. return sqlite_ce_ub_query;
  719. }
  720. static zval * sqlite_instanciate(zend_class_entry *pce, zval *object TSRMLS_DC)
  721. {
  722. if (!object) {
  723. ALLOC_ZVAL(object);
  724. }
  725. Z_TYPE_P(object) = IS_OBJECT;
  726. object_init_ex(object, pce);
  727. object->refcount = 1;
  728. object->is_ref = 1;
  729. return object;
  730. }
  731. typedef struct _sqlite_object_iterator {
  732. zend_object_iterator it;
  733. struct php_sqlite_result *res;
  734. zval *value;
  735. } sqlite_object_iterator;
  736. void sqlite_iterator_dtor(zend_object_iterator *iter TSRMLS_DC)
  737. {
  738. zval *object = (zval*)((sqlite_object_iterator*)iter)->it.data;
  739. if (((sqlite_object_iterator*)iter)->value) {
  740. zval_ptr_dtor(&((sqlite_object_iterator*)iter)->value);
  741. ((sqlite_object_iterator*)iter)->value = NULL;
  742. }
  743. zval_ptr_dtor(&object);
  744. efree(iter);
  745. }
  746. void sqlite_iterator_rewind(zend_object_iterator *iter TSRMLS_DC)
  747. {
  748. struct php_sqlite_result *res = ((sqlite_object_iterator*)iter)->res;
  749. if (((sqlite_object_iterator*)iter)->value) {
  750. zval_ptr_dtor(&((sqlite_object_iterator*)iter)->value);
  751. ((sqlite_object_iterator*)iter)->value = NULL;
  752. }
  753. if (res) {
  754. res->curr_row = 0;
  755. }
  756. }
  757. int sqlite_iterator_has_more(zend_object_iterator *iter TSRMLS_DC)
  758. {
  759. struct php_sqlite_result *res = ((sqlite_object_iterator*)iter)->res;
  760. if (res && res->curr_row < res->nrows && res->nrows) { /* curr_row may be -1 */
  761. return SUCCESS;
  762. } else {
  763. return FAILURE;
  764. }
  765. }
  766. void sqlite_iterator_get_current_data(zend_object_iterator *iter, zval ***data TSRMLS_DC)
  767. {
  768. struct php_sqlite_result *res = ((sqlite_object_iterator*)iter)->res;
  769. *data = &((sqlite_object_iterator*)iter)->value;
  770. if (res && !**data) {
  771. MAKE_STD_ZVAL(**data);
  772. php_sqlite_fetch_array(res, PHPSQLITE_NUM, 1, 0, **data TSRMLS_CC);
  773. }
  774. }
  775. int sqlite_iterator_get_current_key(zend_object_iterator *iter, char **str_key, uint *str_key_len, ulong *int_key TSRMLS_DC)
  776. {
  777. struct php_sqlite_result *res = ((sqlite_object_iterator*)iter)->res;
  778. *str_key = NULL;
  779. *str_key_len = 0;
  780. *int_key = res ? res->curr_row : 0;
  781. return HASH_KEY_IS_LONG;
  782. }
  783. void sqlite_iterator_move_forward(zend_object_iterator *iter TSRMLS_DC)
  784. {
  785. struct php_sqlite_result *res = ((sqlite_object_iterator*)iter)->res;
  786. if (((sqlite_object_iterator*)iter)->value) {
  787. zval_ptr_dtor(&((sqlite_object_iterator*)iter)->value);
  788. ((sqlite_object_iterator*)iter)->value = NULL;
  789. }
  790. if (res) {
  791. if (!res->buffered && res->vm) {
  792. php_sqlite_fetch(res TSRMLS_CC);
  793. }
  794. if (res->curr_row >= res->nrows) {
  795. /* php_error_docref(NULL TSRMLS_CC, E_WARNING, "no more rows available"); */
  796. return;
  797. }
  798. res->curr_row++;
  799. }
  800. }
  801. zend_object_iterator_funcs sqlite_ub_query_iterator_funcs = {
  802. sqlite_iterator_dtor,
  803. sqlite_iterator_has_more,
  804. sqlite_iterator_get_current_data,
  805. sqlite_iterator_get_current_key,
  806. sqlite_iterator_move_forward,
  807. NULL
  808. };
  809. zend_object_iterator_funcs sqlite_query_iterator_funcs = {
  810. sqlite_iterator_dtor,
  811. sqlite_iterator_has_more,
  812. sqlite_iterator_get_current_data,
  813. sqlite_iterator_get_current_key,
  814. sqlite_iterator_move_forward,
  815. sqlite_iterator_rewind
  816. };
  817. zend_object_iterator *sqlite_get_iterator(zend_class_entry *ce, zval *object TSRMLS_DC)
  818. {
  819. sqlite_object_iterator *iterator = emalloc(sizeof(sqlite_object_iterator));
  820. sqlite_object *obj = (sqlite_object*) zend_object_store_get_object(object TSRMLS_CC);
  821. object->refcount++;
  822. iterator->it.data = (void*)object;
  823. iterator->it.funcs = ce->iterator_funcs.funcs;
  824. iterator->res = obj->u.res;
  825. iterator->value = NULL;
  826. return (zend_object_iterator*)iterator;
  827. }
  828. /* }}} */
  829. static int init_sqlite_globals(zend_sqlite_globals *g)
  830. {
  831. g->assoc_case = 0;
  832. return SUCCESS;
  833. }
  834. PHP_MINIT_FUNCTION(sqlite)
  835. {
  836. REGISTER_SQLITE_CLASS(db, NULL);
  837. REGISTER_SQLITE_CLASS(query, NULL);
  838. REGISTER_SQLITE_CLASS(ub_query, NULL);
  839. REGISTER_SQLITE_CLASS(exception, zend_exception_get_default());
  840. sqlite_object_handlers_query.get_class_entry = sqlite_get_ce_query;
  841. sqlite_object_handlers_ub_query.get_class_entry = sqlite_get_ce_ub_query;
  842. sqlite_ce_ub_query->get_iterator = sqlite_get_iterator;
  843. sqlite_ce_ub_query->iterator_funcs.funcs = &sqlite_ub_query_iterator_funcs;
  844. sqlite_ce_query->get_iterator = sqlite_get_iterator;
  845. sqlite_ce_query->iterator_funcs.funcs = &sqlite_query_iterator_funcs;
  846. ZEND_INIT_MODULE_GLOBALS(sqlite, init_sqlite_globals, NULL);
  847. REGISTER_INI_ENTRIES();
  848. #if HAVE_PHP_SESSION
  849. php_session_register_module(ps_sqlite_ptr);
  850. #endif
  851. le_sqlite_db = zend_register_list_destructors_ex(php_sqlite_db_dtor, NULL, "sqlite database", module_number);
  852. le_sqlite_pdb = zend_register_list_destructors_ex(NULL, php_sqlite_db_dtor, "sqlite database (persistent)", module_number);
  853. le_sqlite_result = zend_register_list_destructors_ex(php_sqlite_result_dtor, NULL, "sqlite result", module_number);
  854. REGISTER_LONG_CONSTANT("SQLITE_BOTH", PHPSQLITE_BOTH, CONST_CS|CONST_PERSISTENT);
  855. REGISTER_LONG_CONSTANT("SQLITE_NUM", PHPSQLITE_NUM, CONST_CS|CONST_PERSISTENT);
  856. REGISTER_LONG_CONSTANT("SQLITE_ASSOC", PHPSQLITE_ASSOC, CONST_CS|CONST_PERSISTENT);
  857. REGISTER_LONG_CONSTANT("SQLITE_OK", SQLITE_OK, CONST_CS|CONST_PERSISTENT);
  858. REGISTER_LONG_CONSTANT("SQLITE_ERROR", SQLITE_ERROR, CONST_CS|CONST_PERSISTENT);
  859. REGISTER_LONG_CONSTANT("SQLITE_INTERNAL", SQLITE_INTERNAL, CONST_CS|CONST_PERSISTENT);
  860. REGISTER_LONG_CONSTANT("SQLITE_PERM", SQLITE_PERM, CONST_CS|CONST_PERSISTENT);
  861. REGISTER_LONG_CONSTANT("SQLITE_ABORT", SQLITE_ABORT, CONST_CS|CONST_PERSISTENT);
  862. REGISTER_LONG_CONSTANT("SQLITE_BUSY", SQLITE_BUSY, CONST_CS|CONST_PERSISTENT);
  863. REGISTER_LONG_CONSTANT("SQLITE_LOCKED", SQLITE_LOCKED, CONST_CS|CONST_PERSISTENT);
  864. REGISTER_LONG_CONSTANT("SQLITE_NOMEM", SQLITE_NOMEM, CONST_CS|CONST_PERSISTENT);
  865. REGISTER_LONG_CONSTANT("SQLITE_READONLY", SQLITE_READONLY, CONST_CS|CONST_PERSISTENT);
  866. REGISTER_LONG_CONSTANT("SQLITE_INTERRUPT", SQLITE_INTERRUPT, CONST_CS|CONST_PERSISTENT);
  867. REGISTER_LONG_CONSTANT("SQLITE_IOERR", SQLITE_IOERR, CONST_CS|CONST_PERSISTENT);
  868. REGISTER_LONG_CONSTANT("SQLITE_CORRUPT", SQLITE_CORRUPT, CONST_CS|CONST_PERSISTENT);
  869. REGISTER_LONG_CONSTANT("SQLITE_NOTFOUND", SQLITE_NOTFOUND, CONST_CS|CONST_PERSISTENT);
  870. REGISTER_LONG_CONSTANT("SQLITE_FULL", SQLITE_FULL, CONST_CS|CONST_PERSISTENT);
  871. REGISTER_LONG_CONSTANT("SQLITE_CANTOPEN", SQLITE_CANTOPEN, CONST_CS|CONST_PERSISTENT);
  872. REGISTER_LONG_CONSTANT("SQLITE_PROTOCOL", SQLITE_PROTOCOL, CONST_CS|CONST_PERSISTENT);
  873. REGISTER_LONG_CONSTANT("SQLITE_EMPTY", SQLITE_EMPTY, CONST_CS|CONST_PERSISTENT);
  874. REGISTER_LONG_CONSTANT("SQLITE_SCHEMA", SQLITE_SCHEMA, CONST_CS|CONST_PERSISTENT);
  875. REGISTER_LONG_CONSTANT("SQLITE_TOOBIG", SQLITE_TOOBIG, CONST_CS|CONST_PERSISTENT);
  876. REGISTER_LONG_CONSTANT("SQLITE_CONSTRAINT", SQLITE_CONSTRAINT, CONST_CS|CONST_PERSISTENT);
  877. REGISTER_LONG_CONSTANT("SQLITE_MISMATCH", SQLITE_MISMATCH, CONST_CS|CONST_PERSISTENT);
  878. REGISTER_LONG_CONSTANT("SQLITE_MISUSE", SQLITE_MISUSE, CONST_CS|CONST_PERSISTENT);
  879. REGISTER_LONG_CONSTANT("SQLITE_NOLFS", SQLITE_NOLFS, CONST_CS|CONST_PERSISTENT);
  880. REGISTER_LONG_CONSTANT("SQLITE_AUTH", SQLITE_AUTH, CONST_CS|CONST_PERSISTENT);
  881. #ifdef SQLITE_FORMAT
  882. REGISTER_LONG_CONSTANT("SQLITE_FORMAT", SQLITE_FORMAT, CONST_CS|CONST_PERSISTENT);
  883. #endif
  884. REGISTER_LONG_CONSTANT("SQLITE_ROW", SQLITE_ROW, CONST_CS|CONST_PERSISTENT);
  885. REGISTER_LONG_CONSTANT("SQLITE_DONE", SQLITE_DONE, CONST_CS|CONST_PERSISTENT);
  886. return SUCCESS;
  887. }
  888. PHP_RINIT_FUNCTION(sqlite)
  889. {
  890. return SUCCESS;
  891. }
  892. PHP_MINFO_FUNCTION(sqlite)
  893. {
  894. php_info_print_table_start();
  895. php_info_print_table_header(2, "SQLite support", "enabled");
  896. php_info_print_table_row(2, "PECL Module version", PHP_SQLITE_MODULE_VERSION " $Id$");
  897. php_info_print_table_row(2, "SQLite Library", sqlite_libversion());
  898. php_info_print_table_row(2, "SQLite Encoding", sqlite_libencoding());
  899. php_info_print_table_end();
  900. DISPLAY_INI_ENTRIES();
  901. }
  902. static struct php_sqlite_db *php_sqlite_open(char *filename, int mode, char *persistent_id, zval *return_value, zval *errmsg, zval *object TSRMLS_DC)
  903. {
  904. char *errtext = NULL;
  905. sqlite *sdb = NULL;
  906. struct php_sqlite_db *db = NULL;
  907. sdb = sqlite_open(filename, mode, &errtext);
  908. if (sdb == NULL) {
  909. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", errtext);
  910. if (errmsg) {
  911. ZVAL_STRING(errmsg, errtext, 1);
  912. }
  913. sqlite_freemem(errtext);
  914. /* if object is not an object then we're called from the factory() function */
  915. if (object && Z_TYPE_P(object) != IS_OBJECT) {
  916. RETVAL_NULL();
  917. } else {
  918. RETVAL_FALSE;
  919. }
  920. return NULL;
  921. }
  922. db = (struct php_sqlite_db *)pemalloc(sizeof(struct php_sqlite_db), persistent_id ? 1 : 0);
  923. db->is_persistent = persistent_id ? 1 : 0;
  924. db->last_err_code = SQLITE_OK;
  925. db->db = sdb;
  926. zend_hash_init(&db->callbacks, 0, NULL, php_sqlite_callback_dtor, db->is_persistent);
  927. /* register the PHP functions */
  928. sqlite_create_function(sdb, "php", -1, php_sqlite_generic_function_callback, 0);
  929. /* set default busy handler; keep retrying up until 1 minute has passed,
  930. * then fail with a busy status code */
  931. sqlite_busy_timeout(sdb, 60000);
  932. /* authorizer hook so we can enforce safe mode
  933. * Note: the declaration of php_sqlite_authorizer is correct for 2.8.2 of libsqlite,
  934. * and IS backwards binary compatible with earlier versions */
  935. sqlite_set_authorizer(sdb, php_sqlite_authorizer, NULL);
  936. db->rsrc_id = ZEND_REGISTER_RESOURCE(object ? NULL : return_value, db, persistent_id ? le_sqlite_pdb : le_sqlite_db);
  937. if (object) {
  938. /* if object is not an object then we're called from the factory() function */
  939. if (Z_TYPE_P(object) != IS_OBJECT) {
  940. sqlite_instanciate(sqlite_ce_db, object TSRMLS_CC);
  941. }
  942. /* and now register the object */
  943. SQLITE_REGISTER_OBJECT(db, object, db)
  944. }
  945. if (persistent_id) {
  946. list_entry le;
  947. Z_TYPE(le) = le_sqlite_pdb;
  948. le.ptr = db;
  949. if (FAILURE == zend_hash_update(&EG(persistent_list), persistent_id,
  950. strlen(persistent_id)+1,
  951. (void *)&le, sizeof(le), NULL)) {
  952. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to register persistent resource");
  953. }
  954. }
  955. return db;
  956. }
  957. /* {{{ proto resource sqlite_popen(string filename [, int mode [, string &error_message]])
  958. Opens a persistent handle to a SQLite database. Will create the database if it does not exist. */
  959. PHP_FUNCTION(sqlite_popen)
  960. {
  961. int mode = 0666;
  962. char *filename, *fullpath, *hashkey;
  963. long filename_len, hashkeylen;
  964. zval *errmsg = NULL;
  965. struct php_sqlite_db *db = NULL;
  966. list_entry *le;
  967. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lz/",
  968. &filename, &filename_len, &mode, &errmsg)) {
  969. return;
  970. }
  971. if (errmsg) {
  972. zval_dtor(errmsg);
  973. }
  974. if (strncmp(filename, ":memory:", sizeof(":memory:") - 1)) {
  975. /* resolve the fully-qualified path name to use as the hash key */
  976. fullpath = expand_filepath(filename, NULL TSRMLS_CC);
  977. if (PG(safe_mode) && (!php_checkuid(fullpath, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
  978. RETURN_FALSE;
  979. }
  980. if (php_check_open_basedir(fullpath TSRMLS_CC)) {
  981. RETURN_FALSE;
  982. }
  983. } else {
  984. fullpath = estrndup(filename, filename_len);
  985. }
  986. hashkeylen = spprintf(&hashkey, 0, "sqlite_pdb_%s:%d", fullpath, mode);
  987. /* do we have an existing persistent connection ? */
  988. if (SUCCESS == zend_hash_find(&EG(persistent_list), hashkey, hashkeylen+1, (void*)&le)) {
  989. if (Z_TYPE_P(le) == le_sqlite_pdb) {
  990. db = (struct php_sqlite_db*)le->ptr;
  991. if (db->rsrc_id == FAILURE) {
  992. /* give it a valid resource id for this request */
  993. db->rsrc_id = ZEND_REGISTER_RESOURCE(return_value, db, le_sqlite_pdb);
  994. } else {
  995. int type;
  996. /* sanity check to ensure that the resource is still a valid regular resource
  997. * number */
  998. if (zend_list_find(db->rsrc_id, &type) == db) {
  999. /* already accessed this request; map it */
  1000. zend_list_addref(db->rsrc_id);
  1001. ZVAL_RESOURCE(return_value, db->rsrc_id);
  1002. } else {
  1003. db->rsrc_id = ZEND_REGISTER_RESOURCE(return_value, db, le_sqlite_pdb);
  1004. }
  1005. }
  1006. /* all set */
  1007. efree(fullpath);
  1008. efree(hashkey);
  1009. return;
  1010. }
  1011. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Some other type of persistent resource is using this hash key!?");
  1012. RETURN_FALSE;
  1013. }
  1014. /* now we need to open the database */
  1015. php_sqlite_open(fullpath, mode, hashkey, return_value, errmsg, NULL TSRMLS_CC);
  1016. efree(fullpath);
  1017. efree(hashkey);
  1018. }
  1019. /* }}} */
  1020. /* {{{ proto resource sqlite_open(string filename [, int mode [, string &error_message]])
  1021. Opens a SQLite database. Will create the database if it does not exist. */
  1022. PHP_FUNCTION(sqlite_open)
  1023. {
  1024. int mode = 0666;
  1025. char *filename, *fullpath = NULL;
  1026. long filename_len;
  1027. zval *errmsg = NULL;
  1028. zval *object = getThis();
  1029. php_set_error_handling(object ? EH_THROW : EH_NORMAL, sqlite_ce_exception TSRMLS_CC);
  1030. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lz/",
  1031. &filename, &filename_len, &mode, &errmsg)) {
  1032. php_std_error_handling();
  1033. return;
  1034. }
  1035. if (errmsg) {
  1036. zval_dtor(errmsg);
  1037. }
  1038. if (strncmp(filename, ":memory:", sizeof(":memory:") - 1)) {
  1039. /* resolve the fully-qualified path name to use as the hash key */
  1040. fullpath = expand_filepath(filename, NULL TSRMLS_CC);
  1041. if (PG(safe_mode) && (!php_checkuid(fullpath, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
  1042. php_std_error_handling();
  1043. efree(fullpath);
  1044. if (object) {
  1045. RETURN_NULL();
  1046. } else {
  1047. RETURN_FALSE;
  1048. }
  1049. }
  1050. if (php_check_open_basedir(fullpath TSRMLS_CC)) {
  1051. php_std_error_handling();
  1052. efree(fullpath);
  1053. if (object) {
  1054. RETURN_NULL();
  1055. } else {
  1056. RETURN_FALSE;
  1057. }
  1058. }
  1059. }
  1060. php_sqlite_open(fullpath ? fullpath : filename, mode, NULL, return_value, errmsg, object TSRMLS_CC);
  1061. if (fullpath) {
  1062. efree(fullpath);
  1063. }
  1064. php_std_error_handling();
  1065. }
  1066. /* }}} */
  1067. /* {{{ proto object sqlite_factory(string filename [, int mode [, string &error_message]])
  1068. Opens a SQLite database and creates an object for it. Will create the database if it does not exist. */
  1069. PHP_FUNCTION(sqlite_factory)
  1070. {
  1071. int mode = 0666;
  1072. char *filename;
  1073. long filename_len;
  1074. zval *errmsg = NULL;
  1075. php_set_error_handling(EH_THROW, sqlite_ce_exception TSRMLS_CC);
  1076. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lz/",
  1077. &filename, &filename_len, &mode, &errmsg)) {
  1078. php_std_error_handling();
  1079. RETURN_NULL();
  1080. }
  1081. if (errmsg) {
  1082. zval_dtor(errmsg);
  1083. }
  1084. if (strncmp(filename, ":memory:", sizeof(":memory:") - 1)) {
  1085. if (PG(safe_mode) && (!php_checkuid(filename, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
  1086. php_std_error_handling();
  1087. RETURN_NULL();
  1088. }
  1089. if (php_check_open_basedir(filename TSRMLS_CC)) {
  1090. php_std_error_handling();
  1091. RETURN_NULL();
  1092. }
  1093. }
  1094. php_sqlite_open(filename, mode, NULL, return_value, errmsg, return_value TSRMLS_CC);
  1095. php_std_error_handling();
  1096. }
  1097. /* }}} */
  1098. /* {{{ proto void sqlite_busy_timeout(resource db, int ms)
  1099. Set busy timeout duration. If ms <= 0, all busy handlers are disabled. */
  1100. PHP_FUNCTION(sqlite_busy_timeout)
  1101. {
  1102. zval *zdb;
  1103. struct php_sqlite_db *db;
  1104. long ms;
  1105. zval *object = getThis();
  1106. if (object) {
  1107. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &ms)) {
  1108. return;
  1109. }
  1110. DB_FROM_OBJECT(db, object);
  1111. } else {
  1112. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &zdb, &ms)) {
  1113. return;
  1114. }
  1115. DB_FROM_ZVAL(db, &zdb);
  1116. }
  1117. sqlite_busy_timeout(db->db, ms);
  1118. }
  1119. /* }}} */
  1120. /* {{{ proto void sqlite_close(resource db)
  1121. Closes an open sqlite database. */
  1122. PHP_FUNCTION(sqlite_close)
  1123. {
  1124. zval *zdb;
  1125. struct php_sqlite_db *db;
  1126. zval *object = getThis();
  1127. if (object) {
  1128. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Ignored, you must destruct the object instead");
  1129. } else {
  1130. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zdb)) {
  1131. return;
  1132. }
  1133. DB_FROM_ZVAL(db, &zdb);
  1134. }
  1135. zend_list_delete(Z_RESVAL_P(zdb));
  1136. }
  1137. /* }}} */
  1138. /* {{{ php_sqlite_fetch */
  1139. static int php_sqlite_fetch(struct php_sqlite_result *rres TSRMLS_DC)
  1140. {
  1141. const char **rowdata, **colnames;
  1142. int ret, i, base;
  1143. char *errtext = NULL, *colname;
  1144. next_row:
  1145. ret = sqlite_step(rres->vm, &rres->ncolumns, &rowdata, &colnames);
  1146. if (!rres->nrows) {
  1147. /* first row - lets copy the column names */
  1148. rres->col_names = safe_emalloc(rres->ncolumns, sizeof(char *), 0);
  1149. for (i = 0; i < rres->ncolumns; i++) {
  1150. colname = (char*)colnames[i];
  1151. if (SQLITE_G(assoc_case) == 1) {
  1152. php_sqlite_strtoupper(colname);
  1153. } else if (SQLITE_G(assoc_case) == 2) {
  1154. php_sqlite_strtolower(colname);
  1155. }
  1156. rres->col_names[i] = estrdup(colname);
  1157. }
  1158. if (!rres->buffered) {
  1159. /* non buffered mode - also fetch memory for on single row */
  1160. rres->table = safe_emalloc(rres->ncolumns, sizeof(char *), 0);
  1161. }
  1162. }
  1163. switch (ret) {
  1164. case SQLITE_ROW:
  1165. if (rres->buffered) {
  1166. /* add the row to our collection */
  1167. if (rres->nrows + 1 >= rres->alloc_rows) {
  1168. rres->alloc_rows = rres->alloc_rows ? rres->alloc_rows * 2 : 16;
  1169. rres->table = erealloc(rres->table, rres->alloc_rows * rres->ncolumns * sizeof(char *));
  1170. }
  1171. base = rres->nrows * rres->ncolumns;
  1172. for (i = 0; i < rres->ncolumns; i++) {
  1173. if (rowdata[i]) {
  1174. rres->table[base + i] = estrdup(rowdata[i]);
  1175. } else {
  1176. rres->table[base + i] = NULL;
  1177. }
  1178. }
  1179. rres->nrows++;
  1180. goto next_row;
  1181. } else {
  1182. /* non buffered: only fetch one row but first free data if not first row */
  1183. if (rres->nrows++) {
  1184. for (i = 0; i < rres->ncolumns; i++) {
  1185. if (rres->table[i]) {
  1186. efree(rres->table[i]);
  1187. }
  1188. }
  1189. }
  1190. for (i = 0; i < rres->ncolumns; i++) {
  1191. if (rowdata[i]) {
  1192. rres->table[i] = estrdup(rowdata[i]);
  1193. } else {
  1194. rres->table[i] = NULL;
  1195. }
  1196. }
  1197. }
  1198. ret = SQLITE_OK;
  1199. break;
  1200. case SQLITE_BUSY:
  1201. case SQLITE_ERROR:
  1202. case SQLITE_MISUSE:
  1203. case SQLITE_DONE:
  1204. default:
  1205. if (rres->vm) {
  1206. ret = sqlite_finalize(rres->vm, &errtext);
  1207. }
  1208. rres->vm = NULL;
  1209. if (ret != SQLITE_OK) {
  1210. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", errtext);
  1211. sqlite_freemem(errtext);
  1212. }
  1213. break;
  1214. }
  1215. rres->db->last_err_code = ret;
  1216. return ret;
  1217. }
  1218. /* }}} */
  1219. /* {{{ sqlite_query */
  1220. void sqlite_query(zval *object, struct php_sqlite_db *db, char *sql, long sql_len, int mode, int buffered, zval *return_value, struct php_sqlite_result **prres TSRMLS_DC)
  1221. {
  1222. struct php_sqlite_result res, *rres;
  1223. int ret;
  1224. char *errtext = NULL;
  1225. const char *tail;
  1226. memset(&res, 0, sizeof(res));
  1227. res.buffered = buffered;
  1228. res.mode = mode;
  1229. ret = sqlite_compile(db->db, sql, &tail, &res.vm, &errtext);
  1230. db->last_err_code = ret;
  1231. if (ret != SQLITE_OK) {
  1232. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", errtext);
  1233. sqlite_freemem(errtext);
  1234. if (return_value) {
  1235. RETURN_FALSE;
  1236. } else {
  1237. return;
  1238. }
  1239. }
  1240. if (!prres) {
  1241. rres = NULL;
  1242. prres = &rres;
  1243. }
  1244. if (!*prres) {
  1245. *prres = (struct php_sqlite_result*)emalloc(sizeof(**prres));
  1246. }
  1247. memcpy(*prres, &res, sizeof(**prres));
  1248. (*prres)->db = db;
  1249. zend_list_addref(db->rsrc_id);
  1250. /* now the result set is ready for stepping: get first row */
  1251. if (php_sqlite_fetch((*prres) TSRMLS_CC) != SQLITE_OK) {
  1252. real_result_dtor((*prres) TSRMLS_CC);
  1253. *prres = NULL;
  1254. if (return_value) {
  1255. RETURN_FALSE;
  1256. } else {
  1257. return;
  1258. }
  1259. }
  1260. (*prres)->curr_row = 0;
  1261. if (object) {
  1262. sqlite_object *obj;
  1263. if (buffered) {
  1264. sqlite_instanciate(sqlite_ce_query, return_value TSRMLS_CC);
  1265. } else {
  1266. sqlite_instanciate(sqlite_ce_ub_query, return_value TSRMLS_CC);
  1267. }
  1268. obj = (sqlite_object *) zend_object_store_get_object(return_value TSRMLS_CC);
  1269. obj->type = is_result;
  1270. obj->u.res = (*prres);
  1271. } else if (return_value) {
  1272. ZEND_REGISTER_RESOURCE(object ? NULL : return_value, (*prres), le_sqlite_result);
  1273. }
  1274. }
  1275. /* }}} */
  1276. /* {{{ proto resource sqlite_unbuffered_query(string query, resource db [ , int result_type ])
  1277. Executes a query that does not prefetch and buffer all data. */
  1278. PHP_FUNCTION(sqlite_unbuffered_query)
  1279. {
  1280. zval *zdb;
  1281. struct php_sqlite_db *db;
  1282. char *sql;
  1283. long sql_len;
  1284. int mode = PHPSQLITE_BOTH;
  1285. char *errtext = NULL;
  1286. zval *object = getThis();
  1287. if (object) {
  1288. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &sql, &sql_len, &mode)) {
  1289. return;
  1290. }
  1291. DB_FROM_OBJECT(db, object);
  1292. } else {
  1293. if (FAILURE == zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET,
  1294. ZEND_NUM_ARGS() TSRMLS_CC, "sr|l", &sql, &sql_len, &zdb, &mode) &&
  1295. FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|l", &zdb, &sql, &sql_len, &mode)) {
  1296. return;
  1297. }
  1298. DB_FROM_ZVAL(db, &zdb);
  1299. }
  1300. /* avoid doing work if we can */
  1301. if (!return_value_used) {
  1302. db->last_err_code = sqlite_exec(db->db, sql, NULL, NULL, &errtext);
  1303. if (db->last_err_code != SQLITE_OK) {
  1304. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", errtext);
  1305. sqlite_freemem(errtext);
  1306. }
  1307. return;
  1308. }
  1309. sqlite_query(object, db, sql, sql_len, mode, 0, return_value, NULL TSRMLS_CC);
  1310. }
  1311. /* }}} */
  1312. /* {{{ proto resource sqlite_fetch_column_types(string table_name, resource db)
  1313. Return an array of column types from a particular table. */
  1314. PHP_FUNCTION(sqlite_fetch_column_types)
  1315. {
  1316. zval *zdb;
  1317. struct php_sqlite_db *db;
  1318. char *tbl, *sql;
  1319. long tbl_len;
  1320. char *errtext = NULL;
  1321. zval *object = getThis();
  1322. struct php_sqlite_result res;
  1323. const char **rowdata, **colnames, *tail;
  1324. int i, ncols;
  1325. if (object) {
  1326. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &tbl, &tbl_len)) {
  1327. return;
  1328. }
  1329. DB_FROM_OBJECT(db, object);
  1330. } else {
  1331. if (FAILURE == zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET,
  1332. ZEND_NUM_ARGS() TSRMLS_CC, "sr", &tbl, &tbl_len, &zdb) &&
  1333. FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &zdb, &tbl, &tbl_len)) {
  1334. return;
  1335. }
  1336. DB_FROM_ZVAL(db, &zdb);
  1337. }
  1338. if (!(sql = sqlite_mprintf("SELECT * FROM %q LIMIT 1", tbl))) {
  1339. RETURN_FALSE;
  1340. }
  1341. sqlite_exec(db->db, "PRAGMA show_datatypes = ON", NULL, NULL, &errtext);
  1342. db->last_err_code = sqlite_compile(db->db, sql, &tail, &res.vm, &errtext);
  1343. sqlite_freemem(sql);
  1344. if (db->last_err_code != SQLITE_OK) {
  1345. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", errtext);
  1346. sqlite_freemem(errtext);
  1347. RETVAL_FALSE;
  1348. goto done;
  1349. }
  1350. sqlite_step(res.vm, &ncols, &rowdata, &colnames);
  1351. array_init(return_value);
  1352. for (i = 0; i < ncols; i++) {
  1353. char *colname = (char *)colnames[i];
  1354. if (SQLITE_G(assoc_case) == 1) {
  1355. php_sqlite_strtoupper(colname);
  1356. } else if (SQLITE_G(assoc_case) == 2) {
  1357. php_sqlite_strtolower(colname);
  1358. }
  1359. add_assoc_string(return_value, colname, colnames[ncols + i] ? (char *)colnames[ncols + i] : "", 1);
  1360. }
  1361. done:
  1362. sqlite_exec(db->db, "PRAGMA show_datatypes = OFF", NULL, NULL, &errtext);
  1363. }
  1364. /* }}} */
  1365. /* {{{ proto resource sqlite_query(string query, resource db [, int result_type ])
  1366. Executes a query against a given database and returns a result handle. */
  1367. PHP_FUNCTION(sqlite_query)
  1368. {
  1369. zval *zdb;
  1370. struct php_sqlite_db *db;
  1371. char *sql;
  1372. long sql_len;
  1373. int mode = PHPSQLITE_BOTH;
  1374. char *errtext = NULL;
  1375. zval *object = getThis();
  1376. if (object) {
  1377. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &sql, &sql_len, &mode)) {
  1378. return;
  1379. }
  1380. DB_FROM_OBJECT(db, object);
  1381. } else {
  1382. if (FAILURE == zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET,
  1383. ZEND_NUM_ARGS() TSRMLS_CC, "sr|l", &sql, &sql_len, &zdb, &mode) &&
  1384. FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|l", &zdb, &sql, &sql_len, &mode)) {
  1385. return;
  1386. }
  1387. DB_FROM_ZVAL(db, &zdb);
  1388. }
  1389. /* avoid doing work if we can */
  1390. if (!return_value_used) {
  1391. db->last_err_code = sqlite_exec(db->db, sql, NULL, NULL, &errtext);
  1392. if (db->last_err_code != SQLITE_OK) {
  1393. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", errtext);
  1394. sqlite_freemem(errtext);
  1395. }
  1396. return;
  1397. }
  1398. sqlite_query(object, db, sql, sql_len, mode, 1, return_value, NULL TSRMLS_CC);
  1399. }
  1400. /* }}} */
  1401. /* {{{ php_sqlite_fetch_array */
  1402. 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)
  1403. {
  1404. int j, n = res->ncolumns, buffered = res->buffered;
  1405. const char **rowdata, **colnames;
  1406. /* check range of the row */
  1407. if (res->curr_row >= res->nrows) {
  1408. /* no more */
  1409. RETURN_FALSE;
  1410. }
  1411. colnames = (const char**)res->col_names;
  1412. if (res->buffered) {
  1413. rowdata = (const char**)&res->table[res->curr_row * res->ncolumns];
  1414. } else {
  1415. rowdata = (const char**)res->table;
  1416. }
  1417. /* now populate the result */
  1418. array_init(return_value);
  1419. for (j = 0; j < n; j++) {
  1420. zval *decoded;
  1421. MAKE_STD_ZVAL(decoded);
  1422. if (rowdata[j] == NULL) {
  1423. ZVAL_NULL(decoded);
  1424. } else if (decode_binary && rowdata[j][0] == '\x01') {
  1425. Z_STRVAL_P(decoded) = emalloc(strlen(rowdata[j]));
  1426. Z_STRLEN_P(decoded) = php_sqlite_decode_binary(rowdata[j]+1, Z_STRVAL_P(decoded));
  1427. Z_STRVAL_P(decoded)[Z_STRLEN_P(decoded)] = '\0';
  1428. Z_TYPE_P(decoded) = IS_STRING;
  1429. if (!buffered) {
  1430. efree((char*)rowdata[j]);
  1431. rowdata[j] = NULL;
  1432. }
  1433. } else {
  1434. ZVAL_STRING(decoded, (char*)rowdata[j], buffered);
  1435. if (!buffered) {
  1436. rowdata[j] = NULL;
  1437. }
  1438. }
  1439. if (mode & PHPSQLITE_NUM) {
  1440. if (mode & PHPSQLITE_ASSOC) {
  1441. add_index_zval(return_value, j, decoded);
  1442. ZVAL_ADDREF(decoded);
  1443. add_assoc_zval(return_value, (char*)colnames[j], decoded);
  1444. } else {
  1445. add_next_index_zval(return_value, decoded);
  1446. }
  1447. } else {
  1448. add_assoc_zval(return_value, (char*)colnames[j], decoded);
  1449. }
  1450. }
  1451. if (move_next) {
  1452. if (!res->buffered) {
  1453. /* non buffered: fetch next row */
  1454. php_sqlite_fetch(res TSRMLS_CC);
  1455. }
  1456. /* advance the row pointer */
  1457. res->curr_row++;
  1458. }
  1459. }
  1460. /* }}} */
  1461. /* {{{ php_sqlite_fetch_column */
  1462. static void php_sqlite_fetch_column(struct php_sqlite_result *res, zval *which, zend_bool decode_binary, zval *return_value TSRMLS_DC)
  1463. {
  1464. int j;
  1465. const char **rowdata, **colnames;
  1466. /* check range of the row */
  1467. if (res->curr_row >= res->nrows) {
  1468. /* no more */
  1469. RETURN_FALSE;
  1470. }
  1471. colnames = (const char**)res->col_names;
  1472. if (Z_TYPE_P(which) == IS_LONG) {
  1473. j = Z_LVAL_P(which);
  1474. } else {
  1475. convert_to_string_ex(&which);
  1476. for (j = 0; j < res->ncolumns; j++) {
  1477. if (!strcasecmp((char*)colnames[j], Z_STRVAL_P(which))) {
  1478. break;
  1479. }
  1480. }
  1481. }
  1482. if (j < 0 || j >= res->ncolumns) {
  1483. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No such column %d", j);
  1484. RETURN_FALSE;
  1485. }
  1486. if (res->buffered) {
  1487. rowdata = (const char**)&res->table[res->curr_row * res->ncolumns];
  1488. } else {
  1489. rowdata = (const char**)res->table;
  1490. }
  1491. if (rowdata[j] == NULL) {
  1492. RETURN_NULL();
  1493. } else if (decode_binary && rowdata[j] != NULL && rowdata[j][0] == '\x01') {
  1494. int l = strlen(rowdata[j]);
  1495. char *decoded = emalloc(l);
  1496. l = php_sqlite_decode_binary(rowdata[j]+1, decoded);
  1497. decoded[l] = '\0';
  1498. RETVAL_STRINGL(decoded, l, 0);
  1499. if (!res->buffered) {
  1500. efree((char*)rowdata[j]);
  1501. rowdata[j] = NULL;
  1502. }
  1503. } else {
  1504. RETVAL_STRING((char*)rowdata[j], res->buffered);
  1505. if (!res->buffered) {
  1506. rowdata[j] = NULL;
  1507. }
  1508. }
  1509. }
  1510. /* }}} */
  1511. /* {{{ proto array sqlite_fetch_all(resource result [, int result_type [, bool decode_binary]])
  1512. Fetches all rows from a result set as an array of arrays. */
  1513. PHP_FUNCTION(sqlite_fetch_all)
  1514. {
  1515. zval *zres, *ent;
  1516. int mode = PHPSQLITE_BOTH;
  1517. zend_bool decode_binary = 1;
  1518. struct php_sqlite_result *res;
  1519. zval *object = getThis();
  1520. if (object) {
  1521. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lb", &mode, &decode_binary)) {
  1522. return;
  1523. }
  1524. RES_FROM_OBJECT(res, object);
  1525. if (!ZEND_NUM_ARGS()) {
  1526. mode = res->mode;
  1527. }
  1528. } else {
  1529. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|lb", &zres, &mode, &decode_binary)) {
  1530. return;
  1531. }
  1532. ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);
  1533. if (ZEND_NUM_ARGS() < 2) {
  1534. mode = res->mode;
  1535. }
  1536. }
  1537. if (res->curr_row >= res->nrows && res->nrows) {
  1538. if (!res->buffered) {
  1539. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "One or more rowsets were already returned");
  1540. } else {
  1541. res->curr_row = 0;
  1542. }
  1543. }
  1544. array_init(return_value);
  1545. while (res->curr_row < res->nrows) {
  1546. MAKE_STD_ZVAL(ent);
  1547. php_sqlite_fetch_array(res, mode, decode_binary, 1, ent TSRMLS_CC);
  1548. add_next_index_zval(return_value, ent);
  1549. }
  1550. }
  1551. /* }}} */
  1552. /* {{{ proto array sqlite_fetch_array(resource result [, int result_type [, bool decode_binary]])
  1553. Fetches the next row from a result set as an array. */
  1554. PHP_FUNCTION(sqlite_fetch_array)
  1555. {
  1556. zval *zres;
  1557. int mode = PHPSQLITE_BOTH;
  1558. zend_bool decode_binary = 1;
  1559. struct php_sqlite_result *res;
  1560. zval *object = getThis();
  1561. if (object) {
  1562. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lb", &mode, &decode_binary)) {
  1563. return;
  1564. }
  1565. RES_FROM_OBJECT(res, object);
  1566. if (!ZEND_NUM_ARGS()) {
  1567. mode = res->mode;
  1568. }
  1569. } else {
  1570. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|lb", &zres, &mode, &decode_binary)) {
  1571. return;
  1572. }
  1573. ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);
  1574. if (ZEND_NUM_ARGS() < 2) {
  1575. mode = res->mode;
  1576. }
  1577. }
  1578. php_sqlite_fetch_array(res, mode, decode_binary, 1, return_value TSRMLS_CC);
  1579. }
  1580. /* }}} */
  1581. /* {{{ proto object sqlite_fetch_object(resource result [, string class_name [, NULL|array ctor_params [, bool decode_binary]]])
  1582. Fetches the next row from a result set as an object. */
  1583. /* note that you can do array(&$val) for param ctor_params */
  1584. PHP_FUNCTION(sqlite_fetch_object)
  1585. {
  1586. zval *zres;
  1587. zend_bool decode_binary = 1;
  1588. struct php_sqlite_result *res;
  1589. zval *object = getThis();
  1590. char *class_name;
  1591. int class_name_len;
  1592. zend_class_entry *ce;
  1593. zval dataset;
  1594. zend_fcall_info fci;
  1595. zend_fcall_info_cache fcc;
  1596. zval *retval_ptr;
  1597. zval *ctor_params = NULL;
  1598. php_set_error_handling(object ? EH_THROW : EH_NORMAL, sqlite_ce_exception TSRMLS_CC);
  1599. if (object) {
  1600. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|szb", &class_name, &class_name_len, &ctor_params, &decode_binary)) {
  1601. php_std_error_handling();
  1602. return;
  1603. }
  1604. RES_FROM_OBJECT(res, object);
  1605. if (!ZEND_NUM_ARGS()) {
  1606. ce = zend_standard_class_def;
  1607. } else {
  1608. ce = zend_fetch_class(class_name, class_name_len, ZEND_FETCH_CLASS_AUTO TSRMLS_CC);
  1609. }
  1610. } else {
  1611. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|szb", &zres, &class_name, &class_name_len, &ctor_params, &decode_binary)) {
  1612. php_std_error_handling();
  1613. return;
  1614. }
  1615. ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);
  1616. if (ZEND_NUM_ARGS() < 2) {
  1617. ce = zend_standard_class_def;
  1618. } else {
  1619. ce = zend_fetch_class(class_name, class_name_len, ZEND_FETCH_CLASS_AUTO TSRMLS_CC);
  1620. }
  1621. }
  1622. if (!ce) {
  1623. zend_throw_exception_ex(sqlite_ce_exception, 0 TSRMLS_CC, "Could not find class '%s'", class_name);
  1624. php_std_error_handling();
  1625. return;
  1626. }
  1627. if (res->curr_row < res->nrows) {
  1628. php_sqlite_fetch_array(res, PHPSQLITE_ASSOC, decode_binary, 1, &dataset TSRMLS_CC);
  1629. } else {
  1630. RETURN_FALSE;
  1631. }
  1632. object_and_properties_init(return_value, ce, NULL);
  1633. zend_merge_properties(return_value, Z_ARRVAL(dataset), 1 TSRMLS_CC);
  1634. php_std_error_handling(); /* before calling the ctor */
  1635. if (ce->constructor) {
  1636. fci.size = sizeof(fci);
  1637. fci.function_table = &ce->function_table;
  1638. fci.function_name = NULL;
  1639. fci.symbol_table = NULL;
  1640. fci.object_pp = &return_value;
  1641. fci.retval_ptr_ptr = &retval_ptr;
  1642. if (ctor_params && Z_TYPE_P(ctor_params) != IS_NULL) {
  1643. if (Z_TYPE_P(ctor_params) == IS_ARRAY) {
  1644. HashTable *ht = Z_ARRVAL_P(ctor_params);
  1645. Bucket *p;
  1646. fci.param_count = 0;
  1647. fci.params = safe_emalloc(sizeof(zval*), ht->nNumOfElements, 0);
  1648. p = ht->pListHead;
  1649. while (p != NULL) {
  1650. fci.params[fci.param_count++] = (zval**)p->pData;
  1651. p = p->pListNext;
  1652. }
  1653. } else {
  1654. /* Two problems why we throw exceptions here: PHP is typeless
  1655. * and hence passing one argument that's not an array could be
  1656. * by mistake and the other way round is possible, too. The
  1657. * single value is an array. Also we'd have to make that one
  1658. * argument passed by reference.
  1659. */
  1660. zend_throw_exception(sqlite_ce_exception, "Parameter ctor_params must be an array", 0 TSRMLS_CC);
  1661. return;
  1662. }
  1663. } else {
  1664. fci.param_count = 0;
  1665. fci.params = NULL;
  1666. }
  1667. fci.no_separation = 1;
  1668. fcc.initialized = 1;
  1669. fcc.function_handler = ce->constructor;
  1670. fcc.calling_scope = EG(scope);
  1671. fcc.object_pp = &return_value;
  1672. if (zend_call_function(&fci, &fcc TSRMLS_CC) == FAILURE) {
  1673. zend_throw_exception_ex(sqlite_ce_exception, 0 TSRMLS_CC, "Could not execute %s::%s()", class_name, ce->constructor->common.function_name);
  1674. } else {
  1675. if (retval_ptr) {
  1676. zval_ptr_dtor(&retval_ptr);
  1677. }
  1678. }
  1679. if (fci.params) {
  1680. efree(fci.params);
  1681. }
  1682. } else if (ctor_params && Z_TYPE_P(ctor_params) != IS_NULL) {
  1683. zend_throw_exception_ex(sqlite_ce_exception, 0 TSRMLS_CC, "Class %s does not have a constructor, use NULL for parameter ctor_params or omit it", class_name);
  1684. }
  1685. }
  1686. /* }}} */
  1687. /* {{{ proto array sqlite_array_query(resource db, string query [ , int result_type [, bool decode_binary]])
  1688. Executes a query against a given database and returns an array of arrays. */
  1689. PHP_FUNCTION(sqlite_array_query)
  1690. {
  1691. zval *zdb, *ent;
  1692. struct php_sqlite_db *db;
  1693. struct php_sqlite_result *rres;
  1694. char *sql;
  1695. long sql_len;
  1696. int mode = PHPSQLITE_BOTH;
  1697. char *errtext = NULL;
  1698. zend_bool decode_binary = 1;
  1699. zval *object = getThis();
  1700. if (object) {
  1701. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lb", &sql, &sql_len, &mode, &decode_binary)) {
  1702. return;
  1703. }
  1704. DB_FROM_OBJECT(db, object);
  1705. } else {
  1706. if (FAILURE == zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET,
  1707. ZEND_NUM_ARGS() TSRMLS_CC, "sr|lb", &sql, &sql_len, &zdb, &mode, &decode_binary) &&
  1708. FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|lb", &zdb, &sql, &sql_len, &mode, &decode_binary)) {
  1709. return;
  1710. }
  1711. DB_FROM_ZVAL(db, &zdb);
  1712. }
  1713. /* avoid doing work if we can */
  1714. if (!return_value_used) {
  1715. db->last_err_code = sqlite_exec(db->db, sql, NULL, NULL, &errtext);
  1716. if (db->last_err_code != SQLITE_OK) {
  1717. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", errtext);
  1718. sqlite_freemem(errtext);
  1719. }
  1720. return;
  1721. }
  1722. rres = (struct php_sqlite_result *)emalloc(sizeof(*rres));
  1723. sqlite_query(NULL, db, sql, sql_len, mode, 0, NULL, &rres TSRMLS_CC);
  1724. if (db->last_err_code != SQLITE_OK) {
  1725. if (rres) {
  1726. efree(rres);
  1727. }
  1728. RETURN_FALSE;
  1729. }
  1730. array_init(return_value);
  1731. while (rres->curr_row < rres->nrows) {
  1732. MAKE_STD_ZVAL(ent);
  1733. php_sqlite_fetch_array(rres, mode, decode_binary, 1, ent TSRMLS_CC);
  1734. add_next_index_zval(return_value, ent);
  1735. }
  1736. real_result_dtor(rres TSRMLS_CC);
  1737. }
  1738. /* }}} */
  1739. /* {{{ php_sqlite_fetch_single */
  1740. static void php_sqlite_fetch_single(struct php_sqlite_result *res, zend_bool decode_binary, zval *return_value TSRMLS_DC)
  1741. {
  1742. const char **rowdata;
  1743. char *decoded;
  1744. int decoded_len;
  1745. /* check range of the row */
  1746. if (res->curr_row >= res->nrows) {
  1747. /* no more */
  1748. RETURN_FALSE;
  1749. }
  1750. if (res->buffered) {
  1751. rowdata = (const char**)&res->table[res->curr_row * res->ncolumns];
  1752. } else {
  1753. rowdata = (const char**)res->table;
  1754. }
  1755. if (decode_binary && rowdata[0] != NULL && rowdata[0][0] == '\x01') {
  1756. decoded = emalloc(strlen(rowdata[0]));
  1757. decoded_len = php_sqlite_decode_binary(rowdata[0]+1, decoded);
  1758. if (!res->buffered) {
  1759. efree((char*)rowdata[0]);
  1760. rowdata[0] = NULL;
  1761. }
  1762. } else if (rowdata[0]) {
  1763. decoded_len = strlen((char*)rowdata[0]);
  1764. if (res->buffered) {
  1765. decoded = estrndup((char*)rowdata[0], decoded_len);
  1766. } else {
  1767. decoded = (char*)rowdata[0];
  1768. rowdata[0] = NULL;
  1769. }
  1770. } else {
  1771. decoded = NULL;
  1772. decoded_len = 0;
  1773. }
  1774. if (!res->buffered) {
  1775. /* non buffered: fetch next row */
  1776. php_sqlite_fetch(res TSRMLS_CC);
  1777. }
  1778. /* advance the row pointer */
  1779. res->curr_row++;
  1780. if (decoded == NULL) {
  1781. RETURN_NULL();
  1782. } else {
  1783. RETURN_STRINGL(decoded, decoded_len, 0);
  1784. }
  1785. }
  1786. /* }}} */
  1787. /* {{{ proto array sqlite_single_query(resource db, string query [, bool first_row_only [, bool decode_binary]])
  1788. Executes a query and returns either an array for one single column or the value of the first row. */
  1789. PHP_FUNCTION(sqlite_single_query)
  1790. {
  1791. zval *zdb, *ent;
  1792. struct php_sqlite_db *db;
  1793. struct php_sqlite_result *rres;
  1794. char *sql;
  1795. long sql_len;
  1796. char *errtext = NULL;
  1797. zend_bool decode_binary = 1;
  1798. zend_bool srow = 1;
  1799. zval *object = getThis();
  1800. if (object) {
  1801. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|bb", &sql, &sql_len, &srow, &decode_binary)) {
  1802. return;
  1803. }
  1804. RES_FROM_OBJECT(db, object);
  1805. } else {
  1806. if (FAILURE == zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET,
  1807. ZEND_NUM_ARGS() TSRMLS_CC, "sr|bb", &sql, &sql_len, &zdb, &srow, &decode_binary) &&
  1808. FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|bb", &zdb, &sql, &sql_len, &srow, &decode_binary)) {
  1809. return;
  1810. }
  1811. DB_FROM_ZVAL(db, &zdb);
  1812. }
  1813. /* avoid doing work if we can */
  1814. if (!return_value_used) {
  1815. db->last_err_code = sqlite_exec(db->db, sql, NULL, NULL, &errtext);
  1816. if (db->last_err_code != SQLITE_OK) {
  1817. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", errtext);
  1818. sqlite_freemem(errtext);
  1819. }
  1820. return;
  1821. }
  1822. rres = (struct php_sqlite_result *)emalloc(sizeof(*rres));
  1823. sqlite_query(NULL, db, sql, sql_len, PHPSQLITE_NUM, 0, NULL, &rres TSRMLS_CC);
  1824. if (db->last_err_code != SQLITE_OK) {
  1825. if (rres) {
  1826. efree(rres);
  1827. }
  1828. RETURN_FALSE;
  1829. }
  1830. if (!srow) {
  1831. array_init(return_value);
  1832. }
  1833. while (rres->curr_row < rres->nrows) {
  1834. MAKE_STD_ZVAL(ent);
  1835. php_sqlite_fetch_single(rres, decode_binary, ent TSRMLS_CC);
  1836. /* if set and we only have 1 row in the result set, return the result as a string. */
  1837. if (srow) {
  1838. if (rres->curr_row == 1 && rres->curr_row >= rres->nrows) {
  1839. *return_value = *ent;
  1840. zval_copy_ctor(return_value);
  1841. zval_dtor(ent);
  1842. FREE_ZVAL(ent);
  1843. break;
  1844. } else {
  1845. srow = 0;
  1846. array_init(return_value);
  1847. }
  1848. }
  1849. add_next_index_zval(return_value, ent);
  1850. }
  1851. real_result_dtor(rres TSRMLS_CC);
  1852. }
  1853. /* }}} */
  1854. /* {{{ proto string sqlite_fetch_single(resource result [, bool decode_binary])
  1855. Fetches the first column of a result set as a string. */
  1856. PHP_FUNCTION(sqlite_fetch_single)
  1857. {
  1858. zval *zres;
  1859. zend_bool decode_binary = 1;
  1860. struct php_sqlite_result *res;
  1861. zval *object = getThis();
  1862. if (object) {
  1863. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &decode_binary)) {
  1864. return;
  1865. }
  1866. RES_FROM_OBJECT(res, object);
  1867. } else {
  1868. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|b", &zres, &decode_binary)) {
  1869. return;
  1870. }
  1871. ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);
  1872. }
  1873. php_sqlite_fetch_single(res, decode_binary, return_value TSRMLS_CC);
  1874. }
  1875. /* }}} */
  1876. /* {{{ proto array sqlite_current(resource result [, int result_type [, bool decode_binary]])
  1877. Fetches the current row from a result set as an array. */
  1878. PHP_FUNCTION(sqlite_current)
  1879. {
  1880. zval *zres;
  1881. int mode = PHPSQLITE_BOTH;
  1882. zend_bool decode_binary = 1;
  1883. struct php_sqlite_result *res;
  1884. zval *object = getThis();
  1885. if (object) {
  1886. if (ZEND_NUM_ARGS() && FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lb", &mode, &decode_binary)) {
  1887. return;
  1888. }
  1889. RES_FROM_OBJECT(res, object);
  1890. if (!ZEND_NUM_ARGS()) {
  1891. mode = res->mode;
  1892. }
  1893. } else {
  1894. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|lb", &zres, &mode, &decode_binary)) {
  1895. return;
  1896. }
  1897. ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);
  1898. if (ZEND_NUM_ARGS() < 2) {
  1899. mode = res->mode;
  1900. }
  1901. }
  1902. php_sqlite_fetch_array(res, mode, decode_binary, 0, return_value TSRMLS_CC);
  1903. }
  1904. /* }}} */
  1905. /* {{{ proto mixed sqlite_column(resource result, mixed index_or_name [, bool decode_binary])
  1906. Fetches a column from the current row of a result set. */
  1907. PHP_FUNCTION(sqlite_column)
  1908. {
  1909. zval *zres;
  1910. zval *which;
  1911. zend_bool decode_binary = 1;
  1912. struct php_sqlite_result *res;
  1913. zval *object = getThis();
  1914. if (object) {
  1915. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|b", &which, &decode_binary)) {
  1916. return;
  1917. }
  1918. RES_FROM_OBJECT(res, object);
  1919. } else {
  1920. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz|b", &zres, &which, &decode_binary)) {
  1921. return;
  1922. }
  1923. ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);
  1924. }
  1925. php_sqlite_fetch_column(res, which, decode_binary, return_value TSRMLS_CC);
  1926. }
  1927. /* }}} */
  1928. /* {{{ proto string sqlite_libversion()
  1929. Returns the version of the linked SQLite library. */
  1930. PHP_FUNCTION(sqlite_libversion)
  1931. {
  1932. if (ZEND_NUM_ARGS() != 0) {
  1933. WRONG_PARAM_COUNT;
  1934. }
  1935. RETURN_STRING((char*)sqlite_libversion(), 1);
  1936. }
  1937. /* }}} */
  1938. /* {{{ proto string sqlite_libencoding()
  1939. Returns the encoding (iso8859 or UTF-8) of the linked SQLite library. */
  1940. PHP_FUNCTION(sqlite_libencoding)
  1941. {
  1942. if (ZEND_NUM_ARGS() != 0) {
  1943. WRONG_PARAM_COUNT;
  1944. }
  1945. RETURN_STRING((char*)sqlite_libencoding(), 1);
  1946. }
  1947. /* }}} */
  1948. /* {{{ proto int sqlite_changes(resource db)
  1949. Returns the number of rows that were changed by the most recent SQL statement. */
  1950. PHP_FUNCTION(sqlite_changes)
  1951. {
  1952. zval *zdb;
  1953. struct php_sqlite_db *db;
  1954. zval *object = getThis();
  1955. if (object) {
  1956. if (ZEND_NUM_ARGS() != 0) {
  1957. WRONG_PARAM_COUNT
  1958. }
  1959. DB_FROM_OBJECT(db, object);
  1960. } else {
  1961. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zdb)) {
  1962. return;
  1963. }
  1964. DB_FROM_ZVAL(db, &zdb);
  1965. }
  1966. RETURN_LONG(sqlite_changes(db->db));
  1967. }
  1968. /* }}} */
  1969. /* {{{ proto int sqlite_last_insert_rowid(resource db)
  1970. Returns the rowid of the most recently inserted row. */
  1971. PHP_FUNCTION(sqlite_last_insert_rowid)
  1972. {
  1973. zval *zdb;
  1974. struct php_sqlite_db *db;
  1975. zval *object = getThis();
  1976. if (object) {
  1977. if (ZEND_NUM_ARGS() != 0) {
  1978. WRONG_PARAM_COUNT
  1979. }
  1980. DB_FROM_OBJECT(db, object);
  1981. } else {
  1982. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zdb)) {
  1983. return;
  1984. }
  1985. DB_FROM_ZVAL(db, &zdb);
  1986. }
  1987. RETURN_LONG(sqlite_last_insert_rowid(db->db));
  1988. }
  1989. /* }}} */
  1990. /* {{{ proto int sqlite_num_rows(resource result)
  1991. Returns the number of rows in a buffered result set. */
  1992. PHP_FUNCTION(sqlite_num_rows)
  1993. {
  1994. zval *zres;
  1995. struct php_sqlite_result *res;
  1996. zval *object = getThis();
  1997. if (object) {
  1998. if (ZEND_NUM_ARGS() != 0) {
  1999. WRONG_PARAM_COUNT
  2000. }
  2001. RES_FROM_OBJECT(res, object);
  2002. } else {
  2003. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zres)) {
  2004. return;
  2005. }
  2006. ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);
  2007. }
  2008. if (res->buffered) {
  2009. RETURN_LONG(res->nrows);
  2010. } else {
  2011. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Row count is not available for unbuffered queries");
  2012. RETURN_FALSE;
  2013. }
  2014. }
  2015. /* }}} */
  2016. /* {{{ proto bool sqlite_has_more(resource result)
  2017. Returns whether more rows are available. */
  2018. PHP_FUNCTION(sqlite_has_more)
  2019. {
  2020. zval *zres;
  2021. struct php_sqlite_result *res;
  2022. zval *object = getThis();
  2023. if (object) {
  2024. if (ZEND_NUM_ARGS() != 0) {
  2025. WRONG_PARAM_COUNT
  2026. }
  2027. RES_FROM_OBJECT(res, object);
  2028. } else {
  2029. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zres)) {
  2030. return;
  2031. }
  2032. ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);
  2033. }
  2034. RETURN_BOOL(res->curr_row < res->nrows && res->nrows); /* curr_row may be -1 */
  2035. }
  2036. /* }}} */
  2037. /* {{{ proto bool sqlite_has_prev(resource result)
  2038. * Returns whether a previous row is available. */
  2039. PHP_FUNCTION(sqlite_has_prev)
  2040. {
  2041. zval *zres;
  2042. struct php_sqlite_result *res;
  2043. zval *object = getThis();
  2044. if (object) {
  2045. if (ZEND_NUM_ARGS() != 0) {
  2046. WRONG_PARAM_COUNT
  2047. }
  2048. RES_FROM_OBJECT(res, object);
  2049. } else {
  2050. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zres)) {
  2051. return;
  2052. }
  2053. ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);
  2054. }
  2055. if(!res->buffered) {
  2056. php_error_docref(NULL TSRMLS_CC, E_WARNING, "you cannot use sqlite_has_prev on unbuffered querys");
  2057. RETURN_FALSE;
  2058. }
  2059. RETURN_BOOL(res->curr_row);
  2060. }
  2061. /* }}} */
  2062. /* {{{ proto int sqlite_num_fields(resource result)
  2063. Returns the number of fields in a result set. */
  2064. PHP_FUNCTION(sqlite_num_fields)
  2065. {
  2066. zval *zres;
  2067. struct php_sqlite_result *res;
  2068. zval *object = getThis();
  2069. if (object) {
  2070. if (ZEND_NUM_ARGS() != 0) {
  2071. WRONG_PARAM_COUNT
  2072. }
  2073. RES_FROM_OBJECT(res, object);
  2074. } else {
  2075. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zres)) {
  2076. return;
  2077. }
  2078. ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);
  2079. }
  2080. RETURN_LONG(res->ncolumns);
  2081. }
  2082. /* }}} */
  2083. /* {{{ proto string sqlite_field_name(resource result, int field_index)
  2084. Returns the name of a particular field of a result set. */
  2085. PHP_FUNCTION(sqlite_field_name)
  2086. {
  2087. zval *zres;
  2088. struct php_sqlite_result *res;
  2089. int field;
  2090. zval *object = getThis();
  2091. if (object) {
  2092. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &field)) {
  2093. return;
  2094. }
  2095. RES_FROM_OBJECT(res, object);
  2096. } else {
  2097. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &zres, &field)) {
  2098. return;
  2099. }
  2100. ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);
  2101. }
  2102. if (field < 0 || field >= res->ncolumns) {
  2103. php_error_docref(NULL TSRMLS_CC, E_WARNING, "field %d out of range", field);
  2104. RETURN_FALSE;
  2105. }
  2106. RETURN_STRING(res->col_names[field], 1);
  2107. }
  2108. /* }}} */
  2109. /* {{{ proto bool sqlite_seek(resource result, int row)
  2110. Seek to a particular row number of a buffered result set. */
  2111. PHP_FUNCTION(sqlite_seek)
  2112. {
  2113. zval *zres;
  2114. struct php_sqlite_result *res;
  2115. int row;
  2116. zval *object = getThis();
  2117. if (object) {
  2118. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &row)) {
  2119. return;
  2120. }
  2121. RES_FROM_OBJECT(res, object);
  2122. } else {
  2123. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &zres, &row)) {
  2124. return;
  2125. }
  2126. ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);
  2127. }
  2128. if (!res->buffered) {
  2129. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot seek an unbuffered result set");
  2130. RETURN_FALSE;
  2131. }
  2132. if (row < 0 || row >= res->nrows) {
  2133. php_error_docref(NULL TSRMLS_CC, E_WARNING, "row %d out of range", row);
  2134. RETURN_FALSE;
  2135. }
  2136. res->curr_row = row;
  2137. RETURN_TRUE;
  2138. }
  2139. /* }}} */
  2140. /* {{{ proto bool sqlite_rewind(resource result)
  2141. Seek to the first row number of a buffered result set. */
  2142. PHP_FUNCTION(sqlite_rewind)
  2143. {
  2144. zval *zres;
  2145. struct php_sqlite_result *res;
  2146. zval *object = getThis();
  2147. if (object) {
  2148. if (ZEND_NUM_ARGS() != 0) {
  2149. WRONG_PARAM_COUNT
  2150. }
  2151. RES_FROM_OBJECT(res, object);
  2152. } else {
  2153. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zres)) {
  2154. return;
  2155. }
  2156. ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);
  2157. }
  2158. if (!res->buffered) {
  2159. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot rewind an unbuffered result set");
  2160. RETURN_FALSE;
  2161. }
  2162. if (!res->nrows) {
  2163. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "no rows received");
  2164. RETURN_FALSE;
  2165. }
  2166. res->curr_row = 0;
  2167. RETURN_TRUE;
  2168. }
  2169. /* }}} */
  2170. /* {{{ proto bool sqlite_next(resource result)
  2171. Seek to the next row number of a result set. */
  2172. PHP_FUNCTION(sqlite_next)
  2173. {
  2174. zval *zres;
  2175. struct php_sqlite_result *res;
  2176. zval *object = getThis();
  2177. if (object) {
  2178. if (ZEND_NUM_ARGS() != 0) {
  2179. WRONG_PARAM_COUNT
  2180. }
  2181. RES_FROM_OBJECT(res, object);
  2182. } else {
  2183. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zres)) {
  2184. return;
  2185. }
  2186. ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);
  2187. }
  2188. if (!res->buffered && res->vm) {
  2189. php_sqlite_fetch(res TSRMLS_CC);
  2190. }
  2191. if (res->curr_row >= res->nrows) {
  2192. php_error_docref(NULL TSRMLS_CC, E_WARNING, "no more rows available");
  2193. RETURN_FALSE;
  2194. }
  2195. res->curr_row++;
  2196. RETURN_TRUE;
  2197. }
  2198. /* }}} */
  2199. /* {{{ proto bool sqlite_prev(resource result)
  2200. * Seek to the previous row number of a result set. */
  2201. PHP_FUNCTION(sqlite_prev)
  2202. {
  2203. zval *zres;
  2204. struct php_sqlite_result *res;
  2205. zval *object = getThis();
  2206. if (object) {
  2207. if (ZEND_NUM_ARGS() != 0) {
  2208. WRONG_PARAM_COUNT
  2209. }
  2210. RES_FROM_OBJECT(res, object);
  2211. } else {
  2212. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zres)) {
  2213. return;
  2214. }
  2215. ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);
  2216. }
  2217. if (!res->buffered) {
  2218. php_error_docref(NULL TSRMLS_CC, E_WARNING, "you cannot use sqlite_prev on unbuffered querys");
  2219. RETURN_FALSE;
  2220. }
  2221. if (res->curr_row <= 0) {
  2222. php_error_docref(NULL TSRMLS_CC, E_WARNING, "no previous row available");
  2223. RETURN_FALSE;
  2224. }
  2225. res->curr_row--;
  2226. RETURN_TRUE;
  2227. }
  2228. /* }}} */
  2229. /* {{{ proto string sqlite_escape_string(string item)
  2230. Escapes a string for use as a query parameter. */
  2231. PHP_FUNCTION(sqlite_escape_string)
  2232. {
  2233. char *string = NULL;
  2234. long stringlen;
  2235. char *ret;
  2236. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &string, &stringlen)) {
  2237. return;
  2238. }
  2239. if (stringlen && (string[0] == '\x01' || memchr(string, '\0', stringlen) != NULL)) {
  2240. /* binary string */
  2241. int enclen;
  2242. ret = emalloc( 1 + ((256 * stringlen + 1262) / 253) );
  2243. ret[0] = '\x01';
  2244. enclen = php_sqlite_encode_binary(string, stringlen, ret+1);
  2245. RETVAL_STRINGL(ret, enclen+1, 0);
  2246. } else {
  2247. ret = sqlite_mprintf("%q", string);
  2248. if (ret) {
  2249. RETVAL_STRING(ret, 1);
  2250. sqlite_freemem(ret);
  2251. }
  2252. }
  2253. }
  2254. /* }}} */
  2255. /* {{{ proto int sqlite_last_error(resource db)
  2256. Returns the error code of the last error for a database. */
  2257. PHP_FUNCTION(sqlite_last_error)
  2258. {
  2259. zval *zdb;
  2260. struct php_sqlite_db *db;
  2261. zval *object = getThis();
  2262. if (object) {
  2263. if (ZEND_NUM_ARGS() != 0) {
  2264. WRONG_PARAM_COUNT
  2265. }
  2266. DB_FROM_OBJECT(db, object);
  2267. } else {
  2268. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zdb)) {
  2269. return;
  2270. }
  2271. DB_FROM_ZVAL(db, &zdb);
  2272. }
  2273. RETURN_LONG(db->last_err_code);
  2274. }
  2275. /* }}} */
  2276. /* {{{ proto string sqlite_error_string(int error_code)
  2277. Returns the textual description of an error code. */
  2278. PHP_FUNCTION(sqlite_error_string)
  2279. {
  2280. long code;
  2281. const char *msg;
  2282. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &code)) {
  2283. return;
  2284. }
  2285. msg = sqlite_error_string(code);
  2286. if (msg) {
  2287. RETURN_STRING((char*)msg, 1);
  2288. } else {
  2289. RETURN_NULL();
  2290. }
  2291. }
  2292. /* }}} */
  2293. /* manages duplicate registrations of a particular function, and
  2294. * also handles the case where the db is using a persistent connection */
  2295. enum callback_prep_t { DO_REG, SKIP_REG, ERR };
  2296. static enum callback_prep_t prep_callback_struct(struct php_sqlite_db *db, int is_agg,
  2297. char *funcname,
  2298. zval *step, zval *fini, struct php_sqlite_agg_functions **funcs)
  2299. {
  2300. struct php_sqlite_agg_functions *alloc_funcs, func_tmp;
  2301. char *hashkey;
  2302. int hashkeylen;
  2303. enum callback_prep_t ret;
  2304. hashkeylen = spprintf(&hashkey, 0, "%s-%s", is_agg ? "agg" : "reg", funcname);
  2305. /* is it already registered ? */
  2306. if (SUCCESS == zend_hash_find(&db->callbacks, hashkey, hashkeylen+1, (void*)&alloc_funcs)) {
  2307. /* override the previous definition */
  2308. if (alloc_funcs->is_valid) {
  2309. /* release these */
  2310. if (alloc_funcs->step) {
  2311. zval_ptr_dtor(&alloc_funcs->step);
  2312. alloc_funcs->step = NULL;
  2313. }
  2314. if (alloc_funcs->fini) {
  2315. zval_ptr_dtor(&alloc_funcs->fini);
  2316. alloc_funcs->fini = NULL;
  2317. }
  2318. }
  2319. ret = SKIP_REG;
  2320. } else {
  2321. /* add a new one */
  2322. func_tmp.db = db;
  2323. ret = SUCCESS == zend_hash_update(&db->callbacks, hashkey, hashkeylen+1,
  2324. (void*)&func_tmp, sizeof(func_tmp), (void**)&alloc_funcs) ? DO_REG : ERR;
  2325. }
  2326. efree(hashkey);
  2327. MAKE_STD_ZVAL(alloc_funcs->step);
  2328. *(alloc_funcs->step) = *step;
  2329. zval_copy_ctor(alloc_funcs->step);
  2330. if (is_agg) {
  2331. MAKE_STD_ZVAL(alloc_funcs->fini);
  2332. *(alloc_funcs->fini) = *fini;
  2333. zval_copy_ctor(alloc_funcs->fini);
  2334. } else {
  2335. alloc_funcs->fini = NULL;
  2336. }
  2337. alloc_funcs->is_valid = 1;
  2338. *funcs = alloc_funcs;
  2339. return ret;
  2340. }
  2341. /* {{{ proto bool sqlite_create_aggregate(resource db, string funcname, mixed step_func, mixed finalize_func[, long num_args])
  2342. Registers an aggregate function for queries. */
  2343. PHP_FUNCTION(sqlite_create_aggregate)
  2344. {
  2345. char *funcname = NULL;
  2346. long funcname_len;
  2347. zval *zstep, *zfinal, *zdb;
  2348. struct php_sqlite_db *db;
  2349. struct php_sqlite_agg_functions *funcs;
  2350. char *callable = NULL;
  2351. long num_args = -1;
  2352. zval *object = getThis();
  2353. if (object) {
  2354. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "szz|l", &zdb, &funcname, &funcname_len, &zstep, &zfinal, &num_args)) {
  2355. return;
  2356. }
  2357. DB_FROM_OBJECT(db, object);
  2358. } else {
  2359. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rszz|l", &zdb, &funcname, &funcname_len, &zstep, &zfinal, &num_args)) {
  2360. return;
  2361. }
  2362. DB_FROM_ZVAL(db, &zdb);
  2363. }
  2364. if (!zend_is_callable(zstep, 0, &callable)) {
  2365. php_error_docref(NULL TSRMLS_CC, E_WARNING, "step function `%s' is not callable", callable);
  2366. efree(callable);
  2367. return;
  2368. }
  2369. efree(callable);
  2370. if (!zend_is_callable(zfinal, 0, &callable)) {
  2371. php_error_docref(NULL TSRMLS_CC, E_WARNING, "finalize function `%s' is not callable", callable);
  2372. efree(callable);
  2373. return;
  2374. }
  2375. efree(callable);
  2376. if (prep_callback_struct(db, 1, funcname, zstep, zfinal, &funcs) == DO_REG) {
  2377. sqlite_create_aggregate(db->db, funcname, num_args,
  2378. php_sqlite_agg_step_function_callback,
  2379. php_sqlite_agg_fini_function_callback, funcs);
  2380. }
  2381. }
  2382. /* }}} */
  2383. /* {{{ proto bool sqlite_create_function(resource db, string funcname, mixed callback[, long num_args])
  2384. Registers a "regular" function for queries. */
  2385. PHP_FUNCTION(sqlite_create_function)
  2386. {
  2387. char *funcname = NULL;
  2388. long funcname_len;
  2389. zval *zcall, *zdb;
  2390. struct php_sqlite_db *db;
  2391. struct php_sqlite_agg_functions *funcs;
  2392. char *callable = NULL;
  2393. long num_args = -1;
  2394. zval *object = getThis();
  2395. if (object) {
  2396. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|l", &funcname, &funcname_len, &zcall, &num_args)) {
  2397. return;
  2398. }
  2399. DB_FROM_OBJECT(db, object);
  2400. } else {
  2401. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsz|l", &zdb, &funcname, &funcname_len, &zcall, &num_args)) {
  2402. return;
  2403. }
  2404. DB_FROM_ZVAL(db, &zdb);
  2405. }
  2406. if (!zend_is_callable(zcall, 0, &callable)) {
  2407. php_error_docref(NULL TSRMLS_CC, E_WARNING, "function `%s' is not callable", callable);
  2408. efree(callable);
  2409. return;
  2410. }
  2411. efree(callable);
  2412. if (prep_callback_struct(db, 0, funcname, zcall, NULL, &funcs) == DO_REG) {
  2413. sqlite_create_function(db->db, funcname, num_args, php_sqlite_function_callback, funcs);
  2414. }
  2415. }
  2416. /* }}} */
  2417. /* {{{ proto string sqlite_udf_encode_binary(string data)
  2418. Apply binary encoding (if required) to a string to return from an UDF. */
  2419. PHP_FUNCTION(sqlite_udf_encode_binary)
  2420. {
  2421. char *data = NULL;
  2422. long datalen;
  2423. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!", &data, &datalen)) {
  2424. return;
  2425. }
  2426. if (data == NULL) {
  2427. RETURN_NULL();
  2428. }
  2429. if (datalen && (data[0] == '\x01' || memchr(data, '\0', datalen) != NULL)) {
  2430. /* binary string */
  2431. int enclen;
  2432. char *ret;
  2433. ret = emalloc( 1 + ((256 * datalen + 1262) / 253) );
  2434. ret[0] = '\x01';
  2435. enclen = php_sqlite_encode_binary(data, datalen, ret+1);
  2436. RETVAL_STRINGL(ret, enclen+1, 0);
  2437. } else {
  2438. RETVAL_STRINGL(data, datalen, 1);
  2439. }
  2440. }
  2441. /* }}} */
  2442. /* {{{ proto string sqlite_udf_decode_binary(string data)
  2443. Decode binary encoding on a string parameter passed to an UDF. */
  2444. PHP_FUNCTION(sqlite_udf_decode_binary)
  2445. {
  2446. char *data = NULL;
  2447. long datalen;
  2448. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!", &data, &datalen)) {
  2449. return;
  2450. }
  2451. if (data == NULL) {
  2452. RETURN_NULL();
  2453. }
  2454. if (datalen && data[0] == '\x01') {
  2455. /* encoded string */
  2456. int enclen;
  2457. char *ret;
  2458. ret = emalloc(datalen);
  2459. enclen = php_sqlite_decode_binary(data+1, ret);
  2460. ret[enclen] = '\0';
  2461. RETVAL_STRINGL(ret, enclen, 0);
  2462. } else {
  2463. RETVAL_STRINGL(data, datalen, 1);
  2464. }
  2465. }
  2466. /* }}} */
  2467. /*
  2468. * Local variables:
  2469. * tab-width: 4
  2470. * c-basic-offset: 4
  2471. * End:
  2472. * vim600: sw=4 ts=4 fdm=marker
  2473. * vim<600: sw=4 ts=4
  2474. */