Skip to content

A native macOS / windows / Linux database IDE built with Swift and AppKit. Connect to PostgreSQL, MySQL, SQLite, and Redis from a single app with a fast, keyboard-driven interface.

C++

fix(adapter): fallback empty schema to database name and public schema on Windows

philau2512178d4f663657
diff --git a/windows/Gridex/Models/MySQLAdapter.h b/windows/Gridex/Models/MySQLAdapter.h
index 6d5139f..2626785 100644
--- a/windows/Gridex/Models/MySQLAdapter.h
+++ b/windows/Gridex/Models/MySQLAdapter.h
@@ -76,6 +76,8 @@ namespace DBModels
static std::wstring fromUtf8(const std::string& str);
static std::string quoteIdentifier(const std::wstring& name);
static std::string quoteLiteral(const std::wstring& value);
+ std::wstring resolveSchema(const std::wstring& schema);
+ std::string qualifyTable(const std::wstring& schema, const std::wstring& table);
void ensureConnected() const;
QueryResult executeInternal(const std::string& sql);
};
diff --git a/windows/Gridex/Models/PostgreSQLAdapter.h b/windows/Gridex/Models/PostgreSQLAdapter.h
index cb6d9d4..dd26ba8 100644
--- a/windows/Gridex/Models/PostgreSQLAdapter.h
+++ b/windows/Gridex/Models/PostgreSQLAdapter.h
@@ -89,6 +89,9 @@ namespace DBModels
static std::string quoteIdentifier(const std::wstring& name);
// Helper: quote literal (single-quotes with escaping)
static std::string quoteLiteral(const std::wstring& value);
+ // Helper: resolve schema fallback (defaults to public)
+ static std::wstring resolveSchema(const std::wstring& schema);
+ static std::string qualifyTable(const std::wstring& schema, const std::wstring& table);
// Helper: ensure connected, throw if not
void ensureConnected() const;
// Helper: execute a query and return result, throw on error
diff --git a/windows/Gridex/MySQLAdapter.cpp b/windows/Gridex/MySQLAdapter.cpp
index 977ba8e..ed2bc1a 100644
--- a/windows/Gridex/MySQLAdapter.cpp
+++ b/windows/Gridex/MySQLAdapter.cpp
@@ -49,6 +49,19 @@ namespace DBModels
return escaped;
}
+ std::wstring MySQLAdapter::resolveSchema(const std::wstring& schema)
+ {
+ if (!schema.empty()) return schema;
+ return currentDatabase();
+ }
+
+ std::string MySQLAdapter::qualifyTable(const std::wstring& schema, const std::wstring& table)
+ {
+ std::wstring s = resolveSchema(schema);
+ if (s.empty()) return quoteIdentifier(table);
+ return quoteIdentifier(s) + "." + quoteIdentifier(table);
+ }
+
// Public wrappers — wstring in, wstring out. Delegate to utf8 helpers.
std::wstring MySQLAdapter::quoteSqlLiteral(const std::wstring& value) const
{
@@ -318,7 +331,8 @@ namespace DBModels
const std::wstring& orderBy, bool ascending)
{
// MySQL uses database name instead of schema
- std::string sql = "SELECT * FROM " + quoteIdentifier(schema) + "." + quoteIdentifier(table);
+ std::string targetTable = qualifyTable(schema, table);
+ std::string sql = "SELECT * FROM " + targetTable;
if (!orderBy.empty())
sql += " ORDER BY " + quoteIdentifier(orderBy) + (ascending ? " ASC" : " DESC");
sql += " LIMIT " + std::to_string(limit) + " OFFSET " + std::to_string(offset);
@@ -326,8 +340,7 @@ namespace DBModels
auto result = executeInternal(sql);
// Get total count
- std::string countSql = "SELECT COUNT(*) AS cnt FROM " +
- quoteIdentifier(schema) + "." + quoteIdentifier(table);
+ std::string countSql = "SELECT COUNT(*) AS cnt FROM " + targetTable;
auto countResult = executeInternal(countSql);
if (countResult.success && !countResult.rows.empty())
{
@@ -361,10 +374,11 @@ namespace DBModels
std::vector<TableInfo> MySQLAdapter::listTables(const std::wstring& schema)
{
+ std::wstring targetDb = resolveSchema(schema);
std::string sql =
"SELECT TABLE_NAME, TABLE_COMMENT, TABLE_ROWS, DATA_LENGTH + INDEX_LENGTH AS size_bytes "
"FROM INFORMATION_SCHEMA.TABLES "
- "WHERE TABLE_SCHEMA = " + quoteLiteral(schema) +
+ "WHERE TABLE_SCHEMA = " + quoteLiteral(targetDb) +
" AND TABLE_TYPE = 'BASE TABLE' ORDER BY TABLE_NAME";
auto result = executeInternal(sql);
@@ -373,7 +387,7 @@ namespace DBModels
{
TableInfo info;
info.name = row[L"TABLE_NAME"];
- info.schema = schema;
+ info.schema = targetDb;
info.type = L"table";
info.comment = row[L"TABLE_COMMENT"];
auto estIt = row.find(L"TABLE_ROWS");
@@ -389,9 +403,10 @@ namespace DBModels
std::vector<TableInfo> MySQLAdapter::listViews(const std::wstring& schema)
{
+ std::wstring targetDb = resolveSchema(schema);
std::string sql =
"SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS "
- "WHERE TABLE_SCHEMA = " + quoteLiteral(schema) + " ORDER BY TABLE_NAME";
+ "WHERE TABLE_SCHEMA = " + quoteLiteral(targetDb) + " ORDER BY TABLE_NAME";
auto result = executeInternal(sql);
std::vector<TableInfo> views;
@@ -399,7 +414,7 @@ namespace DBModels
{
TableInfo info;
info.name = row[L"TABLE_NAME"];
- info.schema = schema;
+ info.schema = targetDb;
info.type = L"view";
views.push_back(info);
}
@@ -409,11 +424,12 @@ namespace DBModels
std::vector<ColumnInfo> MySQLAdapter::describeTable(
const std::wstring& table, const std::wstring& schema)
{
+ std::wstring targetDb = resolveSchema(schema);
std::string sql =
"SELECT COLUMN_NAME, COLUMN_TYPE, IS_NULLABLE, COLUMN_DEFAULT, "
"COLUMN_KEY, ORDINAL_POSITION, COLUMN_COMMENT "
"FROM INFORMATION_SCHEMA.COLUMNS "
- "WHERE TABLE_SCHEMA = " + quoteLiteral(schema) +
+ "WHERE TABLE_SCHEMA = " + quoteLiteral(targetDb) +
" AND TABLE_NAME = " + quoteLiteral(table) +
" ORDER BY ORDINAL_POSITION";
@@ -435,7 +451,7 @@ namespace DBModels
}
// Mark FK columns
- auto fks = listForeignKeys(table, schema);
+ auto fks = listForeignKeys(table, targetDb);
for (auto& fk : fks)
for (auto& col : columns)
if (col.name == fk.column)
@@ -451,11 +467,12 @@ namespace DBModels
std::vector<IndexInfo> MySQLAdapter::listIndexes(
const std::wstring& table, const std::wstring& schema)
{
+ std::wstring targetDb = resolveSchema(schema);
std::string sql =
"SELECT INDEX_NAME, GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX) AS columns, "
"NOT NON_UNIQUE AS is_unique, INDEX_TYPE AS algorithm "
"FROM INFORMATION_SCHEMA.STATISTICS "
- "WHERE TABLE_SCHEMA = " + quoteLiteral(schema) +
+ "WHERE TABLE_SCHEMA = " + quoteLiteral(targetDb) +
" AND TABLE_NAME = " + quoteLiteral(table) +
" GROUP BY INDEX_NAME, NON_UNIQUE, INDEX_TYPE ORDER BY INDEX_NAME";
@@ -477,11 +494,12 @@ namespace DBModels
std::vector<ForeignKeyInfo> MySQLAdapter::listForeignKeys(
const std::wstring& table, const std::wstring& schema)
{
+ std::wstring targetDb = resolveSchema(schema);
std::string sql =
"SELECT CONSTRAINT_NAME, COLUMN_NAME, REFERENCED_TABLE_NAME, "
"REFERENCED_COLUMN_NAME "
"FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE "
- "WHERE TABLE_SCHEMA = " + quoteLiteral(schema) +
+ "WHERE TABLE_SCHEMA = " + quoteLiteral(targetDb) +
" AND TABLE_NAME = " + quoteLiteral(table) +
" AND REFERENCED_TABLE_NAME IS NOT NULL ORDER BY CONSTRAINT_NAME";
@@ -503,9 +521,10 @@ namespace DBModels
std::vector<std::wstring> MySQLAdapter::listFunctions(const std::wstring& schema)
{
+ std::wstring targetDb = resolveSchema(schema);
std::string sql =
"SELECT ROUTINE_NAME FROM INFORMATION_SCHEMA.ROUTINES "
- "WHERE ROUTINE_SCHEMA = " + quoteLiteral(schema) +
+ "WHERE ROUTINE_SCHEMA = " + quoteLiteral(targetDb) +
" AND ROUTINE_TYPE = 'FUNCTION' ORDER BY ROUTINE_NAME";
auto result = executeInternal(sql);
@@ -521,7 +540,7 @@ namespace DBModels
std::wstring MySQLAdapter::getFunctionSource(
const std::wstring& name, const std::wstring& schema)
{
- std::string sql = "SHOW CREATE FUNCTION " + quoteIdentifier(schema) + "." + quoteIdentifier(name);
+ std::string sql = "SHOW CREATE FUNCTION " + qualifyTable(schema, name);
auto result = executeInternal(sql);
if (!result.rows.empty())
{
@@ -534,8 +553,7 @@ namespace DBModels
std::wstring MySQLAdapter::getCreateTableSQL(
const std::wstring& table, const std::wstring& schema)
{
- std::string sql = "SHOW CREATE TABLE " +
- quoteIdentifier(schema) + "." + quoteIdentifier(table);
+ std::string sql = "SHOW CREATE TABLE " + qualifyTable(schema, table);
auto result = executeInternal(sql);
if (!result.rows.empty())
{
@@ -550,7 +568,8 @@ namespace DBModels
const std::wstring& table, const std::wstring& schema,
const TableRow& values)
{
- std::string sql = "INSERT INTO " + quoteIdentifier(schema) + "." + quoteIdentifier(table) + " (";
+ std::string targetTable = qualifyTable(schema, table);
+ std::string sql = "INSERT INTO " + targetTable + " (";
std::string valsSql;
bool first = true;
for (auto& [col, val] : values)
@@ -569,7 +588,7 @@ namespace DBModels
// All columns blank → use MySQL's all-defaults insert form so we
// don't produce invalid "INSERT INTO t () VALUES ()".
if (first)
- sql = "INSERT INTO " + quoteIdentifier(schema) + "." + quoteIdentifier(table) + " () VALUES ()";
+ sql = "INSERT INTO " + targetTable + " () VALUES ()";
else
sql += ") VALUES (" + valsSql + ")";
return executeInternal(sql);
@@ -579,7 +598,7 @@ namespace DBModels
const std::wstring& table, const std::wstring& schema,
const TableRow& setValues, const TableRow& whereValues)
{
- std::string sql = "UPDATE " + quoteIdentifier(schema) + "." + quoteIdentifier(table) + " SET ";
+ std::string sql = "UPDATE " + qualifyTable(schema, table) + " SET ";
bool first = true;
for (auto& [col, val] : setValues)
{
@@ -605,7 +624,7 @@ namespace DBModels
const std::wstring& table, const std::wstring& schema,
const TableRow& whereValues)
{
- std::string sql = "DELETE FROM " + quoteIdentifier(schema) + "." + quoteIdentifier(table) + " WHERE ";
+ std::string sql = "DELETE FROM " + qualifyTable(schema, table) + " WHERE ";
bool first = true;
for (auto& [col, val] : whereValues)
{
diff --git a/windows/Gridex/PostgreSQLAdapter.cpp b/windows/Gridex/PostgreSQLAdapter.cpp
index b7e6ff2..4da167b 100644
--- a/windows/Gridex/PostgreSQLAdapter.cpp
+++ b/windows/Gridex/PostgreSQLAdapter.cpp
@@ -51,6 +51,16 @@ namespace DBModels
return escaped;
}
+ std::wstring PostgreSQLAdapter::resolveSchema(const std::wstring& schema)
+ {
+ return schema.empty() ? L"public" : schema;
+ }
+
+ std::string PostgreSQLAdapter::qualifyTable(const std::wstring& schema, const std::wstring& table)
+ {
+ return quoteIdentifier(resolveSchema(schema)) + "." + quoteIdentifier(table);
+ }
+
// Public wrappers over the existing utf8 helpers. Return wstring so
// callers that assemble wide SQL (enterprise row graph) don't have
// to UTF-8 round-trip themselves.
@@ -241,7 +251,8 @@ namespace DBModels
int limit, int offset,
const std::wstring& orderBy, bool ascending)
{
- std::string sql = "SELECT * FROM " + quoteIdentifier(schema) + "." + quoteIdentifier(table);
+ std::string targetTable = qualifyTable(schema, table);
+ std::string sql = "SELECT * FROM " + targetTable;
if (!orderBy.empty())
sql += " ORDER BY " + quoteIdentifier(orderBy) + (ascending ? " ASC" : " DESC");
sql += " LIMIT " + std::to_string(limit) + " OFFSET " + std::to_string(offset);
@@ -249,7 +260,7 @@ namespace DBModels
auto result = executeInternal(sql);
// Get total row count for pagination metadata
- std::string countSql = "SELECT COUNT(*) FROM " + quoteIdentifier(schema) + "." + quoteIdentifier(table);
+ std::string countSql = "SELECT COUNT(*) FROM " + targetTable;
auto countResult = executeInternal(countSql);
if (countResult.success && !countResult.rows.empty())
{
@@ -295,6 +306,7 @@ namespace DBModels
std::vector<TableInfo> PostgreSQLAdapter::listTables(const std::wstring& schema)
{
+ std::wstring targetSchema = resolveSchema(schema);
std::string sql =
"SELECT c.relname AS name, n.nspname AS schema, "
"pg_catalog.obj_description(c.oid) AS comment, "
@@ -302,7 +314,7 @@ namespace DBModels
"pg_total_relation_size(c.oid) AS size_bytes "
"FROM pg_class c "
"JOIN pg_namespace n ON n.oid = c.relnamespace "
- "WHERE c.relkind = 'r' AND n.nspname = " + quoteLiteral(schema) +
+ "WHERE c.relkind = 'r' AND n.nspname = " + quoteLiteral(targetSchema) +
" ORDER BY c.relname";
auto result = executeInternal(sql);
@@ -327,12 +339,13 @@ namespace DBModels
std::vector<TableInfo> PostgreSQLAdapter::listViews(const std::wstring& schema)
{
+ std::wstring targetSchema = resolveSchema(schema);
std::string sql =
"SELECT c.relname AS name, n.nspname AS schema, "
"pg_catalog.obj_description(c.oid) AS comment "
"FROM pg_class c "
"JOIN pg_namespace n ON n.oid = c.relnamespace "
- "WHERE c.relkind IN ('v','m') AND n.nspname = " + quoteLiteral(schema) +
+ "WHERE c.relkind IN ('v','m') AND n.nspname = " + quoteLiteral(targetSchema) +
" ORDER BY c.relname";
auto result = executeInternal(sql);
@@ -351,6 +364,7 @@ namespace DBModels
std::vector<ColumnInfo> PostgreSQLAdapter::describeTable(
const std::wstring& table, const std::wstring& schema)
{
+ std::wstring targetSchema = resolveSchema(schema);
std::string sql =
"SELECT c.column_name, c.data_type, c.is_nullable, c.column_default, "
"c.ordinal_position, c.udt_name, c.character_maximum_length, "
@@ -363,10 +377,10 @@ namespace DBModels
" JOIN information_schema.key_column_usage ku ON tc.constraint_name = ku.constraint_name "
" WHERE tc.constraint_type = 'PRIMARY KEY' "
" AND tc.table_name = " + quoteLiteral(table) +
- " AND tc.table_schema = " + quoteLiteral(schema) +
+ " AND tc.table_schema = " + quoteLiteral(targetSchema) +
") pk ON pk.column_name = c.column_name "
"WHERE c.table_name = " + quoteLiteral(table) +
- " AND c.table_schema = " + quoteLiteral(schema) +
+ " AND c.table_schema = " + quoteLiteral(targetSchema) +
" ORDER BY c.ordinal_position";
auto result = executeInternal(sql);
@@ -413,7 +427,7 @@ namespace DBModels
}
// Mark FK columns using listForeignKeys
- auto fks = listForeignKeys(table, schema);
+ auto fks = listForeignKeys(table, targetSchema);
for (auto& fk : fks)
{
for (auto& col : columns)
@@ -433,6 +447,7 @@ namespace DBModels
std::vector<IndexInfo> PostgreSQLAdapter::listIndexes(
const std::wstring& table, const std::wstring& schema)
{
+ std::wstring targetSchema = resolveSchema(schema);
std::string sql =
"SELECT i.relname AS index_name, "
"am.amname AS algorithm, "
@@ -449,7 +464,7 @@ namespace DBModels
"JOIN pg_class i ON i.oid = ix.indexrelid "
"JOIN pg_am am ON am.oid = i.relam "
"WHERE t.relname = " + quoteLiteral(table) +
- " AND n.nspname = " + quoteLiteral(schema) +
+ " AND n.nspname = " + quoteLiteral(targetSchema) +
" ORDER BY i.relname";
auto result = executeInternal(sql);
@@ -470,6 +485,7 @@ namespace DBModels
std::vector<ForeignKeyInfo> PostgreSQLAdapter::listForeignKeys(
const std::wstring& table, const std::wstring& schema)
{
+ std::wstring targetSchema = resolveSchema(schema);
std::string sql =
"SELECT conname AS name, "
"a.attname AS column_name, "
@@ -487,7 +503,7 @@ namespace DBModels
"JOIN pg_attribute af ON af.attrelid = cf.oid AND af.attnum = ANY(co.confkey) "
"WHERE co.contype = 'f' "
"AND c.relname = " + quoteLiteral(table) +
- " AND n.nspname = " + quoteLiteral(schema) +
+ " AND n.nspname = " + quoteLiteral(targetSchema) +
" ORDER BY conname";
auto result = executeInternal(sql);
@@ -508,11 +524,12 @@ namespace DBModels
std::vector<std::wstring> PostgreSQLAdapter::listFunctions(const std::wstring& schema)
{
+ std::wstring targetSchema = resolveSchema(schema);
std::string sql =
"SELECT p.proname AS name "
"FROM pg_proc p "
"JOIN pg_namespace n ON n.oid = p.pronamespace "
- "WHERE n.nspname = " + quoteLiteral(schema) +
+ "WHERE n.nspname = " + quoteLiteral(targetSchema) +
" AND p.prokind = 'f' "
"ORDER BY p.proname";
@@ -529,12 +546,13 @@ namespace DBModels
std::wstring PostgreSQLAdapter::getFunctionSource(
const std::wstring& name, const std::wstring& schema)
{
+ std::wstring targetSchema = resolveSchema(schema);
std::string sql =
"SELECT pg_get_functiondef(p.oid) AS source "
"FROM pg_proc p "
"JOIN pg_namespace n ON n.oid = p.pronamespace "
"WHERE p.proname = " + quoteLiteral(name) +
- " AND n.nspname = " + quoteLiteral(schema) +
+ " AND n.nspname = " + quoteLiteral(targetSchema) +
" LIMIT 1";
auto result = executeInternal(sql);
@@ -549,11 +567,12 @@ namespace DBModels
std::wstring PostgreSQLAdapter::getCreateTableSQL(
const std::wstring& table, const std::wstring& schema)
{
+ std::wstring targetSchema = resolveSchema(schema);
// Build DDL from describeTable output — basic columns + NOT NULL + DEFAULT + PK
- auto columns = describeTable(table, schema);
+ auto columns = describeTable(table, targetSchema);
if (columns.empty()) return L"";
- std::wstring sql = L"CREATE TABLE \"" + schema + L"\".\"" + table + L"\" (\n";
+ std::wstring sql = L"CREATE TABLE \"" + targetSchema + L"\".\"" + table + L"\" (\n";
std::vector<std::wstring> pkCols;
for (size_t i = 0; i < columns.size(); i++)
{
@@ -587,7 +606,8 @@ namespace DBModels
const std::wstring& table, const std::wstring& schema,
const TableRow& values)
{
- std::string sql = "INSERT INTO " + quoteIdentifier(schema) + "." + quoteIdentifier(table) + " (";
+ std::string targetTable = qualifyTable(schema, table);
+ std::string sql = "INSERT INTO " + targetTable + " (";
std::string valsSql;
bool first = true;
for (auto& [col, val] : values)
@@ -603,7 +623,7 @@ namespace DBModels
}
// All columns blank → PostgreSQL's canonical all-defaults form.
if (first)
- sql = "INSERT INTO " + quoteIdentifier(schema) + "." + quoteIdentifier(table) + " DEFAULT VALUES";
+ sql = "INSERT INTO " + targetTable + " DEFAULT VALUES";
else
sql += ") VALUES (" + valsSql + ")";
return executeInternal(sql);
@@ -613,7 +633,7 @@ namespace DBModels
const std::wstring& table, const std::wstring& schema,
const TableRow& setValues, const TableRow& whereValues)
{
- std::string sql = "UPDATE " + quoteIdentifier(schema) + "." + quoteIdentifier(table) + " SET ";
+ std::string sql = "UPDATE " + qualifyTable(schema, table) + " SET ";
bool first = true;
for (auto& [col, val] : setValues)
{
@@ -640,7 +660,7 @@ namespace DBModels
const std::wstring& table, const std::wstring& schema,
const TableRow& whereValues)
{
- std::string sql = "DELETE FROM " + quoteIdentifier(schema) + "." + quoteIdentifier(table) + " WHERE ";
+ std::string sql = "DELETE FROM " + qualifyTable(schema, table) + " WHERE ";
bool first = true;
for (auto& [col, val] : whereValues)
{