SQL is already a declarative query DSL, why not just use it? Putting another declarative layer on top buys you little (syntactic sugar for most libraries, this one additionally seems to deal with one-to-many relations, and I hope that support is read-write, not just read-only) — and isolates you from the database below.
In every serious application I've worked on there was a need to use something which the DSL did not support. So why do it at all?
For example, if your PERSON entity has TITLE, FIRST_NAME and LAST_NAME fields, that little bit of logic that concatenates them together in the right way to form a full name is something that is useful to define. Likewise, setting up a join of tables that is used many times in your application is something that Korma will let you do.
Even though these components may be implementable on the database side (as user defined functions or views), it may not always be possible (or desirable) to do that way.
I'm not a Clojure user but as I have some authority on this subject I can illustrate your query as SQLAlchemy:
select([table.c.col1, table.c.col2,
func.some_function().
over(
partition_by=[table.c.col1, table.c.col2],
order_by=[table.c.col1,table.c.col2])
.label('some_ck')])
.where(<some condition>)But perhaps it's just a matter of getting used to a different format.
[1] Legacy environment.
[2] Edited to add: most of our newer apps don't actually invoke huge queries from the app layer. We now tend to layer a view or pl/sql table function on top of the hairy query, so that at the app level it's mostly "select * from blah." For that simple a query, a DSL like this is probably fine, albeit, unnecessary.
And if so, can I use SQLAlchemy as a standalone pre-processor? My app's in Rails so I can't exactly go integrate it..
yes. many DB specific constructs are included and you can make your own: http://www.sqlalchemy.org/docs/core/compiler.html
philosophically SQLA does not consider "abstraction" to be "hiding", so ultimately no features of the backend store are concealed
> like create a Postgres user-defined type
sure, the ENUM type does this right now automatically (i.e. CREATE TYPE...AS ENUM). For other kinds of PG types, you can emit the CREATE TYPE yourself then link to it with a SQLAlchemy-side custom type. You can integrate those two steps also.
> or a SQL function?
Again a SQL function you're going to be using PG's PL/pgSQL or other embedded language to create it, so you roll the CREATE FUNCTION yourself, then call it on the Python side using func: http://www.sqlalchemy.org/docs/core/tutorial.html#functions
I work with a number of legacy data sources that often have models that are not ORM-friendly.
I fall into SQL code patterns that seem left behind by tools like this on a regular basis, particularly using SQL functions without default grouping because I'll select other stuff higher up.
A simple example:
This is often part of a larger query (e.g., this is embedded in bowels of a some longer "chain" of SQL where I will be using some_ck to decide on which tuples I want in the end).I get pretty significant mileage out of this - meaning I write much less other code around it to handle the data.
I guess my complaint is that I often find the SQL DSLs lacking in expressiveness that I use regularly enough to miss.
Of course, this could be because I've spent too much time hacking around in legacy data in "real soon now" mode rather than in "hmm . . . let's clean up this data model" mode.