From a94986f27d26921a09ba66183082c374f6635b6e Mon Sep 17 00:00:00 2001 From: Mergen Imeev <imeevma@gmail.com> Date: Thu, 26 Aug 2021 15:52:13 +0300 Subject: [PATCH] sql: allow to bind DECIMAL values After this patch, DECIMAL values can be bound like any other supported by SQL values. Closes #4717 --- .../unreleased/gh-4717-binding-for-decimal.md | 3 +++ src/box/bind.c | 6 +++-- src/box/bind.h | 2 ++ src/box/lua/execute.c | 4 ++++ src/box/sql/mem.c | 2 +- src/box/sql/mem.h | 2 +- src/box/sql/sqlInt.h | 5 ++++ src/box/sql/vdbeapi.c | 10 ++++++++ test/sql-tap/decimal.test.lua | 23 ++++++++++++++++++- 9 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 changelogs/unreleased/gh-4717-binding-for-decimal.md diff --git a/changelogs/unreleased/gh-4717-binding-for-decimal.md b/changelogs/unreleased/gh-4717-binding-for-decimal.md new file mode 100644 index 0000000000..3612e287d8 --- /dev/null +++ b/changelogs/unreleased/gh-4717-binding-for-decimal.md @@ -0,0 +1,3 @@ +## feature/sql + +* Now DECIMAL values can be bound in SQL (gh-4717). diff --git a/src/box/bind.c b/src/box/bind.c index 734f651863..58fff0b987 100644 --- a/src/box/bind.c +++ b/src/box/bind.c @@ -192,8 +192,10 @@ sql_bind_column(struct sql_stmt *stmt, const struct sql_bind *p, return sql_bind_blob64(stmt, pos, (const void *) p->s, p->bytes, SQL_STATIC); case MP_EXT: - assert(p->ext_type == MP_UUID); - return sql_bind_uuid(stmt, pos, &p->uuid); + assert(p->ext_type == MP_UUID || p->ext_type == MP_DECIMAL); + if (p->ext_type == MP_UUID) + return sql_bind_uuid(stmt, pos, &p->uuid); + return sql_bind_dec(stmt, pos, &p->dec); default: unreachable(); } diff --git a/src/box/bind.h b/src/box/bind.h index 143f010ced..1ab8ea72d0 100644 --- a/src/box/bind.h +++ b/src/box/bind.h @@ -40,6 +40,7 @@ extern "C" { #include <stdlib.h> #include "msgpuck.h" +#include "decimal.h" #include "uuid/tt_uuid.h" #include "mp_extension_types.h" @@ -72,6 +73,7 @@ struct sql_bind { /** For string or blob. */ const char *s; struct tt_uuid uuid; + decimal_t dec; }; }; diff --git a/src/box/lua/execute.c b/src/box/lua/execute.c index 62ccaf3f14..777db82cb6 100644 --- a/src/box/lua/execute.c +++ b/src/box/lua/execute.c @@ -375,6 +375,10 @@ lua_sql_bind_decode(struct lua_State *L, struct sql_bind *bind, int idx, int i) bind->uuid = *field.uuidval; break; } + if (field.ext_type == MP_DECIMAL) { + bind->dec = *field.decval; + break; + } diag_set(ClientError, ER_SQL_BIND_TYPE, "USERDATA", sql_bind_name(bind)); return -1; diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index 4c40f15dc3..24de26548f 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -293,7 +293,7 @@ mem_set_double(struct Mem *mem, double value) } void -mem_set_dec(struct Mem *mem, decimal_t *d) +mem_set_dec(struct Mem *mem, const decimal_t *d) { mem_clear(mem); mem->u.d = *d; diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h index 543944b804..0da45b8af5 100644 --- a/src/box/sql/mem.h +++ b/src/box/sql/mem.h @@ -312,7 +312,7 @@ mem_set_uuid(struct Mem *mem, const struct tt_uuid *uuid); /** Clear MEM and set it to DECIMAL. */ void -mem_set_dec(struct Mem *mem, decimal_t *dec); +mem_set_dec(struct Mem *mem, const decimal_t *dec); /** Clear MEM and set it to STRING. The string belongs to another object. */ void diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h index d78076868d..2e250dc291 100644 --- a/src/box/sql/sqlInt.h +++ b/src/box/sql/sqlInt.h @@ -76,6 +76,8 @@ #include "box/txn.h" #include "trivia/util.h" +#include "decimal.h" + /* * These #defines should enable >2GB file support on POSIX if the * underlying operating system supports it. If the OS lacks @@ -639,6 +641,9 @@ sql_bind_zeroblob64(sql_stmt *, int, int sql_bind_uuid(struct sql_stmt *stmt, int i, const struct tt_uuid *uuid); +int +sql_bind_dec(struct sql_stmt *stmt, int i, const decimal_t *dec); + /** * Return the number of wildcards that should be bound to. */ diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c index 8031ee0dc1..77df0e4cc2 100644 --- a/src/box/sql/vdbeapi.c +++ b/src/box/sql/vdbeapi.c @@ -850,6 +850,16 @@ sql_bind_uuid(struct sql_stmt *stmt, int i, const struct tt_uuid *uuid) return 0; } +int +sql_bind_dec(struct sql_stmt *stmt, int i, const decimal_t *dec) +{ + struct Vdbe *p = (struct Vdbe *)stmt; + if (vdbeUnbind(p, i) != 0 || sql_bind_type(p, i, "decimal") != 0) + return -1; + mem_set_dec(&p->aVar[i - 1], dec); + return 0; +} + int sql_bind_parameter_count(const struct sql_stmt *stmt) { diff --git a/test/sql-tap/decimal.test.lua b/test/sql-tap/decimal.test.lua index 69288d696e..d422533d69 100755 --- a/test/sql-tap/decimal.test.lua +++ b/test/sql-tap/decimal.test.lua @@ -3,7 +3,7 @@ local build_path = os.getenv("BUILDDIR") package.cpath = build_path..'/test/sql-tap/?.so;'..build_path..'/test/sql-tap/?.dylib;'..package.cpath local test = require("sqltester") -test:plan(101) +test:plan(104) local dec = require("decimal") local dec1 = dec.new("111") @@ -938,6 +938,27 @@ test:do_catchsql_test( 1, "Inconsistent types: expected string or varbinary got decimal(111)" }) +-- Make sure that DECIMAL value can be bound. +test:do_test( + "dec-16-1", + function() + return box.execute([[SELECT ?;]], {dec1}).rows[1][1] + end, + dec1) +test:do_test( + "dec-16-2", + function() + return box.execute([[SELECT $2;]], {123, dec2}).rows[1][1] + end, + dec2) + +test:do_test( + "dec-16-3", + function() + return box.execute([[SELECT :two;]], {{[":two"] = dec3}}).rows[1][1] + end, + dec3) + test:execsql([[ DROP TRIGGER t; DROP VIEW v; -- GitLab