Skip to content

Commit

Permalink
feat: sql server (#222)
Browse files Browse the repository at this point in the history
* build(compose): add the mssql service to the compose service

* build(dbmigrate): add everything necessary to run migrations on mssql

* feat(client): add mssql client

* feat(connection): add a parser to generate or process a dsn connection string to connnect with sql server

* feat(command): add new connection as flags to connect with sql server

* feat(config): add a sql as an option to connect through a config file

* build(make): add targets to connect with sql server container

* fix: remove sql migrations

* feat(form): add sql to the bubbletea app

* docs(readme): add docs on how to connect with the sql server
  • Loading branch information
danvergara authored Jul 1, 2024
1 parent 61b20d3 commit 81e200c
Show file tree
Hide file tree
Showing 23 changed files with 562 additions and 128 deletions.
18 changes: 18 additions & 0 deletions .dblab.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,22 @@ database:
user: "postgres"
schema: "public"
driver: "postgres"
- name: "oracle"
host: "localhost"
port: 1521
db: "FREEPDB1 "
password: "password"
user: "system"
driver: "oracle"
ssl: "enable"
wallet: "path/to/wallet"
ssl-verify: true
trace: "trace.log"
- name: "sqlserver"
driver: "sqlserver"
host: "localhost"
port: 1433
db: "msdb"
password: "5@klkbN#ABC"
user: "SA"
limit: 50
14 changes: 10 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ run: build
run-mysql: build
./dblab --host localhost --user myuser --db mydb --pass 5@klkbN#ABC --ssl enable --port 3306 --driver mysql

.PHONY: run-oracle
## run-oracle: Runs the application making a connection to the Oreacle database
run-oracle: build
./dblab --host localhost --user system --db FREEPDB1 --pass password --port 1521 --driver oracle --limit 50

.PHONY: run-mysql-socket
## run-mysql-socket: Runs the application with a connection to mysql through a socket file. In this example the socke file is located in /var/lib/mysql/mysql.sock.
Expand All @@ -61,6 +57,16 @@ run-mysql-socket: build
run-postgres-socket: build
./dblab --socket /var/run/postgresql --user myuser --db my_project --pass postgres --ssl disable --port 5432 --driver postgres --limit 50

.PHONY: run-oracle
## run-oracle: Runs the application making a connection to the Oracle database
run-oracle: build
./dblab --host localhost --user system --db FREEPDB1 --pass password --port 1521 --driver oracle --limit 50

.PHONY: run-sql-server
## run-sql-server: Runs the application making a connection to the SQL Server database
run-sql-server: build
./dblab --host localhost --user SA --db msdb --pass '5@klkbN#ABC' --port 1433 --driver sqlserver --limit 50

.PHONY: run-mysql-socket-url
## run-mysql-socket-url: Runs the application with a connection to mysql through a socket file. In this example the socke file is located in /var/lib/mysql/mysql.sock.
run-mysql-socket-url: build
Expand Down
58 changes: 35 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ dblab ![integration tests](https://github.com/danvergara/dblab/actions/workflows
<img style="float: right;" src="assets/gopher-dblab.png" alt="dblab logo"/ width=200>
</p>

__Interactive client for PostgreSQL, MySQL, SQLite3 and Oracle.__
__Interactive client for PostgreSQL, MySQL, SQLite3, Oracle and SQL Server.__

<img src="screenshots/dblab-cover.png" />

Expand Down Expand Up @@ -37,7 +37,7 @@ dblab is a fast and lightweight interactive terminal based UI application for Po
written in Go and works on OSX, Linux and Windows machines. Main idea behind using Go for backend development
is to utilize ability of the compiler to produce zero-dependency binaries for
multiple platforms. dblab was created as an attempt to build very simple and portable
application to work with local or remote PostgreSQL/MySQL/SQLite3/Oracle databases.
application to work with local or remote PostgreSQL/MySQL/SQLite3/Oracle/SQL Server databases.

## Features

Expand Down Expand Up @@ -91,27 +91,30 @@ Available Commands:
version The version of the project
Flags:
--cfg-name string Database config name section
--config Get the connection data from a config file (default locations are: current directory, $HOME/.dblab.yaml or $XDG_CONFIG_HOME/.dblab.yaml)
--db string Database name
--driver string Database driver
-h, --help help for dblab
--host string Server host name or IP
--limit uint Size of the result set from the table content query (should be greater than zero, otherwise the app will error out) (default 100)
--pass string Password for user
--port string Server port
--schema string Database schema (postgres only)
--socket string Path to a Unix socket file
--ssl string SSL mode
--ssl-verify string [enable|disable] or [true|false] enable ssl verify for the server
--sslcert string This parameter specifies the file name of the client SSL certificate, replacing the default ~/.postgresql/postgresql.crt
--sslkey string This parameter specifies the location for the secret key used for the client certificate. It can either specify a file name that will be used instead of the default ~/.postgresql/postgresql.key, or it can specify a key obtained from an external “engine”
--sslpassword string This parameter specifies the password for the secret key specified in sslkey
--sslrootcert string This parameter specifies the name of a file containing SSL certificate authority (CA) certificate(s) The default is ~/.postgresql/root.crt
--trace-file string File name for trace log
-u, --url string Database connection string
--user string Database user
--wallet string Path for auto-login oracle wallet
--cfg-name string Database config name section
--config Get the connection data from a config file (default locations are: current directory, $HOME/.dblab.yaml or $XDG_CONFIG_HOME/.dblab.yaml)
--db string Database name
--driver string Database driver
--encrypt string [strict|disable|false|true] data sent between client and server is encrypted or not
-h, --help help for dblab
--host string Server host name or IP
--limit uint Size of the result set from the table content query (should be greater than zero, otherwise the app will error out) (default 100)
--pass string Password for user
--port string Server port
--schema string Database schema (postgres only)
--socket string Path to a Unix socket file
--ssl string SSL mode
--ssl-verify string [enable|disable] or [true|false] enable ssl verify for the server
--sslcert string This parameter specifies the file name of the client SSL certificate, replacing the default ~/.postgresql/postgresql.crt
--sslkey string This parameter specifies the location for the secret key used for the client certificate. It can either specify a file name that will be used instead of the default ~/.postgresql/postgresql.key, or it can specify a key obtained from an external “engine”
--sslpassword string This parameter specifies the password for the secret key specified in sslkey
--sslrootcert string This parameter specifies the name of a file containing SSL certificate authority (CA) certificate(s) The default is ~/.postgresql/root.crt
--timeout string in seconds (default is 0 for no timeout), set to 0 for no timeout. Recommended to set to 0 and use context to manage query and connection timeouts
--trace-file string File name for trace log
--trust-server-certificate string [false|true] server certificate is checked or not
-u, --url string Database connection string
--user string Database user
--wallet string Path for auto-login oracle wallet
Use "dblab [command] --help" for more information about a command.
```
Expand All @@ -126,6 +129,7 @@ You can start the app passing no flags or parameters, you'll be asked for connec
$ dblab --host localhost --user myuser --db users --pass password --ssl disable --port 5432 --driver postgres --limit 50
$ dblab --db path/to/file.sqlite3 --driver sqlite
$ dblab --host localhost --user system --db FREEPDB1 --pass password --port 1521 --driver oracle --limit 50
$ dblab --host localhost --user SA --db msdb --pass '5@klkbN#ABC' --port 1433 --driver sqlserver --limit 50
```

Connection URL scheme is also supported:
Expand All @@ -135,6 +139,7 @@ $ dblab --url 'postgres://user:password@host:port/database?sslmode=[mode]'
$ dblab --url 'mysql://user:password@tcp(host:port)/db'
$ dblab --url 'file:test.db?_pragma=foreign_keys(1)&_time_format=sqlite'
$ dblab --url 'oracle://user:password@localhost:1521/db'
$ dblab --url 'sqlserver://SA:myStrong(!)Password@localhost:1433?database=tempdb&encrypt=true&trustservercertificate=false&connection+timeout=30'
```

if you're using PostgreSQL, you have the option to define the schema you want to work with, the default value is `public`.
Expand Down Expand Up @@ -223,6 +228,13 @@ database:
ssl: "enable"
wallet: "path/to/wallet"
ssl-verify: true
- name: "sqlserver"
driver: "sqlserver"
host: "localhost"
port: 1433
db: "msdb"
password: "5@klkbN#ABC"
user: "SA"
# should be greater than 0, otherwise the app will error out
limit: 50
```
Expand Down
1 change: 1 addition & 0 deletions cmd/dbmigrate/cmd/migratedown.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/mysql"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/database/sqlserver"
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/spf13/cobra"
)
Expand Down
50 changes: 32 additions & 18 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ var (
traceFile string
sslVerify string
wallet string
// sql server.
encrypt string
trustServerCertificate string
connectionTimeout string
)

// NewRootCmd returns the root command.
Expand All @@ -53,24 +57,27 @@ func NewRootCmd() *cobra.Command {
}
} else {
opts = command.Options{
Driver: driver,
URL: url,
Host: host,
Port: port,
User: user,
Pass: pass,
DBName: db,
Schema: schema,
SSL: ssl,
Limit: limit,
Socket: socket,
SSLCert: sslcert,
SSLKey: sslkey,
SSLPassword: sslpassword,
SSLRootcert: sslrootcert,
SSLVerify: sslVerify,
TraceFile: traceFile,
Wallet: wallet,
Driver: driver,
URL: url,
Host: host,
Port: port,
User: user,
Pass: pass,
DBName: db,
Schema: schema,
SSL: ssl,
Limit: limit,
Socket: socket,
SSLCert: sslcert,
SSLKey: sslkey,
SSLPassword: sslpassword,
SSLRootcert: sslrootcert,
SSLVerify: sslVerify,
TraceFile: traceFile,
Wallet: wallet,
Encrypt: encrypt,
TrustServerCertificate: trustServerCertificate,
ConnectionTimeout: connectionTimeout,
}

if form.IsEmpty(opts) {
Expand Down Expand Up @@ -166,4 +173,11 @@ func init() {
StringVarP(&sslVerify, "ssl-verify", "", "", "[enable|disable] or [true|false] enable ssl verify for the server")
rootCmd.Flags().StringVarP(&traceFile, "trace-file", "", "", "File name for trace log")
rootCmd.Flags().StringVarP(&wallet, "wallet", "", "", "Path for auto-login oracle wallet")

rootCmd.Flags().
StringVarP(&encrypt, "encrypt", "", "", "[strict|disable|false|true] data sent between client and server is encrypted or not")
rootCmd.Flags().
StringVarP(&trustServerCertificate, "trust-server-certificate", "", "", "[false|true] server certificate is checked or not")
rootCmd.Flags().
StringVarP(&connectionTimeout, "timeout", "", "", "in seconds (default is 0 for no timeout), set to 0 for no timeout. Recommended to set to 0 and use context to manage query and connection timeouts")
}
10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ services:
networks:
- dblab

mssql:
image: mcr.microsoft.com/mssql/server:2022-latest
environment:
MSSQL_SA_PASSWORD: 5@klkbN#ABC
ACCEPT_EULA: Y
ports:
- '1433:1433'
networks:
- dblab

dblab:
build:
context: .
Expand Down
26 changes: 18 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,31 @@ require (
github.com/jmoiron/sqlx v1.3.5
github.com/kkyr/fig v0.3.0
github.com/lib/pq v1.10.6
github.com/microsoft/go-mssqldb v1.7.2
github.com/muesli/termenv v0.12.0
github.com/olekukonko/tablewriter v0.0.5
github.com/sijms/go-ora/v2 v2.8.19
github.com/spf13/cobra v1.4.0
github.com/stretchr/testify v1.7.2
golang.org/x/text v0.3.7
github.com/stretchr/testify v1.8.4
golang.org/x/text v0.14.0
modernc.org/sqlite v1.22.1
)

require (
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
github.com/Azure/go-autorest/autorest/adal v0.9.16 // indirect
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
github.com/Azure/go-autorest/logger v0.2.1 // indirect
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/denisenkom/go-mssqldb v0.10.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/golang-jwt/jwt/v4 v4.1.0 // indirect
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
Expand All @@ -54,11 +64,11 @@ require (
github.com/rivo/uniseg v0.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/atomic v1.9.0 // indirect
golang.org/x/mod v0.5.0 // indirect
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirect
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467 // indirect
golang.org/x/tools v0.1.5 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/term v0.16.0 // indirect
golang.org/x/tools v0.6.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/uint128 v1.2.0 // indirect
Expand Down
Loading

0 comments on commit 81e200c

Please sign in to comment.