[Tarantool-patches] [PATCH] sql: fix off-by-one error while setting bind names

Nikita Pettik korablev at tarantool.org
Tue Feb 4 20:12:14 MSK 2020


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



More information about the Tarantool-patches mailing list