Skip to content

Commit

Permalink
Change sqlType to string in config + update examples/readme
Browse files Browse the repository at this point in the history
  • Loading branch information
jackyzhen committed Oct 2, 2018
1 parent 70ba445 commit 7eab526
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 21 deletions.
11 changes: 10 additions & 1 deletion .databases.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
"dbName": "prod_database",
"dbServer": "prod.db.server.com",
"user": "user",
"pass": "pass"
"pass": "pass",
"sqlType": "mysql"
},
"postgresdb": {
"appServer": "pg.server.com",
"dbName": "pg_database",
"dbServer": "pg.db.server.com",
"user": "postgres",
"pass": "postgres",
"sqlType": "postgres"
}
}
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ MySQL pipe

## What does it do?

- `sql` allows you to pipe STDIN (hopefully containing SQL) to one or more pre-configured MySQL databases
- `sql` allows you to pipe STDIN (hopefully containing SQL) to one or more pre-configured MySQL or PostgreSQL databases
- output comes out in `\t`-separated format, allowing further piping (e.g. works really well with [chart](https://github.com/MarianoGappa/chart))
- when more than one database is queried, the requests are made in parallel
- `sql` can either run `mysql` locally, run `mysql` locally but connecting to a remote host (by configuring a `dbServer`), or `ssh` to a remote host and from there run `mysql` to either a local or remote host (by configuring an `appServer` and a `dbServer`)
- `sql` can either run `mysql/psql` locally, run `mysql/psql` locally but connecting to a remote host (by configuring a `dbServer`), or `ssh` to a remote host and from there run `mysql/psql` to either a local or remote host (by configuring an `appServer` and a `dbServer`)

## Installation

Expand All @@ -37,6 +37,8 @@ $ compinit

Create a `.databases.json` dotfile in your home folder or in any [XDG-compliant](https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html) directory. [This](.databases.json.example) is an example file.

`sql` decides to execute with MySQL or PostgreSQL depending on the `sqlType` property set for a database, *defaulting to to MySQL if not set.*

## Example usages

```
Expand All @@ -50,8 +52,9 @@ sql all "SELECT * FROM users WHERE name = 'John'"
## Notes

- when more than one database is queried, the resulting rows are prefixed with the database identifier
- the `all` special keyword means "sql to all configured databases"
- the `all` special keyword means "sql to all configured databases".
- `sql` assumes that you have correctly configured SSH keys on all servers you `ssh` to
- `sql` will error if all targeted databases do not have the same sql type.

## Beware!

Expand All @@ -62,6 +65,7 @@ sql all "SELECT * FROM users WHERE name = 'John'"
## Dependencies

- mysql
- psql
- ssh (only if you configure an "appServer")

## Contribute
Expand Down
15 changes: 14 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ type database struct {
DbName string
User string
Pass string
SQLType sqlType
SQLType string
_sqlType sqlType
}

func mustReadDatabasesConfigFile() map[string]database {
Expand Down Expand Up @@ -67,5 +68,17 @@ func mustReadDatabasesConfigFile() map[string]database {
usage("Couldn't find any database configurations on .databases.json. Looked like this:\n\n%v\n", string(byts))
}

for dbName, db := range databases {
typeStr := db.SQLType
if typeStr == "" {
typeStr = "mysql"
}
if _, ok := validSQLTypes[typeStr]; !ok {
usage("Unknown sql type %v for %v", typeStr, dbName)
}
db._sqlType = validSQLTypes[typeStr]
databases[dbName] = db
}

return databases
}
9 changes: 3 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,11 @@ func _main(databases map[string]database, databasesArgs []string, query string,
targetDatabases = append(targetDatabases, k)
}

sqlTypes := map[sqlType]struct{}{}
sqlTypes := map[sqlType]exists{}
var sqlType sqlType
for _, db := range targetDatabases {
sqlType = databases[db].SQLType
if _, ok := validSQLTypes[sqlType]; !ok {
usage("Unknown sql type %v", sqlType)
}
sqlTypes[sqlType] = struct{}{}
sqlType = databases[db]._sqlType
sqlTypes[sqlType] = exists{}
if len(sqlTypes) > 1 {
usage("More than one sql types specified in target databases.")
}
Expand Down
12 changes: 6 additions & 6 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ func Test_MySQL(t *testing.T) {

var (
testConfig = map[string]database{
"db1": database{DbServer: "test-mysql", DbName: "db1", User: "root", Pass: ""},
"db2": database{DbServer: "test-mysql", DbName: "db2", User: "root", Pass: ""},
"db3": database{DbServer: "test-mysql", DbName: "db3", User: "root", Pass: ""},
"db1": database{DbServer: "test-mysql", DbName: "db1", User: "root", Pass: "", _sqlType: mySQL},
"db2": database{DbServer: "test-mysql", DbName: "db2", User: "root", Pass: "", _sqlType: mySQL},
"db3": database{DbServer: "test-mysql", DbName: "db3", User: "root", Pass: "", _sqlType: mySQL},
}
ts = []struct {
name string
Expand Down Expand Up @@ -129,9 +129,9 @@ func Test_PostgreSQL(t *testing.T) {

var (
testConfig = map[string]database{
"db1": database{DbServer: "test-postgres", DbName: "db1", User: "root", Pass: "", SQLType: postgreSQL},
"db2": database{DbServer: "test-postgres", DbName: "db2", User: "root", Pass: "", SQLType: postgreSQL},
"db3": database{DbServer: "test-postgres", DbName: "db3", User: "root", Pass: "", SQLType: postgreSQL},
"db1": database{DbServer: "test-postgres", DbName: "db1", User: "root", Pass: "", _sqlType: postgreSQL},
"db2": database{DbServer: "test-postgres", DbName: "db2", User: "root", Pass: "", _sqlType: postgreSQL},
"db3": database{DbServer: "test-postgres", DbName: "db3", User: "root", Pass: "", _sqlType: postgreSQL},
}
ts = []struct {
name string
Expand Down
8 changes: 4 additions & 4 deletions sql_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
type sqlType int

const (
mySQL sqlType = iota
mySQL sqlType = iota + 1
postgreSQL
)

Expand All @@ -28,9 +28,9 @@ type sqlOptions struct {
flags string
}

var validSQLTypes = map[sqlType]exists{
mySQL: exists{},
postgreSQL: exists{},
var validSQLTypes = map[string]sqlType{
"mysql": mySQL,
"postgres": postgreSQL,
}

var sqlTypeToOptions = map[sqlType]sqlOptions{
Expand Down

0 comments on commit 7eab526

Please # to comment.