From 8ed1ff67c2d178d9db710da7cc97c62fe46f4b09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Wed, 6 Dec 2017 16:48:15 +0200 Subject: [PATCH] Fix avro regression The fact whether a query explicitly defined a query was ignored by the statement parsing function and it always assumed the database was explicitly defined. This caused the TABLE keyword in a CREATE TABLE statement to be falsely identified as the current database. --- .../modules/routing/avrorouter/avro_schema.c | 43 +++++++++++-------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/server/modules/routing/avrorouter/avro_schema.c b/server/modules/routing/avrorouter/avro_schema.c index 9949c9ebe..2d8199802 100644 --- a/server/modules/routing/avrorouter/avro_schema.c +++ b/server/modules/routing/avrorouter/avro_schema.c @@ -403,10 +403,12 @@ static bool get_table_name(const char* sql, char* dest) /** * Extract the database name from a CREATE TABLE statement + * * @param sql SQL statement * @param dest Destination where the database name is extracted. Must be at least - * MYSQL_DATABASE_MAXLEN bytes long. - * @return True if extraction was successful + * MYSQL_DATABASE_MAXLEN bytes long. + * + * @return True if a database name was extracted */ static bool get_database_name(const char* sql, char* dest) { @@ -426,22 +428,27 @@ static bool get_database_name(const char* sql, char* dest) ptr--; } - while (*ptr == '`' || *ptr == '.' || isspace(*ptr)) + if (*ptr == '.') { - ptr--; + // The query defines an explicit database + + while (*ptr == '`' || *ptr == '.' || isspace(*ptr)) + { + ptr--; + } + + const char* end = ptr + 1; + + while (*ptr != '`' && *ptr != '.' && !isspace(*ptr)) + { + ptr--; + } + + ptr++; + memcpy(dest, ptr, end - ptr); + dest[end - ptr] = '\0'; + rval = true; } - - const char* end = ptr + 1; - - while (*ptr != '`' && *ptr != '.' && !isspace(*ptr)) - { - ptr--; - } - - ptr++; - memcpy(dest, ptr, end - ptr); - dest[end - ptr] = '\0'; - rval = true; } return rval; @@ -717,13 +724,15 @@ TABLE_CREATE* table_create_alloc(const char* sql, int len, const char* db) { err = "table name"; } - /** The CREATE statement contains the database name */ + if (get_database_name(sql, database)) { + // The CREATE statement contains the database name db = database; } else if (*db == '\0') { + // No explicit or current database err = "database name"; }