这是一个让您入门的基本实现:
(defun prompt-for-sql-statement (table type fields)
(interactive
(list
(read-from-minibuffer "Table name? ")
(completing-read "Type of statement? " '("select" "insert" "update" "delete"))
(let (field fields (index 1))
(while (not (string= "" (setq field (read-from-minibuffer (format "Field #%d: " index)))))
(setq fields (cons field fields) index (1+ index)))
(mapconcat 'identity (nreverse fields) ", "))))
(insert type " (" fields ") from " table))
当您键入M-x prompt-for-sql-statement
(或键入您已将命令绑定到的键序列)时,您将收到一系列提示:
Table name? myTable
Type of statement? select
Field #1: foo
Field #2: bar
Field #3: baz
Field #4:
您可以对语句类型进行制表符补全,空字段将终止列表。然后,该函数将在您调用命令时的任何位置插入构造的 SQL 语句。
所写的命令将生成看起来像 SELECT 的 SQL 语句(“select ... from table”、“insert ... from table”等)。更聪明的实现会知道如何为每种类型的 SQL 语句生成正确的语法。