Skip to content

Commit

Permalink
Support mysql8 (#43)
Browse files Browse the repository at this point in the history
* add mysql environment to dev

* use mysql2 instead of mysql to support Mysql8

sidorares/node-mysql2#1021
  • Loading branch information
joe-re authored Jul 19, 2020
1 parent 3e09145 commit d546d98
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 15 deletions.
13 changes: 12 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,16 @@ services:
POSTGRES_PASSWORD: sqlls
volumes:
- postgres:/var/lib/postgresql/data
mysql:
image: mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: mysql_db
MYSQL_USER: sqlls
MYSQL_PASSWORD: sqlls
volumes:
- mysql:/var/lib/mysql
volumes:
postgres:
postgres:
mysql:
1 change: 1 addition & 0 deletions dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ COPY ./package.json yarn.lock /opt/sql-language-server/
COPY ./packages/server/package.json /opt/sql-language-server/packages/server/
COPY ./packages/sql-parser/package.json /opt/sql-language-server/packages/sql-parser/
COPY ./packages/sqlint/package.json /opt/sql-language-server/packages/sqlint/
COPY ./example/monaco_editor/.sqllsrc.personal.json /root/.config/sql-language-server/.sqllsrc.json
WORKDIR /opt/sql-language-server
RUN yarn
22 changes: 22 additions & 0 deletions example/monaco_editor/.sqllsrc.personal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"connections": [
{
"name": "postgres_db",
"adapter": "postgres",
"host": "postgres",
"port": 5432,
"user": "sqlls",
"password": "sqlls",
"database": "postgres_db"
},
{
"name": "mysql_db",
"adapter": "mysql",
"host": "mysql",
"port": 3306,
"user": "sqlls",
"password": "sqlls",
"database": "mysql_db"
}
]
}
8 changes: 4 additions & 4 deletions example/monaco_editor/config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
"dialect": "postgres"
},
"mysql": {
"username": "root",
"password": null,
"database": "database_test",
"host": "127.0.0.1",
"username": "sqlls",
"password": "sqlls",
"database": "mysql_db",
"host": "mysql",
"dialect": "mysql"
}
}
5 changes: 4 additions & 1 deletion example/monaco_editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"copy": "cp src/index.html dist/index.html",
"build": "yarn tsc && webpack && yarn run copy",
"watch": "run-p copy webpack:watch",
"start": "ts-node-dev --transpile-only ./src/server/server.ts"
"start": "ts-node-dev --transpile-only ./src/server/server.ts",
"migrate:postgres": "NODE_ENV=postgres sequelize-cli db:migrate",
"migrate:mysql": "NODE_ENV=mysql sequelize-cli db:migrate"
},
"dependencies": {
"express": "^4.17.1",
Expand All @@ -26,6 +28,7 @@
"@types/ws": "^7.2.6",
"css-loader": "^3.6.0",
"file-loader": "^6.0.0",
"mysql2": "^2.1.0",
"npm-run-all": "^4.1.5",
"sequelize": "^6.3.3",
"sequelize-cli": "^6.2.0",
Expand Down
29 changes: 26 additions & 3 deletions example/monaco_editor/src/client/App.svelte
Original file line number Diff line number Diff line change
@@ -1,31 +1,54 @@
<script>
import { executeFixAllFixableProblemsCommand } from './client'
import {
executeSwitchDatabaseCommand,
executeFixAllFixableProblemsCommand,
getConnectionList,
getCurrecntConnection
} from './client'
const commands = [
{ id: 'fixAllFixableProblems', text: 'fixAllFixableProblems' },
// TODO
// { id: 'switchDatabaseConnection', text: 'switchDatabaseConnection' },
{ id: 'switchDatabaseConnection', text: 'switchDatabaseConnection' },
]
let command = commands[0]
function handleSubmitCommand() {
if (command.id === 'fixAllFixableProblems') {
executeFixAllFixableProblemsCommand()
} else if (command.id === 'switchDatabaseConnection') {
executeSwitchDatabaseCommand(connection)
}
}
let connectionList = []
let connection = ''
function handleChangeCommand() {
connectionList = getConnectionList()
connection = getCurrecntConnection()
}
</script>

<h1>Monaco Language Client SQLLanguageServer Sample</h1>

<form on:submit|preventDefault={handleSubmitCommand}>
<select bind:value={command}>
<select bind:value={command} on:change={handleChangeCommand}>
{#each commands as command}
<option value={command}>
{command.text}
</option>
{/each}
</select>
{#if command.id === 'switchDatabaseConnection'}
<select bind:value={connection}>
{#each connectionList as con}
<option value={con}>
{con}
</option>
{/each}
</select>
{/if}
<button type=submit>Submit</button>
</form>

Expand Down
28 changes: 28 additions & 0 deletions example/monaco_editor/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import ReconnectingWebSocket from "reconnecting-websocket";
import { URI } from 'vscode-uri'

let languageClient: MonacoLanguageClient;
let connectionNames: string[] = []
let connectedConnectionName = ''

export function initClient() {
monaco.languages.register({
id: "sql",
Expand Down Expand Up @@ -40,6 +43,15 @@ export function initClient() {
languageClient = createLanguageClient(connection);
const disposable = languageClient.start();
connection.onClose(() => disposable.dispose());
languageClient.onReady().then(() => {
languageClient.onNotification('sqlLanguageServer.finishSetup', (params) => {
connectionNames =
params.personalConfig?.connections?.
map((v: { name: string}) => v.name).
filter((v: string) => !!v)
connectedConnectionName = params.config?.name || ''
})
})
},
});

Expand Down Expand Up @@ -90,3 +102,19 @@ export function executeFixAllFixableProblemsCommand() {
}
languageClient.sendRequest('workspace/executeCommand', params)
}

export function executeSwitchDatabaseCommand(db: string) {
const params: ExecuteCommandParams = {
command: 'switchDatabaseConnection',
arguments: [db]
}
languageClient.sendRequest('workspace/executeCommand', params)
}

export function getConnectionList() {
return connectionNames
}

export function getCurrecntConnection() {
return connectedConnectionName
}
1 change: 1 addition & 0 deletions example/monaco_editor/src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function startServer() {
const app = express();
app.use(express.static(`${process.cwd()}/dist`));
server = app.listen(3000);
console.log('startServer')

const wss = new ws.Server({
noServer: true,
Expand Down
6 changes: 3 additions & 3 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
"@types/pg": "^7.4.10",
"jest": "^26.0.1",
"log4js": "^6.2.1",
"mysql": "^2.15.0",
"mysql2": "^2.1.0",
"node-ssh-forward": "^0.6.3",
"pg": "^7.4.3",
"sqlint": "^0.9.2",
"sqlite3": "^4.2.0",
"vscode-languageclient": "^6.1.3",
"vscode-languageserver": "^6.1.1",
"vscode-languageserver-textdocument": "^1.0.1",
"yargs": "^12.0.1",
"sqlint": "^0.9.2"
"yargs": "^12.0.1"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^11.1.0",
Expand Down
1 change: 1 addition & 0 deletions packages/server/src/ambient.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
declare module 'yargs'
declare module 'mysql2'
7 changes: 4 additions & 3 deletions packages/server/src/database_libs/MysqlClient.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as mysql from 'mysql'
import * as mysql from 'mysql2'
import * as mysqlType from 'mysql'
import { Settings } from '../SettingStore'
import AbstractClient, { RawField } from './AbstractClient'

export default class MysqlClient extends AbstractClient {
connection: mysql.Connection | null = null
connection: mysqlType.Connection | null = null

constructor(settings: Settings) {
super(settings)
Expand Down Expand Up @@ -47,7 +48,7 @@ export default class MysqlClient extends AbstractClient {
reject(new Error(err.message))
return
}
const tables = results.map((v: any) => v[`table_name`])
const tables = results.map((v: any) => v['table_name'] || v['TABLE_NAME'])
resolve(tables)
})
})
Expand Down

0 comments on commit d546d98

Please sign in to comment.