commit e12bfda36fe79d371c2a30810fe664237d24fce1
Author: Böszörményi Zoltán <zboszormenyi@sicom.com>
Date:   Sat Jan 18 11:45:49 2014 +0100

    ECPG: Modify the parameters of ECPGsetcommit() so it takes a bool
    instead of a string with the "on" / "off" values. This way the
    parser checks for the string value, not the runtime, making it
    less error-prone against hand-crafted C files.
    
    Also add a check to not execute BEGIN if the transaction has
    already failed. Extend the same check in ecpg_do_prologue() to
    match these two checks.

diff --git a/src/interfaces/ecpg/ecpglib/connect.c b/src/interfaces/ecpg/ecpglib/connect.c
index 0c4af66..a52d616 100644
--- a/src/interfaces/ecpg/ecpglib/connect.c
+++ b/src/interfaces/ecpg/ecpglib/connect.c
@@ -162,7 +162,7 @@ ecpg_finish(struct connection * act)
 }
 
 bool
-ECPGsetcommit(int lineno, const char *mode, const char *connection_name)
+ECPGsetcommit(int lineno, const bool turn_on, const char *connection_name)
 {
 	struct connection *con = ecpg_get_connection(connection_name);
 	PGresult   *results;
@@ -170,10 +170,15 @@ ECPGsetcommit(int lineno, const char *mode, const char *connection_name)
 	if (!ecpg_init(con, connection_name, lineno))
 		return (false);
 
-	ecpg_log("ECPGsetcommit on line %d: action \"%s\"; connection \"%s\"\n", lineno, mode, con->name);
+	ecpg_log("ECPGsetcommit on line %d: action \"%s\"; connection \"%s\"\n", lineno, turn_on ? "on" : "off", con->name);
 
-	if (con->autocommit && strncmp(mode, "off", strlen("off")) == 0)
+	if (con->autocommit && !turn_on)
 	{
+		if (con->client_side_error || PQtransactionStatus(con->connection) == PQTRANS_INERROR)
+		{
+			ecpg_raise(lineno, ECPG_TRANS, ECPG_SQLSTATE_IN_FAILED_SQL_TRANSACTION, NULL);
+			return false;
+		}
 		if (PQtransactionStatus(con->connection) == PQTRANS_IDLE)
 		{
 			results = PQexec(con->connection, "begin transaction");
@@ -183,7 +188,7 @@ ECPGsetcommit(int lineno, const char *mode, const char *connection_name)
 		}
 		con->autocommit = false;
 	}
-	else if (!con->autocommit && strncmp(mode, "on", strlen("on")) == 0)
+	else if (!con->autocommit && turn_on)
 	{
 		if (PQtransactionStatus(con->connection) != PQTRANS_IDLE)
 		{
diff --git a/src/interfaces/ecpg/ecpglib/execute.c b/src/interfaces/ecpg/ecpglib/execute.c
index f5ca9e3..e019dd2 100644
--- a/src/interfaces/ecpg/ecpglib/execute.c
+++ b/src/interfaces/ecpg/ecpglib/execute.c
@@ -1879,7 +1879,7 @@ ecpg_do_prologue(int lineno, const int compat, const int force_indicator,
 		return false;
 	}
 
-	if (con->client_side_error)
+	if (con->client_side_error || PQtransactionStatus(con->connection) == PQTRANS_INERROR)
 	{
 		ecpg_do_epilogue(stmt);
 		ecpg_raise(lineno, ECPG_TRANS, ECPG_SQLSTATE_IN_FAILED_SQL_TRANSACTION, NULL);
diff --git a/src/interfaces/ecpg/include/ecpglib.h b/src/interfaces/ecpg/include/ecpglib.h
index 02f319d..1ac15d5 100644
--- a/src/interfaces/ecpg/include/ecpglib.h
+++ b/src/interfaces/ecpg/include/ecpglib.h
@@ -48,7 +48,7 @@ extern		"C"
 
 void		ECPGdebug(int, FILE *);
 bool		ECPGstatus(int, const char *);
-bool		ECPGsetcommit(int, const char *, const char *);
+bool		ECPGsetcommit(int, const bool, const char *);
 bool		ECPGsetconn(int, const char *);
 bool		ECPGconnect(int, int, const char *, const char *, const char *, const char *, int);
 bool		ECPGdo(const int, const int, const int, const char *, const bool, const int, const char *,...);
diff --git a/src/interfaces/ecpg/preproc/ecpg.addons b/src/interfaces/ecpg/preproc/ecpg.addons
index ebc9b16..df85424 100644
--- a/src/interfaces/ecpg/preproc/ecpg.addons
+++ b/src/interfaces/ecpg/preproc/ecpg.addons
@@ -222,7 +222,7 @@ ECPG: stmtViewStmt rule
 	}
 	| ECPGSetAutocommit
 	{
-		fprintf(yyout, "{ ECPGsetcommit(__LINE__, \"%s\", %s);", $1, connection ? connection : "NULL");
+		fprintf(yyout, "{ ECPGsetcommit(__LINE__, %d, %s);", (strcmp($1, "on") == 0), connection ? connection : "NULL");
 		whenever_action(2);
 		free($1);
 	}
diff --git a/src/interfaces/ecpg/test/expected/pgtypeslib-num_test.c b/src/interfaces/ecpg/test/expected/pgtypeslib-num_test.c
index c139e41..5abbf4f 100644
--- a/src/interfaces/ecpg/test/expected/pgtypeslib-num_test.c
+++ b/src/interfaces/ecpg/test/expected/pgtypeslib-num_test.c
@@ -61,7 +61,7 @@ if (sqlca.sqlcode < 0) sqlprint ( );}
 #line 32 "num_test.pgc"
 
 
-	{ ECPGsetcommit(__LINE__, "off", NULL);
+	{ ECPGsetcommit(__LINE__, 0, NULL);
 #line 34 "num_test.pgc"
 
 if (sqlca.sqlcode < 0) sqlprint ( );}
diff --git a/src/interfaces/ecpg/test/expected/sql-array.c b/src/interfaces/ecpg/test/expected/sql-array.c
index f770c09..befa89f 100644
--- a/src/interfaces/ecpg/test/expected/sql-array.c
+++ b/src/interfaces/ecpg/test/expected/sql-array.c
@@ -141,7 +141,7 @@ if (sqlca.sqlcode < 0) sqlprint();}
 #line 27 "array.pgc"
 
 
-	{ ECPGsetcommit(__LINE__, "on", NULL);
+	{ ECPGsetcommit(__LINE__, 1, NULL);
 #line 29 "array.pgc"
 
 if (sqlca.sqlcode < 0) sqlprint();}
diff --git a/src/interfaces/ecpg/test/expected/sql-func.c b/src/interfaces/ecpg/test/expected/sql-func.c
index 5d524b8..823c5b4 100644
--- a/src/interfaces/ecpg/test/expected/sql-func.c
+++ b/src/interfaces/ecpg/test/expected/sql-func.c
@@ -35,7 +35,7 @@ int main() {
 #line 11 "func.pgc"
 
 
-  { ECPGsetcommit(__LINE__, "on", NULL);}
+  { ECPGsetcommit(__LINE__, 1, NULL);}
 #line 13 "func.pgc"
 
   /* exec sql whenever sql_warning  sqlprint ; */
diff --git a/src/interfaces/ecpg/test/expected/sql-indicators.c b/src/interfaces/ecpg/test/expected/sql-indicators.c
index 22aacc1..168ccd4 100644
--- a/src/interfaces/ecpg/test/expected/sql-indicators.c
+++ b/src/interfaces/ecpg/test/expected/sql-indicators.c
@@ -111,7 +111,7 @@ int main()
 	{ ECPGconnect(__LINE__, 0, "regress1" , NULL, NULL , NULL, 0); }
 #line 15 "indicators.pgc"
 
-	{ ECPGsetcommit(__LINE__, "off", NULL);}
+	{ ECPGsetcommit(__LINE__, 0, NULL);}
 #line 16 "indicators.pgc"
 
 
diff --git a/src/interfaces/ecpg/test/expected/sql-parser.c b/src/interfaces/ecpg/test/expected/sql-parser.c
index 616135d..1d6c0d3 100644
--- a/src/interfaces/ecpg/test/expected/sql-parser.c
+++ b/src/interfaces/ecpg/test/expected/sql-parser.c
@@ -38,7 +38,7 @@ int main() {
 #line 14 "parser.pgc"
 
 
-  { ECPGsetcommit(__LINE__, "on", NULL);}
+  { ECPGsetcommit(__LINE__, 1, NULL);}
 #line 16 "parser.pgc"
 
   /* exec sql whenever sql_warning  sqlprint ; */
diff --git a/src/interfaces/ecpg/test/expected/sql-quote.c b/src/interfaces/ecpg/test/expected/sql-quote.c
index 6c4d84f..bfb75ca 100644
--- a/src/interfaces/ecpg/test/expected/sql-quote.c
+++ b/src/interfaces/ecpg/test/expected/sql-quote.c
@@ -41,7 +41,7 @@ int main() {
 #line 14 "quote.pgc"
 
 
-  { ECPGsetcommit(__LINE__, "on", NULL);}
+  { ECPGsetcommit(__LINE__, 1, NULL);}
 #line 16 "quote.pgc"
 
   /* exec sql whenever sql_warning  sqlprint ; */
diff --git a/src/interfaces/ecpg/test/expected/thread-alloc.c b/src/interfaces/ecpg/test/expected/thread-alloc.c
index 199a77e..c74ea26 100644
--- a/src/interfaces/ecpg/test/expected/thread-alloc.c
+++ b/src/interfaces/ecpg/test/expected/thread-alloc.c
@@ -153,7 +153,7 @@ static void* fn(void* arg)
 if (sqlca.sqlcode < 0) sqlprint();}
 #line 47 "alloc.pgc"
 
-	{ ECPGsetcommit(__LINE__, "on", NULL);
+	{ ECPGsetcommit(__LINE__, 1, NULL);
 #line 48 "alloc.pgc"
 
 if (sqlca.sqlcode < 0) sqlprint();}
diff --git a/src/interfaces/ecpg/test/expected/thread-prep.c b/src/interfaces/ecpg/test/expected/thread-prep.c
index cd28c85..3a559ce 100644
--- a/src/interfaces/ecpg/test/expected/thread-prep.c
+++ b/src/interfaces/ecpg/test/expected/thread-prep.c
@@ -153,7 +153,7 @@ static void* fn(void* arg)
 if (sqlca.sqlcode < 0) sqlprint();}
 #line 47 "prep.pgc"
 
-	{ ECPGsetcommit(__LINE__, "on", NULL);
+	{ ECPGsetcommit(__LINE__, 1, NULL);
 #line 48 "prep.pgc"
 
 if (sqlca.sqlcode < 0) sqlprint();}
@@ -210,7 +210,7 @@ int main ()
 if (sqlca.sqlcode < 0) sqlprint();}
 #line 69 "prep.pgc"
 
-	{ ECPGsetcommit(__LINE__, "on", NULL);
+	{ ECPGsetcommit(__LINE__, 1, NULL);
 #line 70 "prep.pgc"
 
 if (sqlca.sqlcode < 0) sqlprint();}
