Skip to content

Commit

Permalink
Fixes a bug with subquery parameter binding
Browse files Browse the repository at this point in the history
  • Loading branch information
vampirical committed Mar 7, 2024
1 parent 73129c9 commit ea0996d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/RecordQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,8 +502,8 @@ class RecordQuery extends Object {
return results;
}

getSql(conn, {count = false, isSubquery = false} = {}) {
const wherePack = getWhereSql(conn, this.recordName, this.recordType.fields, this.wheres);
getSql(conn, {count = false, isSubquery = false, bindParamsUsed = 0} = {}) {
const wherePack = getWhereSql(conn, this.recordName, this.recordType.fields, this.wheres, {bindParamsUsed});

let limitString = null;
const hasLimit = this._limit !== null;
Expand Down
8 changes: 4 additions & 4 deletions src/wheres.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ function getWhereSql(
fieldDefinitions,
key,
value,
{comparison}
{comparison, bindParamsUsed}
);

let sqlLhs = columnSql.lhs;
Expand Down Expand Up @@ -220,7 +220,7 @@ function getWhereSql(
return {query, values};
}

function getColumnWhereSql(conn, recordName, fieldDefinitions, key, value, {comparison = null} = {}) {
function getColumnWhereSql(conn, recordName, fieldDefinitions, key, value, {comparison = null, bindParamsUsed = 0} = {}) {
let lhs = key ? quoteIdentifier(getFieldDbName(fieldDefinitions, key)) : undefined;
let rhs = null;
let values = [];
Expand Down Expand Up @@ -265,7 +265,7 @@ function getColumnWhereSql(conn, recordName, fieldDefinitions, key, value, {comp
Array.prototype.push.apply(values, actualValue);
}
} else if (actualValue && typeof actualValue.getSql === 'function') {
const sqlPack = actualValue.getSql(conn, {isSubquery: true});
const sqlPack = actualValue.getSql(conn, {isSubquery: true, bindParamsUsed});

outputComparison = sqlPack.comparison || outputComparison;
rhs = `(${sqlPack.query})`;
Expand All @@ -277,7 +277,7 @@ function getColumnWhereSql(conn, recordName, fieldDefinitions, key, value, {comp
rhs = isParensComparison ? `(${actualValue})` : actualValue;
}
} else if (value && typeof value.getSql === 'function') { // RecordQuery or custom implementor.
const sqlPack = value.getSql(conn, {isSubquery: true});
const sqlPack = value.getSql(conn, {isSubquery: true, bindParamsUsed});

outputComparison = sqlPack.comparison || comparisonDefs.in;
rhs = `(${sqlPack.query})`;
Expand Down
17 changes: 16 additions & 1 deletion tests/RecordQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,22 @@ test('subquery without limit or offset skips order by', async (t) => {
t.false(queryWithoutOrderBy.includes('ORDER BY'));
});

test('debug', async (t) => {
test('number of binds is correct', async (t) => {
const q = await new SQL.RecordQuery(pool, QueryTestRecord)
.where({
id: SQL.and(
[1, 2],
SQL.notEqual(3),
QueryTestRecord.query({id: 1}, {returns: 'id'}),
SQL.notIn(QueryTestRecord.query({id: 4}, {returns: 'id'}))
)
});
const sql = q.getSql();
t.is(sql.values.length, 5);
t.deepEqual(Array.from(sql.query.matchAll(/\$\d+/g)).map(r => r[0]), ['$1', '$2', '$3', '$4', '$5']);
});

test('debug coverage', async (t) => {
new SQL.RecordQuery(pool, QueryTestRecord, {debug: true});
t.true(true);
});

0 comments on commit ea0996d

Please sign in to comment.