Skip to content

Commit

Permalink
sql: allow to bind DECIMAL values
Browse files Browse the repository at this point in the history
After this patch, DECIMAL values can be bound like any other supported
by SQL values.

Closes #4717
  • Loading branch information
ImeevMA authored and kyukhin committed Sep 1, 2021
1 parent 907f59e commit a94986f
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 5 deletions.
3 changes: 3 additions & 0 deletions changelogs/unreleased/gh-4717-binding-for-decimal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## feature/sql

* Now DECIMAL values can be bound in SQL (gh-4717).
6 changes: 4 additions & 2 deletions src/box/bind.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 2 additions & 0 deletions src/box/bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -72,6 +73,7 @@ struct sql_bind {
/** For string or blob. */
const char *s;
struct tt_uuid uuid;
decimal_t dec;
};
};

Expand Down
4 changes: 4 additions & 0 deletions src/box/lua/execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/box/sql/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/box/sql/mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/box/sql/sqlInt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*/
Expand Down
10 changes: 10 additions & 0 deletions src/box/sql/vdbeapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
23 changes: 22 additions & 1 deletion test/sql-tap/decimal.test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit a94986f

Please sign in to comment.