From 58df3f785e0b66294c4a30c073cd206932711071 Mon Sep 17 00:00:00 2001
From: Hiroshi Inoue <inoue@tpf.co.jp>
Date: Sat, 3 Nov 2001 06:53:50 +0000
Subject: [PATCH] 1) Improve literal handling in parse_statement(). 2) Remove
 some no longer valid comments. 3) Fix an option dialog setting bug. 4) Fix
 ODBCVER handling errors.

---
 src/interfaces/odbc/dlg_specific.c | 19 +++++++++-------
 src/interfaces/odbc/dlg_specific.h |  4 ++--
 src/interfaces/odbc/odbcapi.c      | 11 ++++-----
 src/interfaces/odbc/odbcapi30.c    |  4 +++-
 src/interfaces/odbc/options.c      |  4 ++--
 src/interfaces/odbc/parse.c        | 36 +++++++++++++++++-------------
 src/interfaces/odbc/psqlodbc.rc    |  4 ++--
 7 files changed, 45 insertions(+), 37 deletions(-)

diff --git a/src/interfaces/odbc/dlg_specific.c b/src/interfaces/odbc/dlg_specific.c
index e73c47e314e..d26482161ae 100644
--- a/src/interfaces/odbc/dlg_specific.c
+++ b/src/interfaces/odbc/dlg_specific.c
@@ -439,15 +439,18 @@ updateCommons(const ConnInfo *ci)
 								 INI_KSQO, tmp, fileName);
 
 	/*
-	 * Never update the onlyread, unique_index from this module
-	 * sprintf(tmp, "%d", comval->unique_index);
-	 * SQLWritePrivateProfileString(sectionName, INI_UNIQUEINDEX, tmp,
-	 * fileName);
-	 *
-	 * sprintf(tmp, "%d", comval->onlyread);
-	 * SQLWritePrivateProfileString(sectionName, INI_READONLY, tmp,
-	 * fileName);
+	 * Never update the onlyread, unique_index from this module.
 	 */
