Skip to content

Commit

Permalink
Allow period chars in table names to be escaped
Browse files Browse the repository at this point in the history
  • Loading branch information
marijnh committed Jul 3, 2023
1 parent 95e773c commit 1735b82
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/complete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ export function completeFromSchema(schema: {[table: string]: readonly (string |
let idQuote = dialect?.spec.identifierQuotes?.[0] || '"'
let defaultSchema = top.child(defaultSchemaName || "", idQuote)
for (let table in schema) {
let parts = table.split("."), base = parts.length == 1 ? defaultSchema : top
for (let part of parts) base = base.child(part, idQuote)
let parts = table.split(/(?<!\\)\./), base = parts.length == 1 ? defaultSchema : top
for (let part of parts) base = base.child(part.replace(/\\\./, "."), idQuote)
for (let option of schema[table]) if (option)
base.list.push(typeof option == "string" ? nameCompletion(option, "property", idQuote) : option)
}
Expand Down
16 changes: 11 additions & 5 deletions test/test-complete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function get(doc: string, conf: SQLConfig & {explicit?: boolean} = {}) {
function str(result: CompletionResult | null) {
return !result ? "" : result.options.slice()
.sort((a, b) => (b.boost || 0) - (a.boost || 0) || (a.label < b.label ? -1 : 1))
.map(o => o.label)
.map(o => o.apply || o.label)
.join(", ")
}

Expand Down Expand Up @@ -156,10 +156,10 @@ describe("SQL completion", () => {
})

it("adds identifiers for non-word completions", () => {
ist(get("foo.b|", {schema: {foo: ["b c", "b-c", "bup"]}, dialect: PostgreSQL})!
.options.map(o => o.apply || o.label).join(), '"b c","b-c",bup')
ist(get("foo.b|", {schema: {foo: ["b c", "b-c", "bup"]}, dialect: MySQL})!
.options.map(o => o.apply || o.label).join(), '`b c`,`b-c`,bup')
ist(str(get("foo.b|", {schema: {foo: ["b c", "b-c", "bup"]}, dialect: PostgreSQL})),
'"b c", "b-c", bup')
ist(str(get("foo.b|", {schema: {foo: ["b c", "b-c", "bup"]}, dialect: MySQL})),
'`b c`, `b-c`, bup')
})

it("supports nesting more than two deep", () => {
Expand All @@ -169,4 +169,10 @@ describe("SQL completion", () => {
ist(str(get("one.two.|", s)), "three")
ist(str(get("one.two.three.|", s)), "four")
})

it("supports escaped dots in table names", () => {
let s = {schema: {"db\\.conf": ["abc"]}}
ist(str(get("db|", s)), '"db.conf"')
ist(str(get('"db.conf".|', s)), "abc")
})
})

0 comments on commit 1735b82

Please sign in to comment.