PageRenderTime 50ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/ext/sqlite3/sqlite3.c

http://github.com/infusion/PHP
C | 2187 lines | 1588 code | 387 blank | 212 comment | 278 complexity | 86dc25b227fcf79b0896ad9e74eab96f MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2011 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: sqlite3.c 307203 2011-01-07 01:11:16Z felipe $ */
  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 (strlen(filename) != filename_len) {
  97. return;
  98. }
  99. if (strncmp(filename, ":memory:", 8) != 0) {
  100. if (!(fullpath = expand_filepath(filename, NULL TSRMLS_CC))) {
  101. zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Unable to expand filepath", 0 TSRMLS_CC);
  102. return;
  103. }
  104. if (php_check_open_basedir(fullpath TSRMLS_CC)) {
  105. zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "open_basedir prohibits opening %s", fullpath);
  106. efree(fullpath);
  107. return;
  108. }
  109. } else {
  110. fullpath = estrdup(filename);
  111. }
  112. #if SQLITE_VERSION_NUMBER >= 3005000
  113. if (sqlite3_open_v2(fullpath, &(db_obj->db), flags, NULL) != SQLITE_OK) {
  114. #else
  115. if (sqlite3_open(fullpath, &(db_obj->db)) != SQLITE_OK) {
  116. #endif
  117. zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "Unable to open database: %s", sqlite3_errmsg(db_obj->db));
  118. if (fullpath) {
  119. efree(fullpath);
  120. }
  121. return;
  122. }
  123. #if SQLITE_HAS_CODEC
  124. if (encryption_key_len > 0) {
  125. if (sqlite3_key(db_obj->db, encryption_key, encryption_key_len) != SQLITE_OK) {
  126. zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "Unable to open database: %s", sqlite3_errmsg(db_obj->db));
  127. return;
  128. }
  129. }
  130. #endif
  131. db_obj->initialised = 1;
  132. if (PG(open_basedir) && *PG(open_basedir)) {
  133. sqlite3_set_authorizer(db_obj->db, php_sqlite3_authorizer, NULL);
  134. }
  135. if (fullpath) {
  136. efree(fullpath);
  137. }
  138. }
  139. /* }}} */
  140. /* {{{ proto bool SQLite3::close()
  141. Close a SQLite 3 Database. */
  142. PHP_METHOD(sqlite3, close)
  143. {
  144. php_sqlite3_db_object *db_obj;
  145. zval *object = getThis();
  146. int errcode;
  147. db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
  148. if (zend_parse_parameters_none() == FAILURE) {
  149. return;
  150. }
  151. if (db_obj->initialised) {
  152. zend_llist_clean(&(db_obj->free_list));
  153. errcode = sqlite3_close(db_obj->db);
  154. if (errcode != SQLITE_OK) {
  155. php_sqlite3_error(db_obj, "Unable to close database: %d, %s", errcode, sqlite3_errmsg(db_obj->db));
  156. RETURN_FALSE;
  157. }
  158. db_obj->initialised = 0;
  159. }
  160. RETURN_TRUE;
  161. }
  162. /* }}} */
  163. /* {{{ proto bool SQLite3::exec(String Query)
  164. Executes a result-less query against a given database. */
  165. PHP_METHOD(sqlite3, exec)
  166. {
  167. php_sqlite3_db_object *db_obj;
  168. zval *object = getThis();
  169. char *sql, *errtext = NULL;
  170. int sql_len;
  171. db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
  172. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  173. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &sql, &sql_len)) {
  174. return;
  175. }
  176. if (sqlite3_exec(db_obj->db, sql, NULL, NULL, &errtext) != SQLITE_OK) {
  177. php_sqlite3_error(db_obj, "%s", errtext);
  178. sqlite3_free(errtext);
  179. RETURN_FALSE;
  180. }
  181. RETURN_TRUE;
  182. }
  183. /* }}} */
  184. /* {{{ proto Array SQLite3::version()
  185. Returns the SQLite3 Library version as a string constant and as a number. */
  186. PHP_METHOD(sqlite3, version)
  187. {
  188. if (zend_parse_parameters_none() == FAILURE) {
  189. return;
  190. }
  191. array_init(return_value);
  192. add_assoc_string(return_value, "versionString", (char*)sqlite3_libversion(), 1);
  193. add_assoc_long(return_value, "versionNumber", sqlite3_libversion_number());
  194. return;
  195. }
  196. /* }}} */
  197. /* {{{ proto int SQLite3::lastInsertRowID()
  198. Returns the rowid of the most recent INSERT into the database from the database connection. */
  199. PHP_METHOD(sqlite3, lastInsertRowID)
  200. {
  201. php_sqlite3_db_object *db_obj;
  202. zval *object = getThis();
  203. db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
  204. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  205. if (zend_parse_parameters_none() == FAILURE) {
  206. return;
  207. }
  208. RETURN_LONG(sqlite3_last_insert_rowid(db_obj->db));
  209. }
  210. /* }}} */
  211. /* {{{ proto int SQLite3::lastErrorCode()
  212. Returns the numeric result code of the most recent failed sqlite API call for the database connection. */
  213. PHP_METHOD(sqlite3, lastErrorCode)
  214. {
  215. php_sqlite3_db_object *db_obj;
  216. zval *object = getThis();
  217. db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
  218. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->db, SQLite3)
  219. if (zend_parse_parameters_none() == FAILURE) {
  220. return;
  221. }
  222. RETURN_LONG(sqlite3_errcode(db_obj->db));
  223. }
  224. /* }}} */
  225. /* {{{ proto string SQLite3::lastErrorMsg()
  226. Returns english text describing the most recent failed sqlite API call for the database connection. */
  227. PHP_METHOD(sqlite3, lastErrorMsg)
  228. {
  229. php_sqlite3_db_object *db_obj;
  230. zval *object = getThis();
  231. db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
  232. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->db, SQLite3)
  233. if (zend_parse_parameters_none() == FAILURE) {
  234. return;
  235. }
  236. RETVAL_STRING((char *)sqlite3_errmsg(db_obj->db), 1);
  237. }
  238. /* }}} */
  239. /* {{{ proto bool SQLite3::busyTimeout(int msecs)
  240. 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. */
  241. PHP_METHOD(sqlite3, busyTimeout)
  242. {
  243. php_sqlite3_db_object *db_obj;
  244. zval *object = getThis();
  245. long ms;
  246. int return_code;
  247. db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
  248. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  249. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &ms)) {
  250. return;
  251. }
  252. return_code = sqlite3_busy_timeout(db_obj->db, ms);
  253. if (return_code != SQLITE_OK) {
  254. php_sqlite3_error(db_obj, "Unable to set busy timeout: %d, %s", return_code, sqlite3_errmsg(db_obj->db));
  255. RETURN_FALSE;
  256. }
  257. RETURN_TRUE;
  258. }
  259. /* }}} */
  260. #ifndef SQLITE_OMIT_LOAD_EXTENSION
  261. /* {{{ proto bool SQLite3::loadExtension(String Shared Library)
  262. Attempts to load an SQLite extension library. */
  263. PHP_METHOD(sqlite3, loadExtension)
  264. {
  265. php_sqlite3_db_object *db_obj;
  266. zval *object = getThis();
  267. char *extension, *lib_path, *extension_dir, *errtext = NULL;
  268. char fullpath[MAXPATHLEN];
  269. int extension_len, extension_dir_len;
  270. db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
  271. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  272. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &extension, &extension_len)) {
  273. return;
  274. }
  275. #ifdef ZTS
  276. if ((strncmp(sapi_module.name, "cgi", 3) != 0) &&
  277. (strcmp(sapi_module.name, "cli") != 0) &&
  278. (strncmp(sapi_module.name, "embed", 5) != 0)
  279. ) { php_sqlite3_error(db_obj, "Not supported in multithreaded Web servers");
  280. RETURN_FALSE;
  281. }
  282. #endif
  283. if (!SQLITE3G(extension_dir)) {
  284. php_sqlite3_error(db_obj, "SQLite Extension are disabled");
  285. RETURN_FALSE;
  286. }
  287. if (extension_len == 0) {
  288. php_sqlite3_error(db_obj, "Empty string as an extension");
  289. RETURN_FALSE;
  290. }
  291. extension_dir = SQLITE3G(extension_dir);
  292. extension_dir_len = strlen(SQLITE3G(extension_dir));
  293. if (IS_SLASH(extension_dir[extension_dir_len-1])) {
  294. spprintf(&lib_path, 0, "%s%s", extension_dir, extension);
  295. } else {
  296. spprintf(&lib_path, 0, "%s%c%s", extension_dir, DEFAULT_SLASH, extension);
  297. }
  298. if (!VCWD_REALPATH(lib_path, fullpath)) {
  299. php_sqlite3_error(db_obj, "Unable to load extension at '%s'", lib_path);
  300. efree(lib_path);
  301. RETURN_FALSE;
  302. }
  303. efree(lib_path);
  304. if (strncmp(fullpath, extension_dir, extension_dir_len) != 0) {
  305. php_sqlite3_error(db_obj, "Unable to open extensions outside the defined directory");
  306. RETURN_FALSE;
  307. }
  308. /* Extension loading should only be enabled for when we attempt to load */
  309. sqlite3_enable_load_extension(db_obj->db, 1);
  310. if (sqlite3_load_extension(db_obj->db, fullpath, 0, &errtext) != SQLITE_OK) {
  311. php_sqlite3_error(db_obj, "%s", errtext);
  312. sqlite3_free(errtext);
  313. sqlite3_enable_load_extension(db_obj->db, 0);
  314. RETURN_FALSE;
  315. }
  316. sqlite3_enable_load_extension(db_obj->db, 0);
  317. RETURN_TRUE;
  318. }
  319. /* }}} */
  320. #endif
  321. /* {{{ proto int SQLite3::changes()
  322. Returns the number of database rows that were changed (or inserted or deleted) by the most recent SQL statement. */
  323. PHP_METHOD(sqlite3, changes)
  324. {
  325. php_sqlite3_db_object *db_obj;
  326. zval *object = getThis();
  327. db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
  328. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  329. if (zend_parse_parameters_none() == FAILURE) {
  330. return;
  331. }
  332. RETURN_LONG(sqlite3_changes(db_obj->db));
  333. }
  334. /* }}} */
  335. /* {{{ proto String SQLite3::escapeString(String value)
  336. Returns a string that has been properly escaped. */
  337. PHP_METHOD(sqlite3, escapeString)
  338. {
  339. char *sql, *ret;
  340. int sql_len;
  341. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &sql, &sql_len)) {
  342. return;
  343. }
  344. if (sql_len) {
  345. ret = sqlite3_mprintf("%q", sql);
  346. if (ret) {
  347. RETVAL_STRING(ret, 1);
  348. sqlite3_free(ret);
  349. }
  350. } else {
  351. RETURN_EMPTY_STRING();
  352. }
  353. }
  354. /* }}} */
  355. /* {{{ proto SQLite3Stmt SQLite3::prepare(String Query)
  356. Returns a prepared SQL statement for execution. */
  357. PHP_METHOD(sqlite3, prepare)
  358. {
  359. php_sqlite3_db_object *db_obj;
  360. php_sqlite3_stmt *stmt_obj;
  361. zval *object = getThis();
  362. char *sql;
  363. int sql_len, errcode;
  364. php_sqlite3_free_list *free_item;
  365. db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
  366. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  367. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &sql, &sql_len)) {
  368. return;
  369. }
  370. if (!sql_len) {
  371. RETURN_FALSE;
  372. }
  373. object_init_ex(return_value, php_sqlite3_stmt_entry);
  374. stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(return_value TSRMLS_CC);
  375. stmt_obj->db_obj = db_obj;
  376. stmt_obj->db_obj_zval = getThis();
  377. Z_ADDREF_P(object);
  378. errcode = sqlite3_prepare_v2(db_obj->db, sql, sql_len, &(stmt_obj->stmt), NULL);
  379. if (errcode != SQLITE_OK) {
  380. php_sqlite3_error(db_obj, "Unable to prepare statement: %d, %s", errcode, sqlite3_errmsg(db_obj->db));
  381. zval_dtor(return_value);
  382. RETURN_FALSE;
  383. }
  384. stmt_obj->initialised = 1;
  385. free_item = emalloc(sizeof(php_sqlite3_free_list));
  386. free_item->stmt_obj = stmt_obj;
  387. free_item->stmt_obj_zval = return_value;
  388. zend_llist_add_element(&(db_obj->free_list), &free_item);
  389. }
  390. /* }}} */
  391. /* {{{ proto SQLite3Result SQLite3::query(String Query)
  392. Returns true or false, for queries that return data it will return a SQLite3Result object. */
  393. PHP_METHOD(sqlite3, query)
  394. {
  395. php_sqlite3_db_object *db_obj;
  396. php_sqlite3_result *result;
  397. php_sqlite3_stmt *stmt_obj;
  398. zval *object = getThis();
  399. zval *stmt = NULL;
  400. char *sql, *errtext = NULL;
  401. int sql_len, return_code;
  402. db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
  403. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  404. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &sql, &sql_len)) {
  405. return;
  406. }
  407. if (!sql_len) {
  408. RETURN_FALSE;
  409. }
  410. /* If there was no return value then just execute the query */
  411. if (!return_value_used) {
  412. if (sqlite3_exec(db_obj->db, sql, NULL, NULL, &errtext) != SQLITE_OK) {
  413. php_sqlite3_error(db_obj, "%s", errtext);
  414. sqlite3_free(errtext);
  415. }
  416. return;
  417. }
  418. MAKE_STD_ZVAL(stmt);
  419. object_init_ex(stmt, php_sqlite3_stmt_entry);
  420. stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(stmt TSRMLS_CC);
  421. stmt_obj->db_obj = db_obj;
  422. stmt_obj->db_obj_zval = getThis();
  423. Z_ADDREF_P(object);
  424. return_code = sqlite3_prepare_v2(db_obj->db, sql, sql_len, &(stmt_obj->stmt), NULL);
  425. if (return_code != SQLITE_OK) {
  426. php_sqlite3_error(db_obj, "Unable to prepare statement: %d, %s", return_code, sqlite3_errmsg(db_obj->db));
  427. zval_ptr_dtor(&stmt);
  428. RETURN_FALSE;
  429. }
  430. stmt_obj->initialised = 1;
  431. object_init_ex(return_value, php_sqlite3_result_entry);
  432. result = (php_sqlite3_result *)zend_object_store_get_object(return_value TSRMLS_CC);
  433. result->db_obj = db_obj;
  434. result->stmt_obj = stmt_obj;
  435. result->stmt_obj_zval = stmt;
  436. return_code = sqlite3_step(result->stmt_obj->stmt);
  437. switch (return_code) {
  438. case SQLITE_ROW: /* Valid Row */
  439. case SQLITE_DONE: /* Valid but no results */
  440. {
  441. php_sqlite3_free_list *free_item;
  442. free_item = emalloc(sizeof(php_sqlite3_free_list));
  443. free_item->stmt_obj = stmt_obj;
  444. free_item->stmt_obj_zval = stmt;
  445. zend_llist_add_element(&(db_obj->free_list), &free_item);
  446. sqlite3_reset(result->stmt_obj->stmt);
  447. break;
  448. }
  449. default:
  450. php_sqlite3_error(db_obj, "Unable to execute statement: %s", sqlite3_errmsg(db_obj->db));
  451. sqlite3_finalize(stmt_obj->stmt);
  452. stmt_obj->initialised = 0;
  453. zval_dtor(return_value);
  454. RETURN_FALSE;
  455. }
  456. }
  457. /* }}} */
  458. static zval* sqlite_value_to_zval(sqlite3_stmt *stmt, int column) /* {{{ */
  459. {
  460. zval *data;
  461. MAKE_STD_ZVAL(data);
  462. switch (sqlite3_column_type(stmt, column)) {
  463. case SQLITE_INTEGER:
  464. if ((sqlite3_column_int64(stmt, column)) >= INT_MAX || sqlite3_column_int64(stmt, column) <= INT_MIN) {
  465. ZVAL_STRINGL(data, (char *)sqlite3_column_text(stmt, column), sqlite3_column_bytes(stmt, column), 1);
  466. } else {
  467. ZVAL_LONG(data, sqlite3_column_int64(stmt, column));
  468. }
  469. break;
  470. case SQLITE_FLOAT:
  471. ZVAL_DOUBLE(data, sqlite3_column_double(stmt, column));
  472. break;
  473. case SQLITE_NULL:
  474. ZVAL_NULL(data);
  475. break;
  476. case SQLITE3_TEXT:
  477. ZVAL_STRING(data, (char*)sqlite3_column_text(stmt, column), 1);
  478. break;
  479. case SQLITE_BLOB:
  480. default:
  481. ZVAL_STRINGL(data, (char*)sqlite3_column_blob(stmt, column), sqlite3_column_bytes(stmt, column), 1);
  482. }
  483. return data;
  484. }
  485. /* }}} */
  486. /* {{{ proto SQLite3Result SQLite3::querySingle(String Query [, bool entire_row = false])
  487. Returns a string of the first column, or an array of the entire row. */
  488. PHP_METHOD(sqlite3, querySingle)
  489. {
  490. php_sqlite3_db_object *db_obj;
  491. zval *object = getThis();
  492. char *sql, *errtext = NULL;
  493. int sql_len, return_code;
  494. zend_bool entire_row = 0;
  495. sqlite3_stmt *stmt;
  496. db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
  497. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  498. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &sql, &sql_len, &entire_row)) {
  499. return;
  500. }
  501. if (!sql_len) {
  502. RETURN_FALSE;
  503. }
  504. /* If there was no return value then just execute the query */
  505. if (!return_value_used) {
  506. if (sqlite3_exec(db_obj->db, sql, NULL, NULL, &errtext) != SQLITE_OK) {
  507. php_sqlite3_error(db_obj, "%s", errtext);
  508. sqlite3_free(errtext);
  509. }
  510. return;
  511. }
  512. return_code = sqlite3_prepare_v2(db_obj->db, sql, sql_len, &stmt, NULL);
  513. if (return_code != SQLITE_OK) {
  514. php_sqlite3_error(db_obj, "Unable to prepare statement: %d, %s", return_code, sqlite3_errmsg(db_obj->db));
  515. RETURN_FALSE;
  516. }
  517. return_code = sqlite3_step(stmt);
  518. switch (return_code) {
  519. case SQLITE_ROW: /* Valid Row */
  520. {
  521. if (!entire_row) {
  522. zval *data;
  523. data = sqlite_value_to_zval(stmt, 0);
  524. *return_value = *data;
  525. zval_copy_ctor(return_value);
  526. zval_dtor(data);
  527. FREE_ZVAL(data);
  528. } else {
  529. int i = 0;
  530. array_init(return_value);
  531. for (i = 0; i < sqlite3_data_count(stmt); i++) {
  532. zval *data;
  533. data = sqlite_value_to_zval(stmt, i);
  534. add_assoc_zval(return_value, (char*)sqlite3_column_name(stmt, i), data);
  535. }
  536. }
  537. break;
  538. }
  539. case SQLITE_DONE: /* Valid but no results */
  540. {
  541. if (!entire_row) {
  542. RETVAL_NULL();
  543. } else {
  544. array_init(return_value);
  545. }
  546. break;
  547. }
  548. default:
  549. php_sqlite3_error(db_obj, "Unable to execute statement: %s", sqlite3_errmsg(db_obj->db));
  550. RETVAL_FALSE;
  551. }
  552. sqlite3_finalize(stmt);
  553. }
  554. /* }}} */
  555. 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) /* {{{ */
  556. {
  557. zval ***zargs = NULL;
  558. zval *retval = NULL;
  559. int i;
  560. int ret;
  561. int fake_argc;
  562. php_sqlite3_agg_context *agg_context = NULL;
  563. if (is_agg) {
  564. is_agg = 2;
  565. }
  566. fake_argc = argc + is_agg;
  567. fc->fci.size = sizeof(fc->fci);
  568. fc->fci.function_table = EG(function_table);
  569. fc->fci.function_name = cb;
  570. fc->fci.symbol_table = NULL;
  571. fc->fci.object_ptr = NULL;
  572. fc->fci.retval_ptr_ptr = &retval;
  573. fc->fci.param_count = fake_argc;
  574. /* build up the params */
  575. if (fake_argc) {
  576. zargs = (zval ***)safe_emalloc(fake_argc, sizeof(zval **), 0);
  577. }
  578. if (is_agg) {
  579. /* summon the aggregation context */
  580. agg_context = (php_sqlite3_agg_context *)sqlite3_aggregate_context(context, sizeof(php_sqlite3_agg_context));
  581. if (!agg_context->zval_context) {
  582. MAKE_STD_ZVAL(agg_context->zval_context);
  583. ZVAL_NULL(agg_context->zval_context);
  584. }
  585. zargs[0] = &agg_context->zval_context;
  586. zargs[1] = emalloc(sizeof(zval*));
  587. MAKE_STD_ZVAL(*zargs[1]);
  588. ZVAL_LONG(*zargs[1], agg_context->row_count);
  589. }
  590. for (i = 0; i < argc; i++) {
  591. zargs[i + is_agg] = emalloc(sizeof(zval *));
  592. MAKE_STD_ZVAL(*zargs[i + is_agg]);
  593. switch (sqlite3_value_type(argv[i])) {
  594. case SQLITE_INTEGER:
  595. ZVAL_LONG(*zargs[i + is_agg], sqlite3_value_int(argv[i]));
  596. break;
  597. case SQLITE_FLOAT:
  598. ZVAL_DOUBLE(*zargs[i + is_agg], sqlite3_value_double(argv[i]));
  599. break;
  600. case SQLITE_NULL:
  601. ZVAL_NULL(*zargs[i + is_agg]);
  602. break;
  603. case SQLITE_BLOB:
  604. case SQLITE3_TEXT:
  605. default:
  606. ZVAL_STRINGL(*zargs[i + is_agg], (char*)sqlite3_value_text(argv[i]), sqlite3_value_bytes(argv[i]), 1);
  607. break;
  608. }
  609. }
  610. fc->fci.params = zargs;
  611. if ((ret = zend_call_function(&fc->fci, &fc->fcc TSRMLS_CC)) == FAILURE) {
  612. php_error_docref(NULL TSRMLS_CC, E_WARNING, "An error occurred while invoking the callback");
  613. }
  614. /* clean up the params */
  615. if (fake_argc) {
  616. for (i = is_agg; i < argc + is_agg; i++) {
  617. zval_ptr_dtor(zargs[i]);
  618. efree(zargs[i]);
  619. }
  620. if (is_agg) {
  621. zval_ptr_dtor(zargs[1]);
  622. efree(zargs[1]);
  623. }
  624. efree(zargs);
  625. }
  626. if (!is_agg || !argv) {
  627. /* only set the sqlite return value if we are a scalar function,
  628. * or if we are finalizing an aggregate */
  629. if (retval) {
  630. switch (Z_TYPE_P(retval)) {
  631. case IS_LONG:
  632. sqlite3_result_int(context, Z_LVAL_P(retval));
  633. break;
  634. case IS_NULL:
  635. sqlite3_result_null(context);
  636. break;
  637. case IS_DOUBLE:
  638. sqlite3_result_double(context, Z_DVAL_P(retval));
  639. break;
  640. default:
  641. convert_to_string_ex(&retval);
  642. sqlite3_result_text(context, Z_STRVAL_P(retval), Z_STRLEN_P(retval), SQLITE_TRANSIENT);
  643. break;
  644. }
  645. } else {
  646. sqlite3_result_error(context, "failed to invoke callback", 0);
  647. }
  648. if (agg_context && agg_context->zval_context) {
  649. zval_ptr_dtor(&agg_context->zval_context);
  650. }
  651. } else {
  652. /* we're stepping in an aggregate; the return value goes into
  653. * the context */
  654. if (agg_context && agg_context->zval_context) {
  655. zval_ptr_dtor(&agg_context->zval_context);
  656. }
  657. if (retval) {
  658. agg_context->zval_context = retval;
  659. retval = NULL;
  660. } else {
  661. agg_context->zval_context = NULL;
  662. }
  663. }
  664. if (retval) {
  665. zval_ptr_dtor(&retval);
  666. }
  667. return ret;
  668. }
  669. /* }}}*/
  670. static void php_sqlite3_callback_func(sqlite3_context *context, int argc, sqlite3_value **argv) /* {{{ */
  671. {
  672. php_sqlite3_func *func = (php_sqlite3_func *)sqlite3_user_data(context);
  673. TSRMLS_FETCH();
  674. sqlite3_do_callback(&func->afunc, func->func, argc, argv, context, 0 TSRMLS_CC);
  675. }
  676. /* }}}*/
  677. static void php_sqlite3_callback_step(sqlite3_context *context, int argc, sqlite3_value **argv) /* {{{ */
  678. {
  679. php_sqlite3_func *func = (php_sqlite3_func *)sqlite3_user_data(context);
  680. php_sqlite3_agg_context *agg_context = (php_sqlite3_agg_context *)sqlite3_aggregate_context(context, sizeof(php_sqlite3_agg_context));
  681. TSRMLS_FETCH();
  682. agg_context->row_count++;
  683. sqlite3_do_callback(&func->astep, func->step, argc, argv, context, 1 TSRMLS_CC);
  684. }
  685. /* }}} */
  686. static void php_sqlite3_callback_final(sqlite3_context *context) /* {{{ */
  687. {
  688. php_sqlite3_func *func = (php_sqlite3_func *)sqlite3_user_data(context);
  689. php_sqlite3_agg_context *agg_context = (php_sqlite3_agg_context *)sqlite3_aggregate_context(context, sizeof(php_sqlite3_agg_context));
  690. TSRMLS_FETCH();
  691. agg_context->row_count = 0;
  692. sqlite3_do_callback(&func->afini, func->fini, 0, NULL, context, 1 TSRMLS_CC);
  693. }
  694. /* }}} */
  695. /* {{{ proto bool SQLite3::createFunction(string name, mixed callback [, int argcount])
  696. Allows registration of a PHP function as a SQLite UDF that can be called within SQL statements. */
  697. PHP_METHOD(sqlite3, createFunction)
  698. {
  699. php_sqlite3_db_object *db_obj;
  700. zval *object = getThis();
  701. php_sqlite3_func *func;
  702. char *sql_func, *callback_name;
  703. int sql_func_len;
  704. zval *callback_func;
  705. long sql_func_num_args = -1;
  706. db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
  707. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  708. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|l", &sql_func, &sql_func_len, &callback_func, &sql_func_num_args) == FAILURE) {
  709. return;
  710. }
  711. if (!sql_func_len) {
  712. RETURN_FALSE;
  713. }
  714. if (!zend_is_callable(callback_func, 0, &callback_name TSRMLS_CC)) {
  715. php_sqlite3_error(db_obj, "Not a valid callback function %s", callback_name);
  716. efree(callback_name);
  717. RETURN_FALSE;
  718. }
  719. efree(callback_name);
  720. func = (php_sqlite3_func *)ecalloc(1, sizeof(*func));
  721. if (sqlite3_create_function(db_obj->db, sql_func, sql_func_num_args, SQLITE_UTF8, func, php_sqlite3_callback_func, NULL, NULL) == SQLITE_OK) {
  722. func->func_name = estrdup(sql_func);
  723. MAKE_STD_ZVAL(func->func);
  724. MAKE_COPY_ZVAL(&callback_func, func->func);
  725. func->argc = sql_func_num_args;
  726. func->next = db_obj->funcs;
  727. db_obj->funcs = func;
  728. RETURN_TRUE;
  729. }
  730. efree(func);
  731. RETURN_FALSE;
  732. }
  733. /* }}} */
  734. /* {{{ proto bool SQLite3::createAggregate(string name, mixed step, mixed final [, int argcount])
  735. Allows registration of a PHP function for use as an aggregate. */
  736. PHP_METHOD(sqlite3, createAggregate)
  737. {
  738. php_sqlite3_db_object *db_obj;
  739. zval *object = getThis();
  740. php_sqlite3_func *func;
  741. char *sql_func, *callback_name;
  742. int sql_func_len;
  743. zval *step_callback, *fini_callback;
  744. long sql_func_num_args = -1;
  745. db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
  746. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  747. 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) {
  748. return;
  749. }
  750. if (!sql_func_len) {
  751. RETURN_FALSE;
  752. }
  753. if (!zend_is_callable(step_callback, 0, &callback_name TSRMLS_CC)) {
  754. php_sqlite3_error(db_obj, "Not a valid callback function %s", callback_name);
  755. efree(callback_name);
  756. RETURN_FALSE;
  757. }
  758. efree(callback_name);
  759. if (!zend_is_callable(fini_callback, 0, &callback_name TSRMLS_CC)) {
  760. php_sqlite3_error(db_obj, "Not a valid callback function %s", callback_name);
  761. efree(callback_name);
  762. RETURN_FALSE;
  763. }
  764. efree(callback_name);
  765. func = (php_sqlite3_func *)ecalloc(1, sizeof(*func));
  766. 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) {
  767. func->func_name = estrdup(sql_func);
  768. MAKE_STD_ZVAL(func->step);
  769. MAKE_COPY_ZVAL(&step_callback, func->step);
  770. MAKE_STD_ZVAL(func->fini);
  771. MAKE_COPY_ZVAL(&fini_callback, func->fini);
  772. func->argc = sql_func_num_args;
  773. func->next = db_obj->funcs;
  774. db_obj->funcs = func;
  775. RETURN_TRUE;
  776. }
  777. efree(func);
  778. RETURN_FALSE;
  779. }
  780. /* }}} */
  781. typedef struct {
  782. sqlite3_blob *blob;
  783. size_t position;
  784. size_t size;
  785. } php_stream_sqlite3_data;
  786. static size_t php_sqlite3_stream_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
  787. {
  788. /* php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract; */
  789. return 0;
  790. }
  791. static size_t php_sqlite3_stream_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
  792. {
  793. php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract;
  794. if (sqlite3_stream->position + count >= sqlite3_stream->size) {
  795. count = sqlite3_stream->size - sqlite3_stream->position;
  796. stream->eof = 1;
  797. }
  798. if (count) {
  799. if (sqlite3_blob_read(sqlite3_stream->blob, buf, count, sqlite3_stream->position) != SQLITE_OK) {
  800. return 0;
  801. }
  802. sqlite3_stream->position += count;
  803. }
  804. return count;
  805. }
  806. static int php_sqlite3_stream_close(php_stream *stream, int close_handle TSRMLS_DC)
  807. {
  808. php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract;
  809. if (sqlite3_blob_close(sqlite3_stream->blob) != SQLITE_OK) {
  810. /* Error occured, but it still closed */
  811. }
  812. efree(sqlite3_stream);
  813. return 0;
  814. }
  815. static int php_sqlite3_stream_flush(php_stream *stream TSRMLS_DC)
  816. {
  817. /* do nothing */
  818. return 0;
  819. }
  820. /* {{{ */
  821. static int php_sqlite3_stream_seek(php_stream *stream, off_t offset, int whence, off_t *newoffs TSRMLS_DC)
  822. {
  823. php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract;
  824. switch(whence) {
  825. case SEEK_CUR:
  826. if (offset < 0) {
  827. if (sqlite3_stream->position < (size_t)(-offset)) {
  828. sqlite3_stream->position = 0;
  829. *newoffs = -1;
  830. return -1;
  831. } else {
  832. sqlite3_stream->position = sqlite3_stream->position + offset;
  833. *newoffs = sqlite3_stream->position;
  834. stream->eof = 0;
  835. return 0;
  836. }
  837. } else {
  838. if (sqlite3_stream->position + (size_t)(offset) > sqlite3_stream->size) {
  839. sqlite3_stream->position = sqlite3_stream->size;
  840. *newoffs = -1;
  841. return -1;
  842. } else {
  843. sqlite3_stream->position = sqlite3_stream->position + offset;
  844. *newoffs = sqlite3_stream->position;
  845. stream->eof = 0;
  846. return 0;
  847. }
  848. }
  849. case SEEK_SET:
  850. if (sqlite3_stream->size < (size_t)(offset)) {
  851. sqlite3_stream->position = sqlite3_stream->size;
  852. *newoffs = -1;
  853. return -1;
  854. } else {
  855. sqlite3_stream->position = offset;
  856. *newoffs = sqlite3_stream->position;
  857. stream->eof = 0;
  858. return 0;
  859. }
  860. case SEEK_END:
  861. if (offset > 0) {
  862. sqlite3_stream->position = sqlite3_stream->size;
  863. *newoffs = -1;
  864. return -1;
  865. } else if (sqlite3_stream->size < (size_t)(-offset)) {
  866. sqlite3_stream->position = 0;
  867. *newoffs = -1;
  868. return -1;
  869. } else {
  870. sqlite3_stream->position = sqlite3_stream->size + offset;
  871. *newoffs = sqlite3_stream->position;
  872. stream->eof = 0;
  873. return 0;
  874. }
  875. default:
  876. *newoffs = sqlite3_stream->position;
  877. return -1;
  878. }
  879. }
  880. /* }}} */
  881. static int php_sqlite3_stream_cast(php_stream *stream, int castas, void **ret TSRMLS_DC)
  882. {
  883. return FAILURE;
  884. }
  885. static int php_sqlite3_stream_stat(php_stream *stream, php_stream_statbuf *ssb TSRMLS_DC)
  886. {
  887. php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract;
  888. ssb->sb.st_size = sqlite3_stream->size;
  889. return 0;
  890. }
  891. static php_stream_ops php_stream_sqlite3_ops = {
  892. php_sqlite3_stream_write,
  893. php_sqlite3_stream_read,
  894. php_sqlite3_stream_close,
  895. php_sqlite3_stream_flush,
  896. "SQLite3",
  897. php_sqlite3_stream_seek,
  898. php_sqlite3_stream_cast,
  899. php_sqlite3_stream_stat
  900. };
  901. /* {{{ proto resource SQLite3::openBlob(string table, string column, int rowid [, string dbname])
  902. Open a blob as a stream which we can read / write to. */
  903. PHP_METHOD(sqlite3, openBlob)
  904. {
  905. php_sqlite3_db_object *db_obj;
  906. zval *object = getThis();
  907. char *table, *column, *dbname = "main";
  908. int table_len, column_len, dbname_len;
  909. long rowid, flags = 0;
  910. sqlite3_blob *blob = NULL;
  911. php_stream_sqlite3_data *sqlite3_stream;
  912. php_stream *stream;
  913. db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
  914. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  915. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssl|s", &table, &table_len, &column, &column_len, &rowid, &dbname, &dbname_len) == FAILURE) {
  916. return;
  917. }
  918. if (sqlite3_blob_open(db_obj->db, dbname, table, column, rowid, flags, &blob) != SQLITE_OK) {
  919. php_sqlite3_error(db_obj, "Unable to open blob: %s", sqlite3_errmsg(db_obj->db));
  920. RETURN_FALSE;
  921. }
  922. sqlite3_stream = emalloc(sizeof(php_stream_sqlite3_data));
  923. sqlite3_stream->blob = blob;
  924. sqlite3_stream->position = 0;
  925. sqlite3_stream->size = sqlite3_blob_bytes(blob);
  926. stream = php_stream_alloc(&php_stream_sqlite3_ops, sqlite3_stream, 0, "rb");
  927. if (stream) {
  928. php_stream_to_zval(stream, return_value);
  929. } else {
  930. RETURN_FALSE;
  931. }
  932. }
  933. /* }}} */
  934. /* {{{ proto bool SQLite3::enableExceptions([bool enableExceptions = false])
  935. Enables an exception error mode. */
  936. PHP_METHOD(sqlite3, enableExceptions)
  937. {
  938. php_sqlite3_db_object *db_obj;
  939. zval *object = getThis();
  940. zend_bool enableExceptions = 0;
  941. db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
  942. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &enableExceptions) == FAILURE) {
  943. return;
  944. }
  945. RETVAL_BOOL(db_obj->exception);
  946. db_obj->exception = enableExceptions;
  947. }
  948. /* }}} */
  949. /* {{{ proto int SQLite3Stmt::paramCount()
  950. Returns the number of parameters within the prepared statement. */
  951. PHP_METHOD(sqlite3stmt, paramCount)
  952. {
  953. php_sqlite3_stmt *stmt_obj;
  954. zval *object = getThis();
  955. stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
  956. if (zend_parse_parameters_none() == FAILURE) {
  957. return;
  958. }
  959. RETURN_LONG(sqlite3_bind_parameter_count(stmt_obj->stmt));
  960. }
  961. /* }}} */
  962. /* {{{ proto bool SQLite3Stmt::close()
  963. Closes the prepared statement. */
  964. PHP_METHOD(sqlite3stmt, close)
  965. {
  966. php_sqlite3_stmt *stmt_obj;
  967. zval *object = getThis();
  968. stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
  969. if (zend_parse_parameters_none() == FAILURE) {
  970. return;
  971. }
  972. zend_llist_del_element(&(stmt_obj->db_obj->free_list), object, (int (*)(void *, void *)) php_sqlite3_compare_stmt_zval_free);
  973. RETURN_TRUE;
  974. }
  975. /* }}} */
  976. /* {{{ proto bool SQLite3Stmt::reset()
  977. Reset the prepared statement to the state before it was executed, bindings still remain. */
  978. PHP_METHOD(sqlite3stmt, reset)
  979. {
  980. php_sqlite3_stmt *stmt_obj;
  981. zval *object = getThis();
  982. stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
  983. if (zend_parse_parameters_none() == FAILURE) {
  984. return;
  985. }
  986. if (sqlite3_reset(stmt_obj->stmt) != SQLITE_OK) {
  987. php_sqlite3_error(stmt_obj->db_obj, "Unable to reset statement: %s", sqlite3_errmsg(sqlite3_db_handle(stmt_obj->stmt)));
  988. RETURN_FALSE;
  989. }
  990. RETURN_TRUE;
  991. }
  992. /* }}} */
  993. /* {{{ proto bool SQLite3Stmt::clear()
  994. Clear all current bound parameters. */
  995. PHP_METHOD(sqlite3stmt, clear)
  996. {
  997. php_sqlite3_stmt *stmt_obj;
  998. zval *object = getThis();
  999. stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
  1000. if (zend_parse_parameters_none() == FAILURE) {
  1001. return;
  1002. }
  1003. if (sqlite3_clear_bindings(stmt_obj->stmt) != SQLITE_OK) {
  1004. php_sqlite3_error(stmt_obj->db_obj, "Unable to clear statement: %s", sqlite3_errmsg(sqlite3_db_handle(stmt_obj->stmt)));
  1005. RETURN_FALSE;
  1006. }
  1007. RETURN_TRUE;
  1008. }
  1009. /* }}} */
  1010. /* {{{ proto bool SQLite3Stmt::readOnly()
  1011. Returns true if a statement is definitely read only */
  1012. PHP_METHOD(sqlite3stmt, readOnly)
  1013. {
  1014. php_sqlite3_stmt *stmt_obj;
  1015. zval *object = getThis();
  1016. stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
  1017. if (zend_parse_parameters_none() == FAILURE) {
  1018. return;
  1019. }
  1020. #if SQLITE_VERSION_NUMBER >= 3007004
  1021. if (sqlite3_stmt_readonly(stmt_obj->stmt)) {
  1022. RETURN_TRUE;
  1023. }
  1024. #endif
  1025. RETURN_FALSE;
  1026. }
  1027. /* }}} */
  1028. static int register_bound_parameter_to_sqlite(struct php_sqlite3_bound_param *param, php_sqlite3_stmt *stmt TSRMLS_DC) /* {{{ */
  1029. {
  1030. HashTable *hash;
  1031. hash = stmt->bound_params;
  1032. if (!hash) {
  1033. ALLOC_HASHTABLE(hash);
  1034. zend_hash_init(hash, 13, NULL, sqlite3_param_dtor, 0);
  1035. stmt->bound_params = hash;
  1036. }
  1037. /* We need a : prefix to resolve a name to a parameter number */
  1038. if (param->name) {
  1039. if (param->name[0] != ':') {
  1040. /* pre-increment for character + 1 for null */
  1041. char *temp = emalloc(++param->name_len + 1);
  1042. temp[0] = ':';
  1043. memmove(temp+1, param->name, param->name_len);
  1044. param->name = temp;
  1045. } else {
  1046. param->name = estrndup(param->name, param->name_len);
  1047. }
  1048. /* do lookup*/
  1049. param->param_number = sqlite3_bind_parameter_index(stmt->stmt, param->name);
  1050. }
  1051. if (param->param_number < 1) {
  1052. efree(param->name);
  1053. return 0;
  1054. }
  1055. if (param->param_number >= 1) {
  1056. zend_hash_index_del(hash, param->param_number);
  1057. }
  1058. if (param->name) {
  1059. zend_hash_update(hash, param->name, param->name_len, param, sizeof(*param), NULL);
  1060. } else {
  1061. zend_hash_index_update(hash, param->param_number, param, sizeof(*param), NULL);
  1062. }
  1063. return 1;
  1064. }
  1065. /* }}} */
  1066. /* {{{ proto bool SQLite3Stmt::bindParam(int parameter_number, mixed parameter [, int type])
  1067. Bind Paramater to a stmt variable. */
  1068. PHP_METHOD(sqlite3stmt, bindParam)
  1069. {
  1070. php_sqlite3_stmt *stmt_obj;
  1071. zval *object = getThis();
  1072. struct php_sqlite3_bound_param param = {0};
  1073. stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
  1074. param.param_number = -1;
  1075. param.type = SQLITE3_TEXT;
  1076. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "lz|l", &param.param_number, &param.parameter, &param.type) == FAILURE) {
  1077. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|l", &param.name, &param.name_len, &param.parameter, &param.type) == FAILURE) {
  1078. return;
  1079. }
  1080. }
  1081. Z_ADDREF_P(param.parameter);
  1082. if (!register_bound_parameter_to_sqlite(&param, stmt_obj TSRMLS_CC)) {
  1083. if (param.parameter) {
  1084. zval_ptr_dtor(&(param.parameter));
  1085. param.parameter = NULL;
  1086. }
  1087. RETURN_FALSE;
  1088. }
  1089. RETURN_TRUE;
  1090. }
  1091. /* }}} */
  1092. /* {{{ proto bool SQLite3Stmt::bindValue(int parameter_number, mixed parameter [, int type])
  1093. Bind Value of a parameter to a stmt variable. */
  1094. PHP_METHOD(sqlite3stmt, bindValue)
  1095. {
  1096. php_sqlite3_stmt *stmt_obj;
  1097. zval *object = getThis();
  1098. struct php_sqlite3_bound_param param = {0};
  1099. stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
  1100. param.param_number = -1;
  1101. param.type = SQLITE3_TEXT;
  1102. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "lz/|l", &param.param_number, &param.parameter, &param.type) == FAILURE) {
  1103. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz/|l", &param.name, &param.name_len, &param.parameter, &param.type) == FAILURE) {
  1104. return;
  1105. }
  1106. }
  1107. Z_ADDREF_P(param.parameter);
  1108. if (!register_bound_parameter_to_sqlite(&param, stmt_obj TSRMLS_CC)) {
  1109. if (param.parameter) {
  1110. zval_ptr_dtor(&(param.parameter));
  1111. param.parameter = NULL;
  1112. }
  1113. RETURN_FALSE;
  1114. }
  1115. RETURN_TRUE;
  1116. }
  1117. /* }}} */
  1118. /* {{{ proto SQLite3Result SQLite3Stmt::execute()
  1119. Executes a prepared statement and returns a result set object. */
  1120. PHP_METHOD(sqlite3stmt, execute)
  1121. {
  1122. php_sqlite3_stmt *stmt_obj;
  1123. php_sqlite3_result *result;
  1124. zval *object = getThis();
  1125. int return_code = 0;
  1126. struct php_sqlite3_bound_param *param;
  1127. stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
  1128. if (zend_parse_parameters_none() == FAILURE) {
  1129. return;
  1130. }
  1131. SQLITE3_CHECK_INITIALIZED(stmt_obj->db_obj, stmt_obj->initialised, SQLite3)
  1132. if (stmt_obj->bound_params) {
  1133. zend_hash_internal_pointer_reset(stmt_obj->bound_params);
  1134. while (zend_hash_get_current_data(stmt_obj->bound_params, (void **)&param) == SUCCESS) {
  1135. /* If the ZVAL is null then it should be bound as that */
  1136. if (Z_TYPE_P(param->parameter) == IS_NULL) {
  1137. sqlite3_bind_null(stmt_obj->stmt, param->param_number);
  1138. zend_hash_move_forward(stmt_obj->bound_params);
  1139. continue;
  1140. }
  1141. switch (param->type) {
  1142. case SQLITE_INTEGER:
  1143. convert_to_long(param->parameter);
  1144. sqlite3_bind_int(stmt_obj->stmt, param->param_number, Z_LVAL_P(param->parameter));
  1145. break;
  1146. case SQLITE_FLOAT:
  1147. /* convert_to_double(param->parameter);*/
  1148. sqlite3_bind_double(stmt_obj->stmt, param->param_number, Z_DVAL_P(param->parameter));
  1149. break;
  1150. case SQLITE_BLOB:
  1151. {
  1152. php_stream *stream = NULL;
  1153. int blength;
  1154. char *buffer = NULL;
  1155. if (Z_TYPE_P(param->parameter) == IS_RESOURCE) {
  1156. php_stream_from_zval_no_verify(stream, &param->parameter);
  1157. if (stream == NULL) {
  1158. php_sqlite3_error(stmt_obj->db_obj, "Unable to read stream for parameter %ld", param->param_number);
  1159. RETURN_FALSE;
  1160. }
  1161. blength = php_stream_copy_to_mem(stream, (void *)&buffer, PHP_STREAM_COPY_ALL, 0);
  1162. } else {
  1163. convert_to_string(param->parameter);
  1164. blength = Z_STRLEN_P(param->parameter);
  1165. buffer = Z_STRVAL_P(param->parameter);
  1166. }
  1167. sqlite3_bind_blob(stmt_obj->stmt, param->param_number, buffer, blength, SQLITE_TRANSIENT);
  1168. if (stream) {
  1169. pefree(buffer, 0);
  1170. }
  1171. break;
  1172. }
  1173. case SQLITE3_TEXT:
  1174. convert_to_string(param->parameter);
  1175. sqlite3_bind_text(stmt_obj->stmt, param->param_number, Z_STRVAL_P(param->parameter), Z_STRLEN_P(param->parameter), SQLITE_STATIC);
  1176. break;
  1177. case SQLITE_NULL:
  1178. sqlite3_bind_null(stmt_obj->stmt, param->param_number);
  1179. break;
  1180. default:
  1181. php_sqlite3_error(stmt_obj->db_obj, "Unknown parameter type: %ld for parameter %ld", param->type, param->param_number);
  1182. RETURN_FALSE;
  1183. }
  1184. zend_hash_move_forward(stmt_obj->bound_params);
  1185. }
  1186. }
  1187. return_code = sqlite3_step(stmt_obj->stmt);
  1188. switch (return_code) {
  1189. case SQLITE_ROW: /* Valid Row */
  1190. case SQLITE_DONE: /* Valid but no results */
  1191. {
  1192. sqlite3_reset(stmt_obj->stmt);
  1193. object_init_ex(return_value, php_sqlite3_result_entry);
  1194. result = (php_sqlite3_result *)zend_object_store_get_object(return_value TSRMLS_CC);
  1195. Z_ADDREF_P(object);
  1196. result->is_prepared_statement = 1;
  1197. result->db_obj = stmt_obj->db_obj;
  1198. result->stmt_obj = stmt_obj;
  1199. result->stmt_obj_zval = getThis();
  1200. break;
  1201. }
  1202. case SQLITE_ERROR:
  1203. sqlite3_reset(stmt_obj->stmt);
  1204. default:
  1205. php_sqlite3_error(stmt_obj->db_obj, "Unable to execute statement: %s", sqlite3_errmsg(sqlite3_db_handle(stmt_obj->stmt)));
  1206. zval_dtor(return_value);
  1207. RETURN_FALSE;
  1208. }
  1209. return;
  1210. }
  1211. /* }}} */
  1212. /* {{{ proto int SQLite3Stmt::__construct(SQLite3 dbobject, String Statement)
  1213. __constructor for SQLite3Stmt. */
  1214. PHP_METHOD(sqlite3stmt, __construct)
  1215. {
  1216. php_sqlite3_stmt *stmt_obj;
  1217. php_sqlite3_db_object *db_obj;
  1218. zval *object = getThis();
  1219. zval *db_zval;
  1220. char *sql;
  1221. int sql_len, errcode;
  1222. zend_error_handling error_handling;
  1223. php_sqlite3_free_list *free_item;
  1224. stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
  1225. zend_replace_error_handling(EH_THROW, NULL, &error_handling TSRMLS_CC);
  1226. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Os", &db_zval, php_sqlite3_sc_entry, &sql, &sql_len) == FAILURE) {
  1227. zend_restore_error_handling(&error_handling TSRMLS_CC);
  1228. return;
  1229. }
  1230. db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(db_zval TSRMLS_CC);
  1231. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  1232. zend_restore_error_handling(&error_handling TSRMLS_CC);
  1233. if (!sql_len) {
  1234. RETURN_FALSE;
  1235. }
  1236. stmt_obj->db_obj = db_obj;
  1237. stmt_obj->db_obj_zval = db_zval;
  1238. Z_ADDREF_P(db_zval);
  1239. errcode = sqlite3_prepare_v2(db_obj->db, sql, sql_len, &(stmt_obj->stmt), NULL);
  1240. if (errcode != SQLITE_OK) {
  1241. php_sqlite3_error(db_obj, "Unable to prepare statement: %d, %s", errcode, sqlite3_errmsg(db_obj->db));
  1242. zval_dtor(return_value);
  1243. RETURN_FALSE;
  1244. }
  1245. stmt_obj->initialised = 1;
  1246. free_item = emalloc(sizeof(php_sqlite3_free_list));
  1247. free_item->stmt_obj = stmt_obj;
  1248. free_item->stmt_obj_zval = getThis();
  1249. zend_llist_add_element(&(db_obj->free_list), &free_item);
  1250. }
  1251. /* }}} */
  1252. /* {{{ proto int SQLite3Result::numColumns()
  1253. Number of columns in the result set. */
  1254. PHP_METHOD(sqlite3result, numColumns)
  1255. {
  1256. php_sqlite3_result *result_obj;
  1257. zval *object = getThis();
  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_none() == FAILURE) {
  1261. return;
  1262. }
  1263. RETURN_LONG(sqlite3_column_count(result_obj->stmt_obj->stmt));
  1264. }
  1265. /* }}} */
  1266. /* {{{ proto string SQLite3Result::columnName(int column)
  1267. Returns the name of the nth column. */
  1268. PHP_METHOD(sqlite3result, columnName)
  1269. {
  1270. php_sqlite3_result *result_obj;
  1271. zval *object = getThis();
  1272. long column = 0;
  1273. char *column_name;
  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", &column) == FAILURE) {
  1277. return;
  1278. }
  1279. column_name = (char*) sqlite3_column_name(result_obj->stmt_obj->stmt, column);
  1280. if (column_name == NULL) {
  1281. RETURN_FALSE;
  1282. }
  1283. RETVAL_STRING(column_name, 1);
  1284. }
  1285. /* }}} */
  1286. /* {{{ proto int SQLite3Result::columnType(int column)
  1287. Returns the type of the nth column. */
  1288. PHP_METHOD(sqlite3result, columnType)
  1289. {
  1290. php_sqlite3_result *result_obj;
  1291. zval *object = getThis();
  1292. long column = 0;
  1293. result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
  1294. SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
  1295. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &column) == FAILURE) {
  1296. return;
  1297. }
  1298. if (result_obj->complete) {
  1299. RETURN_FALSE;
  1300. }
  1301. RETURN_LONG(sqlite3_column_type(result_obj->stmt_obj->stmt, column));
  1302. }
  1303. /* }}} */
  1304. /* {{{ proto array SQLite3Result::fetchArray([int mode])
  1305. Fetch a result row as both an associative or numerically indexed array or both. */
  1306. PHP_METHOD(sqlite3result, fetchArray)
  1307. {
  1308. php_sqlite3_result *result_obj;
  1309. zval *object = getThis();
  1310. int i, ret;
  1311. long mode = PHP_SQLITE3_BOTH;
  1312. result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
  1313. SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
  1314. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &mode) == FAILURE) {
  1315. return;
  1316. }
  1317. ret = sqlite3_step(result_obj->stmt_obj->stmt);
  1318. switch (ret) {
  1319. case SQLITE_ROW:
  1320. /* If there was no return value then just skip fetching */
  1321. if (!return_value_used) {
  1322. return;
  1323. }
  1324. array_init(return_value);
  1325. for (i = 0; i < sqlite3_data_count(result_obj->stmt_obj->stmt); i++) {
  1326. zval *data;
  1327. data = sqlite_value_to_zval(result_obj->stmt_obj->stmt, i);
  1328. if (mode & PHP_SQLITE3_NUM) {
  1329. add_index_zval(return_value, i, data);
  1330. }
  1331. if (mode & PHP_SQLITE3_ASSOC) {
  1332. if (mode & PHP_SQLITE3_NUM) {
  1333. Z_ADDREF_P(data);
  1334. }
  1335. add_assoc_zval(return_value, (char*)sqlite3_column_name(result_obj->stmt_obj->stmt, i), data);
  1336. }
  1337. }
  1338. break;
  1339. case SQLITE_DONE:
  1340. result_obj->complete = 1;
  1341. RETURN_FALSE;
  1342. break;
  1343. default:
  1344. php_sqlite3_error(result_obj->db_obj, "Unable to execute statement: %s", sqlite3_errmsg(sqlite3_db_handle(result_obj->stmt_obj->stmt)));
  1345. }
  1346. }
  1347. /* }}} */
  1348. /* {{{ proto bool SQLite3Result::reset()
  1349. Resets the result set back to the first row. */
  1350. PHP_METHOD(sqlite3result, reset)
  1351. {
  1352. php_sqlite3_result *result_obj;
  1353. zval *object = getThis();
  1354. result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
  1355. SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
  1356. if (zend_parse_parameters_none() == FAILURE) {
  1357. return;
  1358. }
  1359. if (sqlite3_reset(result_obj->stmt_obj->stmt) != SQLITE_OK) {
  1360. RETURN_FALSE;
  1361. }
  1362. result_obj->complete = 0;
  1363. RETURN_TRUE;
  1364. }
  1365. /* }}} */
  1366. /* {{{ proto bool SQLite3Result::finalize()
  1367. Closes the result set. */
  1368. PHP_METHOD(sqlite3result, finalize)
  1369. {
  1370. php_sqlite3_result *result_obj;
  1371. zval *object = getThis();
  1372. result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
  1373. SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
  1374. if (zend_parse_parameters_none() == FAILURE) {
  1375. return;
  1376. }
  1377. /* We need to finalize an internal statement */
  1378. if (result_obj->is_prepared_statement == 0) {
  1379. zend_llist_del_element(&(result_obj->db_obj->free_list), result_obj->stmt_obj_zval,
  1380. (int (*)(void *, void *)) php_sqlite3_compare_stmt_zval_free);
  1381. } else {
  1382. sqlite3_reset(result_obj->stmt_obj->stmt);
  1383. }
  1384. RETURN_TRUE;
  1385. }
  1386. /* }}} */
  1387. /* {{{ proto int SQLite3Result::__construct()
  1388. __constructor for SQLite3Result. */
  1389. PHP_METHOD(sqlite3result, __construct)
  1390. {
  1391. zend_throw_exception(zend_exception_get_default(TSRMLS_C), "SQLite3Result cannot be directly instantiated", 0 TSRMLS_CC);
  1392. }
  1393. /* }}} */
  1394. /* {{{ arginfo */
  1395. ZEND_BEGIN_ARG_INFO(arginfo_sqlite3_open, 0)
  1396. ZEND_ARG_INFO(0, filename)
  1397. ZEND_ARG_INFO(0, flags)
  1398. ZEND_ARG_INFO(0, encryption_key)
  1399. ZEND_END_ARG_INFO()
  1400. ZEND_BEGIN_ARG_INFO(arginfo_sqlite3_busytimeout, 0)
  1401. ZEND_ARG_INFO(0, ms)
  1402. ZEND_END_ARG_INFO()
  1403. #ifndef SQLITE_OMIT_LOAD_EXTENSION
  1404. ZEND_BEGIN_ARG_INFO(arginfo_sqlite3_loadextension, 0)
  1405. ZEND_ARG_INFO(0, shared_library)
  1406. ZEND_END_ARG_INFO()
  1407. #endif
  1408. ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_escapestring, 0, 0, 1)
  1409. ZEND_ARG_INFO(0, value)
  1410. ZEND_END_ARG_INFO()
  1411. ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_query, 0, 0, 1)
  1412. ZEND_ARG_INFO(0, query)
  1413. ZEND_END_ARG_INFO()
  1414. ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_querysingle, 0, 0, 1)
  1415. ZEND_ARG_INFO(0, query)
  1416. ZEND_ARG_INFO(0, entire_row)
  1417. ZEND_END_ARG_INFO()
  1418. ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_createfunction, 0, 0, 2)
  1419. ZEND_ARG_INFO(0, name)
  1420. ZEND_ARG_INFO(0, callback)
  1421. ZEND_ARG_INFO(0, argument_count)
  1422. ZEND_END_ARG_INFO()
  1423. ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_createaggregate, 0, 0, 3)
  1424. ZEND_ARG_INFO(0, name)
  1425. ZEND_ARG_INFO(0, step_callback)
  1426. ZEND_ARG_INFO(0, final_callback)
  1427. ZEND_ARG_INFO(0, argument_count)
  1428. ZEND_END_ARG_INFO()
  1429. ZEND_BEGIN_ARG_INFO_EX(argingo_sqlite3_openblob, 0, 0, 3)
  1430. ZEND_ARG_INFO(0, table)
  1431. ZEND_ARG_INFO(0, column)
  1432. ZEND_ARG_INFO(0, rowid)
  1433. ZEND_ARG_INFO(0, dbname)
  1434. ZEND_END_ARG_INFO()
  1435. ZEND_BEGIN_ARG_INFO_EX(argingo_sqlite3_enableexceptions, 0, 0, 1)
  1436. ZEND_ARG_INFO(0, enableExceptions)
  1437. ZEND_END_ARG_INFO()
  1438. ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3stmt_bindparam, 0, 0, 2)
  1439. ZEND_ARG_INFO(0, param_number)
  1440. ZEND_ARG_INFO(1, param)
  1441. ZEND_ARG_INFO(0, type)
  1442. ZEND_END_ARG_INFO()
  1443. ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3stmt_bindvalue, 0, 0, 2)
  1444. ZEND_ARG_INFO(0, param_number)
  1445. ZEND_ARG_INFO(0, param)
  1446. ZEND_ARG_INFO(0, type)
  1447. ZEND_END_ARG_INFO()
  1448. ZEND_BEGIN_ARG_INFO(arginfo_sqlite3stmt_construct, 1)
  1449. ZEND_ARG_INFO(0, sqlite3)
  1450. ZEND_END_ARG_INFO()
  1451. ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3result_columnname, 0, 0, 1)
  1452. ZEND_ARG_INFO(0, column_number)
  1453. ZEND_END_ARG_INFO()
  1454. ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3result_columntyp

Large files files are truncated, but you can click here to view the full file