Skip to content

Commit 5d1ba64

Browse files
added docs, tests for indexedValues
1 parent b31cf51 commit 5d1ba64

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

docs/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Set options of json-sql builder instance.
6969
| `valuesPrefix` | `'$'` | Prefix for values placeholders<br>Option is used if `namedValues = true`. |
7070
| `dialect` | `'base'` | Active dialect. See setDialect for dialects list. |
7171
| `wrappedIdentifiers` | `true` | If `true` - wrap all identifiers with dialect wrapper (name -> "name"). |
72-
| `usePlaceholderId` | `true` | If `true` - uses auto-generated id for values placeholders after the value prefix |
72+
| `indexedValues` | `true` | If `true` - uses auto-generated id for values placeholders after the value prefix |
7373

7474
---
7575

lib/builder.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,14 @@ Builder.prototype._reset = function() {
2626
this._query = '';
2727
};
2828

29-
Builder.prototype._getPlaceholder = function() {
30-
return (this.options.namedValues ? 'p' : '') + (this.options.usePlaceholderId ? (this._placeholderId++) : '');
29+
Builder.prototype._getPlaceholder = function () {
30+
if (this.options.namedValues && !this.options.indexedValues) {
31+
throw new Error('Use of options "indexedValues: false" is ' +
32+
'not allowed together with "namedValues: true"');
33+
}
34+
var placeholder = this.options.namedValues ? 'p' : '';
35+
var index = this.options.indexedValues ? (this._placeholderId++) : '';
36+
return placeholder + index;
3137
};
3238

3339
Builder.prototype._wrapPlaceholder = function(name) {
@@ -65,7 +71,7 @@ Builder.prototype.configure = function(options) {
6571
valuesPrefix: '$',
6672
dialect: 'base',
6773
wrappedIdentifiers: true,
68-
usePlaceholderId: true
74+
indexedValues: true
6975
});
7076

7177
this.setDialect(this.options.dialect);

tests/0_base.js

+33-3
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ describe('Builder', function() {
205205

206206
expect(result.query).to.be.equal('select * from "users" where "name" = $1;');
207207
expect(result.values).to.be.eql(['John']);
208-
});
208+
});
209209

210210
it('should use prefix `@` for values with option `valuesPrefix` = @', function() {
211211
jsonSql.configure({
@@ -219,7 +219,7 @@ describe('Builder', function() {
219219

220220
expect(result.query).to.be.equal('select * from "users" where "name" = @p1;');
221221
expect(result.values).to.be.eql({p1: 'John'});
222-
});
222+
});
223223

224224
it('should return prefixed values with method `prefixValues`', function() {
225225
var result = jsonSql.build({
@@ -230,7 +230,7 @@ describe('Builder', function() {
230230
expect(result.query).to.be.equal('select * from "users" where "name" = @p1;');
231231
expect(result.values).to.be.eql({p1: 'John'});
232232
expect(result.prefixValues()).to.be.eql({'@p1': 'John'});
233-
});
233+
});
234234

235235
it('should return array values with method `getValuesArray`', function() {
236236
var result = jsonSql.build({
@@ -260,6 +260,36 @@ describe('Builder', function() {
260260
expect(result.values).to.be.eql(['John']);
261261
expect(result.prefixValues()).to.be.eql({'$1': 'John'});
262262
expect(result.getValuesObject()).to.be.eql({1: 'John'});
263+
});
264+
265+
it('should throw if `indexedValues = false` and `namedValues = true`', function() {
266+
jsonSql.configure({
267+
namedValues: true,
268+
indexedValues: false
269+
});
270+
271+
expect(function() {
272+
jsonSql.build({
273+
table: 'users',
274+
condition: {name: 'John'}
275+
});
276+
}).to.throw('Use of options "indexedValues: false" is ' +
277+
'not allowed together with "namedValues: true"');
278+
});
279+
280+
it('should not use index for values with option `indexedValues` = false', function() {
281+
jsonSql.configure({
282+
namedValues: false,
283+
indexedValues: false
284+
});
285+
286+
var result = jsonSql.build({
287+
table: 'users',
288+
condition: {name: 'John'}
289+
});
290+
291+
expect(result.query).to.be.equal('select * from "users" where "name" = $;');
292+
expect(result.values).to.be.eql(['John']);
263293
});
264294

265295
it('should create query without values with option `separatedValues` = false', function() {

0 commit comments

Comments
 (0)