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 084B36CE3E; Thu, 11 Nov 2021 13:52:34 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 084B36CE3E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1636627954; bh=lK3rNs5pj6rsYBNy5cJwVUgioN08KOgzoEbRU9y9Fdg=; 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=yVeaMPGgaDSLjuIorylpQaD06cRo9VpwqtZkCjG4tLQSysO59XmJijysr0yXJK9Jl V+yN/YtF+yXg+KEdKV2X6d3nhyITeojqTtnmT8EXs/EUKb9qjZYwt2zWGpBR5sbc/P MIe9slIxPQBscNYnBBF9M45pr2pw0d+zVXWRTysM= 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 D46486CE3E for ; Thu, 11 Nov 2021 13:48:58 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org D46486CE3E Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1ml7dZ-0004uK-S9; Thu, 11 Nov 2021 13:48:58 +0300 To: kyukhin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Thu, 11 Nov 2021 13:48:57 +0300 Message-Id: <83dba41a322852649714e4ed03f5e942b279e794.1636627579.git.imeevma@gmail.com> 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: 4F1203BC0FB41BD9731B3922EC0639794B1FA730FFF2612B18842EFC24E0D5F400894C459B0CD1B9CA89F31D4DC5934F6CFDAD9D842F4A3FBD7B9CED87D2556729FF1465CC0F6BAA X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7AC4684DF4EC4B256EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637AF78C68CB7D402738638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D855902150E3550DB4CFBD5EA2D2CDD737117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCAA867293B0326636D2E47CDBA5A96583BD4B6F7A4D31EC0BC014FD901B82EE079FA2833FD35BB23D27C277FBC8AE2E8BF1175FABE1C0F9B6A471835C12D1D977C4224003CC8364762BB6847A3DEAEFB0F43C7A68FF6260569E8FC8737B5C2249EC8D19AE6D49635B68655334FD4449CB9ECD01F8117BC8BEAAAE862A0553A39223F8577A6DFFEA7C289736CE4F78F08343847C11F186F3C59DAA53EE0834AAEE X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C7E9FEBB9C11794A1D4DE266D342FF3FC06B69C15534DEAC99C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EFCDE59DDCCE53AF2B699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D346B222596F62B8FA9A1F9F4D701E63B979EAA6100E822886A2A6767418BD3272DD26FEB3C12E2C6131D7E09C32AA3244C1119BE27CE1836467B488A9958CF6BC05595C85A795C7BAE729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj4t8MBgWr8bI7k65/K+9gaw== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5DD3DC6AEF3BF3A2F52670012F6FEECD6783D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v1 05/21] sql: refactor PRINTF() 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" Part of #4145 --- src/box/sql/func.c | 62 +++++++++++++++++++------------------------- src/box/sql/printf.c | 6 ++--- src/box/sql/sqlInt.h | 3 ++- 3 files changed, 31 insertions(+), 40 deletions(-) diff --git a/src/box/sql/func.c b/src/box/sql/func.c index c0e8c4416..603deb44e 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -860,6 +860,31 @@ func_octet_length(struct sql_context *ctx, int argc, struct Mem *argv) mem_set_uint(ctx->pOut, arg->n); } +/** Implementation of the PRINTF() function. */ +static void +func_printf(struct sql_context *ctx, int argc, struct Mem *argv) +{ + if (argc < 1 || mem_is_null(&argv[0])) + return; + if (argc == 1 || !mem_is_str(&argv[0])) { + struct Mem *mem = ctx->pOut; + if (mem_copy(mem, &argv[0]) != 0 || mem_to_str(mem) != 0) + ctx->is_aborted = true; + return; + } + struct PrintfArguments pargs; + struct StrAccum acc; + char *format = argv[0].z; + pargs.nArg = argc - 1; + pargs.nUsed = 0; + pargs.apArg = argv + 1; + struct sql *db = sql_get(); + sqlStrAccumInit(&acc, db, 0, 0, db->aLimit[SQL_LIMIT_LENGTH]); + acc.printfFlags = SQL_PRINTF_SQLFUNC; + sqlXPrintf(&acc, format, &pargs); + mem_set_str_allocated(ctx->pOut, sqlStrAccumFinish(&acc), acc.nChar); +} + static const unsigned char * mem_as_ustr(struct Mem *mem) { @@ -954,40 +979,6 @@ typeofFunc(struct sql_context *context, int argc, struct Mem *argv) sql_result_text(context, z, -1, SQL_STATIC); } -/* - * Implementation of the printf() function. - */ -static void -printfFunc(struct sql_context *context, int argc, struct Mem *argv) -{ - PrintfArguments x; - StrAccum str; - const char *zFormat; - int n; - sql *db = sql_context_db_handle(context); - - if (argc >= 1 && (zFormat = mem_as_str0(&argv[0])) != NULL) { - x.nArg = argc - 1; - x.nUsed = 0; - x.apArg = sqlDbMallocRawNN(sql_get(), - (argc - 1) * sizeof(*x.apArg)); - if (x.apArg == NULL) { - context->is_aborted = true; - return; - } - for (int i = 1; i < argc; ++i) - x.apArg[i - 1] = &argv[i]; - sqlStrAccumInit(&str, db, 0, 0, - db->aLimit[SQL_LIMIT_LENGTH]); - str.printfFlags = SQL_PRINTF_SQLFUNC; - sqlXPrintf(&str, zFormat, &x); - sqlDbFree(sql_get(), x.apArg); - n = str.nChar; - sql_result_text(context, sqlStrAccumFinish(&str), n, - SQL_DYNAMIC); - } -} - /* * Implementation of the round() function */ @@ -1930,8 +1921,7 @@ static struct sql_func_definition definitions[] = { FIELD_TYPE_INTEGER, func_position_characters, NULL}, {"POSITION", 2, {FIELD_TYPE_VARBINARY, FIELD_TYPE_VARBINARY}, FIELD_TYPE_INTEGER, func_position_octets, NULL}, - {"PRINTF", -1, {FIELD_TYPE_ANY}, FIELD_TYPE_STRING, printfFunc, - NULL}, + {"PRINTF", -1, {FIELD_TYPE_ANY}, FIELD_TYPE_STRING, func_printf, NULL}, {"QUOTE", 1, {FIELD_TYPE_ANY}, FIELD_TYPE_STRING, quoteFunc, NULL}, {"RANDOM", 0, {}, FIELD_TYPE_INTEGER, randomFunc, NULL}, {"RANDOMBLOB", 1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_VARBINARY, diff --git a/src/box/sql/printf.c b/src/box/sql/printf.c index b4ab0d0f9..5b61646e3 100644 --- a/src/box/sql/printf.c +++ b/src/box/sql/printf.c @@ -144,7 +144,7 @@ getIntArg(PrintfArguments * p) { if (p->nArg <= p->nUsed) return 0; - return mem_get_int_unsafe(p->apArg[p->nUsed++]); + return mem_get_int_unsafe(&p->apArg[p->nUsed++]); } static double @@ -152,7 +152,7 @@ getDoubleArg(PrintfArguments * p) { if (p->nArg <= p->nUsed) return 0.0; - return mem_get_double_unsafe(p->apArg[p->nUsed++]); + return mem_get_double_unsafe(&p->apArg[p->nUsed++]); } static char * @@ -160,7 +160,7 @@ getTextArg(PrintfArguments * p) { if (p->nArg <= p->nUsed) return 0; - struct Mem *mem = p->apArg[p->nUsed++]; + struct Mem *mem = &p->apArg[p->nUsed++]; return (char *)mem_as_str0(mem); } diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h index cfdf71f1f..9361775b1 100644 --- a/src/box/sql/sqlInt.h +++ b/src/box/sql/sqlInt.h @@ -2511,7 +2511,8 @@ int sqlIsNaN(double); struct PrintfArguments { int nArg; /* Total number of arguments */ int nUsed; /* Number of arguments used so far */ - sql_value **apArg; /* The argument values */ + /** The argument values. */ + struct Mem *apArg; }; void sqlVXPrintf(StrAccum *, const char *, va_list); -- 2.25.1