From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id C2D8D6F3E5; Thu, 11 Nov 2021 13:58:37 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org C2D8D6F3E5 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1636628317; bh=Z1BJvytMNhTb/X8QXrWZSlP6hkqOMn5ZzRxidGKXhDc=; h=To:Cc:Date:In-Reply-To:References:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=GBH2Xt9VLAwTC0uf59UQQXFQi/rWS76IWICWHuiJ2Y+/iEhLu10TGX18Unz+ThKf5 F5aWwpVxwMPZdWvNiv0ynwtnYTJxutw+4ND3JNTKZBY/M9K3tGFbfxOQrsxW7dWfJB oGEXPPTisx3Zyij+foyOU4+pJ7GdxIuuH2DJcH68= Received: from smtpng1.i.mail.ru (smtpng1.i.mail.ru [94.100.181.251]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id A65E37106F for ; Thu, 11 Nov 2021 13:49:20 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org A65E37106F Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1ml7dv-00058B-Tc; Thu, 11 Nov 2021 13:49:20 +0300 To: kyukhin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Thu, 11 Nov 2021 13:49:19 +0300 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD9731B3922EC0639796A433260860B48E60A001AF1EB3A7EE600894C459B0CD1B9CE37A60D794CEC66B8B1C43DA75E1EFFBD7B9CED87D2556787D23EDDC3F839DD X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7A8325FA649D0A450EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006379A70878BADDF00B98638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8116DAED497E0A235C8F8E410EDBD4678117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCAA867293B0326636D2E47CDBA5A96583BD4B6F7A4D31EC0BC014FD901B82EE079FA2833FD35BB23D27C277FBC8AE2E8BAA867293B0326636D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B66F6A3E018CF4DC80089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C7E9FEBB9C11794A1711E8B1576933ED3264F9B831B475E189C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EFCDE59DDCCE53AF2B699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D3435BBF0AC4E3A921CBC76A1BD408A8463D5DAADC88B1E779DB071B1D21A9303880EE2498F054662751D7E09C32AA3244CDD745AB02CF2B4D4163C5C20F94BF7F5A8CE788DE6831205729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj4t8MBgWr8bLfat51o6Y1lg== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D9418C24AE49724EAB7B96AD30AD3D00E83D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v1 17/21] sql: refactor QUOTE() function X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Mergen Imeev via Tarantool-patches Reply-To: imeevma@tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" --- 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 3b1e62ebf..a0e599b5e 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -1054,50 +1054,6 @@ func_unicode(struct sql_context *ctx, int argc, struct Mem *argv) mem_set_uint(ctx->pOut, (uint64_t)c); } -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) @@ -1395,83 +1351,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