ID: 46807 User updated by: pwhelan at exis dot cl Reported By: pwhelan at exis dot cl Status: Open -Bug Type: Feature/Change Request +Bug Type: SQLite related PHP Version: 5.2.8 New Comment:
This is related directly to SQLite. Previous Comments: ------------------------------------------------------------------------ [2008-12-09 06:29:50] pwhelan at exis dot cl I found no other way to include the patch, so here it is: diff -u php-5.2.6/ext/sqlite/php_sqlite.h php5-5.2.6/ext/sqlite/php_sqlite.h --- php-5.2.6/ext/sqlite/php_sqlite.h 2007-12-31 04:20:11.000000000 -0300 +++ php5-5.2.6/ext/sqlite/php_sqlite.h 2008-12-09 02:55:26.000000000 -0300 @@ -60,6 +60,7 @@ PHP_FUNCTION(sqlite_num_rows); PHP_FUNCTION(sqlite_num_fields); PHP_FUNCTION(sqlite_field_name); +PHP_FUNCTION(sqlite_field_type); PHP_FUNCTION(sqlite_seek); PHP_FUNCTION(sqlite_rewind); PHP_FUNCTION(sqlite_next); diff -u php-5.2.6/ext/sqlite/sqlite.c php5-5.2.6/ext/sqlite/sqlite.c --- php-5.2.6/ext/sqlite/sqlite.c 2007-12-31 04:20:11.000000000 -0300 +++ php5-5.2.6/ext/sqlite/sqlite.c 2008-12-09 02:54:50.000000000 -0300 @@ -138,6 +138,7 @@ int nrows; int curr_row; char **col_names; + char **col_types; int alloc_rows; int mode; char **table; @@ -186,6 +187,7 @@ PHP_FE(sqlite_num_rows, NULL) PHP_FE(sqlite_num_fields, NULL) PHP_FE(sqlite_field_name, NULL) + PHP_FE(sqlite_field_type, NULL) PHP_FE(sqlite_seek, NULL) PHP_FE(sqlite_rewind, NULL) PHP_FE(sqlite_next, NULL) @@ -235,6 +237,7 @@ PHP_ME_MAPPING(column, sqlite_column, NULL, 0) PHP_ME_MAPPING(numFields, sqlite_num_fields, NULL, 0) PHP_ME_MAPPING(fieldName, sqlite_field_name, NULL, 0) + PHP_ME_MAPPING(fieldType, sqlite_field_type, NULL, 0) /* iterator */ PHP_ME_MAPPING(current, sqlite_current, NULL, 0) PHP_ME_MAPPING(key, sqlite_key, NULL, 0) @@ -259,6 +262,7 @@ PHP_ME_MAPPING(column, sqlite_column, NULL, 0) PHP_ME_MAPPING(numFields, sqlite_num_fields, NULL, 0) PHP_ME_MAPPING(fieldName, sqlite_field_name, NULL, 0) + PHP_ME_MAPPING(fieldType, sqlite_field_type, NULL, 0) /* iterator */ PHP_ME_MAPPING(current, sqlite_current, NULL, 0) PHP_ME_MAPPING(next, sqlite_next, NULL, 0) @@ -396,7 +400,12 @@ } efree(res->col_names); } - + if (res->col_types) { + for (j = 0; j < res->ncolumns; j++) { + efree(res->col_types[j]); + } + efree(res->col_types); + } if (res->db) { zend_list_delete(res->db->rsrc_id); } @@ -1448,6 +1457,16 @@ php_sqlite_strtolower(rres->col_names[i]); } } + rres->col_types = safe_emalloc(rres->ncolumns, sizeof(char *), 0); + for (i = 0; i < rres->ncolumns; i++) { + rres->col_types[i] = estrdup((char*)colnames[i+rres->ncolumns]); + + if (SQLITE_G(assoc_case) == 1) { + php_sqlite_strtoupper(rres->col_types[i]); + } else if (SQLITE_G(assoc_case) == 2) { + php_sqlite_strtolower(rres->col_types[i]); + } + } if (!rres->buffered) { /* non buffered mode - also fetch memory for on single row */ rres->table = safe_emalloc(rres->ncolumns, sizeof(char *), 0); @@ -2624,6 +2643,34 @@ RETURN_STRING(res->col_names[field], 1); } +/* {{{ proto string sqlite_field_type(resource result, int field_index) + Returns the type of a particular field of a result set. */ +PHP_FUNCTION(sqlite_field_type) +{ + zval *zres; + struct php_sqlite_result *res; + long field; + zval *object = getThis(); + + if (object) { + if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &field)) { + return; + } + RES_FROM_OBJECT(res, object); + } else { + if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &zres, &field)) { + return; + } + ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result); + } + + if (field < 0 || field >= res->ncolumns) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "field %ld out of range", field); + RETURN_FALSE; + } + + RETURN_STRING(res->col_types[field], 1); +} /* }}} */ /* {{{ proto bool sqlite_seek(resource result, int row) ------------------------------------------------------------------------ [2008-12-09 06:22:01] pwhelan at exis dot cl Description: ------------ The function sqlite_field_type does not exist. The function sqlite_fetch_column_types is a poor substitute when working with queries with JOINs, since it would require a full blown SQL Parser. This function is trivial to implement taking advantage of the behaviour of sqlite_fetch, which passes the types of the columns in the same way it passes the column names. Reproduce code: --------------- SQL: CREATE TABLE foo ( id INTEGER PRIMARY KEY, foo TEXT, bar VARCHAR ); <?php $db = sqlite_open("database.db"); $res = sqlite_query("SELECT * FROM table LIMIT 1"); for ($i=0; $i < sqlite_num_fields($res); $i++) { print "FIELD ".sqlite_field_name($res, $i). "has type ".sqlite_field_type($res, $i); } ?> Expected result: ---------------- FIELD id has type INTEGER FIELD foo has type TEXT FIELD bar has type VARCHAR Actual result: -------------- Fatal error: Call to undefined function sqlite_field_types() in /var/www/sys/db/fields.php on line 8 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=46807&edit=1