Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLAlchemy: limit in the same string as where

We're trying to enable a SQL query front-end to our Web application, which is WSGI and uses Python, with SQLAlchemy (core, not ORM) to query a PostgreSQL database. We have several data layer functions set up to assist in query construction, and we are now trying to set something up that allows this type of query:

select id from <table_name> where ... limit ...

In the front end, we have a text box which lets the user type in the where clause and the limit clause, so that the data can be queried flexibly and dynamically from the front end, that is, we want to enable ad hoc querying. So, the only thing that we now firsthand is:

select id from <table_name>

And the user will type in, for example:

where date > <some_date>
where location is not null limit 10 order by location desc

using the same back end function. The select, column and table should be managed by the data layer (i.e. it knows what they are, and the user should not need to know that). However, I'm not aware of any way to get SQLAlchemy to automatically parse both the where clause and the limit clause automatically. What we have right now is a function which can return the table name and the name of the id column, and then use that to create a text query, which is passed to SQLAlchemy, as the input to a text() call.

Is there any way I can do this with SQLAlchemy, or some other library? Or is there a better pattern of which I should be aware, which does not involve parsing the SQL while still allowing this functionality from the front-end?

Thanks a lot! All suggestions will be greatly appreciated.

like image 226
Juan Carlos Coto Avatar asked Jan 22 '13 00:01

Juan Carlos Coto


People also ask

What is offset in SQLAlchemy?

queries – A sequence of SQLAlchemy Query objects. limit – Similar to normal query limit this parameter can be used for limiting the number of results for the whole query chain. offset – Similar to normal query offset this parameter can be used for offsetting the query chain as a whole.

Is SQLAlchemy faster than sqlite?

Interesting to note that querying using bare sqlite3 is still about 3 times faster than using SQLAlchemy Core. I guess that's the price you pay for having a ResultProxy returned instead of a bare sqlite3 row. SQLAlchemy Core is about 8 times faster than using ORM. So querying using ORM is a lot slower no matter what.

What is aliased in SQLAlchemy?

SQL alias is a method of giving a temporary name for a table that is more convenient and readable. SQL alias facilitates a simple name to be used in place of a complex table name when it has to be used multiple times in a query. The alias() function in sqlalchemy. sql module represents an SQL alias.

What is lazy true in SQLAlchemy?

Lazy parameter determines how the related objects get loaded when querying through relationships. Below listed are the four main lazy parameters. Typically when you query the database, the data get loaded at once; however, lazy parameter allows you to alternate the way they get loaded. lazy = 'select' (or True)


1 Answers

I'm not sure I follow, but the general SQL-Alchemy usage is like:

 results = db.session.query(User).filter(User.name == "Bob").order_by(User.age.desc()).limit(10)

That will query the User table to return the top ten oldest members named "Bob"

like image 186
reptilicus Avatar answered Sep 19 '22 08:09

reptilicus