def init_clause(cls, dialect, elem, multiparams, params, connection): # partially copied from: # sqlalchemy.engine.base.Connection:_execute_clauseelement distilled_params = _distill_params(multiparams, params) if distilled_params: # note this is usually dict but we support RowProxy # as well; but dict.keys() as an iterable is OK keys = distilled_params[0].keys() else: keys = [] compiled_sql = elem.compile( dialect=dialect, column_keys=keys, inline=len(distilled_params) > 1, ) conn = ConnectionAdaptor(dialect, connection, compiled_sql) rv = cls._init_compiled(dialect, conn, conn, compiled_sql, distilled_params) return rv
def _execute_clauseelement(self, elem, multiparams): # Modeled after `sqlalchemy.engine.base.Connection._execute_clauseelement` # (event signaling, caching are removed; separate parameters are merge into # clause element) if not multiparams and isinstance(elem, Insert) and elem.parameters: # We disable normal way of extracting parameters, so we have to # handle parameters incorporated into clause with # `Insert.values(...)` call manually. if isinstance(elem.parameters, dict): multiparams = (elem.parameters,) else: multiparams = tuple(elem.parameters) distilled_params = _distill_params(multiparams, {}) init_compiled_parameters = distilled_params if isinstance(elem, Insert): # `_init_compiled()` method creates placeholders for each row in # `multiparams`. This is very slow and we don't need them when # passing separately in JSONEachRow format. As workaroung we # pretend that we don't have parameters at all. init_compiled_parameters = () compiled_sql = elem.compile( dialect=self._dialect, inline=True, # Never add constructs to return default values ) return self._execute_context( self._dialect, self._dialect.execution_ctx_cls._init_compiled, compiled_sql, distilled_params, # The rest are `*args` to `_init_compiled` compiled_sql, init_compiled_parameters, )
def __init__(self, context, clauseelement, multiparams, params): self.context = context self.clauseelement = clauseelement self.parameters = _distill_params(multiparams, params) self.statements = []
def __init__(self, context, clauseelement, multiparams, params): self.context = context self.clauseelement = clauseelement self.parameters = _distill_params(multiparams, params) self.statements = []
def _after_execute(self, conn, clause, multiparams, params, result): logger = self.loggers.get(current_thread().ident) if not logger: return try: start_time = clause.start_time except AttributeError: start_time = self.tmp.pop(id(clause)) stop_time = time() duration = (stop_time - start_time) * 1000 config = dt_settings.get_config() try: raw_compiled = clause.compile(dialect=self.engine.dialect, compile_kwargs={}) except AttributeError: try: parameters = _distill_params(multiparams, params) except InvalidRequestError: parameters = [] raw_sql = " ".join(six.text_type(clause).splitlines()) else: try: ctx = CursorlessExecutionContext._init_compiled( self.engine.dialect, conn, conn._Connection__connection, raw_compiled, _distill_params(multiparams, params), ) except Exception: parameters = [] raw_sql = " ".join(six.text_type(clause).splitlines()) else: parameters = ctx.parameters raw_sql = " ".join(ctx.statement.splitlines()) try: sql = " ".join( six.text_type( clause.compile(dialect=self.engine.dialect, compile_kwargs={"literal_binds": True})).splitlines()) except (CompileError, TypeError, NotImplementedError, AttributeError): # not all queries support literal_binds sql = raw_sql if config["ENABLE_STACKTRACES"]: stacktrace = tidy_stacktrace(reversed(get_stack())) else: stacktrace = [] template_info = get_template_info() params = { "vendor": conn.dialect.name, "alias": self.alias, "sql": sql, "duration": duration, "raw_sql": raw_sql, "params": json.dumps([ list(i) if isinstance(i, (list, tuple)) else i for i in parameters if i ]), 'raw_params': tuple( tuple(i.items() if isinstance(i, dict) else i) for i in parameters), "stacktrace": stacktrace, "start_time": start_time, "stop_time": stop_time, "is_slow": duration > config["SQL_WARNING_THRESHOLD"], "is_select": sql.lower().strip().startswith("select"), "template_info": template_info, } logger.record(**params) return params