feat(editor): dialect-aware SQL quick actions with per-database templates

This commit is contained in:
Mo-Moore authored and GitHub committed 2026-09-17 17:29:01 +08:00
1 parent 455117b8c5
commit 65b820e6a9
30 files changed
+1541 -150

No files matched your search

+1 -1
View File
@@ -76,6 +76,6 @@ Windows/Linux 上 `Ctrl+Alt` 可能被键盘布局当作 AltGr;DBX 对相关
<Cards>
<Card title="SQL 编辑器" href="/cn/docs/query-editor">了解执行范围、多结果标签页和编辑器行为。</Card>
<Card title="SQL 快捷操作" href="/cn/docs/sql-quick-actions">选中表名后执行自定义 SQL 模板。</Card>
<Card title="SQL 快捷操作" href="/cn/docs/sql-quick-actions">选中表名或将光标放在表名上后,执行内置或自定义 SQL。</Card>
<Card title="SQL 格式化" href="/cn/docs/sql-formatter">配置格式化规则和格式化设置编辑器快捷键。</Card>
</Cards>
+1 -1
View File
@@ -76,6 +76,6 @@ On Windows/Linux, `Ctrl+Alt` may represent AltGr, so DBX matches those combinati
<Cards>
<Card title="Query Editor" href="/en/docs/query-editor">Review execution ranges, result tabs, and editor behavior.</Card>
<Card title="SQL Quick Actions" href="/en/docs/sql-quick-actions">Run custom SQL templates after selecting a table name.</Card>
<Card title="SQL Quick Actions" href="/en/docs/sql-quick-actions">Run built-in or custom SQL after selecting a table name, or with the caret on a table identifier.</Card>
<Card title="SQL Formatter" href="/en/docs/sql-formatter">Configure formatting rules and shortcuts for the formatter settings editor.</Card>
</Cards>
+48 -10
View File
@@ -1,34 +1,72 @@
---
title: SQL 快捷操作
description: 在查询编辑器中选中表名后,通过快捷键执行自定义 SQL 模板。
description: 在查询编辑器中选中表名后,通过快捷键执行内置模板或自定义 SQL。
---
SQL 快捷操作允许你在查询编辑器中先选中表名(或限定标识符),再按快捷键执行可复用的 SQL 模板。
SQL 快捷操作允许你在查询编辑器中先选中表名(或限定标识符),再按快捷键执行可复用的 SQL。
这与 [SQL 代码片段](/cn/docs/sql-snippets) 不同:代码片段通过补全把模板插入到光标处;快捷操作会根据当前选区生成 SQL,并走正常的执行流程。
## 管理快捷操作
打开 **设置 → 快捷键**,找到 **SQL 快捷操作** 区域。每条操作包含:
打开 **设置 → 快捷键**,找到 **SQL 快捷操作** 区域。
### 内置模板
DBX 默认提供两条内置操作(列表中标记为 **内置模板**):
| 操作 | 默认快捷键 | 可编辑 |
| --- | --- | --- |
| **查前 N 条** | `Mod+Shift+1` | 快捷键与行数 |
| **统计行数** | `Mod+Shift+2` | 仅快捷键 |
内置显示名随界面语言翻译,不可改文案;始终适用于全部数据库,不可删除(可关闭启用开关)。列表为空时会恢复默认;已有列表会按 id 补齐缺失的内置项(不覆盖你改过的同 id 配置)。
### 自定义操作
点击 **添加** 只能新建自定义 SQL 模板。每条自定义操作包含:
| 字段 | 作用 |
| --- | --- |
| 显示名 | 设置列表中显示的名称。 |
| 快捷键 | 触发该操作的按键组合。 |
| 适用数据库 | 可留空表示全部库;或限定为若干数据库类型。 |
| SQL 模板 | 自定义 `${table}` SQL。限定具体库后,可为每种库分别编辑正文。 |
| 状态 | 临时启用或停用,不需要删除模板。 |
| SQL 模板 | 执行前会把 `${table}` 替换为选中内容的 SQL。 |
你可以新增、编辑、启用、停用或删除操作。在编辑对话框中保存后会立即生效;也可以在设置窗口点击 **应用** 统一保存。
**添加** 流程不再提供「查前 N 条」操作类型——该能力由内置模板负责。在编辑对话框中保存后会立即生效;也可以在设置窗口点击 **应用** 统一保存。
SQL 快捷操作**仅在 SQL 编辑器中生效**(MySQL、PostgreSQL、Hive、ClickHouse 等)。Redis、MongoDB、Elasticsearch 等非 SQL 页不会触发。
<Callout type="info">
建议使用包含 `Mod`(macOS 为 `Cmd`,Windows/Linux 为 `Ctrl`)的组合键,比单独使用 `Shift+字母` 更少干扰正常输入。
</Callout>
## 查前 N 条
内置 **查前 N 条** 会按当前连接的方言生成语句:
- MySQL / PostgreSQL / 未知 JDBC:`LIMIT N`
- SQL Server:`TOP N`
- Oracle / 达梦:`ROWNUM`
- DB2:`FETCH FIRST N ROWS ONLY`
只需配置行数和快捷键。未知 JDBC 连接同样使用 `LIMIT`,避免执行无行数限制的 `SELECT *`。
## 按库分别编辑 SQL
自定义操作限定为若干数据库类型后,SQL 模板旁会出现库类型下拉框:
- **切换库**会自动把当前文本框暂存为该库的覆盖(`sqlByDatabaseType`),再载入目标库(有覆盖用覆盖,否则用默认 `sql`)。
- 对话框 **保存** 只持久化默认 `sql` 与已编辑过的库覆盖,不会把当前正文写到所有已选库。
- **应用到全部选中库** 会把当前正文复制到每个已选库,并同步默认 `sql`。
- **取消勾选**某个库会删除其覆盖(再勾选不会复活旧正文)。切回 **全部数据库** 会清空全部覆盖,并保留当前所见正文作为唯一模板。
## 执行快捷操作
1. 在 SQL 编辑器中选中表名或限定标识符,例如 `orders` 或 `public.users`。
1. 在 SQL 编辑器中选中表名或限定标识符,例如 `orders` 或 `public.users`。也可以不选中,只把光标放在标识符上(会带上引号与 `schema.table`)。
2. 按下已配置的快捷键。
3. DBX 会把模板中所有 `${table}` 替换为去除首尾空白后的选区文本,并执行展开后的 SQL。
3. 对 SQL 模板,DBX 会把 `${table}` 替换为选区文本(若有按库正文则优先使用);对「查前 N 条」,会按当前库类型生成 SQL 后执行。
示例模板:
@@ -42,16 +80,16 @@ SELECT COUNT(*) FROM ${table}
SELECT COUNT(*) FROM orders
```
如果没有选中任何内容,快捷键不会触发,编辑器会保持正常输入行为。
有选区时用选区作为 `${table}`;无选区时用光标处的限定标识符(含引号与 `schema.table`)。两者都没有则不触发。若当前连接不在该操作的适用数据库范围内,该快捷键在本标签页中也不会执行。
## 快捷键冲突
DBX 会检查快捷操作是否与以下快捷键冲突:
- 列表中的其他快捷操作
- 适用数据库范围有交集的其他快捷操作(「全部数据库」与任意范围都视为有交集)
- **设置 → 快捷键** 中的内置编辑器和全局快捷键
存在冲突时,设置界面会标红,并且在解决冲突前无法 **应用** 设置。
同库快捷键不可重复;跨库且范围不重叠时可以共用同一快捷键(例如仅 MySQL 与仅 SQL Server)。存在冲突时,设置界面会标红,并且在解决冲突前无法 **应用** 设置。
## 保存、同步与安全
+48 -10
View File
@@ -1,34 +1,72 @@
---
title: SQL Quick Actions
description: Run custom SQL templates from the editor after selecting a table name and pressing a shortcut.
description: Run built-in or custom SQL templates from the editor after selecting a table name.
---
SQL Quick Actions let you run reusable SQL templates against the text you have selected in the query editor. Select a table identifier first, then press the shortcut you configured in **Settings → Shortcuts**.
SQL Quick Actions let you run reusable SQL against the text you have selected in the query editor. Select a table identifier first, then press the shortcut you configured in **Settings → Shortcuts**.
This is different from [SQL Snippets](/en/docs/sql-snippets), which insert templates at the cursor through completion. Quick Actions generate SQL from the current selection and send it through the normal execution path.
## Manage Quick Actions
Open **Settings → Shortcuts** and scroll to **SQL Quick Actions**. Each action contains:
Open **Settings → Shortcuts** and scroll to **SQL Quick Actions**.
### Built-in templates
DBX ships two built-in actions (shown as **Built-in** in the list):
| Action | Default shortcut | Editable |
| --- | --- | --- |
| **Select first N rows** | `Mod+Shift+1` | Shortcut and row count |
| **Count rows** | `Mod+Shift+2` | Shortcut only |
Built-in labels are localized and not user-editable. They always apply to all databases and cannot be deleted (disable them instead). An empty list restores these defaults; existing lists append any missing built-in ids without overwriting your edits.
### Custom actions
Click **Add** to create a custom template. Each custom action includes:
| Field | Purpose |
| --- | --- |
| Label | Name shown in the settings list. |
| Shortcut | Key combination that runs the action. |
| Databases | Optional scope. Empty means all databases; otherwise the action only runs for selected types. |
| SQL Template | Custom SQL with `${table}`. When scoped to specific databases, you can edit a different SQL body per type. |
| Status | Enables or disables the action without deleting it. |
| SQL Template | SQL executed after `${table}` is replaced with the selected text. |
You can add, edit, enable, disable, or delete actions. Changes are saved immediately from the action dialog, or when you click **Apply** in the settings window.
You cannot create another “first N rows” action type from **Add**—that remains a built-in responsibility. Changes are saved immediately from the action dialog, or when you click **Apply** in the settings window.
Quick Actions run **only in the SQL editor** (MySQL, PostgreSQL, Hive, ClickHouse, and other SQL dialects). They do not fire in Redis, MongoDB, Elasticsearch, or other non-SQL editors.
<Callout type="info">
Shortcuts that include `Mod` (`Cmd` on macOS, `Ctrl` on Windows/Linux) are recommended. They are less likely to interfere with normal typing than bare letter keys such as `Shift+U`.
</Callout>
## Select first N rows
The built-in **Select first N rows** action adapts syntax for the active connection:
- MySQL / PostgreSQL / unknown JDBC: `LIMIT N`
- SQL Server: `TOP N`
- Oracle / Dameng: `ROWNUM`
- DB2: `FETCH FIRST N ROWS ONLY`
You only configure the row count and shortcut once. Unknown JDBC connections use `LIMIT` so the action never runs an unbounded `SELECT *`.
## Custom SQL per database
When a custom action is limited to specific database types, the SQL editor shows a dialect dropdown next to the template:
- Switching databases **auto-saves** the current textarea into that type’s override (`sqlByDatabaseType`), then loads the next type (override if present, otherwise the default `sql`).
- Dialog **Save** persists the default `sql` plus overrides only for databases you actually edited—it does not copy the current text onto every selected type.
- **Apply to all selected** copies the current textarea to every selected type and updates the default `sql`.
- Unchecking a database deletes its override (re-checking does not restore it). Switching back to **All databases** clears all overrides and keeps the currently visible text as the single template.
## Run a Quick Action
1. Select a table name or qualified identifier in the SQL editor, for example `orders` or `public.users`.
1. Select a table name or qualified identifier in the SQL editor, for example `orders` or `public.users`. You can also place the caret on the identifier without selecting it (quoted names and `schema.table` are included).
2. Press the configured shortcut.
3. DBX replaces every `${table}` token with the trimmed selection and executes the expanded SQL.
3. For templates, DBX replaces every `${table}` token with the trimmed selection (using the per-database body when present). For first-N actions, it generates dialect-aware SQL using the selection as the table expression, then executes it.
Example template:
@@ -42,16 +80,16 @@ With `orders` selected, DBX runs:
SELECT COUNT(*) FROM orders
```
If nothing is selected, the shortcut does not run and normal editor input continues.
If there is a selection, DBX uses that text as `${table}`. Otherwise it uses the qualified identifier under the caret (including quotes and `schema.table`). If neither is available, the shortcut does nothing. If the action is scoped to other database types than the current connection, the shortcut is ignored for that tab.
## Shortcut Conflicts
DBX checks Quick Action shortcuts against:
- Other Quick Actions in your list
- Other Quick Actions whose **database scopes overlap** (all-databases counts as overlapping everything)
- Built-in editor and global shortcuts shown in **Settings → Shortcuts**
Conflicting bindings are highlighted and block **Apply** until you resolve them.
The same shortcut may be reused for different database types when their scopes do not overlap (for example MySQL-only and SQL Server-only). Conflicting bindings are highlighted and block **Apply** until you resolve them.
## Storage, Sync, and Safety