Skip to content

Commit 10bf73e

Browse files
committed
fix(editor): unquote backtick aliases and keep aliased CTE columns in autocomplete
1 parent c63a373 commit 10bf73e

2 files changed

Lines changed: 6 additions & 7 deletions

File tree

TablePro/Core/Autocomplete/DerivedTableParser.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,8 @@ internal struct DerivedTableParser {
263263
let c = ns.character(at: index)
264264
if c == Self.backtick || c == Self.doubleQuote {
265265
let end = matchingQuote(in: ns, from: index, limit: limit, quote: c)
266-
guard end > index + 1 else { return nil }
267-
return ns.substring(with: NSRange(location: index + 1, length: end - index - 1))
266+
guard end > index + 2 else { return nil }
267+
return ns.substring(with: NSRange(location: index + 1, length: end - index - 2))
268268
}
269269
if c == Self.openBracket {
270270
var j = index + 1

TablePro/Core/Autocomplete/SQLContextAnalyzer.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -790,8 +790,9 @@ final class SQLContextAnalyzer {
790790
}
791791

792792
/// Attach derived-table columns to references and add any derived table or
793-
/// CTE not already in scope. A derived alias wins over a plain reference of
794-
/// the same identifier (e.g. a CTE used directly in a FROM clause).
793+
/// CTE not already in scope. Columns attach to a matching reference (e.g. a
794+
/// CTE aliased in a FROM clause), and the derived table or CTE is still
795+
/// registered under its own name so it resolves by either identifier.
795796
private func mergeDerivedTables(
796797
_ derivedTables: [DerivedTable],
797798
cteNames: [String],
@@ -802,20 +803,18 @@ final class SQLContextAnalyzer {
802803
uniquingKeysWith: { first, _ in first }
803804
)
804805

805-
var consumed = Set<String>()
806806
for index in references.indices {
807807
let ref = references[index]
808808
for key in [ref.tableName.lowercased(), ref.identifier.lowercased()] {
809809
guard let columns = derivedColumnsByAlias[key] else { continue }
810-
consumed.insert(key)
811810
references[index] = TableReference(
812811
tableName: ref.tableName, alias: ref.alias, schema: ref.schema, derivedColumns: columns
813812
)
814813
break
815814
}
816815
}
817816

818-
var present = Set(references.map { $0.identifier.lowercased() }).union(consumed)
817+
var present = Set(references.map { $0.identifier.lowercased() })
819818
for derived in derivedTables where present.insert(derived.alias.lowercased()).inserted {
820819
references.append(
821820
TableReference(tableName: derived.alias, alias: derived.alias, derivedColumns: derived.columns)

0 commit comments

Comments
 (0)