当然是可以的。不过,这需要一些工作。
大致步骤如下:
1. 使用
ini.el 读取和解析 ini 文件。
2. 将不同主机的配置与客户端的配置合并,因为后者是全局应用的。
3. 将每个主机转换为适合
sql-connection-alist
的格式。
4. 填充
sql-connection-alist
。
5. 使用
M-x sql-connect
(自动完成!)。
以下代码还包括一个 .pgpass 解析器,以防万一。您会注意到实现更为简单。
(defun read-file (file)
"Returns file as list of lines."
(with-temp-buffer
(insert-file-contents file)
(split-string (buffer-string) "\n" t)))
(defun pgpass-to-sql-connection (config)
"Returns a suitable list for sql-connection-alist from a pgpass file."
(let ((server (lambda (host port db user _pass)
(list
(concat db ":" user ":" port ":" host)
(list 'sql-product ''postgres)
(list 'sql-server host)
(list 'sql-user user)
(list 'sql-port (string-to-number port))
(list 'sql-database db))))
(pgpass-line (lambda (line)
(apply server (split-string line ":" t)))))
(mapcar pgpass-line config)))
(defun ini-decode (ini_text)
(interactive)
(if (not (stringp ini_text))
(error "Must be a string"))
(let ((lines (split-string ini_text "\n"))
(section)
(section-list)
(alist))
(dolist (l lines)
(unless (or (string-match "^;" l)
(string-match "^[ \t]$" l))
(if (string-match "^\\[\\(.*\\)\\]$" l)
(progn
(if section
(setq alist (cons `(,section . ,section-list) alist))
(setq alist section-list))
(setq section (match-string 1 l))
(setq section-list nil)))
(if (string-match "^\\([^\s\t]+\\)[\s\t]*=[\s\t]*\\(.+\\)$" l)
(let ((property (match-string 1 l))
(value (match-string 2 l)))
(progn
(setq section-list (cons `(,property . ,value) section-list)))))))
(if section
(setq alist (cons `(,section . ,section-list) alist))
(setq alist section-list))
alist))
(defun read-ini (file)
"Returns ini file as alist."
(with-temp-buffer
(insert-file-contents file)
(ini-decode (buffer-string))))
(defun filter-alist (wanted-members alist)
"Returns a copy of given alist, with only fields from wanted-members."
(let ((result nil)
(add-if-member (lambda (elt)
(when (member (car elt) wanted-members)
(add-to-list 'result elt t)))))
(mapc add-if-member alist)
result))
(defun merge-alist (original override)
"Returns a union of original and override alist. On key conflict, the latter wins."
(let ((result (copy-alist override))
(add (lambda (elt)
(setq result (add-to-list
'result elt t
(lambda (left right) (equal (car left) (car right))))))))
(mapc add original)
result))
(defun parse-mycnf-hosts (file-path)
"Returns list of hosts with clients' section applied to all hosts."
(let ((hosts nil)
(global nil)
(fields '("user" "host" "database" "password" "port"))
(section-parse (lambda(section)
(if (equal (car section) "client")
(setq global (filter-alist fields (cdr section)))
(let ((host (car section))
(config (filter-alist fields (cdr section))))
(when config (add-to-list 'hosts (cons host config) t))))))
(merge-host-with-global (lambda (host)
(cons (car host) (merge-alist global (cdr host))))))
(mapc section-parse (read-ini file-path))
(mapcar merge-host-with-global hosts)))
(defun mycnf-to-sql-connection (config)
(let ((add-sql-product
(lambda (config)
(let ((head (car config))
(tail (cdr config)))
(cons head (append tail (list (list 'sql-product ''mysql)))))))
(parse-keys-and-values
(lambda (config)
(let ((head (car config))
(tail (cdr config)))
(cons
head
(mapcar
(lambda (element)
(let ((key (car element))
(value (cdr element)))
(cond ((equal key "host") (list 'sql-server value))
((equal key "port") (list 'sql-port (string-to-number value)))
((equal key "user") (list 'sql-user value))
((equal key "password") (list 'sql-password value))
((equal key "database") (list 'sql-database value))
(t (error (format "Unknown key %s" key))))))
tail))))))
(mapcar add-sql-product (mapcar parse-keys-and-values config))))
(setq sql-connection-alist
(append
(mycnf-to-sql-connection (parse-mycnf-hosts "~/.my.cnf"))
(pgpass-to-sql-connection (read-file "~/.pgpass"))
))
使用以下 .my.cnf
文件:
[client]
user=me
[host1]
database=db1
host=db.host1.com
[host2]
database=db2
user=notme
host=db.host2.com
执行表达式
(mycnf-to-sql-connection (parse-mycnf-hosts "~/.my.cnf"))
会给我返回以下结果(手动美化输出):
(("host2" ((sql-server "db.host2.com")
(sql-user "notme")
(sql-database "db2")))
("host1" ((sql-server "db.host1.com")
(sql-database "db1")
(sql-user "me"))))
最后,不要使用
M-x sql-mysql
,而是使用
M-x sql-connect
,这样你就可以使用别名进行连接,并且具有自动补全功能。
flet
宏已被弃用,我使用了来自此包 https://github.com/nicferrier/emacs-noflet 的noflet
进行替换,并且它可以正常工作。 - cjauvin