Skip to content

Commit fdc3374

Browse files
Merge pull request #60 from Jackson-soft/pgx
change lib/pq to jackc/pgx
2 parents 4ac6fc8 + 76a4250 commit fdc3374

17 files changed

+579
-136
lines changed

README.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ sqls aims to provide advanced intelligence for you to edit sql in your own edito
1515
### Support RDBMS
1616

1717
- MySQL([Go-MySQL-Driver](https://github.com/go-sql-driver/mysql))
18-
- PostgreSQL([pq](https://github.com/lib/pq))
18+
- PostgreSQL([pgx](https://github.com/jackc/pgx))
1919
- SQLite3([go-sqlite3](https://github.com/mattn/go-sqlite3))
2020

2121
### Language Server Features
@@ -57,7 +57,7 @@ sqls aims to provide advanced intelligence for you to edit sql in your own edito
5757

5858
## Installation
5959

60-
```
60+
```shell
6161
go get github.com/lighttiger2505/sqls
6262
```
6363

@@ -120,7 +120,7 @@ connections:
120120
121121
### Workspace configuration Sample
122122
123-
* setting example with vim-lsp.
123+
- setting example with vim-lsp.
124124
125125
```vim
126126
if executable('sqls')
@@ -149,7 +149,7 @@ if executable('sqls')
149149
endif
150150
```
151151

152-
* setting example with coc.nvim.
152+
- setting example with coc.nvim.
153153

154154
In `coc-settings.json` opened by `:CocConfig`
155155

@@ -166,7 +166,7 @@ In `coc-settings.json` opened by `:CocConfig`
166166
}
167167
```
168168

169-
* setting example with [nvim-lspconfig](https://github.com/neovim/nvim-lspconfig/blob/master/CONFIG.md#sqls).
169+
- setting example with [nvim-lspconfig](https://github.com/neovim/nvim-lspconfig/blob/master/CONFIG.md#sqls).
170170

171171
```lua
172172
require'lspconfig'.sqls.setup{
@@ -194,15 +194,15 @@ require'lspconfig'.sqls.setup{
194194
The first setting in `connections` is the default connection.
195195

196196
| Key | Description |
197-
|-------------|----------------------|
197+
| ----------- | -------------------- |
198198
| connections | Database connections |
199199

200200
### connections
201201

202202
`dataSourceName` takes precedence over the value set in `proto`, `user`, `passwd`, `host`, `port`, `dbName`, `params`.
203203

204204
| Key | Description |
205-
|----------------|---------------------------------------------|
205+
| -------------- | ------------------------------------------- |
206206
| alias | Connection alias name. Optional. |
207207
| driver | `mysql`, `postgresql`, `sqlite3`. Required. |
208208
| dataSourceName | Data source name. |
@@ -219,7 +219,7 @@ The first setting in `connections` is the default connection.
219219
#### sshConfig
220220

221221
| Key | Description |
222-
|------------|-----------------------------|
222+
| ---------- | --------------------------- |
223223
| host | ssh host. Required. |
224224
| port | ssh port. Required. |
225225
| user | ssh user. Optional. |
@@ -230,9 +230,9 @@ The first setting in `connections` is the default connection.
230230

231231
See also.
232232

233-
- https://github.com/go-sql-driver/mysql#dsn-data-source-name
234-
- https://godoc.org/github.com/lib/pq
235-
- https://github.com/mattn/go-sqlite3#connection-string
233+
- <https://github.com/go-sql-driver/mysql#dsn-data-source-name>
234+
- <https://pkg.go.dev/github.com/jackc/pgx/v4>
235+
- <https://github.com/mattn/go-sqlite3#connection-string>
236236

237237
## Contributors
238238

go.mod

+10-14
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
module github.com/lighttiger2505/sqls
22

3-
go 1.13
3+
go 1.16
44

55
require (
6-
github.com/go-sql-driver/mysql v1.5.0
7-
github.com/google/go-cmp v0.3.1
6+
github.com/go-sql-driver/mysql v1.6.0
7+
github.com/google/go-cmp v0.5.6
8+
github.com/jackc/pgx/v4 v4.12.0
89
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
9-
github.com/k0kubun/pp v3.0.1+incompatible
10-
github.com/lib/pq v1.3.0
11-
github.com/mattn/go-colorable v0.1.4 // indirect
12-
github.com/mattn/go-isatty v0.0.9 // indirect
13-
github.com/mattn/go-sqlite3 v1.14.1
14-
github.com/olekukonko/tablewriter v0.0.4
15-
github.com/pkg/errors v0.8.1
16-
github.com/sourcegraph/jsonrpc2 v0.0.0-20191222043438-96c4efab7ee2
17-
golang.org/x/crypto v0.0.0-20200429183012-4b2356b1ed79
18-
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
19-
gopkg.in/yaml.v2 v2.2.8
10+
github.com/k0kubun/pp v2.4.0+incompatible
11+
github.com/mattn/go-sqlite3 v1.14.8
12+
github.com/olekukonko/tablewriter v0.0.5
13+
github.com/sourcegraph/jsonrpc2 v0.1.0
14+
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97
15+
gopkg.in/yaml.v2 v2.4.0
2016
)

go.sum

+494-31
Large diffs are not rendered by default.

internal/config/config.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package config
22

33
import (
44
"errors"
5+
"fmt"
56
"io/ioutil"
67
"os"
78
"path/filepath"
89

910
"github.com/lighttiger2505/sqls/internal/database"
10-
"golang.org/x/xerrors"
1111
"gopkg.in/yaml.v2"
1212
)
1313

@@ -64,21 +64,21 @@ func (c *Config) Load(fp string) error {
6464

6565
file, err := os.OpenFile(fp, os.O_RDONLY, 0666)
6666
if err != nil {
67-
return xerrors.Errorf("cannot open config, %+v", err)
67+
return fmt.Errorf("cannot open config, %w", err)
6868
}
6969
defer file.Close()
7070

7171
b, err := ioutil.ReadAll(file)
7272
if err != nil {
73-
return xerrors.Errorf("cannot read config, %+v", err)
73+
return fmt.Errorf("cannot read config, %w", err)
7474
}
7575

7676
if err = yaml.Unmarshal(b, c); err != nil {
77-
return xerrors.Errorf("failed unmarshal yaml, %+v", err, string(b))
77+
return fmt.Errorf("failed unmarshal yaml, %w, %s", err, string(b))
7878
}
7979

8080
if err := c.Validate(); err != nil {
81-
return xerrors.Errorf("failed validation, %+v", err)
81+
return fmt.Errorf("failed validation, %w", err)
8282
}
8383
return nil
8484
}

internal/database/config.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
"github.com/lighttiger2505/sqls/dialect"
99
"golang.org/x/crypto/ssh"
10-
"golang.org/x/xerrors"
1110
)
1211

1312
type Proto string
@@ -107,19 +106,19 @@ func (s *SSHConfig) Endpoint() string {
107106
func (s *SSHConfig) ClientConfig() (*ssh.ClientConfig, error) {
108107
buffer, err := ioutil.ReadFile(s.PrivateKey)
109108
if err != nil {
110-
return nil, xerrors.Errorf("cannot read SSH private key file, PrivateKey=%s, %+v", s.PrivateKey, err)
109+
return nil, fmt.Errorf("cannot read SSH private key file, PrivateKey=%s, %w", s.PrivateKey, err)
111110
}
112111

113112
var key ssh.Signer
114113
if s.PassPhrase != "" {
115114
key, err = ssh.ParsePrivateKeyWithPassphrase(buffer, []byte(s.PassPhrase))
116115
if err != nil {
117-
return nil, xerrors.Errorf("cannot parse SSH private key file with passphrase, PrivateKey=%s, %+v", s.PrivateKey, err)
116+
return nil, fmt.Errorf("cannot parse SSH private key file with passphrase, PrivateKey=%s, %w", s.PrivateKey, err)
118117
}
119118
} else {
120119
key, err = ssh.ParsePrivateKey(buffer)
121120
if err != nil {
122-
return nil, xerrors.Errorf("cannot parse SSH private key file, PrivateKey=%s, %+v", s.PrivateKey, err)
121+
return nil, fmt.Errorf("cannot parse SSH private key file, PrivateKey=%s, %w", s.PrivateKey, err)
123122
}
124123
}
125124

internal/database/database_mock.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ var dummyDatabases = []string{
116116
"world",
117117
}
118118
var dummyDatabaseTables = map[string][]string{
119-
"world": []string{
119+
"world": {
120120
"city",
121121
"country",
122122
"countrylanguage",

internal/database/driver.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
"github.com/lighttiger2505/sqls/dialect"
88
"golang.org/x/crypto/ssh"
9-
"golang.org/x/xerrors"
109
)
1110

1211
var driverOpeners = make(map[dialect.DatabaseDriver]Opener)
@@ -59,15 +58,15 @@ func Registered(name dialect.DatabaseDriver) bool {
5958
func Open(cfg *DBConfig) (*DBConnection, error) {
6059
OpenFn, ok := driverOpeners[cfg.Driver]
6160
if !ok {
62-
return nil, xerrors.Errorf("driver not found, %s", cfg.Driver)
61+
return nil, fmt.Errorf("driver not found, %s", cfg.Driver)
6362
}
6463
return OpenFn(cfg)
6564
}
6665

6766
func CreateRepository(driver dialect.DatabaseDriver, db *sql.DB) (DBRepository, error) {
6867
FactoryFn, ok := driverFactories[driver]
6968
if !ok {
70-
return nil, xerrors.Errorf("driver not found, %s", driver)
69+
return nil, fmt.Errorf("driver not found, %s", driver)
7170
}
7271
return FactoryFn(db), nil
7372
}

internal/database/mysql.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/go-sql-driver/mysql"
1111
"github.com/lighttiger2505/sqls/dialect"
1212
"golang.org/x/crypto/ssh"
13-
"golang.org/x/xerrors"
1413
)
1514

1615
func init() {
@@ -49,7 +48,7 @@ func mysqlOpen(dbConnCfg *DBConfig) (*DBConnection, error) {
4948
conn = dbConn
5049
}
5150
if err := conn.Ping(); err != nil {
52-
return nil, xerrors.Errorf("cannot ping to database, %+v", err)
51+
return nil, fmt.Errorf("cannot ping to database, %w", err)
5352
}
5453

5554
conn.SetMaxIdleConns(DefaultMaxIdleConns)
@@ -77,12 +76,12 @@ func openMySQLViaSSH(dsn string, sshCfg *SSHConfig) (*sql.DB, *ssh.Client, error
7776
}
7877
sshConn, err := ssh.Dial("tcp", sshCfg.Endpoint(), sshConfig)
7978
if err != nil {
80-
return nil, nil, xerrors.Errorf("cannot ssh dial, %+v", err)
79+
return nil, nil, fmt.Errorf("cannot ssh dial, %w", err)
8180
}
8281
mysql.RegisterDialContext("mysql+tcp", (&MySQLViaSSHDialer{sshConn}).Dial)
8382
conn, err := sql.Open("mysql", dsn)
8483
if err != nil {
85-
return nil, nil, xerrors.Errorf("cannot connect database, %+v", err)
84+
return nil, nil, fmt.Errorf("cannot connect database, %w", err)
8685
}
8786
return conn, sshConn, nil
8887
}
@@ -152,6 +151,7 @@ func (db *MySQLDBRepository) Databases(ctx context.Context) ([]string, error) {
152151
if err != nil {
153152
return nil, err
154153
}
154+
defer rows.Close()
155155
databases := []string{}
156156
for rows.Next() {
157157
var database string
@@ -187,6 +187,7 @@ func (db *MySQLDBRepository) SchemaTables(ctx context.Context) (map[string][]str
187187
if err != nil {
188188
return nil, err
189189
}
190+
defer rows.Close()
190191
databaseTables := map[string][]string{}
191192
for rows.Next() {
192193
var schema, table string
@@ -208,6 +209,7 @@ func (db *MySQLDBRepository) Tables(ctx context.Context) ([]string, error) {
208209
if err != nil {
209210
return nil, err
210211
}
212+
defer rows.Close()
211213
tables := []string{}
212214
for rows.Next() {
213215
var table string
@@ -237,6 +239,7 @@ FROM information_schema.COLUMNS
237239
if err != nil {
238240
return nil, err
239241
}
242+
defer rows.Close()
240243
tableInfos := []*ColumnDesc{}
241244
for rows.Next() {
242245
var tableInfo ColumnDesc
@@ -277,6 +280,7 @@ WHERE information_schema.COLUMNS.TABLE_SCHEMA = ?
277280
if err != nil {
278281
return nil, err
279282
}
283+
defer rows.Close()
280284
tableInfos := []*ColumnDesc{}
281285
for rows.Next() {
282286
var tableInfo ColumnDesc

0 commit comments

Comments
 (0)