From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id B8A7C20D13 for ; Sat, 22 Dec 2018 06:32:02 -0500 (EST) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id K1S82sQWrGRK for ; Sat, 22 Dec 2018 06:32:02 -0500 (EST) Received: from smtpng1.m.smailru.net (smtpng1.m.smailru.net [94.100.181.251]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id 7A7C120C53 for ; Sat, 22 Dec 2018 06:32:02 -0500 (EST) From: imeevma@tarantool.org Subject: [tarantool-patches] [PATCH v5 5/5] sql: check new box.sql.execute() Date: Sat, 22 Dec 2018 14:32:00 +0300 Message-Id: In-Reply-To: References: Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-help: List-unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-subscribe: List-owner: List-post: List-archive: To: v.shpilevoy@tarantool.org, tarantool-patches@freelists.org This commit checks that new implementation of box.sql.execute() is able to pass all tests. This is temporary commit and should be dropped later. Even though this commit is temporary it shows that this patch-set should be pushed after patch for issue #3832. Needed for #3505 --- src/box/execute.c | 14 +++++++++----- src/box/execute.h | 2 +- src/box/iproto.cc | 2 +- src/box/lua/schema.lua | 23 +++++++++++++++++++++++ src/box/lua/sql.c | 12 ++++++++---- 5 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/box/execute.c b/src/box/execute.c index f538441..8a70bc4 100644 --- a/src/box/execute.c +++ b/src/box/execute.c @@ -678,6 +678,8 @@ sql_get_description(struct sqlite3_stmt *stmt, struct obuf *out, * column_name simply returns them. */ assert(name != NULL); + if (type == NULL) + type = "UNKNOWN"; size += mp_sizeof_str(strlen(name)); size += mp_sizeof_str(strlen(type)); char *pos = (char *) obuf_alloc(out, size); @@ -696,7 +698,7 @@ sql_get_description(struct sqlite3_stmt *stmt, struct obuf *out, static inline int sql_execute(sqlite3 *db, struct sqlite3_stmt *stmt, struct port *port, - struct region *region) + struct region *region, int error_id) { int rc, column_count = sqlite3_column_count(stmt); if (column_count > 0) { @@ -713,7 +715,7 @@ sql_execute(sqlite3 *db, struct sqlite3_stmt *stmt, struct port *port, assert(rc != SQLITE_ROW && rc != SQLITE_OK); } if (rc != SQLITE_DONE) { - diag_set(ClientError, ER_SQL_EXECUTE, sqlite3_errmsg(db)); + diag_set(ClientError, error_id, sqlite3_errmsg(db)); return -1; } return 0; @@ -722,7 +724,7 @@ sql_execute(sqlite3 *db, struct sqlite3_stmt *stmt, struct port *port, int sql_prepare_and_execute(const char *sql, int len, const struct sql_bind *bind, uint32_t bind_count, struct port *port, - struct region *region) + struct region *region, int error_id) { struct sqlite3_stmt *stmt; sqlite3 *db = sql_get(); @@ -731,13 +733,13 @@ sql_prepare_and_execute(const char *sql, int len, const struct sql_bind *bind, return -1; } if (sqlite3_prepare_v2(db, sql, len, &stmt, NULL) != SQLITE_OK) { - diag_set(ClientError, ER_SQL_EXECUTE, sqlite3_errmsg(db)); + diag_set(ClientError, error_id, sqlite3_errmsg(db)); return -1; } assert(stmt != NULL); port_tuple_create(port); if (sql_bind(stmt, bind, bind_count) == 0 && - sql_execute(db, stmt, port, region) == 0) { + sql_execute(db, stmt, port, region, error_id) == 0) { port_tuple_to_port_sql(port, stmt); return 0; } @@ -888,6 +890,8 @@ lua_sql_get_description(struct sqlite3_stmt *stmt, struct lua_State *L, * column_name simply returns them. */ assert(name != NULL); + if (type == NULL) + type = "UNKNOWN"; lua_pushlstring(L, name, strlen(name)); lua_setfield(L, -2, "name"); lua_pushlstring(L, type, strlen(type)); diff --git a/src/box/execute.h b/src/box/execute.h index bc2d6a5..81f96bf 100644 --- a/src/box/execute.h +++ b/src/box/execute.h @@ -125,7 +125,7 @@ lua_sql_bind_list_decode(struct lua_State *L, struct sql_bind **out_bind, int sql_prepare_and_execute(const char *sql, int len, const struct sql_bind *bind, uint32_t bind_count, struct port *port, - struct region *region); + struct region *region, int error_id); #if defined(__cplusplus) } /* extern "C" { */ diff --git a/src/box/iproto.cc b/src/box/iproto.cc index ee704e0..fe890e7 100644 --- a/src/box/iproto.cc +++ b/src/box/iproto.cc @@ -1631,7 +1631,7 @@ tx_process_sql(struct cmsg *m) sql = msg->sql.sql_text; sql = mp_decode_str(&sql, &len); if (sql_prepare_and_execute(sql, len, bind, bind_count, &port, - &fiber()->gc) != 0) + &fiber()->gc, ER_SQL_EXECUTE) != 0) goto error; /* * Take an obuf only after execute(). Else the buffer can diff --git a/src/box/lua/schema.lua b/src/box/lua/schema.lua index 8a804f0..02ec2fd 100644 --- a/src/box/lua/schema.lua +++ b/src/box/lua/schema.lua @@ -2456,3 +2456,26 @@ box.feedback.save = function(file_name) end box.NULL = msgpack.NULL + +box.sql.execute = function(sql) + local result = box.sql.new_execute(sql) + if result == nil then return end + local ret = nil + if result.rows ~= nil then + ret = {} + for key, row in pairs(result.rows) do + if type(row) == 'cdata' then + table.insert(ret, row:totable()) + end + end + end + if result.metadata ~= nil then + if ret == nil then ret = {} end + ret[0] = {} + for key, row in pairs(result.metadata) do + table.insert(ret[0], row['name']) + end + setmetatable(ret, {__serialize = 'sequence'}) + end + if ret ~= nil then return ret end +end diff --git a/src/box/lua/sql.c b/src/box/lua/sql.c index 354321f..2a2e566 100644 --- a/src/box/lua/sql.c +++ b/src/box/lua/sql.c @@ -129,13 +129,17 @@ lbox_execute(struct lua_State *L) if (lua_gettop(L) == 2 && (bind_count = lua_sql_bind_list_decode(L, &bind, 2)) < 0) - return luaT_error(L); + goto sqlerror; if (sql_prepare_and_execute(sql, length, bind, bind_count, &port, - &fiber()->gc) != 0) - return luaT_error(L); + &fiber()->gc, ER_SYSTEM) != 0) + goto sqlerror; port_dump_lua(&port, L); return 1; + +sqlerror: + lua_pushstring(L, sqlite3_errmsg(db)); + return lua_error(L); } static int @@ -151,7 +155,7 @@ void box_lua_sqlite_init(struct lua_State *L) { static const struct luaL_Reg module_funcs [] = { - {"execute", lua_sql_execute}, + {"old_execute", lua_sql_execute}, {"new_execute", lbox_execute}, {"debug", lua_sql_debug}, {NULL, NULL} -- 2.7.4