From 7eab526c5d30c0a45f55fb6a0a45c3052ec07248 Mon Sep 17 00:00:00 2001 From: Jacky Zhen Date: Wed, 3 Oct 2018 00:19:57 +1300 Subject: [PATCH] Change sqlType to string in config + update examples/readme --- .databases.json.example | 11 ++++++++++- README.md | 10 +++++++--- config.go | 15 ++++++++++++++- main.go | 9 +++------ main_test.go | 12 ++++++------ sql_runner.go | 8 ++++---- 6 files changed, 44 insertions(+), 21 deletions(-) diff --git a/.databases.json.example b/.databases.json.example index dfb09fb..40d773c 100644 --- a/.databases.json.example +++ b/.databases.json.example @@ -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" } } diff --git a/README.md b/README.md index 54ad27d..7108fe1 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 ``` @@ -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! @@ -62,6 +65,7 @@ sql all "SELECT * FROM users WHERE name = 'John'" ## Dependencies - mysql +- psql - ssh (only if you configure an "appServer") ## Contribute diff --git a/config.go b/config.go index 7613082..6ca017c 100644 --- a/config.go +++ b/config.go @@ -15,7 +15,8 @@ type database struct { DbName string User string Pass string - SQLType sqlType + SQLType string + _sqlType sqlType } func mustReadDatabasesConfigFile() map[string]database { @@ -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 } diff --git a/main.go b/main.go index 53ff2ac..b74aa02 100644 --- a/main.go +++ b/main.go @@ -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.") } diff --git a/main_test.go b/main_test.go index 3dc89fc..e3cfe19 100644 --- a/main_test.go +++ b/main_test.go @@ -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 @@ -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 diff --git a/sql_runner.go b/sql_runner.go index 3946d76..df3e52c 100644 --- a/sql_runner.go +++ b/sql_runner.go @@ -13,7 +13,7 @@ import ( type sqlType int const ( - mySQL sqlType = iota + mySQL sqlType = iota + 1 postgreSQL ) @@ -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{