Skip to content

Commit 8b0a159

Browse files
Fix system table comparisons
Plus missed a few places for schema comparisons
1 parent fdab698 commit 8b0a159

4 files changed

Lines changed: 24 additions & 19 deletions

File tree

villagesql/schema/util.cc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919

2020
namespace villagesql {
2121

22-
// TODO(villagesql-beta): Consolidate to one set of functions for checking
23-
// villagesql schema name with proper charset. Also check all of the sites
24-
// that use the constants from SchemaManager to make sure they use this.
2522
bool is_villagesql_system_table(const TABLE *table) {
2623
if (!table || !table->s) return false;
2724
return is_villagesql_schema(table->s->db.str);

villagesql/schema/util.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,32 @@ struct TABLE;
2424

2525
namespace villagesql {
2626

27+
// Case-insensitive comparison for system table/schema identifiers, using
28+
// the system charset. This is how MySQL compares system object names.
29+
inline bool system_name_eq(const char *a, const char *b) {
30+
return my_strcasecmp(system_charset_info, a, b) == 0;
31+
}
32+
2733
// Check if a database name is the 'villagesql' schema. db_name must not be
2834
// nullptr.
2935
inline bool is_villagesql_schema(const char *db_name) {
30-
return (my_strcasecmp(system_charset_info,
31-
SchemaManager::VILLAGESQL_SCHEMA_NAME, db_name) == 0);
36+
return system_name_eq(SchemaManager::VILLAGESQL_SCHEMA_NAME, db_name);
3237
}
3338

3439
// Like the above but also considered true if this is in a mysql system schema.
3540
inline bool is_system_schema(const char *db_name) {
36-
return (my_strcasecmp(system_charset_info, "mysql", db_name) == 0 ||
37-
my_strcasecmp(system_charset_info, "sys", db_name) == 0 ||
41+
return (system_name_eq("mysql", db_name) || system_name_eq("sys", db_name) ||
3842
is_villagesql_schema(db_name));
3943
}
4044

45+
// Check if a schema and table name match a VillageSQL system table.
46+
inline bool is_villagesql_system_table(const char *schema_name,
47+
const char *table_name,
48+
const char *expected_table_name) {
49+
return is_villagesql_schema(schema_name) &&
50+
system_name_eq(table_name, expected_table_name);
51+
}
52+
4153
// Check if a TABLE is in the 'villagesql' schema.
4254
// All tables in the villagesql schema are system tables that can be accessed
4355
// with no_read_locking from INFORMATION_SCHEMA views.

villagesql/schema/victionary_client.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include "villagesql/schema/systable/custom_columns.h"
4949
#include "villagesql/schema/systable/extensions.h"
5050
#include "villagesql/schema/systable/properties.h"
51+
#include "villagesql/schema/util.h"
5152

5253
// Forward declaration for friend class
5354
namespace villagesql_unittest {
@@ -844,8 +845,8 @@ inline TABLE *find_open_table(THD *thd, const char *schema_name,
844845

845846
for (TABLE *table = thd->open_tables; table; table = table->next) {
846847
Table_ref *table_list = table->pos_in_table_list;
847-
if (table_list && strcmp(table_list->db, schema_name) == 0 &&
848-
strcmp(table_list->table_name, table_name) == 0) {
848+
if (table_list && system_name_eq(table_list->db, schema_name) &&
849+
system_name_eq(table_list->table_name, table_name)) {
849850
return table;
850851
}
851852
}

villagesql/sql/metadata_modifier.cc

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ bool Metadata_modifier::add_columns(THD *thd [[maybe_unused]],
7474
const char *table_name = db_table.second;
7575

7676
// Skip special databases
77-
if (strcmp(db_name, "mysql") == 0 || strcmp(db_name, "sys") == 0 ||
78-
strcmp(db_name, SchemaManager::VILLAGESQL_SCHEMA_NAME) == 0) {
77+
if (is_system_schema(db_name)) {
7978
return false;
8079
}
8180

@@ -131,8 +130,7 @@ bool Metadata_modifier::remove_columns(THD *thd [[maybe_unused]],
131130
const char *table_name = db_table.second;
132131

133132
// Skip special databases
134-
if (strcmp(db_name, "mysql") == 0 || strcmp(db_name, "sys") == 0 ||
135-
strcmp(db_name, SchemaManager::VILLAGESQL_SCHEMA_NAME) == 0) {
133+
if (is_system_schema(db_name)) {
136134
return false;
137135
}
138136

@@ -229,8 +227,7 @@ bool Metadata_modifier::alter_columns(THD *thd [[maybe_unused]],
229227
const char *table_name = db_table.second;
230228

231229
// Skip special databases
232-
if (strcmp(db_name, "mysql") == 0 || strcmp(db_name, "sys") == 0 ||
233-
strcmp(db_name, SchemaManager::VILLAGESQL_SCHEMA_NAME) == 0) {
230+
if (is_system_schema(db_name)) {
234231
return false;
235232
}
236233

@@ -721,10 +718,8 @@ bool Metadata_modifier::store(THD *thd) {
721718
// just need to find it in query_tables and lock it.
722719
Table_ref *columns_table = nullptr;
723720
for (Table_ref *tl = thd->lex->query_tables; tl; tl = tl->next_global) {
724-
// TODO(villagesql-beta): table names should be using system charset
725-
// comparison
726-
if (villagesql::is_villagesql_schema(tl->db) &&
727-
strcmp(tl->table_name, SchemaManager::COLUMNS_TABLE_NAME) == 0) {
721+
if (villagesql::is_villagesql_system_table(
722+
tl->db, tl->table_name, SchemaManager::COLUMNS_TABLE_NAME)) {
728723
columns_table = tl;
729724
break;
730725
}

0 commit comments

Comments
 (0)