sqlite3.c 66 KB

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