Skip to content

Commit c8ec91f

Browse files
joe-reh-michael
andauthored
Enable to set personal config file and multiple connection (#24)
* write specification on README.md * implement personal config file * implement switchDatabaseConnection via workspace/executeCommand * Update README.md Co-authored-by: Hirokazu Hata <[email protected]> * add * at top of connection name which is connected * fix typo * write README how to swtich database connection Co-authored-by: Hirokazu Hata <[email protected]>
1 parent 3d76855 commit c8ec91f

File tree

11 files changed

+414
-76
lines changed

11 files changed

+414
-76
lines changed

README.md

Lines changed: 136 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -52,42 +52,67 @@ $ sql-language-server up --method stdio
5252

5353
### Connect to database
5454

55-
#### Create .sqllsrc.json on your project root
55+
#### Using Configuration Files
56+
57+
There are two ways to use configuration files.
58+
59+
- Set personal configuration file(~/.config/sql-language-server/sqllsrc.json)
60+
- Set project configuration file on your project root(\${YOUR_PROJECT/.sqllsrc.json})
61+
62+
#### Example for personal configuration file
5663

5764
- Examples
5865

5966
```json
6067
{
61-
"adapter": "mysql",
62-
"host": "localhost",
63-
"port": 3307,
64-
"user": "username",
65-
"password": "password",
66-
"database": "mysql-development",
67-
"ssh": {
68-
"user": "ubuntu",
69-
"remoteHost": "ec2-xxx-xxx-xxx-xxx.ap-southeast-1.compute.amazonaws.com",
70-
"dbHost": "127.0.0.1",
71-
"port": 3306,
72-
"identityFile": "~/.ssh/id_rsa",
73-
"passphrase": "123456"
74-
}
68+
"connections": [
69+
{
70+
"name": "sql-language-server",
71+
"adapter": "mysql",
72+
"host": "localhost",
73+
"port": 3307,
74+
"user": "username",
75+
"password": "password",
76+
"database": "mysql-development",
77+
"projectPaths": ["/Users/joe-re/src/sql-language-server"],
78+
"ssh": {
79+
"user": "ubuntu",
80+
"remoteHost": "ec2-xxx-xxx-xxx-xxx.ap-southeast-1.compute.amazonaws.com",
81+
"dbHost": "127.0.0.1",
82+
"port": 3306,
83+
"identityFile": "~/.ssh/id_rsa",
84+
"passphrase": "123456"
85+
}
86+
},
87+
{
88+
"name": "postgres-project",
89+
"adapter": "prostgres",
90+
"host": "localhost",
91+
"port": 5432,
92+
"user": "postgres",
93+
"password": "pg_pass",
94+
"database": "pg_test",
95+
"projectPaths": ["/Users/joe-re/src/postgres_ptoject"]
96+
}
97+
]
7598
}
7699
```
77100

78101
Please restart sql-language-server process after create .sqlrc.json.
79102

80103
#### Parameters
81104

82-
| Key | Description | value | required | default |
83-
| -------- | --------------------------- | ----------------------- | -------- | --------------------------------- |
84-
| adapter | Database type | `"mysql" | "postgres"` | true | |
85-
| host | Database host | string | true | |
86-
| port | Database port | string | false | mysql:3306, postgres:5432 |
87-
| user | Database user | string | true | mysql:"root", postgres:"postgres" |
88-
| password | Database password | string | false | |
89-
| database | Database name | string | false | |
90-
| ssh | Settings for port fowarding | \*see below SSH section | false | |
105+
| Key | Description | value | required | default |
106+
| ------------ | ------------------------------------------------------------------------------------------------------------------------- | ----------------------- | -------- | --------------------------------- |
107+
| name | Connection name(free-form text) | | true | |
108+
| adapter | Database type | "mysql" #124; "postgres" | true | |
109+
| host | Database host | string | true | |
110+
| port | Database port | string | false | mysql:3306, postgres:5432 |
111+
| user | Database user | string | true | mysql:"root", postgres:"postgres" |
112+
| password | Database password | string | false | |
113+
| database | Database name | string | false | |
114+
| projectPaths | Project path that you want to apply(if you don't set it configuration will not apply automatically when lsp's started up) | string[] | false | [] |
115+
| ssh | Settings for port fowarding | \*see below SSH section | false | |
91116

92117
##### SSH
93118

@@ -101,10 +126,79 @@ Please restart sql-language-server process after create .sqlrc.json.
101126
| identitiFile | Identity file for ssh | string | false | ~/.ssh/config/id_rsa |
102127
| passphrase | Passphrase to allow to use identity file | string | false | |
103128

129+
#### Personal confuguration file
130+
131+
Personal configuration file is located on `~/.config/sql-language-server/.sqllsrc.json`.
132+
sql-language-server will try to read when it's started.
133+
134+
#### Project confuguration file
135+
136+
Project configuration file is located on `${YOUR_PROJECT_ROOT}/.sqllsrc.json`.
137+
138+
All setting items are similarly to personal configuration file, with some exceptions:
139+
140+
- Specify under `connection` property element directly(you don't need to set array)
141+
- You don't need to set project path.(if you set it it will be ignored)
142+
- It's merged to personal configuration if you have it.
143+
144+
Example:
145+
```json
146+
{
147+
"name": "postgres-project",
148+
"adapter": "prostgres",
149+
"host": "localhost",
150+
"port": 5432,
151+
"user": "postgres",
152+
"database": "pg_test"
153+
}
154+
```
155+
156+
And also if you have set personal configuration and both of them's names are matched, it's merged automatically.
157+
158+
Personal configuration example:
159+
```json
160+
{
161+
connections: [{
162+
"name": "postgres-project",
163+
"password": "password",
164+
"ssh": {
165+
"user": "ubuntu",
166+
"remoteHost": "ec2-xxx-xxx-xxx-xxx.ap-southeast-1.compute.amazonaws.com",
167+
"dbHost": "127.0.0.1",
168+
"port": 5432,
169+
"identityFile": "~/.ssh/id_rsa",
170+
"passphrase": "123456"
171+
}
172+
}]
173+
}
174+
```
175+
176+
It will merge them as following:
177+
178+
```json
179+
{
180+
"name": "postgres-project",
181+
"adapter": "prostgres",
182+
"host": "localhost",
183+
"port": 5432,
184+
"user": "postgres",
185+
"database": "pg_test",
186+
"password": "password",
187+
"ssh": {
188+
"user": "ubuntu",
189+
"remoteHost": "ec2-xxx-xxx-xxx-xxx.ap-southeast-1.compute.amazonaws.com",
190+
"dbHost": "127.0.0.1",
191+
"port": 5432,
192+
"identityFile": "~/.ssh/id_rsa",
193+
"passphrase": "123456"
194+
}
195+
}
196+
```
197+
104198
#### Inject envitonment variables
105199

106-
${ssm:VARIABLE_NAME} syntax allows you to replace configuration value with environt variable.
107-
This is useful when you don't write actual file on configuration file.
200+
${env:VARIABLE_NAME} syntax allows you to replace configuration value with enviroment variable.
201+
This is useful when you don't write actual value on configuration file.
108202

109203
##### example
110204

@@ -127,6 +221,22 @@ This is useful when you don't write actual file on configuration file.
127221
}
128222
```
129223

224+
#### Switch database connection
225+
226+
If you have multiple connection information on personal config file, you can swtich database connection.
227+
228+
![2020-05-25_15-23-01](https://user-images.githubusercontent.com/4954534/82788937-02f63c80-9e9c-11ea-948d-e27ee0090463.gif)
229+
230+
231+
[VSC extension](https://marketplace.visualstudio.com/items?itemName=joe-re.sql-language-server) provides `Switch database connection` command.
232+
233+
Raw RPC param is:
234+
```
235+
method: workspace/executeCommand
236+
command: switchDataBaseConnection
237+
arguments: string(project name)
238+
```
239+
130240
### TODO
131241

132242
- [x] SELECT

package.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,17 @@
2727
"vscode": "^1.45.1"
2828
},
2929
"activationEvents": [
30-
"onLanguage:sql"
30+
"onLanguage:sql",
31+
"extension.switchDatabaseConnection"
3132
],
33+
"contributes": {
34+
"commands": [
35+
{
36+
"command": "extension.switchDatabaseConnection",
37+
"title": "SQLLanguageServer: Switch database connection"
38+
}
39+
]
40+
},
3241
"private": true,
3342
"workspaces": [
3443
"packages/*"

packages/client/extension.ts

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
import * as path from 'path'
2-
import { workspace, ExtensionContext } from 'vscode'
3-
import { LanguageClient, LanguageClientOptions, ServerOptions, TransportKind } from 'vscode-languageclient'
2+
import { workspace, ExtensionContext, commands, window as Window } from 'vscode'
3+
import {
4+
LanguageClient,
5+
LanguageClientOptions,
6+
ServerOptions,
7+
TransportKind,
8+
} from 'vscode-languageclient'
9+
import { ExecuteCommandParams } from 'vscode-languageserver-protocol'
410

511
export function activate(context: ExtensionContext) {
612
let serverModule = context.asAbsolutePath(path.join('packages', 'server', 'dist', 'cli.js'))
713
let execArgs = ['up', '--method', 'node-ipc']
814
let debugOptions = { execArgv: ["--nolazy", "--inspect=6009"] };
15+
let connectionNames = []
16+
let connectedConnectionName = ''
917

1018
let serverOptions: ServerOptions = {
1119
run : { module: serverModule, transport: TransportKind.ipc, args: execArgs },
@@ -14,13 +22,50 @@ export function activate(context: ExtensionContext) {
1422

1523
let clientOptions: LanguageClientOptions = {
1624
documentSelector: [{scheme: 'file', language: 'sql', pattern: '**/*.sql'}],
25+
diagnosticCollectionName: 'sqlLanguageServer',
1726
synchronize: {
1827
configurationSection: 'sqlLanguageServer',
1928
fileEvents: workspace.createFileSystemWatcher('**/.sqllsrc.json')
2029
}
2130
}
2231

23-
let disposable = new LanguageClient('sqlLanguageServer', 'SQL Language Server', serverOptions, clientOptions).start()
32+
let client = new LanguageClient('sqlLanguageServer', 'SQL Language Server', serverOptions, clientOptions)
33+
client.registerProposedFeatures()
34+
const disposable = client.start()
2435

36+
const switchConnection = commands.registerCommand('extension.switchDatabaseConnection', async () => {
37+
if (connectionNames.length === 0) {
38+
Window.showWarningMessage("Need to set personal config file at first.")
39+
return
40+
}
41+
const items = connectionNames.map(v => {
42+
if (connectedConnectionName === v) {
43+
return { label: `* ${v}`, value: v }
44+
}
45+
return { label: ` ${v}`, value: v}
46+
})
47+
const selected = await Window.showQuickPick(items)
48+
if (!selected) {
49+
return
50+
}
51+
const params: ExecuteCommandParams = {
52+
command: 'switchDatabaseConnection',
53+
arguments: [selected.value]
54+
}
55+
client.sendRequest('workspace/executeCommand', params)
56+
})
57+
context.subscriptions.push(switchConnection)
2558
context.subscriptions.push(disposable)
59+
client.onReady().then(() => {
60+
client.onNotification('sqlLanguageServer.finishSetup', (params) => {
61+
connectionNames =
62+
params.personalConfig?.connections?.
63+
map((v: { name: string}) => v.name).
64+
filter((v: string) => !!v)
65+
connectedConnectionName = params.config?.name || ''
66+
})
67+
client.onNotification('sqlLanguageServer.error', (params) => {
68+
Window.showErrorMessage(params.message)
69+
})
70+
})
2671
}

0 commit comments

Comments
 (0)