Skip to content

Commit bbec3b2

Browse files
committed
fixed and simplified logic
1 parent 291e248 commit bbec3b2

1 file changed

Lines changed: 11 additions & 18 deletions

File tree

rds/index.js

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -273,27 +273,20 @@ class StatementBuilder {
273273
return name;
274274
}
275275

276-
buildWhereClause(where, startGrouping = "", endGrouping = "") {
276+
buildWhereClause(where, startGrouping = "", endGrouping = "", default_operator="AND") {
277277
let blocks = [];
278278
for (const key in where) {
279279
if ( ["or", "and"].includes(key)) {
280280
const ops = key.toUpperCase();
281-
// .and [ {cond_1}, {cond_1} ]
282-
if ( where[key].length > 1) {
283-
const parts = [];
284-
for (const part of where[key]) {
285-
parts.push(this.buildWhereClause(part, "(", ")"));
286-
}
287-
blocks.push(`${startGrouping}${parts.join(` ${ops} `)}${endGrouping}`);
288-
} else if (blocks) {
289-
// cond_1.and[ { cond2 } ]
290-
const lastIndex = blocks.length - 1;
291-
blocks[lastIndex] = `${startGrouping}${blocks[lastIndex]} ${ops} ${this.buildWhereClause(where[key][0], "(",")")}${endGrouping}`;
292-
} else {
293-
// .and[{cond_1}]
294-
// Is it possible to have only one condition?
295-
throw new Error(" TODO find the error");
281+
if (!Array.isArray(where[key])) {
282+
// TODO properly handle errors to return a more useful message
283+
throw new Error(`'${key}' expects conditions to be an array`);
284+
}
285+
const parts = [];
286+
for (const part of where[key]) {
287+
parts.push(this.buildWhereClause(part, "(", ")", ops));
296288
}
289+
blocks.push(`${startGrouping}${parts.join(` ${ops} `)}${endGrouping}`);
297290
} else {
298291
// implicit single clause
299292
const block = {};
@@ -302,7 +295,7 @@ class StatementBuilder {
302295
}
303296
}
304297

305-
return blocks;
298+
return blocks.join(` ${default_operator} `);
306299
}
307300

308301
buildWhereStatement(defn, startGrouping = "(", endGrouping = ")") {
@@ -334,7 +327,7 @@ class StatementBuilder {
334327
case "notContains":
335328
return `${startGrouping}${this.quoteChar}${columnName}${this.quoteChar} NOT LIKE ${value}${endGrouping}`;
336329
case "attributeExists":
337-
return `${startGrouping}${this.quoteChar}${columnName}${this.quoteChar} ${value? "NOT" : "IS"} NULL${endGrouping}`;
330+
return `${startGrouping}${this.quoteChar}${columnName}${this.quoteChar} IS ${value? "NOT " : ""}NULL${endGrouping}`;
338331
default:
339332
throw new Error(`Unhandled condition type ${conditionType}`);
340333
}

0 commit comments

Comments
 (0)