[Tarantool-patches] [PATCH 5/6] sql: extend result set with autoincrement
Nikita Pettik
korablev at tarantool.org
Wed Nov 27 15:15:45 MSK 2019
If result set contains column which features attached sequence
(AUTOINCREMENT in terms of SQL) then meta-information will contain
corresponding field ('autoicrement' : boolean) in response.
Part of #4407
---
src/box/execute.c | 18 +++++++++++++++---
src/box/iproto_constants.h | 3 ++-
src/box/lua/execute.c | 5 +++++
src/box/lua/net_box.c | 10 +++++++---
src/box/sql/select.c | 6 ++++++
src/box/sql/sqlInt.h | 3 +++
src/box/sql/vdbe.h | 3 +++
src/box/sql/vdbeInt.h | 2 ++
src/box/sql/vdbeapi.c | 8 ++++++++
src/box/sql/vdbeaux.c | 7 +++++++
10 files changed, 58 insertions(+), 7 deletions(-)
diff --git a/src/box/execute.c b/src/box/execute.c
index 98812ae1e..8305f6f3b 100644
--- a/src/box/execute.c
+++ b/src/box/execute.c
@@ -270,7 +270,7 @@ error:
static size_t
metadata_map_sizeof(const char *name, const char *type, const char *coll,
- int nullable)
+ int nullable, bool is_autoincrement)
{
uint32_t members_count = 2;
size_t map_size = 0;
@@ -284,6 +284,11 @@ metadata_map_sizeof(const char *name, const char *type, const char *coll,
map_size += mp_sizeof_uint(IPROTO_FIELD_NULLABLE);
map_size += mp_sizeof_bool(nullable);
}
+ if (is_autoincrement) {
+ members_count++;
+ map_size += mp_sizeof_uint(IPROTO_FIELD_AUTOINCREMENT);
+ map_size += mp_sizeof_bool(true);
+ }
map_size += mp_sizeof_uint(IPROTO_FIELD_NAME);
map_size += mp_sizeof_uint(IPROTO_FIELD_TYPE);
map_size += mp_sizeof_str(strlen(name));
@@ -319,6 +324,7 @@ sql_get_metadata(struct sql_stmt *stmt, struct obuf *out, int column_count)
const char *name = sql_column_name(stmt, i);
const char *type = sql_column_datatype(stmt, i);
int nullable = sql_column_is_nullable(stmt, i);
+ bool is_autoincrement = sql_column_is_autoincrement(stmt, i);
/*
* Can not fail, since all column names and types
* are preallocated during prepare phase and the
@@ -326,13 +332,15 @@ sql_get_metadata(struct sql_stmt *stmt, struct obuf *out, int column_count)
*/
assert(name != NULL);
assert(type != NULL);
- size = metadata_map_sizeof(name, type, coll, nullable);
+ size = metadata_map_sizeof(name, type, coll, nullable,
+ is_autoincrement);
char *pos = (char *) obuf_alloc(out, size);
if (pos == NULL) {
diag_set(OutOfMemory, size, "obuf_alloc", "pos");
return -1;
}
- uint32_t map_sz = 2 + (coll != NULL) + (nullable != -1);
+ uint32_t map_sz = 2 + (coll != NULL) + (nullable != -1) +
+ is_autoincrement;
pos = mp_encode_map(pos, map_sz);
pos = mp_encode_uint(pos, IPROTO_FIELD_NAME);
pos = mp_encode_str(pos, name, strlen(name));
@@ -346,6 +354,10 @@ sql_get_metadata(struct sql_stmt *stmt, struct obuf *out, int column_count)
pos = mp_encode_uint(pos, IPROTO_FIELD_NULLABLE);
pos = mp_encode_bool(pos, nullable);
}
+ if (is_autoincrement) {
+ pos = mp_encode_uint(pos, IPROTO_FIELD_AUTOINCREMENT);
+ pos = mp_encode_bool(pos, true);
+ }
}
return 0;
}
diff --git a/src/box/iproto_constants.h b/src/box/iproto_constants.h
index 030c25531..4d43583b0 100644
--- a/src/box/iproto_constants.h
+++ b/src/box/iproto_constants.h
@@ -132,7 +132,8 @@ enum iproto_metadata_key {
IPROTO_FIELD_NAME = 0,
IPROTO_FIELD_TYPE = 1,
IPROTO_FIELD_COLL = 2,
- IPROTO_FIELD_NULLABLE = 3
+ IPROTO_FIELD_NULLABLE = 3,
+ IPROTO_FIELD_AUTOINCREMENT = 4,
};
enum iproto_ballot_key {
diff --git a/src/box/lua/execute.c b/src/box/lua/execute.c
index 3d7ca710c..d7dd9432f 100644
--- a/src/box/lua/execute.c
+++ b/src/box/lua/execute.c
@@ -25,6 +25,7 @@ lua_sql_get_metadata(struct sql_stmt *stmt, struct lua_State *L,
const char *name = sql_column_name(stmt, i);
const char *type = sql_column_datatype(stmt, i);
int nullable = sql_column_is_nullable(stmt, i);
+ bool is_autoincrement = sql_column_is_autoincrement(stmt, i);
/*
* Can not fail, since all column names are
* preallocated during prepare phase and the
@@ -44,6 +45,10 @@ lua_sql_get_metadata(struct sql_stmt *stmt, struct lua_State *L,
lua_pushboolean(L, nullable);
lua_setfield(L, -2, "is_nullable");
}
+ if (is_autoincrement) {
+ lua_pushboolean(L, true);
+ lua_setfield(L, -2, "autoincrement");
+ }
lua_rawseti(L, -2, i + 1);
}
}
diff --git a/src/box/lua/net_box.c b/src/box/lua/net_box.c
index 3e93cbc75..3be644785 100644
--- a/src/box/lua/net_box.c
+++ b/src/box/lua/net_box.c
@@ -651,11 +651,15 @@ decode_metadata_optional(struct lua_State *L, const char **data,
const char *coll = mp_decode_str(data, &len);
lua_pushlstring(L, coll, len);
lua_setfield(L, -2, "collation");
- } else {
- assert(key == IPROTO_FIELD_NULLABLE);
+ } else if (key == IPROTO_FIELD_NULLABLE) {
bool is_nullable = mp_decode_bool(data);
lua_pushboolean(L, is_nullable);
lua_setfield(L, -2, "is_nullable");
+ } else {
+ assert(key == IPROTO_FIELD_AUTOINCREMENT);
+ bool autoincrement = mp_decode_bool(data);
+ lua_pushboolean(L, autoincrement);
+ lua_setfield(L, -2, "autoincrement");
}
}
}
@@ -672,7 +676,7 @@ netbox_decode_metadata(struct lua_State *L, const char **data)
lua_createtable(L, count, 0);
for (uint32_t i = 0; i < count; ++i) {
uint32_t map_size = mp_decode_map(data);
- assert(map_size >= 2 && map_size <= 4);
+ assert(map_size >= 2 && map_size <= 5);
(void) map_size;
uint32_t key = mp_decode_uint(data);
assert(key == IPROTO_FIELD_NAME);
diff --git a/src/box/sql/select.c b/src/box/sql/select.c
index b772bcead..f40178194 100644
--- a/src/box/sql/select.c
+++ b/src/box/sql/select.c
@@ -1841,6 +1841,12 @@ generate_column_metadata(struct Parse *pParse, struct SrcList *pTabList,
if (p->op == TK_COLUMN)
vdbe_set_metadata_col_nullability(v, i,
is_nullable);
+ if (pTabList->a[j].space->sequence != NULL) {
+ int afno =
+ pTabList->a[j].space->sequence_fieldno;
+ if (afno == iCol)
+ vdbe_set_metadata_col_autoincrement(v, i);
+ }
} else {
const char *z = pEList->a[i].zSpan;
if (z == NULL)
diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h
index 89920b3d1..590791648 100644
--- a/src/box/sql/sqlInt.h
+++ b/src/box/sql/sqlInt.h
@@ -582,6 +582,9 @@ sql_column_coll(sql_stmt *stmt, int n);
int
sql_column_is_nullable(sql_stmt *stmt, int n);
+bool
+sql_column_is_autoincrement(sql_stmt *stmt, int n);
+
int
sql_initialize(void);
diff --git a/src/box/sql/vdbe.h b/src/box/sql/vdbe.h
index 0f315b660..4e1a67416 100644
--- a/src/box/sql/vdbe.h
+++ b/src/box/sql/vdbe.h
@@ -260,6 +260,9 @@ vdbe_set_metadata_col_collation(struct Vdbe *p, int idx, const char *coll,
void
vdbe_set_metadata_col_nullability(struct Vdbe *p, int idx, int nullable);
+void
+vdbe_set_metadata_col_autoincrement(struct Vdbe *p, int idx);
+
void sqlVdbeCountChanges(Vdbe *);
sql *sqlVdbeDb(Vdbe *);
void sqlVdbeSetSql(Vdbe *, const char *z, int n, int);
diff --git a/src/box/sql/vdbeInt.h b/src/box/sql/vdbeInt.h
index 0e54e42a5..63afb8777 100644
--- a/src/box/sql/vdbeInt.h
+++ b/src/box/sql/vdbeInt.h
@@ -355,6 +355,8 @@ struct sql_column_metadata {
* columns: all other expressions are nullable by default.
*/
int8_t nullable : 2;
+ /** True if column features autoincrement property. */
+ int8_t actoincrement : 1;
};
/*
diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c
index ea8c7c438..ece803262 100644
--- a/src/box/sql/vdbeapi.c
+++ b/src/box/sql/vdbeapi.c
@@ -761,6 +761,14 @@ sql_column_is_nullable(sql_stmt *stmt, int n)
return p->metadata[n].nullable;
}
+bool
+sql_column_is_autoincrement(sql_stmt *stmt, int n)
+{
+ struct Vdbe *p = (struct Vdbe *) stmt;
+ assert(n < sql_column_count(stmt) && n >= 0);
+ return p->metadata[n].actoincrement;
+}
+
/******************************* sql_bind_ **************************
*
* Routines used to attach values to wildcards in a compiled SQL statement.
diff --git a/src/box/sql/vdbeaux.c b/src/box/sql/vdbeaux.c
index 6c3523ba4..34a0c1267 100644
--- a/src/box/sql/vdbeaux.c
+++ b/src/box/sql/vdbeaux.c
@@ -1911,6 +1911,13 @@ vdbe_set_metadata_col_nullability(struct Vdbe *p, int idx, int nullable)
p->metadata[idx].nullable = nullable;
}
+void
+vdbe_set_metadata_col_autoincrement(struct Vdbe *p, int idx)
+{
+ assert(idx < p->nResColumn);
+ p->metadata[idx].actoincrement = 1;
+}
+
/*
* This routine checks that the sql.nVdbeActive count variable
* matches the number of vdbe's in the list sql.pVdbe that are
--
2.15.1
More information about the Tarantool-patches
mailing list