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.

2199 lines
62 KiB

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