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.

2134 lines
60 KiB

18 years ago
17 years ago
17 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: Scott MacVicar <scottmac@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include "php.h"
  23. #include "php_ini.h"
  24. #include "ext/standard/info.h"
  25. #include "php_sqlite3.h"
  26. #include "php_sqlite3_structs.h"
  27. #include "main/SAPI.h"
  28. #include <sqlite3.h>
  29. #include "zend_exceptions.h"
  30. #include "zend_interfaces.h"
  31. #include "SAPI.h"
  32. ZEND_DECLARE_MODULE_GLOBALS(sqlite3)
  33. static PHP_GINIT_FUNCTION(sqlite3);
  34. static int php_sqlite3_authorizer(void *autharg, int access_type, const char *arg3, const char *arg4, const char *arg5, const char *arg6);
  35. static void sqlite3_param_dtor(void *data);
  36. static int php_sqlite3_compare_stmt_zval_free( php_sqlite3_free_list **free_list, zval *statement );
  37. /* {{{ Error Handler
  38. */
  39. static void php_sqlite3_error(php_sqlite3_db_object *db_obj, char *format, ...)
  40. {
  41. va_list arg;
  42. char *message;
  43. TSRMLS_FETCH();
  44. va_start(arg, format);
  45. vspprintf(&message, 0, format, arg);
  46. va_end(arg);
  47. if (db_obj->exception) {
  48. zend_throw_exception(zend_exception_get_default(TSRMLS_C), message, 0 TSRMLS_CC);
  49. } else {
  50. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", message);
  51. }
  52. if (message) {
  53. efree(message);
  54. }
  55. }
  56. /* }}} */
  57. #define SQLITE3_CHECK_INITIALIZED(db_obj, member, class_name) \
  58. if (!(member)) { \
  59. php_sqlite3_error(db_obj, "The " #class_name " object has not been correctly initialised"); \
  60. RETURN_FALSE; \
  61. }
  62. /* {{{ PHP_INI
  63. */
  64. PHP_INI_BEGIN()
  65. STD_PHP_INI_ENTRY("sqlite3.extension_dir", NULL, PHP_INI_SYSTEM, OnUpdateString, extension_dir, zend_sqlite3_globals, sqlite3_globals)
  66. PHP_INI_END()
  67. /* }}} */
  68. /* Handlers */
  69. static zend_object_handlers sqlite3_object_handlers;
  70. static zend_object_handlers sqlite3_stmt_object_handlers;
  71. static zend_object_handlers sqlite3_result_object_handlers;
  72. /* Class entries */
  73. zend_class_entry *php_sqlite3_sc_entry;
  74. zend_class_entry *php_sqlite3_stmt_entry;
  75. zend_class_entry *php_sqlite3_result_entry;
  76. /* {{{ proto bool SQLite3::open(String filename [, int Flags [, string Encryption Key]])
  77. Opens a SQLite 3 Database, if the build includes encryption then it will attempt to use the key. */
  78. PHP_METHOD(sqlite3, open)
  79. {
  80. php_sqlite3_db_object *db_obj;
  81. zval *object = getThis();
  82. char *filename, *encryption_key, *fullpath;
  83. int filename_len, encryption_key_len = 0;
  84. long flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
  85. zend_error_handling error_handling;
  86. db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
  87. zend_replace_error_handling(EH_THROW, NULL, &error_handling TSRMLS_CC);
  88. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ls", &filename, &filename_len, &flags, &encryption_key, &encryption_key_len)) {
  89. zend_restore_error_handling(&error_handling TSRMLS_CC);
  90. return;
  91. }
  92. zend_restore_error_handling(&error_handling TSRMLS_CC);
  93. if (db_obj->initialised) {
  94. zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Already initialised DB Object", 0 TSRMLS_CC);
  95. }
  96. if (strncmp(filename, ":memory:", 8) != 0) {
  97. if (!(fullpath = expand_filepath(filename, NULL TSRMLS_CC))) {
  98. zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Unable to expand filepath", 0 TSRMLS_CC);
  99. return;
  100. }
  101. if (PG(safe_mode) && (!php_checkuid(fullpath, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
  102. zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "safe_mode prohibits opening %s", fullpath);
  103. efree(fullpath);
  104. return;
  105. }
  106. if (php_check_open_basedir(fullpath TSRMLS_CC)) {
  107. zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "open_basedir prohibits opening %s", fullpath);
  108. efree(fullpath);
  109. return;
  110. }
  111. } else {
  112. fullpath = estrdup(filename);
  113. }
  114. #if SQLITE_VERSION_NUMBER >= 3005000
  115. if (sqlite3_open_v2(fullpath, &(db_obj->db), flags, NULL) != SQLITE_OK) {
  116. #else
  117. if (sqlite3_open(fullpath, &(db_obj->db)) != SQLITE_OK) {
  118. #endif
  119. zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "Unable to open database: %s", sqlite3_errmsg(db_obj->db));
  120. if (fullpath) {
  121. efree(fullpath);
  122. }
  123. return;
  124. }
  125. db_obj->initialised = 1;
  126. #if SQLITE_HAS_CODEC
  127. if (encryption_key_len > 0) {
  128. if (sqlite3_key(db_obj->db, encryption_key, encryption_key_len) != SQLITE_OK) {
  129. zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "Unable to open database: %s", sqlite3_errmsg(db_obj->db));
  130. return;
  131. }
  132. }
  133. #endif
  134. if (PG(safe_mode) || (PG(open_basedir) && *PG(open_basedir))) {
  135. sqlite3_set_authorizer(db_obj->db, php_sqlite3_authorizer, NULL);
  136. }
  137. if (fullpath) {
  138. efree(fullpath);
  139. }
  140. }
  141. /* }}} */
  142. /* {{{ proto bool SQLite3::close()
  143. Close a SQLite 3 Database. */
  144. PHP_METHOD(sqlite3, close)
  145. {
  146. php_sqlite3_db_object *db_obj;
  147. zval *object = getThis();
  148. int errcode;
  149. db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
  150. if (zend_parse_parameters_none() == FAILURE) {
  151. return;
  152. }
  153. if (db_obj->initialised) {
  154. zend_llist_clean(&(db_obj->free_list));
  155. errcode = sqlite3_close(db_obj->db);
  156. if (errcode != SQLITE_OK) {
  157. php_sqlite3_error(db_obj, "Unable to close database: %d, %s", errcode, sqlite3_errmsg(db_obj->db));
  158. RETURN_FALSE;
  159. }
  160. db_obj->initialised = 0;
  161. }
  162. RETURN_TRUE;
  163. }
  164. /* }}} */
  165. /* {{{ proto bool SQLite3::exec(String Query)
  166. Executes a result-less query against a given database. */
  167. PHP_METHOD(sqlite3, exec)
  168. {
  169. php_sqlite3_db_object *db_obj;
  170. zval *object = getThis();
  171. char *sql, *errtext = NULL;
  172. int sql_len;
  173. db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
  174. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  175. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &sql, &sql_len)) {
  176. return;
  177. }
  178. if (sqlite3_exec(db_obj->db, sql, NULL, NULL, &errtext) != SQLITE_OK) {
  179. php_sqlite3_error(db_obj, "%s", errtext);
  180. sqlite3_free(errtext);
  181. RETURN_FALSE;
  182. }
  183. RETURN_TRUE;
  184. }
  185. /* }}} */
  186. /* {{{ proto Array SQLite3::version()
  187. Returns the SQLite3 Library version as a string constant and as a number. */
  188. PHP_METHOD(sqlite3, version)
  189. {
  190. if (zend_parse_parameters_none() == FAILURE) {
  191. return;
  192. }
  193. array_init(return_value);
  194. add_assoc_string(return_value, "versionString", (char*)sqlite3_libversion(), 1);
  195. add_assoc_long(return_value, "versionNumber", sqlite3_libversion_number());
  196. return;
  197. }
  198. /* }}} */
  199. /* {{{ proto int SQLite3::lastInsertRowID()
  200. Returns the rowid of the most recent INSERT into the database from the database connection. */
  201. PHP_METHOD(sqlite3, lastInsertRowID)
  202. {
  203. php_sqlite3_db_object *db_obj;
  204. zval *object = getThis();
  205. db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
  206. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  207. if (zend_parse_parameters_none() == FAILURE) {
  208. return;
  209. }
  210. RETURN_LONG(sqlite3_last_insert_rowid(db_obj->db));
  211. }
  212. /* }}} */
  213. /* {{{ proto int SQLite3::lastErrorCode()
  214. Returns the numeric result code of the most recent failed sqlite API call for the database connection. */
  215. PHP_METHOD(sqlite3, lastErrorCode)
  216. {
  217. php_sqlite3_db_object *db_obj;
  218. zval *object = getThis();
  219. db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
  220. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->db, SQLite3)
  221. if (zend_parse_parameters_none() == FAILURE) {
  222. return;
  223. }
  224. RETURN_LONG(sqlite3_errcode(db_obj->db));
  225. }
  226. /* }}} */
  227. /* {{{ proto string SQLite3::lastErrorMsg()
  228. Returns english text describing the most recent failed sqlite API call for the database connection. */
  229. PHP_METHOD(sqlite3, lastErrorMsg)
  230. {
  231. php_sqlite3_db_object *db_obj;
  232. zval *object = getThis();
  233. db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
  234. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->db, SQLite3)
  235. if (zend_parse_parameters_none() == FAILURE) {
  236. return;
  237. }
  238. RETVAL_STRING((char *)sqlite3_errmsg(db_obj->db), 1);
  239. }
  240. /* }}} */
  241. #ifndef SQLITE_OMIT_LOAD_EXTENSION
  242. /* {{{ proto bool SQLite3::loadExtension(String Shared Library)
  243. Attempts to load an SQLite extension library. */
  244. PHP_METHOD(sqlite3, loadExtension)
  245. {
  246. php_sqlite3_db_object *db_obj;
  247. zval *object = getThis();
  248. char *extension, *lib_path, *extension_dir, *errtext = NULL;
  249. char fullpath[MAXPATHLEN];
  250. int extension_len, extension_dir_len;
  251. db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
  252. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  253. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &extension, &extension_len)) {
  254. return;
  255. }
  256. #ifdef ZTS
  257. if ((strncmp(sapi_module.name, "cgi", 3) != 0) &&
  258. (strcmp(sapi_module.name, "cli") != 0) &&
  259. (strncmp(sapi_module.name, "embed", 5) != 0)
  260. ) { php_sqlite3_error(db_obj, "Not supported in multithreaded Web servers");
  261. RETURN_FALSE;
  262. }
  263. #endif
  264. if (!SQLITE3G(extension_dir)) {
  265. php_sqlite3_error(db_obj, "SQLite Extension are disabled");
  266. RETURN_FALSE;
  267. }
  268. if (extension_len == 0) {
  269. php_sqlite3_error(db_obj, "Empty string as an extension");
  270. RETURN_FALSE;
  271. }
  272. extension_dir = SQLITE3G(extension_dir);
  273. extension_dir_len = strlen(SQLITE3G(extension_dir));
  274. if (IS_SLASH(extension_dir[extension_dir_len-1])) {
  275. spprintf(&lib_path, 0, "%s%s", extension_dir, extension);
  276. } else {
  277. spprintf(&lib_path, 0, "%s%c%s", extension_dir, DEFAULT_SLASH, extension);
  278. }
  279. if (!VCWD_REALPATH(lib_path, fullpath)) {
  280. php_sqlite3_error(db_obj, "Unable to load extension at '%s'", lib_path);
  281. efree(lib_path);
  282. RETURN_FALSE;
  283. }
  284. efree(lib_path);
  285. if (strncmp(fullpath, extension_dir, extension_dir_len) != 0) {
  286. php_sqlite3_error(db_obj, "Unable to open extensions outside the defined directory");
  287. RETURN_FALSE;
  288. }
  289. /* Extension loading should only be enabled for when we attempt to load */
  290. sqlite3_enable_load_extension(db_obj->db, 1);
  291. if (sqlite3_load_extension(db_obj->db, fullpath, 0, &errtext) != SQLITE_OK) {
  292. php_sqlite3_error(db_obj, "%s", errtext);
  293. sqlite3_free(errtext);
  294. sqlite3_enable_load_extension(db_obj->db, 0);
  295. RETURN_FALSE;
  296. }
  297. sqlite3_enable_load_extension(db_obj->db, 0);
  298. RETURN_TRUE;
  299. }
  300. /* }}} */
  301. #endif
  302. /* {{{ proto int SQLite3::changes()
  303. Returns the number of database rows that were changed (or inserted or deleted) by the most recent SQL statement. */
  304. PHP_METHOD(sqlite3, changes)
  305. {
  306. php_sqlite3_db_object *db_obj;
  307. zval *object = getThis();
  308. db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
  309. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  310. if (zend_parse_parameters_none() == FAILURE) {
  311. return;
  312. }
  313. RETURN_LONG(sqlite3_changes(db_obj->db));
  314. }
  315. /* }}} */
  316. /* {{{ proto String SQLite3::escapeString(String value)
  317. Returns a string that has been properly escaped. */
  318. PHP_METHOD(sqlite3, escapeString)
  319. {
  320. char *sql, *ret;
  321. int sql_len;
  322. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &sql, &sql_len)) {
  323. return;
  324. }
  325. if (sql_len) {
  326. ret = sqlite3_mprintf("%q", sql);
  327. if (ret) {
  328. RETVAL_STRING(ret, 1);
  329. sqlite3_free(ret);
  330. }
  331. } else {
  332. RETURN_EMPTY_STRING();
  333. }
  334. }
  335. /* }}} */
  336. /* {{{ proto SQLite3Stmt SQLite3::prepare(String Query)
  337. Returns a prepared SQL statement for execution. */
  338. PHP_METHOD(sqlite3, prepare)
  339. {
  340. php_sqlite3_db_object *db_obj;
  341. php_sqlite3_stmt *stmt_obj;
  342. zval *object = getThis();
  343. char *sql;
  344. int sql_len, errcode;
  345. php_sqlite3_free_list *free_item;
  346. db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
  347. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  348. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &sql, &sql_len)) {
  349. return;
  350. }
  351. if (!sql_len) {
  352. RETURN_FALSE;
  353. }
  354. object_init_ex(return_value, php_sqlite3_stmt_entry);
  355. stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(return_value TSRMLS_CC);
  356. stmt_obj->db_obj = db_obj;
  357. stmt_obj->db_obj_zval = getThis();
  358. Z_ADDREF_P(object);
  359. errcode = sqlite3_prepare_v2(db_obj->db, sql, sql_len, &(stmt_obj->stmt), NULL);
  360. if (errcode != SQLITE_OK) {
  361. php_sqlite3_error(db_obj, "Unable to prepare statement: %d, %s", errcode, sqlite3_errmsg(db_obj->db));
  362. zval_dtor(return_value);
  363. RETURN_FALSE;
  364. }
  365. stmt_obj->initialised = 1;
  366. free_item = emalloc(sizeof(php_sqlite3_free_list));
  367. free_item->stmt_obj = stmt_obj;
  368. free_item->stmt_obj_zval = return_value;
  369. zend_llist_add_element(&(db_obj->free_list), &free_item);
  370. }
  371. /* }}} */
  372. /* {{{ proto SQLite3Result SQLite3::query(String Query)
  373. Returns true or false, for queries that return data it will return a SQLite3Result object. */
  374. PHP_METHOD(sqlite3, query)
  375. {
  376. php_sqlite3_db_object *db_obj;
  377. php_sqlite3_result *result;
  378. php_sqlite3_stmt *stmt_obj;
  379. zval *object = getThis();
  380. zval *stmt = NULL;
  381. char *sql, *errtext = NULL;
  382. int sql_len, return_code;
  383. db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
  384. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  385. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &sql, &sql_len)) {
  386. return;
  387. }
  388. if (!sql_len) {
  389. RETURN_FALSE;
  390. }
  391. /* If there was no return value then just execute the query */
  392. if (!return_value_used) {
  393. if (sqlite3_exec(db_obj->db, sql, NULL, NULL, &errtext) != SQLITE_OK) {
  394. php_sqlite3_error(db_obj, "%s", errtext);
  395. sqlite3_free(errtext);
  396. }
  397. return;
  398. }
  399. MAKE_STD_ZVAL(stmt);
  400. object_init_ex(stmt, php_sqlite3_stmt_entry);
  401. stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(stmt TSRMLS_CC);
  402. stmt_obj->db_obj = db_obj;
  403. stmt_obj->db_obj_zval = getThis();
  404. Z_ADDREF_P(object);
  405. return_code = sqlite3_prepare_v2(db_obj->db, sql, sql_len, &(stmt_obj->stmt), NULL);
  406. if (return_code != SQLITE_OK) {
  407. php_sqlite3_error(db_obj, "Unable to prepare statement: %d, %s", return_code, sqlite3_errmsg(db_obj->db));
  408. zval_ptr_dtor(&stmt);
  409. RETURN_FALSE;
  410. }
  411. stmt_obj->initialised = 1;
  412. object_init_ex(return_value, php_sqlite3_result_entry);
  413. result = (php_sqlite3_result *)zend_object_store_get_object(return_value TSRMLS_CC);
  414. result->db_obj = db_obj;
  415. result->stmt_obj = stmt_obj;
  416. result->stmt_obj_zval = stmt;
  417. return_code = sqlite3_step(result->stmt_obj->stmt);
  418. switch (return_code) {
  419. case SQLITE_ROW: /* Valid Row */
  420. case SQLITE_DONE: /* Valid but no results */
  421. {
  422. php_sqlite3_free_list *free_item;
  423. free_item = emalloc(sizeof(php_sqlite3_free_list));
  424. free_item->stmt_obj = stmt_obj;
  425. free_item->stmt_obj_zval = stmt;
  426. zend_llist_add_element(&(db_obj->free_list), &free_item);
  427. sqlite3_reset(result->stmt_obj->stmt);
  428. break;
  429. }
  430. default:
  431. php_sqlite3_error(db_obj, "Unable to execute statement: %s", sqlite3_errmsg(db_obj->db));
  432. sqlite3_finalize(stmt_obj->stmt);
  433. stmt_obj->initialised = 0;
  434. zval_dtor(return_value);
  435. RETURN_FALSE;
  436. }
  437. }
  438. /* }}} */
  439. static zval* sqlite_value_to_zval(sqlite3_stmt *stmt, int column) /* {{{ */
  440. {
  441. zval *data;
  442. MAKE_STD_ZVAL(data);
  443. switch (sqlite3_column_type(stmt, column)) {
  444. case SQLITE_INTEGER:
  445. if ((sqlite3_column_int64(stmt, column)) >= INT_MAX || sqlite3_column_int64(stmt, column) <= INT_MIN) {
  446. ZVAL_STRINGL(data, (char *)sqlite3_column_text(stmt, column), sqlite3_column_bytes(stmt, column), 1);
  447. } else {
  448. ZVAL_LONG(data, sqlite3_column_int64(stmt, column));
  449. }
  450. break;
  451. case SQLITE_FLOAT:
  452. ZVAL_DOUBLE(data, sqlite3_column_double(stmt, column));
  453. break;
  454. case SQLITE_NULL:
  455. ZVAL_NULL(data);
  456. break;
  457. case SQLITE3_TEXT:
  458. ZVAL_STRING(data, (char*)sqlite3_column_text(stmt, column), 1);
  459. break;
  460. case SQLITE_BLOB:
  461. default:
  462. ZVAL_STRINGL(data, (char*)sqlite3_column_blob(stmt, column), sqlite3_column_bytes(stmt, column), 1);
  463. }
  464. return data;
  465. }
  466. /* }}} */
  467. /* {{{ proto SQLite3Result SQLite3::querySingle(String Query [, bool entire_row = false])
  468. Returns a string of the first column, or an array of the entire row. */
  469. PHP_METHOD(sqlite3, querySingle)
  470. {
  471. php_sqlite3_db_object *db_obj;
  472. zval *object = getThis();
  473. char *sql, *errtext = NULL;
  474. int sql_len, return_code;
  475. zend_bool entire_row = 0;
  476. sqlite3_stmt *stmt;
  477. db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
  478. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  479. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &sql, &sql_len, &entire_row)) {
  480. return;
  481. }
  482. if (!sql_len) {
  483. RETURN_FALSE;
  484. }
  485. /* If there was no return value then just execute the query */
  486. if (!return_value_used) {
  487. if (sqlite3_exec(db_obj->db, sql, NULL, NULL, &errtext) != SQLITE_OK) {
  488. php_sqlite3_error(db_obj, "%s", errtext);
  489. sqlite3_free(errtext);
  490. }
  491. return;
  492. }
  493. return_code = sqlite3_prepare_v2(db_obj->db, sql, sql_len, &stmt, NULL);
  494. if (return_code != SQLITE_OK) {
  495. php_sqlite3_error(db_obj, "Unable to prepare statement: %d, %s", return_code, sqlite3_errmsg(db_obj->db));
  496. RETURN_FALSE;
  497. }
  498. return_code = sqlite3_step(stmt);
  499. switch (return_code) {
  500. case SQLITE_ROW: /* Valid Row */
  501. {
  502. if (!entire_row) {
  503. zval *data;
  504. data = sqlite_value_to_zval(stmt, 0);
  505. *return_value = *data;
  506. zval_copy_ctor(return_value);
  507. zval_dtor(data);
  508. FREE_ZVAL(data);
  509. } else {
  510. int i = 0;
  511. array_init(return_value);
  512. for (i = 0; i < sqlite3_data_count(stmt); i++) {
  513. zval *data;
  514. data = sqlite_value_to_zval(stmt, i);
  515. add_assoc_zval(return_value, (char*)sqlite3_column_name(stmt, i), data);
  516. }
  517. }
  518. break;
  519. }
  520. case SQLITE_DONE: /* Valid but no results */
  521. {
  522. if (!entire_row) {
  523. RETVAL_NULL();
  524. } else {
  525. array_init(return_value);
  526. }
  527. break;
  528. }
  529. default:
  530. php_sqlite3_error(db_obj, "Unable to execute statement: %s", sqlite3_errmsg(db_obj->db));
  531. RETVAL_FALSE;
  532. }
  533. sqlite3_finalize(stmt);
  534. }
  535. /* }}} */
  536. static int sqlite3_do_callback(struct php_sqlite3_fci *fc, zval *cb, int argc, sqlite3_value **argv, sqlite3_context *context, int is_agg TSRMLS_DC) /* {{{ */
  537. {
  538. zval ***zargs = NULL;
  539. zval *retval = NULL;
  540. int i;
  541. int ret;
  542. int fake_argc;
  543. php_sqlite3_agg_context *agg_context = NULL;
  544. if (is_agg) {
  545. is_agg = 2;
  546. }
  547. fake_argc = argc + is_agg;
  548. fc->fci.size = sizeof(fc->fci);
  549. fc->fci.function_table = EG(function_table);
  550. fc->fci.function_name = cb;
  551. fc->fci.symbol_table = NULL;
  552. fc->fci.object_ptr = NULL;
  553. fc->fci.retval_ptr_ptr = &retval;
  554. fc->fci.param_count = fake_argc;
  555. /* build up the params */
  556. if (fake_argc) {
  557. zargs = (zval ***)safe_emalloc(fake_argc, sizeof(zval **), 0);
  558. }
  559. if (is_agg) {
  560. /* summon the aggregation context */
  561. agg_context = (php_sqlite3_agg_context *)sqlite3_aggregate_context(context, sizeof(php_sqlite3_agg_context));
  562. if (!agg_context->zval_context) {
  563. MAKE_STD_ZVAL(agg_context->zval_context);
  564. ZVAL_NULL(agg_context->zval_context);
  565. }
  566. zargs[0] = &agg_context->zval_context;
  567. zargs[1] = emalloc(sizeof(zval*));
  568. MAKE_STD_ZVAL(*zargs[1]);
  569. ZVAL_LONG(*zargs[1], agg_context->row_count);
  570. }
  571. for (i = 0; i < argc; i++) {
  572. zargs[i + is_agg] = emalloc(sizeof(zval *));
  573. MAKE_STD_ZVAL(*zargs[i + is_agg]);
  574. switch (sqlite3_value_type(argv[i])) {
  575. case SQLITE_INTEGER:
  576. ZVAL_LONG(*zargs[i + is_agg], sqlite3_value_int(argv[i]));
  577. break;
  578. case SQLITE_FLOAT:
  579. ZVAL_DOUBLE(*zargs[i + is_agg], sqlite3_value_double(argv[i]));
  580. break;
  581. case SQLITE_NULL:
  582. ZVAL_NULL(*zargs[i + is_agg]);
  583. break;
  584. case SQLITE_BLOB:
  585. case SQLITE3_TEXT:
  586. default:
  587. ZVAL_STRINGL(*zargs[i + is_agg], (char*)sqlite3_value_text(argv[i]), sqlite3_value_bytes(argv[i]), 1);
  588. break;
  589. }
  590. }
  591. fc->fci.params = zargs;
  592. if ((ret = zend_call_function(&fc->fci, &fc->fcc TSRMLS_CC)) == FAILURE) {
  593. php_error_docref(NULL TSRMLS_CC, E_WARNING, "An error occurred while invoking the callback");
  594. }
  595. /* clean up the params */
  596. if (fake_argc) {
  597. for (i = is_agg; i < argc + is_agg; i++) {
  598. zval_ptr_dtor(zargs[i]);
  599. efree(zargs[i]);
  600. }
  601. if (is_agg) {
  602. zval_ptr_dtor(zargs[1]);
  603. efree(zargs[1]);
  604. }
  605. efree(zargs);
  606. }
  607. if (!is_agg || !argv) {
  608. /* only set the sqlite return value if we are a scalar function,
  609. * or if we are finalizing an aggregate */
  610. if (retval) {
  611. switch (Z_TYPE_P(retval)) {
  612. case IS_LONG:
  613. sqlite3_result_int(context, Z_LVAL_P(retval));
  614. break;
  615. case IS_NULL:
  616. sqlite3_result_null(context);
  617. break;
  618. case IS_DOUBLE:
  619. sqlite3_result_double(context, Z_DVAL_P(retval));
  620. break;
  621. default:
  622. convert_to_string_ex(&retval);
  623. sqlite3_result_text(context, Z_STRVAL_P(retval), Z_STRLEN_P(retval), SQLITE_TRANSIENT);
  624. break;
  625. }
  626. } else {
  627. sqlite3_result_error(context, "failed to invoke callback", 0);
  628. }
  629. if (agg_context && agg_context->zval_context) {
  630. zval_ptr_dtor(&agg_context->zval_context);
  631. }
  632. } else {
  633. /* we're stepping in an aggregate; the return value goes into
  634. * the context */
  635. if (agg_context && agg_context->zval_context) {
  636. zval_ptr_dtor(&agg_context->zval_context);
  637. }
  638. if (retval) {
  639. agg_context->zval_context = retval;
  640. retval = NULL;
  641. } else {
  642. agg_context->zval_context = NULL;
  643. }
  644. }
  645. if (retval) {
  646. zval_ptr_dtor(&retval);
  647. }
  648. return ret;
  649. }
  650. /* }}}*/
  651. static void php_sqlite3_callback_func(sqlite3_context *context, int argc, sqlite3_value **argv) /* {{{ */
  652. {
  653. php_sqlite3_func *func = (php_sqlite3_func *)sqlite3_user_data(context);
  654. TSRMLS_FETCH();
  655. sqlite3_do_callback(&func->afunc, func->func, argc, argv, context, 0 TSRMLS_CC);
  656. }
  657. /* }}}*/
  658. static void php_sqlite3_callback_step(sqlite3_context *context, int argc, sqlite3_value **argv) /* {{{ */
  659. {
  660. php_sqlite3_func *func = (php_sqlite3_func *)sqlite3_user_data(context);
  661. php_sqlite3_agg_context *agg_context = (php_sqlite3_agg_context *)sqlite3_aggregate_context(context, sizeof(php_sqlite3_agg_context));
  662. TSRMLS_FETCH();
  663. agg_context->row_count++;
  664. sqlite3_do_callback(&func->astep, func->step, argc, argv, context, 1 TSRMLS_CC);
  665. }
  666. /* }}} */
  667. static void php_sqlite3_callback_final(sqlite3_context *context) /* {{{ */
  668. {
  669. php_sqlite3_func *func = (php_sqlite3_func *)sqlite3_user_data(context);
  670. php_sqlite3_agg_context *agg_context = (php_sqlite3_agg_context *)sqlite3_aggregate_context(context, sizeof(php_sqlite3_agg_context));
  671. TSRMLS_FETCH();
  672. agg_context->row_count = 0;
  673. sqlite3_do_callback(&func->afini, func->fini, 0, NULL, context, 1 TSRMLS_CC);
  674. }
  675. /* }}} */
  676. /* {{{ proto bool SQLite3::createFunction(string name, mixed callback [, int argcount])
  677. Allows registration of a PHP function as a SQLite UDF that can be called within SQL statements. */
  678. PHP_METHOD(sqlite3, createFunction)
  679. {
  680. php_sqlite3_db_object *db_obj;
  681. zval *object = getThis();
  682. php_sqlite3_func *func;
  683. char *sql_func, *callback_name;
  684. int sql_func_len;
  685. zval *callback_func;
  686. long sql_func_num_args = -1;
  687. db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
  688. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  689. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|l", &sql_func, &sql_func_len, &callback_func, &sql_func_num_args) == FAILURE) {
  690. return;
  691. }
  692. if (!sql_func_len) {
  693. RETURN_FALSE;
  694. }
  695. if (!zend_is_callable(callback_func, 0, &callback_name TSRMLS_CC)) {
  696. php_sqlite3_error(db_obj, "Not a valid callback function %s", callback_name);
  697. efree(callback_name);
  698. RETURN_FALSE;
  699. }
  700. efree(callback_name);
  701. func = (php_sqlite3_func *)ecalloc(1, sizeof(*func));
  702. if (sqlite3_create_function(db_obj->db, sql_func, sql_func_num_args, SQLITE_UTF8, func, php_sqlite3_callback_func, NULL, NULL) == SQLITE_OK) {
  703. func->func_name = estrdup(sql_func);
  704. MAKE_STD_ZVAL(func->func);
  705. *(func->func) = *callback_func;
  706. zval_copy_ctor(func->func);
  707. INIT_PZVAL(func->func);
  708. func->argc = sql_func_num_args;
  709. func->next = db_obj->funcs;
  710. db_obj->funcs = func;
  711. RETURN_TRUE;
  712. }
  713. efree(func);
  714. RETURN_FALSE;
  715. }
  716. /* }}} */
  717. /* {{{ proto bool SQLite3::createAggregate(string name, mixed step, mixed final [, int argcount])
  718. Allows registration of a PHP function for use as an aggregate. */
  719. PHP_METHOD(sqlite3, createAggregate)
  720. {
  721. php_sqlite3_db_object *db_obj;
  722. zval *object = getThis();
  723. php_sqlite3_func *func;
  724. char *sql_func, *callback_name;
  725. int sql_func_len;
  726. zval *step_callback, *fini_callback;
  727. long sql_func_num_args = -1;
  728. db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
  729. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  730. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "szz|l", &sql_func, &sql_func_len, &step_callback, &fini_callback, &sql_func_num_args) == FAILURE) {
  731. return;
  732. }
  733. if (!sql_func_len) {
  734. RETURN_FALSE;
  735. }
  736. if (!zend_is_callable(step_callback, 0, &callback_name TSRMLS_CC)) {
  737. php_sqlite3_error(db_obj, "Not a valid callback function %s", callback_name);
  738. efree(callback_name);
  739. RETURN_FALSE;
  740. }
  741. efree(callback_name);
  742. if (!zend_is_callable(fini_callback, 0, &callback_name TSRMLS_CC)) {
  743. php_sqlite3_error(db_obj, "Not a valid callback function %s", callback_name);
  744. efree(callback_name);
  745. RETURN_FALSE;
  746. }
  747. efree(callback_name);
  748. func = (php_sqlite3_func *)ecalloc(1, sizeof(*func));
  749. 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) {
  750. func->func_name = estrdup(sql_func);
  751. MAKE_STD_ZVAL(func->step);
  752. *(func->step) = *step_callback;
  753. zval_copy_ctor(func->step);
  754. INIT_PZVAL(func->step);
  755. MAKE_STD_ZVAL(func->fini);
  756. *(func->fini) = *fini_callback;
  757. zval_copy_ctor(func->fini);
  758. INIT_PZVAL(func->fini);
  759. func->argc = sql_func_num_args;
  760. func->next = db_obj->funcs;
  761. db_obj->funcs = func;
  762. RETURN_TRUE;
  763. }
  764. efree(func);
  765. RETURN_FALSE;
  766. }
  767. /* }}} */
  768. typedef struct {
  769. sqlite3_blob *blob;
  770. size_t position;
  771. size_t size;
  772. } php_stream_sqlite3_data;
  773. static size_t php_sqlite3_stream_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
  774. {
  775. /* php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract; */
  776. return 0;
  777. }
  778. static size_t php_sqlite3_stream_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
  779. {
  780. php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract;
  781. if (sqlite3_stream->position + count >= sqlite3_stream->size) {
  782. count = sqlite3_stream->size - sqlite3_stream->position;
  783. stream->eof = 1;
  784. }
  785. if (count) {
  786. if (sqlite3_blob_read(sqlite3_stream->blob, buf, count, sqlite3_stream->position) != SQLITE_OK) {
  787. return 0;
  788. }
  789. sqlite3_stream->position += count;
  790. }
  791. return count;
  792. }
  793. static int php_sqlite3_stream_close(php_stream *stream, int close_handle TSRMLS_DC)
  794. {
  795. php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract;
  796. if (sqlite3_blob_close(sqlite3_stream->blob) != SQLITE_OK) {
  797. /* Error occured, but it still closed */
  798. }
  799. efree(sqlite3_stream);
  800. return 0;
  801. }
  802. static int php_sqlite3_stream_flush(php_stream *stream TSRMLS_DC)
  803. {
  804. /* do nothing */
  805. return 0;
  806. }
  807. /* {{{ */
  808. static int php_sqlite3_stream_seek(php_stream *stream, off_t offset, int whence, off_t *newoffs TSRMLS_DC)
  809. {
  810. php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract;
  811. switch(whence) {
  812. case SEEK_CUR:
  813. if (offset < 0) {
  814. if (sqlite3_stream->position < (size_t)(-offset)) {
  815. sqlite3_stream->position = 0;
  816. *newoffs = -1;
  817. return -1;
  818. } else {
  819. sqlite3_stream->position = sqlite3_stream->position + offset;
  820. *newoffs = sqlite3_stream->position;
  821. stream->eof = 0;
  822. return 0;
  823. }
  824. } else {
  825. if (sqlite3_stream->position + (size_t)(offset) > sqlite3_stream->size) {
  826. sqlite3_stream->position = sqlite3_stream->size;
  827. *newoffs = -1;
  828. return -1;
  829. } else {
  830. sqlite3_stream->position = sqlite3_stream->position + offset;
  831. *newoffs = sqlite3_stream->position;
  832. stream->eof = 0;
  833. return 0;
  834. }
  835. }
  836. case SEEK_SET:
  837. if (sqlite3_stream->size < (size_t)(offset)) {
  838. sqlite3_stream->position = sqlite3_stream->size;
  839. *newoffs = -1;
  840. return -1;
  841. } else {
  842. sqlite3_stream->position = offset;
  843. *newoffs = sqlite3_stream->position;
  844. stream->eof = 0;
  845. return 0;
  846. }
  847. case SEEK_END:
  848. if (offset > 0) {
  849. sqlite3_stream->position = sqlite3_stream->size;
  850. *newoffs = -1;
  851. return -1;
  852. } else if (sqlite3_stream->size < (size_t)(-offset)) {
  853. sqlite3_stream->position = 0;
  854. *newoffs = -1;
  855. return -1;
  856. } else {
  857. sqlite3_stream->position = sqlite3_stream->size + offset;
  858. *newoffs = sqlite3_stream->position;
  859. stream->eof = 0;
  860. return 0;
  861. }
  862. default:
  863. *newoffs = sqlite3_stream->position;
  864. return -1;
  865. }
  866. }
  867. /* }}} */
  868. static int php_sqlite3_stream_cast(php_stream *stream, int castas, void **ret TSRMLS_DC)
  869. {
  870. return FAILURE;
  871. }
  872. static int php_sqlite3_stream_stat(php_stream *stream, php_stream_statbuf *ssb TSRMLS_DC)
  873. {
  874. /* TODO: fill in details based on Data: and Content-Length: headers, and/or data
  875. * from curl_easy_getinfo().
  876. * For now, return -1 to indicate that it doesn't make sense to stat this stream */
  877. return -1;
  878. }
  879. static php_stream_ops php_stream_sqlite3_ops = {
  880. php_sqlite3_stream_write,
  881. php_sqlite3_stream_read,
  882. php_sqlite3_stream_close,
  883. php_sqlite3_stream_flush,
  884. "SQLite3",
  885. php_sqlite3_stream_seek,
  886. php_sqlite3_stream_cast,
  887. php_sqlite3_stream_stat
  888. };
  889. /* {{{ proto resource SQLite3::openBlob(string table, string column, int rowid [, string dbname])
  890. Open a blob as a stream which we can read / write to. */
  891. PHP_METHOD(sqlite3, openBlob)
  892. {
  893. php_sqlite3_db_object *db_obj;
  894. zval *object = getThis();
  895. char *table, *column, *dbname = "main";
  896. int table_len, column_len, dbname_len;
  897. long rowid, flags = 0;
  898. sqlite3_blob *blob = NULL;
  899. php_stream_sqlite3_data *sqlite3_stream;
  900. php_stream *stream;
  901. db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
  902. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  903. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssl|s", &table, &table_len, &column, &column_len, &rowid, &dbname, &dbname_len) == FAILURE) {
  904. return;
  905. }
  906. if (sqlite3_blob_open(db_obj->db, dbname, table, column, rowid, flags, &blob) != SQLITE_OK) {
  907. php_sqlite3_error(db_obj, "Unable to open blob: %s", sqlite3_errmsg(db_obj->db));
  908. RETURN_FALSE;
  909. }
  910. sqlite3_stream = emalloc(sizeof(php_stream_sqlite3_data));
  911. sqlite3_stream->blob = blob;
  912. sqlite3_stream->position = 0;
  913. sqlite3_stream->size = sqlite3_blob_bytes(blob);
  914. stream = php_stream_alloc(&php_stream_sqlite3_ops, sqlite3_stream, 0, "rb");
  915. if (stream) {
  916. php_stream_to_zval(stream, return_value);
  917. } else {
  918. RETURN_FALSE;
  919. }
  920. }
  921. /* }}} */
  922. /* {{{ proto bool SQLite3::enableExceptions([bool enableExceptions = false])
  923. Enables an exception error mode. */
  924. PHP_METHOD(sqlite3, enableExceptions)
  925. {
  926. php_sqlite3_db_object *db_obj;
  927. zval *object = getThis();
  928. zend_bool enableExceptions = 0;
  929. db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
  930. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &enableExceptions) == FAILURE) {
  931. return;
  932. }
  933. RETVAL_BOOL(db_obj->exception);
  934. db_obj->exception = enableExceptions;
  935. }
  936. /* }}} */
  937. /* {{{ proto int SQLite3Stmt::paramCount()
  938. Returns the number of parameters within the prepared statement. */
  939. PHP_METHOD(sqlite3stmt, paramCount)
  940. {
  941. php_sqlite3_stmt *stmt_obj;
  942. zval *object = getThis();
  943. stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
  944. if (zend_parse_parameters_none() == FAILURE) {
  945. return;
  946. }
  947. RETURN_LONG(sqlite3_bind_parameter_count(stmt_obj->stmt));
  948. }
  949. /* }}} */
  950. /* {{{ proto bool SQLite3Stmt::close()
  951. Closes the prepared statement. */
  952. PHP_METHOD(sqlite3stmt, close)
  953. {
  954. php_sqlite3_stmt *stmt_obj;
  955. zval *object = getThis();
  956. stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
  957. if (zend_parse_parameters_none() == FAILURE) {
  958. return;
  959. }
  960. zend_llist_del_element(&(stmt_obj->db_obj->free_list), object, (int (*)(void *, void *)) php_sqlite3_compare_stmt_zval_free);
  961. RETURN_TRUE;
  962. }
  963. /* }}} */
  964. /* {{{ proto bool SQLite3Stmt::reset()
  965. Reset the prepared statement to the state before it was executed, bindings still remain. */
  966. PHP_METHOD(sqlite3stmt, reset)
  967. {
  968. php_sqlite3_stmt *stmt_obj;
  969. zval *object = getThis();
  970. stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
  971. if (zend_parse_parameters_none() == FAILURE) {
  972. return;
  973. }
  974. if (sqlite3_reset(stmt_obj->stmt) != SQLITE_OK) {
  975. php_sqlite3_error(stmt_obj->db_obj, "Unable to reset statement: %s", sqlite3_errmsg(sqlite3_db_handle(stmt_obj->stmt)));
  976. RETURN_FALSE;
  977. }
  978. RETURN_TRUE;
  979. }
  980. /* }}} */
  981. /* {{{ proto bool SQLite3Stmt::clear()
  982. Clear all current bound parameters. */
  983. PHP_METHOD(sqlite3stmt, clear)
  984. {
  985. php_sqlite3_stmt *stmt_obj;
  986. zval *object = getThis();
  987. stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
  988. if (zend_parse_parameters_none() == FAILURE) {
  989. return;
  990. }
  991. if (sqlite3_clear_bindings(stmt_obj->stmt) != SQLITE_OK) {
  992. php_sqlite3_error(stmt_obj->db_obj, "Unable to clear statement: %s", sqlite3_errmsg(sqlite3_db_handle(stmt_obj->stmt)));
  993. RETURN_FALSE;
  994. }
  995. RETURN_TRUE;
  996. }
  997. /* }}} */
  998. static int register_bound_parameter_to_sqlite(struct php_sqlite3_bound_param *param, php_sqlite3_stmt *stmt TSRMLS_DC) /* {{{ */
  999. {
  1000. HashTable *hash;
  1001. hash = stmt->bound_params;
  1002. if (!hash) {
  1003. ALLOC_HASHTABLE(hash);
  1004. zend_hash_init(hash, 13, NULL, sqlite3_param_dtor, 0);
  1005. stmt->bound_params = hash;
  1006. }
  1007. /* We need a : prefix to resolve a name to a parameter number */
  1008. if (param->name) {
  1009. if (param->name[0] != ':') {
  1010. /* pre-increment for character + 1 for null */
  1011. char *temp = emalloc(++param->name_len + 1);
  1012. temp[0] = ':';
  1013. memmove(temp+1, param->name, param->name_len);
  1014. param->name = temp;
  1015. } else {
  1016. param->name = estrndup(param->name, param->name_len);
  1017. }
  1018. /* do lookup*/
  1019. param->param_number = sqlite3_bind_parameter_index(stmt->stmt, param->name);
  1020. }
  1021. if (param->param_number < 1) {
  1022. efree(param->name);
  1023. return 0;
  1024. }
  1025. if (param->param_number >= 1) {
  1026. zend_hash_index_del(hash, param->param_number);
  1027. }
  1028. if (param->name) {
  1029. zend_hash_update(hash, param->name, param->name_len, param, sizeof(*param), NULL);
  1030. } else {
  1031. zend_hash_index_update(hash, param->param_number, param, sizeof(*param), NULL);
  1032. }
  1033. return 1;
  1034. }
  1035. /* }}} */
  1036. /* {{{ proto bool SQLite3Stmt::bindParam(int parameter_number, mixed parameter [, int type])
  1037. Bind Paramater to a stmt variable. */
  1038. PHP_METHOD(sqlite3stmt, bindParam)
  1039. {
  1040. php_sqlite3_stmt *stmt_obj;
  1041. zval *object = getThis();
  1042. struct php_sqlite3_bound_param param = {0};
  1043. stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
  1044. param.param_number = -1;
  1045. param.type = SQLITE3_TEXT;
  1046. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "lz|l", &param.param_number, &param.parameter, &param.type) == FAILURE) {
  1047. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|l", &param.name, &param.name_len, &param.parameter, &param.type) == FAILURE) {
  1048. return;
  1049. }
  1050. }
  1051. Z_ADDREF_P(param.parameter);
  1052. if (!register_bound_parameter_to_sqlite(&param, stmt_obj TSRMLS_CC)) {
  1053. if (param.parameter) {
  1054. zval_ptr_dtor(&(param.parameter));
  1055. param.parameter = NULL;
  1056. }
  1057. RETURN_FALSE;
  1058. }
  1059. RETURN_TRUE;
  1060. }
  1061. /* }}} */
  1062. /* {{{ proto bool SQLite3Stmt::bindValue(int parameter_number, mixed parameter [, int type])
  1063. Bind Value of a parameter to a stmt variable. */
  1064. PHP_METHOD(sqlite3stmt, bindValue)
  1065. {
  1066. php_sqlite3_stmt *stmt_obj;
  1067. zval *object = getThis();
  1068. struct php_sqlite3_bound_param param = {0};
  1069. stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
  1070. param.param_number = -1;
  1071. param.type = SQLITE3_TEXT;
  1072. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "lz/|l", &param.param_number, &param.parameter, &param.type) == FAILURE) {
  1073. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz/|l", &param.name, &param.name_len, &param.parameter, &param.type) == FAILURE) {
  1074. return;
  1075. }
  1076. }
  1077. Z_ADDREF_P(param.parameter);
  1078. if (!register_bound_parameter_to_sqlite(&param, stmt_obj TSRMLS_CC)) {
  1079. if (param.parameter) {
  1080. zval_ptr_dtor(&(param.parameter));
  1081. param.parameter = NULL;
  1082. }
  1083. RETURN_FALSE;
  1084. }
  1085. RETURN_TRUE;
  1086. }
  1087. /* }}} */
  1088. /* {{{ proto SQLite3Result SQLite3Stmt::execute()
  1089. Executes a prepared statement and returns a result set object. */
  1090. PHP_METHOD(sqlite3stmt, execute)
  1091. {
  1092. php_sqlite3_stmt *stmt_obj;
  1093. php_sqlite3_result *result;
  1094. zval *object = getThis();
  1095. int return_code = 0;
  1096. struct php_sqlite3_bound_param *param;
  1097. stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
  1098. if (zend_parse_parameters_none() == FAILURE) {
  1099. return;
  1100. }
  1101. SQLITE3_CHECK_INITIALIZED(stmt_obj->db_obj, stmt_obj->initialised, SQLite3)
  1102. if (stmt_obj->bound_params) {
  1103. zend_hash_internal_pointer_reset(stmt_obj->bound_params);
  1104. while (zend_hash_get_current_data(stmt_obj->bound_params, (void **)&param) == SUCCESS) {
  1105. /* If the ZVAL is null then it should be bound as that */
  1106. if (Z_TYPE_P(param->parameter) == IS_NULL) {
  1107. sqlite3_bind_null(stmt_obj->stmt, param->param_number);
  1108. zend_hash_move_forward(stmt_obj->bound_params);
  1109. continue;
  1110. }
  1111. switch (param->type) {
  1112. case SQLITE_INTEGER:
  1113. convert_to_long(param->parameter);
  1114. sqlite3_bind_int(stmt_obj->stmt, param->param_number, Z_LVAL_P(param->parameter));
  1115. break;
  1116. case SQLITE_FLOAT:
  1117. /* convert_to_double(param->parameter);*/
  1118. sqlite3_bind_double(stmt_obj->stmt, param->param_number, Z_DVAL_P(param->parameter));
  1119. break;
  1120. case SQLITE_BLOB:
  1121. {
  1122. php_stream *stream = NULL;
  1123. int blength;
  1124. char *buffer = NULL;
  1125. if (Z_TYPE_P(param->parameter) == IS_RESOURCE) {
  1126. php_stream_from_zval_no_verify(stream, &param->parameter);
  1127. if (stream == NULL) {
  1128. php_sqlite3_error(stmt_obj->db_obj, "Unable to read stream for parameter %ld", param->param_number);
  1129. RETURN_FALSE;
  1130. }
  1131. blength = php_stream_copy_to_mem(stream, (void *)&buffer, PHP_STREAM_COPY_ALL, 0);
  1132. } else {
  1133. convert_to_string(param->parameter);
  1134. blength = Z_STRLEN_P(param->parameter);
  1135. buffer = Z_STRVAL_P(param->parameter);
  1136. }
  1137. sqlite3_bind_blob(stmt_obj->stmt, param->param_number, buffer, blength, SQLITE_TRANSIENT);
  1138. if (stream) {
  1139. pefree(buffer, 0);
  1140. }
  1141. break;
  1142. }
  1143. case SQLITE3_TEXT:
  1144. convert_to_string(param->parameter);
  1145. sqlite3_bind_text(stmt_obj->stmt, param->param_number, Z_STRVAL_P(param->parameter), Z_STRLEN_P(param->parameter), SQLITE_STATIC);
  1146. break;
  1147. case SQLITE_NULL:
  1148. sqlite3_bind_null(stmt_obj->stmt, param->param_number);
  1149. break;
  1150. default:
  1151. php_sqlite3_error(stmt_obj->db_obj, "Unknown parameter type: %ld for parameter %ld", param->type, param->param_number);
  1152. RETURN_FALSE;
  1153. }
  1154. zend_hash_move_forward(stmt_obj->bound_params);
  1155. }
  1156. }
  1157. return_code = sqlite3_step(stmt_obj->stmt);
  1158. switch (return_code) {
  1159. case SQLITE_ROW: /* Valid Row */
  1160. case SQLITE_DONE: /* Valid but no results */
  1161. {
  1162. sqlite3_reset(stmt_obj->stmt);
  1163. object_init_ex(return_value, php_sqlite3_result_entry);
  1164. result = (php_sqlite3_result *)zend_object_store_get_object(return_value TSRMLS_CC);
  1165. Z_ADDREF_P(object);
  1166. result->is_prepared_statement = 1;
  1167. result->db_obj = stmt_obj->db_obj;
  1168. result->stmt_obj = stmt_obj;
  1169. result->stmt_obj_zval = getThis();
  1170. break;
  1171. }
  1172. case SQLITE_ERROR:
  1173. sqlite3_reset(stmt_obj->stmt);
  1174. default:
  1175. php_sqlite3_error(stmt_obj->db_obj, "Unable to execute statement: %s", sqlite3_errmsg(sqlite3_db_handle(stmt_obj->stmt)));
  1176. zval_dtor(return_value);
  1177. RETURN_FALSE;
  1178. }
  1179. return;
  1180. }
  1181. /* }}} */
  1182. /* {{{ proto int SQLite3Stmt::__construct(SQLite3 dbobject, String Statement)
  1183. __constructor for SQLite3Stmt. */
  1184. PHP_METHOD(sqlite3stmt, __construct)
  1185. {
  1186. php_sqlite3_stmt *stmt_obj;
  1187. php_sqlite3_db_object *db_obj;
  1188. zval *object = getThis();
  1189. zval *db_zval;
  1190. char *sql;
  1191. int sql_len, errcode;
  1192. zend_error_handling error_handling;
  1193. php_sqlite3_free_list *free_item;
  1194. stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
  1195. zend_replace_error_handling(EH_THROW, NULL, &error_handling TSRMLS_CC);
  1196. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Os", &db_zval, php_sqlite3_sc_entry, &sql, &sql_len) == FAILURE) {
  1197. zend_restore_error_handling(&error_handling TSRMLS_CC);
  1198. return;
  1199. }
  1200. db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(db_zval TSRMLS_CC);
  1201. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  1202. zend_restore_error_handling(&error_handling TSRMLS_CC);
  1203. if (!sql_len) {
  1204. RETURN_FALSE;
  1205. }
  1206. stmt_obj->db_obj = db_obj;
  1207. stmt_obj->db_obj_zval = db_zval;
  1208. Z_ADDREF_P(db_zval);
  1209. errcode = sqlite3_prepare_v2(db_obj->db, sql, sql_len, &(stmt_obj->stmt), NULL);
  1210. if (errcode != SQLITE_OK) {
  1211. php_sqlite3_error(db_obj, "Unable to prepare statement: %d, %s", errcode, sqlite3_errmsg(db_obj->db));
  1212. zval_dtor(return_value);
  1213. RETURN_FALSE;
  1214. }
  1215. stmt_obj->initialised = 1;
  1216. free_item = emalloc(sizeof(php_sqlite3_free_list));
  1217. free_item->stmt_obj = stmt_obj;
  1218. free_item->stmt_obj_zval = getThis();
  1219. zend_llist_add_element(&(db_obj->free_list), &free_item);
  1220. }
  1221. /* }}} */
  1222. /* {{{ proto int SQLite3Result::numColumns()
  1223. Number of columns in the result set. */
  1224. PHP_METHOD(sqlite3result, numColumns)
  1225. {
  1226. php_sqlite3_result *result_obj;
  1227. zval *object = getThis();
  1228. result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
  1229. SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
  1230. if (zend_parse_parameters_none() == FAILURE) {
  1231. return;
  1232. }
  1233. RETURN_LONG(sqlite3_column_count(result_obj->stmt_obj->stmt));
  1234. }
  1235. /* }}} */
  1236. /* {{{ proto string SQLite3Result::columnName(int column)
  1237. Returns the name of the nth column. */
  1238. PHP_METHOD(sqlite3result, columnName)
  1239. {
  1240. php_sqlite3_result *result_obj;
  1241. zval *object = getThis();
  1242. long column = 0;
  1243. result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
  1244. SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
  1245. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &column) == FAILURE) {
  1246. return;
  1247. }
  1248. RETVAL_STRING((char*)sqlite3_column_name(result_obj->stmt_obj->stmt, column), 1);
  1249. }
  1250. /* }}} */
  1251. /* {{{ proto int SQLite3Result::columnType(int column)
  1252. Returns the type of the nth column. */
  1253. PHP_METHOD(sqlite3result, columnType)
  1254. {
  1255. php_sqlite3_result *result_obj;
  1256. zval *object = getThis();
  1257. long column = 0;
  1258. result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
  1259. SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
  1260. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &column) == FAILURE) {
  1261. return;
  1262. }
  1263. RETURN_LONG(sqlite3_column_type(result_obj->stmt_obj->stmt, column));
  1264. }
  1265. /* }}} */
  1266. /* {{{ proto array SQLite3Result::fetchArray([int mode])
  1267. Fetch a result row as both an associative or numerically indexed array or both. */
  1268. PHP_METHOD(sqlite3result, fetchArray)
  1269. {
  1270. php_sqlite3_result *result_obj;
  1271. zval *object = getThis();
  1272. int i, ret;
  1273. long mode = PHP_SQLITE3_BOTH;
  1274. result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
  1275. SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
  1276. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &mode) == FAILURE) {
  1277. return;
  1278. }
  1279. ret = sqlite3_step(result_obj->stmt_obj->stmt);
  1280. switch (ret) {
  1281. case SQLITE_ROW:
  1282. /* If there was no return value then just skip fetching */
  1283. if (!return_value_used) {
  1284. return;
  1285. }
  1286. array_init(return_value);
  1287. for (i = 0; i < sqlite3_data_count(result_obj->stmt_obj->stmt); i++) {
  1288. zval *data;
  1289. data = sqlite_value_to_zval(result_obj->stmt_obj->stmt, i);
  1290. if (mode & PHP_SQLITE3_NUM) {
  1291. add_index_zval(return_value, i, data);
  1292. }
  1293. if (mode & PHP_SQLITE3_ASSOC) {
  1294. if (mode & PHP_SQLITE3_NUM) {
  1295. Z_ADDREF_P(data);
  1296. }
  1297. add_assoc_zval(return_value, (char*)sqlite3_column_name(result_obj->stmt_obj->stmt, i), data);
  1298. }
  1299. }
  1300. break;
  1301. case SQLITE_DONE:
  1302. RETURN_FALSE;
  1303. break;
  1304. default:
  1305. php_sqlite3_error(result_obj->db_obj, "Unable to execute statement: %s", sqlite3_errmsg(sqlite3_db_handle(result_obj->stmt_obj->stmt)));
  1306. }
  1307. }
  1308. /* }}} */
  1309. /* {{{ proto bool SQLite3Result::reset()
  1310. Resets the result set back to the first row. */
  1311. PHP_METHOD(sqlite3result, reset)
  1312. {
  1313. php_sqlite3_result *result_obj;
  1314. zval *object = getThis();
  1315. result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
  1316. SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
  1317. if (zend_parse_parameters_none() == FAILURE) {
  1318. return;
  1319. }
  1320. if (sqlite3_reset(result_obj->stmt_obj->stmt) != SQLITE_OK) {
  1321. RETURN_FALSE;
  1322. }
  1323. result_obj->complete = 0;
  1324. RETURN_TRUE;
  1325. }
  1326. /* }}} */
  1327. /* {{{ proto bool SQLite3Result::finalize()
  1328. Closes the result set. */
  1329. PHP_METHOD(sqlite3result, finalize)
  1330. {
  1331. php_sqlite3_result *result_obj;
  1332. zval *object = getThis();
  1333. result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
  1334. SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
  1335. if (zend_parse_parameters_none() == FAILURE) {
  1336. return;
  1337. }
  1338. /* We need to finalize an internal statement */
  1339. if (result_obj->is_prepared_statement == 0) {
  1340. zend_llist_del_element(&(result_obj->db_obj->free_list), result_obj->stmt_obj_zval,
  1341. (int (*)(void *, void *)) php_sqlite3_compare_stmt_zval_free);
  1342. } else {
  1343. sqlite3_reset(result_obj->stmt_obj->stmt);
  1344. }
  1345. RETURN_TRUE;
  1346. }
  1347. /* }}} */
  1348. /* {{{ proto int SQLite3Result::__construct()
  1349. __constructor for SQLite3Result. */
  1350. PHP_METHOD(sqlite3result, __construct)
  1351. {
  1352. zend_throw_exception(zend_exception_get_default(TSRMLS_C), "SQLite3Result cannot be directly instantiated", 0 TSRMLS_CC);
  1353. }
  1354. /* }}} */
  1355. /* {{{ arginfo */
  1356. ZEND_BEGIN_ARG_INFO(arginfo_sqlite3_open, 0)
  1357. ZEND_ARG_INFO(0, filename)
  1358. ZEND_ARG_INFO(0, flags)
  1359. ZEND_ARG_INFO(0, encryption_key)
  1360. ZEND_END_ARG_INFO()
  1361. #ifndef SQLITE_OMIT_LOAD_EXTENSION
  1362. ZEND_BEGIN_ARG_INFO(arginfo_sqlite3_loadextension, 0)
  1363. ZEND_ARG_INFO(0, shared_library)
  1364. ZEND_END_ARG_INFO()
  1365. #endif
  1366. ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_escapestring, 0, 0, 1)
  1367. ZEND_ARG_INFO(0, value)
  1368. ZEND_END_ARG_INFO()
  1369. ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_query, 0, 0, 1)
  1370. ZEND_ARG_INFO(0, query)
  1371. ZEND_END_ARG_INFO()
  1372. ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_querysingle, 0, 0, 1)
  1373. ZEND_ARG_INFO(0, query)
  1374. ZEND_ARG_INFO(0, entire_row)
  1375. ZEND_END_ARG_INFO()
  1376. ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_createfunction, 0, 0, 2)
  1377. ZEND_ARG_INFO(0, name)
  1378. ZEND_ARG_INFO(0, callback)
  1379. ZEND_ARG_INFO(0, argument_count)
  1380. ZEND_END_ARG_INFO()
  1381. ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_createaggregate, 0, 0, 3)
  1382. ZEND_ARG_INFO(0, name)
  1383. ZEND_ARG_INFO(0, step_callback)
  1384. ZEND_ARG_INFO(0, final_callback)
  1385. ZEND_ARG_INFO(0, argument_count)
  1386. ZEND_END_ARG_INFO()
  1387. ZEND_BEGIN_ARG_INFO_EX(argingo_sqlite3_openblob, 0, 0, 3)
  1388. ZEND_ARG_INFO(0, table)
  1389. ZEND_ARG_INFO(0, column)
  1390. ZEND_ARG_INFO(0, rowid)
  1391. ZEND_ARG_INFO(0, dbname)
  1392. ZEND_END_ARG_INFO()
  1393. ZEND_BEGIN_ARG_INFO_EX(argingo_sqlite3_enableexceptions, 0, 0, 1)
  1394. ZEND_ARG_INFO(0, enableExceptions)
  1395. ZEND_END_ARG_INFO()
  1396. ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3stmt_bindparam, 0, 0, 2)
  1397. ZEND_ARG_INFO(0, param_number)
  1398. ZEND_ARG_INFO(1, param)
  1399. ZEND_ARG_INFO(0, type)
  1400. ZEND_END_ARG_INFO()
  1401. ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3stmt_bindvalue, 0, 0, 2)
  1402. ZEND_ARG_INFO(0, param_number)
  1403. ZEND_ARG_INFO(0, param)
  1404. ZEND_ARG_INFO(0, type)
  1405. ZEND_END_ARG_INFO()
  1406. ZEND_BEGIN_ARG_INFO(arginfo_sqlite3stmt_construct, 1)
  1407. ZEND_ARG_INFO(0, sqlite3)
  1408. ZEND_END_ARG_INFO()
  1409. ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3result_columnname, 0, 0, 1)
  1410. ZEND_ARG_INFO(0, column_number)
  1411. ZEND_END_ARG_INFO()
  1412. ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3result_columntype, 0, 0, 1)
  1413. ZEND_ARG_INFO(0, column_number)
  1414. ZEND_END_ARG_INFO()
  1415. ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3result_fetcharray, 0, 0, 1)
  1416. ZEND_ARG_INFO(0, mode)
  1417. ZEND_END_ARG_INFO()
  1418. ZEND_BEGIN_ARG_INFO(arginfo_sqlite3_void, 0)
  1419. ZEND_END_ARG_INFO()
  1420. /* }}} */
  1421. /* {{{ php_sqlite3_class_methods */
  1422. static zend_function_entry php_sqlite3_class_methods[] = {
  1423. PHP_ME(sqlite3, open, arginfo_sqlite3_open, ZEND_ACC_PUBLIC)
  1424. PHP_ME(sqlite3, close, arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
  1425. PHP_ME(sqlite3, exec, arginfo_sqlite3_query, ZEND_ACC_PUBLIC)
  1426. PHP_ME(sqlite3, version, arginfo_sqlite3_void, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
  1427. PHP_ME(sqlite3, lastInsertRowID, arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
  1428. PHP_ME(sqlite3, lastErrorCode, arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
  1429. PHP_ME(sqlite3, lastErrorMsg, arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
  1430. #ifndef SQLITE_OMIT_LOAD_EXTENSION
  1431. PHP_ME(sqlite3, loadExtension, arginfo_sqlite3_loadextension, ZEND_ACC_PUBLIC)
  1432. #endif
  1433. PHP_ME(sqlite3, changes, arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
  1434. PHP_ME(sqlite3, escapeString, arginfo_sqlite3_escapestring, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
  1435. PHP_ME(sqlite3, prepare, arginfo_sqlite3_query, ZEND_ACC_PUBLIC)
  1436. PHP_ME(sqlite3, query, arginfo_sqlite3_query, ZEND_ACC_PUBLIC)
  1437. PHP_ME(sqlite3, querySingle, arginfo_sqlite3_querysingle, ZEND_ACC_PUBLIC)
  1438. PHP_ME(sqlite3, createFunction, arginfo_sqlite3_createfunction, ZEND_ACC_PUBLIC)
  1439. PHP_ME(sqlite3, createAggregate, arginfo_sqlite3_createaggregate, ZEND_ACC_PUBLIC)
  1440. PHP_ME(sqlite3, openBlob, argingo_sqlite3_openblob, ZEND_ACC_PUBLIC)
  1441. PHP_ME(sqlite3, enableExceptions, argingo_sqlite3_enableexceptions, ZEND_ACC_PUBLIC)
  1442. /* Aliases */
  1443. PHP_MALIAS(sqlite3, __construct, open, arginfo_sqlite3_open, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
  1444. {NULL, NULL, NULL}
  1445. };
  1446. /* }}} */
  1447. /* {{{ php_sqlite3_stmt_class_methods */
  1448. static zend_function_entry php_sqlite3_stmt_class_methods[] = {
  1449. PHP_ME(sqlite3stmt, paramCount, arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
  1450. PHP_ME(sqlite3stmt, close, arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
  1451. PHP_ME(sqlite3stmt, reset, arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
  1452. PHP_ME(sqlite3stmt, clear, arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
  1453. PHP_ME(sqlite3stmt, execute, arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
  1454. PHP_ME(sqlite3stmt, bindParam, arginfo_sqlite3stmt_bindparam, ZEND_ACC_PUBLIC)
  1455. PHP_ME(sqlite3stmt, bindValue, arginfo_sqlite3stmt_bindvalue, ZEND_ACC_PUBLIC)
  1456. PHP_ME(sqlite3stmt, __construct, arginfo_sqlite3stmt_construct, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR)
  1457. {NULL, NULL, NULL}
  1458. };
  1459. /* }}} */
  1460. /* {{{ php_sqlite3_result_class_methods */
  1461. static zend_function_entry php_sqlite3_result_class_methods[] = {
  1462. PHP_ME(sqlite3result, numColumns, arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
  1463. PHP_ME(sqlite3result, columnName, arginfo_sqlite3result_columnname, ZEND_ACC_PUBLIC)
  1464. PHP_ME(sqlite3result, columnType, arginfo_sqlite3result_columntype, ZEND_ACC_PUBLIC)
  1465. PHP_ME(sqlite3result, fetchArray, arginfo_sqlite3result_fetcharray, ZEND_ACC_PUBLIC)
  1466. PHP_ME(sqlite3result, reset, arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
  1467. PHP_ME(sqlite3result, finalize, arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
  1468. PHP_ME(sqlite3result, __construct, arginfo_sqlite3_void, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR)
  1469. {NULL, NULL, NULL}
  1470. };
  1471. /* }}} */
  1472. /* {{{ Authorization Callback
  1473. */
  1474. static int php_sqlite3_authorizer(void *autharg, int access_type, const char *arg3, const char *arg4, const char *arg5, const char *arg6)
  1475. {
  1476. TSRMLS_FETCH();
  1477. switch (access_type) {
  1478. case SQLITE_ATTACH:
  1479. {
  1480. if (strncmp(arg3, ":memory:", sizeof(":memory:")-1)) {
  1481. if (PG(safe_mode) && (!php_checkuid(arg3, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
  1482. return SQLITE_DENY;
  1483. }
  1484. if (php_check_open_basedir(arg3 TSRMLS_CC)) {
  1485. return SQLITE_DENY;
  1486. }
  1487. }
  1488. return SQLITE_OK;
  1489. }
  1490. default:
  1491. /* access allowed */
  1492. return SQLITE_OK;
  1493. }
  1494. }
  1495. /* }}} */
  1496. /* {{{ php_sqlite3_free_list_dtor
  1497. */
  1498. static void php_sqlite3_free_list_dtor(void **item)
  1499. {
  1500. php_sqlite3_free_list *free_item = (php_sqlite3_free_list *)*item;
  1501. if (free_item->stmt_obj && free_item->stmt_obj->initialised) {
  1502. sqlite3_finalize(free_item->stmt_obj->stmt);
  1503. free_item->stmt_obj->initialised = 0;
  1504. }
  1505. efree(*item);
  1506. }
  1507. /* }}} */
  1508. static int php_sqlite3_compare_stmt_zval_free( php_sqlite3_free_list **free_list, zval *statement ) /* {{{ */
  1509. {
  1510. return ((*free_list)->stmt_obj->initialised && statement == (*free_list)->stmt_obj_zval);
  1511. }
  1512. /* }}} */
  1513. static int php_sqlite3_compare_stmt_free( php_sqlite3_free_list **free_list, sqlite3_stmt *statement ) /* {{{ */
  1514. {
  1515. return ((*free_list)->stmt_obj->initialised && statement == (*free_list)->stmt_obj->stmt);
  1516. }
  1517. /* }}} */
  1518. static void php_sqlite3_object_free_storage(void *object TSRMLS_DC) /* {{{ */
  1519. {
  1520. php_sqlite3_db_object *intern = (php_sqlite3_db_object *)object;
  1521. php_sqlite3_func *func;
  1522. if (!intern) {
  1523. return;
  1524. }
  1525. while (intern->funcs) {
  1526. func = intern->funcs;
  1527. intern->funcs = func->next;
  1528. if (intern->initialised && intern->db) {
  1529. sqlite3_create_function(intern->db, func->func_name, func->argc, SQLITE_UTF8, func, NULL, NULL, NULL);
  1530. }
  1531. efree((char*)func->func_name);
  1532. if (func->func) {
  1533. zval_ptr_dtor(&func->func);
  1534. }
  1535. if (func->step) {
  1536. zval_ptr_dtor(&func->step);
  1537. }
  1538. if (func->fini) {
  1539. zval_ptr_dtor(&func->fini);
  1540. }
  1541. efree(func);
  1542. }
  1543. if (intern->initialised && intern->db) {
  1544. sqlite3_close(intern->db);
  1545. intern->initialised = 0;
  1546. }
  1547. zend_object_std_dtor(&intern->zo TSRMLS_CC);
  1548. efree(intern);
  1549. }
  1550. /* }}} */
  1551. static void php_sqlite3_stmt_object_free_storage(void *object TSRMLS_DC) /* {{{ */
  1552. {
  1553. php_sqlite3_stmt *intern = (php_sqlite3_stmt *)object;
  1554. if (!intern) {
  1555. return;
  1556. }
  1557. if (intern->bound_params) {
  1558. zend_hash_destroy(intern->bound_params);
  1559. FREE_HASHTABLE(intern->bound_params);
  1560. intern->bound_params = NULL;
  1561. }
  1562. if (intern->initialised) {
  1563. zend_llist_del_element(&(intern->db_obj->free_list), intern->stmt,
  1564. (int (*)(void *, void *)) php_sqlite3_compare_stmt_free);
  1565. }
  1566. if (intern->db_obj_zval) {
  1567. Z_DELREF_P(intern->db_obj_zval);
  1568. }
  1569. zend_object_std_dtor(&intern->zo TSRMLS_CC);
  1570. efree(intern);
  1571. }
  1572. /* }}} */
  1573. static void php_sqlite3_result_object_free_storage(void *object TSRMLS_DC) /* {{{ */
  1574. {
  1575. php_sqlite3_result *intern = (php_sqlite3_result *)object;
  1576. if (!intern) {
  1577. return;
  1578. }
  1579. if (intern->stmt_obj_zval) {
  1580. if (intern->stmt_obj->initialised) {
  1581. sqlite3_reset(intern->stmt_obj->stmt);
  1582. }
  1583. if (intern->is_prepared_statement == 0) {
  1584. zval_dtor(intern->stmt_obj_zval);
  1585. FREE_ZVAL(intern->stmt_obj_zval);
  1586. } else {
  1587. zval_ptr_dtor(&intern->stmt_obj_zval);
  1588. }
  1589. }
  1590. zend_object_std_dtor(&intern->zo TSRMLS_CC);
  1591. efree(intern);
  1592. }
  1593. /* }}} */
  1594. static zend_object_value php_sqlite3_object_new(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
  1595. {
  1596. zval *tmp;
  1597. zend_object_value retval;
  1598. php_sqlite3_db_object *intern;
  1599. /* Allocate memory for it */
  1600. intern = emalloc(sizeof(php_sqlite3_db_object));
  1601. memset(&intern->zo, 0, sizeof(php_sqlite3_db_object));
  1602. intern->exception = 0;
  1603. /* Need to keep track of things to free */
  1604. zend_llist_init(&(intern->free_list), sizeof(php_sqlite3_free_list *), (llist_dtor_func_t)php_sqlite3_free_list_dtor, 0);
  1605. zend_object_std_init(&intern->zo, class_type TSRMLS_CC);
  1606. zend_hash_copy(intern->zo.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref,(void *) &tmp, sizeof(zval *));
  1607. retval.handle = zend_objects_store_put(intern, NULL, (zend_objects_free_object_storage_t) php_sqlite3_object_free_storage, NULL TSRMLS_CC);
  1608. retval.handlers = (zend_object_handlers *) &sqlite3_object_handlers;
  1609. return retval;
  1610. }
  1611. /* }}} */
  1612. static zend_object_value php_sqlite3_stmt_object_new(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
  1613. {
  1614. zval *tmp;
  1615. zend_object_value retval;
  1616. php_sqlite3_stmt *intern;
  1617. /* Allocate memory for it */
  1618. intern = emalloc(sizeof(php_sqlite3_stmt));
  1619. memset(&intern->zo, 0, sizeof(php_sqlite3_stmt));
  1620. intern->db_obj_zval = NULL;
  1621. zend_object_std_init(&intern->zo, class_type TSRMLS_CC);
  1622. zend_hash_copy(intern->zo.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref,(void *) &tmp, sizeof(zval *));
  1623. retval.handle = zend_objects_store_put(intern, NULL, (zend_objects_free_object_storage_t) php_sqlite3_stmt_object_free_storage, NULL TSRMLS_CC);
  1624. retval.handlers = (zend_object_handlers *) &sqlite3_stmt_object_handlers;
  1625. return retval;
  1626. }
  1627. /* }}} */
  1628. static zend_object_value php_sqlite3_result_object_new(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
  1629. {
  1630. zval *tmp;
  1631. zend_object_value retval;
  1632. php_sqlite3_result *intern;
  1633. /* Allocate memory for it */
  1634. intern = emalloc(sizeof(php_sqlite3_result));
  1635. memset(&intern->zo, 0, sizeof(php_sqlite3_result));
  1636. intern->complete = 0;
  1637. intern->is_prepared_statement = 0;
  1638. intern->stmt_obj_zval = NULL;
  1639. zend_object_std_init(&intern->zo, class_type TSRMLS_CC);
  1640. zend_hash_copy(intern->zo.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref,(void *) &tmp, sizeof(zval *));
  1641. retval.handle = zend_objects_store_put(intern, NULL, (zend_objects_free_object_storage_t) php_sqlite3_result_object_free_storage, NULL TSRMLS_CC);
  1642. retval.handlers = (zend_object_handlers *) &sqlite3_result_object_handlers;
  1643. return retval;
  1644. }
  1645. /* }}} */
  1646. static void sqlite3_param_dtor(void *data) /* {{{ */
  1647. {
  1648. struct php_sqlite3_bound_param *param = (struct php_sqlite3_bound_param*)data;
  1649. if (param->name) {
  1650. efree(param->name);
  1651. }
  1652. if (param->parameter) {
  1653. zval_ptr_dtor(&(param->parameter));
  1654. param->parameter = NULL;
  1655. }
  1656. }
  1657. /* }}} */
  1658. /* {{{ PHP_MINIT_FUNCTION
  1659. */
  1660. PHP_MINIT_FUNCTION(sqlite3)
  1661. {
  1662. zend_class_entry ce;
  1663. #if defined(ZTS)
  1664. /* Refuse to load if this wasn't a threasafe library loaded */
  1665. if (!sqlite3_threadsafe()) {
  1666. php_error_docref(NULL TSRMLS_CC, E_WARNING, "A thread safe version of SQLite is required when using a thread safe version of PHP.");
  1667. return FAILURE;
  1668. }
  1669. #endif
  1670. memcpy(&sqlite3_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  1671. memcpy(&sqlite3_stmt_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  1672. memcpy(&sqlite3_result_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  1673. /* Register SQLite 3 Class */
  1674. INIT_CLASS_ENTRY(ce, "SQLite3", php_sqlite3_class_methods);
  1675. ce.create_object = php_sqlite3_object_new;
  1676. sqlite3_object_handlers.clone_obj = NULL;
  1677. php_sqlite3_sc_entry = zend_register_internal_class(&ce TSRMLS_CC);
  1678. /* Register SQLite 3 Prepared Statement Class */
  1679. INIT_CLASS_ENTRY(ce, "SQLite3Stmt", php_sqlite3_stmt_class_methods);
  1680. ce.create_object = php_sqlite3_stmt_object_new;
  1681. sqlite3_stmt_object_handlers.clone_obj = NULL;
  1682. php_sqlite3_stmt_entry = zend_register_internal_class(&ce TSRMLS_CC);
  1683. /* Register SQLite 3 Result Class */
  1684. INIT_CLASS_ENTRY(ce, "SQLite3Result", php_sqlite3_result_class_methods);
  1685. ce.create_object = php_sqlite3_result_object_new;
  1686. sqlite3_result_object_handlers.clone_obj = NULL;
  1687. php_sqlite3_result_entry = zend_register_internal_class(&ce TSRMLS_CC);
  1688. REGISTER_INI_ENTRIES();
  1689. REGISTER_LONG_CONSTANT("SQLITE3_ASSOC", PHP_SQLITE3_ASSOC, CONST_CS | CONST_PERSISTENT);
  1690. REGISTER_LONG_CONSTANT("SQLITE3_NUM", PHP_SQLITE3_NUM, CONST_CS | CONST_PERSISTENT);
  1691. REGISTER_LONG_CONSTANT("SQLITE3_BOTH", PHP_SQLITE3_BOTH, CONST_CS | CONST_PERSISTENT);
  1692. REGISTER_LONG_CONSTANT("SQLITE3_INTEGER", SQLITE_INTEGER, CONST_CS | CONST_PERSISTENT);
  1693. REGISTER_LONG_CONSTANT("SQLITE3_FLOAT", SQLITE_FLOAT, CONST_CS | CONST_PERSISTENT);
  1694. REGISTER_LONG_CONSTANT("SQLITE3_TEXT", SQLITE3_TEXT, CONST_CS | CONST_PERSISTENT);
  1695. REGISTER_LONG_CONSTANT("SQLITE3_BLOB", SQLITE_BLOB, CONST_CS | CONST_PERSISTENT);
  1696. REGISTER_LONG_CONSTANT("SQLITE3_NULL", SQLITE_NULL, CONST_CS | CONST_PERSISTENT);
  1697. REGISTER_LONG_CONSTANT("SQLITE3_OPEN_READONLY", SQLITE_OPEN_READONLY, CONST_CS | CONST_PERSISTENT);
  1698. REGISTER_LONG_CONSTANT("SQLITE3_OPEN_READWRITE", SQLITE_OPEN_READWRITE, CONST_CS | CONST_PERSISTENT);
  1699. REGISTER_LONG_CONSTANT("SQLITE3_OPEN_CREATE", SQLITE_OPEN_CREATE, CONST_CS | CONST_PERSISTENT);
  1700. return SUCCESS;
  1701. }
  1702. /* }}} */
  1703. /* {{{ PHP_MSHUTDOWN_FUNCTION
  1704. */
  1705. PHP_MSHUTDOWN_FUNCTION(sqlite3)
  1706. {
  1707. UNREGISTER_INI_ENTRIES();
  1708. return SUCCESS;
  1709. }
  1710. /* }}} */
  1711. /* {{{ PHP_MINFO_FUNCTION
  1712. */
  1713. PHP_MINFO_FUNCTION(sqlite3)
  1714. {
  1715. php_info_print_table_start();
  1716. php_info_print_table_header(2, "SQLite3 support", "enabled");
  1717. php_info_print_table_row(2, "SQLite3 module version", PHP_SQLITE3_VERSION);
  1718. php_info_print_table_row(2, "SQLite Library", sqlite3_libversion());
  1719. php_info_print_table_end();
  1720. DISPLAY_INI_ENTRIES();
  1721. }
  1722. /* }}} */
  1723. /* {{{ PHP_GINIT_FUNCTION
  1724. */
  1725. static PHP_GINIT_FUNCTION(sqlite3)
  1726. {
  1727. memset(sqlite3_globals, 0, sizeof(*sqlite3_globals));
  1728. }
  1729. /* }}} */
  1730. /* {{{ sqlite3_module_entry
  1731. */
  1732. zend_module_entry sqlite3_module_entry = {
  1733. STANDARD_MODULE_HEADER,
  1734. "sqlite3",
  1735. NULL,
  1736. PHP_MINIT(sqlite3),
  1737. PHP_MSHUTDOWN(sqlite3),
  1738. NULL,
  1739. NULL,
  1740. PHP_MINFO(sqlite3),
  1741. PHP_SQLITE3_VERSION,
  1742. PHP_MODULE_GLOBALS(sqlite3),
  1743. PHP_GINIT(sqlite3),
  1744. NULL,
  1745. NULL,
  1746. STANDARD_MODULE_PROPERTIES_EX
  1747. };
  1748. /* }}} */
  1749. #ifdef COMPILE_DL_SQLITE3
  1750. ZEND_GET_MODULE(sqlite3)
  1751. #endif
  1752. /*
  1753. * Local variables:
  1754. * tab-width: 4
  1755. * c-basic-offset: 4
  1756. * End:
  1757. * vim600: sw=4 ts=4 fdm=marker
  1758. * vim<600: sw=4 ts=4
  1759. */