From: Ilia Alshanetsky Date: Thu, 18 Dec 2003 21:28:00 +0000 (+0000) Subject: Added sqlite_fetch_column_types() function. X-Git-Tag: php-5.0.0b3RC2~50 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=79848fa88532e2663de9a5866cfb8e47f27f0aee;p=php Added sqlite_fetch_column_types() function. --- diff --git a/NEWS b/NEWS index 49a6da95c4..0c86055c30 100644 --- a/NEWS +++ b/NEWS @@ -29,6 +29,7 @@ PHP NEWS . stream_socket_sendto() and stream_socket_recvfrom(). (Wez) . iconv_mime_decode_headers(). (Moriyoshi) . get_declared_interfaces(). (Andrey, Marcus) + . sqlite_fetch_column_types(). (Ilia) - Added proxy support to http:// wrapper. (Sara) - Added rename(), rmdir() and mkdir() support to userstreams. (Sara) - Added rename(), rmdir() and mkdir() support to ftp:// wrapper. (Sara) diff --git a/ext/sqlite/php_sqlite.h b/ext/sqlite/php_sqlite.h index 80ceba1435..122c9d6aa9 100644 --- a/ext/sqlite/php_sqlite.h +++ b/ext/sqlite/php_sqlite.h @@ -88,6 +88,8 @@ PHP_FUNCTION(sqlite_udf_encode_binary); PHP_FUNCTION(sqlite_factory); +PHP_FUNCTION(sqlite_fetch_column_types); + ZEND_BEGIN_MODULE_GLOBALS(sqlite) int assoc_case; ZEND_END_MODULE_GLOBALS(sqlite) diff --git a/ext/sqlite/sqlite.c b/ext/sqlite/sqlite.c index 030dabc52c..29a3bc1a21 100644 --- a/ext/sqlite/sqlite.c +++ b/ext/sqlite/sqlite.c @@ -192,6 +192,7 @@ function_entry sqlite_functions[] = { PHP_FE(sqlite_factory, third_arg_force_ref) PHP_FE(sqlite_udf_encode_binary, NULL) PHP_FE(sqlite_udf_decode_binary, NULL) + PHP_FE(sqlite_fetch_column_types, NULL) {NULL, NULL, NULL} }; @@ -211,6 +212,7 @@ function_entry sqlite_funcs_db[] = { PHP_ME_MAPPING(create_function, sqlite_create_function, NULL) PHP_ME_MAPPING(busy_timeout, sqlite_busy_timeout, NULL) PHP_ME_MAPPING(last_error, sqlite_last_error, NULL) + PHP_ME_MAPPING(fetch_column_types, sqlite_fetch_column_types, NULL) /* PHP_ME_MAPPING(error_string, sqlite_error_string, NULL) static */ /* PHP_ME_MAPPING(escape_string, sqlite_escape_string, NULL) static */ {NULL, NULL, NULL} @@ -1552,6 +1554,72 @@ PHP_FUNCTION(sqlite_unbuffered_query) } /* }}} */ +/* {{{ proto resource sqlite_fetch_column_types(string table_name, resource db) + Return an array of column types from a particular table. */ +PHP_FUNCTION(sqlite_fetch_column_types) +{ + zval *zdb; + struct php_sqlite_db *db; + char *tbl, *sql; + long tbl_len; + char *errtext = NULL; + zval *object = getThis(); + struct php_sqlite_result res; + const char **rowdata, **colnames, *tail; + int i, ncols; + + if (object) { + if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &tbl, &tbl_len)) { + return; + } + DB_FROM_OBJECT(db, object); + } else { + if (FAILURE == zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, + ZEND_NUM_ARGS() TSRMLS_CC, "sr", &tbl, &tbl_len, &zdb) && + FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &zdb, &tbl, &tbl_len)) { + return; + } + DB_FROM_ZVAL(db, &zdb); + } + + if (!(sql = sqlite_mprintf("SELECT * FROM %q LIMIT 1", tbl))) { + RETURN_FALSE; + } + + sqlite_exec(db->db, "PRAGMA show_datatypes = ON", NULL, NULL, &errtext); + + db->last_err_code = sqlite_compile(db->db, sql, &tail, &res.vm, &errtext); + + sqlite_freemem(sql); + + if (db->last_err_code != SQLITE_OK) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", errtext); + sqlite_freemem(errtext); + RETVAL_FALSE; + goto done; + } + + sqlite_step(res.vm, &ncols, &rowdata, &colnames); + + array_init(return_value); + + for (i = 0; i < ncols; i++) { + char *colname = (char *)colnames[i]; + + if (SQLITE_G(assoc_case) == 1) { + php_sqlite_strtoupper(colname); + } else if (SQLITE_G(assoc_case) == 2) { + php_sqlite_strtolower(colname); + } + + add_assoc_string(return_value, colname, colnames[ncols + i] ? (char *)colnames[ncols + i] : "", 1); + } + +done: + sqlite_exec(db->db, "PRAGMA show_datatypes = OFF", NULL, NULL, &errtext); +} +/* }}} */ + /* {{{ proto resource sqlite_query(string query, resource db [, int result_type ]) Executes a query against a given database and returns a result handle. */ PHP_FUNCTION(sqlite_query) diff --git a/ext/sqlite/tests/sqlite_026.phpt b/ext/sqlite/tests/sqlite_026.phpt new file mode 100755 index 0000000000..c508979a27 --- /dev/null +++ b/ext/sqlite/tests/sqlite_026.phpt @@ -0,0 +1,27 @@ +--TEST-- +sqlite: sqlite_fetch_column_types +--SKIPIF-- + +--FILE-- + +--EXPECT-- +array(4) { + ["a"]=> + string(0) "" + ["b"]=> + string(7) "INTEGER" + ["c"]=> + string(11) "VARCHAR(10)" + ["d"]=> + string(0) "" +} diff --git a/ext/sqlite/tests/sqlite_oo_028.phpt b/ext/sqlite/tests/sqlite_oo_028.phpt new file mode 100755 index 0000000000..be59496ce1 --- /dev/null +++ b/ext/sqlite/tests/sqlite_oo_028.phpt @@ -0,0 +1,25 @@ +--TEST-- +sqlite-oo: sqlite_fetch_column_types +--SKIPIF-- + +--FILE-- +query("CREATE TABLE strings(a, b INTEGER, c VARCHAR(10), d)"); +$db->query("INSERT INTO strings VALUES('1', '2', '3', 'abc')"); + +var_dump($db->fetch_column_types("strings")); +?> +--EXPECT-- +array(4) { + ["a"]=> + string(0) "" + ["b"]=> + string(7) "INTEGER" + ["c"]=> + string(11) "VARCHAR(10)" + ["d"]=> + string(0) "" +}