Skip to content

Commit

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

Part of #6164
  • Loading branch information
ImeevMA committed Jul 14, 2021
1 parent 194380f commit 65dc36c
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/box/bind.c
Expand Up @@ -191,6 +191,9 @@ sql_bind_column(struct sql_stmt *stmt, const struct sql_bind *p,
case MP_BIN:
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);
default:
unreachable();
}
Expand Down
5 changes: 5 additions & 0 deletions src/box/bind.h
Expand Up @@ -40,6 +40,8 @@ extern "C" {
#include <stdlib.h>

#include "msgpuck.h"
#include "uuid/tt_uuid.h"
#include "mp_extension_types.h"

struct sql_stmt;

Expand All @@ -59,6 +61,8 @@ struct sql_bind {
uint32_t bytes;
/** MessagePack type of the value. */
enum mp_type type;
/** Subtype of MP_EXT type. */
enum mp_extension_type ext_type;
/** Bind value. */
union {
bool b;
Expand All @@ -67,6 +71,7 @@ struct sql_bind {
uint64_t u64;
/** For string or blob. */
const char *s;
struct tt_uuid uuid;
};
};

Expand Down
5 changes: 5 additions & 0 deletions src/box/lua/execute.c
Expand Up @@ -371,6 +371,10 @@ lua_sql_bind_decode(struct lua_State *L, struct sql_bind *bind, int idx, int i)
bind->s = mp_decode_bin(&field.sval.data, &bind->bytes);
break;
case MP_EXT:
if (field.ext_type == MP_UUID) {
bind->uuid = *field.uuidval;
break;
}
diag_set(ClientError, ER_SQL_BIND_TYPE, "USERDATA",
sql_bind_name(bind));
return -1;
Expand All @@ -386,6 +390,7 @@ lua_sql_bind_decode(struct lua_State *L, struct sql_bind *bind, int idx, int i)
unreachable();
}
bind->type = field.type;
bind->ext_type = field.ext_type;
lua_pop(L, lua_gettop(L) - idx);
return 0;
}
Expand Down
5 changes: 5 additions & 0 deletions src/box/sql/sqlInt.h
Expand Up @@ -326,6 +326,8 @@ struct sql_vfs {
#define SQL_LIMIT_LIKE_PATTERN_LENGTH 8
#define SQL_LIMIT_TRIGGER_DEPTH 9

struct tt_uuid;

enum sql_ret_code {
/** sql_step() has another row ready. */
SQL_ROW = 1,
Expand Down Expand Up @@ -634,6 +636,9 @@ int
sql_bind_zeroblob64(sql_stmt *, int,
sql_uint64);

int
sql_bind_uuid(struct sql_stmt *stmt, int i, const struct tt_uuid *uuid);

/**
* Return the number of wildcards that should be bound to.
*/
Expand Down
10 changes: 10 additions & 0 deletions src/box/sql/vdbeapi.c
Expand Up @@ -840,6 +840,16 @@ sql_bind_zeroblob64(sql_stmt * pStmt, int i, sql_uint64 n)
return sql_bind_zeroblob(pStmt, i, n);
}

int
sql_bind_uuid(struct sql_stmt *stmt, int i, const struct tt_uuid *uuid)
{
struct Vdbe *p = (struct Vdbe *)stmt;
if (vdbeUnbind(p, i) != 0 || sql_bind_type(p, i, "uuid") != 0)
return -1;
mem_set_uuid(&p->aVar[i - 1], uuid);
return 0;
}

int
sql_bind_parameter_count(const struct sql_stmt *stmt)
{
Expand Down
26 changes: 25 additions & 1 deletion test/sql-tap/gh-6164-uuid-follow-ups.test.lua
@@ -1,6 +1,6 @@
#!/usr/bin/env tarantool
local test = require("sqltester")
test:plan(1)
test:plan(4)

-- Make sure that function quote() can work with uuid.
test:do_execsql_test(
Expand All @@ -11,4 +11,28 @@ test:do_execsql_test(
'11111111-1111-1111-1111-111111111111'
})

-- Make sure that uuid value can be binded.
local uuid1 = require('uuid').fromstr('11111111-1111-1111-1111-111111111111')
local uuid2 = require('uuid').fromstr('11111111-2222-1111-1111-111111111111')
local uuid3 = require('uuid').fromstr('11111111-1111-3333-1111-111111111111')
test:do_test(
"gh-6164-2",
function()
return box.execute([[SELECT ?;]], {uuid1}).rows[1][1]
end,
uuid1)
test:do_test(
"gh-6164-3",
function()
return box.execute([[SELECT $2;]], {123, uuid2}).rows[1][1]
end,
uuid2)

test:do_test(
"gh-6164-4",
function()
return box.execute([[SELECT :two;]], {{[":two"] = uuid3}}).rows[1][1]
end,
uuid3)

test:finish_test()

0 comments on commit 65dc36c

Please sign in to comment.