1
0
Fork 0
mirror of synced 2024-06-28 19:10:33 +12:00

Add returning

This commit is contained in:
Rory Powell 2021-11-23 13:27:38 +00:00
parent 1a0c66beb6
commit 7135599295
3 changed files with 29 additions and 16 deletions

View file

@ -416,6 +416,8 @@ class SqlQueryBuilder extends SqlTableQueryBuilder {
id = results?.[0].id
} else if (sqlClient === SqlClients.MY_SQL) {
id = results?.insertId
} else if (sqlClient === SqlClients.ORACLE) {
id = response.outBinds[0][0]
}
row = processFn(
await this.getReturningRow(queryFn, this.checkLookupKeys(id, json))

View file

@ -4,12 +4,14 @@ import {
QueryTypes,
SqlQuery,
QueryJson,
Operation,
} from "../definitions/datasource"
import {
finaliseExternalTables,
getSqlQuery,
buildExternalTableId,
convertSqlType,
SqlClients,
} from "./utils"
import oracledb, {
ExecuteOptions,
@ -168,7 +170,7 @@ module OracleModule {
OR cons.status IS NULL)
`
constructor(config: OracleConfig) {
super("oracledb")
super(SqlClients.ORACLE)
this.config = config
}
@ -346,13 +348,29 @@ module OracleModule {
this.schemaErrors = final.errors
}
private async internalQuery<T>(query: SqlQuery): Promise<Result<T>> {
/**
* Knex default returning behaviour does not work with oracle
* Manually add the behaviour for the return column
*/
private addReturning(query: SqlQuery, bindings: BindParameters, returnColumn: string) {
if (bindings instanceof Array) {
bindings.push({ dir: oracledb.BIND_OUT })
query.sql = query.sql + ` returning \"${returnColumn}\" into :${bindings.length}`
}
}
private async internalQuery<T>(query: SqlQuery, returnColum?: string, operation?: string): Promise<Result<T>> {
let connection
try {
connection = await this.getConnection()
const options: ExecuteOptions = { autoCommit: true }
const bindings: BindParameters = query.bindings || []
if (returnColum && (operation === Operation.CREATE || operation === Operation.UPDATE)) {
this.addReturning(query, bindings, returnColum)
}
const result: Result<T> = await connection.execute<T>(
query.sql,
bindings,
@ -411,20 +429,12 @@ module OracleModule {
}
async query(json: QueryJson) {
const operation = this._operation(json).toLowerCase()
const input = this._query(json, { disableReturning: true })
if (Array.isArray(input)) {
const responses = []
for (let query of input) {
responses.push(await this.internalQuery(query))
}
return responses
} else {
const response = await this.internalQuery(input)
return response.rows && response.rows.length
? response.rows
: [{ [operation]: true }]
}
const primaryKeys = json.meta!.table!.primary
const primaryKey = primaryKeys ? primaryKeys[0] : undefined
const queryFn = (query: any, operation: string) => this.internalQuery(query, primaryKey, operation)
const processFn = (response: any) => response.rows ? response.rows : []
const output = await this.queryWithReturning(json, queryFn, processFn)
return output
}
}

View file

@ -42,6 +42,7 @@ export enum SqlClients {
MS_SQL = "mssql",
POSTGRES = "pg",
MY_SQL = "mysql",
ORACLE = "oracledb"
}
export function isExternalTable(tableId: string) {