diff options
author | Wez Furlong <wez@php.net> | 2004-09-19 10:55:41 +0000 |
---|---|---|
committer | Wez Furlong <wez@php.net> | 2004-09-19 10:55:41 +0000 |
commit | 2f161ab79d4af52fc7b21e98c0450f3900f83bce (patch) | |
tree | b7d2d09b45173dadeeada5e32457efe32a36715f /ext/pdo_sqlite/sqlite_statement.c | |
parent | db7af4d8f00d489384406d5ebbfc66c42a74c6f4 (diff) | |
download | php-git-2f161ab79d4af52fc7b21e98c0450f3900f83bce.tar.gz |
First cut at a PDO driver for SQLite 3.x
Features:
- native prepare/execute and bound parameters.
- finally supports binary data (via bound parameter api)
- full unicode/utf-8 support
Missing:
- UDF functions
- authorizer hooks for safe_mode/open_basedir restrictions
You need to download, compile and install sqlite3 yourself; we're not bundling
it (at least, not yet).
Diffstat (limited to 'ext/pdo_sqlite/sqlite_statement.c')
-rw-r--r-- | ext/pdo_sqlite/sqlite_statement.c | 217 |
1 files changed, 217 insertions, 0 deletions
diff --git a/ext/pdo_sqlite/sqlite_statement.c b/ext/pdo_sqlite/sqlite_statement.c new file mode 100644 index 0000000000..c68106494f --- /dev/null +++ b/ext/pdo_sqlite/sqlite_statement.c @@ -0,0 +1,217 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2004 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.0 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_0.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> | + +----------------------------------------------------------------------+ +*/ + +/* $Id$ */ + +#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 TSRMLS_DC) +{ + pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data; + int i; + + if (S->stmt) { + sqlite3_finalize(S->stmt); + S->stmt = NULL; + } + efree(S); + return 1; +} + +static int pdo_sqlite_stmt_execute(pdo_stmt_t *stmt TSRMLS_DC) +{ + pdo_dbh_t *dbh = stmt->dbh; + pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data; + pdo_sqlite_db_handle *H = S->H; + int i; + + i = sqlite3_step(S->stmt); + switch (i) { + case SQLITE_ROW: + S->pre_fetched = 1; + stmt->column_count = sqlite3_data_count(S->stmt); + return 1; + + case SQLITE_DONE: + stmt->column_count = sqlite3_data_count(S->stmt); + sqlite3_reset(S->stmt); + return 1; + + case SQLITE_ERROR: + 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 TSRMLS_DC) +{ + pdo_dbh_t *dbh = stmt->dbh; + pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data; + pdo_sqlite_db_handle *H = S->H; + int i; + + switch (event_type) { + case PDO_PARAM_EVT_EXEC_PRE: + if (param->is_param) { + switch (param->param_type) { + case PDO_PARAM_LOB: + case PDO_PARAM_STMT: + return 0; + case PDO_PARAM_STR: + default: + if (param->paramno == -1) { + param->paramno = sqlite3_bind_parameter_index(S->stmt, param->name); + } + convert_to_string(param->parameter); + i = sqlite3_bind_blob(S->stmt, param->paramno, + Z_STRVAL_P(param->parameter), + Z_STRLEN_P(param->parameter), + SQLITE_STATIC); + if (i == SQLITE_OK) + return 1; + pdo_sqlite_error_stmt(stmt); + return 0; + } + } + break; + + default: + ; + } + return 1; +} + +static int pdo_sqlite_stmt_fetch(pdo_stmt_t *stmt TSRMLS_DC) +{ + 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; + } + i = sqlite3_step(S->stmt); + switch (i) { + case SQLITE_ROW: + return 1; + + case SQLITE_DONE: + sqlite3_reset(S->stmt); + return 0; + + default: + pdo_sqlite_error_stmt(stmt); + return 0; + } +} + +static int pdo_sqlite_stmt_describe(pdo_stmt_t *stmt, int colno TSRMLS_DC) +{ + pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data; + + if(colno >= sqlite3_data_count(S->stmt)) { + /* error invalid column */ + pdo_sqlite_error_stmt(stmt); + return 0; + } + + stmt->columns[colno].name = estrdup(sqlite3_column_name(S->stmt, colno)); + stmt->columns[colno].namelen = strlen(stmt->columns[colno].name); + stmt->columns[colno].maxlen = 0xffffffff; + stmt->columns[colno].precision = 0; + + switch (sqlite3_column_type(S->stmt, colno)) { + case SQLITE_INTEGER: + case SQLITE_FLOAT: + case SQLITE_TEXT: + case SQLITE_BLOB: + stmt->columns[colno].param_type = PDO_PARAM_STR; + break; + case SQLITE_NULL: + stmt->columns[colno].param_type = PDO_PARAM_NULL; + break; + } + + return 1; +} + +static int pdo_sqlite_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, unsigned long *len TSRMLS_DC) +{ + 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 = strlen(*ptr); + return 1; + } +} + +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 +}; + +/* + * 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 + */ |