+	if (!ci)
+	{
+		sprintf(tmp, "%d", comval->unique_index);
+		SQLWritePrivateProfileString(sectionName, INI_UNIQUEINDEX, tmp,
+			fileName);
+
+	 	sprintf(tmp, "%d", comval->onlyread);
+	 	SQLWritePrivateProfileString(sectionName, INI_READONLY, tmp,
+			fileName);
+	}
 
 	sprintf(tmp, "%d", comval->use_declarefetch);
 	SQLWritePrivateProfileString(sectionName,
diff --git a/src/interfaces/odbc/dlg_specific.h b/src/interfaces/odbc/dlg_specific.h
index 5a8c7514852..3d9985794da 100644
--- a/src/interfaces/odbc/dlg_specific.h
+++ b/src/interfaces/odbc/dlg_specific.h
@@ -93,7 +93,7 @@
 
 /*	Connection Defaults */
 #define DEFAULT_PORT					"5432"
-#define DEFAULT_READONLY				1
+#define DEFAULT_READONLY				0
 #define DEFAULT_PROTOCOL				"6.4"	/* the latest protocol is
 												 * the default */
 #define DEFAULT_USEDECLAREFETCH			0
@@ -102,7 +102,7 @@
 #define DEFAULT_BOOLSASCHAR				1
 #define DEFAULT_OPTIMIZER				1		/* disable */
 #define DEFAULT_KSQO					1		/* on */
-#define DEFAULT_UNIQUEINDEX				0		/* dont recognize */
+#define DEFAULT_UNIQUEINDEX				1		/* dont recognize */
 #define DEFAULT_COMMLOG					0		/* dont log */
 #define DEFAULT_DEBUG					0
 #define DEFAULT_UNKNOWNSIZES			UNKNOWNS_AS_MAX
diff --git a/src/interfaces/odbc/odbcapi.c b/src/interfaces/odbc/odbcapi.c
index 4604c14157e..a87c35850c4 100644
--- a/src/interfaces/odbc/odbcapi.c
+++ b/src/interfaces/odbc/odbcapi.c
@@ -26,9 +26,6 @@
  *-------
  */
 
-#ifdef	WIN32
-#define ODBCVER_REP 0x3000
-#endif
 #include "psqlodbc.h"
 #include <stdio.h>
 #include <string.h>
@@ -200,7 +197,7 @@ SQLFetch(HSTMT StatementHandle)
 {
 	static char *func = "SQLFetch";
 
-#if (ODBCVER >= 0x3000)
+#if (ODBCVER >= 0x0300)
 	StatementClass *stmt = (StatementClass *) StatementHandle;
 	ConnectionClass *conn = SC_get_conn(stmt);
 
@@ -273,7 +270,7 @@ SQLGetFunctions(HDBC ConnectionHandle,
 				SQLUSMALLINT FunctionId, SQLUSMALLINT *Supported)
 {
 	mylog("[SQLGetFunctions]");
-#if (ODBCVER >= 0x3000)
+#if (ODBCVER >= 0x0300)
 	if (FunctionId == SQL_API_ODBC3_ALL_FUNCTIONS)
 		return PGAPI_GetFunctions30(ConnectionHandle, FunctionId, Supported);
 #endif
@@ -284,14 +281,14 @@ SQLGetInfo(HDBC ConnectionHandle,
 		   SQLUSMALLINT InfoType, PTR InfoValue,
 		   SQLSMALLINT BufferLength, SQLSMALLINT *StringLength)
 {
-#if (ODBCVER >= 0x3000)
+#if (ODBCVER >= 0x0300)
 	RETCODE		ret;
 
 	mylog("[SQLGetInfo(30)]");
 	if ((ret = PGAPI_GetInfo(ConnectionHandle, InfoType, InfoValue,
 							 BufferLength, StringLength)) == SQL_ERROR)
 	{
-		if (((ConnectionClass *) ConnectionHandle)->driver_version >= 0x3000)
+		if (((ConnectionClass *) ConnectionHandle)->driver_version >= 0x0300)
 			return PGAPI_GetInfo30(ConnectionHandle, InfoType, InfoValue,
 								   BufferLength, StringLength);
 	}
diff --git a/src/interfaces/odbc/odbcapi30.c b/src/interfaces/odbc/odbcapi30.c
index 161940aed87..5a63748bf50 100644
--- a/src/interfaces/odbc/odbcapi30.c
+++ b/src/interfaces/odbc/odbcapi30.c
@@ -18,7 +18,9 @@
  *-------
  */
 
+#ifndef ODBCVER_REP
 #define ODBCVER_REP 0x0300
+#endif
 #include "psqlodbc.h"
 #include <stdio.h>
 #include <string.h>
@@ -532,7 +534,7 @@ PGAPI_GetFunctions30(HDBC hdbc, UWORD fFunction, UWORD FAR * pfExists)
 {
 	if (fFunction != SQL_API_ODBC3_ALL_FUNCTIONS)
 		return SQL_ERROR;
-	memset(pfExists, 0, sizeof(UWORD) * 250);
+	memset(pfExists, 0, sizeof(UWORD) * SQL_API_ODBC3_ALL_FUNCTIONS_SIZE);
 
 	/* SQL_FUNC_ESET(pfExists, SQL_API_SQLALLOCCONNECT); 1 deprecated */
 	/* SQL_FUNC_ESET(pfExists, SQL_API_SQLALLOCENV); 2 deprecated */
diff --git a/src/interfaces/odbc/options.c b/src/interfaces/odbc/options.c
index 04331456c91..bf2707f4dae 100644
--- a/src/interfaces/odbc/options.c
+++ b/src/interfaces/odbc/options.c
@@ -178,7 +178,7 @@ set_statement_option(ConnectionClass *conn,
 			/* "0" returned in SQLGetStmtOption */
 			break;
 
-		case SQL_RETRIEVE_DATA:	/* ignored, but saved */
+		case SQL_RETRIEVE_DATA:
 			mylog("SetStmtOption(): SQL_RETRIEVE_DATA, vParam = %d\n", vParam);
 			if (conn)
 				conn->stmtOptions.retrieve_data = vParam;
@@ -636,7 +636,7 @@ PGAPI_GetStmtOption(
 			*((SDWORD *) pvParam) = 0;
 			break;
 
-		case SQL_RETRIEVE_DATA:	/* NOT SUPPORTED, but saved */
+		case SQL_RETRIEVE_DATA:
 			*((SDWORD *) pvParam) = stmt->options.retrieve_data;
 			break;
 
diff --git a/src/interfaces/odbc/parse.c b/src/interfaces/odbc/parse.c
index 8a90cce35a5..27d1e95af2d 100644
--- a/src/interfaces/odbc/parse.c
+++ b/src/interfaces/odbc/parse.c
@@ -378,18 +378,19 @@ parse_statement(StatementClass *stmt)
 		{
 			/* just eat the expression */
 			mylog("in_expr=%d or func=%d\n", in_expr, in_func);
-			if (!unquoted)
-				continue;
 
-			if (token[0] == '(')
-			{
-				blevel++;
-				mylog("blevel++ = %d\n", blevel);
-			}
-			else if (token[0] == ')')
+			if (unquoted)
 			{
-				blevel--;
-				mylog("blevel-- = %d\n", blevel);
+				if (token[0] == '(')
+				{
+					blevel++;
+					mylog("blevel++ = %d\n", blevel);
+				}
+				else if (token[0] == ')')
+				{
+					blevel--;
+					mylog("blevel-- = %d\n", blevel);
+				}
 			}
 			if (blevel == 0)
 			{
@@ -400,7 +401,7 @@ parse_statement(StatementClass *stmt)
 					in_expr = FALSE;
 					in_field = FALSE;
 				}
-				else if (!stricmp(token, "as"))
+				else if (unquoted && !stricmp(token, "as"))
 				{
 					mylog("got AS in_expr\n");
 					in_func = FALSE;
@@ -474,9 +475,8 @@ parse_statement(StatementClass *stmt)
 
 				if (quote)
 				{
-					fi[stmt->nfld++]->quote = TRUE;
-					in_expr = TRUE;
-					continue;
+					fi[stmt->nfld]->quote = TRUE;
+					fi[stmt->nfld]->precision = strlen(token);
 				}
 				else if (numeric)
 				{
@@ -573,6 +573,7 @@ parse_statement(StatementClass *stmt)
 			in_expr = TRUE;
 			fi[stmt->nfld - 1]->expr = TRUE;
 			fi[stmt->nfld - 1]->name[0] = '\0';
+			fi[stmt->nfld - 1]->precision = 0;
 			mylog("*** setting expression\n");
 		}
 
@@ -661,7 +662,12 @@ parse_statement(StatementClass *stmt)
 			 * following may be better
 			 */
 			fi[i]->type = PG_TYPE_UNKNOWN;
-			fi[i]->precision = 254;
+			if (fi[i]->precision == 0)
+			{
+				fi[i]->type = PG_TYPE_VARCHAR;
+				fi[i]->precision = 254;
+			}
+			fi[i]->length = fi[i]->precision;
 			continue;
 		}
 		/* it's a dot, resolve to table or alias */
diff --git a/src/interfaces/odbc/psqlodbc.rc b/src/interfaces/odbc/psqlodbc.rc
index 499734f16b7..a6c9b23bc92 100644
--- a/src/interfaces/odbc/psqlodbc.rc
+++ b/src/interfaces/odbc/psqlodbc.rc
@@ -104,7 +104,7 @@ BEGIN
                     BS_AUTOCHECKBOX | WS_TABSTOP,13,47,84,10
     CONTROL         "Cancel as FreeStmt (Exp)",DRV_CANCELASFREESTMT,"Button",
                     BS_AUTOCHECKBOX | WS_TABSTOP,164,50,112,10
-    CONTROL         "Mylog(Debug ouput",DRV_DEBUG,"Button",
+    CONTROL         "Mylog(Debug ouput)",DRV_DEBUG,"Button",
                     BS_AUTOCHECKBOX | WS_TABSTOP,164,63,112,10
     GROUPBOX        "Unknown Sizes",IDC_STATIC,13,76,175,24
     CONTROL         "Maximum",DRV_UNKNOWN_MAX,"Button",BS_AUTORADIOBUTTON | 
@@ -150,7 +150,7 @@ BEGIN
     CONTROL         "Show System &Tables",DS_SHOWSYSTEMTABLES,"Button",
                     BS_AUTOCHECKBOX | WS_TABSTOP,45,28,88,10
     CONTROL         "Disallow &Premature",DS_DISALLOWPREMATURE,"Button",
-                    BS_AUTOCHECKBOX | WS_TABSTOP,149,28,72,10
+                    BS_AUTOCHECKBOX | WS_TABSTOP,149,28,86,10
     GROUPBOX        "Protocol",IDC_STATIC,43,44,180,25
     CONTROL         "7.X,6.4+",DS_PG64,"Button",BS_AUTORADIOBUTTON | 
                     WS_GROUP,53,54,47,10
-- 
GitLab