Skip to content

Commit

Permalink
Initial addition of DuckDB
Browse files Browse the repository at this point in the history
  • Loading branch information
kenshaw committed Nov 3, 2023
1 parent 3483528 commit 39becc7
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*.sqlite3
*.sqlite3-journal
*.duckdb
*.wal

/instantclient*
/*.pc
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,10 @@ The following are the [Go SQL drivers][go-sql] that `usql` supports, the
associated database, scheme / build tag, and scheme aliases:

<!-- DRIVER DETAILS START -->

| Database | Scheme / Tag | Scheme Aliases | Driver Package / Notes |
| -------------------- | --------------- | ----------------------------------------------- | --------------------------------------------------------------------------- |
|----------------------|-----------------|-------------------------------------------------|-----------------------------------------------------------------------------|
| PostgreSQL | `postgres` | `pg`, `pgsql`, `postgresql` | [github.com/lib/pq][d-postgres] |
| ClickHouse | `clickhouse` | `ch` | [github.com/ClickHouse/clickhouse-go/v2][d-clickhouse] |
| MySQL | `mysql` | `my`, `maria`, `aurora`, `mariadb`, `percona` | [github.com/go-sql-driver/mysql][d-mysql] |
| Microsoft SQL Server | `sqlserver` | `ms`, `mssql`, `azuresql` | [github.com/microsoft/go-mssqldb][d-sqlserver] |
| Oracle Database | `oracle` | `or`, `ora`, `oci`, `oci8`, `odpi`, `odpi-c` | [github.com/sijms/go-ora/v2][d-oracle] |
Expand All @@ -232,10 +232,10 @@ associated database, scheme / build tag, and scheme aliases:
| AWS Athena | `athena` | `s3`, `aws`, `awsathena` | [github.com/uber/athenadriver/go][d-athena] |
| Azure CosmosDB | `cosmos` | `cm` | [github.com/btnguyen2k/gocosmos][d-cosmos] |
| Cassandra | `cassandra` | `ca`, `scy`, `scylla`, `datastax`, `cql` | [github.com/MichaelS11/go-cql-driver][d-cassandra] |
| ClickHouse | `clickhouse` | `ch` | [github.com/ClickHouse/clickhouse-go/v2][d-clickhouse] |
| Couchbase | `couchbase` | `n1`, `n1ql` | [github.com/couchbase/go_n1ql][d-couchbase] |
| Cznic QL | `ql` | `cznic`, `cznicql` | [modernc.org/ql][d-ql] |
| Databend | `databend` | `dd`, `bend` | [github.com/databendcloud/databend-go][d-databend] |
| DuckDB | `duckdb` | `dk`, `ddb`, `duck` | [github.com/marcboeker/go-duckdb][d-duckdb] <sup>[][f-cgo]</sup> |
| Exasol | `exasol` | `ex`, `exa` | [github.com/exasol/exasol-driver-go][d-exasol] |
| Firebird | `firebird` | `fb`, `firebirdsql` | [github.com/nakagami/firebirdsql][d-firebird] |
| FlightSQL | `flightsql` | `fl`, `flight` | [github.com/apache/arrow/go/v12/arrow/flight/flightsql/driver][d-flightsql] |
Expand Down Expand Up @@ -284,6 +284,7 @@ associated database, scheme / build tag, and scheme aliases:
[d-couchbase]: https://github.com/couchbase/go_n1ql
[d-csvq]: https://github.com/mithrandie/csvq-driver
[d-databend]: https://github.com/databendcloud/databend-go
[d-duckdb]: https://github.com/marcboeker/go-duckdb
[d-exasol]: https://github.com/exasol/exasol-driver-go
[d-firebird]: https://github.com/nakagami/firebirdsql
[d-flightsql]: https://github.com/apache/arrow/tree/main/go/arrow/flight/flightsql/driver
Expand Down Expand Up @@ -314,7 +315,6 @@ associated database, scheme / build tag, and scheme aliases:
[d-trino]: https://github.com/trinodb/trino-go-client
[d-vertica]: https://github.com/vertica/vertica-sql-go
[d-voltdb]: https://github.com/VoltDB/voltdb-client-go

<!-- DRIVER DETAILS END -->

[f-cgo]: #f-cgo "Requires CGO"
Expand Down
2 changes: 2 additions & 0 deletions contrib/duckdb/usql-config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DB="duckdb:test.duckdb"
VSQL="SELECT library_version AS version FROM pragma_version();"
2 changes: 1 addition & 1 deletion contrib/podman-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ case $DIR in
fi
;;
test)
TARGETS+=(mysql clickhouse postgres sqlserver oracle cassandra)
TARGETS+=(mysql postgres sqlserver oracle clickhouse cassandra)
;;
*)
TARGETS+=($DIR)
Expand Down
2 changes: 1 addition & 1 deletion contrib/usql-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fi

for TARGET in $SRC/*/usql-config; do
NAME=$(basename $(dirname $TARGET))
if [ ! -z "$(podman ps -q --filter "name=$NAME")" ]; then
if [[ ! -z "$(podman ps -q --filter "name=$NAME")" || "$NAME" == "duckdb" || "$NAME" == "sqlite3" ]]; then
unset DB VSQL
source $TARGET
if [ -z "$DB" ]; then
Expand Down
26 changes: 26 additions & 0 deletions drivers/duckdb/duckdb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Package duckdb defines and registers usql's DuckDB driver. Requires CGO.
//
// See: https://github.com/marcboeker/go-duckdb
package duckdb

