Lisp HUG Maillist Archive

Generating SQL strings

Hi,

I'm composing what will turn out to be a fairly lengthy SQL statement
using the SQL package. I'm using the reader syntax (and in places the
functional interface) to construct the query dynamically. When it
comes to some constructs that don't appear to be supported by the
syntax, such as Common Table Expressions [1] I plan to fall back on
string interpolation in order to put generated sub-selects into place.

However, once I have an SQL-QUERY object, I haven't found the accessor
function to call to get the generated statement, and have fallen back
on SLOT-VALUE.

(slot-value [select [*] :from [foo]] 'sql::val)
; => "(SELECT * FROM FOO)"

Is there a better way for composing SQL that isn't supported by the
reader syntax (I tried using SQL-OPERATION and other functions for
WITH ... AS but failed). If not, is there a better way for obtaining
the string from an SQL-QUERY without sending it to the database?

Cheers,
Andrew Kirkpatrick

[1] http://www.postgresql.org/docs/9.1/static/queries-with.html

_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html


Re: Generating SQL strings

On 12 August 2015 at 11:18, Andrew Kirkpatrick <ubermonk@gmail.com> wrote:
> Is there a better way for composing SQL that isn't supported by the
> reader syntax (I tried using SQL-OPERATION and other functions for
> WITH ... AS but failed). If not, is there a better way for obtaining
> the string from an SQL-QUERY without sending it to the database?

Answering one of my own questions;

(sql::db-query-val [select [*] :from [foo]])
; => "(SELECT * FROM FOO)"

But I still wonder if there's a way to use/extend the
symbolic/functional SQL to emit non-standard SQL syntax like common
table expressions or lateral queries.

Cheers,
Andy

_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html


Updated at: 2020-12-10 08:33 UTC