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