[tarantool-patches] [PATCH v1 14/28] sql: remove SQL_RANGE errcode

imeevma at tarantool.org imeevma at tarantool.org
Mon Jun 10 16:56:48 MSK 2019


Removing this error code is part of getting rid of the SQL error
system.
---
 src/box/bind.c         |  37 ++++-----------
 src/box/sql/sqlInt.h   |   2 -
 src/box/sql/vdbeapi.c  | 122 +++++++++++++++++++++----------------------------
 test/sql/bind.result   |   8 ++++
 test/sql/bind.test.lua |   3 ++
 5 files changed, 72 insertions(+), 100 deletions(-)

diff --git a/src/box/bind.c b/src/box/bind.c
index 90d56d6..4888265 100644
--- a/src/box/bind.c
+++ b/src/box/bind.c
@@ -162,7 +162,6 @@ int
 sql_bind_column(struct sql_stmt *stmt, const struct sql_bind *p,
 		uint32_t pos)
 {
-	int rc;
 	if (p->name != NULL) {
 		pos = sql_bind_parameter_lindex(stmt, p->name, p->name_len);
 		if (pos == 0) {
@@ -174,15 +173,12 @@ sql_bind_column(struct sql_stmt *stmt, const struct sql_bind *p,
 	switch (p->type) {
 	case MP_INT:
 	case MP_UINT:
-		rc = sql_bind_int64(stmt, pos, p->i64);
-		break;
+		return sql_bind_int64(stmt, pos, p->i64);
 	case MP_BOOL:
-		rc = sql_bind_boolean(stmt, pos, p->b);
-		break;
+		return sql_bind_boolean(stmt, pos, p->b);
 	case MP_DOUBLE:
 	case MP_FLOAT:
-		rc = sql_bind_double(stmt, pos, p->d);
-		break;
+		return sql_bind_double(stmt, pos, p->d);
 	case MP_STR:
 		/*
 		 * Parameters are allocated within message pack,
@@ -192,31 +188,14 @@ sql_bind_column(struct sql_stmt *stmt, const struct sql_bind *p,
 		 * there is no need to copy the packet and we can
 		 * use SQL_STATIC.
 		 */
-		rc = sql_bind_text64(stmt, pos, p->s, p->bytes,
-					 SQL_STATIC);
-		break;
+		return sql_bind_text64(stmt, pos, p->s, p->bytes, SQL_STATIC);
 	case MP_NIL:
-		rc = sql_bind_null(stmt, pos);
-		break;
+		return sql_bind_null(stmt, pos);
 	case MP_BIN:
-		rc = sql_bind_blob64(stmt, pos, (const void *) p->s,
-					 p->bytes, SQL_STATIC);
-		break;
+		return sql_bind_blob64(stmt, pos, (const void *) p->s, p->bytes,
+				       SQL_STATIC);
 	default:
 		unreachable();
 	}
-	if (rc == 0)
-		return 0;
-
-	switch (rc) {
-	case SQL_NOMEM:
-		diag_set(OutOfMemory, p->bytes, "vdbe", "bind value");
-		break;
-	case SQL_TOOBIG:
-	default:
-		diag_set(ClientError, ER_SQL_BIND_VALUE, sql_bind_name(p),
-			 mp_type_strs[p->type]);
-		break;
-	}
-	return -1;
+	return 0;
 }
diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h
index 8b63c40..ceaca52 100644
--- a/src/box/sql/sqlInt.h
+++ b/src/box/sql/sqlInt.h
@@ -370,8 +370,6 @@ enum sql_ret_code {
 	SQL_TOOBIG,
 	/** Abort due to constraint violation. */
 	SQL_CONSTRAINT,
-	/** 2nd parameter to sql_bind out of range. */
-	SQL_RANGE,
 	SQL_TARANTOOL_ERROR,
 	/** sql_step() has another row ready. */
 	SQL_ROW,
diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c
index 7ac3211..a850d7f 100644
--- a/src/box/sql/vdbeapi.c
+++ b/src/box/sql/vdbeapi.c
@@ -955,11 +955,7 @@ sql_column_origin_name(sql_stmt * pStmt, int N)
  */
 /*
  * Unbind the value bound to variable i in virtual machine p. This is the
- * the same as binding a NULL value to the column. If the "i" parameter is
- * out of range, then SQL_RANGE is returned. Othewise 0.
- *
- * The error code stored in database p->db is overwritten with the return
- * value in any case.
+ * the same as binding a NULL value to the column.
  */
 static int
 vdbeUnbind(Vdbe * p, int i)
@@ -967,8 +963,11 @@ vdbeUnbind(Vdbe * p, int i)
 	Mem *pVar;
 	assert(p != NULL);
 	assert(p->magic == VDBE_MAGIC_RUN && p->pc < 0);
-	if (i < 1 || i > p->nVar) {
-		return SQL_RANGE;
+	assert(i > 0);
+	if(i > p->nVar) {
+		diag_set(ClientError, ER_SQL_EXECUTE, "The number of "\
+			 "parameters is too large");
+		return -1;
 	}
 	i--;
 	pVar = &p->aVar[i];
@@ -1019,19 +1018,19 @@ sql_bind_type(struct Vdbe *v, uint32_t position, const char *type)
 {
 	if (v->res_var_count < position)
 		return 0;
-	int rc = sqlVdbeSetColName(v, v->var_pos[position - 1],
-				       COLNAME_DECLTYPE, type,
-				       SQL_TRANSIENT);
+	int rc = 0;
+	if (sqlVdbeSetColName(v, v->var_pos[position - 1], COLNAME_DECLTYPE,
+			      type, SQL_TRANSIENT) != 0)
+		rc = -1;
 	const char *bind_name = v->aColName[position - 1].z;
 	if (strcmp(bind_name, "?") == 0)
 		return rc;
 	for (uint32_t i = position; i < v->res_var_count; ++i) {
 		if (strcmp(bind_name,  v->aColName[i].z) == 0) {
-			rc = sqlVdbeSetColName(v, v->var_pos[i],
-						   COLNAME_DECLTYPE, type,
-						   SQL_TRANSIENT);
-			if (rc != 0)
-				return rc;
+			if (sqlVdbeSetColName(v, v->var_pos[i],
+					      COLNAME_DECLTYPE, type,
+					      SQL_TRANSIENT) != 0)
+				return -1;
 		}
 	}
 	return 0;
@@ -1050,21 +1049,17 @@ bindText(sql_stmt * pStmt,	/* The statement to bind against */
 {
 	Vdbe *p = (Vdbe *) pStmt;
 	Mem *pVar;
-	int rc;
-
-	rc = vdbeUnbind(p, i);
-	if (rc == 0) {
-		if (zData != 0) {
-			pVar = &p->aVar[i - 1];
-			rc = sqlVdbeMemSetStr(pVar, zData, nData, 1, xDel);
-			if (rc == 0)
-				rc = sql_bind_type(p, i, "TEXT");
-			rc = sqlApiExit(p->db, rc);
-		}
-	} else if (xDel != SQL_STATIC && xDel != SQL_TRANSIENT) {
-		xDel((void *)zData);
+	if (vdbeUnbind(p, i) != 0) {
+		if (xDel != SQL_STATIC && xDel != SQL_TRANSIENT)
+			xDel((void *)zData);
+		return -1;
 	}
-	return rc;
+	if (zData == NULL)
+		return 0;
+	pVar = &p->aVar[i - 1];
+	if (sqlVdbeMemSetStr(pVar, zData, nData, 1, xDel) != 0)
+		return -1;
+	return sql_bind_type(p, i, "TEXT");
 }
 
 /*
@@ -1076,19 +1071,17 @@ sql_bind_blob(sql_stmt * pStmt,
     )
 {
 	struct Vdbe *p = (Vdbe *) pStmt;
-	int rc = vdbeUnbind(p, i);
-	if (rc == 0) {
-		if (zData != 0) {
-			struct Mem *var = &p->aVar[i - 1];
-			rc = sqlVdbeMemSetStr(var, zData, nData, 0, xDel);
-			if (rc == 0)
-				rc = sql_bind_type(p, i, "BLOB");
-			rc = sqlApiExit(p->db, rc);
-		}
-	} else if (xDel != SQL_STATIC && xDel != SQL_TRANSIENT) {
-		xDel((void *)zData);
+	if (vdbeUnbind(p, i) != 0) {
+		if (xDel != SQL_STATIC && xDel != SQL_TRANSIENT)
+			xDel((void *)zData);
+		return -1;
 	}
-	return rc;
+	if (zData == NULL)
+		return 0;
+	struct Mem *var = &p->aVar[i - 1];
+	if (sqlVdbeMemSetStr(var, zData, nData, 0, xDel) != 0)
+		return -1;
+	return sql_bind_type(p, i, "BLOB");
 }
 
 int
@@ -1109,13 +1102,11 @@ sql_bind_blob64(sql_stmt * pStmt,
 int
 sql_bind_double(sql_stmt * pStmt, int i, double rValue)
 {
-	int rc;
 	Vdbe *p = (Vdbe *) pStmt;
-	rc = vdbeUnbind(p, i);
-	if (rc == 0) {
-		rc = sql_bind_type(p, i, "NUMERIC");
-		sqlVdbeMemSetDouble(&p->aVar[i - 1], rValue);
-	}
+	if (vdbeUnbind(p, i) != 0)
+		return -1;
+	int rc = sql_bind_type(p, i, "NUMERIC");
+	sqlVdbeMemSetDouble(&p->aVar[i - 1], rValue);
 	return rc;
 }
 
@@ -1123,11 +1114,10 @@ int
 sql_bind_boolean(struct sql_stmt *stmt, int i, bool value)
 {
 	struct Vdbe *p = (struct Vdbe *) stmt;
-	int rc = vdbeUnbind(p, i);
-	if (rc == 0) {
-		rc = sql_bind_type(p, i, "BOOLEAN");
-		mem_set_bool(&p->aVar[i - 1], value);
-	}
+	if (vdbeUnbind(p, i) != 0)
+		return -1;
+	int rc = sql_bind_type(p, i, "BOOLEAN");
+	mem_set_bool(&p->aVar[i - 1], value);
 	return rc;
 }
 
@@ -1140,25 +1130,21 @@ sql_bind_int(sql_stmt * p, int i, int iValue)
 int
 sql_bind_int64(sql_stmt * pStmt, int i, sql_int64 iValue)
 {
-	int rc;
 	Vdbe *p = (Vdbe *) pStmt;
-	rc = vdbeUnbind(p, i);
-	if (rc == 0) {
-		rc = sql_bind_type(p, i, "INTEGER");
-		sqlVdbeMemSetInt64(&p->aVar[i - 1], iValue);
-	}
+	if (vdbeUnbind(p, i) != 0)
+		return -1;
+	int rc = sql_bind_type(p, i, "INTEGER");
+	sqlVdbeMemSetInt64(&p->aVar[i - 1], iValue);
 	return rc;
 }
 
 int
 sql_bind_null(sql_stmt * pStmt, int i)
 {
-	int rc;
 	Vdbe *p = (Vdbe *) pStmt;
-	rc = vdbeUnbind(p, i);
-	if (rc == 0)
-		rc = sql_bind_type(p, i, "BOOLEAN");
-	return rc;
+	if (vdbeUnbind(p, i) != 0)
+		return -1;
+	return sql_bind_type(p, i, "BOOLEAN");
 }
 
 int
@@ -1199,13 +1185,11 @@ sql_bind_text64(sql_stmt * pStmt,
 int
 sql_bind_zeroblob(sql_stmt * pStmt, int i, int n)
 {
-	int rc;
 	Vdbe *p = (Vdbe *) pStmt;
-	rc = vdbeUnbind(p, i);
-	if (rc == 0) {
-		sqlVdbeMemSetZeroBlob(&p->aVar[i - 1], n);
-	}
-	return rc;
+	if (vdbeUnbind(p, i) != 0)
+		return -1;
+	sqlVdbeMemSetZeroBlob(&p->aVar[i - 1], n);
+	return 0;
 }
 
 int
diff --git a/test/sql/bind.result b/test/sql/bind.result
index 076bf83..18d546a 100644
--- a/test/sql/bind.result
+++ b/test/sql/bind.result
@@ -294,3 +294,11 @@ box.execute('DROP TABLE test')
 ---
 - row_count: 1
 ...
+box.execute('SELECT ?', {1, 2})
+---
+- error: 'Failed to execute SQL statement: The number of parameters is too large'
+...
+box.execute('SELECT $2', {1, 2, 3})
+---
+- error: 'Failed to execute SQL statement: The number of parameters is too large'
+...
diff --git a/test/sql/bind.test.lua b/test/sql/bind.test.lua
index 229207d..9dac485 100644
--- a/test/sql/bind.test.lua
+++ b/test/sql/bind.test.lua
@@ -98,3 +98,6 @@ end;
 test_run:cmd("setopt delimiter ''");
 
 box.execute('DROP TABLE test')
+
+box.execute('SELECT ?', {1, 2})
+box.execute('SELECT $2', {1, 2, 3})
-- 
2.7.4





More information about the Tarantool-patches mailing list