]> granicus.if.org Git - php/commitdiff
First cut at a PDO driver for SQLite 3.x
authorWez Furlong <wez@php.net>
Sun, 19 Sep 2004 10:55:41 +0000 (10:55 +0000)
committerWez Furlong <wez@php.net>
Sun, 19 Sep 2004 10:55:41 +0000 (10:55 +0000)
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).

ext/pdo_sqlite/CREDITS [new file with mode: 0644]
ext/pdo_sqlite/EXPERIMENTAL [new file with mode: 0644]
ext/pdo_sqlite/config.m4 [new file with mode: 0644]
ext/pdo_sqlite/config.w32 [new file with mode: 0644]
ext/pdo_sqlite/pdo_sqlite.c [new file with mode: 0644]
ext/pdo_sqlite/php_pdo_sqlite.h [new file with mode: 0644]
ext/pdo_sqlite/php_pdo_sqlite_int.h [new file with mode: 0644]
ext/pdo_sqlite/sqlite_driver.c [new file with mode: 0644]
ext/pdo_sqlite/sqlite_statement.c [new file with mode: 0644]

diff --git a/ext/pdo_sqlite/CREDITS b/ext/pdo_sqlite/CREDITS
new file mode 100644 (file)
index 0000000..0a6c145
--- /dev/null
@@ -0,0 +1,2 @@
+SQLite 3.x driver for PDO
+Wez Furlong
diff --git a/ext/pdo_sqlite/EXPERIMENTAL b/ext/pdo_sqlite/EXPERIMENTAL
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/ext/pdo_sqlite/config.m4 b/ext/pdo_sqlite/config.m4
new file mode 100644 (file)
index 0000000..ce723c0
--- /dev/null
@@ -0,0 +1,55 @@
+dnl $Id$
+dnl config.m4 for extension pdo_sqlite
+
+PHP_ARG_WITH(pdo-sqlite, for sqlite 3 driver for PDO,
+[  --with-pdo-sqlite             Include PDO sqlite 3 support])
+
+if test "$PHP_PDO_SQLITE" != "no"; then
+  SEARCH_PATH="/usr/local /usr"     # you might want to change this
+  SEARCH_FOR="/include/sqlite3.h"  # you most likely want to change this
+  if test -r $PHP_PDO_SQLITE/$SEARCH_FOR; then # path given as parameter
+    PDO_SQLITE_DIR=$PHP_PDO_SQLITE
+  else # search default path list
+    AC_MSG_CHECKING([for sqlite3 files in default path])
+    for i in $SEARCH_PATH ; do
+      if test -r $i/$SEARCH_FOR; then
+        PDO_SQLITE_DIR=$i
+        AC_MSG_RESULT(found in $i)
+      fi
+    done
+  fi
+  if test -z "$PDO_SQLITE_DIR"; then
+    AC_MSG_RESULT([not found])
+    AC_MSG_ERROR([Please reinstall the sqlite3 distribution])
+  fi
+
+  PHP_ADD_INCLUDE($PDO_SQLITE_DIR/include)
+
+  LIBNAME=sqlite3
+  LIBSYMBOL=sqlite3_open
+
+  PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL,
+  [
+    PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $PDO_SQLITE_DIR/lib, PDO_SQLITE_SHARED_LIBADD)
+    AC_DEFINE(HAVE_PDO_SQLITELIB,1,[ ])
+  ],[
+    AC_MSG_ERROR([wrong sqlite lib version or lib not found])
+  ],[
+    -L$PDO_SQLITE_DIR/lib -lm -ldl
+  ])
+  PHP_SUBST(PDO_SQLITE_SHARED_LIBADD)
+
+  AC_MSG_CHECKING([for PDO includes])
+  if test -f $prefix/include/php/ext/pdo/php_pdo_driver.h; then
+       pdo_inc_path=$prefix/include/php/ext
+  elif test -f $abs_srcdir/include/php/ext/pdo/php_pdo_driver.h; then
+       pdo_inc_path=$abs_srcdir/ext
+  elif test -f ext/pdo/php_pdo_driver.h; then
+       pdo_inc_path=ext
+  else
+       AC_MSG_ERROR([Cannot find php_pdo_driver.h.])
+  fi
+  AC_MSG_RESULT($pdo_inc_path)
+
+  PHP_NEW_EXTENSION(pdo_sqlite, pdo_sqlite.c sqlite_driver.c sqlite_statement.c, $ext_shared,,-I$pdo_inc_path)
+fi
diff --git a/ext/pdo_sqlite/config.w32 b/ext/pdo_sqlite/config.w32
new file mode 100644 (file)
index 0000000..8f72e94
--- /dev/null
@@ -0,0 +1,15 @@
+// $Id$
+// vim:ft=javascript
+
+ARG_WITH("pdo_sqlite", "for pdo_sqlite support", "no");
+
+if (PHP_PDO_SQLITE != "no") {
+       if (CHECK_LIB("libsqlite3.lib", "pdo_sqlite", PHP_PDO_SQLITE) &&
+                       CHECK_HEADER_ADD_INCLUDE("sqlite3.h", "CFLAGS_PDO_SQLITE", PHP_PDO_SQLITE)) {
+               EXTENSION("pdo_sqlite", "pdo_sqlite.c sqlite_driver.c sqlite_statement.c", "/I ..\\pecl");
+       } else {
+               WARNING("pdo_sqlite not enabled; libraries and headers not found");
+       }
+       ADD_EXTENSION_DEP('pdo_sqlite', 'pdo');
+}
+
diff --git a/ext/pdo_sqlite/pdo_sqlite.c b/ext/pdo_sqlite/pdo_sqlite.c
new file mode 100644 (file)
index 0000000..072246f
--- /dev/null
@@ -0,0 +1,122 @@
+/*
+  +----------------------------------------------------------------------+
+  | 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"
+#include "zend_exceptions.h"
+
+#define PHP_PDO_SQLITE_MODULE_VERSION  "0.1-dev"
+
+/* {{{ pdo_sqlite_functions[] */
+function_entry pdo_sqlite_functions[] = {
+       {NULL, NULL, NULL}
+};
+/* }}} */
+
+/* {{{ pdo_sqlite_module_entry
+ */
+zend_module_entry pdo_sqlite_module_entry = {
+       STANDARD_MODULE_HEADER,
+       "pdo_sqlite",
+       pdo_sqlite_functions,
+       PHP_MINIT(pdo_sqlite),
+       PHP_MSHUTDOWN(pdo_sqlite),
+       PHP_RINIT(pdo_sqlite),
+       PHP_RSHUTDOWN(pdo_sqlite),
+       PHP_MINFO(pdo_sqlite),
+       PHP_PDO_SQLITE_MODULE_VERSION,
+       STANDARD_MODULE_PROPERTIES
+};
+/* }}} */
+
+#ifdef COMPILE_DL_PDO_SQLITE
+ZEND_GET_MODULE(pdo_sqlite)
+#endif
+
+/* {{{ PHP_MINIT_FUNCTION */
+PHP_MINIT_FUNCTION(pdo_sqlite)
+{
+       /* If you have INI entries, uncomment these lines 
+       ZEND_INIT_MODULE_GLOBALS(pdo_sqlite, php_pdo_sqlite_init_globals, NULL);
+       REGISTER_INI_ENTRIES();
+       */
+       return SUCCESS;
+}
+/* }}} */
+
+/* {{{ PHP_MSHUTDOWN_FUNCTION
+ */
+PHP_MSHUTDOWN_FUNCTION(pdo_sqlite)
+{
+       /* uncomment this line if you have INI entries
+       UNREGISTER_INI_ENTRIES();
+       */
+       return SUCCESS;
+}
+/* }}} */
+
+/* Remove if there's nothing to do at request start */
+/* {{{ PHP_RINIT_FUNCTION
+ */
+PHP_RINIT_FUNCTION(pdo_sqlite)
+{
+       return php_pdo_register_driver(&pdo_sqlite_driver);
+}
+/* }}} */
+
+/* Remove if there's nothing to do at request end */
+/* {{{ PHP_RSHUTDOWN_FUNCTION
+ */
+PHP_RSHUTDOWN_FUNCTION(pdo_sqlite)
+{
+       php_pdo_unregister_driver(&pdo_sqlite_driver);
+       return SUCCESS;
+}
+/* }}} */
+
+/* {{{ PHP_MINFO_FUNCTION
+ */
+PHP_MINFO_FUNCTION(pdo_sqlite)
+{
+       php_info_print_table_start();
+       php_info_print_table_header(2, "PDO Driver for SQLite 3.x", "enabled");
+       php_info_print_table_row(2, "PECL Module version", PHP_PDO_SQLITE_MODULE_VERSION " $Id$");
+       php_info_print_table_row(2, "SQLite Library", sqlite3_libversion());
+       php_info_print_table_end();
+}
+/* }}} */
+
+/*
+ * 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
+ */
diff --git a/ext/pdo_sqlite/php_pdo_sqlite.h b/ext/pdo_sqlite/php_pdo_sqlite.h
new file mode 100644 (file)
index 0000000..65d0b23
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+  +----------------------------------------------------------------------+
+  | 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$ */
+
+
+#ifndef PHP_PDO_SQLITE_H
+#define PHP_PDO_SQLITE_H
+
+extern zend_module_entry pdo_sqlite_module_entry;
+#define phpext_pdo_sqlite_ptr &pdo_sqlite_module_entry
+
+#ifdef PHP_WIN32
+#define PHP_PDO_SQLITE_API __declspec(dllexport)
+#else
+#define PHP_PDO_SQLITE_API
+#endif
+
+#ifdef ZTS
+#include "TSRM.h"
+#endif
+
+PHP_MINIT_FUNCTION(pdo_sqlite);
+PHP_MSHUTDOWN_FUNCTION(pdo_sqlite);
+PHP_RINIT_FUNCTION(pdo_sqlite);
+PHP_RSHUTDOWN_FUNCTION(pdo_sqlite);
+PHP_MINFO_FUNCTION(pdo_sqlite);
+
+/* 
+       Declare any global variables you may need between the BEGIN
+       and END macros here:     
+
+ZEND_BEGIN_MODULE_GLOBALS(pdo_sqlite)
+       long  global_value;
+       char *global_string;
+ZEND_END_MODULE_GLOBALS(pdo_sqlite)
+*/
+
+/* In every utility function you add that needs to use variables 
+   in php_pdo_sqlite_globals, call TSRMLS_FETCH(); after declaring other 
+   variables used by that function, or better yet, pass in TSRMLS_CC
+   after the last function argument and declare your utility function
+   with TSRMLS_DC after the last declared argument.  Always refer to
+   the globals in your function as PDO_SQLITE_G(variable).  You are 
+   encouraged to rename these macros something shorter, see
+   examples in any other php module directory.
+*/
+
+#ifdef ZTS
+#define PDO_SQLITE_G(v) TSRMG(pdo_sqlite_globals_id, zend_pdo_sqlite_globals *, v)
+#else
+#define PDO_SQLITE_G(v) (pdo_sqlite_globals.v)
+#endif
+
+#endif /* PHP_PDO_SQLITE_H */
+
diff --git a/ext/pdo_sqlite/php_pdo_sqlite_int.h b/ext/pdo_sqlite/php_pdo_sqlite_int.h
new file mode 100644 (file)
index 0000000..7e2a5ef
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+  +----------------------------------------------------------------------+
+  | 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$ */
+
+#ifndef PHP_PDO_SQLITE_INT_H
+#define PHP_PDO_SQLITE_INT_H
+
+#include <sqlite3.h>
+
+typedef struct {
+       const char *file;
+       int line;
+       unsigned int errcode;
+       char *errmsg;
+} pdo_sqlite_error_info;
+
+typedef struct {
+       sqlite3 *db;
+       pdo_sqlite_error_info einfo;
+} pdo_sqlite_db_handle;
+
+typedef struct {
+       pdo_sqlite_db_handle    *H;
+       sqlite3_stmt *stmt;
+       unsigned pre_fetched:1;
+} pdo_sqlite_stmt;
+
+extern pdo_driver_t pdo_sqlite_driver;
+
+extern int _pdo_sqlite_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, const char *file, int line TSRMLS_DC);
+#define pdo_sqlite_error(s) _pdo_sqlite_error(s, NULL, __FILE__, __LINE__ TSRMLS_CC)
+#define pdo_sqlite_error_stmt(s) _pdo_sqlite_error(stmt->dbh, stmt, __FILE__, __LINE__ TSRMLS_CC)
+
+extern struct pdo_stmt_methods sqlite_stmt_methods;
+#endif
diff --git a/ext/pdo_sqlite/sqlite_driver.c b/ext/pdo_sqlite/sqlite_driver.c
new file mode 100644 (file)
index 0000000..bba16ec
--- /dev/null
@@ -0,0 +1,251 @@
+/*
+  +----------------------------------------------------------------------+
+  | 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"
+#include "zend_exceptions.h"
+
+int _pdo_sqlite_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, const char *file, int line TSRMLS_DC) /* {{{ */
+{
+       pdo_sqlite_db_handle *H = (pdo_sqlite_db_handle *)dbh->driver_data;
+       enum pdo_error_type *pdo_err = stmt ? &stmt->error_code : &dbh->error_code;
+       pdo_sqlite_error_info *einfo = &H->einfo;
+
+       einfo->errcode = sqlite3_errcode(H->db);
+       einfo->file = file;
+       einfo->line = line;
+
+       if (einfo->errcode != SQLITE_OK) {
+               einfo->errmsg = (char*)sqlite3_errmsg(H->db);
+       } else { /* no error */
+               *pdo_err = PDO_ERR_NONE;
+               return 0;
+       }
+       switch (einfo->errcode) {
+               case SQLITE_NOTFOUND:
+                       *pdo_err = PDO_ERR_NOT_FOUND;
+                       break;  
+
+               case SQLITE_INTERRUPT:
+                       *pdo_err = PDO_ERR_DISCONNECTED;
+                       break;
+
+               case SQLITE_NOLFS:
+                       *pdo_err = PDO_ERR_NOT_IMPLEMENTED;
+                       break;
+
+               case SQLITE_TOOBIG:
+                       *pdo_err = PDO_ERR_TRUNCATED;
+                       break;
+       
+               case SQLITE_CONSTRAINT:
+                       *pdo_err = PDO_ERR_CONSTRAINT;
+                       break;
+
+               case SQLITE_ERROR: /* empty query */
+                       *pdo_err = PDO_ERR_SYNTAX;
+                       break;
+
+               default:
+                       *pdo_err = PDO_ERR_CANT_MAP;
+                       break;
+       }
+
+       if (!dbh->methods) {
+               zend_throw_exception_ex(php_pdo_get_exception(), *pdo_err TSRMLS_CC, "[%d] %s",
+                               einfo->errcode, einfo->errmsg);
+       }
+       
+       return einfo->errcode;
+}
+/* }}} */
+
+static int pdo_sqlite_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info TSRMLS_DC)
+{
+       pdo_sqlite_db_handle *H = (pdo_sqlite_db_handle *)dbh->driver_data;
+       pdo_sqlite_error_info *einfo = &H->einfo;
+
+       if (einfo->errcode) {
+               add_next_index_long(info, einfo->errcode);
+               add_next_index_string(info, einfo->errmsg, 1);
+       }
+
+       return 1;
+}
+
+static int sqlite_handle_closer(pdo_dbh_t *dbh TSRMLS_DC) /* {{{ */
+{
+       pdo_sqlite_db_handle *H = (pdo_sqlite_db_handle *)dbh->driver_data;
+       
+       if (H) {
+               if (H->db) {
+                       sqlite3_close(H->db);
+                       H->db = NULL;
+               }
+               pefree(H, dbh->is_persistent);
+               dbh->driver_data = NULL;
+       }
+       return 0;
+}
+/* }}} */
+
+static int sqlite_handle_preparer(pdo_dbh_t *dbh, const char *sql, long sql_len, pdo_stmt_t *stmt, long options, zval *driver_options TSRMLS_DC)
+{
+       pdo_sqlite_db_handle *H = (pdo_sqlite_db_handle *)dbh->driver_data;
+       pdo_sqlite_stmt *S = ecalloc(1, sizeof(pdo_sqlite_stmt));
+       int i;
+       const char *tail;
+
+       S->H = H;
+       stmt->driver_data = S;
+       stmt->methods = &sqlite_stmt_methods;
+
+       i = sqlite3_prepare(H->db, sql, sql_len, &S->stmt, &tail);
+       if (i == SQLITE_OK)
+               return 1;
+
+       pdo_sqlite_error(dbh);
+
+       return 0;
+}
+
+static long sqlite_handle_doer(pdo_dbh_t *dbh, const char *sql, long sql_len TSRMLS_DC)
+{
+       pdo_sqlite_db_handle *H = (pdo_sqlite_db_handle *)dbh->driver_data;
+       char *errmsg = NULL;
+
+       if (sqlite3_exec(H->db, sql, NULL, NULL, &errmsg) != SQLITE_OK) {
+               pdo_sqlite_error(dbh);
+               if (errmsg)
+                       sqlite3_free(errmsg);
+
+               return -1;
+       } else {
+               return sqlite3_changes(H->db);
+       }
+}
+
+static long pdo_sqlite_last_insert_id(pdo_dbh_t *dbh TSRMLS_DC)
+{
+       pdo_sqlite_db_handle *H = (pdo_sqlite_db_handle *)dbh->driver_data;
+
+       return (long) sqlite3_last_insert_rowid(H->db);
+}
+
+/* NB: doesn't handle binary strings... use prepared stmts for that */
+static int sqlite_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, int unquotedlen, char **quoted, int *quotedlen  TSRMLS_DC)
+{
+       pdo_sqlite_db_handle *H = (pdo_sqlite_db_handle *)dbh->driver_data;
+       *quoted = emalloc(2*unquotedlen + 3);
+       sqlite3_snprintf(2*unquotedlen + 3, *quoted, "'%q'", unquoted);
+       *quotedlen = strlen(*quoted);
+       return 1;
+}
+
+static int pdo_sqlite_get_attribute(pdo_dbh_t *dbh, long attr, zval *return_value TSRMLS_DC)
+{
+       pdo_sqlite_db_handle *H = (pdo_sqlite_db_handle *)dbh->driver_data;
+
+       switch (attr) {
+               case PDO_ATTR_CLIENT_VERSION:
+               case PDO_ATTR_SERVER_VERSION:
+                       ZVAL_STRING(return_value, (char *)sqlite3_libversion(), 1);
+                       break;
+               
+               default:
+                       return 0;       
+       }
+
+       return 1;
+}
+
+static struct pdo_dbh_methods sqlite_methods = {
+       sqlite_handle_closer,
+       sqlite_handle_preparer,
+       sqlite_handle_doer,
+       sqlite_handle_quoter,
+       NULL,
+       NULL,
+       NULL,
+       NULL,
+       pdo_sqlite_last_insert_id,
+       pdo_sqlite_fetch_error_func,
+       pdo_sqlite_get_attribute
+};
+
+static int pdo_sqlite_handle_factory(pdo_dbh_t *dbh, zval *driver_options TSRMLS_DC) /* {{{ */
+{
+       pdo_sqlite_db_handle *H;
+       int i, ret = 0;
+       long timeout = 60;
+
+       H = pecalloc(1, sizeof(pdo_sqlite_db_handle), dbh->is_persistent);
+       
+       H->einfo.errcode = 0;
+       H->einfo.errmsg = NULL;
+       dbh->driver_data = H;
+
+       i = sqlite3_open(dbh->data_source, &H->db);
+
+       if (i != SQLITE_OK) {
+               pdo_sqlite_error(dbh);
+               goto cleanup;
+       }
+
+       if (driver_options) {
+               timeout = pdo_attr_lval(driver_options, PDO_ATTR_TIMEOUT, timeout TSRMLS_CC);
+       }
+       sqlite_busy_timeout(H->db, timeout * 1000);
+
+       dbh->alloc_own_columns = 1;
+       dbh->supports_placeholders = PDO_PLACEHOLDER_POSITIONAL;
+       dbh->max_escaped_char_length = 2;
+
+       ret = 1;
+       
+cleanup:
+       dbh->methods = &sqlite_methods;
+       
+       return ret;
+}
+/* }}} */
+
+pdo_driver_t pdo_sqlite_driver = {
+       PDO_DRIVER_HEADER(sqlite),
+       pdo_sqlite_handle_factory
+};
+
+/*
+ * 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
+ */
diff --git a/ext/pdo_sqlite/sqlite_statement.c b/ext/pdo_sqlite/sqlite_statement.c
new file mode 100644 (file)
index 0000000..c681064
--- /dev/null
@@ -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
+ */