[Tarantool-patches] [PATCH v1 08/21] sql: refactor ZEROBLOB() function
imeevma at tarantool.org
imeevma at tarantool.org
Fri Oct 8 20:31:46 MSK 2021
Part of #4145
---
src/box/sql/func.c | 49 +++++++++++++++++++++-------------------------
1 file changed, 22 insertions(+), 27 deletions(-)
diff --git a/src/box/sql/func.c b/src/box/sql/func.c
index 51a43f271..485ff33c0 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -913,6 +913,27 @@ func_randomblob(struct sql_context *ctx, int argc, struct Mem *argv)
mem_set_bin_allocated(ctx->pOut, res, len);
}
+/** Implementation of the ZEROBLOB() function. */
+static void
+func_zeroblob(struct sql_context *ctx, int argc, struct Mem *argv)
+{
+ assert(argc == 1);
+ (void)argc;
+ struct Mem *arg = &argv[0];
+ assert(mem_is_null(arg) || mem_is_int(arg));
+ if (mem_is_null(arg) || !mem_is_uint(arg))
+ return;
+ if (arg->u.u == 0)
+ return mem_set_bin_static(ctx->pOut, "", 0);
+ uint64_t len = arg->u.u;
+ char *res = sqlDbMallocZero(sql_get(), len);
+ if (res == NULL) {
+ ctx->is_aborted = true;
+ return;
+ }
+ mem_set_bin_allocated(ctx->pOut, res, len);
+}
+
static const unsigned char *
mem_as_ustr(struct Mem *mem)
{
@@ -1482,32 +1503,6 @@ unicodeFunc(struct sql_context *context, int argc, struct Mem *argv)
sql_result_uint(context, sqlUtf8Read(&z));
}
-/*
- * The zeroblob(N) function returns a zero-filled blob of size N bytes.
- */
-static void
-zeroblobFunc(struct sql_context *context, int argc, struct Mem *argv)
-{
- int64_t n;
- assert(argc == 1);
- UNUSED_PARAMETER(argc);
- n = mem_get_int_unsafe(&argv[0]);
- if (n < 0)
- n = 0;
- if (n > sql_get()->aLimit[SQL_LIMIT_LENGTH]) {
- diag_set(ClientError, ER_SQL_EXECUTE, "string or binary string"\
- "is too big");
- context->is_aborted = true;
- return;
- }
- char *str = sqlDbMallocZero(sql_get(), n);
- if (str == NULL) {
- context->is_aborted = true;
- return;
- }
- mem_set_bin_allocated(context->pOut, str, n);
-}
-
/*
* The replace() function. Three arguments are all strings: call
* them A, B, and C. The result is also a string which is derived
@@ -1953,7 +1948,7 @@ static struct sql_func_definition definitions[] = {
{"UUID", 1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_UUID, sql_func_uuid, NULL},
{"VERSION", 0, {}, FIELD_TYPE_STRING, sql_func_version, NULL},
{"ZEROBLOB", 1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_VARBINARY,
- zeroblobFunc, NULL},
+ func_zeroblob, NULL},
};
static struct sql_func_dictionary *
--
2.25.1
More information about the Tarantool-patches
mailing list