From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtpng2.m.smailru.net (smtpng2.m.smailru.net [94.100.179.3]) (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 6028046970E for ; Tue, 4 Feb 2020 20:12:19 +0300 (MSK) From: Nikita Pettik Date: Tue, 4 Feb 2020 20:12:14 +0300 Message-Id: Subject: [Tarantool-patches] [PATCH] sql: fix off-by-one error while setting bind names List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: tarantool-patches@dev.tarantool.org Cc: v.shpilevoy@tarantool.org Names of bindings are stored in the array indexed from 1 (see struct Vdbe->pVList). So to get name of i-th values to be bound, one should call sqlVListNumToName(list, i+1) not sqlVListNumToName(list, i). For this reason, names of binding parameters returned in meta-information in response to :prepare() call are shifted by one. Let's fix it and calculate position of binding parameter taking into consideration 1-based indexing. Closes #4760 --- Branch: https://github.com/tarantool/tarantool/commits/np/gh-4760-off-by-one-bind Issue: https://github.com/tarantool/tarantool/issues/4760 src/box/sql/vdbeapi.c | 2 +- test/sql/prepared.result | 58 ++++++++++++++++++++++++++++++++++++++++++++++ test/sql/prepared.test.lua | 16 +++++++++++++ 3 files changed, 75 insertions(+), 1 deletion(-) diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c index 182f60aac..c984f9506 100644 --- a/src/box/sql/vdbeapi.c +++ b/src/box/sql/vdbeapi.c @@ -1128,7 +1128,7 @@ sql_bind_parameter_name(const struct sql_stmt *stmt, int i) struct Vdbe *p = (struct Vdbe *) stmt; if (p == NULL) return NULL; - return sqlVListNumToName(p->pVList, i); + return sqlVListNumToName(p->pVList, i+1); } /* diff --git a/test/sql/prepared.result b/test/sql/prepared.result index 71ab0bb57..5a16ea3b1 100644 --- a/test/sql/prepared.result +++ b/test/sql/prepared.result @@ -514,6 +514,64 @@ unprepare(s.stmt_id) | - null | ... +-- gh-4760: make sure that names of all bindings are parsed correctly. +-- +s = prepare("SELECT a FROM test WHERE id = :id AND b = :name") + | --- + | ... +s.params[1] + | --- + | - name: :id + | type: ANY + | ... +s.params[2] + | --- + | - name: :name + | type: ANY + | ... +unprepare(s.stmt_id) + | --- + | - null + | ... + +s = prepare("SELECT ?, :id, :name, ?, @name2, ?") + | --- + | ... +s.params[1] + | --- + | - name: '?' + | type: ANY + | ... +s.params[2] + | --- + | - name: :id + | type: ANY + | ... +s.params[3] + | --- + | - name: :name + | type: ANY + | ... +s.params[4] + | --- + | - name: '?' + | type: ANY + | ... +s.params[5] + | --- + | - name: '@name2' + | type: ANY + | ... +s.params[6] + | --- + | - name: '?' + | type: ANY + | ... +unprepare(s.stmt_id) + | --- + | - null + | ... + -- DML s = prepare("INSERT INTO test VALUES (?, ?, ?);") | --- diff --git a/test/sql/prepared.test.lua b/test/sql/prepared.test.lua index 1e3f02b09..85116aa47 100644 --- a/test/sql/prepared.test.lua +++ b/test/sql/prepared.test.lua @@ -189,6 +189,22 @@ execute(s.stmt_id, {'6'}) execute(s.stmt_id, {'9'}) unprepare(s.stmt_id) +-- gh-4760: make sure that names of all bindings are parsed correctly. +-- +s = prepare("SELECT a FROM test WHERE id = :id AND b = :name") +s.params[1] +s.params[2] +unprepare(s.stmt_id) + +s = prepare("SELECT ?, :id, :name, ?, @name2, ?") +s.params[1] +s.params[2] +s.params[3] +s.params[4] +s.params[5] +s.params[6] +unprepare(s.stmt_id) + -- DML s = prepare("INSERT INTO test VALUES (?, ?, ?);") execute(s.stmt_id, {5, 6, '7'}) -- 2.15.1