123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414 |
- /*
- +----------------------------------------------------------------------+
- | PHP Version 7 |
- +----------------------------------------------------------------------+
- | Copyright (c) 1997-2018 The PHP Group |
- +----------------------------------------------------------------------+
- | This source file is subject to version 3.01 of the PHP license, |
- | that is bundled with this package in the file LICENSE, and is |
- | available through the world-wide-web at the following url: |
- | http://www.php.net/license/3_01.txt |
- | If you did not receive a copy of the PHP license and are unable to |
- | obtain it through the world-wide-web, please send a note to |
- | license@php.net so we can mail you a copy immediately. |
- +----------------------------------------------------------------------+
- | Author: Wez Furlong <wez@php.net> |
- +----------------------------------------------------------------------+
- */
- #ifdef HAVE_CONFIG_H
- #include "config.h"
- #endif
- #include "php.h"
- #include "php_ini.h"
- #include "ext/standard/info.h"
- #include "pdo/php_pdo.h"
- #include "pdo/php_pdo_driver.h"
- #include "php_pdo_sqlite.h"
- #include "php_pdo_sqlite_int.h"
- static int pdo_sqlite_stmt_dtor(pdo_stmt_t *stmt)
- {
- pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
- if (S->stmt) {
- sqlite3_finalize(S->stmt);
- S->stmt = NULL;
- }
- efree(S);
- return 1;
- }
- /**
- * Change the column count on the statement.
- *
- * Since PHP 7.2 sqlite3_prepare_v2 is used which auto recompile prepared statement on schema change.
- * Instead of raise an error on schema change, the result set will change, and the statement's columns must be updated.
- *
- * See bug #78192
- */
- static void pdo_sqlite_stmt_set_column_count(pdo_stmt_t *stmt, int new_count)
- {
- /* Columns not yet "described" */
- if (!stmt->columns) {
- stmt->column_count = new_count;
- return;
- }
- /*
- * The column count has not changed : no need to reload columns description
- * Note: Do not handle attribute name change, without column count change
- */
- if (new_count == stmt->column_count) {
- return;
- }
- /* Free previous columns to force reload description */
- int i;
- for (i = 0; i < stmt->column_count; i++) {
- if (stmt->columns[i].name) {
- zend_string_release(stmt->columns[i].name);
- stmt->columns[i].name = NULL;
- }
- }
- efree(stmt->columns);
- stmt->columns = NULL;
- stmt->column_count = new_count;
- }
- static int pdo_sqlite_stmt_execute(pdo_stmt_t *stmt)
- {
- pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
- if (stmt->executed && !S->done) {
- sqlite3_reset(S->stmt);
- }
- S->done = 0;
- switch (sqlite3_step(S->stmt)) {
- case SQLITE_ROW:
- S->pre_fetched = 1;
- pdo_sqlite_stmt_set_column_count(stmt, sqlite3_data_count(S->stmt));
- return 1;
- case SQLITE_DONE:
- pdo_sqlite_stmt_set_column_count(stmt, sqlite3_column_count(S->stmt));
- stmt->row_count = sqlite3_changes(S->H->db);
- sqlite3_reset(S->stmt);
- S->done = 1;
- return 1;
- case SQLITE_ERROR:
- sqlite3_reset(S->stmt);
- case SQLITE_MISUSE:
- case SQLITE_BUSY:
- default:
- pdo_sqlite_error_stmt(stmt);
- return 0;
- }
- }
- static int pdo_sqlite_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param,
- enum pdo_param_event event_type)
- {
- pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
- zval *parameter;
- switch (event_type) {
- case PDO_PARAM_EVT_EXEC_PRE:
- if (stmt->executed && !S->done) {
- sqlite3_reset(S->stmt);
- S->done = 1;
- }
- if (param->is_param) {
- if (param->paramno == -1) {
- param->paramno = sqlite3_bind_parameter_index(S->stmt, ZSTR_VAL(param->name)) - 1;
- }
- switch (PDO_PARAM_TYPE(param->param_type)) {
- case PDO_PARAM_STMT:
- return 0;
- case PDO_PARAM_NULL:
- if (sqlite3_bind_null(S->stmt, param->paramno + 1) == SQLITE_OK) {
- return 1;
- }
- pdo_sqlite_error_stmt(stmt);
- return 0;
- case PDO_PARAM_INT:
- case PDO_PARAM_BOOL:
- if (Z_ISREF(param->parameter)) {
- parameter = Z_REFVAL(param->parameter);
- } else {
- parameter = ¶m->parameter;
- }
- if (Z_TYPE_P(parameter) == IS_NULL) {
- if (sqlite3_bind_null(S->stmt, param->paramno + 1) == SQLITE_OK) {
- return 1;
- }
- } else {
- convert_to_long(parameter);
- #if ZEND_LONG_MAX > 2147483647
- if (SQLITE_OK == sqlite3_bind_int64(S->stmt, param->paramno + 1, Z_LVAL_P(parameter))) {
- return 1;
- }
- #else
- if (SQLITE_OK == sqlite3_bind_int(S->stmt, param->paramno + 1, Z_LVAL_P(parameter))) {
- return 1;
- }
- #endif
- }
- pdo_sqlite_error_stmt(stmt);
- return 0;
- case PDO_PARAM_LOB:
- if (Z_ISREF(param->parameter)) {
- parameter = Z_REFVAL(param->parameter);
- } else {
- parameter = ¶m->parameter;
- }
- if (Z_TYPE_P(parameter) == IS_RESOURCE) {
- php_stream *stm = NULL;
- php_stream_from_zval_no_verify(stm, parameter);
- if (stm) {
- zend_string *mem = php_stream_copy_to_mem(stm, PHP_STREAM_COPY_ALL, 0);
- zval_ptr_dtor(parameter);
- ZVAL_STR(parameter, mem ? mem : ZSTR_EMPTY_ALLOC());
- } else {
- pdo_raise_impl_error(stmt->dbh, stmt, "HY105", "Expected a stream resource");
- return 0;
- }
- } else if (Z_TYPE_P(parameter) == IS_NULL) {
- if (sqlite3_bind_null(S->stmt, param->paramno + 1) == SQLITE_OK) {
- return 1;
- }
- pdo_sqlite_error_stmt(stmt);
- return 0;
- } else {
- convert_to_string(parameter);
- }
- if (SQLITE_OK == sqlite3_bind_blob(S->stmt, param->paramno + 1,
- Z_STRVAL_P(parameter),
- Z_STRLEN_P(parameter),
- SQLITE_STATIC)) {
- return 1;
- }
- return 0;
- case PDO_PARAM_STR:
- default:
- if (Z_ISREF(param->parameter)) {
- parameter = Z_REFVAL(param->parameter);
- } else {
- parameter = ¶m->parameter;
- }
- if (Z_TYPE_P(parameter) == IS_NULL) {
- if (sqlite3_bind_null(S->stmt, param->paramno + 1) == SQLITE_OK) {
- return 1;
- }
- } else {
- convert_to_string(parameter);
- if (SQLITE_OK == sqlite3_bind_text(S->stmt, param->paramno + 1,
- Z_STRVAL_P(parameter),
- Z_STRLEN_P(parameter),
- SQLITE_STATIC)) {
- return 1;
- }
- }
- pdo_sqlite_error_stmt(stmt);
- return 0;
- }
- }
- break;
- default:
- ;
- }
- return 1;
- }
- static int pdo_sqlite_stmt_fetch(pdo_stmt_t *stmt,
- enum pdo_fetch_orientation ori, zend_long offset)
- {
- pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
- int i;
- if (!S->stmt) {
- return 0;
- }
- if (S->pre_fetched) {
- S->pre_fetched = 0;
- return 1;
- }
- if (S->done) {
- return 0;
- }
- i = sqlite3_step(S->stmt);
- switch (i) {
- case SQLITE_ROW:
- return 1;
- case SQLITE_DONE:
- S->done = 1;
- sqlite3_reset(S->stmt);
- return 0;
- case SQLITE_ERROR:
- sqlite3_reset(S->stmt);
- default:
- pdo_sqlite_error_stmt(stmt);
- return 0;
- }
- }
- static int pdo_sqlite_stmt_describe(pdo_stmt_t *stmt, int colno)
- {
- pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
- const char *str;
- if(colno >= sqlite3_column_count(S->stmt)) {
- /* error invalid column */
- pdo_sqlite_error_stmt(stmt);
- return 0;
- }
- str = sqlite3_column_name(S->stmt, colno);
- stmt->columns[colno].name = zend_string_init(str, strlen(str), 0);
- stmt->columns[colno].maxlen = SIZE_MAX;
- stmt->columns[colno].precision = 0;
- switch (sqlite3_column_type(S->stmt, colno)) {
- case SQLITE_INTEGER:
- case SQLITE_FLOAT:
- case SQLITE3_TEXT:
- case SQLITE_BLOB:
- case SQLITE_NULL:
- default:
- stmt->columns[colno].param_type = PDO_PARAM_STR;
- break;
- }
- return 1;
- }
- static int pdo_sqlite_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, zend_ulong *len, int *caller_frees)
- {
- pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
- if (!S->stmt) {
- return 0;
- }
- if(colno >= sqlite3_data_count(S->stmt)) {
- /* error invalid column */
- pdo_sqlite_error_stmt(stmt);
- return 0;
- }
- switch (sqlite3_column_type(S->stmt, colno)) {
- case SQLITE_NULL:
- *ptr = NULL;
- *len = 0;
- return 1;
- case SQLITE_BLOB:
- *ptr = (char*)sqlite3_column_blob(S->stmt, colno);
- *len = sqlite3_column_bytes(S->stmt, colno);
- return 1;
- default:
- *ptr = (char*)sqlite3_column_text(S->stmt, colno);
- *len = sqlite3_column_bytes(S->stmt, colno);
- return 1;
- }
- }
- static int pdo_sqlite_stmt_col_meta(pdo_stmt_t *stmt, zend_long colno, zval *return_value)
- {
- pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
- const char *str;
- zval flags;
- if (!S->stmt) {
- return FAILURE;
- }
- if(colno >= sqlite3_column_count(S->stmt)) {
- /* error invalid column */
- pdo_sqlite_error_stmt(stmt);
- return FAILURE;
- }
- array_init(return_value);
- array_init(&flags);
- switch (sqlite3_column_type(S->stmt, colno)) {
- case SQLITE_NULL:
- add_assoc_string(return_value, "native_type", "null");
- break;
- case SQLITE_FLOAT:
- add_assoc_string(return_value, "native_type", "double");
- break;
- case SQLITE_BLOB:
- add_next_index_string(&flags, "blob");
- case SQLITE_TEXT:
- add_assoc_string(return_value, "native_type", "string");
- break;
- case SQLITE_INTEGER:
- add_assoc_string(return_value, "native_type", "integer");
- break;
- }
- str = sqlite3_column_decltype(S->stmt, colno);
- if (str) {
- add_assoc_string(return_value, "sqlite:decl_type", (char *)str);
- }
- #ifdef SQLITE_ENABLE_COLUMN_METADATA
- str = sqlite3_column_table_name(S->stmt, colno);
- if (str) {
- add_assoc_string(return_value, "table", (char *)str);
- }
- #endif
- add_assoc_zval(return_value, "flags", &flags);
- return SUCCESS;
- }
- static int pdo_sqlite_stmt_cursor_closer(pdo_stmt_t *stmt)
- {
- pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
- sqlite3_reset(S->stmt);
- return 1;
- }
- const struct pdo_stmt_methods sqlite_stmt_methods = {
- pdo_sqlite_stmt_dtor,
- pdo_sqlite_stmt_execute,
- pdo_sqlite_stmt_fetch,
- pdo_sqlite_stmt_describe,
- pdo_sqlite_stmt_get_col,
- pdo_sqlite_stmt_param_hook,
- NULL, /* set_attr */
- NULL, /* get_attr */
- pdo_sqlite_stmt_col_meta,
- NULL, /* next_rowset */
- pdo_sqlite_stmt_cursor_closer
- };
- /*
- * Local variables:
- * tab-width: 4
- * c-basic-offset: 4
- * End:
- * vim600: noet sw=4 ts=4 fdm=marker
- * vim<600: noet sw=4 ts=4
- */
|