sqlite3orm
Version:
ORM for sqlite3 and TypeScript/JavaScript
74 lines • 2.47 kB
JavaScript
/* eslint-disable @typescript-eslint/no-explicit-any */
Object.defineProperty(exports, "__esModule", { value: true });
exports.QueryCondition = void 0;
const QueryModelPredicates_1 = require("./QueryModelPredicates");
const Where_1 = require("./Where");
class QueryCondition {
op;
subOperations;
sql;
constructor(cond) {
this.subOperations = [];
this.sql = '';
const keys = Object.keys(cond);
/* istanbul ignore if */
if (keys.length !== 1) {
throw new Error(`unknown operation: ${keys.toString()}`);
}
const key = keys[0];
/* istanbul ignore if */
if (key !== 'not' && key !== 'and' && key !== 'or' && key !== 'sql') {
throw new Error(`unknown operation: '${key}'`);
}
this.op = key;
if (this.op === 'sql') {
this.sql = cond[key];
}
else if (this.op === 'not') {
const value = cond[key];
if ((0, Where_1.isModelPredicates)(value)) {
this.subOperations.push(new QueryModelPredicates_1.QueryModelPredicates(value));
}
else {
this.subOperations.push(new QueryCondition(value));
}
}
else {
const value = cond[key];
value.forEach((item) => {
if ((0, Where_1.isModelPredicates)(item)) {
this.subOperations.push(new QueryModelPredicates_1.QueryModelPredicates(item));
}
else {
this.subOperations.push(new QueryCondition(item));
}
});
}
}
async toSql(metaModel, params, tablePrefix) {
if (this.op === 'sql') {
return this.sql;
}
const parts = [];
for (const subOperation of this.subOperations) {
const part = await subOperation.toSql(metaModel, params, tablePrefix);
if (part.length) {
parts.push(part);
}
}
if (!parts.length) {
return '';
}
switch (this.op) {
case 'not':
return `not (${parts[0]})`;
case 'and':
return '(' + parts.join(') and (') + ')';
case 'or':
return '(' + parts.join(') or (') + ')';
}
}
}
exports.QueryCondition = QueryCondition;
//# sourceMappingURL=QueryCondition.js.map
;