def compile_query(statement, bind=None): """ For debug purposes only! """ import sqlalchemy.orm if isinstance(statement, sqlalchemy.orm.Query): if bind is None: bind = statement.session.get_bind( statement._mapper_zero_or_none() ) statement = statement.statement elif bind is None: bind = statement.bind dialect = bind.dialect compiler = statement._compiler(dialect) class LiteralCompiler(compiler.__class__): def visit_bindparam( self, bindparam, within_columns_clause=False, literal_binds=False, **kwargs ): return super(LiteralCompiler, self).render_literal_bindparam( bindparam, within_columns_clause=within_columns_clause, literal_binds=literal_binds, **kwargs ) compiler = LiteralCompiler(dialect, statement) return compiler.process(statement)
def compile_param(expr): if not isinstance(expr, expression.ColumnClause): if not hasattr(expr, 'self_group'): # assuming base type (int, string, etc.) return compat.text_type(expr) else: expr = expr.self_group() return compiler.process(expr, include_table=False, literal_binds=True)
def compile_do_update(do_update, compiler, **kw): compiled_cf = compiler.process(do_update.conflict_target) if not compiled_cf: raise CompileError("Cannot have empty conflict_target") text = "ON CONFLICT %s DO UPDATE" % compiled_cf if not do_update.values_to_set: raise CompileEror("Cannot have empty set of values to SET in DO UPDATE") names = [] for col, value in do_update.values_to_set.items(): fmt_name = compiler.preparer.format_column(col) if isinstance(col, ColumnClause) else compiler.preparer.format_column(None, name=col) if value is _EXCLUDED: fmt_value = "excluded.%s" % fmt_name elif isinstance(value, ColumnElement): fmt_value = compiler.process(value) else: raise CompileError("Value to SET in DO UPDATE of unsupported type: %r" % value) names.append("%s = %s" % (fmt_name, fmt_value)) text += (" SET " + ", ".join(names)) return text
def compile_conflict_target(conflict_target, compiler, **kw): target = conflict_target.contents if isinstance(target, (PrimaryKeyConstraint, UniqueConstraint)): fmt_cnst = None if target.name is not None: fmt_cnst = compiler.preparer.format_constraint(target) if fmt_cnst is not None: return "ON CONSTRAINT %s" % fmt_cnst else: return "(" + (", ".join(compiler.preparer.format_column(i) for i in target.columns.values())) + ")" if isinstance(target, (str, ColumnClause)): return "(" + compiler.preparer.format_column(target) + ")" if isinstance(target, (list, tuple)): return "(" + (", ".join(compiler.preparer.format_column(i) for i in target)) + ")" if isinstance(target, Index): # columns required first. ops = target.dialect_options["postgresql"]["ops"] text = "(%s)" \ % ( ', '.join([ compiler.process( expr.self_group() if not isinstance(expr, ColumnClause) else expr, include_table=False, literal_binds=True) + ( (' ' + ops[expr.key]) if hasattr(expr, 'key') and expr.key in ops else '' ) for expr in target.expressions ]) ) whereclause = target.dialect_options["postgresql"]["where"] if whereclause is not None: where_compiled = compiler.process( whereclause, include_table=False, literal_binds=True) text += " WHERE " + where_compiled return text
def _compile_param(self, expr): compiler = self.sql_compiler if isinstance(expr, (list, tuple)): return '(' + ', '.join(self._compile_param(el) for el in expr) + ')' if not isinstance(expr, expression.ColumnClause): if not hasattr(expr, 'self_group'): # assuming base type (int, string, etc.) return compat.text_type(expr) else: expr = expr.self_group() return compiler.process(expr, include_table=False, literal_binds=True)
def _compile_param(self, expr, opt_list=False): compiler = self.sql_compiler # Do not render unnecessary brackets. if isinstance(expr, (list, tuple)) and len(expr) == 1 and opt_list: expr = expr[0] if isinstance(expr, (list, tuple)): return '(' + ', '.join(self._compile_param(el) for el in expr) + ')' if not isinstance(expr, expression.ColumnClause): if not hasattr(expr, 'self_group'): # assuming base type (int, string, etc.) return str(expr) else: expr = expr.self_group() return compiler.process(expr, include_table=False, literal_binds=True)
def _compile(element, compiler, **kw): return compiler.process(cast(element.bindvalue, Unicode), **kw)
def compile_do_nothing(do_nothing, compiler, **kw): if do_nothing.conflict_target is not None: return "ON CONFLICT %s DO NOTHING" % compiler.process(do_nothing.conflict_target) else: return "ON CONFLICT DO NOTHING"
def pg_in_time(element, compiler, **kw): field, interval = list(element.clauses) return "{} >= TIMEZONE('utc', CURRENT_TIMESTAMP) - interval {}" \ .format(compiler.process(field), compiler.process(interval))