From 9c0cfd2ef1ee5b236fa870061127052689dd581a Mon Sep 17 00:00:00 2001 From: Russ Tyndall Date: Fri, 18 Dec 2009 12:28:18 -0500 Subject: [PATCH] rewrote output-sql on relational-expressions to again try to make empty or/ands not render out (sql-and a) is output as simply a re ADWolf:#610 --- sql/expressions.lisp | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/sql/expressions.lisp b/sql/expressions.lisp index 5967f24..304cb3b 100644 --- a/sql/expressions.lisp +++ b/sql/expressions.lisp @@ -245,18 +245,32 @@ (with-slots (operator sub-expressions) expr (when sub-expressions - (let ((subs (if (consp (car sub-expressions)) - (car sub-expressions) - sub-expressions))) - (write-char #\( *sql-stream*) - (do ((sub subs (cdr sub))) - ((null (cdr sub)) - (output-sql (car sub) database)) - (output-sql (car sub) database) - (write-char #\Space *sql-stream*) - (output-sql operator database) - (write-char #\Space *sql-stream*)) - (write-char #\) *sql-stream*)))) + (let (has-written) + ;; we do this as two runs so as not to emit confusing superflous parentheses + ;; The first loop renders all the child outputs so that we can skip anding with + ;; empty output (which causes sql errors) + ;; the next loop simply emits each sub-expression with the appropriate number of + ;; parens and operators + (let ((str-subs (loop for sub in sub-expressions + for str-sub = (string-trim '(#\space #\newline #\return #\tab #\no-break_space) + (with-output-to-string (*sql-stream*) + (output-sql sub database))) + when (and str-sub (> (length str-sub) 0)) + collect str-sub + ))) + (loop for str-sub in str-subs + do + (progn + (when (and (not has-written) + (> (length str-subs) 1)) + (write-char #\( *sql-stream*)) + (when has-written + (write-char #\Space *sql-stream*) + (output-sql operator database)) + (write-string str-sub *sql-stream*) + (setf has-written T))) + (when (and has-written (> (length str-subs) 1)) + (write-char #\) *sql-stream*)))))) t) (defclass sql-array-exp (sql-relational-exp) -- 2.34.1