From a50f285d8de0eeb24df5a54f077e7aa2fee8eeee Mon Sep 17 00:00:00 2001
From: Michael Meskes <meskes@postgresql.org>
Date: Tue, 8 Apr 2003 12:34:25 +0000
Subject: [PATCH] Added some more informix compatibility functions.

---
 src/interfaces/ecpg/ChangeLog             |  6 ++++++
 src/interfaces/ecpg/compatlib/informix.c  |  5 ++++-
 src/interfaces/ecpg/ecpglib/connect.c     | 19 +++++++++++++++++--
 src/interfaces/ecpg/include/ecpglib.h     |  2 ++
 src/interfaces/ecpg/pgtypeslib/datetime.c |  5 +++--
 src/interfaces/ecpg/preproc/ecpg.c        |  4 ++--
 6 files changed, 34 insertions(+), 7 deletions(-)

diff --git a/src/interfaces/ecpg/ChangeLog b/src/interfaces/ecpg/ChangeLog
index 321bd3a63c6..4c5e84b6944 100644
--- a/src/interfaces/ecpg/ChangeLog
+++ b/src/interfaces/ecpg/ChangeLog
@@ -1385,6 +1385,12 @@ Sat Mar 29 22:03:16 CET 2003
 Sun Mar 30 13:43:13 CEST 2003
 
 	- Interval datetype now fully functional.
+
+Tue Apr  8 14:03:32 CEST 2003
+
+	- Added rstrdate function.
+	- Made Informix mode honor environment variable to set dbname to
+	  connect to.
 	- Set ecpg version to 2.12.0.
 	- Set ecpg library to 3.4.2.
 	- Set pgtypes library to 1.0.0
diff --git a/src/interfaces/ecpg/compatlib/informix.c b/src/interfaces/ecpg/compatlib/informix.c
index e32c09077c2..fe12da7b0d5 100644
--- a/src/interfaces/ecpg/compatlib/informix.c
+++ b/src/interfaces/ecpg/compatlib/informix.c
@@ -223,7 +223,10 @@ rstrdate (char *str, Date *d)
 {
 	Date dat = PGTYPESdate_from_asc(str, NULL);
 
-	/* XXX: ERROR handling hier und in datetime.c */
+	if (errno != PGTYPES_DATE_BAD_DATE && dat == 0)
+		return -1218;
+
+	*d=dat;
 	return 0;
 }
 
diff --git a/src/interfaces/ecpg/ecpglib/connect.c b/src/interfaces/ecpg/ecpglib/connect.c
index 69433bdc4b1..ee5ed471185 100644
--- a/src/interfaces/ecpg/ecpglib/connect.c
+++ b/src/interfaces/ecpg/ecpglib/connect.c
@@ -1,4 +1,4 @@
-/* $Header: /cvsroot/pgsql/src/interfaces/ecpg/ecpglib/connect.c,v 1.2 2003/04/04 20:42:13 momjian Exp $ */
+/* $Header: /cvsroot/pgsql/src/interfaces/ecpg/ecpglib/connect.c,v 1.3 2003/04/08 12:34:25 meskes Exp $ */
 
 #include "postgres_fe.h"
 
@@ -11,6 +11,8 @@
 static struct connection *all_connections = NULL,
 		   *actual_connection = NULL;
 
