Пример #1
0
def compile_query(query):
    '''
    주어진 SQLAlchemy Query 풀버전을 문자열로 돌려준다.

    >>> query = model.Session().query(model.Board)
    >>> print compile_query(query)
    u'SELECT boards.id, boards.category_id, boards.board_name, boards.board_alias, boards.board_description, boards.deleted, boards.read_only, boards.hide, boards."order", boards.type, boards.to_read_level, boards.to_write_level \nFROM boards'

    @type  query: sqlalchemy.orm.query.Query
    @param query: 들여다볼 쿼리 객체
    @rtype: str
    @return: 쿼리문 본문
    '''
    dialect = query.session.bind.dialect
    statement = query.statement
    comp = compiler.SQLCompiler(dialect, statement)
    comp.compile()
    enc = dialect.encoding
    params = []
    for key in comp.positiontup:
        value = comp.params[key]
        if isinstance(value, unicode):
            value = value.encode(enc)
        params.append(escape(value, conversions))
    return (comp.string.encode(enc) % tuple(params)).decode(enc)
Пример #2
0
def compile_query(query):
    """ 把一个 sqlalchemy query object 编译成mysql风格的 sql 语句 """
    from sqlalchemy.sql import compiler
    from MySQLdb.converters import conversions, escape
    from sqlalchemy.dialects import mysql as mysql_dialetct

    dialect = mysql_dialetct.dialect()
    statement = query.statement
    comp = compiler.SQLCompiler(dialect, statement)
    comp.compile()
    enc = dialect.encoding
    comp_params = comp.params
    params = []
    for k in comp.positiontup:
        v = comp_params[k]
        # python2, escape(u'123', conversions) is '123', should be "'123'"
        # escape('123', conversions) is ok
        if six.PY2 and isinstance(v, unicode):
            v = v.encode(enc)
        v = escape(v, conversions)
        # python3, escape return bytes
        if six.PY3 and isinstance(v, bytes):
            v = v.decode(enc)
        params.append(v)
    return (comp.string % tuple(params))
Пример #3
0
def sqlalchemy_expr_to_str(sqlalchemy_expr):
    dialect = mysql.dialect()
    compiler = sqlalchemy_expr.compile(dialect=dialect)
    params = (compiler.params[k] for k in compiler.positiontup)
    coerced_params = (p.encode('utf-8') if isinstance(p, unicode) else p
                      for p in params)
    escaped_params = tuple(escape(p, conversions) for p in coerced_params)
    return compiler.string % escaped_params
Пример #4
0
def compile_query(query):
    dialect = query.session.bind.dialect
    statement = query.statement
    comp = compiler.SQLCompiler(dialect, statement)
    comp.compile()
    enc = dialect.encoding
    params = []
    for k in comp.positiontup:
        v = comp.params[k]
        if isinstance(v, unicode):
            v = v.encode(enc)
        params.append(escape(v, conversions))
    return (comp.string.encode(enc) % tuple(params)).decode(enc)
Пример #5
0
def compile_query(query):
    dialect = query.session.bind.dialect
    statement = query.statement
    comp = compiler.SQLCompiler(dialect, statement)
    comp.compile()
    enc = dialect.encoding
    params = []
    for k in comp.positiontup:
        v = comp.params[k]
        if isinstance(v, unicode):
            v = v.encode(enc)
        params.append(escape(v, conversions))
    return (comp.string.encode(enc) % tuple(params)).decode(enc)
Пример #6
0
def compile_query_mysql(query, dialect):
    from MySQLdb.converters import conversions, escape

    statement = query.statement
    comp = compiler.SQLCompiler(dialect, statement)
    comp.compile()
    enc = dialect.encoding
    params = []
    for k in comp.positiontup:
        v = comp.params[k]
        if isinstance(v, unicode):
            v = v.encode(enc)
        params.append( escape(v, conversions) )
    return (comp.string.encode(enc) % tuple(params)).decode(enc)
Пример #7
0
def compile_query(query):
    """
    Compile sqlalchemy query object to regular SQL with escaped values
    @param query: sqlalchemy query object
    @return: string
    """
    dialect = query.session.bind.dialect
    statement = query.statement
    comp = compiler.SQLCompiler(dialect, statement)
    enc = dialect.encoding
    params = []
    for k in comp.positiontup:
        v = comp.params[k]
        if isinstance(v, unicode):
            v = v.encode(enc)
        params.append(escape(v, conversions))
    return (comp.string.encode(enc) % tuple(params)).decode(enc)
Пример #8
0
def compile_query(query):
    from sqlalchemy.sql import compiler
    try:
        from MySQLdb.converters import conversions, escape
    except ImportError:
        return 'no sql conversion available'
    dialect = query.session.bind.dialect
    statement = query.statement
    comp = compiler.SQLCompiler(dialect, statement)
    enc = dialect.encoding
    params = []
    for k in comp.positiontup:
        v = comp.params[k]
        if isinstance(v, unicode):
            v = v.encode(enc)
        params.append(escape(v, conversions))

    comp = comp.string.encode(enc)
    comp = comp.replace('?', '%s')

    return (comp % tuple(params)).decode(enc)
Пример #9
0
def sqlalchemy_expr_to_str(sqlalchemy_expr):
    dialect = mysql.dialect()
    compiler = sqlalchemy_expr.compile(dialect=dialect)
    params = (compiler.params[k] for k in compiler.positiontup)
    escaped_params = tuple(as_unicode(escape(p, conversions)) for p in params)
    return compiler.string % escaped_params