- SQLite depends on SPL if it is present

- Add SQLiteResult::count()
- SQLiteResult implements Countable if present
This commit is contained in:
Marcus Boerger 2005-02-15 21:09:42 +00:00
parent 8ff4a1002c
commit 7956ca0211
4 changed files with 54 additions and 7 deletions

View File

@ -70,6 +70,7 @@ if test "$PHP_SQLITE" != "no"; then
libsqlite/src/where.c libsqlite/src/trigger.c"
PHP_NEW_EXTENSION(sqlite, sqlite.c sess_sqlite.c $sources, $ext_shared,,$PHP_SQLITE_CFLAGS)
PHP_ADD_EXTENSION_DEP(sqlite, spl)
PHP_ADD_BUILD_DIR($ext_builddir/libsqlite)
PHP_ADD_BUILD_DIR($ext_builddir/libsqlite/src)
AC_CHECK_SIZEOF(char *,4)

View File

@ -27,6 +27,5 @@ if (PHP_SQLITE != "no") {
if (!PHP_SQLITE_SHARED) {
ADD_DEF_FILE(configure_module_dirname + "\\php_sqlite.def");
}
ADD_EXTENSION_DEP('sqlite', 'spl')
}

View File

@ -46,6 +46,7 @@
#ifdef HAVE_SPL
extern PHPAPI zend_class_entry *spl_ce_RuntimeException;
extern PHPAPI zend_class_entry *spl_ce_Countable;
#endif
#ifndef safe_emalloc
@ -65,6 +66,8 @@ extern int sqlite_decode_binary(const unsigned char *in, unsigned char *out);
#define php_sqlite_encode_binary(in, n, out) sqlite_encode_binary((const unsigned char *)in, n, (unsigned char *)out)
#define php_sqlite_decode_binary(in, out) sqlite_decode_binary((const unsigned char *)in, (unsigned char *)out)
static int sqlite_count_elements(zval *object, long *count TSRMLS_DC);
static int le_sqlite_db, le_sqlite_result, le_sqlite_pdb;
static inline void php_sqlite_strtoupper(char *s)
@ -90,7 +93,6 @@ STD_PHP_INI_ENTRY_EX("sqlite.assoc_case", "0", PHP_INI_ALL, OnUpdateLong, assoc_
PHP_INI_END()
/* }}} */
#define DB_FROM_ZVAL(db, zv) ZEND_FETCH_RESOURCE2(db, struct php_sqlite_db *, zv, -1, "sqlite database", le_sqlite_db, le_sqlite_pdb)
#define DB_FROM_OBJECT(db, object) \
@ -113,10 +115,6 @@ PHP_INI_END()
} \
}
#define SQLITE_THROW(message) \
PG(suppress_errors) = 0; \
EG(exception) = zend_throw_exception(sqlite_ce_exception, message, 0 TSRMLS_CC);
#define PHP_SQLITE_EMPTY_QUERY \
if (!sql_len) { \
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot execute empty query."); \
@ -234,6 +232,8 @@ function_entry sqlite_funcs_query[] = {
PHP_ME_MAPPING(next, sqlite_next, NULL)
PHP_ME_MAPPING(valid, sqlite_valid, NULL)
PHP_ME_MAPPING(rewind, sqlite_rewind, NULL)
/* countable */
PHP_ME_MAPPING(count, sqlite_num_rows, NULL)
/* additional */
PHP_ME_MAPPING(prev, sqlite_prev, NULL)
PHP_ME_MAPPING(hasPrev, sqlite_has_prev, NULL)
@ -1009,11 +1009,16 @@ PHP_MINIT_FUNCTION(sqlite)
#endif
sqlite_object_handlers_query.get_class_entry = sqlite_get_ce_query;
sqlite_object_handlers_ub_query.get_class_entry = sqlite_get_ce_ub_query;
sqlite_object_handlers_ub_query.count_elements = sqlite_count_elements;
sqlite_ce_ub_query->get_iterator = sqlite_get_iterator;
sqlite_ce_ub_query->iterator_funcs.funcs = &sqlite_ub_query_iterator_funcs;
#ifdef HAVE_SPL
zend_class_implements(sqlite_ce_query TSRMLS_CC, 2, zend_ce_iterator, spl_ce_Countable);
#else
zend_class_implements(sqlite_ce_query TSRMLS_CC, 1, zend_ce_iterator);
#endif
sqlite_ce_query->get_iterator = sqlite_get_iterator;
sqlite_ce_query->iterator_funcs.funcs = &sqlite_query_iterator_funcs;
@ -2398,6 +2403,19 @@ PHP_FUNCTION(sqlite_last_insert_rowid)
}
/* }}} */
static int sqlite_count_elements(zval *object, long *count TSRMLS_DC) /* {{{ */
{
sqlite_object *obj = (sqlite_object*) zend_object_store_get_object(object TSRMLS_CC);
if (obj->u.res->buffered) {
* count = obj->u.res->nrows;
return SUCCESS;
} else {
zend_throw_exception(sqlite_ce_exception, "Row count is not available for unbuffered queries", 0 TSRMLS_CC);
return FAILURE;
}
} /* }}} */
/* {{{ proto int sqlite_num_rows(resource result)
Returns the number of rows in a buffered result set. */
PHP_FUNCTION(sqlite_num_rows)

View File

@ -0,0 +1,29 @@
--TEST--
sqlite-oo: and SPL Countable
--SKIPIF--
<?php # vim:ft=php
if (!extension_loaded("sqlite")) print "skip";
if (!extension_loaded("spl")) print "skip SPL is not present";
?>
--FILE--
<?php
include "blankdb_oo.inc";
$db->query("CREATE TABLE menu(id_l int PRIMARY KEY, id_r int UNIQUE, key VARCHAR(10))");
$db->query("INSERT INTO menu VALUES( 1, 12, 'A')");
$db->query("INSERT INTO menu VALUES( 2, 9, 'B')");
$db->query("INSERT INTO menu VALUES(10, 11, 'F')");
$db->query("INSERT INTO menu VALUES( 3, 6, 'C')");
$db->query("INSERT INTO menu VALUES( 7, 8, 'E')");
$db->query("INSERT INTO menu VALUES( 4, 5, 'D')");
$res = $db->query("SELECT * from menu");
var_dump($res->count());
var_dump(count($res));
?>
===DONE===
--EXPECT--
int(6)
int(6)
===DONE===