[Tarantool-patches] [PATCH v4 52/53] sql: introduce mem_get_binary()

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


This patch introduces mem_get_binary() which is used to receive binary
value from MEM.

Part of #5818
---
 src/box/sql/func.c    | 31 ++++++++++++++++++++++---------
 src/box/sql/mem.c     | 35 +++++++++++++----------------------
 src/box/sql/mem.h     |  6 +++---
 src/box/sql/sqlInt.h  |  3 ---
 src/box/sql/vdbeapi.c |  7 -------
 5 files changed, 38 insertions(+), 44 deletions(-)

diff --git a/src/box/sql/func.c b/src/box/sql/func.c
index 78f4ec3b5..199f3abef 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -72,6 +72,19 @@ mem_get_ustr(struct Mem *mem)
 	return (const unsigned char *)str;
 }
 
+static const void *
+mem_get_blob(struct Mem *mem)
+{
+	const char *s;
+	if (!mem_is_varstring(mem) && mem_convert_to_string(mem) != 0)
+		return NULL;
+	if (ExpandBlob(mem) != 0)
+		return NULL;
+	if (mem_get_binary(mem, &s) != 0)
+		return NULL;
+	return (const void *)s;
+}
+
 /*
  * Return the collating function associated with a function.
  */
@@ -154,7 +167,7 @@ port_vdbemem_dump_lua(struct port *base, struct lua_State *L, bool is_flat)
 		case MP_BIN:
 		case MP_ARRAY:
 		case MP_MAP:
