Skip to content

Commit 4baf5af

Browse files
authored
Support sqlite3 (#34)
1 parent c65da97 commit 4baf5af

File tree

16 files changed

+208
-26
lines changed

16 files changed

+208
-26
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ packages/server/dist/*
66
**/*/package-lock.json
77
**/*/yarn.lock
88
yarn.lock
9+
*.sqlite3

.vscodeignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
**/*/jest.config.js
1212
**/*/yarn-error.log
1313
README.md
14-
**/*/.npmignore
14+
**/*/.npmignore
15+
!./node_modules/sqlite3

README.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,13 @@ There are two ways to use configuration files.
9292
"user": "postgres",
9393
"password": "pg_pass",
9494
"database": "pg_test",
95-
"projectPaths": ["/Users/joe-re/src/postgres_ptoject"]
95+
"projectPaths": ["/Users/joe-re/src/postgres_project"]
96+
},
97+
{
98+
"name": "sqlite3-project",
99+
"adapter": "sqlite3",
100+
"filename": "/Users/noguchimasato/src/sql-language-server/packages/server/test.sqlite3",
101+
"projectPaths": ["/Users/joe-re/src/sqlite2_project"]
96102
}
97103
]
98104
}
@@ -105,12 +111,13 @@ Please restart sql-language-server process after create .sqlrc.json.
105111
| Key | Description | value | required | default |
106112
| ------------ | ------------------------------------------------------------------------------------------------------------------------- | ----------------------- | -------- | --------------------------------- |
107113
| name | Connection name(free-form text) | | true | |
108-
| adapter | Database type | "mysql" #124; "postgres" | true | |
109-
| host | Database host | string | true | |
114+
| adapter | Database type | "mysql" #124; "postgres" #124; "sqlite3" | true | |
115+
| host | Database host | string | false | |
110116
| port | Database port | string | false | mysql:3306, postgres:5432 |
111-
| user | Database user | string | true | mysql:"root", postgres:"postgres" |
117+
| user | Database user | string | false | mysql:"root", postgres:"postgres" |
112118
| password | Database password | string | false | |
113119
| database | Database name | string | false | |
120+
| filename | Database filename(only for sqlite3) | string | false | |
114121
| 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 | [] |
115122
| ssh | Settings for port fowarding | \*see below SSH section | false | |
116123

@@ -237,6 +244,17 @@ command: switchDataBaseConnection
237244
arguments: string(project name)
238245
```
239246

247+
248+
#### SQLite3 Notes
249+
250+
If you get error when you use sqlite3 connection, you may need to rebuild sqlite3 on your environment.
251+
252+
VSC extension provides the command to rebuild it.(Name: `Rebuild SQLite3 Client`)
253+
![image](https://user-images.githubusercontent.com/4954534/85928359-ef952180-b8de-11ea-8cb3-7a9a509cd6d7.png)
254+
255+
If you're using sql-language-server directly, after go to the directry of it and call `npm rebuild sqlite` to rebuild it.
256+
257+
240258
#### Lint
241259

242260
You can use lint rules that are provided [sqlint](https://github.com/joe-re/sql-language-server/blob/master/packages/sqlint/README.md).

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"watch": "run-p watch:client watch:server",
2222
"watch:client": "cd ./packages/client && yarn run watch",
2323
"watch:server": "cd ./packages/server && yarn run watch",
24-
"vscode:prepublish": "yarn run compile"
24+
"vscode:prepublish": "yarn run compile && rm -rf node_modules/lib/binding"
2525
},
2626
"engines": {
2727
"vscode": "^1.45.1"
@@ -40,6 +40,11 @@
4040
"command": "extension.fixAllFixableProblems",
4141
"title": "Fix all auto-fixable problems",
4242
"category": "SQLLanguageServer"
43+
},
44+
{
45+
"command": "extension.rebuildSqlite3",
46+
"title": "Rebuild SQLite3 Client",
47+
"category": "SQLLanguageServer"
4348
}
4449
]
4550
},

packages/client/extension.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
TransportKind,
88
} from 'vscode-languageclient'
99
import { ExecuteCommandParams } from 'vscode-languageserver-protocol'
10+
import { rebuild } from './rebuild'
1011

1112
export function activate(context: ExtensionContext) {
1213
let serverModule = context.asAbsolutePath(path.join('packages', 'server', 'dist', 'cli.js'))
@@ -67,8 +68,27 @@ export function activate(context: ExtensionContext) {
6768
client.sendRequest('workspace/executeCommand', params)
6869
})
6970

71+
let isRebuilding = false
72+
const rebuildSqlite3 = commands.registerCommand('extension.rebuildSqlite3', async () => {
73+
if (isRebuilding) {
74+
Window.showInformationMessage('Already started rebuild Sqlite3 process')
75+
return
76+
}
77+
isRebuilding = true
78+
try {
79+
Window.showInformationMessage('Start to rebuild Sqlite3.')
80+
await rebuild()
81+
Window.showInformationMessage('Done to rebuild Sqlite3.')
82+
} catch (e) {
83+
Window.showErrorMessage(e)
84+
} finally {
85+
isRebuilding = false
86+
}
87+
})
88+
7089
context.subscriptions.push(switchConnection)
7190
context.subscriptions.push(fixAllFixableProblem)
91+
context.subscriptions.push(rebuildSqlite3)
7292
context.subscriptions.push(disposable)
7393
client.onReady().then(() => {
7494
client.onNotification('sqlLanguageServer.finishSetup', (params) => {

packages/client/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
],
1818
"dependencies": {
1919
"@types/node": "12.12.6",
20+
"electron-rebuild": "^1.11.0",
2021
"typescript": "^3.9.2",
2122
"vscode-languageclient": "^6.1.3",
2223
"vscode-test": "^1.3.0"

packages/client/rebuild.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import electronRebuild from 'electron-rebuild'
2+
3+
const electronVersion = (process.versions as any).electron
4+
export function rebuild(): Promise<void> {
5+
return electronRebuild({ buildPath: `${__dirname}/../../../node_modules/sqlite3`, electronVersion, force: true, useCache: false })
6+
}

packages/server/SettingStore.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ export type SSHConfig = {
1515
}
1616
export type Settings = {
1717
name: string | null,
18-
adapter: 'mysql' | 'postgresql' | null,
19-
host: string | null,
20-
port: number | null,
21-
user: string | null,
22-
database: string | null,
23-
password: string | null,
24-
projectPaths: string[],
18+
adapter: 'mysql' | 'postgresql' | 'sqlite3' | null,
19+
host: string | null
20+
port: number | null
21+
user: string | null
22+
database: string | null
23+
password: string | null
24+
filename: string | null // for sqlite3
25+
projectPaths: string[]
2526
ssh: SSHConfig | null
2627
}
2728

@@ -55,6 +56,7 @@ export default class SettingStore extends EventEmitter {
5556
database: null,
5657
password: null,
5758
ssh: null,
59+
filename: null,
5860
projectPaths: []
5961
}
6062
private static instance: SettingStore;

packages/server/createServer.ts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import getDatabaseClient from './database_libs/getDatabaseClient'
1818
import initializeLogging from './initializeLogging'
1919
import { lint, LintResult } from 'sqlint'
2020
import log4js from 'log4js'
21+
import { RequireSqlite3Error } from './database_libs/Sqlite3Client'
2122

2223
export type ConnectionMethod = 'node-ipc' | 'stdio'
2324
type Args = {
@@ -41,18 +42,33 @@ export default function createServer() {
4142
connection.onInitialize((params): InitializeResult => {
4243
logger.debug(`onInitialize: ${params.rootPath}`)
4344
SettingStore.getInstance().on('change', async () => {
45+
logger.debug('onInitialize: receive change event from SettingStore')
4446
try {
45-
const client = getDatabaseClient(SettingStore.getInstance().getSetting())
46-
schema = await client.getSchema()
47-
logger.debug('get schema')
48-
logger.debug(JSON.stringify(schema))
49-
connection.sendNotification('sqlLanguageServer.finishSetup', {
50-
personalConfig: SettingStore.getInstance().getPersonalConfig(),
51-
config: SettingStore.getInstance().getSetting()
52-
})
53-
} catch (e) {
54-
logger.error(e)
55-
}
47+
setTimeout(() => {
48+
connection.sendNotification('sqlLanguageServer.finishSetup', {
49+
personalConfig: SettingStore.getInstance().getPersonalConfig(),
50+
config: SettingStore.getInstance().getSetting()
51+
})
52+
}, 1000) // TODO: Need to think about better way to sendNotification
53+
try {
54+
const client = getDatabaseClient(
55+
SettingStore.getInstance().getSetting()
56+
)
57+
schema = await client.getSchema()
58+
logger.debug("get schema")
59+
logger.debug(JSON.stringify(schema))
60+
} catch (e) {
61+
logger.error("failed to get schema info")
62+
if (e instanceof RequireSqlite3Error) {
63+
connection.sendNotification('sqlLanguageServer.error', {
64+
message: "Need to rebuild sqlite3 module."
65+
})
66+
}
67+
throw e
68+
}
69+
} catch (e) {
70+
logger.error(e)
71+
}
5672
})
5773
if (params.rootPath) {
5874
SettingStore.getInstance().setSettingFromFile(

packages/server/database_libs/AbstractClient.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default abstract class AbstractClient {
2727

2828
constructor(protected settings: Settings) {}
2929

30-
abstract connect(): void
30+
abstract connect(): boolean
3131
abstract disconnect(): void
3232
abstract getTables(): Promise<string[]>
3333
abstract getColumns(tableName: string): Promise<RawField[]>
@@ -57,7 +57,10 @@ export default abstract class AbstractClient {
5757
return []
5858
})
5959
}
60-
this.connect()
60+
if (!this.connect()) {
61+
logger.error('AbstractClinet.getSchema: failed to connect database')
62+
return []
63+
}
6164
try {
6265
const tables = await this.getTables()
6366
schema = await Promise.all(

0 commit comments

Comments
 (0)