2 messages in com.googlegroups.sqlalchemy[sqlalchemy] Re: String to query (pyt...
FromSent OnAttachments
maxi27 Mar 2008 14:31 
Michael Bayer29 Mar 2008 06:54 
Subject:[sqlalchemy] Re: String to query (python) expression
From:Michael Bayer (mike@zzzcomputing.com)
Date:03/29/2008 06:54:31 AM
List:com.googlegroups.sqlalchemy

On Mar 27, 2008, at 5:31 PM, maxi wrote:

Hi,

Is possible (Are there some built-in function?) to convert a string sql expresion to sqlalchemy expression?

I will try to explain this with an example....

An user_table and User mapped class:

sql_where = 'name like '%John%'

sql_where -> User.name.like('John')

sql = user_table.select() sql.append_whereclause(sql_converted_expresion)

Or....

param = ['name', 'like', '%John%']

and convert param list into sqlalchemy expression

Is possible do anything like this? (I hope be clear)

hey there -

no, thats not built in, it would require that you construct a SQL parser which generates a construct as it encounters tokens. Ned Batchelder did a great blog post on available parsers for Python:
http://nedbatchelder.com/text/python-parsers.html . In particular is the "kwParsing" module, used by Gadfly:
http://gadfly.sourceforge.net/ . Thats a SQL database written in Python so they are definitely parsing SQL in there. Their parsing approach could almost certainly be repurposed to output SQLA tokens (or any kind of tokens) with some degree of effort.