diff options
author | Lorry Tar Creator <lorry-tar-importer@baserock.org> | 2013-03-14 05:42:27 +0000 |
---|---|---|
committer | <> | 2013-04-03 16:25:08 +0000 |
commit | c4dd7a1a684490673e25aaf4fabec5df138854c4 (patch) | |
tree | 4d57c44caae4480efff02b90b9be86f44bf25409 /ext/pdo_sqlite | |
download | php2-master.tar.gz |
Imported from /home/lorry/working-area/delta_php2/php-5.4.13.tar.bz2.HEADphp-5.4.13master
Diffstat (limited to 'ext/pdo_sqlite')
33 files changed, 2634 insertions, 0 deletions
diff --git a/ext/pdo_sqlite/CREDITS b/ext/pdo_sqlite/CREDITS new file mode 100644 index 0000000..0a6c145 --- /dev/null +++ b/ext/pdo_sqlite/CREDITS @@ -0,0 +1,2 @@ +SQLite 3.x driver for PDO +Wez Furlong diff --git a/ext/pdo_sqlite/config.m4 b/ext/pdo_sqlite/config.m4 new file mode 100644 index 0000000..b2b70a5 --- /dev/null +++ b/ext/pdo_sqlite/config.m4 @@ -0,0 +1,112 @@ +dnl $Id$ +dnl config.m4 for extension pdo_sqlite +dnl vim:et:sw=2:ts=2: + +PHP_ARG_WITH(pdo-sqlite, for sqlite 3 support for PDO, +[ --without-pdo-sqlite[=DIR] + PDO: sqlite 3 support. DIR is the sqlite base + install directory [BUNDLED]], $PHP_PDO) + +if test "$PHP_PDO_SQLITE" != "no"; then + + if test "$PHP_PDO" = "no" && test "$ext_shared" = "no"; then + AC_MSG_ERROR([PDO is not enabled! Add --enable-pdo to your configure line.]) + fi + + ifdef([PHP_CHECK_PDO_INCLUDES], + [ + PHP_CHECK_PDO_INCLUDES + ],[ + AC_MSG_CHECKING([for PDO includes]) + if test -f $abs_srcdir/include/php/ext/pdo/php_pdo_driver.h; then + pdo_cv_inc_path=$abs_srcdir/ext + elif test -f $abs_srcdir/ext/pdo/php_pdo_driver.h; then + pdo_cv_inc_path=$abs_srcdir/ext + elif test -f $prefix/include/php/ext/pdo/php_pdo_driver.h; then + pdo_cv_inc_path=$prefix/include/php/ext + else + AC_MSG_ERROR([Cannot find php_pdo_driver.h.]) + fi + AC_MSG_RESULT($pdo_cv_inc_path) + ]) + + php_pdo_sqlite_sources_core="pdo_sqlite.c sqlite_driver.c sqlite_statement.c" + + if test "$PHP_PDO_SQLITE" != "yes"; then + SEARCH_PATH="$PHP_PDO_SQLITE /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/$PHP_LIBDIR, 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/$PHP_LIBDIR -lm + ]) + PHP_CHECK_LIBRARY(sqlite3,sqlite3_key,[ + AC_DEFINE(HAVE_SQLITE3_KEY,1, [have commercial sqlite3 with crypto support]) + ]) + + PHP_SUBST(PDO_SQLITE_SHARED_LIBADD) + PHP_NEW_EXTENSION(pdo_sqlite, $php_pdo_sqlite_sources_core, $ext_shared,,-I$pdo_cv_inc_path) + else + # use bundled libs + if test "$enable_maintainer_zts" = "yes"; then + threadsafe_flags="-DSQLITE_THREADSAFE=1" + else + threadsafe_flags="-DSQLITE_THREADSAFE=0" + fi + + other_flags="-DSQLITE_ENABLE_FTS3=1 -DSQLITE_CORE=1 -DSQLITE_ENABLE_COLUMN_METADATA=1" + + dnl As long as intl is not shared we can have ICU support + if test "$PHP_INTL" = "yes" && test "$PHP_INTL_SHARED" != "yes"; then + other_flags="$other_flags -DSQLITE_ENABLE_ICU=1" + fi + + if test "$PHP_SQLITE3" != "yes"; then + PHP_ADD_SOURCES(PHP_EXT_DIR(sqlite3), libsqlite/sqlite3.c) + fi + + PHP_NEW_EXTENSION(pdo_sqlite, + $php_pdo_sqlite_sources_core, + $ext_shared,,-DPDO_SQLITE_BUNDLED=1 $other_flags $threadsafe_flags -I$pdo_cv_inc_path) + + PHP_SUBST(PDO_SQLITE_SHARED_LIBADD) + PHP_ADD_EXTENSION_DEP(pdo_sqlite, sqlite3) + PHP_ADD_INCLUDE($abs_srcdir/ext/sqlite3/libsqlite) + + AC_CHECK_FUNCS(usleep nanosleep) + AC_CHECK_HEADERS(time.h) + fi + + dnl Solaris fix + PHP_CHECK_LIBRARY(rt, fdatasync, [PHP_ADD_LIBRARY(rt,, PDO_SQLITE_SHARED_LIBADD)]) + + ifdef([PHP_ADD_EXTENSION_DEP], + [ + PHP_ADD_EXTENSION_DEP(pdo_sqlite, pdo) + ]) +fi diff --git a/ext/pdo_sqlite/config.w32 b/ext/pdo_sqlite/config.w32 new file mode 100644 index 0000000..4f6a5d4 --- /dev/null +++ b/ext/pdo_sqlite/config.w32 @@ -0,0 +1,26 @@ +// $Id$ +// vim:ft=javascript + +ARG_WITH("pdo-sqlite", "for pdo_sqlite support", "no"); + +if (PHP_PDO_SQLITE != "no") { + EXTENSION("pdo_sqlite", "pdo_sqlite.c sqlite_driver.c sqlite_statement.c", null, "/DSQLITE_THREADSAFE=" + (PHP_ZTS == "yes" ? "1" : "0") + " /D SQLITE_ENABLE_FTS3=1 /D SQLITE_ENABLE_COLUMN_METADATA=1 /D SQLITE_CORE=1 /I" + configure_module_dirname + "/../sqlite3/libsqlite /I" + configure_module_dirname); + + ADD_EXTENSION_DEP('pdo_sqlite', 'pdo'); + // If pdo_sqlite is static, and sqlite3 is also static, then we don't add a second copy of the sqlite3 libs + if (PHP_PDO_SQLITE_SHARED || PHP_SQLITE3_SHARED || PHP_SQLITE3 == 'no') { + ADD_SOURCES(configure_module_dirname + "/../sqlite3/libsqlite", "sqlite3.c", "pdo_sqlite"); + } +} + +ARG_WITH("pdo-sqlite-external", "for pdo_sqlite support from an external dll", "no"); +if (PHP_PDO_SQLITE_EXTERNAL != "no") { + if (CHECK_HEADER_ADD_INCLUDE("sqlite3.h", "CFLAGS_PDO_SQLITE_EXTERNAL", PHP_PDO_SQLITE_EXTERNAL + ";" + PHP_PHP_BUILD + "\\include\\sqlite3") && + CHECK_LIB("sqlite3-import.lib", "pdo_sqlite_external", PHP_PDO_SQLITE_EXTERNAL + ";" + PHP_PHP_BUILD + "\\lib") + && ADD_EXTENSION_DEP('pdo_sqlite_external', 'pdo') ) { + EXTENSION("pdo_sqlite_external", "pdo_sqlite.c sqlite_driver.c sqlite_statement.c", null, "/I" + configure_module_dirname, null, "ext\\pdo_sqlite_external"); + } else { + WARNING("pdo-sqlite-external support can't be enabled, libraries or headers are missing") + PHP_PDO_SQLITE_EXTERNAL = "no" + } +} diff --git a/ext/pdo_sqlite/package2.xml b/ext/pdo_sqlite/package2.xml new file mode 100644 index 0000000..9b58229 --- /dev/null +++ b/ext/pdo_sqlite/package2.xml @@ -0,0 +1,203 @@ +<?xml version="1.0" encoding="UTF-8"?> +<package packagerversion="1.4.5" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 +http://pear.php.net/dtd/tasks-1.0.xsd +http://pear.php.net/dtd/package-2.0 +http://pear.php.net/dtd/package-2.0.xsd"> + <name>PDO_SQLITE</name> + <channel>pecl.php.net</channel> + <summary>SQLite v3 Interface driver for PDO</summary> + <description>This extension provides an SQLite v3 driver for PDO. +SQLite V3 is NOT compatible with the bundled SQLite 2 in PHP 5, but is a significant +step forwards, featuring complete utf-8 support, native support for blobs, +native support for prepared statements with bound parameters and improved +concurrency. + + </description> + <lead> + <name>Wez Furlong</name> + <user>wez</user> + <email>wez@php.net</email> + <active>yes</active> + </lead> + <date>2006-05-01</date> + <version> + <release>1.0.1</release> + <api>1.0.1</api> + </version> + <stability> + <release>stable</release> + <api>stable</api> + </stability> + <license uri="http://www.php.net/license">PHP</license> + <notes> +- Updated libsqlite in ext/pdo_sqlite to 3.2.8. (Ilia) +- Upgraded to new package2 format +- Fixed PECL Bug #5633; build issues +- Added sqliteCreateFunction() and sqliteCreateAggregate() +- Fixed PECL Bug #3452; problem when first row of a result set contains a NULL value. +- Upgraded bundled sqlite to 3.1.3 +- setting PDO_ATTR_TIMEOUT controls the busy timeout +- Fixed PECL Bug #3391; cannot bind NULL parameters +- Fixed build problem when building the bundled sqlite library + +Windows binary: +http://pecl4win.php.net/ext.php/php_pdo_sqlite.dll + </notes> + <contents> + <dir name="/"> + <dir name="sqlite"> + <dir name="src"> + <file name="alter.c" role="src" /> + <file name="analyze.c" role="src" /> + <file name="attach.c" role="src" /> + <file name="auth.c" role="src" /> + <file name="btree.c" role="src" /> + <file name="btree.h" role="src" /> + <file name="build.c" role="src" /> + <file name="callback.c" role="src" /> + <file name="date.c" role="src" /> + <file name="delete.c" role="src" /> + <file name="expr.c" role="src" /> + <file name="func.c" role="src" /> + <file name="hash.c" role="src" /> + <file name="hash.h" role="src" /> + <file name="insert.c" role="src" /> + <file name="keywordhash.h" role="src" /> + <file name="legacy.c" role="src" /> + <file name="main.c" role="src" /> + <file name="md5.c" role="src" /> + <file name="opcodes.c" role="src" /> + <file name="opcodes.h" role="src" /> + <file name="os.h" role="src" /> + <file name="os.c" role="src" /> + <file name="os_common.h" role="src" /> + <file name="os_test.c" role="src" /> + <file name="os_test.h" role="src" /> + <file name="os_unix.c" role="src" /> + <file name="os_unix.h" role="src" /> + <file name="os_win.c" role="src" /> + <file name="os_win.h" role="src" /> + <file name="pager.c" role="src" /> + <file name="pager.h" role="src" /> + <file name="parse.c" role="src" /> + <file name="parse.h" role="src" /> + <file name="parse.y" role="src" /> + <file name="pragma.c" role="src" /> + <file name="prepare.c" role="src" /> + <file name="printf.c" role="src" /> + <file name="random.c" role="src" /> + <file name="select.c" role="src" /> + <file name="shell.c" role="src" /> + <file name="sqlite.h.in" role="src" /> + <file name="sqliteInt.h" role="src" /> + <file name="table.c" role="src" /> + <file name="tclsqlite.c" role="src" /> + <file name="test1.c" role="src" /> + <file name="test2.c" role="src" /> + <file name="test3.c" role="src" /> + <file name="test4.c" role="src" /> + <file name="test5.c" role="src" /> + <file name="tokenize.c" role="src" /> + <file name="trigger.c" role="src" /> + <file name="update.c" role="src" /> + <file name="utf.c" role="src" /> + <file name="util.c" role="src" /> + <file name="vacuum.c" role="src" /> + <file name="vdbe.c" role="src" /> + <file name="vdbe.h" role="src" /> + <file name="vdbeapi.c" role="src" /> + <file name="vdbeaux.c" role="src" /> + <file name="vdbefifo.c" role="src" /> + <file name="vdbeInt.h" role="src" /> + <file name="vdbemem.c" role="src" /> + <file name="where.c" role="src" /> + <file name="vtab.c" role="src" /> + <file name="loadext.c" role="src" /> + <file name="complete.c" role="src" /> + <file name="sqlite3ext.h" role="src" /> + <file name="btmutex.c" role="src" /> + <file name="btreeInt.h" role="src" /> + <file name="experimental.c" role="src" /> + <file name="fault.c" role="src" /> + <file name="journal.c" role="src" /> + <file name="limits.h" role="src" /> + <file name="malloc.c" role="src" /> + <file name="mem1.c" role="src" /> + <file name="mutex.c" role="src" /> + <file name="mutex.h" role="src" /> + <file name="mutex_unix.c" role="src" /> + <file name="mutex_w32.c" role="src" /> + <file name="sqliteLimit.h" role="src" /> + <file name="vdbeblob.c" role="src" /> + </dir> <!-- //sqlite/src --> + <dir name="tool"> + <file name="diffdb.c" role="src" /> + <file name="lemon.c" role="src" /> + <file name="lempar.c" role="src" /> + <file name="memleak.awk" role="src" /> + <file name="memleak2.awk" role="src" /> + <file name="memleak3.tcl" role="src" /> + <file name="mkkeywordhash.c" role="src" /> + <file name="mkopts.tcl" role="src" /> + <file name="opcodeDoc.awk" role="src" /> + <file name="report1.txt" role="src" /> + <file name="showdb.c" role="src" /> + <file name="showjournal.c" role="src" /> + <file name="spaceanal.tcl" role="src" /> + <file name="space_used.tcl" role="src" /> + <file name="speedtest.tcl" role="src" /> + <file name="speedtest2.tcl" role="src" /> + </dir> <!-- //sqlite/tool --> + <file name="aclocal.m4" role="src" /> + <file name="config.guess" role="src" /> + <file name="config.sub" role="src" /> + <file name="configure" role="src" /> + <file name="configure.ac" role="src" /> + <file name="install-sh" role="src" /> + <file name="ltmain.sh" role="src" /> + <file name="main.mk" role="src" /> + <file name="Makefile.in" role="src" /> + <file name="Makefile.linux-gcc" role="src" /> + <file name="mkdll.sh" role="src" /> + <file name="mkopcodec.awk" role="src" /> + <file name="mkopcodeh.awk" role="src" /> + <file name="mkso.sh" role="src" /> + <file name="publish.sh" role="src" /> + <file name="README" role="src" /> + <file name="spec.template" role="src" /> + <file name="sqlite.1" role="src" /> + <file name="sqlite.pc.in" role="src" /> + <file name="sqlite3.def" role="src" /> + <file name="sqlite3.pc.in" role="src" /> + <file name="VERSION" role="src" /> + </dir> <!-- //sqlite --> + <file name="config.m4" role="src" /> + <file name="config.w32" role="src" /> + <file name="CREDITS" role="doc" /> + <file name="pdo_sqlite.c" role="src" /> + <file name="php_pdo_sqlite.h" role="src" /> + <file name="php_pdo_sqlite_int.h" role="src" /> + <file name="sqlite_driver.c" role="src" /> + <file name="sqlite_statement.c" role="src" /> + </dir> <!-- / --> + </contents> + <dependencies> + <required> + <php> + <min>5.0.3</min> + </php> + <pearinstaller> + <min>1.4.0</min> + </pearinstaller> + <package> + <name>pdo</name> + <channel>pecl.php.net</channel> + <min>1.0.3</min> + <providesextension>PDO</providesextension> + </package> + </required> + </dependencies> + <providesextension>PDO_SQLITE</providesextension> + <extsrcrelease /> +</package> +<!-- vim:se ts=1 sw=1 et: --> diff --git a/ext/pdo_sqlite/pdo_sqlite.c b/ext/pdo_sqlite/pdo_sqlite.c new file mode 100644 index 0000000..cc9b03e --- /dev/null +++ b/ext/pdo_sqlite/pdo_sqlite.c @@ -0,0 +1,110 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2013 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> | + +----------------------------------------------------------------------+ +*/ + +/* $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 "1.0.1" + +/* {{{ pdo_sqlite_functions[] */ +const zend_function_entry pdo_sqlite_functions[] = { + PHP_FE_END +}; +/* }}} */ + +/* {{{ pdo_sqlite_deps + */ +#if ZEND_MODULE_API_NO >= 20050922 +static const zend_module_dep pdo_sqlite_deps[] = { + ZEND_MOD_REQUIRED("pdo") + ZEND_MOD_END +}; +#endif +/* }}} */ + +/* {{{ pdo_sqlite_module_entry + */ +zend_module_entry pdo_sqlite_module_entry = { +#if ZEND_MODULE_API_NO >= 20050922 + STANDARD_MODULE_HEADER_EX, NULL, + pdo_sqlite_deps, +#else + STANDARD_MODULE_HEADER, +#endif + "pdo_sqlite", + pdo_sqlite_functions, + PHP_MINIT(pdo_sqlite), + PHP_MSHUTDOWN(pdo_sqlite), + NULL, + NULL, + PHP_MINFO(pdo_sqlite), + PHP_PDO_SQLITE_MODULE_VERSION, + STANDARD_MODULE_PROPERTIES +}; +/* }}} */ + +#if defined(COMPILE_DL_PDO_SQLITE) || defined(COMPILE_DL_PDO_SQLITE_EXTERNAL) +ZEND_GET_MODULE(pdo_sqlite) +#endif + +/* {{{ PHP_MINIT_FUNCTION */ +PHP_MINIT_FUNCTION(pdo_sqlite) +{ + return php_pdo_register_driver(&pdo_sqlite_driver); +} +/* }}} */ + +/* {{{ PHP_MSHUTDOWN_FUNCTION */ +PHP_MSHUTDOWN_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, "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 index 0000000..6fa3bab --- /dev/null +++ b/ext/pdo_sqlite/php_pdo_sqlite.h @@ -0,0 +1,65 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2013 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> | + +----------------------------------------------------------------------+ +*/ + +/* $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 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 index 0000000..8e20ca5 --- /dev/null +++ b/ext/pdo_sqlite/php_pdo_sqlite_int.h @@ -0,0 +1,78 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2013 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> | + +----------------------------------------------------------------------+ +*/ + +/* $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; + +struct pdo_sqlite_fci { + zend_fcall_info fci; + zend_fcall_info_cache fcc; +}; + +struct pdo_sqlite_func { + struct pdo_sqlite_func *next; + + zval *func, *step, *fini; + int argc; + const char *funcname; + + /* accelerated callback references */ + struct pdo_sqlite_fci afunc, astep, afini; +}; + +struct pdo_sqlite_collation { + struct pdo_sqlite_collation *next; + + const char *name; + zval *callback; + struct pdo_sqlite_fci fc; +}; + +typedef struct { + sqlite3 *db; + pdo_sqlite_error_info einfo; + struct pdo_sqlite_func *funcs; + struct pdo_sqlite_collation *collations; +} pdo_sqlite_db_handle; + +typedef struct { + pdo_sqlite_db_handle *H; + sqlite3_stmt *stmt; + unsigned pre_fetched:1; + unsigned done: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 index 0000000..0f4a245 --- /dev/null +++ b/ext/pdo_sqlite/sqlite_driver.c @@ -0,0 +1,873 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2013 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> | + +----------------------------------------------------------------------+ +*/ + +/* $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; + 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) { + if (einfo->errmsg) { + pefree(einfo->errmsg, dbh->is_persistent); + } + einfo->errmsg = pestrdup((char*)sqlite3_errmsg(H->db), dbh->is_persistent); + } else { /* no error */ + strncpy(*pdo_err, PDO_ERR_NONE, sizeof(PDO_ERR_NONE)); + return 0; + } + switch (einfo->errcode) { + case SQLITE_NOTFOUND: + strncpy(*pdo_err, "42S02", sizeof("42S02")); + break; + + case SQLITE_INTERRUPT: + strncpy(*pdo_err, "01002", sizeof("01002")); + break; + + case SQLITE_NOLFS: + strncpy(*pdo_err, "HYC00", sizeof("HYC00")); + break; + + case SQLITE_TOOBIG: + strncpy(*pdo_err, "22001", sizeof("22001")); + break; + + case SQLITE_CONSTRAINT: + strncpy(*pdo_err, "23000", sizeof("23000")); + break; + + case SQLITE_ERROR: + default: + strncpy(*pdo_err, "HY000", sizeof("HY000")); + break; + } + + if (!dbh->methods) { + zend_throw_exception_ex(php_pdo_get_exception(), einfo->errcode TSRMLS_CC, "SQLSTATE[%s] [%d] %s", + *pdo_err, 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 void pdo_sqlite_cleanup_callbacks(pdo_sqlite_db_handle *H TSRMLS_DC) +{ + struct pdo_sqlite_func *func; + + while (H->funcs) { + func = H->funcs; + H->funcs = func->next; + + if (H->db) { + /* delete the function from the handle */ + sqlite3_create_function(H->db, + func->funcname, + func->argc, + SQLITE_UTF8, + func, + NULL, NULL, NULL); + } + + efree((char*)func->funcname); + if (func->func) { + zval_ptr_dtor(&func->func); + } + if (func->step) { + zval_ptr_dtor(&func->step); + } + if (func->fini) { + zval_ptr_dtor(&func->fini); + } + efree(func); + } + + while (H->collations) { + struct pdo_sqlite_collation *collation; + collation = H->collations; + H->collations = collation->next; + + if (H->db) { + /* delete the collation from the handle */ + sqlite3_create_collation(H->db, + collation->name, + SQLITE_UTF8, + collation, + NULL); + } + + efree((char*)collation->name); + if (collation->callback) { + zval_ptr_dtor(&collation->callback); + } + efree(collation); + } +} + +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) { + pdo_sqlite_error_info *einfo = &H->einfo; + + pdo_sqlite_cleanup_callbacks(H TSRMLS_CC); + if (H->db) { + sqlite3_close(H->db); + H->db = NULL; + } + if (einfo->errmsg) { + pefree(einfo->errmsg, dbh->is_persistent); + einfo->errmsg = 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, 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; + stmt->supports_placeholders = PDO_PLACEHOLDER_POSITIONAL|PDO_PLACEHOLDER_NAMED; + + if (PDO_CURSOR_FWDONLY != pdo_attr_lval(driver_options, PDO_ATTR_CURSOR, PDO_CURSOR_FWDONLY TSRMLS_CC)) { + H->einfo.errcode = SQLITE_ERROR; + pdo_sqlite_error(dbh); + return 0; + } + + 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 char *pdo_sqlite_last_insert_id(pdo_dbh_t *dbh, const char *name, unsigned int *len TSRMLS_DC) +{ + pdo_sqlite_db_handle *H = (pdo_sqlite_db_handle *)dbh->driver_data; + char *id; + + id = php_pdo_int64_to_str(sqlite3_last_insert_rowid(H->db) TSRMLS_CC); + *len = strlen(id); + return id; +} + +/* 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, enum pdo_param_type paramtype TSRMLS_DC) +{ + *quoted = safe_emalloc(2, unquotedlen, 3); + sqlite3_snprintf(2*unquotedlen + 3, *quoted, "'%q'", unquoted); + *quotedlen = strlen(*quoted); + return 1; +} + +static int sqlite_handle_begin(pdo_dbh_t *dbh TSRMLS_DC) +{ + pdo_sqlite_db_handle *H = (pdo_sqlite_db_handle *)dbh->driver_data; + char *errmsg = NULL; + + if (sqlite3_exec(H->db, "BEGIN", NULL, NULL, &errmsg) != SQLITE_OK) { + pdo_sqlite_error(dbh); + if (errmsg) + sqlite3_free(errmsg); + return 0; + } + return 1; +} + +static int sqlite_handle_commit(pdo_dbh_t *dbh TSRMLS_DC) +{ + pdo_sqlite_db_handle *H = (pdo_sqlite_db_handle *)dbh->driver_data; + char *errmsg = NULL; + + if (sqlite3_exec(H->db, "COMMIT", NULL, NULL, &errmsg) != SQLITE_OK) { + pdo_sqlite_error(dbh); + if (errmsg) + sqlite3_free(errmsg); + return 0; + } + return 1; +} + +static int sqlite_handle_rollback(pdo_dbh_t *dbh TSRMLS_DC) +{ + pdo_sqlite_db_handle *H = (pdo_sqlite_db_handle *)dbh->driver_data; + char *errmsg = NULL; + + if (sqlite3_exec(H->db, "ROLLBACK", NULL, NULL, &errmsg) != SQLITE_OK) { + pdo_sqlite_error(dbh); + if (errmsg) + sqlite3_free(errmsg); + return 0; + } + return 1; +} + +static int pdo_sqlite_get_attribute(pdo_dbh_t *dbh, long attr, zval *return_value TSRMLS_DC) +{ + 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 int pdo_sqlite_set_attr(pdo_dbh_t *dbh, long attr, zval *val TSRMLS_DC) +{ + pdo_sqlite_db_handle *H = (pdo_sqlite_db_handle *)dbh->driver_data; + + switch (attr) { + case PDO_ATTR_TIMEOUT: + convert_to_long(val); + sqlite3_busy_timeout(H->db, Z_LVAL_P(val) * 1000); + return 1; + } + return 0; +} + +static int do_callback(struct pdo_sqlite_fci *fc, zval *cb, + int argc, sqlite3_value **argv, sqlite3_context *context, + int is_agg TSRMLS_DC) +{ + zval ***zargs = NULL; + zval *retval = NULL; + int i; + int ret; + int fake_argc; + zval **agg_context = NULL; + + if (is_agg) { + is_agg = 2; + } + + fake_argc = argc + is_agg; + + fc->fci.size = sizeof(fc->fci); + fc->fci.function_table = EG(function_table); + fc->fci.function_name = cb; + fc->fci.symbol_table = NULL; + fc->fci.object_ptr = NULL; + fc->fci.retval_ptr_ptr = &retval; + fc->fci.param_count = fake_argc; + + /* build up the params */ + + if (fake_argc) { + zargs = (zval ***)safe_emalloc(fake_argc, sizeof(zval **), 0); + } + + if (is_agg) { + /* summon the aggregation context */ + agg_context = (zval**)sqlite3_aggregate_context(context, sizeof(zval*)); + if (!*agg_context) { + MAKE_STD_ZVAL(*agg_context); + ZVAL_NULL(*agg_context); + } + zargs[0] = agg_context; + + zargs[1] = emalloc(sizeof(zval*)); + MAKE_STD_ZVAL(*zargs[1]); + ZVAL_LONG(*zargs[1], sqlite3_aggregate_count(context)); + } + + for (i = 0; i < argc; i++) { + zargs[i + is_agg] = emalloc(sizeof(zval *)); + MAKE_STD_ZVAL(*zargs[i + is_agg]); + + /* get the value */ + switch (sqlite3_value_type(argv[i])) { + case SQLITE_INTEGER: + ZVAL_LONG(*zargs[i + is_agg], sqlite3_value_int(argv[i])); + break; + + case SQLITE_FLOAT: + ZVAL_DOUBLE(*zargs[i + is_agg], sqlite3_value_double(argv[i])); + break; + + case SQLITE_NULL: + ZVAL_NULL(*zargs[i + is_agg]); + break; + + case SQLITE_BLOB: + case SQLITE3_TEXT: + default: + ZVAL_STRINGL(*zargs[i + is_agg], (char*)sqlite3_value_text(argv[i]), + sqlite3_value_bytes(argv[i]), 1); + break; + } + } + + + fc->fci.params = zargs; + + + if ((ret = zend_call_function(&fc->fci, &fc->fcc TSRMLS_CC)) == FAILURE) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "An error occurred while invoking the callback"); + } + + /* clean up the params */ + if (zargs) { + for (i = is_agg; i < fake_argc; i++) { + zval_ptr_dtor(zargs[i]); + efree(zargs[i]); + } + if (is_agg) { + zval_ptr_dtor(zargs[1]); + efree(zargs[1]); + } + efree(zargs); + } + + if (!is_agg || !argv) { + /* only set the sqlite return value if we are a scalar function, + * or if we are finalizing an aggregate */ + if (retval) { + switch (Z_TYPE_P(retval)) { + case IS_LONG: + sqlite3_result_int(context, Z_LVAL_P(retval)); + break; + + case IS_NULL: + sqlite3_result_null(context); + break; + + case IS_DOUBLE: + sqlite3_result_double(context, Z_DVAL_P(retval)); + break; + + default: + convert_to_string_ex(&retval); + sqlite3_result_text(context, Z_STRVAL_P(retval), + Z_STRLEN_P(retval), SQLITE_TRANSIENT); + break; + } + } else { + sqlite3_result_error(context, "failed to invoke callback", 0); + } + + if (agg_context) { + zval_ptr_dtor(agg_context); + } + } else { + /* we're stepping in an aggregate; the return value goes into + * the context */ + if (agg_context) { + zval_ptr_dtor(agg_context); + } + if (retval) { + *agg_context = retval; + retval = NULL; + } else { + *agg_context = NULL; + } + } + + if (retval) { + zval_ptr_dtor(&retval); + } + + return ret; +} + +static void php_sqlite3_func_callback(sqlite3_context *context, int argc, + sqlite3_value **argv) +{ + struct pdo_sqlite_func *func = (struct pdo_sqlite_func*)sqlite3_user_data(context); + TSRMLS_FETCH(); + + do_callback(&func->afunc, func->func, argc, argv, context, 0 TSRMLS_CC); +} + +static void php_sqlite3_func_step_callback(sqlite3_context *context, int argc, + sqlite3_value **argv) +{ + struct pdo_sqlite_func *func = (struct pdo_sqlite_func*)sqlite3_user_data(context); + TSRMLS_FETCH(); + + do_callback(&func->astep, func->step, argc, argv, context, 1 TSRMLS_CC); +} + +static void php_sqlite3_func_final_callback(sqlite3_context *context) +{ + struct pdo_sqlite_func *func = (struct pdo_sqlite_func*)sqlite3_user_data(context); + TSRMLS_FETCH(); + + do_callback(&func->afini, func->fini, 0, NULL, context, 1 TSRMLS_CC); +} + +static int php_sqlite3_collation_callback(void *context, + int string1_len, const void *string1, + int string2_len, const void *string2) +{ + int ret; + zval *zstring1, *zstring2; + zval **zargs[2]; + zval *retval = NULL; + struct pdo_sqlite_collation *collation = (struct pdo_sqlite_collation*) context; + TSRMLS_FETCH(); + + collation->fc.fci.size = sizeof(collation->fc.fci); + collation->fc.fci.function_table = EG(function_table); + collation->fc.fci.function_name = collation->callback; + collation->fc.fci.symbol_table = NULL; + collation->fc.fci.object_ptr = NULL; + collation->fc.fci.retval_ptr_ptr = &retval; + + // Prepare the arguments. + MAKE_STD_ZVAL(zstring1); + ZVAL_STRINGL(zstring1, (char *) string1, string1_len, 1); + zargs[0] = &zstring1; + MAKE_STD_ZVAL(zstring2); + ZVAL_STRINGL(zstring2, (char *) string2, string2_len, 1); + zargs[1] = &zstring2; + collation->fc.fci.param_count = 2; + collation->fc.fci.params = zargs; + + if ((ret = zend_call_function(&collation->fc.fci, &collation->fc.fcc TSRMLS_CC)) == FAILURE) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "An error occurred while invoking the callback"); + } + else if (retval) { + if (Z_TYPE_P(retval) != IS_LONG) { + convert_to_long_ex(&retval); + } + ret = 0; + if (Z_LVAL_P(retval) > 0) { + ret = 1; + } + else if (Z_LVAL_P(retval) < 0) { + ret = -1; + } + zval_ptr_dtor(&retval); + } + + zval_ptr_dtor(zargs[0]); + zval_ptr_dtor(zargs[1]); + + return ret; +} + +/* {{{ bool SQLite::sqliteCreateFunction(string name, mixed callback [, int argcount]) + Registers a UDF with the sqlite db handle */ +static PHP_METHOD(SQLite, sqliteCreateFunction) +{ + struct pdo_sqlite_func *func; + zval *callback; + char *func_name; + int func_name_len; + long argc = -1; + char *cbname = NULL; + pdo_dbh_t *dbh; + pdo_sqlite_db_handle *H; + int ret; + + if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|l", + &func_name, &func_name_len, &callback, &argc)) { + RETURN_FALSE; + } + + dbh = zend_object_store_get_object(getThis() TSRMLS_CC); + PDO_CONSTRUCT_CHECK; + + if (!zend_is_callable(callback, 0, &cbname TSRMLS_CC)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "function '%s' is not callable", cbname); + efree(cbname); + RETURN_FALSE; + } + efree(cbname); + + H = (pdo_sqlite_db_handle *)dbh->driver_data; + + func = (struct pdo_sqlite_func*)ecalloc(1, sizeof(*func)); + + ret = sqlite3_create_function(H->db, func_name, argc, SQLITE_UTF8, + func, php_sqlite3_func_callback, NULL, NULL); + if (ret == SQLITE_OK) { + func->funcname = estrdup(func_name); + + MAKE_STD_ZVAL(func->func); + MAKE_COPY_ZVAL(&callback, func->func); + + func->argc = argc; + + func->next = H->funcs; + H->funcs = func; + + RETURN_TRUE; + } + + efree(func); + RETURN_FALSE; +} +/* }}} */ + +/* {{{ bool SQLite::sqliteCreateAggregate(string name, mixed step, mixed fini [, int argcount]) + Registers a UDF with the sqlite db handle */ + +/* The step function should have the prototype: + mixed step(mixed $context, int $rownumber, $value [, $value2 [, ...]]) + + $context will be null for the first row; on subsequent rows it will have + the value that was previously returned from the step function; you should + use this to maintain state for the aggregate. + + The fini function should have the prototype: + mixed fini(mixed $context, int $rownumber) + + $context will hold the return value from the very last call to the step function. + rownumber will hold the number of rows over which the aggregate was performed. + The return value of this function will be used as the return value for this + aggregate UDF. +*/ + +static PHP_METHOD(SQLite, sqliteCreateAggregate) +{ + struct pdo_sqlite_func *func; + zval *step_callback, *fini_callback; + char *func_name; + int func_name_len; + long argc = -1; + char *cbname = NULL; + pdo_dbh_t *dbh; + pdo_sqlite_db_handle *H; + int ret; + + if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "szz|l", + &func_name, &func_name_len, &step_callback, &fini_callback, &argc)) { + RETURN_FALSE; + } + + dbh = zend_object_store_get_object(getThis() TSRMLS_CC); + PDO_CONSTRUCT_CHECK; + + if (!zend_is_callable(step_callback, 0, &cbname TSRMLS_CC)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "function '%s' is not callable", cbname); + efree(cbname); + RETURN_FALSE; + } + efree(cbname); + if (!zend_is_callable(fini_callback, 0, &cbname TSRMLS_CC)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "function '%s' is not callable", cbname); + efree(cbname); + RETURN_FALSE; + } + efree(cbname); + + H = (pdo_sqlite_db_handle *)dbh->driver_data; + + func = (struct pdo_sqlite_func*)ecalloc(1, sizeof(*func)); + + ret = sqlite3_create_function(H->db, func_name, argc, SQLITE_UTF8, + func, NULL, php_sqlite3_func_step_callback, php_sqlite3_func_final_callback); + if (ret == SQLITE_OK) { + func->funcname = estrdup(func_name); + + MAKE_STD_ZVAL(func->step); + MAKE_COPY_ZVAL(&step_callback, func->step); + + MAKE_STD_ZVAL(func->fini); + MAKE_COPY_ZVAL(&fini_callback, func->fini); + + func->argc = argc; + + func->next = H->funcs; + H->funcs = func; + + RETURN_TRUE; + } + + efree(func); + RETURN_FALSE; +} +/* }}} */ + +/* {{{ bool SQLite::sqliteCreateCollation(string name, mixed callback) + Registers a collation with the sqlite db handle */ +static PHP_METHOD(SQLite, sqliteCreateCollation) +{ + struct pdo_sqlite_collation *collation; + zval *callback; + char *collation_name; + int collation_name_len; + char *cbname = NULL; + pdo_dbh_t *dbh; + pdo_sqlite_db_handle *H; + int ret; + + if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", + &collation_name, &collation_name_len, &callback)) { + RETURN_FALSE; + } + + dbh = zend_object_store_get_object(getThis() TSRMLS_CC); + PDO_CONSTRUCT_CHECK; + + if (!zend_is_callable(callback, 0, &cbname TSRMLS_CC)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "function '%s' is not callable", cbname); + efree(cbname); + RETURN_FALSE; + } + efree(cbname); + + H = (pdo_sqlite_db_handle *)dbh->driver_data; + + collation = (struct pdo_sqlite_collation*)ecalloc(1, sizeof(*collation)); + + ret = sqlite3_create_collation(H->db, collation_name, SQLITE_UTF8, collation, php_sqlite3_collation_callback); + if (ret == SQLITE_OK) { + collation->name = estrdup(collation_name); + + MAKE_STD_ZVAL(collation->callback); + MAKE_COPY_ZVAL(&callback, collation->callback); + + collation->next = H->collations; + H->collations = collation; + + RETURN_TRUE; + } + + efree(collation); + RETURN_FALSE; +} +/* }}} */ + +static const zend_function_entry dbh_methods[] = { + PHP_ME(SQLite, sqliteCreateFunction, NULL, ZEND_ACC_PUBLIC) + PHP_ME(SQLite, sqliteCreateAggregate, NULL, ZEND_ACC_PUBLIC) + PHP_ME(SQLite, sqliteCreateCollation, NULL, ZEND_ACC_PUBLIC) + PHP_FE_END +}; + +static const zend_function_entry *get_driver_methods(pdo_dbh_t *dbh, int kind TSRMLS_DC) +{ + switch (kind) { + case PDO_DBH_DRIVER_METHOD_KIND_DBH: + return dbh_methods; + + default: + return NULL; + } +} + +static void pdo_sqlite_request_shutdown(pdo_dbh_t *dbh TSRMLS_DC) +{ + pdo_sqlite_db_handle *H = (pdo_sqlite_db_handle *)dbh->driver_data; + /* unregister functions, so that they don't linger for the next + * request */ + if (H) { + pdo_sqlite_cleanup_callbacks(H TSRMLS_CC); + } +} + +static struct pdo_dbh_methods sqlite_methods = { + sqlite_handle_closer, + sqlite_handle_preparer, + sqlite_handle_doer, + sqlite_handle_quoter, + sqlite_handle_begin, + sqlite_handle_commit, + sqlite_handle_rollback, + pdo_sqlite_set_attr, + pdo_sqlite_last_insert_id, + pdo_sqlite_fetch_error_func, + pdo_sqlite_get_attribute, + NULL, /* check_liveness: not needed */ + get_driver_methods, + pdo_sqlite_request_shutdown +}; + +static char *make_filename_safe(const char *filename TSRMLS_DC) +{ + if (*filename && memcmp(filename, ":memory:", sizeof(":memory:"))) { + char *fullpath = expand_filepath(filename, NULL TSRMLS_CC); + + if (!fullpath) { + return NULL; + } + + if (php_check_open_basedir(fullpath TSRMLS_CC)) { + efree(fullpath); + return NULL; + } + return fullpath; + } + return estrdup(filename); +} + +static int authorizer(void *autharg, int access_type, const char *arg3, const char *arg4, + const char *arg5, const char *arg6) +{ + char *filename; + switch (access_type) { + case SQLITE_COPY: { + TSRMLS_FETCH(); + filename = make_filename_safe(arg4 TSRMLS_CC); + if (!filename) { + return SQLITE_DENY; + } + efree(filename); + return SQLITE_OK; + } + + case SQLITE_ATTACH: { + TSRMLS_FETCH(); + filename = make_filename_safe(arg3 TSRMLS_CC); + if (!filename) { + return SQLITE_DENY; + } + efree(filename); + return SQLITE_OK; + } + + default: + /* access allowed */ + return SQLITE_OK; + } +} + +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; + char *filename; + + H = pecalloc(1, sizeof(pdo_sqlite_db_handle), dbh->is_persistent); + + H->einfo.errcode = 0; + H->einfo.errmsg = NULL; + dbh->driver_data = H; + + filename = make_filename_safe(dbh->data_source TSRMLS_CC); + + if (!filename) { + zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, + "open_basedir prohibits opening %s", + dbh->data_source); + goto cleanup; + } + + i = sqlite3_open(filename, &H->db); + efree(filename); + + if (i != SQLITE_OK) { + pdo_sqlite_error(dbh); + goto cleanup; + } + + if (PG(open_basedir) && *PG(open_basedir)) { + sqlite3_set_authorizer(H->db, authorizer, NULL); + } + + if (driver_options) { + timeout = pdo_attr_lval(driver_options, PDO_ATTR_TIMEOUT, timeout TSRMLS_CC); + } + sqlite3_busy_timeout(H->db, timeout * 1000); + + dbh->alloc_own_columns = 1; + 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 index 0000000..e970ad3 --- /dev/null +++ b/ext/pdo_sqlite/sqlite_statement.c @@ -0,0 +1,362 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2013 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> | + +----------------------------------------------------------------------+ +*/ + +/* $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; + + 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_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; + stmt->column_count = sqlite3_data_count(S->stmt); + return 1; + + case SQLITE_DONE: + stmt->column_count = 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 TSRMLS_DC) +{ + pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data; + + 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, 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_TYPE_P(param->parameter) == IS_NULL) { + if (sqlite3_bind_null(S->stmt, param->paramno + 1) == SQLITE_OK) { + return 1; + } + } else { + convert_to_long(param->parameter); +#if LONG_MAX > 2147483647 + if (SQLITE_OK == sqlite3_bind_int64(S->stmt, param->paramno + 1, Z_LVAL_P(param->parameter))) { + return 1; + } +#else + if (SQLITE_OK == sqlite3_bind_int(S->stmt, param->paramno + 1, Z_LVAL_P(param->parameter))) { + return 1; + } +#endif + } + pdo_sqlite_error_stmt(stmt); + return 0; + + case PDO_PARAM_LOB: + if (Z_TYPE_P(param->parameter) == IS_RESOURCE) { + php_stream *stm; + php_stream_from_zval_no_verify(stm, ¶m->parameter); + if (stm) { + SEPARATE_ZVAL(¶m->parameter); + Z_TYPE_P(param->parameter) = IS_STRING; + Z_STRLEN_P(param->parameter) = php_stream_copy_to_mem(stm, + &Z_STRVAL_P(param->parameter), PHP_STREAM_COPY_ALL, 0); + } else { + pdo_raise_impl_error(stmt->dbh, stmt, "HY105", "Expected a stream resource" TSRMLS_CC); + return 0; + } + } else if (Z_TYPE_P(param->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(param->parameter); + } + + if (SQLITE_OK == sqlite3_bind_blob(S->stmt, param->paramno + 1, + Z_STRVAL_P(param->parameter), + Z_STRLEN_P(param->parameter), + SQLITE_STATIC)) { + return 1; + } + pdo_sqlite_error_stmt(stmt); + return 0; + + case PDO_PARAM_STR: + default: + if (Z_TYPE_P(param->parameter) == IS_NULL) { + if (sqlite3_bind_null(S->stmt, param->paramno + 1) == SQLITE_OK) { + return 1; + } + } else { + convert_to_string(param->parameter); + if(SQLITE_OK == sqlite3_bind_text(S->stmt, param->paramno + 1, + Z_STRVAL_P(param->parameter), + Z_STRLEN_P(param->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, long offset 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; + } + 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 TSRMLS_DC) +{ + pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data; + + if(colno >= sqlite3_column_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 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, unsigned long *len, int *caller_frees 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 = sqlite3_column_bytes(S->stmt, colno); + return 1; + } +} + +static int pdo_sqlite_stmt_col_meta(pdo_stmt_t *stmt, long colno, zval *return_value TSRMLS_DC) +{ + pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data; + const char *str; + zval *flags; + + if (!S->stmt) { + return FAILURE; + } + if(colno >= sqlite3_data_count(S->stmt)) { + /* error invalid column */ + pdo_sqlite_error_stmt(stmt); + return FAILURE; + } + + array_init(return_value); + MAKE_STD_ZVAL(flags); + array_init(flags); + + switch (sqlite3_column_type(S->stmt, colno)) { + case SQLITE_NULL: + add_assoc_string(return_value, "native_type", "null", 1); + break; + + case SQLITE_FLOAT: + add_assoc_string(return_value, "native_type", "double", 1); + break; + + case SQLITE_BLOB: + add_next_index_string(flags, "blob", 1); + case SQLITE_TEXT: + add_assoc_string(return_value, "native_type", "string", 1); + break; + + case SQLITE_INTEGER: + add_assoc_string(return_value, "native_type", "integer", 1); + break; + } + + str = sqlite3_column_decltype(S->stmt, colno); + if (str) { + add_assoc_string(return_value, "sqlite:decl_type", (char *)str, 1); + } + +#ifdef SQLITE_ENABLE_COLUMN_METADATA + str = sqlite3_column_table_name(S->stmt, colno); + if (str) { + add_assoc_string(return_value, "table", (char *)str, 1); + } +#endif + + add_assoc_zval(return_value, "flags", flags); + + return SUCCESS; +} + +static int pdo_sqlite_stmt_cursor_closer(pdo_stmt_t *stmt TSRMLS_DC) +{ + pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data; + sqlite3_reset(S->stmt); + 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, + 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 + */ diff --git a/ext/pdo_sqlite/tests/bug33841.phpt b/ext/pdo_sqlite/tests/bug33841.phpt new file mode 100644 index 0000000..d472af7 --- /dev/null +++ b/ext/pdo_sqlite/tests/bug33841.phpt @@ -0,0 +1,28 @@ +--TEST-- +PDO SQLite Bug #33841 (rowCount() does not work on prepared statements) +--SKIPIF-- +<?php # vim:ft=php +if (!extension_loaded('pdo_sqlite')) print 'skip not loaded'; +?> +--FILE-- +<?php +require dirname(__FILE__) . '/../../../ext/pdo/tests/pdo_test.inc'; +$db = PDOTest::test_factory(dirname(__FILE__) . '/common.phpt'); + +$db->exec('CREATE TABLE test (text)'); + +$stmt = $db->prepare("INSERT INTO test VALUES ( :text )"); +$stmt->bindParam(':text', $name); +$name = 'test1'; +var_dump($stmt->execute(), $stmt->rowCount()); + +$stmt = $db->prepare("UPDATE test SET text = :text "); +$stmt->bindParam(':text', $name); +$name = 'test2'; +var_dump($stmt->execute(), $stmt->rowCount()); + +--EXPECT-- +bool(true) +int(1) +bool(true) +int(1) diff --git a/ext/pdo_sqlite/tests/bug35336.phpt b/ext/pdo_sqlite/tests/bug35336.phpt new file mode 100644 index 0000000..59dbda3 --- /dev/null +++ b/ext/pdo_sqlite/tests/bug35336.phpt @@ -0,0 +1,26 @@ +--TEST-- +Bug #35336 (crash on PDO::FETCH_CLASS + __set()) +--SKIPIF-- +<?php # vim:ft=php +if (!extension_loaded('pdo_sqlite')) print 'skip not loaded'; +?> +--FILE-- +<?php +class EEE { + function __set ($field, $value) { + echo "hello world\n"; + } +} + +$a = new PDO("sqlite::memory:");// pool ("sqlite::memory:"); +$a->query ("CREATE TABLE test (a integer primary key, b text)"); +$b = $a->prepare("insert into test (b) values (?)"); +$b->execute(array (5)); +$rez = $a->query ("SELECT * FROM test")->fetchAll(PDO::FETCH_CLASS, 'EEE'); + +echo "Done\n"; +?> +--EXPECTF-- +hello world +hello world +Done diff --git a/ext/pdo_sqlite/tests/bug43831.phpt b/ext/pdo_sqlite/tests/bug43831.phpt new file mode 100644 index 0000000..2746e7c --- /dev/null +++ b/ext/pdo_sqlite/tests/bug43831.phpt @@ -0,0 +1,53 @@ +--TEST-- +Bug #43831 ($this gets mangled when extending PDO with persistent connection) +--SKIPIF-- +<?php if (!extension_loaded('pdo_sqlite')) print 'skip not loaded'; ?> +--FILE-- +<?php + +class Foo extends PDO { + function __construct($dsn) { + parent::__construct($dsn, null, null, array(PDO::ATTR_PERSISTENT => true)); + } +} + +class Baz extends PDO { + function __construct($dsn) { + parent::__construct($dsn, null, null, array(PDO::ATTR_PERSISTENT => true)); + } +} + +class Bar extends Baz { + function quux() { + echo get_class($this), "\n"; + $foo = new Foo("sqlite::memory:"); + echo get_class($this), "\n"; + } +} + +$bar = new Bar("sqlite::memory:"); +$bar->quux(); + + +class MyPDO extends PDO {} + +$bar = new PDO("sqlite::memory:", null, null, array(PDO::ATTR_PERSISTENT => true)); +$baz = new MyPDO("sqlite::memory:", null, null, array(PDO::ATTR_PERSISTENT => true)); + +var_dump($bar); +unset($bar); +var_dump($baz); +var_dump($bar); + + +?> +--EXPECTF-- +Bar +Bar +object(MyPDO)#%d (0) { +} +object(MyPDO)#%d (0) { +} + +Notice: Undefined variable: bar in %s on line %d +NULL diff --git a/ext/pdo_sqlite/tests/bug44327_2.phpt b/ext/pdo_sqlite/tests/bug44327_2.phpt new file mode 100644 index 0000000..4add073 --- /dev/null +++ b/ext/pdo_sqlite/tests/bug44327_2.phpt @@ -0,0 +1,50 @@ +--TEST-- +Bug #44327.2 (PDORow::queryString property & numeric offsets / Crash) +--SKIPIF-- +<?php +if (!extension_loaded('pdo_sqlite')) print 'skip not loaded'; +?> +--FILE-- +<?php + +$db = new pdo('sqlite::memory:'); + +$x = $db->query('select 1 as queryString'); +var_dump($x, $x->queryString); + +$y = $x->fetch(); +var_dump($y, @$y->queryString); + +print "--------------------------------------------\n"; + +$x = $db->query('select 1 as queryString'); +var_dump($x, $x->queryString); + +$y = $x->fetch(PDO::FETCH_LAZY); +var_dump($y, $y->queryString); + +?> +--EXPECTF-- +object(PDOStatement)#%d (1) { + ["queryString"]=> + string(23) "select 1 as queryString" +} +string(23) "select 1 as queryString" +array(2) { + ["queryString"]=> + string(1) "1" + [0]=> + string(1) "1" +} +NULL +-------------------------------------------- +object(PDOStatement)#%d (1) { + ["queryString"]=> + string(23) "select 1 as queryString" +} +string(23) "select 1 as queryString" +object(PDORow)#%d (1) { + ["queryString"]=> + string(1) "1" +} +string(1) "1" diff --git a/ext/pdo_sqlite/tests/bug44327_3.phpt b/ext/pdo_sqlite/tests/bug44327_3.phpt new file mode 100644 index 0000000..86f5642 --- /dev/null +++ b/ext/pdo_sqlite/tests/bug44327_3.phpt @@ -0,0 +1,33 @@ +--TEST-- +Bug #44327.3 (PDORow::queryString property & numeric offsets / Crash) +--SKIPIF-- +<?php +if (!extension_loaded('pdo_sqlite')) print 'skip not loaded'; +?> +--FILE-- +<?php + +$db = new pdo('sqlite::memory:'); + +$x = $db->query('select 1 as queryStringxx'); +$y = $x->fetch(PDO::FETCH_LAZY); +var_dump($y, $y->queryString, $y->queryStringzz, $y->queryStringxx); + +print "---\n"; + +var_dump($y[5], $y->{3}); + +?> +--EXPECTF-- +object(PDORow)#%d (2) { + ["queryString"]=> + string(25) "select 1 as queryStringxx" + ["queryStringxx"]=> + string(1) "1" +} +string(25) "select 1 as queryStringxx" +NULL +string(1) "1" +--- +NULL +NULL diff --git a/ext/pdo_sqlite/tests/bug46139.phpt b/ext/pdo_sqlite/tests/bug46139.phpt new file mode 100644 index 0000000..c361d7a --- /dev/null +++ b/ext/pdo_sqlite/tests/bug46139.phpt @@ -0,0 +1,42 @@ +--TEST-- +Bug #46139 (PDOStatement->setFetchMode() forgets FETCH_PROPS_LATE) +--SKIPIF-- +<?php +if (!extension_loaded('pdo_sqlite')) print 'skip not loaded'; +?> +--FILE-- +<?php + +require dirname(__FILE__) . '/../../../ext/pdo/tests/pdo_test.inc'; +$db = PDOTest::test_factory(dirname(__FILE__) . '/common.phpt'); + +class Person { + public $test = NULL; + public function __construct() { + var_dump($this->test); + } +} + +$stmt = $db->query("SELECT 'foo' test, 1"); +$stmt->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Person'); +$r1 = $stmt->fetch(); +printf("'%s'\n", $r1->test); + +$stmt = $db->query("SELECT 'foo' test, 1"); +$stmt->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Person'); +$r1 = $stmt->fetchAll(); +printf("'%s'\n", $r1[0]->test); + +$stmt = $db->query("SELECT 'foo' test, 1"); +$stmt->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Person'); +$r1 = $stmt->fetch(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE); +printf("'%s'\n", $r1->test); + +?> +--EXPECT-- +NULL +'foo' +NULL +'foo' +NULL +'foo' diff --git a/ext/pdo_sqlite/tests/bug46542.phpt b/ext/pdo_sqlite/tests/bug46542.phpt new file mode 100644 index 0000000..2de0c05 --- /dev/null +++ b/ext/pdo_sqlite/tests/bug46542.phpt @@ -0,0 +1,24 @@ +--TEST-- +Bug #46542 Extending PDO class with a __call() function +--SKIPIF-- +<?php # vim:ft=php +if (!extension_loaded('pdo_sqlite')) print 'skip not loaded'; +?> +--FILE-- +<?php +class A extends PDO +{ function __call($m, $p) {print __CLASS__."::$m\n";} } + +$a = new A('sqlite:' . __DIR__ . '/dummy.db'); + +$a->truc(); +$a->TRUC(); + +?> +--CLEAN-- +<?php +unlink(__DIR__ . '/dummy.db'); +?> +--EXPECT-- +A::truc +A::TRUC diff --git a/ext/pdo_sqlite/tests/bug48773.phpt b/ext/pdo_sqlite/tests/bug48773.phpt new file mode 100644 index 0000000..b8bdea9 --- /dev/null +++ b/ext/pdo_sqlite/tests/bug48773.phpt @@ -0,0 +1,34 @@ +--TEST-- +Bug #48773 (Incorrect error when setting PDO::ATTR_STATEMENT_CLASS with ctor_args) +--SKIPIF-- +<?php +if (!extension_loaded('pdo_sqlite')) print 'skip not loaded'; +?> +--FILE-- +<?php + +class bar extends PDOStatement { + private function __construct() { + } +} + +class foo extends PDO { + public $statementClass = 'bar'; + function __construct($dsn, $username, $password, $driver_options = array()) { + $driver_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION; + parent::__construct($dsn, $username, $password, $driver_options); + + $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array($this->statementClass, array($this))); + } +} + +$db = new foo('sqlite::memory:', '', ''); +$stmt = $db->query('SELECT 1'); +var_dump($stmt); + +?> +--EXPECTF-- +object(bar)#%d (1) { + [%u|b%"queryString"]=> + %unicode|string%(8) "SELECT 1" +} diff --git a/ext/pdo_sqlite/tests/bug50728.phpt b/ext/pdo_sqlite/tests/bug50728.phpt new file mode 100644 index 0000000..3dbbb1f --- /dev/null +++ b/ext/pdo_sqlite/tests/bug50728.phpt @@ -0,0 +1,16 @@ +--TEST-- +Bug #50728 (All PDOExceptions hardcode 'code' property to 0) +--SKIPIF-- +<?php +if (!extension_loaded('pdo_sqlite')) print 'skip not loaded'; +?> +--FILE-- +<?php +try { + $a = new PDO("sqlite:/this/path/should/not/exist.db"); +} catch (PDOException $e) { + var_dump($e->getCode()); +} +?> +--EXPECTF-- +int(14) diff --git a/ext/pdo_sqlite/tests/bug52487.phpt b/ext/pdo_sqlite/tests/bug52487.phpt new file mode 100644 index 0000000..fc65bd3 --- /dev/null +++ b/ext/pdo_sqlite/tests/bug52487.phpt @@ -0,0 +1,21 @@ +--TEST-- +Bug #52487 (PDO::FETCH_INTO leaks memory) +--SKIPIF-- +<?php # vim:ft=php +if (!extension_loaded('pdo_sqlite')) print 'skip not loaded'; +?> +--FILE-- +<?php +require dirname(__FILE__) . '/../../../ext/pdo/tests/pdo_test.inc'; +$db = PDOTest::test_factory(dirname(__FILE__) . '/common.phpt'); + +$stmt = $db->prepare("select 1 as attr"); +for ($i = 0; $i < 10; $i++) { + $stmt->setFetchMode(PDO::FETCH_INTO, new stdClass); +} + +print "ok\n"; + +?> +--EXPECT-- +ok diff --git a/ext/pdo_sqlite/tests/bug60104.phpt b/ext/pdo_sqlite/tests/bug60104.phpt new file mode 100644 index 0000000..fd36b57 --- /dev/null +++ b/ext/pdo_sqlite/tests/bug60104.phpt @@ -0,0 +1,21 @@ +--TEST-- +Bug #60104 (Segmentation Fault in pdo_sqlite when using sqliteCreateFunction()) +--SKIPIF-- +<?php +if (!extension_loaded('pdo_sqlite')) print 'skip not loaded'; +?> +--FILE-- +<?php +function setUp() +{ + $handler = new PDO( "sqlite::memory:" ); + $handler->sqliteCreateFunction( "md5", "md5", 1 ); + unset( $handler ); +} + +setUp(); +setUp(); +echo "done"; +?> +--EXPECTF-- +done diff --git a/ext/pdo_sqlite/tests/bug_42589.phpt b/ext/pdo_sqlite/tests/bug_42589.phpt new file mode 100644 index 0000000..abd5e8c --- /dev/null +++ b/ext/pdo_sqlite/tests/bug_42589.phpt @@ -0,0 +1,23 @@ +--TEST-- +PDO SQLite Feature Request #42589 (getColumnMeta() should also return table name) +--SKIPIF-- +<?php +if (!extension_loaded('pdo') || !extension_loaded('pdo_sqlite')) die('skip not loaded'); +?> +--FILE-- +<?php +$db = new PDO("sqlite::memory:"); + +$db->exec('CREATE TABLE test (field1 VARCHAR(10))'); +$db->exec('INSERT INTO test VALUES("test")'); + +$result = $db->query('SELECT * FROM test t1 LEFT JOIN test t2 ON t1.field1 = t2.field1'); +$meta1 = $result->getColumnMeta(0); +$meta2 = $result->getColumnMeta(1); + +var_dump(!empty($meta1['table']) && $meta1['table'] == 'test'); +var_dump(!empty($meta2['table']) && $meta2['table'] == 'test'); +?> +--EXPECTF-- +bool(true) +bool(true) diff --git a/ext/pdo_sqlite/tests/bug_63916-2.phpt b/ext/pdo_sqlite/tests/bug_63916-2.phpt new file mode 100644 index 0000000..bc9bfc9 --- /dev/null +++ b/ext/pdo_sqlite/tests/bug_63916-2.phpt @@ -0,0 +1,27 @@ +--TEST-- +Bug #63916 PDO::PARAM_INT casts to 32bit int internally even on 64bit builds in pdo_sqlite +--SKIPIF-- +<?php +if (!extension_loaded('pdo_sqlite')) die('skip'); +if (PHP_INT_SIZE > 4) die('skip'); +?> +--FILE-- +<?php +$num = PHP_INT_MAX; // 32 bits +$conn = new PDO('sqlite::memory:'); +$conn->query('CREATE TABLE users (id INTEGER NOT NULL, num INTEGER NOT NULL, PRIMARY KEY(id))'); + +$stmt = $conn->prepare('insert into users (id, num) values (:id, :num)'); +$stmt->bindValue(':id', 1, PDO::PARAM_INT); +$stmt->bindValue(':num', $num, PDO::PARAM_INT); +$stmt->execute(); + +$stmt = $conn->query('SELECT num FROM users'); +$result = $stmt->fetchAll(PDO::FETCH_COLUMN); + +var_dump($num,$result[0]); + +?> +--EXPECT-- +int(2147483647) +string(10) "2147483647" diff --git a/ext/pdo_sqlite/tests/bug_63916.phpt b/ext/pdo_sqlite/tests/bug_63916.phpt new file mode 100644 index 0000000..582413d --- /dev/null +++ b/ext/pdo_sqlite/tests/bug_63916.phpt @@ -0,0 +1,27 @@ +--TEST-- +Bug #63916 PDO::PARAM_INT casts to 32bit int internally even on 64bit builds in pdo_sqlite +--SKIPIF-- +<?php +if (!extension_loaded('pdo_sqlite')) die('skip'); +if (PHP_INT_SIZE < 8) die('skip'); +?> +--FILE-- +<?php +$num = 100004313234244; // exceeds 32 bits +$conn = new PDO('sqlite::memory:'); +$conn->query('CREATE TABLE users (id INTEGER NOT NULL, num INTEGER NOT NULL, PRIMARY KEY(id))'); + +$stmt = $conn->prepare('insert into users (id, num) values (:id, :num)'); +$stmt->bindValue(':id', 1, PDO::PARAM_INT); +$stmt->bindValue(':num', $num, PDO::PARAM_INT); +$stmt->execute(); + +$stmt = $conn->query('SELECT num FROM users'); +$result = $stmt->fetchAll(PDO::FETCH_COLUMN); + +var_dump($num,$result[0]); + +?> +--EXPECT-- +int(100004313234244) +string(15) "100004313234244" diff --git a/ext/pdo_sqlite/tests/common.phpt b/ext/pdo_sqlite/tests/common.phpt new file mode 100644 index 0000000..65ea0c9 --- /dev/null +++ b/ext/pdo_sqlite/tests/common.phpt @@ -0,0 +1,12 @@ +--TEST-- +SQLite +--SKIPIF-- +<?php # vim:ft=php +if (!extension_loaded('pdo_sqlite')) print 'skip'; ?> +--REDIRECTTEST-- +return array( + 'ENV' => array( + 'PDOTEST_DSN' => 'sqlite::memory:' + ), + 'TESTS' => 'ext/pdo/tests' + ); diff --git a/ext/pdo_sqlite/tests/debugdumpparams_001.phpt b/ext/pdo_sqlite/tests/debugdumpparams_001.phpt new file mode 100644 index 0000000..3e51b53 --- /dev/null +++ b/ext/pdo_sqlite/tests/debugdumpparams_001.phpt @@ -0,0 +1,37 @@ +--TEST-- +Testing PDOStatement::debugDumpParams() with bound params +--SKIPIF-- +<?php +if (!extension_loaded('pdo_sqlite')) print 'skip not loaded'; +?> +--FILE-- +<?php + +$db = new pdo('sqlite::memory:'); + +$x= $db->prepare('select :a, :b, ?'); +$x->bindValue(':a', 1, PDO::PARAM_INT); +$x->bindValue(':b', 'foo'); +$x->bindValue(3, 1313); +var_dump($x->debugDumpParams()); + +?> +--EXPECT-- +SQL: [16] select :a, :b, ? +Params: 3 +Key: Name: [2] :a +paramno=-1 +name=[2] ":a" +is_param=1 +param_type=1 +Key: Name: [2] :b +paramno=-1 +name=[2] ":b" +is_param=1 +param_type=2 +Key: Position #2: +paramno=2 +name=[0] "" +is_param=1 +param_type=2 +NULL diff --git a/ext/pdo_sqlite/tests/pdo_fetch_func_001.phpt b/ext/pdo_sqlite/tests/pdo_fetch_func_001.phpt new file mode 100644 index 0000000..efcb2e7 --- /dev/null +++ b/ext/pdo_sqlite/tests/pdo_fetch_func_001.phpt @@ -0,0 +1,130 @@ +--TEST-- +Testing several callbacks using PDO::FETCH_FUNC +--SKIPIF-- +<?php +if (!extension_loaded('pdo_sqlite')) print 'skip not loaded'; +?> +--FILE-- +<?php + +$db = new PDO('sqlite::memory:'); +$db->exec('CREATE TABLE testing (id INTEGER , name VARCHAR)'); +$db->exec('INSERT INTO testing VALUES(1, "php")'); +$db->exec('INSERT INTO testing VALUES(2, "")'); + +$st = $db->query('SELECT * FROM testing'); +$st->fetchAll(PDO::FETCH_FUNC, function($x, $y) use ($st) { var_dump($st); print "data: $x, $y\n"; }); + +$st = $db->query('SELECT name FROM testing'); +var_dump($st->fetchAll(PDO::FETCH_FUNC, 'strtoupper')); + +$st = $db->query('SELECT * FROM testing'); +var_dump($st->fetchAll(PDO::FETCH_FUNC, 'nothing')); + +$st = $db->query('SELECT * FROM testing'); +var_dump($st->fetchAll(PDO::FETCH_FUNC, '')); + +$st = $db->query('SELECT * FROM testing'); +var_dump($st->fetchAll(PDO::FETCH_FUNC, NULL)); + +$st = $db->query('SELECT * FROM testing'); +var_dump($st->fetchAll(PDO::FETCH_FUNC, 1)); + +$st = $db->query('SELECT * FROM testing'); +var_dump($st->fetchAll(PDO::FETCH_FUNC, array('self', 'foo'))); + +class foo { + public function foo($x) { + return "--- $x ---"; + } +} +class bar extends foo { + public function __construct($db) { + $st = $db->query('SELECT * FROM testing'); + var_dump($st->fetchAll(PDO::FETCH_FUNC, array($this, 'parent::foo'))); + } + + static public function test($x, $y) { + return $x .'---'. $y; + } + + private function test2($x, $y) { + return $x; + } + + public function test3($x, $y) { + return $x .'==='. $y; + } +} + +new bar($db); + +$st = $db->query('SELECT * FROM testing'); +var_dump($st->fetchAll(PDO::FETCH_FUNC, array('bar', 'test'))); + +$st = $db->query('SELECT * FROM testing'); +var_dump($st->fetchAll(PDO::FETCH_FUNC, array('bar', 'test2'))); + +$st = $db->query('SELECT * FROM testing'); +var_dump($st->fetchAll(PDO::FETCH_FUNC, array('bar', 'test3'))); + +$st = $db->query('SELECT * FROM testing'); +var_dump($st->fetchAll(PDO::FETCH_FUNC, array('bar', 'inexistent'))); + +?> +--EXPECTF-- +object(PDOStatement)#%d (1) { + [%u|b%"queryString"]=> + %string|unicode%(21) "SELECT * FROM testing" +} +data: 1, php +object(PDOStatement)#%d (1) { + [%u|b%"queryString"]=> + %string|unicode%(21) "SELECT * FROM testing" +} +data: 2, +array(2) { + [0]=> + %string|unicode%(3) "PHP" + [1]=> + %string|unicode%(0) "" +} + +Warning: PDOStatement::fetchAll(): SQLSTATE[HY000]: General error: function 'nothing' not found or invalid function name in %s on line %d +bool(false) + +Warning: PDOStatement::fetchAll(): SQLSTATE[HY000]: General error: function '' not found or invalid function name in %s on line %d +bool(false) + +Warning: PDOStatement::fetchAll(): SQLSTATE[HY000]: General error: no array or string given in %s on line %d +bool(false) + +Warning: PDOStatement::fetchAll(): SQLSTATE[HY000]: General error: no array or string given in %s on line %d +bool(false) + +Warning: PDOStatement::fetchAll(): SQLSTATE[HY000]: General error: cannot access self:: when no class scope is active in %s on line %d +bool(false) +array(2) { + [0]=> + %string|unicode%(9) "--- 1 ---" + [1]=> + %string|unicode%(9) "--- 2 ---" +} +array(2) { + [0]=> + %string|unicode%(7) "1---php" + [1]=> + %string|unicode%(4) "2---" +} + +Warning: PDOStatement::fetchAll(): SQLSTATE[HY000]: General error: cannot access private method bar::test2() in %s on line %d +bool(false) +array(2) { + [0]=> + %string|unicode%(7) "1===php" + [1]=> + %string|unicode%(4) "2===" +} + +Warning: PDOStatement::fetchAll(): SQLSTATE[HY000]: General error: class 'bar' does not have a method 'inexistent' in %s on line %d +bool(false) diff --git a/ext/pdo_sqlite/tests/pdo_sqlite_createaggregate.phpt b/ext/pdo_sqlite/tests/pdo_sqlite_createaggregate.phpt new file mode 100644 index 0000000..975dcd9 --- /dev/null +++ b/ext/pdo_sqlite/tests/pdo_sqlite_createaggregate.phpt @@ -0,0 +1,31 @@ +--TEST-- +PDO_sqlite: Testing sqliteCreateAggregate() +--SKIPIF-- +<?php if (!extension_loaded('pdo_sqlite')) print 'skip not loaded'; ?> +--FILE-- +<?php + +$db = new pdo('sqlite::memory:'); + +$db->query('CREATE TABLE IF NOT EXISTS foobar (id INT AUTO INCREMENT, name TEXT)'); + +$db->query('INSERT INTO foobar VALUES (NULL, "PHP")'); +$db->query('INSERT INTO foobar VALUES (NULL, "PHP6")'); + +$db->sqliteCreateAggregate('testing', function(&$a, $b) { $a .= $b; return $a; }, function(&$v) { return $v; }); + + +foreach ($db->query('SELECT testing(name) FROM foobar') as $row) { + var_dump($row); +} + +$db->query('DROP TABLE foobar'); + +?> +--EXPECTF-- +array(2) { + ["testing(name)"]=> + %string|unicode%(2) "12" + [0]=> + %string|unicode%(2) "12" +} diff --git a/ext/pdo_sqlite/tests/pdo_sqlite_createaggregate_002.phpt b/ext/pdo_sqlite/tests/pdo_sqlite_createaggregate_002.phpt new file mode 100644 index 0000000..671e4b3 --- /dev/null +++ b/ext/pdo_sqlite/tests/pdo_sqlite_createaggregate_002.phpt @@ -0,0 +1,17 @@ +--TEST-- +PDO_sqlite: Testing invalid callback for sqliteCreateAggregate() +--SKIPIF-- +<?php if (!extension_loaded('pdo_sqlite')) print 'skip not loaded'; ?> +--FILE-- +<?php + +$pdo = new PDO('sqlite::memory:'); + +$pdo->sqliteCreateAggregate('foo', 'a', ''); +$pdo->sqliteCreateAggregate('foo', 'strlen', ''); + +?> +--EXPECTF-- +Warning: PDO::sqliteCreateAggregate(): function 'a' is not callable in %s on line %d + +Warning: PDO::sqliteCreateAggregate(): function '' is not callable in %s on line %d diff --git a/ext/pdo_sqlite/tests/pdo_sqlite_createcollation.phpt b/ext/pdo_sqlite/tests/pdo_sqlite_createcollation.phpt new file mode 100644 index 0000000..c35e363 --- /dev/null +++ b/ext/pdo_sqlite/tests/pdo_sqlite_createcollation.phpt @@ -0,0 +1,38 @@ +--TEST-- +PDO_sqlite: Testing sqliteCreateCollation() +--SKIPIF-- +<?php if (!extension_loaded('pdo_sqlite')) print 'skip not loaded'; ?> +--FILE-- +<?php + +$db = new pdo('sqlite::memory:'); +$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + +$db->query('CREATE TABLE IF NOT EXISTS foobar (id INT AUTO INCREMENT, name TEXT)'); + +$db->query('INSERT INTO foobar VALUES (NULL, "1")'); +$db->query('INSERT INTO foobar VALUES (NULL, "2")'); +$db->query('INSERT INTO foobar VALUES (NULL, "10")'); + +$db->sqliteCreateCollation('MYCOLLATE', function($a, $b) { return strnatcmp($a, $b); }); + +$result = $db->query('SELECT name FROM foobar ORDER BY name COLLATE MYCOLLATE'); +foreach ($result as $row) { + echo $row['name'] . "\n"; +} + +$result = $db->query('SELECT name FROM foobar ORDER BY name'); +foreach ($result as $row) { + echo $row['name'] . "\n"; +} + +$db->query('DROP TABLE foobar'); + +?> +--EXPECTF-- +1 +2 +10 +1 +10 +2 diff --git a/ext/pdo_sqlite/tests/pdo_sqlite_createfunction.phpt b/ext/pdo_sqlite/tests/pdo_sqlite_createfunction.phpt new file mode 100644 index 0000000..b675879 --- /dev/null +++ b/ext/pdo_sqlite/tests/pdo_sqlite_createfunction.phpt @@ -0,0 +1,38 @@ +--TEST-- +PDO_sqlite: Testing sqliteCreateFunction() +--SKIPIF-- +<?php if (!extension_loaded('pdo_sqlite')) print 'skip not loaded'; ?> +--FILE-- +<?php + +$db = new pdo('sqlite::memory:'); + +$db->query('CREATE TABLE IF NOT EXISTS foobar (id INT AUTO INCREMENT, name TEXT)'); + +$db->query('INSERT INTO foobar VALUES (NULL, "PHP")'); +$db->query('INSERT INTO foobar VALUES (NULL, "PHP6")'); + + +$db->sqliteCreateFunction('testing', function($v) { return strtolower($v); }); + + +foreach ($db->query('SELECT testing(name) FROM foobar') as $row) { + var_dump($row); +} + +$db->query('DROP TABLE foobar'); + +?> +--EXPECTF-- +array(2) { + ["testing(name)"]=> + %string|unicode%(3) "php" + [0]=> + %string|unicode%(3) "php" +} +array(2) { + ["testing(name)"]=> + %string|unicode%(4) "php6" + [0]=> + %string|unicode%(4) "php6" +} diff --git a/ext/pdo_sqlite/tests/pdo_sqlite_get_attribute.phpt b/ext/pdo_sqlite/tests/pdo_sqlite_get_attribute.phpt new file mode 100644 index 0000000..d6e095d --- /dev/null +++ b/ext/pdo_sqlite/tests/pdo_sqlite_get_attribute.phpt @@ -0,0 +1,15 @@ +--TEST-- +PDO_sqlite: Testing getAttribute() +--SKIPIF-- +<?php if (!extension_loaded('pdo_sqlite')) print 'skip not loaded'; ?> +--FILE-- +<?php + +$pdo = new PDO('sqlite::memory:'); +var_dump($pdo->getAttribute(PDO::ATTR_SERVER_VERSION)); +var_dump($pdo->getAttribute(PDO::ATTR_CLIENT_VERSION)); + +?> +--EXPECTF-- +string(%d) "%s" +string(%d) "%s" diff --git a/ext/pdo_sqlite/tests/pdo_sqlite_lastinsertid.phpt b/ext/pdo_sqlite/tests/pdo_sqlite_lastinsertid.phpt new file mode 100644 index 0000000..2ff0acd --- /dev/null +++ b/ext/pdo_sqlite/tests/pdo_sqlite_lastinsertid.phpt @@ -0,0 +1,32 @@ +--TEST-- +PDO_sqlite: Testing lastInsertId() +--SKIPIF-- +<?php if (!extension_loaded('pdo_sqlite')) print 'skip not loaded'; ?> +--FILE-- +<?php + +$db = new pdo('sqlite::memory:'); +$db->query('CREATE TABLE IF NOT EXISTS foo (id INT AUTO INCREMENT, name TEXT)'); +$db->query('INSERT INTO foo VALUES (NULL, "PHP")'); +$db->query('INSERT INTO foo VALUES (NULL, "PHP6")'); +var_dump($db->query('SELECT * FROM foo')); +var_dump($db->errorInfo()); +var_dump($db->lastInsertId()); + +$db->query('DROP TABLE foo'); + +?> +--EXPECTF-- +object(PDOStatement)#2 (1) { + ["queryString"]=> + %string|unicode%(17) "SELECT * FROM foo" +} +array(3) { + [0]=> + %string|unicode%(5) "00000" + [1]=> + NULL + [2]=> + NULL +} +%string|unicode%(1) "2" diff --git a/ext/pdo_sqlite/tests/pdo_sqlite_transaction.phpt b/ext/pdo_sqlite/tests/pdo_sqlite_transaction.phpt new file mode 100644 index 0000000..101cc73 --- /dev/null +++ b/ext/pdo_sqlite/tests/pdo_sqlite_transaction.phpt @@ -0,0 +1,28 @@ +--TEST-- +PDO_sqlite: Testing transaction +--SKIPIF-- +<?php if (!extension_loaded('pdo_sqlite')) print 'skip not loaded'; ?> +--FILE-- +<?php + +$db = new pdo('sqlite::memory:'); + +$db->beginTransaction(); + +$db->query('CREATE TABLE IF NOT EXISTS foobar (id INT AUTO INCREMENT, name TEXT)'); +$db->commit(); + +$db->beginTransaction(); +$db->query('INSERT INTO foobar VALUES (NULL, "PHP")'); +$db->query('INSERT INTO foobar VALUES (NULL, "PHP6")'); +$db->rollback(); + +$r = $db->query('SELECT COUNT(*) FROM foobar'); +var_dump($r->rowCount()); + + +$db->query('DROP TABLE foobar'); + +?> +--EXPECTF-- +int(0) |