def compile_case(compile, expr, state): stmt = "CASE WHEN %s THEN %s" % (expr_compile(expr.condition, state), expr_compile(expr.result, state)) if expr.else_ is not None: stmt += ' ELSE ' + expr_compile(expr.else_, state) stmt += ' END' return stmt
def compile_prefix_expr(compile, expr, state): stmt = "CASE WHEN %s THEN %s" % (expr_compile(expr.condition, state), expr_compile(expr.result, state)) if expr.else_: stmt += ' ELSE ' + expr_compile(expr.else_, state) stmt += ' END' return stmt
def compile_generate_series(compile, expr, state): state.push("context", EXPR) if expr.step is Undef: expr = 'generate_series(%s, %s)' % (expr_compile(expr.start, state), expr_compile(expr.end, state)) else: expr = 'generate_series(%s, %s, %s)' % (expr_compile(expr.start, state), expr_compile(expr.end, state), expr_compile(expr.step, state)) state.pop() return expr
def compile_over(compile, expr, state): result = ' %s OVER (' % expr_compile(expr.attribute, state) if expr.partitions: partitions = ', '.join(expr_compile(i, state) for i in expr.partitions) result += 'PARTITION BY %s ' % partitions if expr.orders: orders = ', '.join(expr_compile(i, state) for i in expr.orders) result += 'ORDER BY %s ' % orders result += ')' return result
def compile_in(expr_compile, expr, state): expr1 = expr_compile(expr.expr1, state) state.precedence = 0 # We're forcing parenthesis here. return "%s %s (%s)" % (expr1, expr.oper, expr_compile(expr.expr2, state))
def compile_contains(expr_compile, expr, state): # We currently support only the first argument as a list. expr1 = "ARRAY[%s]" % ",".join(expr_compile(i, state) for i in expr.expr1) return '%s%s%s' % (expr1, expr.oper, expr_compile(expr.expr2, state))
def compile_array(expr_compile, expr, state): return "ARRAY[%s]" % ",".join(expr_compile(i, state) for i in expr.array)
def compile_between(compile, expr, state): return ' %s BETWEEN %s AND %s ' % ( expr_compile(expr.value, state), expr_compile(expr.start, state), expr_compile(expr.end, state))
def compile_position(compile, expr, state): return "%s(%s in %s)" % (expr.name, expr_compile(expr.substring, state), expr_compile(expr.string, state))
def compile_trim(compile, expr, state): return "TRIM(%s %s FROM %s)" % ( expr.op, expr_compile(expr.character, state), expr_compile(expr.column, state))
def compile_concat(compile, expr, state): return " || ".join(expr_compile(input_, state) for input_ in expr.inputs)
def compile_filter(compile, expr, state): stmt = "%s FILTER (WHERE %s )" % (expr_compile(expr.expr, state), expr_compile(expr.clause, state)) return stmt