+extern enum COMPAT_MODE ecpg_compat_mode;
+
 struct connection *
 ECPGget_connection(const char *connection_name)
 {
@@ -267,13 +269,26 @@ ECPGconnect(int lineno, const char *name, const char *user, const char *passwd,
 			   *tmp,
 			   *port = NULL,
 			   *realname = NULL,
-			   *options = NULL;
+			   *options = NULL,
+			   *envname;
 
 	ECPGinit_sqlca();
 
 	if ((this = (struct connection *) ECPGalloc(sizeof(struct connection), lineno)) == NULL)
 		return false;
 
+	if (ecpg_compat_mode == ECPG_COMPAT_INFORMIX)
+	{
+		/* Informix uses an environment variable DBPATH that overrides
+		 * the connection parameters given here */
+		envname = getenv("DBPATH");
+		if (envname)
+		{
+			free(dbname);
+			dbname=strdup(envname);
+		}
+	}
+	
 	if (dbname == NULL && connection_name == NULL)
 		connection_name = "DEFAULT";
 
diff --git a/src/interfaces/ecpg/include/ecpglib.h b/src/interfaces/ecpg/include/ecpglib.h
index e5c3f193151..b4700deaff7 100644
--- a/src/interfaces/ecpg/include/ecpglib.h
+++ b/src/interfaces/ecpg/include/ecpglib.h
@@ -74,6 +74,8 @@ bool		ECPGget_desc(int, char *, int,...);
 /* dynamic result allocation */
 void		ECPGfree_auto_mem(void);
 
+enum COMPAT_MODE { ECPG_COMPAT_PGSQL = 0, ECPG_COMPAT_INFORMIX};
+
 #ifdef __cplusplus
 }
 
diff --git a/src/interfaces/ecpg/pgtypeslib/datetime.c b/src/interfaces/ecpg/pgtypeslib/datetime.c
index 1a9bed268c0..1249a606fdb 100644
--- a/src/interfaces/ecpg/pgtypeslib/datetime.c
+++ b/src/interfaces/ecpg/pgtypeslib/datetime.c
@@ -51,17 +51,18 @@ PGTYPESdate_from_asc(char *str, char **endptr)
 	
 	bool		EuroDates = FALSE;
 
+	errno = 0;
 	if (strlen(str) >= sizeof(lowstr))
 	{
 		errno = PGTYPES_DATE_BAD_DATE;
-		return -1;
+		return 0;
 	}
 
 	if ((ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf, ptr) != 0)
 	 || (DecodeDateTime(field, ftype, nf, &dtype, tm, &fsec, &tzp, EuroDates) != 0))
 	{
 		errno = PGTYPES_DATE_BAD_DATE;
-		return -1;
+		return 0;
 	}
 
 	switch (dtype)
diff --git a/src/interfaces/ecpg/preproc/ecpg.c b/src/interfaces/ecpg/preproc/ecpg.c
index fa35b30e7f5..f1c39edba66 100644
--- a/src/interfaces/ecpg/preproc/ecpg.c
+++ b/src/interfaces/ecpg/preproc/ecpg.c
@@ -1,4 +1,4 @@
-/* $Header: /cvsroot/pgsql/src/interfaces/ecpg/preproc/ecpg.c,v 1.65 2003/04/04 20:42:13 momjian Exp $ */
+/* $Header: /cvsroot/pgsql/src/interfaces/ecpg/preproc/ecpg.c,v 1.66 2003/04/08 12:34:25 meskes Exp $ */
 
 /* New main for ecpg, the PostgreSQL embedded SQL precompiler. */
 /* (C) Michael Meskes <meskes@postgresql.org> Feb 5th, 1998 */
@@ -332,7 +332,7 @@ main(int argc, char *const argv[])
 				lex_init();
 
 				/* we need several includes */
-				fprintf(yyout, "/* Processed by ecpg (%d.%d.%d) */\n/* These four include files are added by the preprocessor */\n#include <ecpgtype.h>\n#include <ecpglib.h>\n#include <ecpgerrno.h>\n#include <sqlca.h>\n#line 1 \"%s\"\n", MAJOR_VERSION, MINOR_VERSION, PATCHLEVEL, input_filename);
+				fprintf(yyout, "/* Processed by ecpg (%d.%d.%d) */\n/* These four include files are added by the preprocessor */\n#include <ecpgtype.h>\n#include <ecpglib.h>\n#include <ecpgerrno.h>\n#include <sqlca.h>\n#line 1 \"%s\"\nenum COMPAT_MODE ecpg_compat_mode=%d;\n", MAJOR_VERSION, MINOR_VERSION, PATCHLEVEL, input_filename, compat);
 
 				/* add some compatibility headers */
 				if (compat == ECPG_COMPAT_INFORMIX)
-- 
GitLab