def parse(self, query): result = [] query = query[:] while query: expression = query.pop() if (not Expression.is_expression(expression) and expression not in self.operators): raise InvalidExpressionException if Expression.is_expression(expression): result += self.get_expressions(expression) else: op = self.operators[expression] q = [] for _ in range(0, op.n_pops): q1 = result.pop() q.append(q1) result.append(op(q)) return And(reversed(result))
def parse(self, query): result = [] while query: expression = query.pop() if (not Expression.is_expression(expression) and expression not in self.operators): raise InvalidExpressionException if Expression.is_expression(expression): field = expression[0] column = getattr(self.table, field) table = self.table if '.' in field: for idx, field_join in enumerate(field.split('.')): if idx + 1 < len(field.split('.')): fk = self.foreign_key(table._name)[field_join] table_join = Table(fk['foreign_table_name']) join = Join(self.join_on, table_join) column = getattr(table, field_join) fk_col = getattr(join.right, fk['foreign_column_name']) join.condition = Equal(column, fk_col) join = self.get_join(join) if not join: join = self.join_on.join(table_join) join.condition = Equal(column, fk_col) self.joins.append(join) table = table_join else: table = join.right else: column = getattr(join.right, field_join) expression = Expression(expression) expression.left = column result.append(expression.expression) else: op = self.operators[expression] q = [] for _ in range(0, op.n_pops): q1 = result.pop() q.append(q1) result.append(op(q)) return And(reversed(result))
with description('Creating an expression'): with context('if is an invalid expression'): with it('must raise an InvalidExpressionException'): def callback(): Expression(('a',)) expect(callback).to(raise_error(InvalidExpressionException)) with context('if is a valid expresion'): with it('not sould fail'): exp = Expression(('a', '=', 'b')) expect(exp.expression).to(equal(Equal('a', 'b'))) with context('if has an invalid operand'): with it('must raise an exception'): def callback(): Expression(('a', '!!=', 'b')) expect(callback).to(raise_error( ValueError, 'Operator !!= is not supported' )) with context('testing if is a valid expression'): with it('must return true if is a valid expression'): is_exp = Expression.is_expression(('a', '=', 'b')) expect(is_exp).to(be_true) with it('must return false if is an invalid expression'): is_exp = Expression.is_expression(('a', '=')) expect(is_exp).to(be_false)
with description('Creating an expression'): with context('if is an invalid expression'): with it('must raise an InvalidExpressionException'): def callback(): Expression(('a', )) expect(callback).to(raise_error(InvalidExpressionException)) with context('if is a valid expresion'): with it('not sould fail'): exp = Expression(('a', '=', 'b')) expect(exp.expression).to(equal(Equal('a', 'b'))) with context('if has an invalid operand'): with it('must raise an exception'): def callback(): Expression(('a', '!!=', 'b')) expect(callback).to( raise_error(ValueError, 'Operator !!= is not supported')) with context('testing if is a valid expression'): with it('must return true if is a valid expression'): is_exp = Expression.is_expression(('a', '=', 'b')) expect(is_exp).to(be_true) with it('must return false if is an invalid expression'): is_exp = Expression.is_expression(('a', '=')) expect(is_exp).to(be_false)