[tarantool-patches] Re: [PATCH 2/6] sql: don't update SQL string during renaming
Vladislav Shpilevoy
v.shpilevoy at tarantool.org
Mon Dec 10 17:16:15 MSK 2018
Thanks for the patch!
On 10/12/2018 00:30, Nikita Pettik wrote:
> Since SQL string containing "CREATE TABLE ..." statement is not used
> anymore for ordinary tables/space, it makes no sense to modify it during
> renaming. Hence, now rename routine needs only to update name in _space,
> so it can be done using simple update operation.
>
> Moreover, now we are able to rename spaces created from Lua-land.
>
> Part of #2647
> ---
> src/box/sql.c | 187 ++++++++------------------------------------
> src/box/sql/tarantoolInt.h | 3 +-
> src/box/sql/vdbe.c | 4 +-
> test/sql-tap/alter.test.lua | 32 +++++++-
> 4 files changed, 63 insertions(+), 163 deletions(-)
You forgot to remove some code and comments. My
review fixes here and on the branch.
========================================================
commit 3eeb01aa01766001fb313eaf26d539273100b71b
Author: Vladislav Shpilevoy <v.shpilevoy at tarantool.org>
Date: Mon Dec 10 16:27:04 2018 +0300
Review fixes
diff --git a/src/box/sql/alter.c b/src/box/sql/alter.c
index 89e51eb30..d0ce9d893 100644
--- a/src/box/sql/alter.c
+++ b/src/box/sql/alter.c
@@ -77,78 +77,6 @@ tnt_error:
goto exit_rename_table;
}
-/*
- * This function implements part of the ALTER TABLE command.
- * The first argument is the text of a CREATE TABLE or CREATE INDEX command.
- * The second is a table name. The table name in the CREATE TABLE or
- * CREATE INDEX statement is replaced with the second argument and
- * the result returned. There is no need to deallocate returned memory, since
- * it will be doe automatically by VDBE. Note that new statement always
- * contains quoted new table name.
- *
- * Examples:
- *
- * sqlite_rename_table('CREATE TABLE abc(a, b, c)', 'def')
- * -> 'CREATE TABLE "def"(a, b, c)'
- *
- * sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def')
- * -> 'CREATE INDEX i ON "def"(a, b, c)'
- *
- * @param sql_stmt text of a CREATE TABLE or CREATE INDEX statement
- * @param table_name new table name
- * @param[out] is_quoted true if statement to be modified contains quoted name
- *
- * @retval new SQL statement on success, NULL otherwise.
- */
-char*
-rename_table(sqlite3 *db, const char *sql_stmt, const char *table_name,
- bool *is_quoted)
-{
- assert(sql_stmt);
- assert(table_name);
- assert(is_quoted);
-
- int token;
- Token old_name;
- const char *csr = sql_stmt;
- int len = 0;
- char *new_sql_stmt;
- bool unused;
-
- /* The principle used to locate the table name in the CREATE TABLE
- * statement is that the table name is the first non-space token that
- * is immediately followed by a TK_LP or TK_USING token.
- */
- do {
- if (!*csr) {
- /* Ran out of input before finding a bracket. */
- return NULL;
- }
- /* Store the token that zCsr points to in tname. */
- old_name.z = (char *)csr;
- old_name.n = len;
- /* Advance zCsr to the next token.
- * Store that token type in 'token', and its length
- * in 'len' (to be used next iteration of this loop).
- */
- do {
- csr += len;
- len = sql_token(csr, &token, &unused);
- } while (token == TK_SPACE);
- assert(len > 0);
- } while (token != TK_LP && token != TK_USING);
-
- if (*old_name.z == '"')
- *is_quoted = true;
- /* No need to care about deallocating zRet, since its memory
- * will be automatically freed by VDBE.
- */
- new_sql_stmt = sqlite3MPrintf(db, "%.*s\"%w\"%s",
- (int)((old_name.z) - sql_stmt), sql_stmt,
- table_name, old_name.z + old_name.n);
- return new_sql_stmt;
-}
-
/* This function is used to implement the ALTER TABLE command.
* The table name in the CREATE TRIGGER statement is replaced with the third
* argument and the result returned. This is analagous to rename_table()
diff --git a/src/box/sql/sqliteInt.h b/src/box/sql/sqliteInt.h
index dbf58d967..85adcdb18 100644
--- a/src/box/sql/sqliteInt.h
+++ b/src/box/sql/sqliteInt.h
@@ -4462,7 +4462,6 @@ int sqlite3ResolveOrderGroupBy(Parse *, Select *, ExprList *, const char *);
void
sqlite3ColumnDefault(Vdbe *v, struct space_def *def, int i, int ireg);
-char* rename_table(sqlite3 *, const char *, const char *, bool *);
char* rename_trigger(sqlite3 *, char const *, char const *, bool *);
/**
* Find a collation by name. Set error in @a parser if not found.
diff --git a/src/box/sql/tarantoolInt.h b/src/box/sql/tarantoolInt.h
index e963132fe..0aa31bdd6 100644
--- a/src/box/sql/tarantoolInt.h
+++ b/src/box/sql/tarantoolInt.h
@@ -61,37 +61,15 @@ sql_delete_by_key(struct space *space, uint32_t iid, char *key,
int tarantoolSqlite3ClearTable(struct space *space, uint32_t *tuple_count);
/**
- * Rename the table in _space. Update tuple with corresponding id
- * with new name and statement fields and insert back. If sql_stmt
- * is NULL, then return from function after getting length of new
- * statement: it is the way how to dynamically allocate memory for
- * new statement in VDBE. So basically this function should be
- * called twice: firstly to get length of CREATE TABLE statement,
- * and secondly to make routine of replacing tuple and filling out
- * param sql_stmt with new CREATE TABLE statement.
- *
+ * Rename the table in _space.
* @param space_id Table's space identifier.
* @param new_name new name of table
*
- * @retval SQLITE_OK on success, SQLITE_TARANTOOL_ERROR otherwise.
+ * @retval 0 on success, SQL_TARANTOOL_ERROR otherwise.
*/
int
sql_rename_table(uint32_t space_id, const char *new_name);
-/**
- * Update CREATE INDEX field (def->opt.sql) replacing table name
- * w/ new one in _index space.
- *
- * @param idef Index definition.
- * @param new_tbl_name new name of table
- * @param[out] sql_stmt New CREATE INDEX statement.
- *
- * @retval 0 on success, -1 otherwise.
- */
-int
-sql_index_update_table_name(struct index_def *idef, const char *new_tbl_name,
- char **sql_stmt);
-
/* Alter trigger statement after rename table. */
int tarantoolSqlite3RenameTrigger(const char *zTriggerName,
const char *zOldName, const char *zNewName);
More information about the Tarantool-patches
mailing list