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.

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