Skip to content
This repository has been archived by the owner on Jul 14, 2023. It is now read-only.

Commit

Permalink
Merge pull request #280 from rbhuva/main
Browse files Browse the repository at this point in the history
fix: return affected row on update ql
  • Loading branch information
sebastianesch committed Mar 19, 2022
2 parents bbdef88 + e423555 commit 90956ac
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
13 changes: 13 additions & 0 deletions __tests__/lib/pg/ql.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,17 @@ describe('QL to PostgreSQL', () => {
expect(beers[0].modifiedAt).toMatch(timestampRegex)
})
})

describe('UPDATE', () => {
beforeEach(async () => {
await deploy(this._model, {}).to(this._dbProperties)
})
test('-> Get affected rows ', async () => {
const { Beers } = cds.entities('csw')
const affectedRows = await cds.run(
UPDATE(Beers).set({ name: 'TEST' }).where({ ID: '9e1704e3-6fd0-4a5d-bfb1-13ac47f7976b' })
)
expect(affectedRows).toStrictEqual(1)
})
})
})
4 changes: 3 additions & 1 deletion lib/pg/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,9 @@ async function _executeSQLReturningRows(dbc, sql, values, isOne, postMapper, pro

let rawResult = await dbc.query(sql, values)
let result
if (isOne && rawResult.rows.length > 0) {
if (rawResult.command === 'UPDATE') {
result = rawResult.rowCount
} else if (isOne && rawResult.rows.length > 0) {
result = rawResult.rows[0]
} else if (isOne && rawResult.rows.length === 0) {
result = null
Expand Down

0 comments on commit 90956ac

Please sign in to comment.