[tarantool-patches] [PATCH 6/8] refactoring: use sql_prepare() and sql_execute() in tx_process_sql()

Nikita Pettik korablev at tarantool.org
Tue Aug 27 16:34:27 MSK 2019


In case of dry-run execution we don't need to substitute binding values
or execute statement. So now we split sql_prepare_and_execute() into
independent steps, so that we can omit some of them depending on
execution mode.

Needed for #3292
---
 src/box/execute.c | 17 +----------------
 src/box/execute.h | 17 +++++++++++++++++
 src/box/iproto.cc | 15 +++++++++++++--
 3 files changed, 31 insertions(+), 18 deletions(-)

diff --git a/src/box/execute.c b/src/box/execute.c
index 078753342..a8a2e516b 100644
--- a/src/box/execute.c
+++ b/src/box/execute.c
@@ -398,22 +398,7 @@ port_sql_dump_msgpack(struct port *port, struct obuf *out)
 	return 0;
 }
 
-/**
- * Execute prepared SQL statement.
- *
- * This function uses region to allocate memory for temporary
- * objects. After this function, region will be in the same state
- * in which it was before this function.
- *
- * @param db SQL handle.
- * @param stmt Prepared statement.
- * @param port Port to store SQL response.
- * @param region Region to allocate temporary objects.
- *
- * @retval  0 Success.
- * @retval -1 Error.
- */
-static inline int
+int
 sql_execute(struct sql_stmt *stmt, struct port *port, struct region *region)
 {
 	int rc, column_count = sql_column_count(stmt);
diff --git a/src/box/execute.h b/src/box/execute.h
index 1b4e14246..23366d65c 100644
--- a/src/box/execute.h
+++ b/src/box/execute.h
@@ -81,6 +81,23 @@ int
 sql_prepare(const char *sql, int length, struct sql_stmt **stmt,
 	    const char **sql_tail);
 
+/**
+ * Execute prepared SQL statement.
+ *
+ * This function uses region to allocate memory for temporary
+ * objects. After this function, region will be in the same state
+ * in which it was before this function.
+ *
+ * @param stmt Prepared statement.
+ * @param port Port to store SQL response.
+ * @param region Region to allocate temporary objects.
+ *
+ * @retval  0 Success.
+ * @retval -1 Error.
+ */
+int
+sql_execute(struct sql_stmt *stmt, struct port *port, struct region *region);
+
 #if defined(__cplusplus)
 } /* extern "C" { */
 #endif
diff --git a/src/box/iproto.cc b/src/box/iproto.cc
index 8f899fed8..9b59e1af0 100644
--- a/src/box/iproto.cc
+++ b/src/box/iproto.cc
@@ -1661,9 +1661,20 @@ 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)
+	/* Compile, bind and execute SQL statement. */
+	struct sql_stmt *stmt;
+	if (sql_prepare(sql, len, &stmt, NULL) != 0)
 		goto error;
+	assert(stmt != NULL);
+	port_sql_create(&port, stmt);
+	if (sql_bind(stmt, bind, bind_count) != 0) {
+		port_destroy(&port);
+		goto error;
+	}
+	if (sql_execute(stmt, &port, &fiber()->gc) != 0) {
+		port_destroy(&port);
+		goto error;
+	}
 	/*
 	 * Take an obuf only after execute(). Else the buffer can
 	 * become out of date during yield.
-- 
2.15.1





More information about the Tarantool-patches mailing list