News for hackers. Stuff that matters.
Re:Records: Python library for making raw SQL queries to Pos... by drdaeman on Sunday February 07, 2016 @01:17PM (#11053958)
It's sad that ORMs are always restrictive and raw SQL can't be safely and easily modified. I'd always wished for a library that one could just write SQL, and the library could do the transformations.

I.e. one writes plain SQL strings, but they're parsed under the hood, so AST transformations can be applied, like adding extra WHERE clauses conditionally or applying LIMIT.

Sadly, I haven't found any good Python SQL parsing library. That was long time ago, though - maybe someone had written one already.

Re:Records: Python library for making raw SQL queries to Pos... by josefdlange on Sunday February 07, 2016 @01:41PM (#11054078)
Peewee [1] is my tool of choice because of its convenience and flexibility combined. I can create pretty complex queries using its Query API, but also execute arbitrary queries when I know I need something a little more nonstandard. Plus the maintainer (Charles Leifer) is an awesome dude who has always been really helpful to his users.

[1] Peewee: https://github.com/coleifer/peewee

Re:Records: Python library for making raw SQL queries to Pos... by coleifer on Sunday February 07, 2016 @02:58PM (#11054512)
Thanks for the kind words!
Re:Records: Python library for making raw SQL queries to Pos... by deno on Sunday February 07, 2016 @01:58PM (#11054188)
That’s sort of like SQL Alchemy Core works[1]. The ORM stuff is all built on top of that and completely optional.

[1] http://docs.sqlalchemy.org/en/latest/core/tutorial.html

Re:Records: Python library for making raw SQL queries to Pos... by drdaeman on Sunday February 07, 2016 @02:07PM (#11054232)
Unfortunately, no, that's not what I meant, sorry.

I can't give SQLAlchemy (Core or ORM) a string like "SELECT * FROM foo AS f LEFT JOIN bar AS b ON b.id = f.bar_id WHERE f.baz > %(baz)s" and then transform it, by, say, appending the LIMIT clause or adding extra WHERE condition. AFAIK, there's no way to provide a raw SQL string and then say something like `query.where("NOT f.fnord")` OR `query.limit(10)` and get the updated SQL.

With ORMs or non-object-mapping wrappers if I want transformations, I have to use their own language instead of SQL. I do, but don't really want to.

Or things have changed and this is what SA can do this nowadays? I'll be more than happy to learn that I'm wrong.

Re:Records: Python library for making raw SQL queries to Pos... by zzzeek on Sunday February 07, 2016 @11:17PM (#11056303)
you know what the problem is there, that a. parsing your SQL string into a tokenized structure b. altering it based on your given instructions c. re-compiling it back into a string, all on top of an interpreted scripting language, and then d. sending it to a database so that the database can parse it a second time, is just so inefficient, for a particular API style that is arguably not even any better than just learning how to write Core expressions. Core and ORM expressions can be cached to their resultant SQL to save on the tokenized-structure->string step too.

there are certainly SQL parsers that can easily produce such tokenized structures and from a technical standpoint, your API is pretty simple to produce, with or without shallow or deep SQLAlchemy integrations. It's just there's not really any interest in such a system and it's never been requested.

Re:Records: Python library for making raw SQL queries to Pos... by ForHackernews on Sunday February 07, 2016 @02:18PM (#11054296)
You actually can do that with SA. See the example here: http://docs.sqlalchemy.org/en/latest/core/tutorial.html#spec...

If you specify the columns involved with your text query, then you get back a 'TextAsFrom' object, and you can apply other transformations to it like .where() or .unique().

Re:Records: Python library for making raw SQL queries to Pos... by stuaxo on Sunday February 07, 2016 @06:48PM (#11055602)
At least you can do that by adding other SA objects.

I used to do exactly this kind of filtering, by building up by SA query, back in the day (about 2008) when I last used SA in anger.