From 684cb8d69f3e3974b0993ef589d0e92fdf5da2a8 Mon Sep 17 00:00:00 2001
From: Georgy Kirichenko <georgy@tarantool.org>
Date: Thu, 12 Jul 2018 18:04:45 +0300
Subject: [PATCH] sql: split conflict action and affinity for Expr

Lets introduce separate field in struct Expr to store conflict action of
RAISE() function, instead of messing it with affinity.
---
 src/box/sql/expr.c      | 46 ++++++++++++++++-------------------------
 src/box/sql/fkey.c      |  2 +-
 src/box/sql/parse.y     |  4 ++--
 src/box/sql/sqliteInt.h |  7 ++++++-
 src/box/sql/treeview.c  |  6 ++++--
 5 files changed, 31 insertions(+), 34 deletions(-)

diff --git a/src/box/sql/expr.c b/src/box/sql/expr.c
index 3602606aa6..af5f3e5608 100644
--- a/src/box/sql/expr.c
+++ b/src/box/sql/expr.c
@@ -4267,35 +4267,25 @@ sqlite3ExprCodeTarget(Parse * pParse, Expr * pExpr, int target)
 			sqlite3VdbeResolveLabel(v, endLabel);
 			break;
 		}
-	case TK_RAISE:{
-			assert(pExpr->affinity == ON_CONFLICT_ACTION_ROLLBACK
-			       || pExpr->affinity == ON_CONFLICT_ACTION_ABORT
-			       || pExpr->affinity == ON_CONFLICT_ACTION_FAIL
-			       || pExpr->affinity == ON_CONFLICT_ACTION_IGNORE);
-			if (!pParse->pTriggerTab) {
-				sqlite3ErrorMsg(pParse,
-						"RAISE() may only be used within a trigger-program");
-				return 0;
-			}
-			if (pExpr->affinity == ON_CONFLICT_ACTION_ABORT) {
-				sqlite3MayAbort(pParse);
-			}
-			assert(!ExprHasProperty(pExpr, EP_IntValue));
-			if (pExpr->affinity == ON_CONFLICT_ACTION_IGNORE) {
-				sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_OK,
-						  ON_CONFLICT_ACTION_IGNORE, 0,
-						  pExpr->u.zToken,
-						  0);
-				VdbeCoverage(v);
-			} else {
-				sqlite3HaltConstraint(pParse,
-						      SQLITE_CONSTRAINT_TRIGGER,
-						      pExpr->affinity,
-						      pExpr->u.zToken, 0, 0);
-			}
-
-			break;
+	case TK_RAISE:
+		if (pParse->pTriggerTab == NULL) {
+			sqlite3ErrorMsg(pParse, "RAISE() may only be used "
+					"within a trigger-program");
+			return 0;
 		}
+		if (pExpr->on_conflict_action == ON_CONFLICT_ACTION_ABORT)
+			sqlite3MayAbort(pParse);
+		assert(!ExprHasProperty(pExpr, EP_IntValue));
+		if (pExpr->on_conflict_action == ON_CONFLICT_ACTION_IGNORE) {
+			sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_OK,
+					  ON_CONFLICT_ACTION_IGNORE, 0,
+					  pExpr->u.zToken, 0);
+		} else {
+			sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER,
+					      pExpr->on_conflict_action,
+					      pExpr->u.zToken, 0, 0);
+		}
+		break;
 	}
 	sqlite3ReleaseTempReg(pParse, regFree1);
 	sqlite3ReleaseTempReg(pParse, regFree2);
diff --git a/src/box/sql/fkey.c b/src/box/sql/fkey.c
index 56885e448f..fc39c879e4 100644
--- a/src/box/sql/fkey.c
+++ b/src/box/sql/fkey.c
@@ -858,7 +858,7 @@ fkey_action_trigger(struct Parse *pParse, struct Table *pTab, struct fkey *fkey,
 		struct Expr *r = sqlite3Expr(db, TK_RAISE, "FOREIGN KEY "\
 					     "constraint failed");
 		if (r != NULL)
-			r->affinity = ON_CONFLICT_ACTION_ABORT;
+			r->on_conflict_action = ON_CONFLICT_ACTION_ABORT;
 		select = sqlite3SelectNew(pParse,
 					  sql_expr_list_append(db, NULL, r),
 					  sqlite3SrcListAppend(db, NULL, &err),
diff --git a/src/box/sql/parse.y b/src/box/sql/parse.y
index e8027ab800..90d22aca65 100644
--- a/src/box/sql/parse.y
+++ b/src/box/sql/parse.y
@@ -1405,14 +1405,14 @@ expr(A) ::= RAISE(X) LP IGNORE RP(Y).  {
   spanSet(&A,&X,&Y);  /*A-overwrites-X*/
   A.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0); 
   if( A.pExpr ){
-    A.pExpr->affinity = ON_CONFLICT_ACTION_IGNORE;
+    A.pExpr->on_conflict_action = ON_CONFLICT_ACTION_IGNORE;
   }
 }
 expr(A) ::= RAISE(X) LP raisetype(T) COMMA STRING(Z) RP(Y).  {
   spanSet(&A,&X,&Y);  /*A-overwrites-X*/
   A.pExpr = sqlite3ExprAlloc(pParse->db, TK_RAISE, &Z, 1); 
   if( A.pExpr ) {
-    A.pExpr->affinity = (char)T;
+    A.pExpr->on_conflict_action = (enum on_conflict_action) T;
   }
 }
 
diff --git a/src/box/sql/sqliteInt.h b/src/box/sql/sqliteInt.h
index d8eb516451..816e81f675 100644
--- a/src/box/sql/sqliteInt.h
+++ b/src/box/sql/sqliteInt.h
@@ -2070,7 +2070,12 @@ typedef int ynVar;
  */
 struct Expr {
 	u8 op;			/* Operation performed by this node */
-	char affinity;		/* The affinity of the column or 0 if not a column */
+	union {
+		/** The affinity of the column. */
+		enum affinity_type affinity;
+		/** Conflict action for RAISE() function. */
+		enum on_conflict_action on_conflict_action;
+	};
 	u32 flags;		/* Various flags.  EP_* See below */
 	union {
 		char *zToken;	/* Token value. Zero terminated and dequoted */
diff --git a/src/box/sql/treeview.c b/src/box/sql/treeview.c
index 4261e733ef..f6a1755f45 100644
--- a/src/box/sql/treeview.c
+++ b/src/box/sql/treeview.c
@@ -566,8 +566,8 @@ sqlite3TreeViewExpr(TreeView * pView, const Expr * pExpr, u8 moreToFollow)
 			break;
 		}
 	case TK_RAISE:{
-			const char *zType = "unk";
-			switch (pExpr->affinity) {
+			const char *zType;
+			switch (pExpr->on_conflict_action) {
 			case ON_CONFLICT_ACTION_ROLLBACK:
 				zType = "rollback";
 				break;
@@ -580,6 +580,8 @@ sqlite3TreeViewExpr(TreeView * pView, const Expr * pExpr, u8 moreToFollow)
 			case ON_CONFLICT_ACTION_IGNORE:
 				zType = "ignore";
 				break;
+			default:
+				unreachable();
 			}
 			sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType,
 					    pExpr->u.zToken);
-- 
GitLab