Tarantool development patches archive
 help / color / mirror / Atom feed
From: Kirill Shcherbatov <kshcherbatov@tarantool.org>
To: tarantool-patches@freelists.org
Cc: v.shpilevoy@tarantool.org,
	Kirill Shcherbatov <kshcherbatov@tarantool.org>
Subject: [tarantool-patches] [PATCH v3 05/10] sql: refactor sql_expr_compile to return AST
Date: Thu, 14 Jun 2018 20:32:23 +0300	[thread overview]
Message-ID: <a9853a023962d90fdee7baa4741b7f9d2ef06bd5.1528997527.git.kshcherbatov@tarantool.org> (raw)
In-Reply-To: <cover.1528997527.git.kshcherbatov@tarantool.org>
In-Reply-To: <cover.1528997527.git.kshcherbatov@tarantool.org>

---
 src/box/alter.cc       |  6 +++---
 src/box/sql.c          |  5 +++--
 src/box/sql.h          |  9 ++++-----
 src/box/sql/tokenize.c | 32 +++++++++++++++++++-------------
 4 files changed, 29 insertions(+), 23 deletions(-)

diff --git a/src/box/alter.cc b/src/box/alter.cc
index b62f8ad..f2bf85d 100644
--- a/src/box/alter.cc
+++ b/src/box/alter.cc
@@ -406,9 +406,9 @@ field_def_decode(struct field_def *field, const char **data,
 	}
 
 	if (field->default_value != NULL &&
-	    sql_expr_compile(sql_get(), field->default_value,
-			     strlen(field->default_value),
-			     &field->default_value_expr) != 0)
+		(field->default_value_expr =
+			sql_expr_compile(sql_get(), field->default_value,
+					 strlen(field->default_value))) == NULL)
 		diag_raise();
 }
 
diff --git a/src/box/sql.c b/src/box/sql.c
index 85de926..d18f379 100644
--- a/src/box/sql.c
+++ b/src/box/sql.c
@@ -1813,8 +1813,9 @@ sql_check_list_item_init(struct ExprList *expr_list, int column,
 			return -1;
 		}
 	}
