php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #47051
Patch pgsql_convert_boolean_and_integer_in_results revision 2012-01-27 01:33 UTC by morphunreal at gmail dot com
revision 2012-01-26 13:13 UTC by morphunreal at gmail dot com

Patch pgsql_convert_boolean_and_integer_in_results for PostgreSQL related Bug #47051

Patch version 2012-01-26 13:13 UTC

Return to Bug #47051 | Download this patch
This patch is obsolete

Obsoleted by patches:

Patch Revisions:

Developer: morphunreal@gmail.com

Index: pgsql.c
===================================================================
--- pgsql.c	(revision 322809)
+++ pgsql.c	(working copy)
@@ -73,7 +73,16 @@
 	RETURN_LONG((long)oid); \
 } while(0)
 
+/* from postgresql/src/include/catalog/pg_type.h */
 
+#define PGSQL_OID_BOOL     16
+#define PGSQL_OID_BYTEA    17
+#define PGSQL_OID_INT8     20
+#define PGSQL_OID_INT2     21
+#define PGSQL_OID_INT4     23
+#define PGSQL_OID_TEXT     25
+#define PGSQL_OID_OID      26
+
 #if HAVE_PQSETNONBLOCKING
 #define PQ_SETNONBLOCKING(pg_link, flag) PQsetnonblocking(pg_link, flag)
 #else
@@ -905,11 +914,13 @@
  */
 PHP_INI_BEGIN()
 STD_PHP_INI_BOOLEAN( "pgsql.allow_persistent",      "1",  PHP_INI_SYSTEM, OnUpdateBool, allow_persistent,      zend_pgsql_globals, pgsql_globals)
-STD_PHP_INI_ENTRY_EX("pgsql.max_persistent",       "-1",  PHP_INI_SYSTEM, OnUpdateLong, max_persistent,        zend_pgsql_globals, pgsql_globals, display_link_numbers)
-STD_PHP_INI_ENTRY_EX("pgsql.max_links",            "-1",  PHP_INI_SYSTEM, OnUpdateLong, max_links,             zend_pgsql_globals, pgsql_globals, display_link_numbers)
+STD_PHP_INI_ENTRY_EX("pgsql.max_persistent",        "-1",  PHP_INI_SYSTEM, OnUpdateLong, max_persistent,       zend_pgsql_globals, pgsql_globals, display_link_numbers)
+STD_PHP_INI_ENTRY_EX("pgsql.max_links",             "-1",  PHP_INI_SYSTEM, OnUpdateLong, max_links,            zend_pgsql_globals, pgsql_globals, display_link_numbers)
 STD_PHP_INI_BOOLEAN( "pgsql.auto_reset_persistent", "0",  PHP_INI_SYSTEM, OnUpdateBool, auto_reset_persistent, zend_pgsql_globals, pgsql_globals)
 STD_PHP_INI_BOOLEAN( "pgsql.ignore_notice",         "0",  PHP_INI_ALL,    OnUpdateBool, ignore_notices,        zend_pgsql_globals, pgsql_globals)
 STD_PHP_INI_BOOLEAN( "pgsql.log_notice",            "0",  PHP_INI_ALL,    OnUpdateBool, log_notices,           zend_pgsql_globals, pgsql_globals)
+STD_PHP_INI_BOOLEAN( "pgsql.convert_boolean_type",  "0",  PHP_INI_ALL,    OnUpdateBool, convert_boolean_type,  zend_pgsql_globals, pgsql_globals)
+STD_PHP_INI_BOOLEAN( "pgsql.convert_integer_type",  "0",  PHP_INI_ALL,    OnUpdateBool, convert_integer_type,  zend_pgsql_globals, pgsql_globals)
 PHP_INI_END()
 /* }}} */
 
@@ -2424,7 +2435,7 @@
 	char            *field_name;
 	zval            *ctor_params = NULL;
 	zend_class_entry *ce = NULL;
-
+	
 	if (into_object) {
 		char *class_name = NULL;
 		int class_name_len;
@@ -2498,27 +2509,51 @@
 		} else {
 			char *element = PQgetvalue(pgsql_result, pgsql_row, i);
 			if (element) {
+				Oid oid = PQftype(pgsql_result, i); 
 				char *data;
 				int data_len;
 				int should_copy=0;
 				const uint element_len = strlen(element);
-
-				if (PG(magic_quotes_runtime)) {
-					data = php_addslashes(element, element_len, &data_len, 0 TSRMLS_CC);
+				
+				if (oid == PGSQL_OID_BOOL && PGG(convert_boolean_type)){
+					int boolval = *element != 'f' ? 1 : 0;
+					if (result_type & PGSQL_NUM) {
+						add_index_bool(return_value, i, boolval);
+					}
+				
+					if (result_type & PGSQL_ASSOC) {
+						field_name = PQfname(pgsql_result, i);
+						add_assoc_bool(return_value, field_name, boolval);
+					}
+				} else if ((oid == PGSQL_OID_INT2 || oid == PGSQL_OID_INT4 || 
+							(oid == PGSQL_OID_INT8 && sizeof(long)>=8)) && PGG(convert_integer_type)){
+					long int longval = atol(element);
+					if (result_type & PGSQL_NUM) {
+						add_index_long(return_value, i, longval);
+					}
+				
+					if (result_type & PGSQL_ASSOC) {
+						field_name = PQfname(pgsql_result, i);
+						add_assoc_long(return_value, field_name, longval);
+					}
 				} else {
-					data = safe_estrndup(element, element_len);
-					data_len = element_len;
-				}
-			
-				if (result_type & PGSQL_NUM) {
-					add_index_stringl(return_value, i, data, data_len, should_copy);
-					should_copy=1;
-				}
-			
-				if (result_type & PGSQL_ASSOC) {
-					field_name = PQfname(pgsql_result, i);
-					add_assoc_stringl(return_value, field_name, data, data_len, should_copy);
-				}
+					if (PG(magic_quotes_runtime)) {
+						data = php_addslashes(element, element_len, &data_len, 0 TSRMLS_CC);
+					} else {
+						data = safe_estrndup(element, element_len);
+						data_len = element_len;
+					}
+				
+					if (result_type & PGSQL_NUM) {
+						add_index_stringl(return_value, i, data, data_len, should_copy);
+						should_copy=1;
+					}
+				
+					if (result_type & PGSQL_ASSOC) {
+						field_name = PQfname(pgsql_result, i);
+						add_assoc_stringl(return_value, field_name, data, data_len, should_copy);
+					}
+				}	
 			}
 		}
 	}
Index: php_pgsql.h
===================================================================
--- php_pgsql.h	(revision 322809)
+++ php_pgsql.h	(working copy)
@@ -284,6 +284,7 @@
 	int le_lofp,le_string;
 	int ignore_notices,log_notices;
 	HashTable notices;  /* notice message for each connection */
+	int convert_boolean_type, convert_integer_type;
 ZEND_END_MODULE_GLOBALS(pgsql)
 
 ZEND_EXTERN_MODULE_GLOBALS(pgsql)
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue Nov 30 10:03:35 2021 UTC