[Tarantool-patches] [PATCH v1 04/10] sql: introduce mem_convert_to_binary()

imeevma at tarantool.org imeevma at tarantool.org
Mon Feb 1 11:14:56 MSK 2021


This patch introduces function mem_convert_to_binary() which allows us
to unify a method to convert a MEM to a MEM of type VARBINARY.

Part of #4470
---
 src/box/sql/vdbeInt.h |  8 ++++++++
 src/box/sql/vdbemem.c | 15 +++++++++++++++
 2 files changed, 23 insertions(+)

diff --git a/src/box/sql/vdbeInt.h b/src/box/sql/vdbeInt.h
index 0bb045170..213531d05 100644
--- a/src/box/sql/vdbeInt.h
+++ b/src/box/sql/vdbeInt.h
@@ -713,6 +713,14 @@ mems_have_same_type(const struct Mem *mem1, const struct Mem *mem2)
 	       (mem2->flags & MEM_PURE_TYPE_MASK);
 }
 
+/**
+ * Cast MEM to varbinary according to explicit cast rules.
+ *
+ * @param mem VDBE memory register to convert.
+ */
+int
+mem_convert_to_binary(struct Mem *mem);
+
 void sqlVdbeMemInit(Mem *, sql *, u32);
 void sqlVdbeMemSetNull(Mem *);
 void sqlVdbeMemSetZeroBlob(Mem *, int);
diff --git a/src/box/sql/vdbemem.c b/src/box/sql/vdbemem.c
index 186aebe03..849d490b8 100644
--- a/src/box/sql/vdbemem.c
+++ b/src/box/sql/vdbemem.c
@@ -981,6 +981,21 @@ mem_set_array(struct Mem *mem, char *value, uint32_t size, int alloc_type)
 	return 0;
 }
 
+int
+mem_convert_to_binary(struct Mem *mem)
+{
+	if (mem_is_null(mem) || mem_is_binary(mem))
+		return 0;
+	if (mem_is_string(mem)) {
+		mem->flags = (mem->flags & (MEM_Dyn | MEM_Static | MEM_Ephem)) |
+			     MEM_Blob;
+		return 0;
+	}
+	diag_set(ClientError, ER_SQL_TYPE_MISMATCH, sql_value_to_diag_str(mem),
+		 "varbinary");
+	return -1;
+}
+
 /*
  * Return true if the Mem object contains a TEXT or BLOB that is
  * too large - whose size exceeds SQL_MAX_LENGTH.
-- 
2.25.1



More information about the Tarantool-patches mailing list