From: imeevma@tarantool.org To: v.shpilevoy@tarantool.org Cc: tarantool-patches@freelists.org Subject: [tarantool-patches] [PATCH v9 6/7] sql: create box.execute() Date: Fri, 22 Mar 2019 13:50:42 +0300 [thread overview] Message-ID: <a999e6251cc6a0dfff2792bd6f3bce6f0c57531f.1553251042.git.imeevma@gmail.com> (raw) In-Reply-To: <cover.1553251041.git.imeevma@gmail.com> This patch creates the method dump_lua() for port_sql and uses it in the new function box.execute(). The function box.execute() replaces box.sql.execute() in the next patch. Part of #3505 --- src/box/execute.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++- src/box/lua/init.c | 23 +++++++++++++++ test/box/misc.result | 1 + 3 files changed, 106 insertions(+), 1 deletion(-) diff --git a/src/box/execute.c b/src/box/execute.c index 460eebc..ae3f7a4 100644 --- a/src/box/execute.c +++ b/src/box/execute.c @@ -43,6 +43,7 @@ #include "port.h" #include "tuple.h" #include "sql/vdbe.h" +#include "lua/utils.h" const char *sql_type_strs[] = { NULL, @@ -145,6 +146,16 @@ static_assert(sizeof(struct port_sql) <= sizeof(struct port), static int port_sql_dump_msgpack(struct port *port, struct obuf *out); +/** + * Dump data from port to Lua stack. Data in port contains tuples, + * metadata, or information obtained from an executed SQL query. + * + * @param port Port that contains SQL response. + * @param L Lua stack. + */ +static void +port_sql_dump_lua(struct port *port, struct lua_State *L); + static void port_sql_destroy(struct port *base) { @@ -155,7 +166,7 @@ port_sql_destroy(struct port *base) static const struct port_vtab port_sql_vtab = { /* .dump_msgpack = */ port_sql_dump_msgpack, /* .dump_msgpack_16 = */ NULL, - /* .dump_lua = */ NULL, + /* .dump_lua = */ port_sql_dump_lua, /* .dump_plain = */ NULL, /* .destroy = */ port_sql_destroy, }; @@ -671,6 +682,76 @@ port_sql_dump_msgpack(struct port *port, struct obuf *out) } /** + * Serialize a description of the prepared statement. + * + * @param stmt Prepared statement. + * @param L Lua stack. + * @param column_count Statement's column count. + */ +static inline void +lua_sql_get_description(struct sql_stmt *stmt, struct lua_State *L, + int column_count) +{ + assert(column_count > 0); + lua_createtable(L, column_count, 0); + for (int i = 0; i < column_count; ++i) { + lua_createtable(L, 0, 2); + const char *name = sql_column_name(stmt, i); + const char *type = sql_column_datatype(stmt, i); + /* + * Can not fail, since all column names are + * preallocated during prepare phase and the + * column_name simply returns them. + */ + assert(name != NULL); + assert(type != NULL); + lua_pushstring(L, name); + lua_setfield(L, -2, "name"); + lua_pushstring(L, type); + lua_setfield(L, -2, "type"); + lua_rawseti(L, -2, i + 1); + } +} + +static void +port_sql_dump_lua(struct port *port, struct lua_State *L) +{ + assert(port->vtab == &port_sql_vtab); + struct sql *db = sql_get(); + struct sql_stmt *stmt = ((struct port_sql *)port)->stmt; + int column_count = sql_column_count(stmt); + if (column_count > 0) { + lua_createtable(L, 0, 2); + lua_sql_get_description(stmt, L, column_count); + lua_setfield(L, -2, "metadata"); + port_tuple_vtab.dump_lua(port, L); + lua_setfield(L, -2, "rows"); + } else { + assert(((struct port_tuple *)port)->size == 0); + struct stailq *autoinc_id_list = + vdbe_autoinc_id_list((struct Vdbe *)stmt); + lua_createtable(L, 0, stailq_empty(autoinc_id_list) ? 1 : 2); + + luaL_pushuint64(L, db->nChange); + lua_setfield(L, -2, "rowcount"); + + if (!stailq_empty(autoinc_id_list)) { + lua_newtable(L); + int i = 1; + struct autoinc_id_entry *id_entry; + stailq_foreach_entry(id_entry, autoinc_id_list, link) { + if (id_entry->id >= 0) + luaL_pushuint64(L, id_entry->id); + else + luaL_pushint64(L, id_entry->id); + lua_rawseti(L, -2, i++); + } + lua_setfield(L, -2, "autoincrement_ids"); + } + } +} + +/** * Execute prepared SQL statement. * * This function uses region to allocate memory for temporary diff --git a/src/box/lua/init.c b/src/box/lua/init.c index 744b2c8..eb2ca31 100644 --- a/src/box/lua/init.c +++ b/src/box/lua/init.c @@ -40,6 +40,7 @@ #include "box/box.h" #include "box/txn.h" #include "box/vclock.h" +#include "box/execute.h" #include "box/lua/error.h" #include "box/lua/tuple.h" @@ -266,12 +267,34 @@ lbox_backup_stop(struct lua_State *L) return 0; } +static int +lbox_execute(struct lua_State *L) +{ + struct sql_bind *bind = NULL; + int bind_count = 0; + size_t length; + struct port port; + int top = lua_gettop(L); + + if (top != 1 || ! lua_isstring(L, 1)) + return luaL_error(L, "Usage: box.execute(sqlstring)"); + + const char *sql = lua_tolstring(L, 1, &length); + if (sql_prepare_and_execute(sql, length, bind, bind_count, &port, + &fiber()->gc) != 0) + return luaT_error(L); + port_dump_lua(&port, L); + port_destroy(&port); + return 1; +} + static const struct luaL_Reg boxlib[] = { {"commit", lbox_commit}, {"rollback", lbox_rollback}, {"on_commit", lbox_on_commit}, {"on_rollback", lbox_on_rollback}, {"snapshot", lbox_snapshot}, + {"execute", lbox_execute}, {NULL, NULL} }; diff --git a/test/box/misc.result b/test/box/misc.result index 4f1116e..714d5de 100644 --- a/test/box/misc.result +++ b/test/box/misc.result @@ -63,6 +63,7 @@ t - commit - ctl - error + - execute - feedback - index - info -- 2.7.4
next prev parent reply other threads:[~2019-03-22 10:50 UTC|newest] Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top 2019-03-22 10:50 [tarantool-patches] [PATCH v9 0/7] sql: remove box.sql.execute imeevma 2019-03-22 10:50 ` [tarantool-patches] [PATCH v9 1/7] sql: add column name to SQL change counter imeevma 2019-03-22 15:42 ` [tarantool-patches] " Konstantin Osipov 2019-03-25 19:34 ` Mergen Imeev 2019-03-29 12:00 ` Kirill Yukhin 2019-03-22 10:50 ` [tarantool-patches] [PATCH v9 2/7] sql: fix error code for SQL errors in execute.c imeevma 2019-03-22 15:45 ` [tarantool-patches] " Konstantin Osipov 2019-03-26 21:48 ` Vladislav Shpilevoy 2019-03-27 11:43 ` Konstantin Osipov 2019-03-28 17:46 ` Mergen Imeev 2019-03-29 12:01 ` Kirill Yukhin 2019-03-22 10:50 ` [tarantool-patches] [PATCH v9 3/7] sql: remove box.sql.debug() imeevma 2019-03-22 15:46 ` [tarantool-patches] " Konstantin Osipov 2019-03-25 19:39 ` Mergen Imeev 2019-03-26 21:48 ` Vladislav Shpilevoy 2019-03-28 17:48 ` Mergen Imeev 2019-03-28 18:01 ` Vladislav Shpilevoy 2019-03-29 12:02 ` Kirill Yukhin 2019-03-22 10:50 ` [tarantool-patches] [PATCH v9 4/7] lua: remove exceptions from function luaL_tofield() imeevma 2019-03-22 15:53 ` [tarantool-patches] " Konstantin Osipov 2019-03-29 19:26 ` Vladislav Shpilevoy 2019-03-26 21:48 ` Vladislav Shpilevoy 2019-03-28 17:54 ` Mergen Imeev 2019-03-28 18:40 ` Vladislav Shpilevoy 2019-03-28 19:56 ` Mergen Imeev 2019-03-28 21:41 ` Mergen Imeev 2019-03-29 21:06 ` Vladislav Shpilevoy 2019-03-22 10:50 ` [tarantool-patches] [PATCH v9 5/7] iproto: create port_sql imeevma 2019-03-22 15:55 ` [tarantool-patches] " Konstantin Osipov 2019-03-22 10:50 ` imeevma [this message] 2019-03-22 15:57 ` [tarantool-patches] Re: [PATCH v9 6/7] sql: create box.execute() Konstantin Osipov 2019-03-22 10:50 ` [tarantool-patches] [PATCH v9 7/7] sql: remove box.sql.execute() imeevma 2019-03-26 21:48 ` [tarantool-patches] " Vladislav Shpilevoy 2019-03-28 20:13 ` Mergen Imeev 2019-03-29 21:06 ` Vladislav Shpilevoy 2019-03-29 21:07 ` [tarantool-patches] Re: [PATCH v9 0/7] sql: remove box.sql.execute Vladislav Shpilevoy
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=a999e6251cc6a0dfff2792bd6f3bce6f0c57531f.1553251042.git.imeevma@gmail.com \ --to=imeevma@tarantool.org \ --cc=tarantool-patches@freelists.org \ --cc=v.shpilevoy@tarantool.org \ --subject='Re: [tarantool-patches] [PATCH v9 6/7] sql: create box.execute()' \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox