def to_sql(self): stack = [] params = [] # Process the domain from right to left, using a stack, to generate a SQL expression. for i, e in reverse_enumerate(self.__exp): if is_leaf(e, internal=True): table = self.__field_tables.get(i, self.__main_table) q, p = self.__leaf_to_sql(e, table) params.insert(0, p) stack.append(q) elif e == NOT_OPERATOR: stack.append('(NOT (%s))' % (stack.pop(), )) else: ops = {AND_OPERATOR: ' AND ', OR_OPERATOR: ' OR '} q1 = stack.pop() q2 = stack.pop() stack.append('(%s %s %s)' % ( q1, ops[e], q2, )) assert len(stack) == 1 query = stack[0] joins = ' AND '.join(self.__joins) if joins: query = '(%s) AND %s' % (joins, query) return (query, flatten(params))
def to_sql(self): stack = [] params = [] for i, e in reverse_enumerate(self.__exp): if self._is_leaf(e, internal=True): table = self.__field_tables.get(i, self.__main_table) q, p = self.__leaf_to_sql(e, table) params.insert(0, p) stack.append(q) else: if e == NOT_OPERATOR: stack.append('(NOT (%s))' % (stack.pop(),)) else: ops = {AND_OPERATOR: ' AND ', OR_OPERATOR: ' OR '} q1 = stack.pop() q2 = stack.pop() stack.append('(%s %s %s)' % (q1, ops[e], q2,)) query = ' AND '.join(reversed(stack)) joins = ' AND '.join(self.__joins) if joins: query = '(%s) AND (%s)' % (joins, query) return (query, flatten(params))
def to_sql(self): stack = [] params = [] # Process the domain from right to left, using a stack, to generate a SQL expression. for i, e in reverse_enumerate(self.__exp): if is_leaf(e, internal=True): table = self.__field_tables.get(i, self.__main_table) q, p = self.__leaf_to_sql(e, table) params.insert(0, p) stack.append(q) elif e == NOT_OPERATOR: stack.append('(NOT (%s))' % (stack.pop(),)) else: ops = {AND_OPERATOR: ' AND ', OR_OPERATOR: ' OR '} q1 = stack.pop() q2 = stack.pop() stack.append('(%s %s %s)' % (q1, ops[e], q2,)) assert len(stack) == 1 query = stack[0] joins = ' AND '.join(self.__joins) if joins: query = '(%s) AND %s' % (joins, query) return (query, flatten(params))