Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
To: tarantool-patches@freelists.org
Cc: kyukhin@tarantool.org, imeevma@tarantool.org,
	Mergen Imeev <imeevma@gmail.com>
Subject: [tarantool-patches] [PATCH v10 3/4] sql: create box.execute()
Date: Mon,  1 Apr 2019 22:59:04 +0300	[thread overview]
Message-ID: <4b3ca18628a2ed3d0cbf70c4e0d0e6e182fa33ad.1554148476.git.v.shpilevoy@tarantool.org> (raw)
In-Reply-To: <cover.1554148476.git.v.shpilevoy@tarantool.org>
In-Reply-To: <cover.1554148476.git.v.shpilevoy@tarantool.org>

From: Mergen Imeev <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/CMakeLists.txt |   1 +
 src/box/execute.c      |   3 +-
 src/box/lua/execute.c  | 106 +++++++++++++++++++++++++++++++++++++++++
 src/box/lua/init.c     |   2 +
 test/box/misc.result   |   1 +
 5 files changed, 112 insertions(+), 1 deletion(-)
 create mode 100644 src/box/lua/execute.c

diff --git a/src/box/CMakeLists.txt b/src/box/CMakeLists.txt
index 67b7e9f50..8a965c886 100644
--- a/src/box/CMakeLists.txt
+++ b/src/box/CMakeLists.txt
@@ -140,6 +140,7 @@ add_library(box STATIC
     lua/net_box.c
     lua/xlog.c
     lua/sql.c
+    lua/execute.c
     ${bin_sources})
 
 target_link_libraries(box box_error tuple stat xrow xlog vclock crc32 scramble
diff --git a/src/box/execute.c b/src/box/execute.c
index 625bff0c2..af2182867 100644
--- a/src/box/execute.c
+++ b/src/box/execute.c
@@ -44,6 +44,7 @@
 #include "port.h"
 #include "tuple.h"
 #include "sql/vdbe.h"
+#include "box/lua/execute.h"
 
 const char *sql_info_key_strs[] = {
 	"row count",
@@ -104,7 +105,7 @@ port_sql_destroy(struct port *base)
 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,
 };
diff --git a/src/box/lua/execute.c b/src/box/lua/execute.c
new file mode 100644
index 000000000..ab1f7cd62
--- /dev/null
+++ b/src/box/lua/execute.c
@@ -0,0 +1,106 @@
+#include "execute.h"
+#include "lua/utils.h"
+#include "box/sql/sqlInt.h"
+#include "box/port.h"
+#include "box/execute.h"
+
+/**
+ * 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);
+	}
+}
+
+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");
+		}
+	}
+}
+
+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;
+}
+
+void
+box_lua_execute_init(struct lua_State *L)
+{
+	lua_getfield(L, LUA_GLOBALSINDEX, "box");
+	lua_pushstring(L, "execute");
+	lua_pushcfunction(L, lbox_execute);
+	lua_settable(L, -3);
+	lua_pop(L, 1);
+}
diff --git a/src/box/lua/init.c b/src/box/lua/init.c
index 744b2c895..534057d13 100644
--- a/src/box/lua/init.c
+++ b/src/box/lua/init.c
@@ -59,6 +59,7 @@
 #include "box/lua/console.h"
 #include "box/lua/tuple.h"
 #include "box/lua/sql.h"
+#include "box/lua/execute.h"
 
 extern char session_lua[],
 	tuple_lua[],
@@ -308,6 +309,7 @@ box_lua_init(struct lua_State *L)
 	box_lua_session_init(L);
 	box_lua_xlog_init(L);
 	box_lua_sql_init(L);
+	box_lua_execute_init(L);
 	luaopen_net_box(L);
 	lua_pop(L, 1);
 	tarantool_lua_console_init(L);
diff --git a/test/box/misc.result b/test/box/misc.result
index 5dda7526a..32e4e5cf4 100644
--- a/test/box/misc.result
+++ b/test/box/misc.result
@@ -63,6 +63,7 @@ t
   - commit
   - ctl
   - error
+  - execute
   - feedback
   - index
   - info
-- 
2.17.2 (Apple Git-113)

  parent reply	other threads:[~2019-04-01 19:59 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-01 19:59 [tarantool-patches] [PATCH v10 0/4] box.sql.execute removal Vladislav Shpilevoy
2019-04-01 19:59 ` [tarantool-patches] [PATCH v10 1/4] iproto: create port_sql Vladislav Shpilevoy
2019-04-02  8:01   ` [tarantool-patches] " Konstantin Osipov
2019-04-01 19:59 ` [tarantool-patches] [PATCH v10 2/4] sql: export sql_bind structure and API Vladislav Shpilevoy
2019-04-02  8:03   ` [tarantool-patches] " Konstantin Osipov
2019-04-01 19:59 ` Vladislav Shpilevoy [this message]
2019-04-02  8:13   ` [tarantool-patches] Re: [PATCH v10 3/4] sql: create box.execute() Konstantin Osipov
2019-04-02  8:28     ` Vladislav Shpilevoy
2019-04-02  9:22       ` Konstantin Osipov
2019-04-02  9:33         ` Vladislav Shpilevoy
2019-04-02  9:45           ` Vladislav Shpilevoy
2019-04-02  8:21   ` Konstantin Osipov
2019-04-02  8:34     ` Imeev Mergen
2019-04-02  8:55       ` Konstantin Osipov
2019-04-01 19:59 ` [tarantool-patches] [PATCH v10 4/4] sql: remove box.sql.execute() Vladislav Shpilevoy
2019-04-02  8:19   ` [tarantool-patches] " Konstantin Osipov
2019-04-01 20:02 ` [tarantool-patches] Re: [PATCH v10 0/4] box.sql.execute removal Vladislav Shpilevoy
2019-04-03  7:56 ` Kirill Yukhin

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=4b3ca18628a2ed3d0cbf70c4e0d0e6e182fa33ad.1554148476.git.v.shpilevoy@tarantool.org \
    --to=v.shpilevoy@tarantool.org \
    --cc=imeevma@gmail.com \
    --cc=imeevma@tarantool.org \
    --cc=kyukhin@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --subject='Re: [tarantool-patches] [PATCH v10 3/4] 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