From: Mergen Imeev via Tarantool-patches <tarantool-patches@dev.tarantool.org> To: v.shpilevoy@tarantool.org Cc: tarantool-patches@dev.tarantool.org Subject: [Tarantool-patches] [PATCH v1 17/21] sql: refactor QUOTE() function Date: Fri, 8 Oct 2021 20:32:03 +0300 [thread overview] Message-ID: <68b2fdf4019309fb8bca9d3ce0df8fd9a3a07b20.1633713432.git.imeevma@gmail.com> (raw) In-Reply-To: <cover.1633713432.git.imeevma@gmail.com> --- src/box/sql/func.c | 154 +++++++++++++++------------------------------ 1 file changed, 50 insertions(+), 104 deletions(-) diff --git a/src/box/sql/func.c b/src/box/sql/func.c index f26d101e9..4e7404337 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -1020,50 +1020,6 @@ func_unicode(struct sql_context *ctx, int argc, struct Mem *argv) mem_set_uint(ctx->pOut, sqlUtf8Read((const unsigned char **)&str)); } -static const unsigned char * -mem_as_ustr(struct Mem *mem) -{ - return (const unsigned char *)mem_as_str0(mem); -} - -static const void * -mem_as_bin(struct Mem *mem) -{ - const char *s; - if (mem_cast_explicit(mem, FIELD_TYPE_VARBINARY) != 0 && - mem_to_str(mem) != 0) - return NULL; - if (mem_get_bin(mem, &s) != 0) - return NULL; - return s; -} - -/* - * Allocate nByte bytes of space using sqlMalloc(). If the - * allocation fails, return NULL. If nByte is larger than the - * maximum string or blob length, then raise an error and return - * NULL. - */ -static void * -contextMalloc(struct sql_context *context, i64 nByte) -{ - char *z; - sql *db = sql_context_db_handle(context); - assert(nByte > 0); - testcase(nByte == db->aLimit[SQL_LIMIT_LENGTH]); - testcase(nByte == db->aLimit[SQL_LIMIT_LENGTH] + 1); - if (nByte > db->aLimit[SQL_LIMIT_LENGTH]) { - diag_set(ClientError, ER_SQL_EXECUTE, "string or blob too big"); - context->is_aborted = true; - z = 0; - } else { - z = sqlMalloc(nByte); - if (z == NULL) - context->is_aborted = true; - } - return z; -} - #define Utf8Read(s, e) \ ucnv_getNextUChar(icu_utf8_conv, &(s), (e), &status) @@ -1359,83 +1315,73 @@ quoteFunc(struct sql_context *context, int argc, struct Mem *argv) case MEM_TYPE_UUID: { char buf[UUID_STR_LEN + 1]; tt_uuid_to_string(&argv[0].u.uuid, &buf[0]); - sql_result_text(context, buf, UUID_STR_LEN, SQL_TRANSIENT); + if (mem_copy_str(context->pOut, buf, UUID_STR_LEN) != 0) + context->is_aborted = true; break; } case MEM_TYPE_DOUBLE: case MEM_TYPE_DEC: case MEM_TYPE_UINT: case MEM_TYPE_INT: { - sql_result_value(context, &argv[0]); - break; - } + if (mem_copy(context->pOut, &argv[0]) != 0) + context->is_aborted = true; + break; + } case MEM_TYPE_BIN: case MEM_TYPE_ARRAY: case MEM_TYPE_MAP: { - char *zText = 0; - char const *zBlob = mem_as_bin(&argv[0]); - int nBlob = mem_len_unsafe(&argv[0]); - assert(zBlob == mem_as_bin(&argv[0])); /* No encoding change */ - zText = - (char *)contextMalloc(context, - (2 * (i64) nBlob) + 4); - if (zText) { - int i; - for (i = 0; i < nBlob; i++) { - zText[(i * 2) + 2] = - hexdigits[(zBlob[i] >> 4) & 0x0F]; - zText[(i * 2) + 3] = - hexdigits[(zBlob[i]) & 0x0F]; - } - zText[(nBlob * 2) + 2] = '\''; - zText[(nBlob * 2) + 3] = '\0'; - zText[0] = 'X'; - zText[1] = '\''; - sql_result_text(context, zText, -1, - SQL_TRANSIENT); - sql_free(zText); - } - break; + const char *zBlob = argv[0].z; + int nBlob = argv[0].n; + uint32_t size = 2 * nBlob + 3; + char *zText = zText = sqlDbMallocRawNN(sql_get(), size); + if (zText == NULL) { + context->is_aborted = true; + return; } + for (int i = 0; i < nBlob; i++) { + zText[(i * 2) + 2] = hexdigits[(zBlob[i] >> 4) & 0x0F]; + zText[(i * 2) + 3] = hexdigits[(zBlob[i]) & 0x0F]; + } + zText[(nBlob * 2) + 2] = '\''; + zText[0] = 'X'; + zText[1] = '\''; + mem_set_str_allocated(context->pOut, zText, size); + break; + } case MEM_TYPE_STR: { - int i, j; - u64 n; - const unsigned char *zArg = mem_as_ustr(&argv[0]); - char *z; + const char *str = argv[0].z; + uint32_t len = argv[0].n; + uint32_t count = 0; + for (uint32_t i = 0; i < len; ++i) { + if (str[i] == '\'') + ++count; + } + uint32_t size = len + count + 2; - if (zArg == 0) - return; - for (i = 0, n = 0; zArg[i]; i++) { - if (zArg[i] == '\'') - n++; - } - z = contextMalloc(context, ((i64) i) + ((i64) n) + 3); - if (z) { - z[0] = '\''; - for (i = 0, j = 1; zArg[i]; i++) { - z[j++] = zArg[i]; - if (zArg[i] == '\'') { - z[j++] = '\''; - } - } - z[j++] = '\''; - z[j] = 0; - sql_result_text(context, z, j, - sql_free); - } - break; + char *res = sqlDbMallocRawNN(sql_get(), size); + if (res == NULL) { + context->is_aborted = true; + return; + } + res[0] = '\''; + for (uint32_t i = 0, j = 1; i < len; ++i) { + res[j++] = str[i]; + if (str[i] == '\'') + res[j++] = '\''; } + res[size - 1] = '\''; + mem_set_str_allocated(context->pOut, res, size); + break; + } case MEM_TYPE_BOOL: { - sql_result_text(context, - SQL_TOKEN_BOOLEAN(argv[0].u.b), - -1, SQL_TRANSIENT); + mem_set_str0_static(context->pOut, + SQL_TOKEN_BOOLEAN(argv[0].u.b)); break; } default:{ - assert(mem_is_null(&argv[0])); - sql_result_text(context, "NULL", 4, SQL_STATIC); - break; - } + assert(mem_is_null(&argv[0])); + mem_set_str0_static(context->pOut, "NULL"); + } } } -- 2.25.1
next prev parent reply other threads:[~2021-10-08 17:40 UTC|newest] Thread overview: 54+ messages / expand[flat|nested] mbox.gz Atom feed top 2021-10-08 17:31 [Tarantool-patches] [PATCH v1 00/21] Refactor non-standard and non-aggragate functions Mergen Imeev via Tarantool-patches 2021-10-08 17:31 ` [Tarantool-patches] [PATCH v1 01/21] sql: refactor CHAR() function Mergen Imeev via Tarantool-patches 2021-10-14 22:42 ` Vladislav Shpilevoy via Tarantool-patches 2021-10-25 8:02 ` Mergen Imeev via Tarantool-patches 2021-10-29 23:42 ` Vladislav Shpilevoy via Tarantool-patches 2021-11-02 11:35 ` Mergen Imeev via Tarantool-patches 2021-10-08 17:31 ` [Tarantool-patches] [PATCH v1 02/21] sql: refactor GREATEST() and LEAST() functions Mergen Imeev via Tarantool-patches 2021-10-14 22:42 ` Vladislav Shpilevoy via Tarantool-patches 2021-10-25 8:17 ` Mergen Imeev via Tarantool-patches 2021-10-08 17:31 ` [Tarantool-patches] [PATCH v1 03/21] sql: refactor HEX() function Mergen Imeev via Tarantool-patches 2021-10-14 22:43 ` Vladislav Shpilevoy via Tarantool-patches 2021-10-25 8:19 ` Mergen Imeev via Tarantool-patches 2021-10-08 17:31 ` [Tarantool-patches] [PATCH v1 04/21] sql: refactor LENGTH() function Mergen Imeev via Tarantool-patches 2021-10-14 22:43 ` Vladislav Shpilevoy via Tarantool-patches 2021-10-25 8:30 ` Mergen Imeev via Tarantool-patches 2021-10-29 23:42 ` Vladislav Shpilevoy via Tarantool-patches 2021-11-02 11:39 ` Mergen Imeev via Tarantool-patches 2021-10-08 17:31 ` [Tarantool-patches] [PATCH v1 05/21] sql: refactor PRINTF() function Mergen Imeev via Tarantool-patches 2021-10-14 22:44 ` Vladislav Shpilevoy via Tarantool-patches 2021-10-25 8:33 ` Mergen Imeev via Tarantool-patches 2021-10-08 17:31 ` [Tarantool-patches] [PATCH v1 06/21] sql: refactor RANDOM() function Mergen Imeev via Tarantool-patches 2021-10-25 8:35 ` Mergen Imeev via Tarantool-patches 2021-10-08 17:31 ` [Tarantool-patches] [PATCH v1 07/21] sql: rework RANDOMBLOB() function Mergen Imeev via Tarantool-patches 2021-10-25 8:36 ` Mergen Imeev via Tarantool-patches 2021-10-08 17:31 ` [Tarantool-patches] [PATCH v1 08/21] sql: refactor ZEROBLOB() function Mergen Imeev via Tarantool-patches 2021-10-25 8:37 ` Mergen Imeev via Tarantool-patches 2021-10-08 17:31 ` [Tarantool-patches] [PATCH v1 09/21] sql: refactor TYPEOF() function Mergen Imeev via Tarantool-patches 2021-10-08 17:31 ` [Tarantool-patches] [PATCH v1 10/21] sql: refactor ROUND() function Mergen Imeev via Tarantool-patches 2021-10-08 17:31 ` [Tarantool-patches] [PATCH v1 11/21] sql: refactor ROW_COUNT() function Mergen Imeev via Tarantool-patches 2021-10-08 17:31 ` [Tarantool-patches] [PATCH v1 12/21] sql: rework UUID() function Mergen Imeev via Tarantool-patches 2021-10-25 8:38 ` Mergen Imeev via Tarantool-patches 2021-10-08 17:31 ` [Tarantool-patches] [PATCH v1 13/21] sql: refactor VERSION() function Mergen Imeev via Tarantool-patches 2021-10-08 17:31 ` [Tarantool-patches] [PATCH v1 14/21] sql: refactor UNICODE() function Mergen Imeev via Tarantool-patches 2021-10-14 22:44 ` Vladislav Shpilevoy via Tarantool-patches 2021-10-25 8:40 ` Mergen Imeev via Tarantool-patches 2021-11-02 11:42 ` Mergen Imeev via Tarantool-patches 2021-10-08 17:32 ` [Tarantool-patches] [PATCH v1 15/21] sql: refactor of SOUNDEX() function Mergen Imeev via Tarantool-patches 2021-10-08 17:32 ` [Tarantool-patches] [PATCH v1 16/21] sql: refactor REPLACE() function Mergen Imeev via Tarantool-patches 2021-10-14 22:45 ` Vladislav Shpilevoy via Tarantool-patches 2021-10-25 8:45 ` Mergen Imeev via Tarantool-patches 2021-10-08 17:32 ` Mergen Imeev via Tarantool-patches [this message] 2021-10-08 17:32 ` [Tarantool-patches] [PATCH v1 18/21] sql: remove unused code Mergen Imeev via Tarantool-patches 2021-10-25 8:51 ` Mergen Imeev via Tarantool-patches 2021-10-08 17:32 ` [Tarantool-patches] [PATCH v1 19/21] sql: remove MEM_Dyn flag Mergen Imeev via Tarantool-patches 2021-10-14 22:46 ` Vladislav Shpilevoy via Tarantool-patches 2021-10-25 8:54 ` Mergen Imeev via Tarantool-patches 2021-10-29 23:43 ` Vladislav Shpilevoy via Tarantool-patches 2021-11-02 11:43 ` Mergen Imeev via Tarantool-patches 2021-10-08 17:32 ` [Tarantool-patches] [PATCH v1 20/21] sql: remove MEM_Term flag Mergen Imeev via Tarantool-patches 2021-10-14 22:47 ` Vladislav Shpilevoy via Tarantool-patches 2021-10-25 9:57 ` Mergen Imeev via Tarantool-patches 2021-10-08 17:32 ` [Tarantool-patches] [PATCH v1 21/21] sql: make arguments to be const Mergen Imeev via Tarantool-patches 2021-11-02 22:15 ` [Tarantool-patches] [PATCH v1 00/21] Refactor non-standard and non-aggragate functions Vladislav Shpilevoy via Tarantool-patches 2021-11-11 10:48 Mergen Imeev via Tarantool-patches 2021-11-11 10:49 ` [Tarantool-patches] [PATCH v1 17/21] sql: refactor QUOTE() function Mergen Imeev via Tarantool-patches
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=68b2fdf4019309fb8bca9d3ce0df8fd9a3a07b20.1633713432.git.imeevma@gmail.com \ --to=tarantool-patches@dev.tarantool.org \ --cc=imeevma@tarantool.org \ --cc=v.shpilevoy@tarantool.org \ --subject='Re: [Tarantool-patches] [PATCH v1 17/21] sql: refactor QUOTE() function' \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox