道友能分享一下emacs 与 数据库,sql 的工作流吗?

道友能分享一下emacs 与 数据库(如果是pg最好了),sql 的工作流吗?

怎么使用 emacs 操作数据库是比较舒服的,是写原生 sql ,还是借用其它库?

同,目前好像没有什么可用的 emacs 上的 sql client

你是想要一个现成的 sql client 还是只是想要一个能够访问数据库的API?

我怎么感觉这两个都需要

我感觉php pdo操作数据库就比较舒服,因为可以统一用同一套api来操作不同的数据库。不用切换一个数据库时操作的方法也要换。

org-mode和php如果使用http来通信相对于大佬们使用RPC,IPC来通信真的是要简单容易很多。

确实没啥好用的,我就简单配了一下

;; use interactive contents in the sql commint buffer
;; for dabbrev completions in the sql mode
(add-hook 'sql-mode-hook
	  (lambda () (setq-local dabbrev-select-buffers-function
			    (lambda () (list (eval 'sql-buffer))))))

(defun pgformatter-buffer ()
  "A function to invoke pgFormatter as an external program."
  (interactive)
  (if (derived-mode-p 'sql-mode)
      (progn (shell-command (format "/usr/bin/pg_format -i %s" (buffer-file-name)))
	     (revert-buffer nil t))
    (message "This is not a SQL file")))

  (define-key sql-mode-map (kbd "C-c C-f") #'pgformatter-buffer)

如果要 API, 可以尝试一下 deno-bridge, Deno 用的是 TypeScript, TypeScript 访问各种数据库都非常成熟。

现成的 SQL Client我没调研过, 不清楚。

这个思路,用 JavaScript做数据库链接层,emacs 显示返回的数据,确实可行,

但自己 emacs 显示返回的数据这方面有些菜 :sneezing_face:

看 deno-bridge README 吧, 并不复杂。

正在看, :joy:

我是用的 lsp-mode 里整合好的,直接 lsp-sql-execute-query 就好了

直接用lsp吧,sqls目前算是可用的,连接上数据库后,可以提供数据库的表名 列名之类的自动补全,以及执行sql query啥的

用 vterm 打开一个 pry,用 Rails 的 Active Record 操作DB

[email protected](main) [1] ❯ l  # List all tables(and model classes)

+------------+-------------+------+---------+
| Table Name | Model Class | Abbr | Comment |
+------------+-------------+------+---------+
| post       | Post        | P    |         |
| user       | User        | U    |         |
| org        | Org         | O    |         |
| user_org   | UserOrg     | UO   |         |
| student    | Student     | S    |         |
| course     | Course      | C    |         |
| score      | Score       | S2   |         |
+------------+-------------+------+---------+
[email protected](main) [2] ❯ P.t   # Print DB table structure as a table

Table: post
+----+--------------+------------------+-----------+-------+-----------+-------+---------+----------+--------------+
| PK | Name         | SQL Type         | Ruby Type | Limit | Precision | Scale | Default | Nullable | Comment      |
+----+--------------+------------------+-----------+-------+-----------+-------+---------+----------+--------------+
| Y  | id           | int(10) unsigned | integer   | 4     |           |       |         | false    | ID           |
|    | name         | varchar(256)     | string    | 256   |           |       |         | true     |              |
|    | gender       | varchar(256)     | string    | 256   |           |       |         | true     |              |
|    | phone        | varchar(256)     | string    | 256   |           |       |         | true     |              |
|    | id_no        | varchar(256)     | string    | 256   |           |       |         | true     |              |
|    | note         | varchar(256)     | string    | 256   |           |       |         | true     |              |
|    | gmt_created  | datetime         | datetime  |       | 0         |       |         | false    | 创建时间     |
|    | gmt_modified | datetime         | datetime  |       | 0         |       |         | false    | 最后修改时间 |
+----+--------------+------------------+-----------+-------+-----------+-------+---------+----------+--------------+
=> nil
[email protected](main) [3] ❯ P.count   # Get total records count of the post table
=> 15
[email protected](main) [4] ❯ show-sql   # Turn on SQL log
[email protected](main) [5] ❯ P.count  # Get total records count of the post table
D, [2022-10-17T16:16:49.679907 #79805] DEBUG -- :   Post Count (9.0ms)  SELECT COUNT(*) FROM `post`
=> 15
[email protected](main) [6] ❯ P.all.t    # Print all post records as a table
D, [2022-10-17T16:17:04.541135 #79805] DEBUG -- :   Post Load (9.5ms)  SELECT `post`.* FROM `post`
+----+----------+--------+-------+----------------------------------+------+---------------------------+---------------------------+
| id | name     | gender | phone | id_no                            | note | gmt_created               | gmt_modified              |
+----+----------+--------+-------+----------------------------------+------+---------------------------+---------------------------+
| 67 | David    | M      |       | 3cc4ccbbd1e7c13477ef2b09a9dcc1c6 |      | 2021-04-16 12:45:59 +0800 | 2021-04-26 15:37:04 +0800 |
| 68 | Hello    | M      |       | fb6fea4b23b1d3c54739774946246e4c |      | 2021-04-16 12:46:11 +0800 | 2021-04-16 12:49:44 +0800 |
| 69 | AAA      | M      |       | f99a90d691067307eb2249f7dbcd7aa6 |      | 2021-04-16 12:46:11 +0800 | 2021-04-26 15:38:42 +0800 |
| 70 | BBB      | M      |       | 3f0846b8d6693717e93160949e6c810a |      | 2021-04-16 12:47:32 +0800 | 2021-04-26 15:38:42 +0800 |
| 71 | CCC      | M      |       | 403a531a295cce6c69ec42af819c1b84 |      | 2021-04-16 12:47:46 +0800 | 2021-04-26 15:38:53 +0800 |
| 72 | DDD      | M      |       | 43f7186f292e265ad5bc0f09168b8679 |      | 2021-04-16 12:48:55 +0800 | 2021-04-26 15:38:53 +0800 |
| 73 | Zhangsan | M      |       | 6165a37114488b62edaedd94231e6e95 |      | 2021-04-16 22:30:44 +0800 | 2021-04-16 22:30:44 +0800 |
| 74 | Sa       | F      |       | 677b950987bacd1b08f42645341bb28a |      | 2021-04-16 22:30:44 +0800 | 2021-04-16 22:30:44 +0800 |
| 75 | Zhangsan | M      |       | 6165a37114488b62edaedd94231e6e95 |      | 2021-04-16 22:30:57 +0800 | 2021-04-16 22:30:57 +0800 |
| 76 | Sa       | F      |       | 677b950987bacd1b08f42645341bb28a |      | 2021-04-16 22:30:57 +0800 | 2021-04-16 22:30:57 +0800 |
| 77 | Jack     | M      |       | 6165a37114488b62edaedd94231e6e95 |      | 2021-04-26 15:35:14 +0800 | 2021-04-26 15:35:14 +0800 |
| 78 | Jack     | M      |       | 6165a37114488b62edaedd94231e6e95 |      | 2021-04-26 15:35:29 +0800 | 2021-04-26 15:35:29 +0800 |
| 79 | Jack     | M      |       | 6165a37114488b62edaedd94231e6e95 |      | 2021-04-26 15:35:29 +0800 | 2021-04-26 15:35:29 +0800 |
| 80 | Jack     | M      |       | 6165a37114488b62edaedd94231e6e95 |      | 2021-04-26 15:36:36 +0800 | 2021-04-26 15:36:36 +0800 |
| 81 | Jack     | M      |       | 6165a37114488b62edaedd94231e6e95 |      | 2021-04-26 15:36:52 +0800 | 2021-04-26 15:36:52 +0800 |
+----+----------+--------+-------+----------------------------------+------+---------------------------+---------------------------+
=> nil
[email protected](main) [7] ❯ P.where(name: 'Hello').t    # Print posts which name is `Hello' as a table
D, [2022-10-17T16:17:16.437988 #79805] DEBUG -- :   Post Load (9.3ms)  SELECT `post`.* FROM `post` WHERE `post`.`name` = 'Hello'
+----+-------+--------+-------+----------------------------------+------+---------------------------+---------------------------+
| id | name  | gender | phone | id_no                            | note | gmt_created               | gmt_modified              |
+----+-------+--------+-------+----------------------------------+------+---------------------------+---------------------------+
| 68 | Hello | M      |       | fb6fea4b23b1d3c54739774946246e4c |      | 2021-04-16 12:46:11 +0800 | 2021-04-16 12:49:44 +0800 |
+----+-------+--------+-------+----------------------------------+------+---------------------------+---------------------------+
=> nil
[email protected](main) [8] ❯ P.indexes  # Print indexes of the post table (there is no index)
++
++
=> nil
[email protected](main) [9] ❯ P.add_index :name  # Add an index on the name column
D, [2022-10-17T16:17:28.635759 #79805] DEBUG -- :    (138.3ms)  CREATE INDEX `index_post_on_name` ON `post` (`name`)
=> nil
[email protected](main) [10] ❯ P.indexes   # Print indices of post
+-------+--------------------+---------+--------+---------+
| Table | Name               | Columns | Unique | Comment |
+-------+--------------------+---------+--------+---------+
| post  | index_post_on_name | name    | false  |         |
+-------+--------------------+---------+--------+---------+
=> nil
[email protected](main) [11] ❯ 

比较长的表格也可以结合 visidata 查看

数据库在 Emacs 项目中都有哪些应用场景呢?分享 2 个我了解的

epkgs

Emacs 有近一万个包,并且包的有依赖关系,所以关系型数据库排得上用场。

elfeed

缓存用户 Feed 的数据,数据库也比单个缓存文件(JSON、Lisp 等格式)更高效、可靠、容易维护。