Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix whereJsonPath queries for postgres and cockroachdb #5011

Merged
merged 1 commit into from
Feb 8, 2022
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
4 changes: 2 additions & 2 deletions lib/dialects/cockroachdb/crdb-querycompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ class QueryCompiler_CRDB extends QueryCompiler_PG {

whereJsonPath(statement) {
let castValue = '';
if (parseInt(statement.value)) {
if (!isNaN(statement.value) && parseInt(statement.value)) {
castValue = '::int';
} else if (parseFloat(statement.value)) {
} else if (!isNaN(statement.value) && parseFloat(statement.value)) {
castValue = '::float';
} else {
castValue = " #>> '{}'";
Expand Down
4 changes: 2 additions & 2 deletions lib/dialects/postgres/query/pg-querycompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,9 @@ class QueryCompiler_PG extends QueryCompiler {

whereJsonPath(statement) {
let castValue = '';
if (parseInt(statement.value)) {
if (!isNaN(statement.value) && parseInt(statement.value)) {
castValue = '::int';
} else if (parseFloat(statement.value)) {
} else if (!isNaN(statement.value) && parseFloat(statement.value)) {
castValue = '::float';
} else {
castValue = " #>> '{}'";
Expand Down
14 changes: 14 additions & 0 deletions test/integration2/query/select/where.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,20 @@ describe('Where', function () {
});
});

it('where json path equal string starting with number', async () => {
const result = await knex('cities')
.select('name')
.whereJsonPath(
'statistics',
'$.statisticId',
'=',
'6qITEHRUNJ4bdAmA0lk82'
);
expect(result[0]).to.eql({
name: 'Paris',
});
});

it('where multiple json path', async () => {
const result = await knex('cities')
.select('name')
Expand Down
3 changes: 3 additions & 0 deletions test/util/dataInsertHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ async function insertCities(knex) {
min: 1234,
max: 1333,
},
statisticId: '6qITEHRUNJ4bdAmA0lk82',
},
temperature: {
desc: 'cold',
Expand Down Expand Up @@ -172,6 +173,7 @@ async function insertCities(knex) {
max: 1655,
},
hotYears: [2012, 2015, 2021],
statisticId: '-4mZkPL-ZzRyEed00RQTQ',
},
temperature: {
desc: 'warm',
Expand Down Expand Up @@ -201,6 +203,7 @@ async function insertCities(knex) {
max: 156,
},
hotYears: [2016],
statisticId: '6bBGzpyL3sTRrxWs5gXTa',
},
temperature: {
desc: 'verycold',
Expand Down