sqlite3.c 68 KB

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