createConditionClause()

in salesforce/lib/query-builder.js [103:155]


    createConditionClause(conditions, operator, depth) {
        if (_.isString(conditions)) {
            return conditions;
        }
        conditions = conditions || [];
        operator = operator || "AND";
        depth = depth || 0;
        if (!isArray(conditions)) {
            conditions = _.keys(conditions).map((key) => ({
                key: key,
                value: conditions[key]
            }));
        } else {
            conditions = conditions.map((cond) => {
                let conds = [];
                for (let key in cond) {
                    conds.push({
                        key: key,
                        value: cond[key]
                    });
                }
                return conds.length > 1 ? conds : conds[0];
            });
        }

        conditions = conditions.map((cond) => {
            let d = depth + 1;
            let op;
            switch (cond.key) {
                case "$or":
                case "$and":
                case "$not":
                    if (operator !== "NOT" && conditions.length === 1) {
                        d = depth;
                    }
                    op = cond.key === "$or" ? "OR"
                        : cond.key === "$and" ? "AND"
                            : "NOT";
                    return this.createConditionClause(cond.value, op, d);
                default:
                    return this.createFieldExpression(cond.key, cond.value);
            }
        }).filter((expr) => expr);

        let paren;
        if (operator === "NOT") {
            paren = depth > 0;
            return (paren ? "(" : "") + "NOT " + conditions[0] + (paren ? ")" : "");
        } else {
            paren = depth > 0 && conditions.length > 1;
            return (paren ? "(" : "") + conditions.join(" " + operator + " ") + (paren ? ")" : "");
        }
    }