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

fix: return null when SELECT.one returns empty result #266

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions __tests__/lib/pg/ql.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const deploy = require('@sap/cds/lib/deploy')
cds.env.requires.db = { kind: 'postgres' }
cds.env.requires.postgres = {
dialect: 'plain',
impl: './cds-pg', // hint: not really sure as to why this is, but...
impl: './cds-pg' // hint: not really sure as to why this is, but...
}

jest.setTimeout(100000)
Expand All @@ -22,8 +22,8 @@ describe('QL to PostgreSQL', () => {
port: '5432',
database: 'beershop',
username: 'postgres',
password: 'postgres',
},
password: 'postgres'
}
}
cds.db = await cds.connect.to(this._dbProperties)
})
Expand Down Expand Up @@ -61,6 +61,13 @@ describe('QL to PostgreSQL', () => {
expect(beer).not.toHaveProperty('abv')
})

test('-> with one - no result returns null not empty array', async () => {
const { Beers } = cds.entities('csw')
const beer = await cds.run(SELECT.one(Beers).where({ name: 'does not exist' }))
expect(beer).not.toBeInstanceOf(Array)
expect(beer).toBeNull()
})

test.todo('-> with distinct')
test.todo('-> with orderBy')
test.todo('-> with groupBy')
Expand Down
17 changes: 12 additions & 5 deletions lib/pg/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ const executeInsertCQN = async (model, dbc, cqn, user) => {
async function executePlainSQL(dbc, rawSql, rawValues) {
const { sql, values } = _replacePlaceholders({
sql: rawSql,
values: rawValues,
values: rawValues
})

DEBUG && DEBUG('sql > ', sql)
Expand Down Expand Up @@ -149,11 +149,11 @@ function _cqnToSQL(model, cqn, user, isExpand = false) {
SelectBuilder: PGSelectBuilder,
ResourceBuilder: PGResourceBuilder,
ExpressionBuilder: PGExpressionBuilder,
FunctionBuilder: PGFunctionBuilder,
FunctionBuilder: PGFunctionBuilder
},
isExpand, // Passed to inform the select builder that we are dealing with an expand call
now: 'NOW ()',
user,
user
},
model,
isExpand
Expand Down Expand Up @@ -210,7 +210,14 @@ async function _executeSQLReturningRows(dbc, sql, values, isOne, postMapper, pro
DEBUG && values && values.length > 0 && DEBUG('values > ', values)

let rawResult = await dbc.query(sql, values)
const result = isOne && rawResult.rows.length > 0 ? rawResult.rows[0] : rawResult.rows
let result
if (isOne && rawResult.rows.length > 0) {
result = rawResult.rows[0]
} else if (isOne && rawResult.rows.length === 0) {
result = null
} else {
result = rawResult.rows
}
return postProcess(result, postMapper, propertyMapper, objStructMapper)
}

Expand All @@ -226,5 +233,5 @@ module.exports = {
read: executeSelectCQN,
//stream: executeSelectStreamCQN,
cqn: executeGenericCQN,
sql: executePlainSQL,
sql: executePlainSQL
}