[Tarantool-patches] [PATCH v2 5/6] sql: extend result set with autoincrement
Nikita Pettik
korablev at tarantool.org
Wed Dec 11 16:44:57 MSK 2019
If result set contains column which features attached sequence
(AUTOINCREMENT in terms of SQL) then meta-information will contain
corresponding field ('is_autoicrement' : boolean) in response.
Part of #4407
---
src/box/execute.c | 18 +++++++++++++++---
src/box/iproto_constants.h | 1 +
src/box/lua/execute.c | 8 +++++++-
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 +++++++
test/sql/full_metadata.result | 4 +++-
test/sql/full_metadata.test.lua | 2 +-
12 files changed, 63 insertions(+), 9 deletions(-)
diff --git a/src/box/execute.c b/src/box/execute.c
index 2e43cdb22..c853991a0 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_IS_NULLABLE);
map_size += mp_sizeof_bool(nullable);
}
+ if (is_autoincrement) {
+ members_count++;
+ map_size += mp_sizeof_uint(IPROTO_FIELD_IS_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_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_IS_NULLABLE);
pos = mp_encode_bool(pos, nullable);
}
+ if (is_autoincrement) {
+ pos = mp_encode_uint(pos, IPROTO_FIELD_IS_AUTOINCREMENT);
+ pos = mp_encode_bool(pos, true);
+ }
}
return 0;
}
diff --git a/src/box/iproto_constants.h b/src/box/iproto_constants.h
index 53d014b60..30d1af4cb 100644
--- a/src/box/iproto_constants.h
+++ b/src/box/iproto_constants.h
@@ -133,6 +133,7 @@ enum iproto_metadata_key {
IPROTO_FIELD_TYPE = 1,
IPROTO_FIELD_COLL = 2,
IPROTO_FIELD_IS_NULLABLE = 3,
+ IPROTO_FIELD_IS_AUTOINCREMENT = 4,
};
enum iproto_ballot_key {
diff --git a/src/box/lua/execute.c b/src/box/lua/execute.c
index debe94cb9..e8e3e2a9f 100644
--- a/src/box/lua/execute.c
+++ b/src/box/lua/execute.c
@@ -24,7 +24,9 @@ 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_nullable(stmt, i);
- size_t table_sz = 2 + (coll != NULL) + (nullable != -1);
+ bool is_autoincrement = sql_column_is_autoincrement(stmt, i);
+ size_t table_sz = 2 + (coll != NULL) + (nullable != -1) +
+ is_autoincrement;
lua_createtable(L, 0, table_sz);
/*
* Can not fail, since all column names are
@@ -45,6 +47,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, "is_autoincrement");
+ }
lua_rawseti(L, -2, i + 1);
}
}
diff --git a/src/box/lua/net_box.c b/src/box/lua/net_box.c
index 644b373c9..88ef4ff78 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_IS_NULLABLE);
+ } else if (key == IPROTO_FIELD_IS_NULLABLE) {
bool is_nullable = mp_decode_bool(data);
lua_pushboolean(L, is_nullable);
lua_setfield(L, -2, "is_nullable");
+ } else {
+ assert(key == IPROTO_FIELD_IS_AUTOINCREMENT);
+ bool autoincrement = mp_decode_bool(data);
+ lua_pushboolean(L, autoincrement);
+ lua_setfield(L, -2, "is_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);
uint32_t key = mp_decode_uint(data);
assert(key == IPROTO_FIELD_NAME);
(void) key;
diff --git a/src/box/sql/select.c b/src/box/sql/select.c
index 73ce95eba..d92da4d8e 100644
--- a/src/box/sql/select.c
+++ b/src/box/sql/select.c
@@ -1845,6 +1845,12 @@ generate_column_metadata(struct Parse *pParse, struct SrcList *pTabList,
space_def->fields[iCol].is_nullable;
vdbe_metadata_set_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_metadata_set_col_autoincrement(v, i);
+ }
}
} else {
const char *z = NULL;
diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h
index 3698a5942..e248bc673 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_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 384d7dc0a..da9a311b8 100644
--- a/src/box/sql/vdbe.h
+++ b/src/box/sql/vdbe.h
@@ -260,6 +260,9 @@ vdbe_metadata_set_col_collation(struct Vdbe *p, int idx, const char *coll,
void
vdbe_metadata_set_col_nullability(struct Vdbe *p, int idx, int nullable);
+void
+vdbe_metadata_set_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 92a50dd7b..d79635b14 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. */
+ bool is_actoincrement;
};
/*
diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c
index 30f173984..5b423c9df 100644
--- a/src/box/sql/vdbeapi.c
+++ b/src/box/sql/vdbeapi.c
@@ -761,6 +761,14 @@ sql_column_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].is_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 2de41a70a..e3672097c 100644
--- a/src/box/sql/vdbeaux.c
+++ b/src/box/sql/vdbeaux.c
@@ -1911,6 +1911,13 @@ vdbe_metadata_set_col_nullability(struct Vdbe *p, int idx, int nullable)
p->metadata[idx].nullable = nullable;
}
+void
+vdbe_metadata_set_col_autoincrement(struct Vdbe *p, int idx)
+{
+ assert(idx < p->nResColumn);
+ p->metadata[idx].is_actoincrement = 1;
+}
+
/*
* This routine checks that the sql.nVdbeActive count variable
* matches the number of vdbe's in the list sql.pVdbe that are
diff --git a/test/sql/full_metadata.result b/test/sql/full_metadata.result
index 20ed29e1c..7c2982682 100644
--- a/test/sql/full_metadata.result
+++ b/test/sql/full_metadata.result
@@ -81,12 +81,13 @@ execute("SELECT c COLLATE \"unicode\" FROM t;")
| - ['aSd']
| ...
--- Make sure that nullability is presented.
+-- Make sure that nullability/autoincrement are presented.
--
execute("SELECT id, a, c FROM t;")
| ---
| - metadata:
| - type: integer
+ | is_autoincrement: true
| name: ID
| is_nullable: false
| - type: integer
@@ -103,6 +104,7 @@ execute("SELECT * FROM t;")
| ---
| - metadata:
| - type: integer
+ | is_autoincrement: true
| name: ID
| is_nullable: false
| - type: integer
diff --git a/test/sql/full_metadata.test.lua b/test/sql/full_metadata.test.lua
index 0ff5fefe6..e6bfa1eaf 100644
--- a/test/sql/full_metadata.test.lua
+++ b/test/sql/full_metadata.test.lua
@@ -30,7 +30,7 @@ execute("SELECT 'aSd' COLLATE \"unicode_ci\";")
execute("SELECT c FROM t;")
execute("SELECT c COLLATE \"unicode\" FROM t;")
--- Make sure that nullability is presented.
+-- Make sure that nullability/autoincrement are presented.
--
execute("SELECT id, a, c FROM t;")
execute("SELECT * FROM t;")
--
2.15.1
More information about the Tarantool-patches
mailing list