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 9CCE921614 for ; Sat, 9 Jun 2018 05:37:01 -0400 (EDT) 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 F9nmo-H7mawc for ; Sat, 9 Jun 2018 05:37:01 -0400 (EDT) Received: from fallback.mail.ru (fallback7.mail.ru [94.100.181.128]) (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 479831FCBC for ; Sat, 9 Jun 2018 05:37:01 -0400 (EDT) Received: from [10.161.100.15] (port=54074 helo=smtpng3.m.smailru.net) by fallback7.mail.ru with esmtp (envelope-from ) id 1fRaEe-0001FS-88 for tarantool-patches@freelists.org; Sat, 09 Jun 2018 12:32:36 +0300 From: Kirill Shcherbatov Subject: [tarantool-patches] [PATCH v2 06/11] sql: refactor sql_expr_compile to return AST Date: Sat, 9 Jun 2018 12:32:19 +0300 Message-Id: <74d9d3a2f3198fc4821395b91bb7b637b35af5d3.1528535873.git.kshcherbatov@tarantool.org> In-Reply-To: References: 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: tarantool-patches@freelists.org Cc: v.shpilevoy@tarantool.org, Kirill Shcherbatov --- src/box/alter.cc | 6 +++--- src/box/sql.c | 5 +++-- src/box/sql.h | 9 ++++----- src/box/sql/tokenize.c | 33 +++++++++++++++++++++------------ 4 files changed, 31 insertions(+), 22 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 82bddfb..599cb60 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..5b7c97d 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,23 @@ 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; + char *sql_error; + if (sqlite3RunParser(&parser, stmt, &sql_error) != SQLITE_OK) { + char *error = tt_static_buf(); + snprintf(error, TT_STATIC_BUF_LEN, "%s", sql_error); + diag_set(ClientError, ER_SQL, error); + sqlite3DbFree(db, sql_error); + } else { + expression = parser.parsed_expr; } - *result = parser.parsed_expr; sql_parser_destroy(&parser); - return 0; + return expression; } -- 2.7.4