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 const schema = meta.table.schema
return resource.fields.map(field => { 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 ( if (
client === SqlClient.POSTGRES && 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 ( if (
client === SqlClient.MS_SQL && client === SqlClient.MS_SQL &&
schema[column]?.type === FieldType.DATETIME && columnSchema?.type === FieldType.DATETIME &&
schema[column].timeOnly columnSchema.timeOnly
) { ) {
// Time gets returned as timestamp from mssql, not matching the expected // Time gets returned as timestamp from mssql, not matching the expected
// HH:mm format // HH:mm format
@ -147,12 +179,16 @@ function generateSelectStatement(
// case, we want to split it into `table`.`doc1.column` for reasons that // 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 // aren't actually clear to me, but `table`.`doc1` breaks things with the
// sample data tests. // sample data tests.
return knex.raw( if (table) {
`${quote(client, table)}.${quote( return knex.raw(
client, `${quote(client, table)}.${quote(client, column)} as ${quote(
[column, ...rest].join(".") client,
)} as ${quote(client, field)}` 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) { } catch (err: any) {
let readableMessage = getReadableErrorMessage(SourceName.MYSQL, err.errno) let readableMessage = getReadableErrorMessage(SourceName.MYSQL, err.errno)
if (readableMessage) { if (readableMessage) {
throw new Error(readableMessage) throw new Error(readableMessage, { cause: err })
} else { } else {
throw new Error(err.message as string) throw err
} }
} finally { } finally {
if (opts?.connect && this.client) { if (opts?.connect && this.client) {