[Tarantool-patches] [PATCH v4 48/53] sql: introduce mem_get_unsigned()

imeevma at tarantool.org imeevma at tarantool.org
Tue Mar 23 12:36:43 MSK 2021


This patch introduces mem_get_unsigned() function which is used to
receive unsigned value from MEM.

Part of #5818
---
 src/box/sql/func.c    | 16 +++++++++++-----
 src/box/sql/mem.c     | 37 +++++++++++++++++++++++++++----------
 src/box/sql/mem.h     |  6 +++---
 src/box/sql/sqlInt.h  |  3 ---
 src/box/sql/vdbeapi.c |  6 ------
 5 files changed, 41 insertions(+), 27 deletions(-)

diff --git a/src/box/sql/func.c b/src/box/sql/func.c
index 0fa0f6ac7..a851d98f2 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -118,9 +118,12 @@ port_vdbemem_dump_lua(struct port *base, struct lua_State *L, bool is_flat)
 			luaL_pushint64(L, n);
 			break;
 		}
-		case MP_UINT:
-			luaL_pushuint64(L, sql_value_uint64(param));
+		case MP_UINT: {
+			uint64_t u;
+			mem_get_unsigned(param, &u);
+			luaL_pushuint64(L, u);
 			break;
+		}
 		case MP_DOUBLE:
 			lua_pushnumber(L, sql_value_double(param));
 			break;
@@ -171,7 +174,8 @@ port_vdbemem_get_msgpack(struct port *base, uint32_t *size)
 			FALLTHROUGH;
 		}
 		case MP_UINT: {
-			sql_uint64 val = sql_value_uint64(param);
+			uint64_t val;
+			mem_get_unsigned(param, &val);
 			mpstream_encode_uint(&stream, val);
 			break;
 		}
@@ -511,7 +515,9 @@ absFunc(sql_context * context, int argc, sql_value ** argv)
 	UNUSED_PARAMETER(argc);
 	switch (sql_value_type(argv[0])) {
 	case MP_UINT: {
-		sql_result_uint(context, sql_value_uint64(argv[0]));
+		uint64_t u;
+		mem_get_unsigned(argv[0], &u);
+		sql_result_uint(context, u);
 		break;
 	}
 	case MP_INT: {
@@ -1487,7 +1493,7 @@ charFunc(sql_context * context, int argc, sql_value ** argv)
 		if (sql_value_type(argv[i]) == MP_INT)
 			x = 0xfffd;
 		else
-			x = sql_value_uint64(argv[i]);
+			mem_get_unsigned(argv[i], &x);
 		if (x > 0x10ffff)
 			x = 0xfffd;
 		c = (unsigned)(x & 0x1fffff);
diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c
index 540931723..6ab6d77ad 100644
--- a/src/box/sql/mem.c
+++ b/src/box/sql/mem.c
@@ -1259,6 +1259,33 @@ mem_get_integer(const struct Mem *mem, int64_t *i, bool *is_neg)
 	return -1;
 }
 
+int
+mem_get_unsigned(const struct Mem *mem, uint64_t *u)
+{
+	if ((mem->flags & MEM_Int) != 0)
+		return -1;
+	if ((mem->flags & MEM_UInt) != 0) {
+		*u = mem->u.u;
+		return 0;
+	}
+	if ((mem->flags & (MEM_Str | MEM_Blob)) != 0) {
+		bool is_neg;
+		if (sql_atoi64(mem->z, (int64_t *)u, &is_neg, mem->n) != 0 ||
+		    is_neg)
+			return -1;
+		return 0;
+	}
+	if ((mem->flags & MEM_Real) != 0) {
+		double d = mem->u.r;
+		if (d >= 0 && d < (double)UINT64_MAX) {
+			*u = (uint64_t)d;
+			return 0;
+		}
+		return -1;
+	}
+	return -1;
+}
+
 int
 mem_copy(struct Mem *to, const struct Mem *from)
 {
@@ -2497,16 +2524,6 @@ sql_value_boolean(sql_value *val)
 	return b;
 }
 
-uint64_t
-sql_value_uint64(sql_value *val)
-{
-	int64_t i = 0;
-	bool is_neg;
-	mem_get_integer((struct Mem *) val, &i, &is_neg);
-	assert(!is_neg);
-	return i;
-}
-
 const unsigned char *
 sql_value_text(sql_value * pVal)
 {
diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h
index 7857efc1d..ebc9c6c20 100644
--- a/src/box/sql/mem.h
+++ b/src/box/sql/mem.h
@@ -348,6 +348,9 @@ mem_implicit_cast_old(struct Mem *mem, enum field_type type);
 int
 mem_get_integer(const struct Mem *mem, int64_t *i, bool *is_neg);
 
+int
+mem_get_unsigned(const struct Mem *mem, uint64_t *u);
+
 /**
  * Simple type to str convertor. It is used to simplify
  * error reporting.
@@ -439,9 +442,6 @@ sql_value_double(struct Mem *);
 bool
 sql_value_boolean(struct Mem *val);
 
-uint64_t
-sql_value_uint64(struct Mem *val);
-
 const unsigned char *
 sql_value_text(struct Mem *);
 
diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h
index 14385705f..7f9539aea 100644
--- a/src/box/sql/sqlInt.h
+++ b/src/box/sql/sqlInt.h
@@ -448,9 +448,6 @@ sql_column_double(sql_stmt *, int iCol);
 bool
 sql_column_boolean(struct sql_stmt *stmt, int column);
 
-uint64_t
-sql_column_uint64(struct sql_stmt *stmt, int column);
-
 const unsigned char *
 sql_column_text(sql_stmt *,
 		    int iCol);
diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c
index abdc5f0d0..b6db98031 100644
--- a/src/box/sql/vdbeapi.c
+++ b/src/box/sql/vdbeapi.c
@@ -497,12 +497,6 @@ sql_column_boolean(struct sql_stmt *stmt, int i)
 	return sql_value_boolean(columnMem(stmt, i));
 }
 
-uint64_t
-sql_column_uint64(sql_stmt * pStmt, int i)
-{
-	return sql_value_uint64(columnMem(pStmt, i));
-}
-
 const unsigned char *
 sql_column_text(sql_stmt * pStmt, int i)
 {
-- 
2.25.1



More information about the Tarantool-patches mailing list