From 27c3c301bd184029b199a2c53fb1c765b66e990c Mon Sep 17 00:00:00 2001
From: Ard Biesheuvel <abies@php.net>
Date: Fri, 21 May 2004 13:46:04 +0000
Subject: Added test for duplicate field names when returning rows as hashes or
 objects # This change should be in 5.0.0, as it might break BC

---
 ext/interbase/ibase_query.c | 28 ++++++++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)

(limited to 'ext/interbase')

diff --git a/ext/interbase/ibase_query.c b/ext/interbase/ibase_query.c
index 463bcd66c5..2e370601ff 100644
--- a/ext/interbase/ibase_query.c
+++ b/ext/interbase/ibase_query.c
@@ -1504,7 +1504,31 @@ static void _php_ibase_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, int fetch_type)
 	array_init(return_value);
 
 	for (i = 0; i < ib_result->out_sqlda->sqld; ++i) {
+		char alias[METADATALENGTH+4];
 		XSQLVAR *var = &ib_result->out_sqlda->sqlvar[i];
+		
+		if (! (fetch_type & FETCH_ROW)) {
+			int i = 0;
+			char const *base = "FIELD"; /* use 'FIELD' if name is empty */
+			
+			/**
+			* Ensure no two columns have identical names: 
+			* keep generating new names until we find one that is unique.
+			*/
+			switch (*var->aliasname) {
+				void *p;
+				
+				default:
+					i = 1;
+					strcpy(alias, base = var->aliasname);
+					
+					while (SUCCESS == zend_symtable_find(Z_ARRVAL_P(return_value),alias,strlen(alias)+1,&p)) {
+				
+				case '\0':
+						sprintf(alias, "%s_%02d", base, i++);
+					}
+			}
+		}
 
 		if (((var->sqltype & 1) == 0) || *var->sqlind != -1) {
 			zval *result;
@@ -1614,13 +1638,13 @@ static void _php_ibase_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, int fetch_type)
 			if (fetch_type & FETCH_ROW) {
 				add_index_zval(return_value, i, result);
 			} else {
-				add_assoc_zval(return_value, var->aliasname, result);
+				add_assoc_zval(return_value, alias, result);
 			}
 		} else {
 			if (fetch_type & FETCH_ROW) {
 				add_index_null(return_value, i);
 			} else {
-				add_assoc_null(return_value, var->aliasname);
+				add_assoc_null(return_value, alias);
 			}
 		}
 	} /* for field */
-- 
cgit v1.2.1