Skip to content

Commit e0c249e

Browse files
committed
feat(builder): add BETWEEN and NOT BETWEEN condition handling
1 parent 4b44e88 commit e0c249e

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/builder.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,24 @@ class Builder {
298298
return null;
299299
}
300300

301+
_buildConditionBetween(condition, isNot = false) {
302+
if (!Array.isArray(condition.value) || condition.value.length !== 2) {
303+
throw new Error('Value must be an array with two elements for "BETWEEN" condition');
304+
}
305+
this.values.push(condition.value[0] || null);
306+
this.values.push(condition.value[1] || null);
307+
if (condition.key.indexOf('->') !== -1) {
308+
let keys = condition.key.split('->');
309+
let k = `${this._buildFieldKey(keys[0])}`;
310+
let sql = `JSON_EXTRACT(${k}, '${keys[1]}') `;
311+
sql += isNot ? 'NOT BETWEEN' : 'BETWEEN';
312+
sql += ' ? AND ?';
313+
return sql;
314+
}
315+
const opt = isNot ? 'NOT BETWEEN' : 'BETWEEN';
316+
return `${this._buildFieldKey(condition.key)} ${opt} ? AND ?`;
317+
}
318+
301319
_buildConditionIn(condition, isNot = false) {
302320
if (Array.isArray(condition.value) && !condition.value.length) {
303321
throw new Error('Value must not be empty for "IN" condition');
@@ -369,6 +387,10 @@ class Builder {
369387
return this._buildConditionIn(c);
370388
} else if (opt === 'not in') {
371389
return this._buildConditionIn(c, true);
390+
} else if (opt === 'between') {
391+
return this._buildConditionBetween(c);
392+
} else if (opt === 'not between') {
393+
return this._buildConditionBetween(c, true);
372394
} else if (opt === 'contain') {
373395
return this._buildConditionContain(c);
374396
} else if (opt === 'not contain') {

0 commit comments

Comments
 (0)