Updating select statement generation.
This commit is contained in:
parent
888c4214bd
commit
f7d9b8a9b3
|
@ -94,6 +94,23 @@ class InternalBuilder {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// states the various situations in which we need a full mapped select statement
|
||||||
|
private readonly SPECIAL_SELECT_CASES = {
|
||||||
|
POSTGRES_MONEY: (field: FieldSchema | undefined) => {
|
||||||
|
return (
|
||||||
|
this.client === SqlClient.POSTGRES &&
|
||||||
|
field?.externalType?.includes("money")
|
||||||
|
)
|
||||||
|
},
|
||||||
|
MSSQL_DATES: (field: FieldSchema | undefined) => {
|
||||||
|
return (
|
||||||
|
this.client === SqlClient.MS_SQL &&
|
||||||
|
field?.type === FieldType.DATETIME &&
|
||||||
|
field.timeOnly
|
||||||
|
)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
get table(): Table {
|
get table(): Table {
|
||||||
return this.query.meta.table
|
return this.query.meta.table
|
||||||
}
|
}
|
||||||
|
@ -127,8 +144,20 @@ class InternalBuilder {
|
||||||
.join(".")
|
.join(".")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private isFullSelectStatementRequired(): boolean {
|
||||||
|
const { meta } = this.query
|
||||||
|
for (let column of Object.values(meta.table.schema)) {
|
||||||
|
if (this.SPECIAL_SELECT_CASES.POSTGRES_MONEY(column)) {
|
||||||
|
return true
|
||||||
|
} else if (this.SPECIAL_SELECT_CASES.MSSQL_DATES(column)) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
private generateSelectStatement(): (string | Knex.Raw)[] | "*" {
|
private generateSelectStatement(): (string | Knex.Raw)[] | "*" {
|
||||||
const { endpoint, resource, tableAliases } = this.query
|
const { meta, endpoint, resource, tableAliases } = this.query
|
||||||
|
|
||||||
if (!resource || !resource.fields || resource.fields.length === 0) {
|
if (!resource || !resource.fields || resource.fields.length === 0) {
|
||||||
return "*"
|
return "*"
|
||||||
|
@ -137,75 +166,48 @@ class InternalBuilder {
|
||||||
const alias = tableAliases?.[endpoint.entityId]
|
const alias = tableAliases?.[endpoint.entityId]
|
||||||
? tableAliases?.[endpoint.entityId]
|
? tableAliases?.[endpoint.entityId]
|
||||||
: endpoint.entityId
|
: endpoint.entityId
|
||||||
|
const schema = meta.table.schema
|
||||||
|
if (!this.isFullSelectStatementRequired()) {
|
||||||
return [this.knex.raw(`${this.quote(alias)}.*`)]
|
return [this.knex.raw(`${this.quote(alias)}.*`)]
|
||||||
//
|
}
|
||||||
//
|
// get just the fields for this table
|
||||||
// const schema = meta.table.schema
|
return resource.fields
|
||||||
// return resource.fields.map(field => {
|
.map(field => {
|
||||||
// const parts = field.split(/\./g)
|
const parts = field.split(/\./g)
|
||||||
// let table: string | undefined = undefined
|
let table: string | undefined = undefined
|
||||||
// let column: string | undefined = undefined
|
let column = parts[0]
|
||||||
//
|
|
||||||
// // Just a column name, e.g.: "column"
|
// Just a column name, e.g.: "column"
|
||||||
// if (parts.length === 1) {
|
if (parts.length > 1) {
|
||||||
// column = parts[0]
|
table = parts[0]
|
||||||
// }
|
column = parts.slice(1).join(".")
|
||||||
//
|
}
|
||||||
// // A table name and a column name, e.g.: "table.column"
|
|
||||||
// if (parts.length === 2) {
|
return { table, column, field }
|
||||||
// table = parts[0]
|
})
|
||||||
// column = parts[1]
|
.filter(({ table }) => !table || table === alias)
|
||||||
// }
|
.map(({ table, column, field }) => {
|
||||||
//
|
const columnSchema = schema[column]
|
||||||
// // A link doc, e.g.: "table.doc1.fieldName"
|
|
||||||
// if (parts.length > 2) {
|
if (this.SPECIAL_SELECT_CASES.POSTGRES_MONEY(columnSchema)) {
|
||||||
// table = parts[0]
|
return this.knex.raw(
|
||||||
// column = parts.slice(1).join(".")
|
`${this.quotedIdentifier(
|
||||||
// }
|
[table, column].join(".")
|
||||||
//
|
)}::money::numeric as ${this.quote(field)}`
|
||||||
// if (!column) {
|
)
|
||||||
// throw new Error(`Invalid field name: ${field}`)
|
}
|
||||||
// }
|
|
||||||
//
|
if (this.SPECIAL_SELECT_CASES.MSSQL_DATES(columnSchema)) {
|
||||||
// const columnSchema = schema[column]
|
// Time gets returned as timestamp from mssql, not matching the expected
|
||||||
//
|
// HH:mm format
|
||||||
// if (
|
return this.knex.raw(`CONVERT(varchar, ${field}, 108) as "${field}"`)
|
||||||
// this.client === SqlClient.POSTGRES &&
|
}
|
||||||
// columnSchema?.externalType?.includes("money")
|
|
||||||
// ) {
|
const quoted = table
|
||||||
// return this.knex.raw(
|
? `${this.quote(table)}.${this.quote(column)}`
|
||||||
// `${this.quotedIdentifier(
|
: this.quote(field)
|
||||||
// [table, column].join(".")
|
return this.knex.raw(quoted)
|
||||||
// )}::money::numeric as ${this.quote(field)}`
|
})
|
||||||
// )
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// if (
|
|
||||||
// this.client === SqlClient.MS_SQL &&
|
|
||||||
// columnSchema?.type === FieldType.DATETIME &&
|
|
||||||
// columnSchema.timeOnly
|
|
||||||
// ) {
|
|
||||||
// // Time gets returned as timestamp from mssql, not matching the expected
|
|
||||||
// // HH:mm format
|
|
||||||
// return this.knex.raw(`CONVERT(varchar, ${field}, 108) as "${field}"`)
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // There's at least two edge cases being handled in the expression below.
|
|
||||||
// // 1. The column name could start/end with a space, and in that case we
|
|
||||||
// // want to preseve that space.
|
|
||||||
// // 2. Almost all column names are specified in the form table.column, except
|
|
||||||
// // in the case of relationships, where it's table.doc1.column. In 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
|
|
||||||
// // sample data tests.
|
|
||||||
// if (table) {
|
|
||||||
// return this.knex.raw(
|
|
||||||
// `${this.quote(table)}.${this.quote(column)} as ${this.quote(field)}`
|
|
||||||
// )
|
|
||||||
// } else {
|
|
||||||
// return this.knex.raw(`${this.quote(field)} as ${this.quote(field)}`)
|
|
||||||
// }
|
|
||||||
// })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// OracleDB can't use character-large-objects (CLOBs) in WHERE clauses,
|
// OracleDB can't use character-large-objects (CLOBs) in WHERE clauses,
|
||||||
|
|
Loading…
Reference in New Issue