fix(completion): avoid unnecessary MySQL identifier quotes
This commit is contained in:
2 files changed
+73
-2
No files matched your search
@@ -1,4 +1,5 @@
|
|||||||
const SIMPLE_LOWER_IDENTIFIER = /^[a-z_][a-z0-9_$]*$/;
|
const SIMPLE_LOWER_IDENTIFIER = /^[a-z_][a-z0-9_$]*$/;
|
||||||
|
const SIMPLE_MYSQL_IDENTIFIER = /^[A-Za-z_$\u0080-\uFFFF][A-Za-z0-9_$\u0080-\uFFFF]*$/u;
|
||||||
|
|
||||||
// PostgreSQL pg_get_keywords() categories R and T. GaussDB's PostgreSQL-
|
// PostgreSQL pg_get_keywords() categories R and T. GaussDB's PostgreSQL-
|
||||||
// compatible modes use the same identifier constraints for bare names.
|
// compatible modes use the same identifier constraints for bare names.
|
||||||
@@ -191,14 +192,16 @@ export function requiresPostgresIdentifierQuote(identifier: string, additionalKe
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function requiresMysqlIdentifierQuote(identifier: string, additionalKeywords?: ReadonlySet<string>): boolean {
|
export function requiresMysqlIdentifierQuote(identifier: string, additionalKeywords?: ReadonlySet<string>): boolean {
|
||||||
return requiresPostgresIdentifierQuote(identifier, additionalKeywords) || MYSQL_ONLY_RESERVED_IDENTIFIER_KEYWORDS.has(identifier);
|
if (!SIMPLE_MYSQL_IDENTIFIER.test(identifier)) return true;
|
||||||
|
const normalized = identifier.toLowerCase();
|
||||||
|
return POSTGRES_RESERVED_IDENTIFIER_KEYWORDS.has(normalized) || additionalKeywords?.has(normalized) === true || MYSQL_ONLY_RESERVED_IDENTIFIER_KEYWORDS.has(normalized);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function quoteGaussDbJdbcIdentifier(identifier: string, identifierQuote: string): string {
|
export function quoteGaussDbJdbcIdentifier(identifier: string, identifierQuote: string): string {
|
||||||
if (isExplicitlyQuotedSqlIdentifier(identifier)) return identifier;
|
if (isExplicitlyQuotedSqlIdentifier(identifier)) return identifier;
|
||||||
const quote = identifierQuote.trim();
|
const quote = identifierQuote.trim();
|
||||||
if (!quote) return identifier;
|
if (!quote) return identifier;
|
||||||
const requiresQuote = quote === "`" ? requiresMysqlIdentifierQuote(identifier) : requiresPostgresIdentifierQuote(identifier);
|
const requiresQuote = quote === "`" ? requiresPostgresIdentifierQuote(identifier) || MYSQL_ONLY_RESERVED_IDENTIFIER_KEYWORDS.has(identifier.toLowerCase()) : requiresPostgresIdentifierQuote(identifier);
|
||||||
if (!requiresQuote) return identifier;
|
if (!requiresQuote) return identifier;
|
||||||
return `${quote}${identifier.replaceAll(quote, quote + quote)}${quote}`;
|
return `${quote}${identifier.replaceAll(quote, quote + quote)}${quote}`;
|
||||||
}
|
}
|
||||||
+68
@@ -522,6 +522,63 @@ test("leaves safe MySQL table identifiers unquoted when completion inserts them"
|
|||||||
assert.equal(table?.apply, "article");
|
assert.equal(table?.apply, "article");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("leaves safe mixed-case MySQL identifiers unquoted when completion inserts them", () => {
|
||||||
|
const schemaSql = "select * from Sales";
|
||||||
|
const schemaItems = buildSqlCompletionItems(schemaSql, schemaSql.length, {
|
||||||
|
tables: [],
|
||||||
|
columnsByTable: new Map(),
|
||||||
|
schemas: ["SalesDB"],
|
||||||
|
dialect: "mysql",
|
||||||
|
});
|
||||||
|
assert.equal(schemaItems.find((item) => item.type === "schema" && item.label === "SalesDB")?.apply, "SalesDB.");
|
||||||
|
|
||||||
|
const tableSql = "select * from OrderI";
|
||||||
|
const tableItems = buildSqlCompletionItems(tableSql, tableSql.length, {
|
||||||
|
tables: [{ name: "OrderItems", schema: "SalesDB", type: "table" }],
|
||||||
|
columnsByTable: new Map(),
|
||||||
|
dialect: "mysql",
|
||||||
|
});
|
||||||
|
assert.equal(tableItems.find((item) => item.type === "table" && item.label === "OrderItems")?.apply, "OrderItems");
|
||||||
|
|
||||||
|
const columnSql = "select from OrderItems";
|
||||||
|
const columnItems = buildSqlCompletionItems(columnSql, "select ".length, {
|
||||||
|
tables: [{ name: "OrderItems", schema: "SalesDB", type: "table" }],
|
||||||
|
columnsByTable: new Map([
|
||||||
|
[
|
||||||
|
"SalesDB.OrderItems",
|
||||||
|
[
|
||||||
|
{ name: "MixedCase", table: "OrderItems", schema: "SalesDB", dataType: "int" },
|
||||||
|
{ name: "UPPER_COL", table: "OrderItems", schema: "SalesDB", dataType: "int" },
|
||||||
|
],
|
||||||
|
],
|
||||||
|
]),
|
||||||
|
dialect: "mysql",
|
||||||
|
});
|
||||||
|
assert.equal(columnItems.find((item) => item.type === "column" && item.label === "MixedCase")?.apply, "MixedCase");
|
||||||
|
assert.equal(columnItems.find((item) => item.type === "column" && item.label === "UPPER_COL")?.apply, "UPPER_COL");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("does not reuse MySQL backticks for Caché JDBC completion identifiers", () => {
|
||||||
|
const schemaSql = "select top 100 * from SQL";
|
||||||
|
const schemaItems = buildSqlCompletionItems(schemaSql, schemaSql.length, {
|
||||||
|
tables: [],
|
||||||
|
columnsByTable: new Map(),
|
||||||
|
schemas: ["SQLUser"],
|
||||||
|
databaseType: "iris",
|
||||||
|
dialect: "mysql",
|
||||||
|
});
|
||||||
|
assert.equal(schemaItems.find((item) => item.type === "schema" && item.label === "SQLUser")?.apply, "SQLUser.");
|
||||||
|
|
||||||
|
const tableSql = "select top 100 * from SQLUser.PA";
|
||||||
|
const tableItems = buildSqlCompletionItems(tableSql, tableSql.length, {
|
||||||
|
tables: [{ name: "PA_Adm", schema: "SQLUser", type: "table" }],
|
||||||
|
columnsByTable: new Map(),
|
||||||
|
databaseType: "iris",
|
||||||
|
dialect: "mysql",
|
||||||
|
});
|
||||||
|
assert.equal(tableItems.find((item) => item.type === "table" && item.label === "PA_Adm")?.apply, "PA_Adm");
|
||||||
|
});
|
||||||
|
|
||||||
test("quotes MySQL reserved-word column identifiers with backticks when completion inserts them", () => {
|
test("quotes MySQL reserved-word column identifiers with backticks when completion inserts them", () => {
|
||||||
const reservedColumns = ["order", "do", "returning", "ilike", "window", "true"];
|
const reservedColumns = ["order", "do", "returning", "ilike", "window", "true"];
|
||||||
const reservedColumnsByTable = new Map<string, SqlCompletionColumn[]>([["public.bookings", reservedColumns.map((name) => ({ name, table: "bookings", schema: "public", dataType: "text" }))]]);
|
const reservedColumnsByTable = new Map<string, SqlCompletionColumn[]>([["public.bookings", reservedColumns.map((name) => ({ name, table: "bookings", schema: "public", dataType: "text" }))]]);
|
||||||
@@ -538,6 +595,17 @@ test("quotes MySQL reserved-word column identifiers with backticks when completi
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("quotes MySQL reserved identifiers case-insensitively", () => {
|
||||||
|
const sql = "select Ord from bookings";
|
||||||
|
const items = buildSqlCompletionItems(sql, "select Ord".length, {
|
||||||
|
tables: [{ name: "bookings", schema: "public", type: "table" }],
|
||||||
|
columnsByTable: new Map([["public.bookings", [{ name: "Order", table: "bookings", schema: "public", dataType: "text" }]]]),
|
||||||
|
dialect: "mysql",
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.equal(items.find((item) => item.type === "column" && item.label === "Order")?.apply, "`Order`");
|
||||||
|
});
|
||||||
|
|
||||||
test("quotes MySQL-only reserved identifiers in schema, table, column, and join completions", () => {
|
test("quotes MySQL-only reserved identifiers in schema, table, column, and join completions", () => {
|
||||||
const schemaSql = "select * from data";
|
const schemaSql = "select * from data";
|
||||||
const schemaItems = buildSqlCompletionItems(schemaSql, schemaSql.length, {
|
const schemaItems = buildSqlCompletionItems(schemaSql, schemaSql.length, {
|
||||||
|
|||||||
Reference in new issue
Block a user