News for hackers. Stuff that matters.
Re:Meet Korma, a SQL DSL for Clojure by tom_b on Wednesday November 02, 2011 @04:14PM (#3188722)
I am attracted to these types of DSLs but . . .

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:

  select
    col1,
    col2,
    some_function() over (partition by col1,col2 order by col1,col2) as some_ck
  where <some conditions>
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.

Re:Meet Korma, a SQL DSL for Clojure by jwr on Wednesday November 02, 2011 @04:21PM (#3188753)
I think writing DSLs for SQL makes little sense.

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?

Re:Meet Korma, a SQL DSL for Clojure by technomancy on Wednesday November 02, 2011 @04:33PM (#3188806)
Raw SQL is not very composable, while Korma emphasizes composability on the second paragraph of the page.
Re:Meet Korma, a SQL DSL for Clojure by hosh on Wednesday November 02, 2011 @05:18PM (#3188977)
And composability lets you create business DSLs and macros on top of that. I've used this to good effect with Rails' Arel, but since it is still Ruby, you can't go as far unless you're willing to parse the Ruby into something more legible, like S-expressions.
Re:Meet Korma, a SQL DSL for Clojure by its2010already on Wednesday November 02, 2011 @04:37PM (#3188826)
It does make sense to be able to define business/application logic as reusable query components.

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.

Re:Meet Korma, a SQL DSL for Clojure by pradocchia on Wednesday November 02, 2011 @10:08PM (#3189864)
I spend much of my time in the database, legacy and greenfield, and I would love a better DSL than SQL, something that ran directly in the database itself and targeted the planner/optimizer rather than just emitting more SQL. I would like to be able to pass a WHERE clause to a function, or a SELECT statement to a procedure, not just simple ints, or strings to be exec'd.
Re:Meet Korma, a SQL DSL for Clojure by groovy2shoes on Wednesday November 02, 2011 @04:28PM (#3188790)
In this case, an embedded DSL brings all the goodies of Lisp's code-as-data to SQL.
Re:Meet Korma, a SQL DSL for Clojure by zzzeek on Wednesday November 02, 2011 @06:29PM (#3189238)
a SQL DSL and an ORM aren't really the same thing IMHO. ORMs are about linking objects in memory to database rows, whereas the SQL DSL is just about a thin layer over the rendering of a SQL string, not unlike how you could compose a DOM object of an XML or HTML document and get a string out of it.

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>)
Re:Meet Korma, a SQL DSL for Clojure by msluyter on Wednesday November 02, 2011 @07:44PM (#3189458)
I guess I've been corrupted by working in a heavily database oriented environment, and further corrupted by the fact that it's Oracle, but for most of our own use cases, plain SQL is preferable to this. We'll probably have to run it ad hoc against the database anyway to figure out what it's doing and/or tune it. Many of our queries go into the hundreds of lines[1][2], so anything that obscures the underlying SQL itself tends to make life more difficult.

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.

Re:Meet Korma, a SQL DSL for Clojure by zzzeek on Wednesday November 02, 2011 @08:47PM (#3189626)
I wrote SQLAlchemy after spending about eight years in three all-Oracle jobs one after the next, and I wrote it specifically around the use case of being able to compose hundreds-of-lines queries from smaller pieces.
Re:Meet Korma, a SQL DSL for Clojure by jaylevitt on Thursday November 03, 2011 @08:11AM (#3191364)
Can SQLAlchemy core do arbitrary, non-ANSI things, like create a Postgres user-defined type or a SQL function?

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..

Re:Meet Korma, a SQL DSL for Clojure by zzzeek on Sunday November 06, 2011 @12:05PM (#3203143)
> Can SQLAlchemy core do arbitrary, non-ANSI things

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

Re:Meet Korma, a SQL DSL for Clojure by msluyter on Thursday November 03, 2011 @09:28AM (#3191605)
Fair enough. I'll check it out.
Re:Meet Korma, a SQL DSL for Clojure by ghotli on Wednesday November 02, 2011 @08:46PM (#3189623)
SQL that is hundreds of lines long can be hard to follow and harder to maintain. SQL is probably the fastest path to execution but perhaps not the most testable and maintainable. Perhaps that's the lens to view it through.