-	if (expr_str != NULL && sql_expr_compile(db, expr_str, expr_str_len,
-						 &item->pExpr) != 0) {
+	if (expr_str != NULL &&
+	    (item->pExpr = sql_expr_compile(db, expr_str, expr_str_len)) ==
+		NULL) {
 		sqlite3DbFree(db, item->zName);
 		return -1;
 	}
diff --git a/src/box/sql.h b/src/box/sql.h
index 23021e5..3f6cf22 100644
--- a/src/box/sql.h
+++ b/src/box/sql.h
@@ -75,13 +75,12 @@ struct Table;
  * @param db SQL context handle.
  * @param expr Expression to parse.
  * @param expr_len Length of @an expr.
- * @param[out] result Result: AST structure.
  *
- * @retval Error code if any.
+ * @retval NULL on error.
+ * @retval not NULL Expr AST pointer on success.
  */
-int
-sql_expr_compile(struct sqlite3 *db, const char *expr, int expr_len,
-		 struct Expr **result);
+struct Expr *
+sql_expr_compile(struct sqlite3 *db, const char *expr, int expr_len);
 
 /**
  * Store duplicate of a parsed expression into @a parser.
diff --git a/src/box/sql/tokenize.c b/src/box/sql/tokenize.c
index 42c70a2..d84a840 100644
--- a/src/box/sql/tokenize.c
+++ b/src/box/sql/tokenize.c
@@ -42,6 +42,7 @@
 
 #include "say.h"
 #include "sqliteInt.h"
+#include "tarantoolInt.h"
 
 /* Character classes for tokenizing
  *
@@ -510,8 +511,13 @@ sqlite3RunParser(Parse * pParse, const char *zSql, char **pzErrMsg)
 	}
 	if (pParse->rc != SQLITE_OK && pParse->rc != SQLITE_DONE
 	    && pParse->zErrMsg == 0) {
-		pParse->zErrMsg =
-		    sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
+		const char *error;
+		if (is_tarantool_error(pParse->rc) &&
+		    tarantoolErrorMessage() != NULL)
+			error = tarantoolErrorMessage();
+		else
+			error = sqlite3ErrStr(pParse->rc);
+		pParse->zErrMsg = sqlite3MPrintf(db, "%s", error);
 	}
 	assert(pzErrMsg != 0);
 	if (pParse->zErrMsg) {
@@ -539,9 +545,8 @@ sqlite3RunParser(Parse * pParse, const char *zSql, char **pzErrMsg)
 	return nErr;
 }
 
-int
-sql_expr_compile(sqlite3 *db, const char *expr, int expr_len,
-		 struct Expr **result)
+struct Expr *
+sql_expr_compile(sqlite3 *db, const char *expr, int expr_len)
 {
 	const char *outer = "SELECT ";
 	int len = strlen(outer) + expr_len;
@@ -550,19 +555,20 @@ sql_expr_compile(sqlite3 *db, const char *expr, int expr_len,
 	sql_parser_create(&parser, db);
 	parser.parse_only = true;
 
+	struct Expr *expression = NULL;
 	char *stmt = (char *)region_alloc(&parser.region, len + 1);
 	if (stmt == NULL) {
 		diag_set(OutOfMemory, len + 1, "region_alloc", "stmt");
-		return -1;
+		return NULL;
 	}
 	sprintf(stmt, "%s%.*s", outer, expr_len, expr);
 
-	char *unused;
-	if (sqlite3RunParser(&parser, stmt, &unused) != SQLITE_OK) {
-		diag_set(ClientError, ER_SQL_EXECUTE, expr);
-		return -1;
-	}
-	*result = parser.parsed_expr;
+	char *sql_error;
+	if (sqlite3RunParser(&parser, stmt, &sql_error) != SQLITE_OK &&
+		parser.rc != SQL_TARANTOOL_ERROR)
+		diag_set(ClientError, ER_SQL, sql_error);
+	else
+		expression = parser.parsed_expr;
 	sql_parser_destroy(&parser);
-	return 0;
+	return expression;
 }
-- 
2.7.4

  parent reply	other threads:[~2018-06-14 17:32 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-14 17:32 [tarantool-patches] [PATCH v3 00/10] sql: remove Triggers to server Kirill Shcherbatov
2018-06-14 17:32 ` [tarantool-patches] [PATCH v3 01/10] box: move db->pShchema init to sql_init Kirill Shcherbatov
2018-06-14 17:32 ` [tarantool-patches] [PATCH v3 10/10] sql: VDBE tests for trigger existence Kirill Shcherbatov
2018-06-14 19:27   ` [tarantool-patches] " Vladislav Shpilevoy
2018-06-15 16:21     ` Kirill Shcherbatov
2018-06-18 15:42       ` Vladislav Shpilevoy
2018-06-18 19:22         ` Kirill Shcherbatov
2018-06-19 10:24           ` Vladislav Shpilevoy
2018-06-19 15:12             ` Kirill Shcherbatov
2018-06-19 15:23               ` Vladislav Shpilevoy
2018-06-20  6:38                 ` Kirill Shcherbatov
2018-06-20  8:10                   ` Vladislav Shpilevoy
2018-06-20  8:24                     ` Kirill Shcherbatov
2018-06-14 17:32 ` [tarantool-patches] [PATCH v3 02/10] sql: fix leak on CREATE TABLE and resolve self ref Kirill Shcherbatov
2018-06-14 22:46   ` [tarantool-patches] " n.pettik
2018-06-15  9:25     ` Vladislav Shpilevoy
2018-06-14 17:32 ` [tarantool-patches] [PATCH v3 03/10] sql: fix sql len in tarantoolSqlite3RenameTrigger Kirill Shcherbatov
2018-06-14 17:32 ` [tarantool-patches] [PATCH v3 04/10] box: port schema_find_id to C Kirill Shcherbatov
2018-06-14 19:27   ` [tarantool-patches] " Vladislav Shpilevoy
2018-06-14 22:46     ` n.pettik
2018-06-15  9:25       ` Vladislav Shpilevoy
2018-06-14 17:32 ` Kirill Shcherbatov [this message]
2018-06-14 19:27   ` [tarantool-patches] Re: [PATCH v3 05/10] sql: refactor sql_expr_compile to return AST Vladislav Shpilevoy
2018-06-15 16:21     ` Kirill Shcherbatov
2018-06-14 17:32 ` [tarantool-patches] [PATCH v3 06/10] sql: move sqlite3DeleteTrigger to sql.h Kirill Shcherbatov
2018-06-14 19:27   ` [tarantool-patches] " Vladislav Shpilevoy
2018-06-14 17:32 ` [tarantool-patches] [PATCH v3 07/10] box: sort error codes in misc.test Kirill Shcherbatov
2018-06-14 17:32 ` [tarantool-patches] [PATCH v3 08/10] sql: new _trigger space format with space_id Kirill Shcherbatov
2018-06-14 19:27   ` [tarantool-patches] " Vladislav Shpilevoy
2018-06-15 16:21     ` Kirill Shcherbatov
2018-06-14 17:32 ` [tarantool-patches] [PATCH v3 09/10] sql: move Triggers to server Kirill Shcherbatov
2018-06-14 19:27   ` [tarantool-patches] " Vladislav Shpilevoy
2018-06-15 16:21     ` Kirill Shcherbatov
2018-06-18 15:42       ` Vladislav Shpilevoy
2018-06-18 19:22         ` Kirill Shcherbatov
2018-06-14 17:34 ` [tarantool-patches] Re: [PATCH v3 00/10] sql: remove " Kirill Shcherbatov
2018-06-20  8:35 ` Vladislav Shpilevoy
2018-06-28 15:47   ` n.pettik

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=a9853a023962d90fdee7baa4741b7f9d2ef06bd5.1528997527.git.kshcherbatov@tarantool.org \
    --to=kshcherbatov@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [tarantool-patches] [PATCH v3 05/10] sql: refactor sql_expr_compile to return AST' \
    /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