fix(grid): support Unicode aliases in editable queries
This commit is contained in:
5 files changed
+64
-7
No files matched your search
Generated
+1
@@ -1747,6 +1747,7 @@ dependencies = [
|
|||||||
"tokio-util",
|
"tokio-util",
|
||||||
"tracing",
|
"tracing",
|
||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
|
"unicode-ident",
|
||||||
"unicode-width",
|
"unicode-width",
|
||||||
"utoipa",
|
"utoipa",
|
||||||
"uuid",
|
"uuid",
|
||||||
|
|||||||
@@ -246,7 +246,7 @@ function parseStarSelectColumn(col: string, sources?: EditableQuerySource[]): Ed
|
|||||||
expression: col,
|
expression: col,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
const starMatch = col.match(/^((?:[A-Za-z_][\w$]*|"[^"]+"|`[^`]+`|\[[^\]]+\]))\s*\.\s*\*$/);
|
const starMatch = col.match(/^((?:[\p{ID_Start}_][\p{ID_Continue}$]*|"[^"]+"|`[^`]+`|\[[^\]]+\]))\s*\.\s*\*$/u);
|
||||||
if (!starMatch) return null;
|
if (!starMatch) return null;
|
||||||
const qualifier = readIdentifier(starMatch[1]!, 0);
|
const qualifier = readIdentifier(starMatch[1]!, 0);
|
||||||
if (!qualifier) return null;
|
if (!qualifier) return null;
|
||||||
@@ -272,7 +272,7 @@ function parseComputedSelectColumn(col: string): EditableQueryColumn | null {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function parseExpressionAlias(col: string): { expression: string; resultName: string } | null {
|
function parseExpressionAlias(col: string): { expression: string; resultName: string } | null {
|
||||||
const asMatch = col.match(/\bAS\s+((?:"[^"]+")|(?:`[^`]+`)|(?:\[[^\]]+\])|(?:[A-Za-z_][\w$]*))\s*$/i);
|
const asMatch = col.match(/\bAS\s+((?:"[^"]+")|(?:`[^`]+`)|(?:\[[^\]]+\])|(?:[\p{ID_Start}_][\p{ID_Continue}$]*))\s*$/iu);
|
||||||
if (asMatch?.index === undefined) return null;
|
if (asMatch?.index === undefined) return null;
|
||||||
const alias = readIdentifier(asMatch[1], 0);
|
const alias = readIdentifier(asMatch[1], 0);
|
||||||
if (!alias || alias.end !== asMatch[1].length) return null;
|
if (!alias || alias.end !== asMatch[1].length) return null;
|
||||||
@@ -426,7 +426,7 @@ function readIdentifier(text: string, start: number): { value: string; quoted: b
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
const match = text.slice(pos).match(/^[A-Za-z_][\w$]*/);
|
const match = text.slice(pos).match(/^[\p{ID_Start}_][\p{ID_Continue}$]*/u);
|
||||||
return match ? { value: match[0], quoted: false, end: pos + match[0].length } : null;
|
return match ? { value: match[0], quoted: false, end: pos + match[0].length } : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -484,7 +484,7 @@ function findTopLevelKeyword(sql: string, keyword: string, start: number): numbe
|
|||||||
}
|
}
|
||||||
|
|
||||||
function isIdentifierChar(ch: string): boolean {
|
function isIdentifierChar(ch: string): boolean {
|
||||||
return /[\w$]/.test(ch);
|
return /[\p{ID_Continue}$]/u.test(ch);
|
||||||
}
|
}
|
||||||
|
|
||||||
function escapeRegExp(value: string): string {
|
function escapeRegExp(value: string): string {
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ serde = { version = "1.0", features = ["derive"] }
|
|||||||
serde_json = { version = "1.0", features = ["arbitrary_precision", "preserve_order"] }
|
serde_json = { version = "1.0", features = ["arbitrary_precision", "preserve_order"] }
|
||||||
utoipa = { version = "5.5", optional = true }
|
utoipa = { version = "5.5", optional = true }
|
||||||
unicode-width = "0.2"
|
unicode-width = "0.2"
|
||||||
|
unicode-ident = "1"
|
||||||
serde_yaml_ng = "0.10"
|
serde_yaml_ng = "0.10"
|
||||||
json5 = "0.4"
|
json5 = "0.4"
|
||||||
regex = "1"
|
regex = "1"
|
||||||
|
|||||||
@@ -720,12 +720,12 @@ fn read_identifier(text: &str, start: usize) -> Option<Identifier> {
|
|||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
if !(first.is_ascii_alphabetic() || first == '_') {
|
if !is_identifier_start(first) {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
let mut end = pos + first.len_utf8();
|
let mut end = pos + first.len_utf8();
|
||||||
for (offset, ch) in text[end..].char_indices() {
|
for (offset, ch) in text[end..].char_indices() {
|
||||||
if !(ch.is_ascii_alphanumeric() || ch == '_' || ch == '$') {
|
if !is_identifier_char(ch) {
|
||||||
return Some(Identifier { value: text[pos..end + offset].to_string(), quoted: false, end: end + offset });
|
return Some(Identifier { value: text[pos..end + offset].to_string(), quoted: false, end: end + offset });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -846,8 +846,12 @@ fn previous_char(text: &str, index: usize) -> Option<char> {
|
|||||||
text[..index].chars().next_back()
|
text[..index].chars().next_back()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_identifier_start(ch: char) -> bool {
|
||||||
|
ch == '_' || unicode_ident::is_xid_start(ch)
|
||||||
|
}
|
||||||
|
|
||||||
fn is_identifier_char(ch: char) -> bool {
|
fn is_identifier_char(ch: char) -> bool {
|
||||||
ch.is_ascii_alphanumeric() || ch == '_' || ch == '$'
|
ch == '$' || unicode_ident::is_xid_continue(ch)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -1142,6 +1146,35 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn accepts_unquoted_unicode_aliases_in_mysql_and_sql_server_queries() {
|
||||||
|
for sql in [
|
||||||
|
"SELECT Guid, FDeleted AS 禁用, IsAuditing AS 审核 FROM xy.dbo.GL_CUSTOM WHERE TJBH=\n24049",
|
||||||
|
"SELECT Guid, FDeleted AS 禁用, IsAuditing AS 审核 FROM xy.GL_CUSTOM WHERE TJBH=24049",
|
||||||
|
] {
|
||||||
|
let result = analyze_editable_query_editability(sql);
|
||||||
|
|
||||||
|
assert!(result.editable, "{sql}");
|
||||||
|
assert_eq!(
|
||||||
|
result.analysis.unwrap().columns,
|
||||||
|
vec![
|
||||||
|
column(Some("Guid"), false, None, None, "Guid", "Guid"),
|
||||||
|
column(Some("FDeleted"), false, None, None, "禁用", "FDeleted"),
|
||||||
|
column(Some("IsAuditing"), false, None, None, "审核", "IsAuditing"),
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let computed = analyze_editable_query_editability(
|
||||||
|
"SELECT Guid, FDeleted AS 禁用, CASE WHEN IsAuditing = 1 THEN 1 ELSE 0 END AS 审核状态 FROM xy.dbo.GL_CUSTOM",
|
||||||
|
);
|
||||||
|
assert!(computed.editable);
|
||||||
|
assert_eq!(
|
||||||
|
computed.analysis.unwrap().columns[2],
|
||||||
|
column(None, false, None, None, "审核状态", "CASE WHEN IsAuditing = 1 THEN 1 ELSE 0 END",)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn serializes_reason_values_like_frontend_union() {
|
fn serializes_reason_values_like_frontend_union() {
|
||||||
let json = serde_json::to_value(not_editable(QueryEditabilityReason::SetOperation)).unwrap();
|
let json = serde_json::to_value(not_editable(QueryEditabilityReason::SetOperation)).unwrap();
|
||||||
|
|||||||
+22
@@ -202,6 +202,28 @@ test("accepts aliased primary key source columns for row identity", () => {
|
|||||||
assert.equal(allPrimaryKeysPresent(["id"], ["id", "name"], analyzeEditableQuery("select id, name from users")!), true);
|
assert.equal(allPrimaryKeysPresent(["id"], ["id", "name"], analyzeEditableQuery("select id, name from users")!), true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("accepts unquoted Unicode aliases in editable MySQL and SQL Server queries", () => {
|
||||||
|
for (const sql of ["SELECT Guid, FDeleted AS 禁用, IsAuditing AS 审核 FROM xy.dbo.GL_CUSTOM WHERE TJBH=\n24049", "SELECT Guid, FDeleted AS 禁用, IsAuditing AS 审核 FROM xy.GL_CUSTOM WHERE TJBH=24049"]) {
|
||||||
|
const result = analyzeEditableQueryEditability(sql);
|
||||||
|
|
||||||
|
assert.equal(result.editable, true, sql);
|
||||||
|
assert.deepEqual(result.analysis.columns, [
|
||||||
|
{ sourceName: "Guid", sourceNameQuoted: false, resultName: "Guid", expression: "Guid" },
|
||||||
|
{ sourceName: "FDeleted", sourceNameQuoted: false, resultName: "禁用", expression: "FDeleted" },
|
||||||
|
{ sourceName: "IsAuditing", sourceNameQuoted: false, resultName: "审核", expression: "IsAuditing" },
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
const computed = analyzeEditableQueryEditability("SELECT Guid, FDeleted AS 禁用, CASE WHEN IsAuditing = 1 THEN 1 ELSE 0 END AS 审核状态 FROM xy.dbo.GL_CUSTOM");
|
||||||
|
assert.equal(computed.editable, true);
|
||||||
|
assert.deepEqual(computed.analysis.columns[2], {
|
||||||
|
sourceName: undefined,
|
||||||
|
sourceNameQuoted: false,
|
||||||
|
resultName: "审核状态",
|
||||||
|
expression: "CASE WHEN IsAuditing = 1 THEN 1 ELSE 0 END",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
test("resolves metadata columns with dialect and quote aware identifier rules", () => {
|
test("resolves metadata columns with dialect and quote aware identifier rules", () => {
|
||||||
const postgresColumns = ["id", "ID", "name"];
|
const postgresColumns = ["id", "ID", "name"];
|
||||||
assert.equal(resolveMetadataColumnName("postgres", "ID", false, postgresColumns), "id");
|
assert.equal(resolveMetadataColumnName("postgres", "ID", false, postgresColumns), "id");
|
||||||
|
|||||||
Reference in new issue
Block a user