php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #46807 missing sqlite_field_type function
Submitted: 2008-12-09 06:22 UTC Modified: 2017-10-24 23:31 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: pwhelan at exis dot cl Assigned:
Status: Suspended Package: SQLite (PECL)
PHP Version: 5.2.8 OS:
Private report: No CVE-ID: None
 [2008-12-09 06:22 UTC] 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


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-12-09 06:29 UTC] 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)
 [2011-04-08 20:49 UTC] jani@php.net
-Package: Feature/Change Request +Package: SQLite related
 [2016-06-27 16:18 UTC] cmb@php.net
-Package: SQLite related +Package: SQLite
 [2017-10-24 23:31 UTC] kalle@php.net
-Status: Open +Status: Suspended
 [2017-10-24 23:31 UTC] kalle@php.net
The SQLite extension was moved out of the PHP source and back in to PECL and have not since had a release or any activity, I would highly suggest to use the bundled sqlite3 extension instead. If development begins at this extension, then this report should be re-opened
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Oct 07 14:01:27 2024 UTC