这是使用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*)
而且我无法破解我生命中的错误信息。
答案 0 :(得分:2)
错误消息不是特别有用。 sql
是一个宏,不会评估它的参数,而是根据其未评估的参数做一些事情。如果它不是宏,(sql (:insert-rows-into ...))
将是一个错误,因为要评估它需要首先评估(:insert-rows-into ...)
,而:insert-rows-into
不是定义的函数。
实现这些类型的宏的常用方法是使用(defun expand-sql-code (code) ...)
之类的函数实现代码转换,然后提供宏版本(defmacro sql (code) (expand-sql-code ',code))
。
这是后现代的sql
宏的定义:
(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)))))
看起来sql-expand
正在完成大部分繁重的工作,所以你可能有兴趣调用它。例如:
(s-sql::sql-expand '(: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 "
(S-SQL::EXPAND-ROWS
'(("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,"))
3))
但是,您仍然需要将字符串连接应用于结果。原始形式的宏观扩展具有指导意义:
(macroexpand-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,")))))
(S-SQL::STRCAT
(LIST "INSERT INTO scrape (term, name, address) VALUES "
(S-SQL::EXPAND-ROWS
'(("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,"))
3)))
T
总而言之,因为没有一个唯一的函数可以实现sql
转换,所以这可能是最简单的选项可能只是使用eval
的情况之一:
(eval `(postmodern:sql ,*sql*)) ; or (eval (cons 'postmodern:sql *sql*))
;=> "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,')"
答案 1 :(得分:1)
尝试sql-compile功能。这是“sql宏的运行时变体。”