1

这是使用postmodern.

这有效:

(sql (:INSERT-ROWS-INTO 'SCRAPE :COLUMNS 'TERM 'NAME 'ADDRESS :VALUES
  '(("wine" "Bubba Wine & Spirits"
 "1234 N San Fake Rd,")
    ("wine" "Wine Shop"
     "123 Not Real Blvd,")
    ("wine" "Some Wine Group"
     "777 S Does Not Exist Ave,"))))

回报:

"INSERT INTO scrape (term, name, address) 
 VALUES (E'wine', E'Bubba Wine & Spirits', E'1234 N San Fake Rd,'), 
        (E'wine', E'Wine Shop', E'123 Not Real Blvd,'), 
        (E'wine', E'Some Wine Group', E'777 S Does Not Exist Ave,')"

这不起作用:

(defvar *sql* '(:INSERT-ROWS-INTO 'SCRAPE :COLUMNS 'TERM 'NAME 'ADDRESS :VALUES
  '(("wine" "Bubba Wine & Spirits"
 "1234 N San Fake Rd,")
    ("wine" "Wine Shop"
     "123 Not Real Blvd,")
    ("wine" "Some Wine Group"
     "777 S Does Not Exist Ave,"))))

(sql *sql*)

这给了我:

Value (:INSERT-ROWS-INTO 'SCRAPE :COLUMNS 'TERM 'NAME 'ADDRESS
       :VALUES
       '(("wine" "Bubba Wine & Spirits"
          "1234 N San Fake Rd,")
         ("wine" "Wine Shop" "123 Not Real Blvd,")
         ("wine" "Some Wine Group" "777 S Does Not Exist Ave,"))) can not be converted to an SQL literal.
   [Condition of type SIMPLE-ERROR]

Restarts:
 0: [ABORT] Exit debugger, returning to top level.

Backtrace:
  0: ((SB-PCL::FAST-METHOD CL-POSTGRES:TO-SQL-STRING (T)) #<unavailable argument> #<unavailable argument> (:INSERT-ROWS-INTO 'SCRAPE :COLUMNS 'TERM 'NAME 'ADDRESS ...))
  1: ((SB-PCL::FAST-METHOD SQL-ESCAPE (T)) #<unavailable argument> #<unavailable argument> (:INSERT-ROWS-INTO 'SCRAPE :COLUMNS 'TERM 'NAME 'ADDRESS ...))
  2: (SB-INT:SIMPLE-EVAL-IN-LEXENV (SQL *SQL*) #<NULL-LEXENV>)
  3: (EVAL (SQL *SQL*))
  4: (SB-EXT:INTERACTIVE-EVAL (SQL *SQL*) :EVAL NIL)
  5: (SB-IMPL::REPL-FUN NIL)
  6: ((LAMBDA () :IN SB-IMPL::TOPLEVEL-REPL))
  7: (SB-IMPL::%WITH-REBOUND-IO-SYNTAX #<CLOSURE (LAMBDA # :IN SB-IMPL::TOPLEVEL-REPL) {BFC3BFD}>)
  8: (SB-IMPL::TOPLEVEL-REPL NIL)
  9: (SB-IMPL::TOPLEVEL-INIT)
 10: ((FLET #:WITHOUT-INTERRUPTS-BODY-223764 :IN SB-EXT:SAVE-LISP-AND-DIE))
 11: ((LABELS SB-IMPL::RESTART-LISP :IN SB-EXT:SAVE-LISP-AND-DIE))

所以我很明显搞砸了(sql *sql*),我一生都无法破译错误信息。

4

2 回答 2

2

The error message isn't particularly useful. sql is a macro and doesn't evaluate its arguments, but does something based on the its unevaluated argument. If it weren't a macro, (sql (:insert-rows-into ...)) would be an error, because to evaluate it would require evaluating (:insert-rows-into ...) first, and :insert-rows-into isn't a defined function.

A common way of implementing these types of macros is to implement the code transformation with a function like (defun expand-sql-code (code) ...) and then provide a macro version (defmacro sql (code) (expand-sql-code ',code)).

This is the definition of Postmodern's sql macro:

(defmacro sql (form)
  "Compile form to an sql expression as far as possible."
  (let ((list (reduce-strings (sql-expand form))))
    (if (= 1 (length list))
        (car list)
        `(strcat (list ,@list)))))

It looks like sql-expand is doing most of the heavy lifting, so you might be interested in calling it. For instance:

(s-sql::sql-expand '(:INSERT-ROWS-INTO 'SCRAPE :COLUMNS 'TERM 'NAME 'ADDRESS :VALUES
                     '(("wine" "Bubba Wine &amp; Spirits"
                        "1234 N San Fake Rd,")
                       ("wine" "Wine Shop"
                        "123 Not Real Blvd,")
                       ("wine" "Some Wine Group"
                        "777 S Does Not Exist Ave,"))))

("INSERT INTO " "scrape" " " "(" "term" ", " "name" ", " "address" ") "
 "VALUES "
 (S-SQL::EXPAND-ROWS
  '(("wine" "Bubba Wine &amp; Spirits" "1234 N San Fake Rd,")
    ("wine" "Wine Shop" "123 Not Real Blvd,")
    ("wine" "Some Wine Group" "777 S Does Not Exist Ave,"))
  3))

You'd still need to apply the string concatenation though to the result, though. The macro expansion of your original form is instructive:

(macroexpand-1 '(postmodern:sql (:INSERT-ROWS-INTO 'SCRAPE :COLUMNS 'TERM 'NAME 'ADDRESS :VALUES
                                 '(("wine" "Bubba Wine &amp; Spirits"
                                    "1234 N San Fake Rd,")
                                   ("wine" "Wine Shop"
                                    "123 Not Real Blvd,")
                                   ("wine" "Some Wine Group"
                                    "777 S Does Not Exist Ave,")))))
(S-SQL::STRCAT
 (LIST "INSERT INTO scrape (term, name, address) VALUES "
       (S-SQL::EXPAND-ROWS
        '(("wine" "Bubba Wine &amp; Spirits" "1234 N San Fake Rd,")
          ("wine" "Wine Shop" "123 Not Real Blvd,")
          ("wine" "Some Wine Group" "777 S Does Not Exist Ave,"))
        3)))
T

All in all, though, since there's not a single unique function that implements the sql transformation, this might be one of those cases where the easiest option might just be to use eval:

(eval `(postmodern:sql ,*sql*)) ; or (eval (cons 'postmodern:sql *sql*))
;=> "INSERT INTO scrape (term, name, address) VALUES (E'wine', E'Bubba Wine &amp; Spirits', E'1234 N San Fake Rd,'), (E'wine', E'Wine Shop', E'123 Not Real Blvd,'), (E'wine', E'Some Wine Group', E'777 S Does Not Exist Ave,')"
于 2013-11-08T04:22:19.027 回答
1

试试 sql-compile 函数。这是“sql 宏的运行时变体”。

于 2013-11-09T03:38:13.480 回答