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.

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