Deletion support for returning rows

This commit is contained in:
Rory Powell 2021-11-26 16:50:15 +00:00
parent 115fc08aa4
commit f75928a9c9
1 changed files with 16 additions and 4 deletions

View File

@ -414,7 +414,7 @@ module OracleModule {
} }
async query(json: QueryJson) { async query(json: QueryJson) {
const operation = this._operation(json).toLowerCase() const operation = this._operation(json)
const input = this._query(json, { disableReturning: true }) const input = this._query(json, { disableReturning: true })
if (Array.isArray(input)) { if (Array.isArray(input)) {
const responses = [] const responses = []
@ -423,22 +423,34 @@ module OracleModule {
} }
return responses return responses
} else { } else {
// read the row to be deleted up front for the return
let deletedRows
if (operation === Operation.DELETE) {
const queryFn = (query: any) => this.internalQuery(query)
deletedRows = await this.getReturningRow(queryFn, json)
}
// run the query
const response = await this.internalQuery(input) const response = await this.internalQuery(input)
if (response.rows?.length) {
// get the results or return the created / updated / deleted row
if (deletedRows?.rows?.length) {
return deletedRows.rows
} else if (response.rows?.length) {
return response.rows return response.rows
} else { } else {
// get the last row that was updated // get the last row that was updated
if ( if (
response.lastRowid && response.lastRowid &&
json.endpoint?.entityId && json.endpoint?.entityId &&
operation.toUpperCase() !== Operation.DELETE operation !== Operation.DELETE
) { ) {
const lastRow = await this.internalQuery({ const lastRow = await this.internalQuery({
sql: `SELECT * FROM \"${json.endpoint.entityId}\" WHERE ROWID = '${response.lastRowid}'`, sql: `SELECT * FROM \"${json.endpoint.entityId}\" WHERE ROWID = '${response.lastRowid}'`,
}) })
return lastRow.rows return lastRow.rows
} else { } else {
return [{ [operation]: true }] return [{ [ operation.toLowerCase() ]: true }]
} }
} }
} }