import (
"context"

_ "github.com/marcboeker/go-duckdb" // DRIVER
"github.com/xo/usql/drivers"
)

func init() {
drivers.Register("duckdb", drivers.Driver{
AllowMultilineComments: true,
Version: func(ctx context.Context, db drivers.DB) (string, error) {
var ver string
err := db.QueryRowContext(ctx, `SELECT library_version FROM pragma_version()`).Scan(&ver)
if err != nil {
return "", err
}
return "DuckDB " + ver, nil
},
Copy: drivers.CopyWithInsert(func(int) string { return "?" }),
})
}
1 change: 1 addition & 0 deletions drivers/qtype.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var queryMap = map[string]bool{
"LIST": true, // list permissions, roles, users (cassandra)
"EXEC": true, // execute a stored procedure that returns rows (not postgres)
"TABLE": true, // shortcut for select * from <table> (postgresql)
"CALL": true,
}

// execMap is the map of SQL prefixes to execute.
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ require (
github.com/jeandeaual/go-locale v0.0.0-20220711133428-7de61946b173
github.com/jmrobles/h2go v0.5.0
github.com/lib/pq v1.10.9
github.com/marcboeker/go-duckdb v1.5.1
github.com/mattn/go-adodb v0.0.1
github.com/mattn/go-isatty v0.0.20
github.com/mattn/go-sqlite3 v1.14.17
Expand All @@ -50,7 +51,7 @@ require (
github.com/trinodb/trino-go-client v0.313.0
github.com/uber/athenadriver v1.1.14
github.com/vertica/vertica-sql-go v1.3.3
github.com/xo/dburl v0.16.0
github.com/xo/dburl v0.16.3
github.com/xo/tblfmt v0.10.3
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e
github.com/yookoala/realpath v1.0.0
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,8 @@ github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/lucor/goinfo v0.0.0-20200401173949-526b5363a13a/go.mod h1:ORP3/rB5IsulLEBwQZCJyyV6niqmI7P4EWSmkug+1Ng=
github.com/marcboeker/go-duckdb v1.5.1 h1:Mh6h0ke9EyM2XA9dWiNOawM+oUFXYOY5o2csJ32uxBw=
github.com/marcboeker/go-duckdb v1.5.1/go.mod h1:wm91jO2GNKa6iO9NTcjXIRsW+/ykPoJbQcHSXhdAl28=
github.com/mattn/go-adodb v0.0.1 h1:g/pk3V8m/WFX2IQRI58wAC24OQUFFXEiNsvs7dQ1WKg=
github.com/mattn/go-adodb v0.0.1/go.mod h1:jaSTRde4bohMuQgYQPxW3xRTPtX/cZKyxPrFVseJULo=
github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=
Expand Down Expand Up @@ -985,8 +987,8 @@ github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
github.com/xhit/go-str2duration/v2 v2.1.0 h1:lxklc02Drh6ynqX+DdPyp5pCKLUQpRT8bp8Ydu2Bstc=
github.com/xhit/go-str2duration/v2 v2.1.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU=
github.com/xo/dburl v0.16.0 h1:jlBeGe8fnsW+vBYemte903WHQbJnZx7OpJZy2ofq+5g=
github.com/xo/dburl v0.16.0/go.mod h1:B7/G9FGungw6ighV8xJNwWYQPMfn3gsi2sn5SE8Bzco=
github.com/xo/dburl v0.16.3 h1:ikRz71tJW3S16fUO+eaxu9hZv660VNdS7ZOyEFjShKQ=
github.com/xo/dburl v0.16.3/go.mod h1:B7/G9FGungw6ighV8xJNwWYQPMfn3gsi2sn5SE8Bzco=
github.com/xo/tblfmt v0.0.0-20190609041254-28c54ec42ce8/go.mod h1:3U5kKQdIhwACye7ml3acccHmjGExY9WmUGU7rnDWgv0=
github.com/xo/tblfmt v0.10.3 h1:Fb2Le5wvzBjOwA/KFRpP5bZo+iR9cZnzSAN3tULoUPI=
github.com/xo/tblfmt v0.10.3/go.mod h1:W6oqhxtqDOEMkrS2GPO0zmcww7ozM2E6BRv3kNfXd24=
Expand Down
9 changes: 9 additions & 0 deletions internal/duckdb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/internal.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 39becc7

Please sign in to comment.