@black-cyq/sql-cli 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +79 -1
- package/jar/sql-cli.jar +0 -0
- package/package.json +3 -1
- package/scripts/postinstall.js +31 -0
- package/skill/sql-cli-skill.md +403 -0
package/README.md
CHANGED
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
## 特性
|
|
16
16
|
|
|
17
17
|
- **多数据库支持** — MySQL、Oracle、PostgreSQL、SQL Server、SQLite、达梦等,可通过自定义驱动扩展
|
|
18
|
+
- **交互式 Shell** — REPL 模式支持连接复用、SQL 历史记录、快捷命令
|
|
18
19
|
- **安全保护** — 密码 AES-256-GCM 加密存储、安全级别控制(严格/正常/无)
|
|
19
20
|
- **数据导入导出** — CSV、JSON、SQL INSERT/UPDATE 格式
|
|
20
21
|
- **连接管理** — 保存连接配置,支持分组和标签
|
|
@@ -96,6 +97,52 @@ sql-cli meta describe -t users -c mydb
|
|
|
96
97
|
sql-cli export -t users -c mydb -f csv -o /tmp/users.csv
|
|
97
98
|
```
|
|
98
99
|
|
|
100
|
+
## 交互式 Shell(REPL)
|
|
101
|
+
|
|
102
|
+
对于频繁查询的场景,使用交互式 Shell 模式可复用连接,大幅提升响应速度:
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
# 进入交互式 Shell
|
|
106
|
+
sql-cli shell -c mydb
|
|
107
|
+
|
|
108
|
+
# 或使用内联参数直接连接
|
|
109
|
+
sql-cli shell --type mysql --host localhost --port 3306 \
|
|
110
|
+
--user root --password xxx --db testdb
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
在 Shell 中:
|
|
114
|
+
|
|
115
|
+
```
|
|
116
|
+
mydb> SELECT * FROM users LIMIT 5;
|
|
117
|
+
| id | name | email |
|
|
118
|
+
|----|-------|-------------------|
|
|
119
|
+
| 1 | Alice | alice@example.com |
|
|
120
|
+
...
|
|
121
|
+
|
|
122
|
+
mydb> \dt # 列出所有表
|
|
123
|
+
mydb> \d users # 查看表结构
|
|
124
|
+
mydb> \format json # 切换输出格式
|
|
125
|
+
mydb> \c otherdb # 切换到其他连接
|
|
126
|
+
mydb> exit # 退出 Shell
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
**快捷命令:**
|
|
130
|
+
|
|
131
|
+
| 命令 | 说明 |
|
|
132
|
+
|------|------|
|
|
133
|
+
| `\dt`, `\tables` | 列出所有表 |
|
|
134
|
+
| `\d <表名>` | 查看表结构 |
|
|
135
|
+
| `\db`, `\databases` | 列出所有数据库 |
|
|
136
|
+
| `\dv`, `\views` | 列出所有视图 |
|
|
137
|
+
| `\c <连接名>` | 切换到其他连接 |
|
|
138
|
+
| `\format [markdown|json|csv]` | 设置输出格式 |
|
|
139
|
+
| `\limit <n>` | 设置行数限制 |
|
|
140
|
+
| `\nolimit` | 取消行数限制 |
|
|
141
|
+
| `\timeout <n>` | 设置查询超时 |
|
|
142
|
+
| `\status` | 显示当前设置 |
|
|
143
|
+
| `\help` | 显示帮助 |
|
|
144
|
+
| `exit`, `quit` | 退出 Shell |
|
|
145
|
+
|
|
99
146
|
## 使用示例
|
|
100
147
|
|
|
101
148
|
```
|
|
@@ -112,10 +159,41 @@ $ sql-cli query "SELECT id, name, email FROM users LIMIT 5" -c mydb
|
|
|
112
159
|
|
|
113
160
|
更多用法详见 [完整文档](skill/sql-cli-skill.md)。
|
|
114
161
|
|
|
115
|
-
##
|
|
162
|
+
## AI 编程助手集成
|
|
163
|
+
|
|
164
|
+
sql-cli 提供了 AI Skill,可让 Claude Code、Cursor、GitHub Copilot 等 AI 编程助手直接使用 sql-cli 操作数据库。
|
|
165
|
+
|
|
166
|
+
### 安装 Skill
|
|
167
|
+
|
|
168
|
+
```bash
|
|
169
|
+
# 全局安装(推荐,自动适配已安装的 AI 工具)
|
|
170
|
+
npx skills add xianyuchen-king/sql-cli -g
|
|
171
|
+
|
|
172
|
+
# 自动确认,无需交互
|
|
173
|
+
npx skills add xianyuchen-king/sql-cli -g -y
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### 支持的 AI 工具
|
|
177
|
+
|
|
178
|
+
安装后自动适配以下 AI 编程助手:
|
|
179
|
+
|
|
180
|
+
| AI 工具 | 支持方式 |
|
|
181
|
+
|---------|---------|
|
|
182
|
+
| Claude Code | 自动 |
|
|
183
|
+
| Cursor | 自动 |
|
|
184
|
+
| GitHub Copilot | 自动 |
|
|
185
|
+
| Codex | 自动 |
|
|
186
|
+
| Gemini CLI | 自动 |
|
|
187
|
+
| Cline | 自动 |
|
|
188
|
+
| Windsurf / Amp / Trae 等 | 自动 |
|
|
189
|
+
|
|
190
|
+
完整列表请运行 `npx skills add xianyuchen-king/sql-cli -g` 查看。
|
|
191
|
+
|
|
192
|
+
## 新功能 (v1.0.4)
|
|
116
193
|
|
|
117
194
|
| 功能 | 命令 |
|
|
118
195
|
|------|------|
|
|
196
|
+
| 交互式 Shell | `sql-cli shell -c mydb` — REPL 模式,连接复用 |
|
|
119
197
|
| 查询计时 | `sql-cli query "SELECT ..." -c mydb` 自动显示耗时 |
|
|
120
198
|
| 查询超时 | `sql-cli query "SELECT ..." -c mydb --timeout 30` |
|
|
121
199
|
| 输出到文件 | `sql-cli query "SELECT ..." -c mydb -o result.csv` |
|
package/jar/sql-cli.jar
CHANGED
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@black-cyq/sql-cli",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "A CLI tool for connecting to relational databases (MySQL, Oracle, PostgreSQL, SQLite, etc.) via JDBC. Designed for AI coding agents like Claude Code.",
|
|
5
5
|
"bin": {
|
|
6
6
|
"sql-cli": "./bin/sql-cli.js"
|
|
@@ -8,6 +8,8 @@
|
|
|
8
8
|
"files": [
|
|
9
9
|
"bin/sql-cli.js",
|
|
10
10
|
"jar/",
|
|
11
|
+
"scripts/postinstall.js",
|
|
12
|
+
"skill/",
|
|
11
13
|
"README.md"
|
|
12
14
|
],
|
|
13
15
|
"scripts": {
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const { existsSync, mkdirSync } = require('fs');
|
|
5
|
+
const { join } = require('path');
|
|
6
|
+
const { homedir } = require('os');
|
|
7
|
+
|
|
8
|
+
const configDir = join(homedir(), '.sql-cli');
|
|
9
|
+
const driversDir = join(configDir, 'drivers');
|
|
10
|
+
|
|
11
|
+
// Create directories if not exist
|
|
12
|
+
if (!existsSync(configDir)) {
|
|
13
|
+
mkdirSync(configDir, { recursive: true });
|
|
14
|
+
console.log('[sql-cli] Created config directory: ' + configDir);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (!existsSync(driversDir)) {
|
|
18
|
+
mkdirSync(driversDir, { recursive: true });
|
|
19
|
+
console.log('[sql-cli] Created drivers directory: ' + driversDir);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Generate encryption key if not set
|
|
23
|
+
const envVar = 'SQL_CLI_SECRET';
|
|
24
|
+
if (!process.env[envVar]) {
|
|
25
|
+
const crypto = require('crypto');
|
|
26
|
+
const secret = crypto.randomBytes(32).toString('base64');
|
|
27
|
+
console.log('[sql-cli] Encryption key not set. Add to your shell profile:');
|
|
28
|
+
console.log(` export ${envVar}="${secret}"`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
console.log('[sql-cli] Setup complete. Run "sql-cli init" to create default config.');
|
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: sql-cli
|
|
3
|
+
description: Database CLI tool for connecting to relational databases (MySQL, Oracle, PostgreSQL, SQLite, SQL Server, etc.) via JDBC. Provides query execution, metadata browsing, data import/export, and connection management.
|
|
4
|
+
type: user
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# SQL Database Tool (sql-cli)
|
|
8
|
+
|
|
9
|
+
## Prerequisites
|
|
10
|
+
|
|
11
|
+
- **Java 21+** must be installed and on PATH. sql-cli checks this on startup.
|
|
12
|
+
- **Node.js 16+** required for npm installation (not needed at runtime).
|
|
13
|
+
|
|
14
|
+
## Installation & Setup
|
|
15
|
+
|
|
16
|
+
### Check Installation
|
|
17
|
+
Run `sql-cli --version` to check if sql-cli is installed.
|
|
18
|
+
|
|
19
|
+
### Install via npm (Recommended)
|
|
20
|
+
```bash
|
|
21
|
+
npm install -g @black-cyq/sql-cli
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
After installation, `~/.sql-cli/` directory is auto-created with `drivers/` subdirectory.
|
|
25
|
+
|
|
26
|
+
### Install from source (development)
|
|
27
|
+
1. Clone the repository and build:
|
|
28
|
+
```bash
|
|
29
|
+
git clone https://github.com/xianyuchen-king/sql-cli.git
|
|
30
|
+
cd sql-cli
|
|
31
|
+
./gradlew shadowJar
|
|
32
|
+
```
|
|
33
|
+
2. Run directly:
|
|
34
|
+
```bash
|
|
35
|
+
java -jar build/libs/sql-cli.jar --help
|
|
36
|
+
```
|
|
37
|
+
3. Run `sql-cli init` to initialize configuration
|
|
38
|
+
4. Set the encryption key environment variable (shown during init)
|
|
39
|
+
|
|
40
|
+
### Uninstall
|
|
41
|
+
```bash
|
|
42
|
+
npm uninstall -g @black-cyq/sql-cli
|
|
43
|
+
sql-cli uninstall --confirm # Removes ~/.sql-cli/ entirely
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Initialize Configuration
|
|
47
|
+
```bash
|
|
48
|
+
sql-cli init
|
|
49
|
+
```
|
|
50
|
+
This creates `~/.sql-cli/config.yml` and `~/.sql-cli/drivers/`.
|
|
51
|
+
|
|
52
|
+
### Install JDBC Drivers
|
|
53
|
+
```bash
|
|
54
|
+
# Install common database drivers (downloads from Maven Central)
|
|
55
|
+
sql-cli driver install mysql
|
|
56
|
+
sql-cli driver install oracle
|
|
57
|
+
sql-cli driver install postgresql
|
|
58
|
+
sql-cli driver install sqlite
|
|
59
|
+
sql-cli driver install mssql
|
|
60
|
+
|
|
61
|
+
# List installed drivers
|
|
62
|
+
sql-cli driver list
|
|
63
|
+
|
|
64
|
+
# List available drivers
|
|
65
|
+
sql-cli driver available
|
|
66
|
+
|
|
67
|
+
# Install specific version
|
|
68
|
+
sql-cli driver install mysql --version 5.1.49
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
For uncommon databases, manually place the JDBC driver jar in `~/.sql-cli/drivers/` and register the type:
|
|
72
|
+
```bash
|
|
73
|
+
sql-cli conn register-type \
|
|
74
|
+
--name dm \
|
|
75
|
+
--driver DmJdbcDriver18.jar \
|
|
76
|
+
--driver-class dm.jdbc.driver.DmDriver \
|
|
77
|
+
--url-template "jdbc:dm://{host}:{port}/{db}" \
|
|
78
|
+
--default-port 5236
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Update
|
|
82
|
+
```bash
|
|
83
|
+
sql-cli update check # Check for new version
|
|
84
|
+
sql-cli update # Update to latest version
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Usage
|
|
88
|
+
|
|
89
|
+
### 1. Check existing connections
|
|
90
|
+
```bash
|
|
91
|
+
sql-cli conn list
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### 2. Add a connection
|
|
95
|
+
```bash
|
|
96
|
+
# Non-interactive
|
|
97
|
+
sql-cli conn add --name mydb --type mysql --host localhost --port 3306 --user root --password xxx --db testdb --driver mysql-connector-j-8.3.0.jar
|
|
98
|
+
|
|
99
|
+
# SQL Server
|
|
100
|
+
sql-cli conn add --name mssqldb --type mssql --host localhost --port 1433 --user sa --password xxx --db mydb --driver mssql-jdbc-12.4.2.jre11.jar
|
|
101
|
+
|
|
102
|
+
# Direct JDBC URL (for uncommon databases)
|
|
103
|
+
sql-cli conn add --name mydb --type generic --url "jdbc:xxx://host:port/db" --user admin --password xxx --driver MyDriver.jar --driver-class com.example.Driver
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### 3. Test a connection
|
|
107
|
+
```bash
|
|
108
|
+
sql-cli conn test mydb # Basic test
|
|
109
|
+
sql-cli conn test mydb -v # Verbose (shows JDBC URL and details)
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### 4. Execute queries
|
|
113
|
+
```bash
|
|
114
|
+
# SELECT query (returns Markdown table by default, with timing)
|
|
115
|
+
sql-cli query "SELECT * FROM users LIMIT 10" -c mydb
|
|
116
|
+
|
|
117
|
+
# Change output format
|
|
118
|
+
sql-cli query "SELECT * FROM users" -c mydb -f json
|
|
119
|
+
sql-cli query "SELECT * FROM users" -c mydb -f csv
|
|
120
|
+
|
|
121
|
+
# Custom row limit
|
|
122
|
+
sql-cli query "SELECT * FROM users" -c mydb --limit 100
|
|
123
|
+
sql-cli query "SELECT * FROM users" -c mydb --no-limit
|
|
124
|
+
|
|
125
|
+
# Query timeout (seconds)
|
|
126
|
+
sql-cli query "SELECT * FROM large_table" -c mydb --timeout 30
|
|
127
|
+
|
|
128
|
+
# Save results to file
|
|
129
|
+
sql-cli query "SELECT * FROM users" -c mydb -o /tmp/result.csv
|
|
130
|
+
|
|
131
|
+
# Direct connection (without saved config)
|
|
132
|
+
sql-cli query "SELECT 1" --type mysql --host localhost --port 3306 --user root --password xxx --db testdb --driver mysql-connector-j-8.3.0.jar
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### 5. Interactive Shell (REPL)
|
|
136
|
+
|
|
137
|
+
For frequent queries, use the interactive shell to reuse connections and improve response speed:
|
|
138
|
+
|
|
139
|
+
```bash
|
|
140
|
+
# Start shell with saved connection
|
|
141
|
+
sql-cli shell -c mydb
|
|
142
|
+
|
|
143
|
+
# Start shell with direct connection parameters
|
|
144
|
+
sql-cli shell --type mysql --host localhost --port 3306 --user root --password xxx --db testdb
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
**Slash Commands:**
|
|
148
|
+
|
|
149
|
+
| Command | Description |
|
|
150
|
+
|---------|-------------|
|
|
151
|
+
| `\dt`, `\tables` | List all tables |
|
|
152
|
+
| `\d <table>` | Describe table structure |
|
|
153
|
+
| `\db`, `\databases` | List all databases |
|
|
154
|
+
| `\dv`, `\views` | List all views |
|
|
155
|
+
| `\c <name>` | Switch to another connection |
|
|
156
|
+
| `\format [markdown\|json\|csv]` | Set output format |
|
|
157
|
+
| `\limit <n>` | Set max rows |
|
|
158
|
+
| `\nolimit` | Disable row limit |
|
|
159
|
+
| `\timeout <n>` | Set query timeout (seconds) |
|
|
160
|
+
| `\status` | Show current settings |
|
|
161
|
+
| `\help` | Show help |
|
|
162
|
+
| `exit`, `quit` | Exit shell |
|
|
163
|
+
|
|
164
|
+
**Example Shell Session:**
|
|
165
|
+
|
|
166
|
+
```
|
|
167
|
+
$ sql-cli shell -c mydb
|
|
168
|
+
|
|
169
|
+
sql-cli shell v1.0.4
|
|
170
|
+
Connected to: mysql://localhost:3306/testdb
|
|
171
|
+
Type '\help' for available commands, 'exit' or Ctrl+D to exit.
|
|
172
|
+
|
|
173
|
+
mydb> SELECT * FROM users LIMIT 5;
|
|
174
|
+
| id | name | email |
|
|
175
|
+
|----|-------|-------------------|
|
|
176
|
+
| 1 | Alice | alice@example.com |
|
|
177
|
+
|
|
178
|
+
5 rows in set (0.02s)
|
|
179
|
+
|
|
180
|
+
mydb> \d users
|
|
181
|
+
Table: users
|
|
182
|
+
...table structure...
|
|
183
|
+
|
|
184
|
+
mydb> \c otherdb
|
|
185
|
+
Switched to connection: otherdb
|
|
186
|
+
Connected to: mysql://localhost:3306/otherdb
|
|
187
|
+
|
|
188
|
+
otherdb> exit
|
|
189
|
+
Connection closed. Bye!
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
**Features:**
|
|
193
|
+
- Connection reuse across queries (faster response)
|
|
194
|
+
- SQL history (up/down arrow keys)
|
|
195
|
+
- Multi-line SQL support (statements end with `;`)
|
|
196
|
+
- Auto row limit and query timeout
|
|
197
|
+
|
|
198
|
+
### 6. Execute DDL/DML
|
|
199
|
+
```bash
|
|
200
|
+
# Safe operations
|
|
201
|
+
sql-cli exec "CREATE TABLE test (id INT PRIMARY KEY, name VARCHAR(50))" -c mydb
|
|
202
|
+
|
|
203
|
+
# Dangerous operations require --confirm
|
|
204
|
+
sql-cli exec "DROP TABLE temp_data" -c mydb --confirm
|
|
205
|
+
sql-cli exec "TRUNCATE TABLE temp_data" -c mydb --confirm
|
|
206
|
+
|
|
207
|
+
# BLOCKED operations (will always fail):
|
|
208
|
+
# - DROP DATABASE / DROP SCHEMA
|
|
209
|
+
# - DELETE without WHERE
|
|
210
|
+
# - UPDATE without WHERE
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### 6. Browse metadata
|
|
214
|
+
```bash
|
|
215
|
+
# List databases/schemas
|
|
216
|
+
sql-cli meta dbs -c mydb
|
|
217
|
+
|
|
218
|
+
# List tables
|
|
219
|
+
sql-cli meta tables -c mydb
|
|
220
|
+
sql-cli meta tables -c mydb -d schema_name
|
|
221
|
+
|
|
222
|
+
# List columns
|
|
223
|
+
sql-cli meta columns -t users -c mydb
|
|
224
|
+
|
|
225
|
+
# List indexes
|
|
226
|
+
sql-cli meta indexes -t users -c mydb
|
|
227
|
+
|
|
228
|
+
# List views
|
|
229
|
+
sql-cli meta views -c mydb
|
|
230
|
+
|
|
231
|
+
# Comprehensive table description (columns + PK + indexes + foreign keys)
|
|
232
|
+
sql-cli meta describe -t users -c mydb
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
### 7. Export data
|
|
236
|
+
```bash
|
|
237
|
+
# Export table to CSV
|
|
238
|
+
sql-cli export -t users -c mydb -f csv -o /tmp/users.csv
|
|
239
|
+
|
|
240
|
+
# Export to JSON / INSERT / UPDATE
|
|
241
|
+
sql-cli export -t users -c mydb -f json -o /tmp/users.json
|
|
242
|
+
sql-cli export -t users -c mydb -f insert -o /tmp/users.sql
|
|
243
|
+
sql-cli export -t users -c mydb -f update --where-columns id -o /tmp/users_update.sql
|
|
244
|
+
|
|
245
|
+
# Export all tables
|
|
246
|
+
sql-cli export --all-tables -c mydb -o /tmp/backup/
|
|
247
|
+
|
|
248
|
+
# Export to stdout
|
|
249
|
+
sql-cli export -t users -c mydb -f csv
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
### 8. Import data
|
|
253
|
+
```bash
|
|
254
|
+
# Import CSV
|
|
255
|
+
sql-cli import -t users -c mydb -f /tmp/users.csv
|
|
256
|
+
|
|
257
|
+
# Import SQL file
|
|
258
|
+
sql-cli import -c mydb -f /tmp/backup.sql --format insert
|
|
259
|
+
|
|
260
|
+
# Import with options
|
|
261
|
+
sql-cli import -t users -c mydb -f /tmp/users.csv --batch-size 1000 --on-error skip
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
### 9. Manage connections
|
|
265
|
+
```bash
|
|
266
|
+
sql-cli conn list # List all connections
|
|
267
|
+
sql-cli conn list --group prod # Filter by group
|
|
268
|
+
sql-cli conn list --tag mysql # Filter by tag
|
|
269
|
+
sql-cli conn show mydb # Show connection details
|
|
270
|
+
sql-cli conn update mydb --port 3307 # Update connection
|
|
271
|
+
sql-cli conn remove mydb # Remove connection
|
|
272
|
+
sql-cli conn group list # List groups
|
|
273
|
+
sql-cli conn tag list # List tags
|
|
274
|
+
sql-cli conn types # List registered database types
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
## Real-World Example
|
|
278
|
+
|
|
279
|
+
Complete workflow for connecting to a MySQL database and querying data:
|
|
280
|
+
|
|
281
|
+
```bash
|
|
282
|
+
# 1. Install MySQL driver
|
|
283
|
+
sql-cli driver install mysql
|
|
284
|
+
|
|
285
|
+
# 2. Add connection
|
|
286
|
+
sql-cli conn add \
|
|
287
|
+
--name mydb \
|
|
288
|
+
--type mysql \
|
|
289
|
+
--host localhost \
|
|
290
|
+
--port 3306 \
|
|
291
|
+
--user root \
|
|
292
|
+
--password xxxxxx \
|
|
293
|
+
--db testdb \
|
|
294
|
+
--driver mysql-connector-j-8.3.0.jar
|
|
295
|
+
|
|
296
|
+
# 3. Test connection with verbose output
|
|
297
|
+
sql-cli conn test mydb -v
|
|
298
|
+
|
|
299
|
+
# 4. Query all databases
|
|
300
|
+
sql-cli query "SHOW DATABASES" -c mydb
|
|
301
|
+
|
|
302
|
+
# 5. Describe table structure
|
|
303
|
+
sql-cli meta describe -t users -c mydb
|
|
304
|
+
|
|
305
|
+
# 6. Query first 10 rows
|
|
306
|
+
sql-cli query "SELECT * FROM users LIMIT 10" -c mydb
|
|
307
|
+
|
|
308
|
+
# 7. Interactive shell for multiple queries (connection reuse)
|
|
309
|
+
sql-cli shell -c mydb
|
|
310
|
+
# Then inside shell:
|
|
311
|
+
# mydb> SELECT * FROM users WHERE name LIKE '%test%';
|
|
312
|
+
# mydb> \d users
|
|
313
|
+
# mydb> \c otherdb
|
|
314
|
+
# mydb> exit
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
**Interactive Shell Tips:**
|
|
318
|
+
- Use shell mode when running multiple queries in succession (much faster due to connection reuse)
|
|
319
|
+
- Use `\dt` to list tables, `\d <table>` to describe table structure
|
|
320
|
+
- Use `\c <name>` to switch between connections without exiting
|
|
321
|
+
- SQL history is saved to `~/.sql-cli/history` for convenience
|
|
322
|
+
- Multi-line SQL is supported (statements end with `;`)
|
|
323
|
+
|
|
324
|
+
## Troubleshooting
|
|
325
|
+
|
|
326
|
+
### Connection Issues
|
|
327
|
+
|
|
328
|
+
**"Unknown database" error**
|
|
329
|
+
```bash
|
|
330
|
+
# Solution 1: Test without database first
|
|
331
|
+
sql-cli conn update mydb --db ""
|
|
332
|
+
sql-cli conn test mydb
|
|
333
|
+
|
|
334
|
+
# Then list databases and find correct one
|
|
335
|
+
sql-cli query "SHOW DATABASES" -c mydb
|
|
336
|
+
|
|
337
|
+
# Solution 2: Update to correct database name
|
|
338
|
+
sql-cli conn update mydb --db correct_db_name
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
**"Communications link failure"**
|
|
342
|
+
```bash
|
|
343
|
+
# Check network connectivity
|
|
344
|
+
ping <host>
|
|
345
|
+
telnet <host> <port>
|
|
346
|
+
|
|
347
|
+
# Add connection parameters for MySQL
|
|
348
|
+
sql-cli conn add --name mydb ... --param useSSL=false --param allowPublicKeyRetrieval=true
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
**"Driver not found" error**
|
|
352
|
+
```bash
|
|
353
|
+
# Check installed drivers
|
|
354
|
+
sql-cli driver list
|
|
355
|
+
|
|
356
|
+
# Install missing driver
|
|
357
|
+
sql-cli driver install mysql
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
### Connection Test Shows "NOT reachable" but verbose shows nothing
|
|
361
|
+
```bash
|
|
362
|
+
# Always use -v for verbose error details
|
|
363
|
+
sql-cli conn test mydb -v
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
## Safety Rules
|
|
367
|
+
|
|
368
|
+
1. **Strict mode connections**: Only SELECT and metadata queries allowed
|
|
369
|
+
2. **Dangerous operations** (DROP TABLE, TRUNCATE, ALTER TABLE DROP): Require `--confirm` flag
|
|
370
|
+
3. **Blocked operations** (DROP DATABASE, DELETE/UPDATE without WHERE): Always rejected
|
|
371
|
+
4. **Auto row limit**: SELECT queries default to 500 rows max, use `--limit N` or `--no-limit` to override
|
|
372
|
+
5. **Query timeout**: Use `--timeout` to prevent runaway queries
|
|
373
|
+
6. **NEVER execute destructive SQL on production databases** without explicit user confirmation
|
|
374
|
+
|
|
375
|
+
## Supported Databases
|
|
376
|
+
|
|
377
|
+
| Database | Type ID | Default Port |
|
|
378
|
+
|-----------|----------------|-------------|
|
|
379
|
+
| MySQL | `mysql` | 3306 |
|
|
380
|
+
| Oracle | `oracle` | 1521 |
|
|
381
|
+
| PostgreSQL| `postgresql` | 5432 |
|
|
382
|
+
| SQL Server| `mssql` | 1433 |
|
|
383
|
+
| SQLite | `sqlite` | - |
|
|
384
|
+
| DM (达梦) | `dm` (custom) | 5236 |
|
|
385
|
+
| Others | `generic` | - |
|
|
386
|
+
|
|
387
|
+
## Connection Parameters Reference
|
|
388
|
+
|
|
389
|
+
| Parameter | Description |
|
|
390
|
+
|-----------|-------------|
|
|
391
|
+
| `-c, --connection` | Use saved connection by name |
|
|
392
|
+
| `--type` | Database type (mysql/oracle/postgresql/mssql/sqlite/generic) |
|
|
393
|
+
| `--host` | Database host |
|
|
394
|
+
| `--port` | Database port |
|
|
395
|
+
| `--user` | Username |
|
|
396
|
+
| `--password` | Password |
|
|
397
|
+
| `--db` | Database name |
|
|
398
|
+
| `--url` | Direct JDBC URL |
|
|
399
|
+
| `--driver` | Driver jar file name (in drivers/ directory) |
|
|
400
|
+
| `--driver-class` | JDBC Driver class name |
|
|
401
|
+
| `--timeout` | Query timeout in seconds |
|
|
402
|
+
| `-o, --output` | Save query results to file |
|
|
403
|
+
| `-f, --format` | Output format: markdown/json/csv |
|