def format_sql(sql, params, cursor): # type: (str, List[Any], Any) -> Optional[str] real_sql = None real_params = None try: # Prefer our own SQL formatting logic because it's the only one that # has proper value trimming. real_sql, real_params = _format_sql_impl(sql, params) if real_sql: real_sql = format_and_strip(real_sql, real_params) except Exception: pass if not real_sql and hasattr(cursor, "mogrify"): # If formatting failed and we're using psycopg2, it could be that we're # looking at a query that uses Composed objects. Use psycopg2's mogrify # function to format the query. We lose per-parameter trimming but gain # accuracy in formatting. # # This is intentionally the second choice because we assume Composed # queries are not widely used, while per-parameter trimming is # generally highly desirable. try: if hasattr(cursor, "mogrify"): real_sql = cursor.mogrify(sql, params) if isinstance(real_sql, bytes): real_sql = real_sql.decode(cursor.connection.encoding) except Exception: pass return real_sql or None
def record_sql(sql, params): hub = Hub.current if hub.get_integration(DjangoIntegration) is None: return real_sql, real_params = format_sql(sql, params) if real_params: try: real_sql = format_and_strip(real_sql, real_params) except Exception: pass hub.add_breadcrumb(message=real_sql, category="query")
def record_sql(sql, params): real_sql, real_params = format_sql(sql, params) if real_params: try: real_sql = format_and_strip(real_sql, real_params) except Exception: pass # maybe category to 'django.%s.%s' % (vendor, alias or # 'default') ? add_breadcrumb(message=real_sql, category="query")
def record_sql(sql, params, cursor=None): # type: (Any, Any, Any) -> None hub = Hub.current if hub.get_integration(DjangoIntegration) is None: return with capture_internal_exceptions(): if cursor and hasattr(cursor, "mogrify"): # psycopg2 real_sql = cursor.mogrify(sql, params) with capture_internal_exceptions(): if isinstance(real_sql, bytes): real_sql = real_sql.decode(cursor.connection.encoding) else: real_sql, real_params = format_sql(sql, params) if real_params: try: real_sql = format_and_strip(real_sql, real_params) except Exception: pass hub.add_breadcrumb(message=real_sql, category="query")
def record_sql(sql, params, cursor=None): # type: (Any, Any, Any) -> None hub = Hub.current if hub.get_integration(DjangoIntegration) is None: return real_sql = None real_params = None try: # Prefer our own SQL formatting logic because it's the only one that # has proper value trimming. real_sql, real_params = format_sql(sql, params) if real_sql: real_sql = format_and_strip(real_sql, real_params) except Exception: pass if not real_sql and cursor and hasattr(cursor, "mogrify"): # If formatting failed and we're using psycopg2, it could be that we're # looking at a query that uses Composed objects. Use psycopg2's mogrify # function to format the query. We lose per-parameter trimming but gain # accuracy in formatting. # # This is intentionally the second choice because we assume Composed # queries are not widely used, while per-parameter trimming is # generally highly desirable. try: if cursor and hasattr(cursor, "mogrify"): real_sql = cursor.mogrify(sql, params) if isinstance(real_sql, bytes): real_sql = real_sql.decode(cursor.connection.encoding) except Exception: pass if real_sql: with capture_internal_exceptions(): hub.add_breadcrumb(message=real_sql, category="query")
def x(template, params): return format_and_strip( template, params, strip_string=lambda x, **_: strip_string(x, max_length=max_length), )