"use strict"; const splitFilter = require("split-filter"); const operations = require("../operations"); const NoChange = require("./util/no-change"); // FIXME: Have some sort of internally-cacheable way to find nodes of a certain type? So that different optimizer visitors don't need to filter the list of clauses over and over again... module.exports = { name: "collapse-where", category: [ "normalization" ], visitors: { select: ({ collection, clauses }) => { let [ whereClauses, otherClauses ] = splitFilter(clauses, (clause) => clause.type === "where"); if (whereClauses.length > 1) { let whereExpressions = whereClauses.map((clause) => clause.expression); let newWhere = operations.where(operations.allOf(whereExpressions)); return operations.select(collection, [ newWhere ].concat(otherClauses)); } else { return NoChange; } } } };