Skip to content

Commit

Permalink
First cut at a PDO driver for SQLite 3.x
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
wez committed Sep 19, 2004
1 parent db7af4d commit 2f161ab
Show file tree
Hide file tree
Showing 9 changed files with 784 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ext/pdo_sqlite/CREDITS
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,2 @@
SQLite 3.x driver for PDO
Wez Furlong
Empty file added ext/pdo_sqlite/EXPERIMENTAL
Empty file.
55 changes: 55 additions & 0 deletions ext/pdo_sqlite/config.m4
Original file line number Original file line Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions ext/pdo_sqlite/config.w32
Original file line number Original file line Diff line number Diff line change
@@ -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');
}

122 changes: 122 additions & 0 deletions ext/pdo_sqlite/pdo_sqlite.c
Original file line number Original file line Diff line number Diff line change
@@ -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
*/
71 changes: 71 additions & 0 deletions ext/pdo_sqlite/php_pdo_sqlite.h
Original file line number Original file line Diff line number Diff line change
@@ -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 */

51 changes: 51 additions & 0 deletions ext/pdo_sqlite/php_pdo_sqlite_int.h
Original file line number Original file line Diff line number Diff line change
@@ -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
Loading

0 comments on commit 2f161ab

Please sign in to comment.