* [Tarantool-patches] [PATCH] sql: fix off-by-one error while setting bind names
@ 2020-02-04 17:12 Nikita Pettik
2020-02-04 21:36 ` Vladislav Shpilevoy
0 siblings, 1 reply; 3+ messages in thread
From: Nikita Pettik @ 2020-02-04 17:12 UTC (permalink / raw)
To: tarantool-patches; +Cc: v.shpilevoy
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
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2020-02-06 17:30 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-04 17:12 [Tarantool-patches] [PATCH] sql: fix off-by-one error while setting bind names Nikita Pettik
2020-02-04 21:36 ` Vladislav Shpilevoy
2020-02-06 17:30 ` Nikita Pettik
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox