]> granicus.if.org Git - php/commitdiff
Added sqlite_fetch_column_types() function.
authorIlia Alshanetsky <iliaa@php.net>
Thu, 18 Dec 2003 21:28:00 +0000 (21:28 +0000)
committerIlia Alshanetsky <iliaa@php.net>
Thu, 18 Dec 2003 21:28:00 +0000 (21:28 +0000)
NEWS
ext/sqlite/php_sqlite.h
ext/sqlite/sqlite.c
ext/sqlite/tests/sqlite_026.phpt [new file with mode: 0755]
ext/sqlite/tests/sqlite_oo_028.phpt [new file with mode: 0755]

diff --git a/NEWS b/NEWS
index 49a6da95c432b0442091b98c5736f5c22634547d..0c86055c30a51bbc3cdb5994a4776227364cd094 100644 (file)
--- 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)
index 80ceba14354cb32dfe403a6d4a947507f765b56c..122c9d6aa92ceccbf88319a3249b7a55604d008b 100644 (file)
@@ -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)
index 030dabc52cfde6dd1541e6168dccdcd52f5ca5af..29a3bc1a2154fe47262cc2684a00305bf7a13d4f 100644 (file)
@@ -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 (executable)
index 0000000..c508979
--- /dev/null
@@ -0,0 +1,27 @@
+--TEST--
+sqlite: sqlite_fetch_column_types
+--SKIPIF--
+<?php # vim:ft=php
+if (!extension_loaded("sqlite")) print "skip"; ?>
+--FILE--
+<?php 
+include "blankdb.inc";
+
+sqlite_query($db, "CREATE TABLE strings(a, b INTEGER, c VARCHAR(10), d)");
+sqlite_query($db, "INSERT INTO strings VALUES('1', '2', '3', 'abc')");
+
+var_dump(sqlite_fetch_column_types($db, "strings"));
+
+sqlite_close($db);
+?>
+--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 (executable)
index 0000000..be59496
--- /dev/null
@@ -0,0 +1,25 @@
+--TEST--
+sqlite-oo: sqlite_fetch_column_types
+--SKIPIF--
+<?php # vim:ft=php
+if (!extension_loaded("sqlite")) print "skip"; ?>
+--FILE--
+<?php 
+include "blankdb_oo.inc";
+
+$db->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) ""
+}