-			lua_pushlstring(L, sql_value_blob(param),
+			lua_pushlstring(L, mem_get_blob(param),
 					(size_t) sql_value_bytes(param));
 			break;
 		case MP_NIL:
@@ -218,7 +231,7 @@ port_vdbemem_get_msgpack(struct port *base, uint32_t *size)
 		case MP_ARRAY:
 		case MP_MAP: {
 			mpstream_encode_binl(&stream, sql_value_bytes(param));
-			mpstream_memcpy(&stream, sql_value_blob(param),
+			mpstream_memcpy(&stream, mem_get_blob(param),
 					sql_value_bytes(param));
 			break;
 		}
@@ -641,8 +654,8 @@ position_func(struct sql_context *context, int argc, struct Mem **argv)
 		const unsigned char *haystack_str;
 		const unsigned char *needle_str;
 		if (haystack_type == MP_BIN) {
-			needle_str = sql_value_blob(needle);
-			haystack_str = sql_value_blob(haystack);
+			needle_str = mem_get_blob(needle);
+			haystack_str = mem_get_blob(haystack);
 			assert(needle_str != NULL);
 			assert(haystack_str != NULL || n_haystack_bytes == 0);
 			/*
@@ -785,7 +798,7 @@ substrFunc(sql_context * context, int argc, sql_value ** argv)
 	mem_get_integer(argv[1], &p1, &unused);
 	if (p0type == MP_BIN) {
 		len = sql_value_bytes(argv[0]);
-		z = sql_value_blob(argv[0]);
+		z = mem_get_blob(argv[0]);
 		if (z == 0)
 			return;
 		assert(len == sql_value_bytes(argv[0]));
@@ -1420,9 +1433,9 @@ quoteFunc(sql_context * context, int argc, sql_value ** argv)
 	case MP_ARRAY:
 	case MP_MAP: {
 			char *zText = 0;
-			char const *zBlob = sql_value_blob(argv[0]);
+			char const *zBlob = mem_get_blob(argv[0]);
 			int nBlob = sql_value_bytes(argv[0]);
-			assert(zBlob == sql_value_blob(argv[0]));	/* No encoding change */
+			assert(zBlob == mem_get_blob(argv[0]));	/* No encoding change */
 			zText =
 			    (char *)contextMalloc(context,
 						  (2 * (i64) nBlob) + 4);
@@ -1556,9 +1569,9 @@ hexFunc(sql_context * context, int argc, sql_value ** argv)
 	char *zHex, *z;
 	assert(argc == 1);
 	UNUSED_PARAMETER(argc);
-	pBlob = sql_value_blob(argv[0]);
+	pBlob = mem_get_blob(argv[0]);
 	n = sql_value_bytes(argv[0]);
-	assert(pBlob == sql_value_blob(argv[0]));	/* No encoding change */
+	assert(pBlob == mem_get_blob(argv[0]));	/* No encoding change */
 	z = zHex = contextMalloc(context, ((i64) n) * 2 + 1);
 	if (zHex) {
 		for (i = 0; i < n; i++, pBlob++) {
diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c
index efd3e4677..bed9ae64a 100644
--- a/src/box/sql/mem.c
+++ b/src/box/sql/mem.c
@@ -1328,6 +1328,19 @@ mem_get_string0(const struct Mem *mem, const char **s)
 	return 0;
 }
 
+int
+mem_get_binary(const struct Mem *mem, const char **s)
+{
+	if ((mem->flags & MEM_Str) != 0) {
+		*s = mem->n > 0 ? mem->z : NULL;
+		return 0;
+	}
+	if ((mem->flags & MEM_Blob) == 0 || (mem->flags & MEM_Zero) != 0)
+		return -1;
+	*s = mem->z;
+	return 0;
+}
+
 int
 mem_copy(struct Mem *to, const struct Mem *from)
 {
@@ -2434,28 +2447,6 @@ releaseMemArray(Mem * p, int N)
 	}
 }
 
-/**************************** sql_value_  ******************************
- * The following routines extract information from a Mem or sql_value
- * structure.
- */
-const void *
-sql_value_blob(sql_value * pVal)
-{
-	Mem *p = (Mem *) pVal;
-	if (p->flags & (MEM_Blob | MEM_Str)) {
-		if (ExpandBlob(p) != 0) {
-			assert(p->flags == MEM_Null && p->z == 0);
-			return 0;
-		}
-		p->flags |= MEM_Blob;
-		return p->n ? p->z : 0;
-	} else {
-		if (mem_convert_to_string(pVal) != 0)
-			return NULL;
-		return pVal->z;
-	}
-}
-
 int
 sql_value_bytes(sql_value * pVal)
 {
diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h
index fb2385541..8af924d43 100644
--- a/src/box/sql/mem.h
+++ b/src/box/sql/mem.h
@@ -360,6 +360,9 @@ mem_get_boolean(const struct Mem *mem, bool *b);
 int
 mem_get_string0(const struct Mem *mem, const char **s);
 
+int
+mem_get_binary(const struct Mem *mem, const char **s);
+
 /**
  * Simple type to str convertor. It is used to simplify
  * error reporting.
@@ -435,9 +438,6 @@ releaseMemArray(Mem * p, int N);
 
 /** Getters. */
 
-const void *
-sql_value_blob(struct Mem *);
-
 int
 sql_value_bytes(struct Mem *);
 
diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h
index 2f995ffc6..9dfd0e4cc 100644
--- a/src/box/sql/sqlInt.h
+++ b/src/box/sql/sqlInt.h
@@ -433,9 +433,6 @@ sql_stmt_compile(const char *sql, int bytes_count, struct Vdbe *re_prepared,
 int
 sql_step(sql_stmt *);
 
-const void *
-sql_column_blob(sql_stmt *, int iCol);
-
 int
 sql_column_bytes(sql_stmt *, int iCol);
 
diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c
index 465284567..e76170965 100644
--- a/src/box/sql/vdbeapi.c
+++ b/src/box/sql/vdbeapi.c
@@ -471,13 +471,6 @@ columnMem(sql_stmt * pStmt, int i)
  * The following routines are used to access elements of the current row
  * in the result set.
  */
-const void *
-sql_column_blob(sql_stmt * pStmt, int i)
-{
-	const void *val;
-	val = sql_value_blob(columnMem(pStmt, i));
-	return val;
-}
 
 int
 sql_column_bytes(sql_stmt * pStmt, int i)
-- 
2.25.1



More information about the Tarantool-patches mailing list