def literalquery(statement, dialect=None):
 if isinstance(statement, sqlalchemy.orm.Query):
  if (dialect is None):
   dialect = statement.session.get_bind(statement._mapper_zero_or_none()).dialect
  statement = statement.statement
 if (dialect is None):
  dialect = getattr(statement.bind, 'dialect', None)
 if (dialect is None):
  from sqlalchemy.dialects import mysql
  dialect = mysql.dialect()
 Compiler = type(statement._compiler(dialect))
 class LiteralCompiler(Compiler, ):
  visit_bindparam = Compiler.render_literal_bindparam
  def render_literal_value(self, value, type_):
   if isinstance(value, (Decimal, long)):
    return str(value)
   elif isinstance(value, datetime):
    return repr(str(value))
   else:
    value = super(LiteralCompiler, self).render_literal_value(value, type_)
    if isinstance(value, unicode):
     return value.encode('UTF-8')
    else:
     return value
 return LiteralCompiler(dialect, statement)

def literalquery ( statement , dialect = None ) : if isinstance ( statement , sqlalchemy . orm . Query ) : if ( dialect is None ) : dialect = statement . session . get_bind ( statement . _mapper_zero_or_none ( ) ) . dialect statement = statement . statement if ( dialect is None ) : dialect = getattr ( statement . bind , 'dialect' , None ) if ( dialect is None ) : from sqlalchemy . dialects import mysql dialect = mysql . dialect ( ) Compiler = type ( statement . _compiler ( dialect ) ) class LiteralCompiler ( Compiler , ) : visit_bindparam = Compiler . render_literal_bindparam def render_literal_value ( self , value , type_ ) : if isinstance ( value , ( Decimal , long ) ) : return str ( value ) elif isinstance ( value , datetime ) : return repr ( str ( value ) ) else : value = super ( LiteralCompiler , self ) . render_literal_value ( value , type_ ) if isinstance ( value , unicode ) : return value . encode ( 'UTF-8' ) else : return value return LiteralCompiler ( dialect , statement )
generate an sql expression string with bound parameters rendered inline for the given sqlalchemy statement .