wip
This commit is contained in:
parent
ebcbadfd3a
commit
0695888659
|
@ -99,6 +99,23 @@ function isSqs(table: Table): boolean {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function escapeQuotes(value: string, quoteChar = '"'): string {
|
||||||
|
return value.replace(new RegExp(quoteChar, "g"), `${quoteChar}${quoteChar}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
function wrap(value: string, quoteChar = '"'): string {
|
||||||
|
return `${quoteChar}${escapeQuotes(value, quoteChar)}${quoteChar}`
|
||||||
|
}
|
||||||
|
|
||||||
|
function stringifyArray(value: any[], quoteStyle = '"'): string {
|
||||||
|
for (let i in value) {
|
||||||
|
if (typeof value[i] === "string") {
|
||||||
|
value[i] = wrap(value[i], quoteStyle)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return `[${value.join(",")}]`
|
||||||
|
}
|
||||||
|
|
||||||
const allowEmptyRelationships: Record<SearchFilterKey, boolean> = {
|
const allowEmptyRelationships: Record<SearchFilterKey, boolean> = {
|
||||||
[BasicOperator.EQUAL]: false,
|
[BasicOperator.EQUAL]: false,
|
||||||
[BasicOperator.NOT_EQUAL]: true,
|
[BasicOperator.NOT_EQUAL]: true,
|
||||||
|
@ -194,6 +211,15 @@ class InternalBuilder {
|
||||||
return key.map(part => this.quote(part)).join(".")
|
return key.map(part => this.quote(part)).join(".")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private quotedValue(value: string): string {
|
||||||
|
const formatter = this.knexClient.formatter(this.knexClient.queryBuilder())
|
||||||
|
return formatter.wrap(value, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
private rawQuotedValue(value: string): Knex.Raw {
|
||||||
|
return this.knex.raw(this.quotedValue(value))
|
||||||
|
}
|
||||||
|
|
||||||
// Unfortuantely we cannot rely on knex's identifier escaping because it trims
|
// Unfortuantely we cannot rely on knex's identifier escaping because it trims
|
||||||
// the identifier string before escaping it, which breaks cases for us where
|
// the identifier string before escaping it, which breaks cases for us where
|
||||||
// columns that start or end with a space aren't referenced correctly anymore.
|
// columns that start or end with a space aren't referenced correctly anymore.
|
||||||
|
@ -682,25 +708,20 @@ class InternalBuilder {
|
||||||
return q
|
return q
|
||||||
}
|
}
|
||||||
|
|
||||||
function stringifyArray(value: any[], quoteStyle = '"'): string {
|
|
||||||
for (let i in value) {
|
|
||||||
if (typeof value[i] === "string") {
|
|
||||||
value[i] = `${quoteStyle}${value[i]}${quoteStyle}`
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return `[${value.join(",")}]`
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.client === SqlClient.POSTGRES) {
|
if (this.client === SqlClient.POSTGRES) {
|
||||||
iterate(mode, ArrayOperator.CONTAINS, (q, key, value) => {
|
iterate(mode, ArrayOperator.CONTAINS, (q, key, value) => {
|
||||||
const wrap = any ? "" : "'"
|
q = addModifiers(q)
|
||||||
const op = any ? "\\?| array" : "@>"
|
if (any) {
|
||||||
|
return q.whereRaw(`COALESCE(??::jsonb \\?| array??, FALSE)`, [
|
||||||
const stringifiedArray = stringifyArray(value, any ? "'" : '"')
|
this.rawQuotedIdentifier(key),
|
||||||
return addModifiers(q).whereRaw(
|
this.knex.raw(stringifyArray(value, "'")),
|
||||||
`COALESCE(??::jsonb ${op} ${wrap}${stringifiedArray}${wrap}, FALSE)`,
|
])
|
||||||
[this.rawQuotedIdentifier(key)]
|
} else {
|
||||||
)
|
return q.whereRaw(`COALESCE(??::jsonb @> '??', FALSE)`, [
|
||||||
|
this.rawQuotedIdentifier(key),
|
||||||
|
this.knex.raw(stringifyArray(value)),
|
||||||
|
])
|
||||||
|
}
|
||||||
})
|
})
|
||||||
} else if (
|
} else if (
|
||||||
this.client === SqlClient.MY_SQL ||
|
this.client === SqlClient.MY_SQL ||
|
||||||
|
@ -710,7 +731,7 @@ class InternalBuilder {
|
||||||
return addModifiers(q).whereRaw(`COALESCE(?(??, ?), FALSE)`, [
|
return addModifiers(q).whereRaw(`COALESCE(?(??, ?), FALSE)`, [
|
||||||
this.knex.raw(any ? "JSON_OVERLAPS" : "JSON_CONTAINS"),
|
this.knex.raw(any ? "JSON_OVERLAPS" : "JSON_CONTAINS"),
|
||||||
this.rawQuotedIdentifier(key),
|
this.rawQuotedIdentifier(key),
|
||||||
stringifyArray(value),
|
this.knex.raw(wrap(stringifyArray(value))),
|
||||||
])
|
])
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -793,7 +793,7 @@ describe.each([
|
||||||
})
|
})
|
||||||
|
|
||||||
const stringTypes = [FieldType.STRING, FieldType.LONGFORM] as const
|
const stringTypes = [FieldType.STRING, FieldType.LONGFORM] as const
|
||||||
describe.only.each(stringTypes)("%s", type => {
|
describe.each(stringTypes)("%s", type => {
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
tableOrViewId = await createTableOrView({
|
tableOrViewId = await createTableOrView({
|
||||||
name: { name: "name", type },
|
name: { name: "name", type },
|
||||||
|
@ -2163,7 +2163,7 @@ describe.each([
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("multi user", () => {
|
describe.only("multi user", () => {
|
||||||
let user1: User
|
let user1: User
|
||||||
let user2: User
|
let user2: User
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue