一個(gè) Go 工具包攬一切 SQL 數(shù)據(jù)庫客戶端
今天發(fā)現(xiàn)了一個(gè)超級棒的工具:通用的 SQL 數(shù)據(jù)庫命令行工具。如果你喜歡通過命令行操作數(shù)據(jù)庫,這個(gè)工具絕對值的擁有。為什么?
第一、它跨平臺,支持 Windows、Mac 和 Linux 等平臺,相應(yīng)可以通過 Scoop、Homebrew 等安裝,或者通過預(yù)編譯好的二進(jìn)制安裝。如果你有 Go 開發(fā)環(huán)境,可以通過源碼安裝。因?yàn)檫@個(gè)工具是用 Go 語言實(shí)現(xiàn)的。
第二、支持眾多 SQL 數(shù)據(jù)庫,比如常見的 PostgreSQL, MySQL, Oracle Database, SQLite3, Microsoft SQL Server。源碼安裝默認(rèn)支持它們。如果要支持更多,可以通過 build tags 編譯。詳細(xì)的支持列表如下:
| Driver | Build Tag | Driver Used |
|---|---|---|
| Microsoft SQL Server | mssql | github.com/denisenkom/go-mssqldb |
| MySQL | mysql | github.com/go-sql-driver/mysql |
| Oracle Database | oracle | github.com/sijms/go-ora |
| PostgreSQL | postgres | github.com/lib/pq |
| SQLite3 | sqlite3 | github.com/mattn/go-sqlite3 |
| MySQL | mymysql | github.com/ziutek/mymysql/godrv |
| Oracle Database | godror | github.com/godror/godror |
| PostgreSQL | pgx | github.com/jackc/pgx/stdlib |
| Alibaba MaxCompute | maxcompute | sqlflow.org/gomaxcompute |
| Apache Avatica | avatica | github.com/Boostport/avatica |
| Apache H2 | h2 | github.com/jmrobles/h2go |
| Apache Hive | hive | sqlflow.org/gohive |
| Apache Ignite | ignite | github.com/amsokol/ignite-go-client |
| Apache Impala | impala | github.com/bippio/go-impala |
| AWS Athena | athena | github.com/uber/athenadriver/go |
| Azure Cosmos | cosmos | github.com/btnguyen2k/gocosmos |
| Cassandra | cassandra | github.com/MichaelS11/go-cql-driver |
| ClickHouse | clickhouse | github.com/kshvakov/clickhouse |
| Couchbase | couchbase | github.com/couchbase/go_n1ql |
| Cznic QL | ql | modernc.org/ql |
| Firebird SQL | firebird | github.com/nakagami/firebirdsql |
| Genji | genji | github.com/genjidb/genji/sql/driver |
| Google BigQuery | bigquery | gorm.io/driver/bigquery/driver |
| Google Spanner | spanner | github.com/rakyll/go-sql-driver-spanner |
| Microsoft ADODB | adodb | github.com/mattn/go-adodb |
| ModernC SQLite | moderncsqlite | modernc.org/sqlite |
| ODBC | odbc | github.com/alexbrainman/odbc |
| Presto | presto | github.com/prestodb/presto-go-client/presto |
| SAP ASE | tds | github.com/thda/tds |
| SAP HANA | hdb | github.com/SAP/go-hdb/driver |
| Snowflake | snowflake | github.com/snowflakedb/gosnowflake |
| Trino | trino | github.com/trinodb/trino-go-client/trino |
| Vertica | vertica | github.com/vertica/vertica-sql-go |
| VoltDB | voltdb | github.com/VoltDB/voltdb-client-go/voltdbclient |
| MOST DRIVERS | most | all drivers excluding ODBC (requires CGO and additional dependencies) |
| ALL DRIVERS | all | all drivers |
實(shí)話說,真的不要太強(qiáng)大。
而且,雖然支持這么多數(shù)據(jù)庫,但連接和使用卻是類似的。
如何連接數(shù)據(jù)庫
usql 命令接上 DSN 就可以連上對應(yīng)的數(shù)據(jù)庫了。DSN 的語法如下(三種形式):
driver+transport://user:pass@host/dbname?opt1=a&opt2=b
driver:/path/to/file
/path/to/file
比如連接到 MySQL 數(shù)據(jù)庫的例子:
$ usql my://user:pass@host/dbname
$ usql mysql://user:pass@host:port/dbname
$ usql my://
$ usql /var/run/mysqld/mysqld.sock
鏈接到 SQLite3:
$ usql dbname.sqlite3
此外,還支持直接使用 usql 進(jìn)入交互界面,然后通過 \c + dsn 的方式連接。這樣可以隨時(shí)切換數(shù)據(jù)庫。
此外,該工具還支持語法高亮,執(zhí)行外部 shell 命令等。這么好的工具,趕緊試試吧!
對了,項(xiàng)目地址:https://github.com/xo/usql,Star 數(shù) 6.1k+。
文末「閱讀原文」可直達(dá)項(xiàng)目首頁。
推薦閱讀
