diff --git a/db-service/lib/cqn2sql.js b/db-service/lib/cqn2sql.js index 77d680f1a..ac41b0236 100644 --- a/db-service/lib/cqn2sql.js +++ b/db-service/lib/cqn2sql.js @@ -423,8 +423,8 @@ class CQN2SQLRenderer { ? c => this.expr(c) + (c.element?.[this.class._localized] ? ' COLLATE NOCASE' : '') + - (c.sort === 'desc' || c.sort === -1 ? ' DESC' : ' ASC') - : c => this.expr(c) + (c.sort === 'desc' || c.sort === -1 ? ' DESC' : ' ASC'), + (c.sort?.toLowerCase() === 'desc' || c.sort === -1 ? ' DESC' : ' ASC') + : c => this.expr(c) + (c.sort?.toLowerCase() === 'desc' || c.sort === -1 ? ' DESC' : ' ASC'), ) } diff --git a/hana/lib/HANAService.js b/hana/lib/HANAService.js index 7d7f57cfe..3a8ba7444 100644 --- a/hana/lib/HANAService.js +++ b/hana/lib/HANAService.js @@ -791,8 +791,8 @@ SELECT ${mixing} FROM JSON_TABLE(SRC.JSON, '$' COLUMNS(${extraction})) AS NEW LE ? ` COLLATE ${collations[this.context.locale] || collations[this.context.locale.split('_')[0]] || collations[''] }` : '') + - (c.sort === 'desc' || c.sort === -1 ? ' DESC' : ' ASC') - : c => this.expr(c) + (c.sort === 'desc' || c.sort === -1 ? ' DESC' : ' ASC'), + (c.sort?.toLowerCase() === 'desc' || c.sort === -1 ? ' DESC' : ' ASC') + : c => this.expr(c) + (c.sort?.toLowerCase() === 'desc' || c.sort === -1 ? ' DESC' : ' ASC'), ) } diff --git a/postgres/lib/PostgresService.js b/postgres/lib/PostgresService.js index a1093c776..80f909f37 100644 --- a/postgres/lib/PostgresService.js +++ b/postgres/lib/PostgresService.js @@ -348,8 +348,8 @@ GROUP BY k ? c => this.expr(c) + (c.element?.[this.class._localized] ? ` COLLATE "${locale}"` : '') + - (c.sort === 'desc' || c.sort === -1 ? ' DESC NULLS LAST' : ' ASC NULLS FIRST') - : c => this.expr(c) + (c.sort === 'desc' || c.sort === -1 ? ' DESC NULLS LAST' : ' ASC NULLS FIRST'), + (c.sort?.toLowerCase() === 'desc' || c.sort === -1 ? ' DESC NULLS LAST' : ' ASC NULLS FIRST') + : c => this.expr(c) + (c.sort?.toLowerCase() === 'desc' || c.sort === -1 ? ' DESC NULLS LAST' : ' ASC NULLS FIRST'), ) } diff --git a/test/compliance/SELECT.test.js b/test/compliance/SELECT.test.js index 5083391fd..f585220ad 100644 --- a/test/compliance/SELECT.test.js +++ b/test/compliance/SELECT.test.js @@ -569,6 +569,18 @@ describe('SELECT', () => { assert.deepEqual(res, sorted, 'Ensure that all rows are in the correct order') }) + test('sort is case insensitive', async () => { + const { string } = cds.entities('basic.literals') + const mixedDesc = SELECT.from(string).columns('string').orderBy('string DeSc') + const desc = SELECT.from(string).columns('string').orderBy('string desc') + const mixedAsc = SELECT.from(string).columns('string').orderBy('string aSC') + const asc = SELECT.from(string).columns('string').orderBy('string asc') + + expect(await cds.run(mixedDesc)).to.eql(await cds.run(desc)) + expect(await cds.run(mixedAsc)).to.eql(await cds.run(asc)) + }) + + test('localized', async () => { const { string } = cds.entities('basic.literals') const cqn = CQL`SELECT string FROM ${string} ORDER BY string`