Improve assertion in ParameterizedSQL builder

Fixed the problem where the application hangs when a param is a very
large Buffer.

Avoid expensive string building if assertion passed.

Co-authored-by: Miroslav Bajtoš <mbajtoss@gmail.com>
This commit is contained in:
ewrayjohnson 2020-07-13 19:03:05 -04:00 committed by Miroslav Bajtoš
parent 7d43b461d1
commit acfb5e2fce
No known key found for this signature in database
GPG Key ID: 6F2304BA9361C7E3
1 changed files with 14 additions and 5 deletions

View File

@ -5,6 +5,7 @@
'use strict';
const assert = require('assert');
const util = require('util');
const PLACEHOLDER = '?';
module.exports = ParameterizedSQL;
@ -35,11 +36,19 @@ function ParameterizedSQL(sql, params) {
assert(Array.isArray(this.params), 'params must be an array');
const parts = this.sql.split(PLACEHOLDER);
assert(parts.length - 1 === this.params.length,
'The number of ? (' + (parts.length - 1) +
') in the sql (' + this.sql + ') must match the number of params (' +
this.params.length +
') ' + this.params);
if (parts.length - 1 !== this.params.length) {
throw new assert.AssertionError({
message: util.format(
'The number of ? (%s) in the sql (%s) must match the number of params (%s) %o',
parts.length - 1,
this.sql,
this.params.length,
this.params,
),
actual: this.params.length,
expected: parts.length - 1,
});
}
}
/**