PageRenderTime 106ms CodeModel.GetById 23ms RepoModel.GetById 2ms app.codeStats 0ms

/ext/sqlite3/sqlite3.c

http://github.com/php/php-src
C | 2596 lines | 1912 code | 450 blank | 234 comment | 408 complexity | 4b829e0737f02bf5afd78b34b80e17cc MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | http://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Scott MacVicar <scottmac@php.net> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #ifdef HAVE_CONFIG_H
  17. #include "config.h"
  18. #endif
  19. #include "php.h"
  20. #include "php_ini.h"
  21. #include "ext/standard/info.h"
  22. #include "php_sqlite3.h"
  23. #include "php_sqlite3_structs.h"
  24. #include "sqlite3_arginfo.h"
  25. #include "main/SAPI.h"
  26. #include <sqlite3.h>
  27. #include "zend_exceptions.h"
  28. #include "zend_interfaces.h"
  29. #include "SAPI.h"
  30. ZEND_DECLARE_MODULE_GLOBALS(sqlite3)
  31. static PHP_GINIT_FUNCTION(sqlite3);
  32. static int php_sqlite3_authorizer(void *autharg, int action, const char *arg1, const char *arg2, const char *arg3, const char *arg4);
  33. static void sqlite3_param_dtor(zval *data);
  34. static int php_sqlite3_compare_stmt_zval_free(php_sqlite3_free_list **free_list, zval *statement);
  35. /* {{{ Error Handler
  36. */
  37. static void php_sqlite3_error(php_sqlite3_db_object *db_obj, char *format, ...)
  38. {
  39. va_list arg;
  40. char *message;
  41. va_start(arg, format);
  42. vspprintf(&message, 0, format, arg);
  43. va_end(arg);
  44. if (db_obj && db_obj->exception) {
  45. zend_throw_exception(zend_ce_exception, message, 0);
  46. } else {
  47. php_error_docref(NULL, E_WARNING, "%s", message);
  48. }
  49. if (message) {
  50. efree(message);
  51. }
  52. }
  53. /* }}} */
  54. #define SQLITE3_CHECK_INITIALIZED(db_obj, member, class_name) \
  55. if (!(db_obj) || !(member)) { \
  56. php_sqlite3_error(db_obj, "The " #class_name " object has not been correctly initialised"); \
  57. RETURN_FALSE; \
  58. }
  59. #define SQLITE3_CHECK_INITIALIZED_STMT(member, class_name) \
  60. if (!(member)) { \
  61. php_error_docref(NULL, E_WARNING, "The " #class_name " object has not been correctly initialised"); \
  62. RETURN_FALSE; \
  63. }
  64. /* {{{ PHP_INI
  65. */
  66. PHP_INI_BEGIN()
  67. STD_PHP_INI_ENTRY("sqlite3.extension_dir", NULL, PHP_INI_SYSTEM, OnUpdateString, extension_dir, zend_sqlite3_globals, sqlite3_globals)
  68. #if SQLITE_VERSION_NUMBER >= 3026000
  69. STD_PHP_INI_ENTRY("sqlite3.defensive", "1", PHP_INI_SYSTEM, OnUpdateBool, dbconfig_defensive, zend_sqlite3_globals, sqlite3_globals)
  70. #endif
  71. PHP_INI_END()
  72. /* }}} */
  73. /* Handlers */
  74. static zend_object_handlers sqlite3_object_handlers;
  75. static zend_object_handlers sqlite3_stmt_object_handlers;
  76. static zend_object_handlers sqlite3_result_object_handlers;
  77. /* Class entries */
  78. zend_class_entry *php_sqlite3_sc_entry;
  79. zend_class_entry *php_sqlite3_stmt_entry;
  80. zend_class_entry *php_sqlite3_result_entry;
  81. /* {{{ proto void SQLite3::open(String filename [, int Flags [, string Encryption Key]])
  82. Opens a SQLite 3 Database, if the build includes encryption then it will attempt to use the key. */
  83. PHP_METHOD(SQLite3, open)
  84. {
  85. php_sqlite3_db_object *db_obj;
  86. zval *object = ZEND_THIS;
  87. char *filename, *encryption_key, *fullpath;
  88. size_t filename_len, encryption_key_len = 0;
  89. zend_long flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
  90. int rc;
  91. db_obj = Z_SQLITE3_DB_P(object);
  92. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "p|ls", &filename, &filename_len, &flags, &encryption_key, &encryption_key_len)) {
  93. RETURN_THROWS();
  94. }
  95. if (db_obj->initialised) {
  96. zend_throw_exception(zend_ce_exception, "Already initialised DB Object", 0);
  97. RETURN_THROWS();
  98. }
  99. if (filename_len != 0 && (filename_len != sizeof(":memory:")-1 ||
  100. memcmp(filename, ":memory:", sizeof(":memory:")-1) != 0)) {
  101. if (!(fullpath = expand_filepath(filename, NULL))) {
  102. zend_throw_exception(zend_ce_exception, "Unable to expand filepath", 0);
  103. RETURN_THROWS();
  104. }
  105. if (php_check_open_basedir(fullpath)) {
  106. zend_throw_exception_ex(zend_ce_exception, 0, "open_basedir prohibits opening %s", fullpath);
  107. efree(fullpath);
  108. RETURN_THROWS();
  109. }
  110. } else {
  111. /* filename equals "" or ":memory:" */
  112. fullpath = filename;
  113. }
  114. rc = sqlite3_open_v2(fullpath, &(db_obj->db), flags, NULL);
  115. if (rc != SQLITE_OK) {
  116. sqlite3_close(db_obj->db);
  117. zend_throw_exception_ex(zend_ce_exception, 0, "Unable to open database: %s",
  118. #ifdef HAVE_SQLITE3_ERRSTR
  119. db_obj->db ? sqlite3_errmsg(db_obj->db) : sqlite3_errstr(rc));
  120. #else
  121. db_obj->db ? sqlite3_errmsg(db_obj->db) : "");
  122. #endif
  123. if (fullpath != filename) {
  124. efree(fullpath);
  125. }
  126. return;
  127. }
  128. #if SQLITE_HAS_CODEC
  129. if (encryption_key_len > 0) {
  130. if (sqlite3_key(db_obj->db, encryption_key, encryption_key_len) != SQLITE_OK) {
  131. sqlite3_close(db_obj->db);
  132. zend_throw_exception_ex(zend_ce_exception, 0, "Unable to open database: %s", sqlite3_errmsg(db_obj->db));
  133. RETURN_THROWS();
  134. }
  135. }
  136. #endif
  137. db_obj->initialised = 1;
  138. db_obj->authorizer_fci = empty_fcall_info;
  139. db_obj->authorizer_fcc = empty_fcall_info_cache;
  140. sqlite3_set_authorizer(db_obj->db, php_sqlite3_authorizer, db_obj);
  141. #if SQLITE_VERSION_NUMBER >= 3026000
  142. if (SQLITE3G(dbconfig_defensive)) {
  143. sqlite3_db_config(db_obj->db, SQLITE_DBCONFIG_DEFENSIVE, 1, NULL);
  144. }
  145. #endif
  146. if (fullpath != filename) {
  147. efree(fullpath);
  148. }
  149. }
  150. /* }}} */
  151. /* {{{ proto bool SQLite3::close()
  152. Close a SQLite 3 Database. */
  153. PHP_METHOD(SQLite3, close)
  154. {
  155. php_sqlite3_db_object *db_obj;
  156. zval *object = ZEND_THIS;
  157. int errcode;
  158. db_obj = Z_SQLITE3_DB_P(object);
  159. if (zend_parse_parameters_none() == FAILURE) {
  160. RETURN_THROWS();
  161. }
  162. if (db_obj->initialised) {
  163. zend_llist_clean(&(db_obj->free_list));
  164. if(db_obj->db) {
  165. errcode = sqlite3_close(db_obj->db);
  166. if (errcode != SQLITE_OK) {
  167. php_sqlite3_error(db_obj, "Unable to close database: %d, %s", errcode, sqlite3_errmsg(db_obj->db));
  168. RETURN_FALSE;
  169. }
  170. }
  171. db_obj->initialised = 0;
  172. }
  173. RETURN_TRUE;
  174. }
  175. /* }}} */
  176. /* {{{ proto bool SQLite3::exec(String Query)
  177. Executes a result-less query against a given database. */
  178. PHP_METHOD(SQLite3, exec)
  179. {
  180. php_sqlite3_db_object *db_obj;
  181. zval *object = ZEND_THIS;
  182. zend_string *sql;
  183. char *errtext = NULL;
  184. db_obj = Z_SQLITE3_DB_P(object);
  185. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "S", &sql)) {
  186. RETURN_THROWS();
  187. }
  188. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  189. if (sqlite3_exec(db_obj->db, ZSTR_VAL(sql), NULL, NULL, &errtext) != SQLITE_OK) {
  190. php_sqlite3_error(db_obj, "%s", errtext);
  191. sqlite3_free(errtext);
  192. RETURN_FALSE;
  193. }
  194. RETURN_TRUE;
  195. }
  196. /* }}} */
  197. /* {{{ proto Array SQLite3::version()
  198. Returns the SQLite3 Library version as a string constant and as a number. */
  199. PHP_METHOD(SQLite3, version)
  200. {
  201. if (zend_parse_parameters_none() == FAILURE) {
  202. RETURN_THROWS();
  203. }
  204. array_init(return_value);
  205. add_assoc_string(return_value, "versionString", (char*)sqlite3_libversion());
  206. add_assoc_long(return_value, "versionNumber", sqlite3_libversion_number());
  207. return;
  208. }
  209. /* }}} */
  210. /* {{{ proto int SQLite3::lastInsertRowID()
  211. Returns the rowid of the most recent INSERT into the database from the database connection. */
  212. PHP_METHOD(SQLite3, lastInsertRowID)
  213. {
  214. php_sqlite3_db_object *db_obj;
  215. zval *object = ZEND_THIS;
  216. db_obj = Z_SQLITE3_DB_P(object);
  217. if (zend_parse_parameters_none() == FAILURE) {
  218. RETURN_THROWS();
  219. }
  220. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  221. RETURN_LONG((zend_long) sqlite3_last_insert_rowid(db_obj->db));
  222. }
  223. /* }}} */
  224. /* {{{ proto int SQLite3::lastErrorCode()
  225. Returns the numeric result code of the most recent failed sqlite API call for the database connection. */
  226. PHP_METHOD(SQLite3, lastErrorCode)
  227. {
  228. php_sqlite3_db_object *db_obj;
  229. zval *object = ZEND_THIS;
  230. db_obj = Z_SQLITE3_DB_P(object);
  231. if (zend_parse_parameters_none() == FAILURE) {
  232. RETURN_THROWS();
  233. }
  234. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->db, SQLite3)
  235. if (db_obj->initialised) {
  236. RETURN_LONG(sqlite3_errcode(db_obj->db));
  237. } else {
  238. RETURN_LONG(0);
  239. }
  240. }
  241. /* }}} */
  242. /* {{{ proto int SQLite3::lastExtendedErrorCode()
  243. Returns the numeric extended result code of the most recent failed sqlite API call for the database connection. */
  244. PHP_METHOD(SQLite3, lastExtendedErrorCode)
  245. {
  246. php_sqlite3_db_object *db_obj;
  247. zval *object = ZEND_THIS;
  248. db_obj = Z_SQLITE3_DB_P(object);
  249. if (zend_parse_parameters_none() == FAILURE) {
  250. RETURN_THROWS();
  251. }
  252. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->db, SQLite3)
  253. if (db_obj->initialised) {
  254. RETURN_LONG(sqlite3_extended_errcode(db_obj->db));
  255. } else {
  256. RETURN_LONG(0);
  257. }
  258. }
  259. /* }}} */
  260. /* {{{ proto bool SQLite3::enableExtendedResultCodes([bool enable = true])
  261. Turns on or off the extended result codes feature of SQLite. */
  262. PHP_METHOD(SQLite3, enableExtendedResultCodes)
  263. {
  264. php_sqlite3_db_object *db_obj;
  265. zval *object = ZEND_THIS;
  266. zend_bool enable = 1;
  267. db_obj = Z_SQLITE3_DB_P(object);
  268. int ret;
  269. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &enable) == FAILURE) {
  270. RETURN_THROWS();
  271. }
  272. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->db, SQLite3)
  273. if (db_obj->initialised) {
  274. ret = sqlite3_extended_result_codes(db_obj->db, enable ? 1 : 0);
  275. if (ret == SQLITE_OK)
  276. {
  277. RETURN_TRUE;
  278. }
  279. }
  280. RETURN_FALSE;
  281. }
  282. /* }}} */
  283. /* {{{ proto string SQLite3::lastErrorMsg()
  284. Returns english text describing the most recent failed sqlite API call for the database connection. */
  285. PHP_METHOD(SQLite3, lastErrorMsg)
  286. {
  287. php_sqlite3_db_object *db_obj;
  288. zval *object = ZEND_THIS;
  289. db_obj = Z_SQLITE3_DB_P(object);
  290. if (zend_parse_parameters_none() == FAILURE) {
  291. RETURN_THROWS();
  292. }
  293. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->db, SQLite3)
  294. if (db_obj->initialised) {
  295. RETURN_STRING((char *)sqlite3_errmsg(db_obj->db));
  296. } else {
  297. RETURN_EMPTY_STRING();
  298. }
  299. }
  300. /* }}} */
  301. /* {{{ proto bool SQLite3::busyTimeout(int msecs)
  302. Sets a busy handler that will sleep until database is not locked or timeout is reached. Passing a value less than or equal to zero turns off all busy handlers. */
  303. PHP_METHOD(SQLite3, busyTimeout)
  304. {
  305. php_sqlite3_db_object *db_obj;
  306. zval *object = ZEND_THIS;
  307. zend_long ms;
  308. #ifdef SQLITE_ENABLE_API_ARMOR
  309. int return_code;
  310. #endif
  311. db_obj = Z_SQLITE3_DB_P(object);
  312. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "l", &ms)) {
  313. RETURN_THROWS();
  314. }
  315. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  316. #ifdef SQLITE_ENABLE_API_ARMOR
  317. return_code = sqlite3_busy_timeout(db_obj->db, ms);
  318. if (return_code != SQLITE_OK) {
  319. php_sqlite3_error(db_obj, "Unable to set busy timeout: %d, %s", return_code, sqlite3_errmsg(db_obj->db));
  320. RETURN_FALSE;
  321. }
  322. #else
  323. php_ignore_value(sqlite3_busy_timeout(db_obj->db, ms));
  324. #endif
  325. RETURN_TRUE;
  326. }
  327. /* }}} */
  328. #ifndef SQLITE_OMIT_LOAD_EXTENSION
  329. /* {{{ proto bool SQLite3::loadExtension(String Shared Library)
  330. Attempts to load an SQLite extension library. */
  331. PHP_METHOD(SQLite3, loadExtension)
  332. {
  333. php_sqlite3_db_object *db_obj;
  334. zval *object = ZEND_THIS;
  335. char *extension, *lib_path, *extension_dir, *errtext = NULL;
  336. char fullpath[MAXPATHLEN];
  337. size_t extension_len, extension_dir_len;
  338. db_obj = Z_SQLITE3_DB_P(object);
  339. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "s", &extension, &extension_len)) {
  340. RETURN_THROWS();
  341. }
  342. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  343. #ifdef ZTS
  344. if ((strncmp(sapi_module.name, "cgi", 3) != 0) &&
  345. (strcmp(sapi_module.name, "cli") != 0) &&
  346. (strncmp(sapi_module.name, "embed", 5) != 0)
  347. ) { php_sqlite3_error(db_obj, "Not supported in multithreaded Web servers");
  348. RETURN_FALSE;
  349. }
  350. #endif
  351. if (!SQLITE3G(extension_dir)) {
  352. php_sqlite3_error(db_obj, "SQLite Extension are disabled");
  353. RETURN_FALSE;
  354. }
  355. if (extension_len == 0) {
  356. php_sqlite3_error(db_obj, "Empty string as an extension");
  357. RETURN_FALSE;
  358. }
  359. extension_dir = SQLITE3G(extension_dir);
  360. extension_dir_len = strlen(SQLITE3G(extension_dir));
  361. if (IS_SLASH(extension_dir[extension_dir_len-1])) {
  362. spprintf(&lib_path, 0, "%s%s", extension_dir, extension);
  363. } else {
  364. spprintf(&lib_path, 0, "%s%c%s", extension_dir, DEFAULT_SLASH, extension);
  365. }
  366. if (!VCWD_REALPATH(lib_path, fullpath)) {
  367. php_sqlite3_error(db_obj, "Unable to load extension at '%s'", lib_path);
  368. efree(lib_path);
  369. RETURN_FALSE;
  370. }
  371. efree(lib_path);
  372. if (strncmp(fullpath, extension_dir, extension_dir_len) != 0) {
  373. php_sqlite3_error(db_obj, "Unable to open extensions outside the defined directory");
  374. RETURN_FALSE;
  375. }
  376. /* Extension loading should only be enabled for when we attempt to load */
  377. sqlite3_enable_load_extension(db_obj->db, 1);
  378. if (sqlite3_load_extension(db_obj->db, fullpath, 0, &errtext) != SQLITE_OK) {
  379. php_sqlite3_error(db_obj, "%s", errtext);
  380. sqlite3_free(errtext);
  381. sqlite3_enable_load_extension(db_obj->db, 0);
  382. RETURN_FALSE;
  383. }
  384. sqlite3_enable_load_extension(db_obj->db, 0);
  385. RETURN_TRUE;
  386. }
  387. /* }}} */
  388. #endif
  389. /* {{{ proto int SQLite3::changes()
  390. Returns the number of database rows that were changed (or inserted or deleted) by the most recent SQL statement. */
  391. PHP_METHOD(SQLite3, changes)
  392. {
  393. php_sqlite3_db_object *db_obj;
  394. zval *object = ZEND_THIS;
  395. db_obj = Z_SQLITE3_DB_P(object);
  396. if (zend_parse_parameters_none() == FAILURE) {
  397. RETURN_THROWS();
  398. }
  399. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  400. RETURN_LONG(sqlite3_changes(db_obj->db));
  401. }
  402. /* }}} */
  403. /* {{{ proto String SQLite3::escapeString(String value)
  404. Returns a string that has been properly escaped. */
  405. PHP_METHOD(SQLite3, escapeString)
  406. {
  407. zend_string *sql;
  408. char *ret;
  409. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "S", &sql)) {
  410. RETURN_THROWS();
  411. }
  412. if (ZSTR_LEN(sql)) {
  413. ret = sqlite3_mprintf("%q", ZSTR_VAL(sql));
  414. if (ret) {
  415. RETVAL_STRING(ret);
  416. sqlite3_free(ret);
  417. }
  418. } else {
  419. RETURN_EMPTY_STRING();
  420. }
  421. }
  422. /* }}} */
  423. /* {{{ proto SQLite3Stmt SQLite3::prepare(String Query)
  424. Returns a prepared SQL statement for execution. */
  425. PHP_METHOD(SQLite3, prepare)
  426. {
  427. php_sqlite3_db_object *db_obj;
  428. php_sqlite3_stmt *stmt_obj;
  429. zval *object = ZEND_THIS;
  430. zend_string *sql;
  431. int errcode;
  432. php_sqlite3_free_list *free_item;
  433. db_obj = Z_SQLITE3_DB_P(object);
  434. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "S", &sql)) {
  435. RETURN_THROWS();
  436. }
  437. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  438. if (!ZSTR_LEN(sql)) {
  439. RETURN_FALSE;
  440. }
  441. object_init_ex(return_value, php_sqlite3_stmt_entry);
  442. stmt_obj = Z_SQLITE3_STMT_P(return_value);
  443. stmt_obj->db_obj = db_obj;
  444. Z_ADDREF_P(object);
  445. ZVAL_OBJ(&stmt_obj->db_obj_zval, Z_OBJ_P(object));
  446. errcode = sqlite3_prepare_v2(db_obj->db, ZSTR_VAL(sql), ZSTR_LEN(sql), &(stmt_obj->stmt), NULL);
  447. if (errcode != SQLITE_OK) {
  448. php_sqlite3_error(db_obj, "Unable to prepare statement: %d, %s", errcode, sqlite3_errmsg(db_obj->db));
  449. zval_ptr_dtor(return_value);
  450. RETURN_FALSE;
  451. }
  452. stmt_obj->initialised = 1;
  453. free_item = emalloc(sizeof(php_sqlite3_free_list));
  454. free_item->stmt_obj = stmt_obj;
  455. ZVAL_OBJ(&free_item->stmt_obj_zval, Z_OBJ_P(return_value));
  456. zend_llist_add_element(&(db_obj->free_list), &free_item);
  457. }
  458. /* }}} */
  459. /* {{{ proto SQLite3Result SQLite3::query(String Query)
  460. Returns true or false, for queries that return data it will return a SQLite3Result object. */
  461. PHP_METHOD(SQLite3, query)
  462. {
  463. php_sqlite3_db_object *db_obj;
  464. php_sqlite3_result *result;
  465. php_sqlite3_stmt *stmt_obj;
  466. zval *object = ZEND_THIS;
  467. zval stmt;
  468. zend_string *sql;
  469. char *errtext = NULL;
  470. int return_code;
  471. db_obj = Z_SQLITE3_DB_P(object);
  472. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "S", &sql)) {
  473. RETURN_THROWS();
  474. }
  475. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  476. if (!ZSTR_LEN(sql)) {
  477. RETURN_FALSE;
  478. }
  479. /* If there was no return value then just execute the query */
  480. if (!USED_RET()) {
  481. if (sqlite3_exec(db_obj->db, ZSTR_VAL(sql), NULL, NULL, &errtext) != SQLITE_OK) {
  482. php_sqlite3_error(db_obj, "%s", errtext);
  483. sqlite3_free(errtext);
  484. }
  485. return;
  486. }
  487. object_init_ex(&stmt, php_sqlite3_stmt_entry);
  488. stmt_obj = Z_SQLITE3_STMT_P(&stmt);
  489. stmt_obj->db_obj = db_obj;
  490. Z_ADDREF_P(object);
  491. ZVAL_OBJ(&stmt_obj->db_obj_zval, Z_OBJ_P(object));
  492. return_code = sqlite3_prepare_v2(db_obj->db, ZSTR_VAL(sql), ZSTR_LEN(sql), &(stmt_obj->stmt), NULL);
  493. if (return_code != SQLITE_OK) {
  494. php_sqlite3_error(db_obj, "Unable to prepare statement: %d, %s", return_code, sqlite3_errmsg(db_obj->db));
  495. zval_ptr_dtor(&stmt);
  496. RETURN_FALSE;
  497. }
  498. stmt_obj->initialised = 1;
  499. object_init_ex(return_value, php_sqlite3_result_entry);
  500. result = Z_SQLITE3_RESULT_P(return_value);
  501. result->db_obj = db_obj;
  502. result->stmt_obj = stmt_obj;
  503. ZVAL_OBJ(&result->stmt_obj_zval, Z_OBJ(stmt));
  504. return_code = sqlite3_step(result->stmt_obj->stmt);
  505. switch (return_code) {
  506. case SQLITE_ROW: /* Valid Row */
  507. case SQLITE_DONE: /* Valid but no results */
  508. {
  509. php_sqlite3_free_list *free_item;
  510. free_item = emalloc(sizeof(php_sqlite3_free_list));
  511. free_item->stmt_obj = stmt_obj;
  512. free_item->stmt_obj_zval = stmt;
  513. zend_llist_add_element(&(db_obj->free_list), &free_item);
  514. sqlite3_reset(result->stmt_obj->stmt);
  515. break;
  516. }
  517. default:
  518. if (!EG(exception)) {
  519. php_sqlite3_error(db_obj, "Unable to execute statement: %s", sqlite3_errmsg(db_obj->db));
  520. }
  521. sqlite3_finalize(stmt_obj->stmt);
  522. stmt_obj->initialised = 0;
  523. zval_ptr_dtor(return_value);
  524. RETURN_FALSE;
  525. }
  526. }
  527. /* }}} */
  528. static void sqlite_value_to_zval(sqlite3_stmt *stmt, int column, zval *data) /* {{{ */
  529. {
  530. sqlite3_int64 val;
  531. switch (sqlite3_column_type(stmt, column)) {
  532. case SQLITE_INTEGER:
  533. val = sqlite3_column_int64(stmt, column);
  534. #if LONG_MAX <= 2147483647
  535. if (val > ZEND_LONG_MAX || val < ZEND_LONG_MIN) {
  536. ZVAL_STRINGL(data, (char *)sqlite3_column_text(stmt, column), sqlite3_column_bytes(stmt, column));
  537. } else {
  538. #endif
  539. ZVAL_LONG(data, (zend_long) val);
  540. #if LONG_MAX <= 2147483647
  541. }
  542. #endif
  543. break;
  544. case SQLITE_FLOAT:
  545. ZVAL_DOUBLE(data, sqlite3_column_double(stmt, column));
  546. break;
  547. case SQLITE_NULL:
  548. ZVAL_NULL(data);
  549. break;
  550. case SQLITE3_TEXT:
  551. ZVAL_STRING(data, (char*)sqlite3_column_text(stmt, column));
  552. break;
  553. case SQLITE_BLOB:
  554. default:
  555. ZVAL_STRINGL(data, (char*)sqlite3_column_blob(stmt, column), sqlite3_column_bytes(stmt, column));
  556. }
  557. }
  558. /* }}} */
  559. /* {{{ proto SQLite3Result SQLite3::querySingle(String Query [, bool entire_row = false])
  560. Returns a string of the first column, or an array of the entire row. */
  561. PHP_METHOD(SQLite3, querySingle)
  562. {
  563. php_sqlite3_db_object *db_obj;
  564. zval *object = ZEND_THIS;
  565. zend_string *sql;
  566. char *errtext = NULL;
  567. int return_code;
  568. zend_bool entire_row = 0;
  569. sqlite3_stmt *stmt;
  570. db_obj = Z_SQLITE3_DB_P(object);
  571. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "S|b", &sql, &entire_row)) {
  572. RETURN_THROWS();
  573. }
  574. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  575. if (!ZSTR_LEN(sql)) {
  576. RETURN_FALSE;
  577. }
  578. /* If there was no return value then just execute the query */
  579. if (!USED_RET()) {
  580. if (sqlite3_exec(db_obj->db, ZSTR_VAL(sql), NULL, NULL, &errtext) != SQLITE_OK) {
  581. php_sqlite3_error(db_obj, "%s", errtext);
  582. sqlite3_free(errtext);
  583. }
  584. return;
  585. }
  586. return_code = sqlite3_prepare_v2(db_obj->db, ZSTR_VAL(sql), ZSTR_LEN(sql), &stmt, NULL);
  587. if (return_code != SQLITE_OK) {
  588. php_sqlite3_error(db_obj, "Unable to prepare statement: %d, %s", return_code, sqlite3_errmsg(db_obj->db));
  589. RETURN_FALSE;
  590. }
  591. return_code = sqlite3_step(stmt);
  592. switch (return_code) {
  593. case SQLITE_ROW: /* Valid Row */
  594. {
  595. if (!entire_row) {
  596. sqlite_value_to_zval(stmt, 0, return_value);
  597. } else {
  598. int i = 0;
  599. array_init(return_value);
  600. for (i = 0; i < sqlite3_data_count(stmt); i++) {
  601. zval data;
  602. sqlite_value_to_zval(stmt, i, &data);
  603. add_assoc_zval(return_value, (char*)sqlite3_column_name(stmt, i), &data);
  604. }
  605. }
  606. break;
  607. }
  608. case SQLITE_DONE: /* Valid but no results */
  609. {
  610. if (!entire_row) {
  611. RETVAL_NULL();
  612. } else {
  613. RETVAL_EMPTY_ARRAY();
  614. }
  615. break;
  616. }
  617. default:
  618. if (!EG(exception)) {
  619. php_sqlite3_error(db_obj, "Unable to execute statement: %s", sqlite3_errmsg(db_obj->db));
  620. }
  621. RETVAL_FALSE;
  622. }
  623. sqlite3_finalize(stmt);
  624. }
  625. /* }}} */
  626. static int sqlite3_do_callback(struct php_sqlite3_fci *fc, zval *cb, int argc, sqlite3_value **argv, sqlite3_context *context, int is_agg) /* {{{ */
  627. {
  628. zval *zargs = NULL;
  629. zval retval;
  630. int i;
  631. int ret;
  632. int fake_argc;
  633. php_sqlite3_agg_context *agg_context = NULL;
  634. if (is_agg) {
  635. is_agg = 2;
  636. }
  637. fake_argc = argc + is_agg;
  638. fc->fci.size = sizeof(fc->fci);
  639. ZVAL_COPY_VALUE(&fc->fci.function_name, cb);
  640. fc->fci.object = NULL;
  641. fc->fci.retval = &retval;
  642. fc->fci.param_count = fake_argc;
  643. /* build up the params */
  644. if (fake_argc) {
  645. zargs = (zval *)safe_emalloc(fake_argc, sizeof(zval), 0);
  646. }
  647. if (is_agg) {
  648. /* summon the aggregation context */
  649. agg_context = (php_sqlite3_agg_context *)sqlite3_aggregate_context(context, sizeof(php_sqlite3_agg_context));
  650. if (Z_ISUNDEF(agg_context->zval_context)) {
  651. ZVAL_NULL(&agg_context->zval_context);
  652. }
  653. ZVAL_COPY(&zargs[0], &agg_context->zval_context);
  654. ZVAL_LONG(&zargs[1], agg_context->row_count);
  655. }
  656. for (i = 0; i < argc; i++) {
  657. switch (sqlite3_value_type(argv[i])) {
  658. case SQLITE_INTEGER:
  659. #if ZEND_LONG_MAX > 2147483647
  660. ZVAL_LONG(&zargs[i + is_agg], sqlite3_value_int64(argv[i]));
  661. #else
  662. ZVAL_LONG(&zargs[i + is_agg], sqlite3_value_int(argv[i]));
  663. #endif
  664. break;
  665. case SQLITE_FLOAT:
  666. ZVAL_DOUBLE(&zargs[i + is_agg], sqlite3_value_double(argv[i]));
  667. break;
  668. case SQLITE_NULL:
  669. ZVAL_NULL(&zargs[i + is_agg]);
  670. break;
  671. case SQLITE_BLOB:
  672. case SQLITE3_TEXT:
  673. default:
  674. ZVAL_STRINGL(&zargs[i + is_agg], (char*)sqlite3_value_text(argv[i]), sqlite3_value_bytes(argv[i]));
  675. break;
  676. }
  677. }
  678. fc->fci.params = zargs;
  679. if ((ret = zend_call_function(&fc->fci, &fc->fcc)) == FAILURE) {
  680. php_error_docref(NULL, E_WARNING, "An error occurred while invoking the callback");
  681. }
  682. if (is_agg) {
  683. zval_ptr_dtor(&zargs[0]);
  684. }
  685. /* clean up the params */
  686. if (fake_argc) {
  687. for (i = is_agg; i < argc + is_agg; i++) {
  688. zval_ptr_dtor(&zargs[i]);
  689. }
  690. if (is_agg) {
  691. zval_ptr_dtor(&zargs[1]);
  692. }
  693. efree(zargs);
  694. }
  695. if (!is_agg || !argv) {
  696. /* only set the sqlite return value if we are a scalar function,
  697. * or if we are finalizing an aggregate */
  698. if (!Z_ISUNDEF(retval)) {
  699. switch (Z_TYPE(retval)) {
  700. case IS_LONG:
  701. #if ZEND_LONG_MAX > 2147483647
  702. sqlite3_result_int64(context, Z_LVAL(retval));
  703. #else
  704. sqlite3_result_int(context, Z_LVAL(retval));
  705. #endif
  706. break;
  707. case IS_NULL:
  708. sqlite3_result_null(context);
  709. break;
  710. case IS_DOUBLE:
  711. sqlite3_result_double(context, Z_DVAL(retval));
  712. break;
  713. default: {
  714. zend_string *str = zval_try_get_string(&retval);
  715. if (UNEXPECTED(!str)) {
  716. ret = FAILURE;
  717. break;
  718. }
  719. sqlite3_result_text(context, ZSTR_VAL(str), ZSTR_LEN(str), SQLITE_TRANSIENT);
  720. zend_string_release(str);
  721. break;
  722. }
  723. }
  724. } else {
  725. sqlite3_result_error(context, "failed to invoke callback", 0);
  726. }
  727. if (agg_context && !Z_ISUNDEF(agg_context->zval_context)) {
  728. zval_ptr_dtor(&agg_context->zval_context);
  729. }
  730. } else {
  731. /* we're stepping in an aggregate; the return value goes into
  732. * the context */
  733. if (agg_context && !Z_ISUNDEF(agg_context->zval_context)) {
  734. zval_ptr_dtor(&agg_context->zval_context);
  735. }
  736. ZVAL_COPY_VALUE(&agg_context->zval_context, &retval);
  737. ZVAL_UNDEF(&retval);
  738. }
  739. if (!Z_ISUNDEF(retval)) {
  740. zval_ptr_dtor(&retval);
  741. }
  742. return ret;
  743. }
  744. /* }}}*/
  745. static void php_sqlite3_callback_func(sqlite3_context *context, int argc, sqlite3_value **argv) /* {{{ */
  746. {
  747. php_sqlite3_func *func = (php_sqlite3_func *)sqlite3_user_data(context);
  748. sqlite3_do_callback(&func->afunc, &func->func, argc, argv, context, 0);
  749. }
  750. /* }}}*/
  751. static void php_sqlite3_callback_step(sqlite3_context *context, int argc, sqlite3_value **argv) /* {{{ */
  752. {
  753. php_sqlite3_func *func = (php_sqlite3_func *)sqlite3_user_data(context);
  754. php_sqlite3_agg_context *agg_context = (php_sqlite3_agg_context *)sqlite3_aggregate_context(context, sizeof(php_sqlite3_agg_context));
  755. agg_context->row_count++;
  756. sqlite3_do_callback(&func->astep, &func->step, argc, argv, context, 1);
  757. }
  758. /* }}} */
  759. static void php_sqlite3_callback_final(sqlite3_context *context) /* {{{ */
  760. {
  761. php_sqlite3_func *func = (php_sqlite3_func *)sqlite3_user_data(context);
  762. php_sqlite3_agg_context *agg_context = (php_sqlite3_agg_context *)sqlite3_aggregate_context(context, sizeof(php_sqlite3_agg_context));
  763. agg_context->row_count = 0;
  764. sqlite3_do_callback(&func->afini, &func->fini, 0, NULL, context, 1);
  765. }
  766. /* }}} */
  767. static int php_sqlite3_callback_compare(void *coll, int a_len, const void *a, int b_len, const void* b) /* {{{ */
  768. {
  769. php_sqlite3_collation *collation = (php_sqlite3_collation*)coll;
  770. zval zargs[2];
  771. zval retval;
  772. int ret;
  773. // Exception occurred on previous callback. Don't attempt to call function.
  774. if (EG(exception)) {
  775. return 0;
  776. }
  777. collation->fci.fci.size = (sizeof(collation->fci.fci));
  778. ZVAL_COPY_VALUE(&collation->fci.fci.function_name, &collation->cmp_func);
  779. collation->fci.fci.object = NULL;
  780. collation->fci.fci.retval = &retval;
  781. collation->fci.fci.param_count = 2;
  782. ZVAL_STRINGL(&zargs[0], a, a_len);
  783. ZVAL_STRINGL(&zargs[1], b, b_len);
  784. collation->fci.fci.params = zargs;
  785. if ((ret = zend_call_function(&collation->fci.fci, &collation->fci.fcc)) == FAILURE) {
  786. php_error_docref(NULL, E_WARNING, "An error occurred while invoking the compare callback");
  787. }
  788. zval_ptr_dtor(&zargs[0]);
  789. zval_ptr_dtor(&zargs[1]);
  790. if (EG(exception)) {
  791. ret = 0;
  792. } else if (Z_TYPE(retval) != IS_LONG){
  793. //retval ought to contain a ZVAL_LONG by now
  794. // (the result of a comparison, i.e. most likely -1, 0, or 1)
  795. //I suppose we could accept any scalar return type, though.
  796. php_error_docref(NULL, E_WARNING, "An error occurred while invoking the compare callback (invalid return type). Collation behaviour is undefined.");
  797. } else {
  798. ret = Z_LVAL(retval);
  799. }
  800. zval_ptr_dtor(&retval);
  801. return ret;
  802. }
  803. /* }}} */
  804. /* {{{ proto bool SQLite3::createFunction(string name, mixed callback [, int argcount, int flags])
  805. Allows registration of a PHP function as a SQLite UDF that can be called within SQL statements. */
  806. PHP_METHOD(SQLite3, createFunction)
  807. {
  808. php_sqlite3_db_object *db_obj;
  809. zval *object = ZEND_THIS;
  810. php_sqlite3_func *func;
  811. char *sql_func;
  812. size_t sql_func_len;
  813. zval *callback_func;
  814. zend_long sql_func_num_args = -1;
  815. zend_long flags = 0;
  816. db_obj = Z_SQLITE3_DB_P(object);
  817. if (zend_parse_parameters(ZEND_NUM_ARGS(), "sz|ll", &sql_func, &sql_func_len, &callback_func, &sql_func_num_args, &flags) == FAILURE) {
  818. RETURN_THROWS();
  819. }
  820. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  821. if (!sql_func_len) {
  822. RETURN_FALSE;
  823. }
  824. if (!zend_is_callable(callback_func, 0, NULL)) {
  825. zend_string *callback_name = zend_get_callable_name(callback_func);
  826. php_sqlite3_error(db_obj, "Not a valid callback function %s", ZSTR_VAL(callback_name));
  827. zend_string_release_ex(callback_name, 0);
  828. RETURN_FALSE;
  829. }
  830. func = (php_sqlite3_func *)ecalloc(1, sizeof(*func));
  831. if (sqlite3_create_function(db_obj->db, sql_func, sql_func_num_args, flags | SQLITE_UTF8, func, php_sqlite3_callback_func, NULL, NULL) == SQLITE_OK) {
  832. func->func_name = estrdup(sql_func);
  833. ZVAL_COPY(&func->func, callback_func);
  834. func->argc = sql_func_num_args;
  835. func->next = db_obj->funcs;
  836. db_obj->funcs = func;
  837. RETURN_TRUE;
  838. }
  839. efree(func);
  840. RETURN_FALSE;
  841. }
  842. /* }}} */
  843. /* {{{ proto bool SQLite3::createAggregate(string name, mixed step, mixed final [, int argcount])
  844. Allows registration of a PHP function for use as an aggregate. */
  845. PHP_METHOD(SQLite3, createAggregate)
  846. {
  847. php_sqlite3_db_object *db_obj;
  848. zval *object = ZEND_THIS;
  849. php_sqlite3_func *func;
  850. char *sql_func;
  851. size_t sql_func_len;
  852. zval *step_callback, *fini_callback;
  853. zend_long sql_func_num_args = -1;
  854. db_obj = Z_SQLITE3_DB_P(object);
  855. if (zend_parse_parameters(ZEND_NUM_ARGS(), "szz|l", &sql_func, &sql_func_len, &step_callback, &fini_callback, &sql_func_num_args) == FAILURE) {
  856. RETURN_THROWS();
  857. }
  858. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  859. if (!sql_func_len) {
  860. RETURN_FALSE;
  861. }
  862. if (!zend_is_callable(step_callback, 0, NULL)) {
  863. zend_string *callback_name = zend_get_callable_name(step_callback);
  864. php_sqlite3_error(db_obj, "Not a valid callback function %s", ZSTR_VAL(callback_name));
  865. zend_string_release_ex(callback_name, 0);
  866. RETURN_FALSE;
  867. }
  868. if (!zend_is_callable(fini_callback, 0, NULL)) {
  869. zend_string *callback_name = zend_get_callable_name(fini_callback);
  870. php_sqlite3_error(db_obj, "Not a valid callback function %s", ZSTR_VAL(callback_name));
  871. zend_string_release_ex(callback_name, 0);
  872. RETURN_FALSE;
  873. }
  874. func = (php_sqlite3_func *)ecalloc(1, sizeof(*func));
  875. if (sqlite3_create_function(db_obj->db, sql_func, sql_func_num_args, SQLITE_UTF8, func, NULL, php_sqlite3_callback_step, php_sqlite3_callback_final) == SQLITE_OK) {
  876. func->func_name = estrdup(sql_func);
  877. ZVAL_COPY(&func->step, step_callback);
  878. ZVAL_COPY(&func->fini, fini_callback);
  879. func->argc = sql_func_num_args;
  880. func->next = db_obj->funcs;
  881. db_obj->funcs = func;
  882. RETURN_TRUE;
  883. }
  884. efree(func);
  885. RETURN_FALSE;
  886. }
  887. /* }}} */
  888. /* {{{ proto bool SQLite3::createCollation(string name, mixed callback)
  889. Registers a PHP function as a comparator that can be used with the SQL COLLATE operator. Callback must accept two strings and return an integer (as strcmp()). */
  890. PHP_METHOD(SQLite3, createCollation)
  891. {
  892. php_sqlite3_db_object *db_obj;
  893. zval *object = ZEND_THIS;
  894. php_sqlite3_collation *collation;
  895. char *collation_name;
  896. size_t collation_name_len;
  897. zval *callback_func;
  898. db_obj = Z_SQLITE3_DB_P(object);
  899. if (zend_parse_parameters(ZEND_NUM_ARGS(), "sz", &collation_name, &collation_name_len, &callback_func) == FAILURE) {
  900. RETURN_THROWS();
  901. }
  902. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  903. if (!collation_name_len) {
  904. RETURN_FALSE;
  905. }
  906. if (!zend_is_callable(callback_func, 0, NULL)) {
  907. zend_string *callback_name = zend_get_callable_name(callback_func);
  908. php_sqlite3_error(db_obj, "Not a valid callback function %s", ZSTR_VAL(callback_name));
  909. zend_string_release_ex(callback_name, 0);
  910. RETURN_FALSE;
  911. }
  912. collation = (php_sqlite3_collation *)ecalloc(1, sizeof(*collation));
  913. if (sqlite3_create_collation(db_obj->db, collation_name, SQLITE_UTF8, collation, php_sqlite3_callback_compare) == SQLITE_OK) {
  914. collation->collation_name = estrdup(collation_name);
  915. ZVAL_COPY(&collation->cmp_func, callback_func);
  916. collation->next = db_obj->collations;
  917. db_obj->collations = collation;
  918. RETURN_TRUE;
  919. }
  920. efree(collation);
  921. RETURN_FALSE;
  922. }
  923. /* }}} */
  924. typedef struct {
  925. sqlite3_blob *blob;
  926. size_t position;
  927. size_t size;
  928. int flags;
  929. } php_stream_sqlite3_data;
  930. static ssize_t php_sqlite3_stream_write(php_stream *stream, const char *buf, size_t count)
  931. {
  932. php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract;
  933. if (sqlite3_stream->flags & SQLITE_OPEN_READONLY) {
  934. php_error_docref(NULL, E_WARNING, "Can't write to blob stream: is open as read only");
  935. return -1;
  936. }
  937. if (sqlite3_stream->position + count > sqlite3_stream->size) {
  938. php_error_docref(NULL, E_WARNING, "It is not possible to increase the size of a BLOB");
  939. return -1;
  940. }
  941. if (sqlite3_blob_write(sqlite3_stream->blob, buf, count, sqlite3_stream->position) != SQLITE_OK) {
  942. return -1;
  943. }
  944. if (sqlite3_stream->position + count >= sqlite3_stream->size) {
  945. stream->eof = 1;
  946. sqlite3_stream->position = sqlite3_stream->size;
  947. }
  948. else {
  949. sqlite3_stream->position += count;
  950. }
  951. return count;
  952. }
  953. static ssize_t php_sqlite3_stream_read(php_stream *stream, char *buf, size_t count)
  954. {
  955. php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract;
  956. if (sqlite3_stream->position + count >= sqlite3_stream->size) {
  957. count = sqlite3_stream->size - sqlite3_stream->position;
  958. stream->eof = 1;
  959. }
  960. if (count) {
  961. if (sqlite3_blob_read(sqlite3_stream->blob, buf, count, sqlite3_stream->position) != SQLITE_OK) {
  962. return -1;
  963. }
  964. sqlite3_stream->position += count;
  965. }
  966. return count;
  967. }
  968. static int php_sqlite3_stream_close(php_stream *stream, int close_handle)
  969. {
  970. php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract;
  971. if (sqlite3_blob_close(sqlite3_stream->blob) != SQLITE_OK) {
  972. /* Error occurred, but it still closed */
  973. }
  974. efree(sqlite3_stream);
  975. return 0;
  976. }
  977. static int php_sqlite3_stream_flush(php_stream *stream)
  978. {
  979. /* do nothing */
  980. return 0;
  981. }
  982. /* {{{ */
  983. static int php_sqlite3_stream_seek(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffs)
  984. {
  985. php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract;
  986. switch(whence) {
  987. case SEEK_CUR:
  988. if (offset < 0) {
  989. if (sqlite3_stream->position < (size_t)(-offset)) {
  990. sqlite3_stream->position = 0;
  991. *newoffs = -1;
  992. return -1;
  993. } else {
  994. sqlite3_stream->position = sqlite3_stream->position + offset;
  995. *newoffs = sqlite3_stream->position;
  996. stream->eof = 0;
  997. return 0;
  998. }
  999. } else {
  1000. if (sqlite3_stream->position + (size_t)(offset) > sqlite3_stream->size) {
  1001. sqlite3_stream->position = sqlite3_stream->size;
  1002. *newoffs = -1;
  1003. return -1;
  1004. } else {
  1005. sqlite3_stream->position = sqlite3_stream->position + offset;
  1006. *newoffs = sqlite3_stream->position;
  1007. stream->eof = 0;
  1008. return 0;
  1009. }
  1010. }
  1011. case SEEK_SET:
  1012. if (sqlite3_stream->size < (size_t)(offset)) {
  1013. sqlite3_stream->position = sqlite3_stream->size;
  1014. *newoffs = -1;
  1015. return -1;
  1016. } else {
  1017. sqlite3_stream->position = offset;
  1018. *newoffs = sqlite3_stream->position;
  1019. stream->eof = 0;
  1020. return 0;
  1021. }
  1022. case SEEK_END:
  1023. if (offset > 0) {
  1024. sqlite3_stream->position = sqlite3_stream->size;
  1025. *newoffs = -1;
  1026. return -1;
  1027. } else if (sqlite3_stream->size < (size_t)(-offset)) {
  1028. sqlite3_stream->position = 0;
  1029. *newoffs = -1;
  1030. return -1;
  1031. } else {
  1032. sqlite3_stream->position = sqlite3_stream->size + offset;
  1033. *newoffs = sqlite3_stream->position;
  1034. stream->eof = 0;
  1035. return 0;
  1036. }
  1037. default:
  1038. *newoffs = sqlite3_stream->position;
  1039. return -1;
  1040. }
  1041. }
  1042. /* }}} */
  1043. static int php_sqlite3_stream_cast(php_stream *stream, int castas, void **ret)
  1044. {
  1045. return FAILURE;
  1046. }
  1047. static int php_sqlite3_stream_stat(php_stream *stream, php_stream_statbuf *ssb)
  1048. {
  1049. php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract;
  1050. ssb->sb.st_size = sqlite3_stream->size;
  1051. return 0;
  1052. }
  1053. static const php_stream_ops php_stream_sqlite3_ops = {
  1054. php_sqlite3_stream_write,
  1055. php_sqlite3_stream_read,
  1056. php_sqlite3_stream_close,
  1057. php_sqlite3_stream_flush,
  1058. "SQLite3",
  1059. php_sqlite3_stream_seek,
  1060. php_sqlite3_stream_cast,
  1061. php_sqlite3_stream_stat,
  1062. NULL
  1063. };
  1064. /* {{{ proto resource SQLite3::openBlob(string table, string column, int rowid [, string dbname [, int flags]])
  1065. Open a blob as a stream which we can read / write to. */
  1066. PHP_METHOD(SQLite3, openBlob)
  1067. {
  1068. php_sqlite3_db_object *db_obj;
  1069. zval *object = ZEND_THIS;
  1070. char *table, *column, *dbname = "main", *mode = "rb";
  1071. size_t table_len, column_len, dbname_len;
  1072. zend_long rowid, flags = SQLITE_OPEN_READONLY, sqlite_flags = 0;
  1073. sqlite3_blob *blob = NULL;
  1074. php_stream_sqlite3_data *sqlite3_stream;
  1075. php_stream *stream;
  1076. db_obj = Z_SQLITE3_DB_P(object);
  1077. if (zend_parse_parameters(ZEND_NUM_ARGS(), "ssl|sl", &table, &table_len, &column, &column_len, &rowid, &dbname, &dbname_len, &flags) == FAILURE) {
  1078. RETURN_THROWS();
  1079. }
  1080. if (ZEND_NUM_ARGS() >= 4 && CHECK_NULL_PATH(dbname, dbname_len)) {
  1081. zend_argument_type_error(4, "must not contain null bytes");
  1082. RETURN_THROWS();
  1083. }
  1084. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  1085. sqlite_flags = (flags & SQLITE_OPEN_READWRITE) ? 1 : 0;
  1086. if (sqlite3_blob_open(db_obj->db, dbname, table, column, rowid, sqlite_flags, &blob) != SQLITE_OK) {
  1087. php_sqlite3_error(db_obj, "Unable to open blob: %s", sqlite3_errmsg(db_obj->db));
  1088. RETURN_FALSE;
  1089. }
  1090. sqlite3_stream = emalloc(sizeof(php_stream_sqlite3_data));
  1091. sqlite3_stream->blob = blob;
  1092. sqlite3_stream->flags = flags;
  1093. sqlite3_stream->position = 0;
  1094. sqlite3_stream->size = sqlite3_blob_bytes(blob);
  1095. if (sqlite_flags != 0) {
  1096. mode = "r+b";
  1097. }
  1098. stream = php_stream_alloc(&php_stream_sqlite3_ops, sqlite3_stream, 0, mode);
  1099. if (stream) {
  1100. php_stream_to_zval(stream, return_value);
  1101. } else {
  1102. RETURN_FALSE;
  1103. }
  1104. }
  1105. /* }}} */
  1106. /* {{{ proto bool SQLite3::enableExceptions([bool enableExceptions = false])
  1107. Enables an exception error mode. */
  1108. PHP_METHOD(SQLite3, enableExceptions)
  1109. {
  1110. php_sqlite3_db_object *db_obj;
  1111. zval *object = ZEND_THIS;
  1112. zend_bool enableExceptions = 0;
  1113. db_obj = Z_SQLITE3_DB_P(object);
  1114. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &enableExceptions) == FAILURE) {
  1115. RETURN_THROWS();
  1116. }
  1117. RETVAL_BOOL(db_obj->exception);
  1118. db_obj->exception = enableExceptions;
  1119. }
  1120. /* }}} */
  1121. /* {{{ proto bool SQLite3::setAuthorizer(mixed callback)
  1122. Register a callback function to be used as an authorizer by SQLite. The callback should return SQLite3::OK, SQLite3::IGNORE or SQLite3::DENY. */
  1123. PHP_METHOD(SQLite3, setAuthorizer)
  1124. {
  1125. php_sqlite3_db_object *db_obj;
  1126. zval *object = ZEND_THIS;
  1127. db_obj = Z_SQLITE3_DB_P(object);
  1128. zend_fcall_info fci;
  1129. zend_fcall_info_cache fcc;
  1130. ZEND_PARSE_PARAMETERS_START(1, 1)
  1131. Z_PARAM_FUNC_EX(fci, fcc, 1, 0)
  1132. ZEND_PARSE_PARAMETERS_END();
  1133. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  1134. /* Clear previously set callback */
  1135. if (ZEND_FCI_INITIALIZED(db_obj->authorizer_fci)) {
  1136. zval_ptr_dtor(&db_obj->authorizer_fci.function_name);
  1137. db_obj->authorizer_fci.size = 0;
  1138. }
  1139. /* Only enable userland authorizer if argument is not NULL */
  1140. if (ZEND_FCI_INITIALIZED(fci)) {
  1141. db_obj->authorizer_fci = fci;
  1142. Z_ADDREF(db_obj->authorizer_fci.function_name);
  1143. db_obj->authorizer_fcc = fcc;
  1144. }
  1145. RETURN_TRUE;
  1146. }
  1147. /* }}} */
  1148. #if SQLITE_VERSION_NUMBER >= 3006011
  1149. /* {{{ proto bool SQLite3::backup(SQLite3 destination_db[, string source_dbname = "main"[, string destination_dbname = "main"]])
  1150. Backups the current database to another one. */
  1151. PHP_METHOD(SQLite3, backup)
  1152. {
  1153. php_sqlite3_db_object *source_obj;
  1154. php_sqlite3_db_object *destination_obj;
  1155. char *source_dbname = "main", *destination_dbname = "main";
  1156. size_t source_dbname_length, destination_dbname_length;
  1157. zval *source_zval = ZEND_THIS;
  1158. zval *destination_zval;
  1159. sqlite3_backup *dbBackup;
  1160. int rc; // Return code
  1161. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|ss", &destination_zval, php_sqlite3_sc_entry, &source_dbname, &source_dbname_length, &destination_dbname, &destination_dbname_length) == FAILURE) {
  1162. RETURN_THROWS();
  1163. }
  1164. if (ZEND_NUM_ARGS() >= 2 && CHECK_NULL_PATH(source_dbname, source_dbname_length)) {
  1165. zend_argument_type_error(2, "must not contain null bytes");
  1166. RETURN_THROWS();
  1167. }
  1168. if (ZEND_NUM_ARGS() >= 3 && CHECK_NULL_PATH(destination_dbname, destination_dbname_length)) {
  1169. zend_argument_type_error(3, "must not contain null bytes");
  1170. RETURN_THROWS();
  1171. }
  1172. source_obj = Z_SQLITE3_DB_P(source_zval);
  1173. SQLITE3_CHECK_INITIALIZED(source_obj, source_obj->initialised, SQLite3)
  1174. destination_obj = Z_SQLITE3_DB_P(destination_zval);
  1175. SQLITE3_CHECK_INITIALIZED(destination_obj, destination_obj->initialised, SQLite3)
  1176. dbBackup = sqlite3_backup_init(destination_obj->db, destination_dbname, source_obj->db, source_dbname);
  1177. if (dbBackup) {
  1178. do {
  1179. rc = sqlite3_backup_step(dbBackup, -1);
  1180. } while (rc == SQLITE_OK);
  1181. /* Release resources allocated by backup_init(). */
  1182. rc = sqlite3_backup_finish(dbBackup);
  1183. }
  1184. else {
  1185. rc = sqlite3_errcode(source_obj->db);
  1186. }
  1187. if (rc != SQLITE_OK) {
  1188. if (rc == SQLITE_BUSY) {
  1189. php_sqlite3_error(source_obj, "Backup failed: source database is busy");
  1190. }
  1191. else if (rc == SQLITE_LOCKED) {
  1192. php_sqlite3_error(source_obj, "Backup failed: source database is locked");
  1193. }
  1194. else {
  1195. php_sqlite3_error(source_obj, "Backup failed: %d, %s", rc, sqlite3_errmsg(source_obj->db));
  1196. }
  1197. RETURN_FALSE;
  1198. }
  1199. RETURN_TRUE;
  1200. }
  1201. /* }}} */
  1202. #endif
  1203. /* {{{ proto int SQLite3Stmt::paramCount()
  1204. Returns the number of parameters within the prepared statement. */
  1205. PHP_METHOD(SQLite3Stmt, paramCount)
  1206. {
  1207. php_sqlite3_stmt *stmt_obj;
  1208. zval *object = ZEND_THIS;
  1209. stmt_obj = Z_SQLITE3_STMT_P(object);
  1210. if (zend_parse_parameters_none() == FAILURE) {
  1211. RETURN_THROWS();
  1212. }
  1213. SQLITE3_CHECK_INITIALIZED(stmt_obj->db_obj, stmt_obj->initialised, SQLite3);
  1214. SQLITE3_CHECK_INITIALIZED_STMT(stmt_obj->stmt, SQLite3Stmt);
  1215. RETURN_LONG(sqlite3_bind_parameter_count(stmt_obj->stmt));
  1216. }
  1217. /* }}} */
  1218. /* {{{ proto bool SQLite3Stmt::close()
  1219. Closes the prepared statement. */
  1220. PHP_METHOD(SQLite3Stmt, close)
  1221. {
  1222. php_sqlite3_stmt *stmt_obj;
  1223. zval *object = ZEND_THIS;
  1224. stmt_obj = Z_SQLITE3_STMT_P(object);
  1225. if (zend_parse_parameters_none() == FAILURE) {
  1226. RETURN_THROWS();
  1227. }
  1228. SQLITE3_CHECK_INITIALIZED(stmt_obj->db_obj, stmt_obj->initialised, SQLite3);
  1229. if(stmt_obj->db_obj) {
  1230. zend_llist_del_element(&(stmt_obj->db_obj->free_list), object, (int (*)(void *, void *)) php_sqlite3_compare_stmt_zval_free);
  1231. }
  1232. RETURN_TRUE;
  1233. }
  1234. /* }}} */
  1235. /* {{{ proto bool SQLite3Stmt::reset()
  1236. Reset the prepared statement to the state before it was executed, bindings still remain. */
  1237. PHP_METHOD(SQLite3Stmt, reset)
  1238. {
  1239. php_sqlite3_stmt *stmt_obj;
  1240. zval *object = ZEND_THIS;
  1241. stmt_obj = Z_SQLITE3_STMT_P(object);
  1242. if (zend_parse_parameters_none() == FAILURE) {
  1243. RETURN_THROWS();
  1244. }
  1245. SQLITE3_CHECK_INITIALIZED(stmt_obj->db_obj, stmt_obj->initialised, SQLite3);
  1246. SQLITE3_CHECK_INITIALIZED_STMT(stmt_obj->stmt, SQLite3Stmt);
  1247. if (sqlite3_reset(stmt_obj->stmt) != SQLITE_OK) {
  1248. php_sqlite3_error(stmt_obj->db_obj, "Unable to reset statement: %s", sqlite3_errmsg(sqlite3_db_handle(stmt_obj->stmt)));
  1249. RETURN_FALSE;
  1250. }
  1251. RETURN_TRUE;
  1252. }
  1253. /* }}} */
  1254. /* {{{ proto bool SQLite3Stmt::clear()
  1255. Clear all current bound parameters. */
  1256. PHP_METHOD(SQLite3Stmt, clear)
  1257. {
  1258. php_sqlite3_stmt *stmt_obj;
  1259. zval *object = ZEND_THIS;
  1260. stmt_obj = Z_SQLITE3_STMT_P(object);
  1261. if (zend_parse_parameters_none() == FAILURE) {
  1262. RETURN_THROWS();
  1263. }
  1264. SQLITE3_CHECK_INITIALIZED(stmt_obj->db_obj, stmt_obj->initialised, SQLite3);
  1265. SQLITE3_CHECK_INITIALIZED_STMT(stmt_obj->stmt, SQLite3Stmt);
  1266. if (sqlite3_clear_bindings(stmt_obj->stmt) != SQLITE_OK) {
  1267. php_sqlite3_error(stmt_obj->db_obj, "Unable to clear statement: %s", sqlite3_errmsg(sqlite3_db_handle(stmt_obj->stmt)));
  1268. RETURN_FALSE;
  1269. }
  1270. if (stmt_obj->bound_params) {
  1271. zend_hash_destroy(stmt_obj->bound_params);
  1272. FREE_HASHTABLE(stmt_obj->bound_params);
  1273. stmt_obj->bound_params = NULL;
  1274. }
  1275. RETURN_TRUE;
  1276. }
  1277. /* }}} */
  1278. /* {{{ proto bool SQLite3Stmt::readOnly()
  1279. Returns true if a statement is definitely read only */
  1280. PHP_METHOD(SQLite3Stmt, readOnly)
  1281. {
  1282. php_sqlite3_stmt *stmt_obj;
  1283. zval *object = ZEND_THIS;
  1284. stmt_obj = Z_SQLITE3_STMT_P(object);
  1285. if (zend_parse_parameters_none() == FAILURE) {
  1286. RETURN_THROWS();
  1287. }
  1288. SQLITE3_CHECK_INITIALIZED(stmt_obj->db_obj, stmt_obj->initialised, SQLite3);
  1289. SQLITE3_CHECK_INITIALIZED_STMT(stmt_obj->stmt, SQLite3Stmt);
  1290. if (sqlite3_stmt_readonly(stmt_obj->stmt)) {
  1291. RETURN_TRUE;
  1292. }
  1293. RETURN_FALSE;
  1294. }
  1295. /* }}} */
  1296. /* bind parameters to a statement before execution */
  1297. static int php_sqlite3_bind_params(php_sqlite3_stmt *stmt_obj) /* {{{ */
  1298. {
  1299. struct php_sqlite3_bound_param *param;
  1300. int return_code;
  1301. if (stmt_obj->bound_params) {
  1302. ZEND_HASH_FOREACH_PTR(stmt_obj->bound_params, param) {
  1303. zval *parameter;
  1304. /* parameter must be a reference? */
  1305. if (Z_ISREF(param->parameter)) {
  1306. parameter = Z_REFVAL(param->parameter);
  1307. } else {
  1308. parameter = &param->parameter;
  1309. }
  1310. /* If the ZVAL is null then it should be bound as that */
  1311. if (Z_TYPE_P(parameter) == IS_NULL) {
  1312. return_code = sqlite3_bind_null(stmt_obj->stmt, param->param_number);
  1313. if (return_code != SQLITE_OK) {
  1314. php_sqlite3_error(stmt_obj->db_obj, "Unable to bind parameter number " ZEND_LONG_FMT " (%d)", param->param_number, return_code);
  1315. }
  1316. continue;
  1317. }
  1318. switch (param->type) {
  1319. case SQLITE_INTEGER:
  1320. convert_to_long(parameter);
  1321. #if ZEND_LONG_MAX > 2147483647
  1322. return_code = sqlite3_bind_int64(stmt_obj->stmt, param->param_number, Z_LVAL_P(parameter));
  1323. #else
  1324. return_code = sqlite3_bind_int(stmt_obj->stmt, param->param_number, Z_LVAL_P(parameter));
  1325. #endif
  1326. if (return_code != SQLITE_OK) {
  1327. php_sqlite3_error(stmt_obj->db_obj, "Unable to bind parameter number " ZEND_LONG_FMT " (%d)", param->param_number, return_code);
  1328. }
  1329. break;
  1330. case SQLITE_FLOAT:
  1331. convert_to_double(parameter);
  1332. return_code = sqlite3_bind_double(stmt_obj->stmt, param->param_number, Z_DVAL_P(parameter));
  1333. if (return_code != SQLITE_OK) {
  1334. php_sqlite3_error(stmt_obj->db_obj, "Unable to bind parameter number " ZEND_LONG_FMT " (%d)", param->param_number, return_code);
  1335. }
  1336. break;
  1337. case SQLITE_BLOB:
  1338. {
  1339. php_stream *stream = NULL;
  1340. zend_string *buffer = NULL;
  1341. if (Z_TYPE_P(parameter) == IS_RESOURCE) {
  1342. php_stream_from_zval_no_verify(stream, parameter);
  1343. if (stream == NULL) {
  1344. php_sqlite3_error(stmt_obj->db_obj, "Unable to read stream for parameter %ld", param->param_number);
  1345. return FAILURE;
  1346. }
  1347. buffer = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0);
  1348. } else {
  1349. buffer = zval_get_string(parameter);
  1350. }
  1351. if (buffer) {
  1352. return_code = sqlite3_bind_blob(stmt_obj->stmt, param->param_number, ZSTR_VAL(buffer), ZSTR_LEN(buffer), SQLITE_TRANSIENT);
  1353. zend_string_release_ex(buffer, 0);
  1354. if (return_code != SQLITE_OK) {
  1355. php_sqlite3_error(stmt_obj->db_obj, "Unable to bind parameter number " ZEND_LONG_FMT " (%d)", param->param_number, return_code);
  1356. }
  1357. } else {
  1358. return_code = sqlite3_bind_null(stmt_obj->stmt, param->param_number);
  1359. if (return_code != SQLITE_OK) {
  1360. php_sqlite3_error(stmt_obj->db_obj, "Unable to bind parameter number " ZEND_LONG_FMT " (%d)", param->param_number, return_code);
  1361. }
  1362. }
  1363. break;
  1364. }
  1365. case SQLITE3_TEXT: {
  1366. zend_string *str = zval_try_get_string(parameter);
  1367. if (UNEXPECTED(!str)) {
  1368. return FAILURE;
  1369. }
  1370. return_code = sqlite3_bind_text(stmt_obj->stmt, param->param_number, ZSTR_VAL(str), ZSTR_LEN(str), SQLITE_TRANSIENT);
  1371. if (return_code != SQLITE_OK) {
  1372. php_sqlite3_error(stmt_obj->db_obj, "Unable to bind parameter number " ZEND_LONG_FMT " (%d)", param->param_number, return_code);
  1373. }
  1374. zend_string_release(str);
  1375. break;
  1376. }
  1377. case SQLITE_NULL:
  1378. return_code = sqlite3_bind_null(stmt_obj->stmt, param->param_number);
  1379. if (return_code != SQLITE_OK) {
  1380. php_sqlite3_error(stmt_obj->db_obj, "Unable to bind parameter number " ZEND_LONG_FMT " (%d)", param->param_number, return_code);
  1381. }
  1382. break;
  1383. default:
  1384. php_sqlite3_error(stmt_obj->db_obj, "Unknown parameter type: %pd for parameter %pd", param->type, param->param_number);
  1385. return FAILURE;
  1386. }
  1387. } ZEND_HASH_FOREACH_END();
  1388. }
  1389. return SUCCESS;
  1390. }
  1391. /* }}} */
  1392. /* {{{ proto string SQLite3Stmt::getSQL([expanded = false])
  1393. Returns the SQL statement used to prepare the query. If expanded is true, binded parameters and values will be expanded. */
  1394. PHP_METHOD(SQLite3Stmt, getSQL)
  1395. {
  1396. php_sqlite3_stmt *stmt_obj;
  1397. zend_bool expanded = 0;
  1398. zval *object = getThis();
  1399. stmt_obj = Z_SQLITE3_STMT_P(object);
  1400. int bind_rc;
  1401. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &expanded) == FAILURE) {
  1402. RETURN_THROWS();
  1403. }
  1404. SQLITE3_CHECK_INITIALIZED(stmt_obj->db_obj, stmt_obj->initialised, SQLite3);
  1405. SQLITE3_CHECK_INITIALIZED_STMT(stmt_obj->stmt, SQLite3Stmt);
  1406. bind_rc = php_sqlite3_bind_params(stmt_obj);
  1407. if (bind_rc == FAILURE || EG(exception)) {
  1408. RETURN_FALSE;
  1409. }
  1410. if (expanded) {
  1411. #if SQLITE_VERSION_NUMBER >= 3014000
  1412. char *sql = sqlite3_expanded_sql(stmt_obj->stmt);
  1413. RETVAL_STRING(sql);
  1414. sqlite3_free(sql);
  1415. #else
  1416. php_sqlite3_error(stmt_obj->db_obj, "The expanded parameter requires SQLite3 >= 3.14 and %s is installed", sqlite3_libversion());
  1417. RETURN_FALSE;
  1418. #endif
  1419. } else {
  1420. const char *sql = sqlite3_sql(stmt_obj->stmt);
  1421. RETVAL_STRING(sql);
  1422. }
  1423. }
  1424. /* }}} */
  1425. static int register_bound_parameter_to_sqlite(struct php_sqlite3_bound_param *param, php_sqlite3_stmt *stmt) /* {{{ */
  1426. {
  1427. HashTable *hash;
  1428. hash = stmt->bound_params;
  1429. if (!hash) {
  1430. ALLOC_HASHTABLE(hash);
  1431. zend_hash_init(hash, 13, NULL, sqlite3_param_dtor, 0);
  1432. stmt->bound_params = hash;
  1433. }
  1434. /* We need a : prefix to resolve a name to a parameter number */
  1435. if (param->name) {
  1436. if (ZSTR_VAL(param->name)[0] != ':' && ZSTR_VAL(param->name)[0] != '@') {
  1437. /* pre-increment for character + 1 for null */
  1438. zend_string *temp = zend_string_alloc(ZSTR_LEN(param->name) + 1, 0);
  1439. ZSTR_VAL(temp)[0] = ':';
  1440. memmove(ZSTR_VAL(temp) + 1, ZSTR_VAL(param->name), ZSTR_LEN(param->name) + 1);
  1441. param->name = temp;
  1442. } else {
  1443. param->name = zend_string_copy(param->name);
  1444. }
  1445. /* do lookup*/
  1446. param->param_number = sqlite3_bind_parameter_index(stmt->stmt, ZSTR_VAL(param->name));
  1447. }
  1448. if (param->param_number < 1) {
  1449. if (param->name) {
  1450. zend_string_release_ex(param->name, 0);
  1451. }
  1452. return 0;
  1453. }
  1454. if (param->param_number >= 1) {
  1455. zend_hash_index_del(hash, param->param_number);
  1456. }
  1457. if (param->name) {
  1458. zend_hash_update_mem(hash, param->name, param, sizeof(struct php_sqlite3_bound_param));
  1459. } else {
  1460. zend_hash_index_update_mem(hash, param->param_number, param, sizeof(struct php_sqlite3_bound_param));
  1461. }
  1462. return 1;
  1463. }
  1464. /* }}} */
  1465. /* {{{ Best try to map between PHP and SQLite. Default is still text. */
  1466. #define PHP_SQLITE3_SET_TYPE(z, p) \
  1467. switch (Z_TYPE_P(z)) { \
  1468. default: \
  1469. (p).type = SQLITE_TEXT; \
  1470. break; \
  1471. case IS_LONG: \
  1472. case IS_TRUE: \
  1473. case IS_FALSE: \
  1474. (p).type = SQLITE_INTEGER; \
  1475. break; \
  1476. case IS_DOUBLE: \
  1477. (p).type = SQLITE_FLOAT; \
  1478. break; \
  1479. case IS_NULL: \
  1480. (p).type = SQLITE_NULL; \
  1481. break; \
  1482. }
  1483. /* }}} */
  1484. /* {{{ Common implementation of ::bindParam() and ::bindValue */
  1485. static void sqlite3stmt_bind(INTERNAL_FUNCTION_PARAMETERS)
  1486. {
  1487. php_sqlite3_stmt *stmt_obj;
  1488. zval *object = ZEND_THIS;
  1489. struct php_sqlite3_bound_param param = {0};
  1490. zval *parameter;
  1491. stmt_obj = Z_SQLITE3_STMT_P(object);
  1492. param.param_number = -1;
  1493. param.type = SQLITE3_TEXT;
  1494. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "lz|l", &param.param_number, &parameter, &param.type) == FAILURE) {
  1495. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sz|l", &param.name, &parameter, &param.type) == FAILURE) {
  1496. RETURN_THROWS();
  1497. }
  1498. }
  1499. SQLITE3_CHECK_INITIALIZED(stmt_obj->db_obj, stmt_obj->initialised, SQLite3);
  1500. SQLITE3_CHECK_INITIALIZED_STMT(stmt_obj->stmt, SQLite3Stmt);
  1501. ZVAL_COPY(&param.parameter, parameter);
  1502. if (ZEND_NUM_ARGS() < 3) {
  1503. PHP_SQLITE3_SET_TYPE(parameter, param);
  1504. }
  1505. if (!register_bound_parameter_to_sqlite(&param, stmt_obj)) {
  1506. if (!Z_ISUNDEF(param.parameter)) {
  1507. zval_ptr_dtor(&(param.parameter));
  1508. ZVAL_UNDEF(&param.parameter);
  1509. }
  1510. RETURN_FALSE;
  1511. }
  1512. RETURN_TRUE;
  1513. }
  1514. /* }}} */
  1515. /* {{{ proto bool SQLite3Stmt::bindParam(int parameter_number, mixed parameter [, int type])
  1516. Bind Parameter to a stmt variable. */
  1517. PHP_METHOD(SQLite3Stmt, bindParam)
  1518. {
  1519. sqlite3stmt_bind(INTERNAL_FUNCTION_PARAM_PASSTHRU);
  1520. }
  1521. /* }}} */
  1522. /* {{{ proto bool SQLite3Stmt::bindValue(int parameter_number, mixed parameter [, int type])
  1523. Bind Value of a parameter to a stmt variable. */
  1524. PHP_METHOD(SQLite3Stmt, bindValue)
  1525. {
  1526. sqlite3stmt_bind(INTERNAL_FUNCTION_PARAM_PASSTHRU);
  1527. }
  1528. /* }}} */
  1529. #undef PHP_SQLITE3_SET_TYPE
  1530. /* {{{ proto SQLite3Result SQLite3Stmt::execute()
  1531. Executes a prepared statement and returns a result set object. */
  1532. PHP_METHOD(SQLite3Stmt, execute)
  1533. {
  1534. php_sqlite3_stmt *stmt_obj;
  1535. php_sqlite3_result *result;
  1536. zval *object = ZEND_THIS;
  1537. int return_code = 0;
  1538. int bind_rc = 0;
  1539. stmt_obj = Z_SQLITE3_STMT_P(object);
  1540. if (zend_parse_parameters_none() == FAILURE) {
  1541. RETURN_THROWS();
  1542. }
  1543. SQLITE3_CHECK_INITIALIZED(stmt_obj->db_obj, stmt_obj->initialised, SQLite3);
  1544. /* Always reset statement before execution, see bug #77051 */
  1545. sqlite3_reset(stmt_obj->stmt);
  1546. /* Bind parameters to the statement */
  1547. bind_rc = php_sqlite3_bind_params(stmt_obj);
  1548. if (bind_rc == FAILURE || EG(exception)) {
  1549. RETURN_FALSE;
  1550. }
  1551. return_code = sqlite3_step(stmt_obj->stmt);
  1552. switch (return_code) {
  1553. case SQLITE_ROW: /* Valid Row */
  1554. case SQLITE_DONE: /* Valid but no results */
  1555. {
  1556. sqlite3_reset(stmt_obj->stmt);
  1557. object_init_ex(return_value, php_sqlite3_result_entry);
  1558. result = Z_SQLITE3_RESULT_P(return_value);
  1559. result->is_prepared_statement = 1;
  1560. result->db_obj = stmt_obj->db_obj;
  1561. result->stmt_obj = stmt_obj;
  1562. Z_ADDREF_P(object);
  1563. ZVAL_OBJ(&result->stmt_obj_zval, Z_OBJ_P(object));
  1564. break;
  1565. }
  1566. case SQLITE_ERROR:
  1567. sqlite3_reset(stmt_obj->stmt);
  1568. default:
  1569. if (!EG(exception)) {
  1570. php_sqlite3_error(stmt_obj->db_obj, "Unable to execute statement: %s", sqlite3_errmsg(sqlite3_db_handle(stmt_obj->stmt)));
  1571. }
  1572. zval_ptr_dtor(return_value);
  1573. RETURN_FALSE;
  1574. }
  1575. return;
  1576. }
  1577. /* }}} */
  1578. /* {{{ proto SQLite3Stmt::__construct(SQLite3 dbobject, String Statement)
  1579. __constructor for SQLite3Stmt. */
  1580. PHP_METHOD(SQLite3Stmt, __construct)
  1581. {
  1582. php_sqlite3_stmt *stmt_obj;
  1583. php_sqlite3_db_object *db_obj;
  1584. zval *object = ZEND_THIS;
  1585. zval *db_zval;
  1586. zend_string *sql;
  1587. int errcode;
  1588. zend_error_handling error_handling;
  1589. php_sqlite3_free_list *free_item;
  1590. stmt_obj = Z_SQLITE3_STMT_P(object);
  1591. if (zend_parse_parameters(ZEND_NUM_ARGS(), "OS", &db_zval, php_sqlite3_sc_entry, &sql) == FAILURE) {
  1592. RETURN_THROWS();
  1593. }
  1594. db_obj = Z_SQLITE3_DB_P(db_zval);
  1595. zend_replace_error_handling(EH_THROW, NULL, &error_handling);
  1596. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  1597. zend_restore_error_handling(&error_handling);
  1598. if (!ZSTR_LEN(sql)) {
  1599. RETURN_FALSE;
  1600. }
  1601. stmt_obj->db_obj = db_obj;
  1602. Z_ADDREF_P(db_zval);
  1603. ZVAL_OBJ(&stmt_obj->db_obj_zval, Z_OBJ_P(db_zval));
  1604. errcode = sqlite3_prepare_v2(db_obj->db, ZSTR_VAL(sql), ZSTR_LEN(sql), &(stmt_obj->stmt), NULL);
  1605. if (errcode != SQLITE_OK) {
  1606. php_sqlite3_error(db_obj, "Unable to prepare statement: %d, %s", errcode, sqlite3_errmsg(db_obj->db));
  1607. zval_ptr_dtor(return_value);
  1608. RETURN_FALSE;
  1609. }
  1610. stmt_obj->initialised = 1;
  1611. free_item = emalloc(sizeof(php_sqlite3_free_list));
  1612. free_item->stmt_obj = stmt_obj;
  1613. //?? free_item->stmt_obj_zval = ZEND_THIS;
  1614. ZVAL_OBJ(&free_item->stmt_obj_zval, Z_OBJ_P(object));
  1615. zend_llist_add_element(&(db_obj->free_list), &free_item);
  1616. }
  1617. /* }}} */
  1618. /* {{{ proto int SQLite3Result::numColumns()
  1619. Number of columns in the result set. */
  1620. PHP_METHOD(SQLite3Result, numColumns)
  1621. {
  1622. php_sqlite3_result *result_obj;
  1623. zval *object = ZEND_THIS;
  1624. result_obj = Z_SQLITE3_RESULT_P(object);
  1625. if (zend_parse_parameters_none() == FAILURE) {
  1626. RETURN_THROWS();
  1627. }
  1628. SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
  1629. RETURN_LONG(sqlite3_column_count(result_obj->stmt_obj->stmt));
  1630. }
  1631. /* }}} */
  1632. /* {{{ proto string SQLite3Result::columnName(int column)
  1633. Returns the name of the nth column. */
  1634. PHP_METHOD(SQLite3Result, columnName)
  1635. {
  1636. php_sqlite3_result *result_obj;
  1637. zval *object = ZEND_THIS;
  1638. zend_long column = 0;
  1639. char *column_name;
  1640. result_obj = Z_SQLITE3_RESULT_P(object);
  1641. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &column) == FAILURE) {
  1642. RETURN_THROWS();
  1643. }
  1644. SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
  1645. column_name = (char*) sqlite3_column_name(result_obj->stmt_obj->stmt, column);
  1646. if (column_name == NULL) {
  1647. RETURN_FALSE;
  1648. }
  1649. RETVAL_STRING(column_name);
  1650. }
  1651. /* }}} */
  1652. /* {{{ proto int SQLite3Result::columnType(int column)
  1653. Returns the type of the nth column. */
  1654. PHP_METHOD(SQLite3Result, columnType)
  1655. {
  1656. php_sqlite3_result *result_obj;
  1657. zval *object = ZEND_THIS;
  1658. zend_long column = 0;
  1659. result_obj = Z_SQLITE3_RESULT_P(object);
  1660. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &column) == FAILURE) {
  1661. RETURN_THROWS();
  1662. }
  1663. SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
  1664. if (!sqlite3_data_count(result_obj->stmt_obj->stmt)) {
  1665. RETURN_FALSE;
  1666. }
  1667. RETURN_LONG(sqlite3_column_type(result_obj->stmt_obj->stmt, column));
  1668. }
  1669. /* }}} */
  1670. /* {{{ proto array SQLite3Result::fetchArray([int mode])
  1671. Fetch a result row as both an associative or numerically indexed array or both. */
  1672. PHP_METHOD(SQLite3Result, fetchArray)
  1673. {
  1674. php_sqlite3_result *result_obj;
  1675. zval *object = ZEND_THIS;
  1676. int i, ret;
  1677. zend_long mode = PHP_SQLITE3_BOTH;
  1678. result_obj = Z_SQLITE3_RESULT_P(object);
  1679. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &mode) == FAILURE) {
  1680. RETURN_THROWS();
  1681. }
  1682. SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
  1683. ret = sqlite3_step(result_obj->stmt_obj->stmt);
  1684. switch (ret) {
  1685. case SQLITE_ROW:
  1686. /* If there was no return value then just skip fetching */
  1687. if (!USED_RET()) {
  1688. return;
  1689. }
  1690. array_init(return_value);
  1691. for (i = 0; i < sqlite3_data_count(result_obj->stmt_obj->stmt); i++) {
  1692. zval data;
  1693. sqlite_value_to_zval(result_obj->stmt_obj->stmt, i, &data);
  1694. if (mode & PHP_SQLITE3_NUM) {
  1695. add_index_zval(return_value, i, &data);
  1696. }
  1697. if (mode & PHP_SQLITE3_ASSOC) {
  1698. if (mode & PHP_SQLITE3_NUM) {
  1699. if (Z_REFCOUNTED(data)) {
  1700. Z_ADDREF(data);
  1701. }
  1702. }
  1703. add_assoc_zval(return_value, (char*)sqlite3_column_name(result_obj->stmt_obj->stmt, i), &data);
  1704. }
  1705. }
  1706. break;
  1707. case SQLITE_DONE:
  1708. RETURN_FALSE;
  1709. break;
  1710. default:
  1711. php_sqlite3_error(result_obj->db_obj, "Unable to execute statement: %s", sqlite3_errmsg(sqlite3_db_handle(result_obj->stmt_obj->stmt)));
  1712. }
  1713. }
  1714. /* }}} */
  1715. /* {{{ proto bool SQLite3Result::reset()
  1716. Resets the result set back to the first row. */
  1717. PHP_METHOD(SQLite3Result, reset)
  1718. {
  1719. php_sqlite3_result *result_obj;
  1720. zval *object = ZEND_THIS;
  1721. result_obj = Z_SQLITE3_RESULT_P(object);
  1722. if (zend_parse_parameters_none() == FAILURE) {
  1723. RETURN_THROWS();
  1724. }
  1725. SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
  1726. if (sqlite3_reset(result_obj->stmt_obj->stmt) != SQLITE_OK) {
  1727. RETURN_FALSE;
  1728. }
  1729. RETURN_TRUE;
  1730. }
  1731. /* }}} */
  1732. /* {{{ proto bool SQLite3Result::finalize()
  1733. Closes the result set. */
  1734. PHP_METHOD(SQLite3Result, finalize)
  1735. {
  1736. php_sqlite3_result *result_obj;
  1737. zval *object = ZEND_THIS;
  1738. result_obj = Z_SQLITE3_RESULT_P(object);
  1739. if (zend_parse_parameters_none() == FAILURE) {
  1740. RETURN_THROWS();
  1741. }
  1742. SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
  1743. /* We need to finalize an internal statement */
  1744. if (result_obj->is_prepared_statement == 0) {
  1745. zend_llist_del_element(&(result_obj->db_obj->free_list), &result_obj->stmt_obj_zval,
  1746. (int (*)(void *, void *)) php_sqlite3_compare_stmt_zval_free);
  1747. } else {
  1748. sqlite3_reset(result_obj->stmt_obj->stmt);
  1749. }
  1750. RETURN_TRUE;
  1751. }
  1752. /* }}} */
  1753. /* {{{ proto SQLite3Result::__construct()
  1754. __constructor for SQLite3Result. */
  1755. PHP_METHOD(SQLite3Result, __construct)
  1756. {
  1757. zend_throw_exception(zend_ce_exception, "SQLite3Result cannot be directly instantiated", 0);
  1758. }
  1759. /* }}} */
  1760. /* {{{ Authorization Callback
  1761. */
  1762. static int php_sqlite3_authorizer(void *autharg, int action, const char *arg1, const char *arg2, const char *arg3, const char *arg4)
  1763. {
  1764. /* Check open_basedir restrictions first */
  1765. if (PG(open_basedir) && *PG(open_basedir)) {
  1766. if (action == SQLITE_ATTACH) {
  1767. if (memcmp(arg1, ":memory:", sizeof(":memory:")) && *arg1) {
  1768. if (strncmp(arg1, "file:", 5) == 0) {
  1769. /* starts with "file:" */
  1770. if (!arg1[5]) {
  1771. return SQLITE_DENY;
  1772. }
  1773. if (php_check_open_basedir(arg1 + 5)) {
  1774. return SQLITE_DENY;
  1775. }
  1776. }
  1777. if (php_check_open_basedir(arg1)) {
  1778. return SQLITE_DENY;
  1779. }
  1780. }
  1781. }
  1782. }
  1783. php_sqlite3_db_object *db_obj = (php_sqlite3_db_object *)autharg;
  1784. zend_fcall_info *fci = &db_obj->authorizer_fci;
  1785. /* fallback to access allowed if authorizer callback is not defined */
  1786. if (fci->size == 0) {
  1787. return SQLITE_OK;
  1788. }
  1789. /* call userland authorizer callback, if set */
  1790. zval retval;
  1791. zval argv[5];
  1792. ZVAL_LONG(&argv[0], action);
  1793. if (NULL == arg1) {
  1794. ZVAL_NULL(&argv[1]);
  1795. } else {
  1796. ZVAL_STRING(&argv[1], arg1);
  1797. }
  1798. if (NULL == arg2) {
  1799. ZVAL_NULL(&argv[2]);
  1800. } else {
  1801. ZVAL_STRING(&argv[2], arg2);
  1802. }
  1803. if (NULL == arg3) {
  1804. ZVAL_NULL(&argv[3]);
  1805. } else {
  1806. ZVAL_STRING(&argv[3], arg3);
  1807. }
  1808. if (NULL == arg4) {
  1809. ZVAL_NULL(&argv[4]);
  1810. } else {
  1811. ZVAL_STRING(&argv[4], arg4);
  1812. }
  1813. fci->retval = &retval;
  1814. fci->param_count = 5;
  1815. fci->params = argv;
  1816. fci->no_separation = 0;
  1817. int authreturn = SQLITE_DENY;
  1818. if (zend_call_function(fci, &db_obj->authorizer_fcc) != SUCCESS || Z_ISUNDEF(retval)) {
  1819. php_sqlite3_error(db_obj, "An error occurred while invoking the authorizer callback");
  1820. } else {
  1821. if (Z_TYPE(retval) != IS_LONG) {
  1822. php_sqlite3_error(db_obj, "The authorizer callback returned an invalid type: expected int");
  1823. } else {
  1824. authreturn = Z_LVAL(retval);
  1825. if (authreturn != SQLITE_OK && authreturn != SQLITE_IGNORE && authreturn != SQLITE_DENY) {
  1826. php_sqlite3_error(db_obj, "The authorizer callback returned an invalid value");
  1827. authreturn = SQLITE_DENY;
  1828. }
  1829. }
  1830. }
  1831. zend_fcall_info_args_clear(fci, 0);
  1832. zval_ptr_dtor(&retval);
  1833. return authreturn;
  1834. }
  1835. /* }}} */
  1836. /* {{{ php_sqlite3_free_list_dtor
  1837. */
  1838. static void php_sqlite3_free_list_dtor(void **item)
  1839. {
  1840. php_sqlite3_free_list *free_item = (php_sqlite3_free_list *)*item;
  1841. if (free_item->stmt_obj && free_item->stmt_obj->initialised) {
  1842. sqlite3_finalize(free_item->stmt_obj->stmt);
  1843. free_item->stmt_obj->initialised = 0;
  1844. }
  1845. efree(*item);
  1846. }
  1847. /* }}} */
  1848. static int php_sqlite3_compare_stmt_zval_free(php_sqlite3_free_list **free_list, zval *statement ) /* {{{ */
  1849. {
  1850. return ((*free_list)->stmt_obj->initialised && Z_PTR_P(statement) == Z_PTR((*free_list)->stmt_obj_zval));
  1851. }
  1852. /* }}} */
  1853. static int php_sqlite3_compare_stmt_free( php_sqlite3_free_list **free_list, sqlite3_stmt *statement ) /* {{{ */
  1854. {
  1855. return ((*free_list)->stmt_obj->initialised && statement == (*free_list)->stmt_obj->stmt);
  1856. }
  1857. /* }}} */
  1858. static void php_sqlite3_object_free_storage(zend_object *object) /* {{{ */
  1859. {
  1860. php_sqlite3_db_object *intern = php_sqlite3_db_from_obj(object);
  1861. php_sqlite3_func *func;
  1862. php_sqlite3_collation *collation;
  1863. if (!intern) {
  1864. return;
  1865. }
  1866. /* Release function_name from authorizer */
  1867. if (intern->authorizer_fci.size > 0) {
  1868. zval_ptr_dtor(&intern->authorizer_fci.function_name);
  1869. }
  1870. while (intern->funcs) {
  1871. func = intern->funcs;
  1872. intern->funcs = func->next;
  1873. if (intern->initialised && intern->db) {
  1874. sqlite3_create_function(intern->db, func->func_name, func->argc, SQLITE_UTF8, func, NULL, NULL, NULL);
  1875. }
  1876. efree((char*)func->func_name);
  1877. if (!Z_ISUNDEF(func->func)) {
  1878. zval_ptr_dtor(&func->func);
  1879. }
  1880. if (!Z_ISUNDEF(func->step)) {
  1881. zval_ptr_dtor(&func->step);
  1882. }
  1883. if (!Z_ISUNDEF(func->fini)) {
  1884. zval_ptr_dtor(&func->fini);
  1885. }
  1886. efree(func);
  1887. }
  1888. while (intern->collations){
  1889. collation = intern->collations;
  1890. intern->collations = collation->next;
  1891. if (intern->initialised && intern->db){
  1892. sqlite3_create_collation(intern->db, collation->collation_name, SQLITE_UTF8, NULL, NULL);
  1893. }
  1894. efree((char*)collation->collation_name);
  1895. if (!Z_ISUNDEF(collation->cmp_func)) {
  1896. zval_ptr_dtor(&collation->cmp_func);
  1897. }
  1898. efree(collation);
  1899. }
  1900. if (intern->initialised && intern->db) {
  1901. sqlite3_close(intern->db);
  1902. intern->initialised = 0;
  1903. }
  1904. zend_object_std_dtor(&intern->zo);
  1905. }
  1906. /* }}} */
  1907. static void php_sqlite3_stmt_object_free_storage(zend_object *object) /* {{{ */
  1908. {
  1909. php_sqlite3_stmt *intern = php_sqlite3_stmt_from_obj(object);
  1910. if (!intern) {
  1911. return;
  1912. }
  1913. if (intern->bound_params) {
  1914. zend_hash_destroy(intern->bound_params);
  1915. FREE_HASHTABLE(intern->bound_params);
  1916. intern->bound_params = NULL;
  1917. }
  1918. if (intern->initialised) {
  1919. zend_llist_del_element(&(intern->db_obj->free_list), intern->stmt,
  1920. (int (*)(void *, void *)) php_sqlite3_compare_stmt_free);
  1921. }
  1922. if (!Z_ISUNDEF(intern->db_obj_zval)) {
  1923. zval_ptr_dtor(&intern->db_obj_zval);
  1924. }
  1925. zend_object_std_dtor(&intern->zo);
  1926. }
  1927. /* }}} */
  1928. static void php_sqlite3_result_object_free_storage(zend_object *object) /* {{{ */
  1929. {
  1930. php_sqlite3_result *intern = php_sqlite3_result_from_obj(object);
  1931. if (!intern) {
  1932. return;
  1933. }
  1934. if (!Z_ISNULL(intern->stmt_obj_zval)) {
  1935. if (intern->stmt_obj && intern->stmt_obj->initialised) {
  1936. sqlite3_reset(intern->stmt_obj->stmt);
  1937. }
  1938. zval_ptr_dtor(&intern->stmt_obj_zval);
  1939. }
  1940. zend_object_std_dtor(&intern->zo);
  1941. }
  1942. /* }}} */
  1943. static zend_object *php_sqlite3_object_new(zend_class_entry *class_type) /* {{{ */
  1944. {
  1945. php_sqlite3_db_object *intern;
  1946. /* Allocate memory for it */
  1947. intern = zend_object_alloc(sizeof(php_sqlite3_db_object), class_type);
  1948. /* Need to keep track of things to free */
  1949. zend_llist_init(&(intern->free_list), sizeof(php_sqlite3_free_list *), (llist_dtor_func_t)php_sqlite3_free_list_dtor, 0);
  1950. zend_object_std_init(&intern->zo, class_type);
  1951. object_properties_init(&intern->zo, class_type);
  1952. intern->zo.handlers = &sqlite3_object_handlers;
  1953. return &intern->zo;
  1954. }
  1955. /* }}} */
  1956. static zend_object *php_sqlite3_stmt_object_new(zend_class_entry *class_type) /* {{{ */
  1957. {
  1958. php_sqlite3_stmt *intern;
  1959. /* Allocate memory for it */
  1960. intern = zend_object_alloc(sizeof(php_sqlite3_stmt), class_type);
  1961. zend_object_std_init(&intern->zo, class_type);
  1962. object_properties_init(&intern->zo, class_type);
  1963. intern->zo.handlers = &sqlite3_stmt_object_handlers;
  1964. return &intern->zo;
  1965. }
  1966. /* }}} */
  1967. static zend_object *php_sqlite3_result_object_new(zend_class_entry *class_type) /* {{{ */
  1968. {
  1969. php_sqlite3_result *intern;
  1970. /* Allocate memory for it */
  1971. intern = zend_object_alloc(sizeof(php_sqlite3_result), class_type);
  1972. zend_object_std_init(&intern->zo, class_type);
  1973. object_properties_init(&intern->zo, class_type);
  1974. intern->zo.handlers = &sqlite3_result_object_handlers;
  1975. return &intern->zo;
  1976. }
  1977. /* }}} */
  1978. static void sqlite3_param_dtor(zval *data) /* {{{ */
  1979. {
  1980. struct php_sqlite3_bound_param *param = (struct php_sqlite3_bound_param*)Z_PTR_P(data);
  1981. if (param->name) {
  1982. zend_string_release_ex(param->name, 0);
  1983. }
  1984. if (!Z_ISNULL(param->parameter)) {
  1985. zval_ptr_dtor(&(param->parameter));
  1986. ZVAL_UNDEF(&param->parameter);
  1987. }
  1988. efree(param);
  1989. }
  1990. /* }}} */
  1991. /* {{{ PHP_MINIT_FUNCTION
  1992. */
  1993. PHP_MINIT_FUNCTION(sqlite3)
  1994. {
  1995. zend_class_entry ce;
  1996. #if defined(ZTS)
  1997. /* Refuse to load if this wasn't a threasafe library loaded */
  1998. if (!sqlite3_threadsafe()) {
  1999. php_error_docref(NULL, E_WARNING, "A thread safe version of SQLite is required when using a thread safe version of PHP.");
  2000. return FAILURE;
  2001. }
  2002. #endif
  2003. memcpy(&sqlite3_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
  2004. memcpy(&sqlite3_stmt_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
  2005. memcpy(&sqlite3_result_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
  2006. /* Register SQLite 3 Class */
  2007. INIT_CLASS_ENTRY(ce, "SQLite3", class_SQLite3_methods);
  2008. ce.create_object = php_sqlite3_object_new;
  2009. sqlite3_object_handlers.offset = XtOffsetOf(php_sqlite3_db_object, zo);
  2010. sqlite3_object_handlers.clone_obj = NULL;
  2011. sqlite3_object_handlers.free_obj = php_sqlite3_object_free_storage;
  2012. php_sqlite3_sc_entry = zend_register_internal_class(&ce);
  2013. php_sqlite3_sc_entry->serialize = zend_class_serialize_deny;
  2014. php_sqlite3_sc_entry->unserialize = zend_class_unserialize_deny;
  2015. /* Register SQLite 3 Prepared Statement Class */
  2016. INIT_CLASS_ENTRY(ce, "SQLite3Stmt", class_SQLite3Stmt_methods);
  2017. ce.create_object = php_sqlite3_stmt_object_new;
  2018. sqlite3_stmt_object_handlers.offset = XtOffsetOf(php_sqlite3_stmt, zo);
  2019. sqlite3_stmt_object_handlers.clone_obj = NULL;
  2020. sqlite3_stmt_object_handlers.free_obj = php_sqlite3_stmt_object_free_storage;
  2021. php_sqlite3_stmt_entry = zend_register_internal_class(&ce);
  2022. php_sqlite3_stmt_entry->serialize = zend_class_serialize_deny;
  2023. php_sqlite3_stmt_entry->unserialize = zend_class_unserialize_deny;
  2024. /* Register SQLite 3 Result Class */
  2025. INIT_CLASS_ENTRY(ce, "SQLite3Result", class_SQLite3Result_methods);
  2026. ce.create_object = php_sqlite3_result_object_new;
  2027. sqlite3_result_object_handlers.offset = XtOffsetOf(php_sqlite3_result, zo);
  2028. sqlite3_result_object_handlers.clone_obj = NULL;
  2029. sqlite3_result_object_handlers.free_obj = php_sqlite3_result_object_free_storage;
  2030. php_sqlite3_result_entry = zend_register_internal_class(&ce);
  2031. php_sqlite3_result_entry->serialize = zend_class_serialize_deny;
  2032. php_sqlite3_result_entry->unserialize = zend_class_unserialize_deny;
  2033. REGISTER_INI_ENTRIES();
  2034. REGISTER_LONG_CONSTANT("SQLITE3_ASSOC", PHP_SQLITE3_ASSOC, CONST_CS | CONST_PERSISTENT);
  2035. REGISTER_LONG_CONSTANT("SQLITE3_NUM", PHP_SQLITE3_NUM, CONST_CS | CONST_PERSISTENT);
  2036. REGISTER_LONG_CONSTANT("SQLITE3_BOTH", PHP_SQLITE3_BOTH, CONST_CS | CONST_PERSISTENT);
  2037. REGISTER_LONG_CONSTANT("SQLITE3_INTEGER", SQLITE_INTEGER, CONST_CS | CONST_PERSISTENT);
  2038. REGISTER_LONG_CONSTANT("SQLITE3_FLOAT", SQLITE_FLOAT, CONST_CS | CONST_PERSISTENT);
  2039. REGISTER_LONG_CONSTANT("SQLITE3_TEXT", SQLITE3_TEXT, CONST_CS | CONST_PERSISTENT);
  2040. REGISTER_LONG_CONSTANT("SQLITE3_BLOB", SQLITE_BLOB, CONST_CS | CONST_PERSISTENT);
  2041. REGISTER_LONG_CONSTANT("SQLITE3_NULL", SQLITE_NULL, CONST_CS | CONST_PERSISTENT);
  2042. REGISTER_LONG_CONSTANT("SQLITE3_OPEN_READONLY", SQLITE_OPEN_READONLY, CONST_CS | CONST_PERSISTENT);
  2043. REGISTER_LONG_CONSTANT("SQLITE3_OPEN_READWRITE", SQLITE_OPEN_READWRITE, CONST_CS | CONST_PERSISTENT);
  2044. REGISTER_LONG_CONSTANT("SQLITE3_OPEN_CREATE", SQLITE_OPEN_CREATE, CONST_CS | CONST_PERSISTENT);
  2045. /* Class constants */
  2046. zend_declare_class_constant_long(php_sqlite3_sc_entry, "OK", sizeof("OK") - 1, SQLITE_OK);
  2047. /* Constants for authorizer return */
  2048. zend_declare_class_constant_long(php_sqlite3_sc_entry, "DENY", sizeof("DENY") - 1, SQLITE_DENY);
  2049. zend_declare_class_constant_long(php_sqlite3_sc_entry, "IGNORE", sizeof("IGNORE") - 1, SQLITE_IGNORE);
  2050. /* Constants for authorizer actions */
  2051. zend_declare_class_constant_long(php_sqlite3_sc_entry, "CREATE_INDEX", sizeof("CREATE_INDEX") - 1, SQLITE_CREATE_INDEX);
  2052. zend_declare_class_constant_long(php_sqlite3_sc_entry, "CREATE_TABLE", sizeof("CREATE_TABLE") - 1, SQLITE_CREATE_TABLE);
  2053. zend_declare_class_constant_long(php_sqlite3_sc_entry, "CREATE_TEMP_INDEX", sizeof("CREATE_TEMP_INDEX") - 1, SQLITE_CREATE_TEMP_INDEX);
  2054. zend_declare_class_constant_long(php_sqlite3_sc_entry, "CREATE_TEMP_TABLE", sizeof("CREATE_TEMP_TABLE") - 1, SQLITE_CREATE_TEMP_TABLE);
  2055. zend_declare_class_constant_long(php_sqlite3_sc_entry, "CREATE_TEMP_TRIGGER", sizeof("CREATE_TEMP_TRIGGER") - 1, SQLITE_CREATE_TEMP_TRIGGER);
  2056. zend_declare_class_constant_long(php_sqlite3_sc_entry, "CREATE_TEMP_VIEW", sizeof("CREATE_TEMP_VIEW") - 1, SQLITE_CREATE_TEMP_VIEW);
  2057. zend_declare_class_constant_long(php_sqlite3_sc_entry, "CREATE_TRIGGER", sizeof("CREATE_TRIGGER") - 1, SQLITE_CREATE_TRIGGER);
  2058. zend_declare_class_constant_long(php_sqlite3_sc_entry, "CREATE_VIEW", sizeof("CREATE_VIEW") - 1, SQLITE_CREATE_VIEW);
  2059. zend_declare_class_constant_long(php_sqlite3_sc_entry, "DELETE", sizeof("DELETE") - 1, SQLITE_DELETE);
  2060. zend_declare_class_constant_long(php_sqlite3_sc_entry, "DROP_INDEX", sizeof("DROP_INDEX") - 1, SQLITE_DROP_INDEX);
  2061. zend_declare_class_constant_long(php_sqlite3_sc_entry, "DROP_TABLE", sizeof("DROP_TABLE") - 1, SQLITE_DROP_TABLE);
  2062. zend_declare_class_constant_long(php_sqlite3_sc_entry, "DROP_TEMP_INDEX", sizeof("DROP_TEMP_INDEX") - 1, SQLITE_DROP_TEMP_INDEX);
  2063. zend_declare_class_constant_long(php_sqlite3_sc_entry, "DROP_TEMP_TABLE", sizeof("DROP_TEMP_TABLE") - 1, SQLITE_DROP_TEMP_TABLE);
  2064. zend_declare_class_constant_long(php_sqlite3_sc_entry, "DROP_TEMP_TRIGGER", sizeof("DROP_TEMP_TRIGGER") - 1, SQLITE_DROP_TEMP_TRIGGER);
  2065. zend_declare_class_constant_long(php_sqlite3_sc_entry, "DROP_TEMP_VIEW", sizeof("DROP_TEMP_VIEW") - 1, SQLITE_DROP_TEMP_VIEW);
  2066. zend_declare_class_constant_long(php_sqlite3_sc_entry, "DROP_TRIGGER", sizeof("DROP_TRIGGER") - 1, SQLITE_DROP_TRIGGER);
  2067. zend_declare_class_constant_long(php_sqlite3_sc_entry, "DROP_VIEW", sizeof("DROP_VIEW") - 1, SQLITE_DROP_VIEW);
  2068. zend_declare_class_constant_long(php_sqlite3_sc_entry, "INSERT", sizeof("INSERT") - 1, SQLITE_INSERT);
  2069. zend_declare_class_constant_long(php_sqlite3_sc_entry, "PRAGMA", sizeof("PRAGMA") - 1, SQLITE_PRAGMA);
  2070. zend_declare_class_constant_long(php_sqlite3_sc_entry, "READ", sizeof("READ") - 1, SQLITE_READ);
  2071. zend_declare_class_constant_long(php_sqlite3_sc_entry, "SELECT", sizeof("SELECT") - 1, SQLITE_SELECT);
  2072. zend_declare_class_constant_long(php_sqlite3_sc_entry, "TRANSACTION", sizeof("TRANSACTION") - 1, SQLITE_TRANSACTION);
  2073. zend_declare_class_constant_long(php_sqlite3_sc_entry, "UPDATE", sizeof("UPDATE") - 1, SQLITE_UPDATE);
  2074. zend_declare_class_constant_long(php_sqlite3_sc_entry, "ATTACH", sizeof("ATTACH") - 1, SQLITE_ATTACH);
  2075. zend_declare_class_constant_long(php_sqlite3_sc_entry, "DETACH", sizeof("DETACH") - 1, SQLITE_DETACH);
  2076. zend_declare_class_constant_long(php_sqlite3_sc_entry, "ALTER_TABLE", sizeof("ALTER_TABLE") - 1, SQLITE_ALTER_TABLE);
  2077. zend_declare_class_constant_long(php_sqlite3_sc_entry, "REINDEX", sizeof("REINDEX") - 1, SQLITE_REINDEX);
  2078. zend_declare_class_constant_long(php_sqlite3_sc_entry, "ANALYZE", sizeof("ANALYZE") - 1, SQLITE_ANALYZE);
  2079. zend_declare_class_constant_long(php_sqlite3_sc_entry, "CREATE_VTABLE", sizeof("CREATE_VTABLE") - 1, SQLITE_CREATE_VTABLE);
  2080. zend_declare_class_constant_long(php_sqlite3_sc_entry, "DROP_VTABLE", sizeof("DROP_VTABLE") - 1, SQLITE_DROP_VTABLE);
  2081. zend_declare_class_constant_long(php_sqlite3_sc_entry, "FUNCTION", sizeof("FUNCTION") - 1, SQLITE_FUNCTION);
  2082. zend_declare_class_constant_long(php_sqlite3_sc_entry, "SAVEPOINT", sizeof("SAVEPOINT") - 1, SQLITE_SAVEPOINT);
  2083. zend_declare_class_constant_long(php_sqlite3_sc_entry, "COPY", sizeof("COPY") - 1, SQLITE_COPY);
  2084. #ifdef SQLITE_RECURSIVE
  2085. zend_declare_class_constant_long(php_sqlite3_sc_entry, "RECURSIVE", sizeof("RECURSIVE") - 1, SQLITE_RECURSIVE);
  2086. #endif
  2087. #ifdef SQLITE_DETERMINISTIC
  2088. REGISTER_LONG_CONSTANT("SQLITE3_DETERMINISTIC", SQLITE_DETERMINISTIC, CONST_CS | CONST_PERSISTENT);
  2089. #endif
  2090. return SUCCESS;
  2091. }
  2092. /* }}} */
  2093. /* {{{ PHP_MSHUTDOWN_FUNCTION
  2094. */
  2095. PHP_MSHUTDOWN_FUNCTION(sqlite3)
  2096. {
  2097. UNREGISTER_INI_ENTRIES();
  2098. return SUCCESS;
  2099. }
  2100. /* }}} */
  2101. /* {{{ PHP_MINFO_FUNCTION
  2102. */
  2103. PHP_MINFO_FUNCTION(sqlite3)
  2104. {
  2105. php_info_print_table_start();
  2106. php_info_print_table_header(2, "SQLite3 support", "enabled");
  2107. php_info_print_table_row(2, "SQLite Library", sqlite3_libversion());
  2108. php_info_print_table_end();
  2109. DISPLAY_INI_ENTRIES();
  2110. }
  2111. /* }}} */
  2112. /* {{{ PHP_GINIT_FUNCTION
  2113. */
  2114. static PHP_GINIT_FUNCTION(sqlite3)
  2115. {
  2116. #if defined(COMPILE_DL_SQLITE3) && defined(ZTS)
  2117. ZEND_TSRMLS_CACHE_UPDATE();
  2118. #endif
  2119. memset(sqlite3_globals, 0, sizeof(*sqlite3_globals));
  2120. }
  2121. /* }}} */
  2122. /* {{{ sqlite3_module_entry
  2123. */
  2124. zend_module_entry sqlite3_module_entry = {
  2125. STANDARD_MODULE_HEADER,
  2126. "sqlite3",
  2127. NULL,
  2128. PHP_MINIT(sqlite3),
  2129. PHP_MSHUTDOWN(sqlite3),
  2130. NULL,
  2131. NULL,
  2132. PHP_MINFO(sqlite3),
  2133. PHP_SQLITE3_VERSION,
  2134. PHP_MODULE_GLOBALS(sqlite3),
  2135. PHP_GINIT(sqlite3),
  2136. NULL,
  2137. NULL,
  2138. STANDARD_MODULE_PROPERTIES_EX
  2139. };
  2140. /* }}} */
  2141. #ifdef COMPILE_DL_SQLITE3
  2142. #ifdef ZTS
  2143. ZEND_TSRMLS_CACHE_DEFINE()
  2144. #endif
  2145. ZEND_GET_MODULE(sqlite3)
  2146. #endif