Skip to content

Instantly share code, notes, and snippets.

@GermanFilipp
Last active June 12, 2020 11:30
Show Gist options
  • Select an option

  • Save GermanFilipp/7d7eb0a8f1fced9291da370e908157b5 to your computer and use it in GitHub Desktop.

Select an option

Save GermanFilipp/7d7eb0a8f1fced9291da370e908157b5 to your computer and use it in GitHub Desktop.

Revisions

  1. GermanFilipp revised this gist Jun 12, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions print_parametrized_query.py
    Original file line number Diff line number Diff line change
    @@ -8,9 +8,9 @@ def literal_processor(self, dialect):

    def process(value):
    if isinstance(value, int):
    return text(value)
    return str(value)
    if not isinstance(value, str):
    value = text(value)
    value = str(value)
    result = super_processor(value)
    if isinstance(result, bytes):
    result = result.decode(dialect.encoding)
  2. GermanFilipp revised this gist Jun 10, 2020. 1 changed file with 0 additions and 7 deletions.
    7 changes: 0 additions & 7 deletions print_parametrized_query.py
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,6 @@
    from sqlalchemy.engine.default import DefaultDialect
    from sqlalchemy.sql.sqltypes import String, DateTime, NullType

    # python2/3 compatible.
    PY3 = str is not bytes
    text = str if PY3 else unicode
    int_type = int if PY3 else (int, long)
    str_type = str if PY3 else (str, unicode)


    class StringLiteral(String):
    """Teach SA how to literalize various things."""
    def literal_processor(self, dialect):
  3. GermanFilipp created this gist Jun 10, 2020.
    47 changes: 47 additions & 0 deletions print_parametrized_query.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    from sqlalchemy.engine.default import DefaultDialect
    from sqlalchemy.sql.sqltypes import String, DateTime, NullType

    # python2/3 compatible.
    PY3 = str is not bytes
    text = str if PY3 else unicode
    int_type = int if PY3 else (int, long)
    str_type = str if PY3 else (str, unicode)


    class StringLiteral(String):
    """Teach SA how to literalize various things."""
    def literal_processor(self, dialect):
    super_processor = super(StringLiteral, self).literal_processor(dialect)

    def process(value):
    if isinstance(value, int):
    return text(value)
    if not isinstance(value, str):
    value = text(value)
    result = super_processor(value)
    if isinstance(result, bytes):
    result = result.decode(dialect.encoding)
    return result
    return process


    class LiteralDialect(DefaultDialect):
    colspecs = {
    # prevent various encoding explosions
    String: StringLiteral,
    # teach SA about how to literalize a datetime
    DateTime: StringLiteral,
    # don't format py2 long integers to NULL
    NullType: StringLiteral,
    }


    def literalquery(statement):
    """NOTE: This is entirely insecure. DO NOT execute the resulting strings."""
    import sqlalchemy.orm
    if isinstance(statement, sqlalchemy.orm.Query):
    statement = statement.statement
    return statement.compile(
    dialect=LiteralDialect(),
    compile_kwargs={'literal_binds': True},
    ).string