1
0
Fork 0
mirror of synced 2024-09-20 19:33:10 +12:00

Fix generic-sql.spec.ts

This commit is contained in:
Sam Rose 2024-07-18 11:40:44 +01:00
parent df765c1dc7
commit 481bf9a8b8
No known key found for this signature in database
2 changed files with 49 additions and 13 deletions

View file

@ -122,17 +122,49 @@ function generateSelectStatement(
const schema = meta.table.schema
return resource.fields.map(field => {
const [table, column, ...rest] = field.split(/\./g)
const parts = field.split(/\./g)
let table: string | undefined = undefined
let column: string | undefined = undefined
// Just a column name, e.g.: "column"
if (parts.length === 1) {
column = parts[0]
}
// A table name and a column name, e.g.: "table.column"
if (parts.length === 2) {
table = parts[0]
column = parts[1]
}
// A link doc, e.g.: "table.doc1.fieldName"
if (parts.length > 2) {
table = parts[0]
column = parts.slice(1).join(".")
}
if (!column) {
throw new Error(`Invalid field name: ${field}`)
}
const columnSchema = schema[column]
if (
client === SqlClient.POSTGRES &&
schema[column].externalType?.includes("money")
columnSchema?.externalType?.includes("money")
) {
return knex.raw(`"${table}"."${column}"::money::numeric as "${field}"`)
return knex.raw(
`${quotedIdentifier(
client,
[table, column].join(".")
)}::money::numeric as ${quote(client, field)}`
)
}
if (
client === SqlClient.MS_SQL &&
schema[column]?.type === FieldType.DATETIME &&
schema[column].timeOnly
columnSchema?.type === FieldType.DATETIME &&
columnSchema.timeOnly
) {
// Time gets returned as timestamp from mssql, not matching the expected
// HH:mm format
@ -147,12 +179,16 @@ function generateSelectStatement(
// case, we want to split it into `table`.`doc1.column` for reasons that
// aren't actually clear to me, but `table`.`doc1` breaks things with the
// sample data tests.
return knex.raw(
`${quote(client, table)}.${quote(
client,
[column, ...rest].join(".")
)} as ${quote(client, field)}`
)
if (table) {
return knex.raw(
`${quote(client, table)}.${quote(client, column)} as ${quote(
client,
field
)}`
)
} else {
return knex.raw(`${quote(client, field)} as ${quote(client, field)}`)
}
})
}

View file

@ -272,9 +272,9 @@ class MySQLIntegration extends Sql implements DatasourcePlus {
} catch (err: any) {
let readableMessage = getReadableErrorMessage(SourceName.MYSQL, err.errno)
if (readableMessage) {
throw new Error(readableMessage)
throw new Error(readableMessage, { cause: err })
} else {
throw new Error(err.message as string)
throw err
}
} finally {
if (opts?.connect && this.client) {