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.
This commit is contained in:
Markus Mäkelä
2017-12-06 16:48:15 +02:00
parent 7a6d00e807
commit 8ed1ff67c2

View File

@ -403,10 +403,12 @@ static bool get_table_name(const char* sql, char* dest)
/** /**
* Extract the database name from a CREATE TABLE statement * Extract the database name from a CREATE TABLE statement
*
* @param sql SQL statement * @param sql SQL statement
* @param dest Destination where the database name is extracted. Must be at least * @param dest Destination where the database name is extracted. Must be at least
* MYSQL_DATABASE_MAXLEN bytes long. * MYSQL_DATABASE_MAXLEN bytes long.
* @return True if extraction was successful *
* @return True if a database name was extracted
*/ */
static bool get_database_name(const char* sql, char* dest) 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--; 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; return rval;
@ -717,13 +724,15 @@ TABLE_CREATE* table_create_alloc(const char* sql, int len, const char* db)
{ {
err = "table name"; err = "table name";
} }
/** The CREATE statement contains the database name */
if (get_database_name(sql, database)) if (get_database_name(sql, database))
{ {
// The CREATE statement contains the database name
db = database; db = database;
} }
else if (*db == '\0') else if (*db == '\0')
{ {
// No explicit or current database
err = "database name"; err = "database name";
} }