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