使用sql-send-buffer
,我可以将SQL查询从文件发送到打开的SQL REPL。我的许多查询都有参数占位符(在Postgres语法中,$1
,$2
等)。是否有人有类似于sql-send-buffer
的代码,它们会提示您为这些参数填写值?理想情况下,我希望它存储我提供的值,除非添加参数或关闭文件,否则不要再次提示。
目前我是:
答案 0 :(得分:0)
也许是这样的
(defvar my-sql-replacements nil)
(make-variable-buffer-local 'my-sql-replacements)
(defun my-sql-send-buffer-replace ()
(interactive)
(let ((string (buffer-substring-no-properties (point-min) (point-max))))
(while (string-match "[$][0-9]" string)
(let* ((placeholder (match-string 0 string))
(replacement (or (cdr (assoc placeholder my-sql-replacements))
(read-string (format "Replacement for %s: " placeholder)))))
(unless (assoc placeholder my-sql-replacements)
(push (cons placeholder replacement) my-sql-replacements))
(setq string (replace-regexp-in-string (regexp-quote placeholder) replacement string))))
(sql-send-string string)))
我尚未在实际的SQL Server上进行过测试,但是通过跟踪sql-send-string
看来它应该可以工作。它将替换项存储在缓冲区局部变量中。