Skip to content

Commit

Permalink
Add sqliteshim package
Browse files Browse the repository at this point in the history
  • Loading branch information
tie committed Jun 29, 2021
1 parent a275bc4 commit 214aa00
Show file tree
Hide file tree
Showing 10 changed files with 278 additions and 3 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ tag:
git tag dialect/mysqldialect/$(VERSION)
git tag dialect/sqlitedialect/$(VERSION)
git tag driver/pgdriver/$(VERSION)
git tag driver/sqliteshim/$(VERSION)
git tag dbfixture/$(VERSION)
git tag extra/bundebug/$(VERSION)
git tag extra/bunotel/$(VERSION)
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ You also need to install a database/sql driver and the corresponding Bun

## Quickstart

First you need to create a `sql.DB`. Here we using the SQLite3 driver.
First you need to create a `sql.DB`. Here we are using the [SQLite3 driver](https://pkg.go.dev/github.com/uptrace/bun/driver/sqliteshim).

```go
import _ "github.com/mattn/go-sqlite3"
import _ "github.com/uptrace/bun/driver/sqliteshim"

sqldb, err := sql.Open("sqlite3", ":memory:?cache=shared")
sqldb, err := sql.Open("sqliteshim", "file::memory:?cache=shared")
if err != nil {
panic(err)
}
Expand Down
9 changes: 9 additions & 0 deletions driver/sqliteshim/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/uptrace/bun/driver/sqliteshim

go 1.16

require (
github.com/mattn/go-sqlite3 v1.14.7
github.com/stretchr/testify v1.7.0
modernc.org/sqlite v1.11.1
)
83 changes: 83 additions & 0 deletions driver/sqliteshim/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/google/go-cmp v0.5.3 h1:x95R7cp+rSeeqAMI2knLtQ0DKlaBhv2NrtrOvafPHRo=
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/go-sqlite3 v1.14.7 h1:fxWBnXkxfM6sRiuH3bqJ4CfzZojMOLVc0UTsTglEghA=
github.com/mattn/go-sqlite3 v1.14.7/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 h1:OdAsTTz6OkFY5QxjkYwrChwuRruF69c169dPK26NUlk=
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201126233918-771906719818/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c h1:VwygUrnw9jn88c4u8GD3rZQbqrP/tgas88tPUbBxQrk=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20201124115921-2c860bdd6e78 h1:M8tBwCtWD/cZV9DZpFYRUgaymAYAr+aIUTWzDaM3uPs=
golang.org/x/tools v0.0.0-20201124115921-2c860bdd6e78/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
lukechampine.com/uint128 v1.1.1 h1:pnxCASz787iMf+02ssImqk6OLt+Z5QHMoZyUXR4z6JU=
lukechampine.com/uint128 v1.1.1/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk=
modernc.org/cc/v3 v3.33.6 h1:r63dgSzVzRxUpAJFPQWHy1QeZeY1ydNENUDaBx1GqYc=
modernc.org/cc/v3 v3.33.6/go.mod h1:iPJg1pkwXqAV16SNgFBVYmggfMg6xhs+2oiO0vclK3g=
modernc.org/ccgo/v3 v3.9.5 h1:dEuUSf8WN51rDkprFuAqjfchKEzN0WttP/Py3enBwjk=
modernc.org/ccgo/v3 v3.9.5/go.mod h1:umuo2EP2oDSBnD3ckjaVUXMrmeAw8C8OSICVa0iFf60=
modernc.org/httpfs v1.0.6 h1:AAgIpFZRXuYnkjftxTAZwMIiwEqAfk8aVB2/oA6nAeM=
modernc.org/httpfs v1.0.6/go.mod h1:7dosgurJGp0sPaRanU53W4xZYKh14wfzX420oZADeHM=
modernc.org/libc v1.7.13-0.20210308123627-12f642a52bb8/go.mod h1:U1eq8YWr/Kc1RWCMFUWEdkTg8OTcfLw2kY8EDwl039w=
modernc.org/libc v1.9.8/go.mod h1:U1eq8YWr/Kc1RWCMFUWEdkTg8OTcfLw2kY8EDwl039w=
modernc.org/libc v1.9.11 h1:QUxZMs48Ahg2F7SN41aERvMfGLY2HU/ADnB9DC4Yts8=
modernc.org/libc v1.9.11/go.mod h1:NyF3tsA5ArIjJ83XB0JlqhjTabTCHm9aX4XMPHyQn0Q=
modernc.org/mathutil v1.1.1/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E=
modernc.org/mathutil v1.2.2/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E=
modernc.org/mathutil v1.4.0 h1:GCjoRaBew8ECCKINQA2nYjzvufFW9YiEuuB+rQ9bn2E=
modernc.org/mathutil v1.4.0/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E=
modernc.org/memory v1.0.4 h1:utMBrFcpnQDdNsmM6asmyH/FM9TqLPS7XF7otpJmrwM=
modernc.org/memory v1.0.4/go.mod h1:nV2OApxradM3/OVbs2/0OsP6nPfakXpi50C7dcoHXlc=
modernc.org/opt v0.1.1 h1:/0RX92k9vwVeDXj+Xn23DKp2VJubL7k8qNffND6qn3A=
modernc.org/opt v0.1.1/go.mod h1:WdSiB5evDcignE70guQKxYUl14mgWtbClRi5wmkkTX0=
modernc.org/sqlite v1.11.1 h1:SSLaty1rFr9JhVH/Usm2BxzlzaCtf0GhTJ/MJ32GU3Y=
modernc.org/sqlite v1.11.1/go.mod h1:+mhs/P1ONd+6G7hcAs6irwDi/bjTQ7nLW6LHRBsEa3A=
modernc.org/strutil v1.1.1 h1:xv+J1BXY3Opl2ALrBwyfEikFAj8pmqcpnfmuwUwcozs=
modernc.org/strutil v1.1.1/go.mod h1:DE+MQQ/hjKBZS2zNInV5hhcipt5rLPWkmpbGeW5mmdw=
modernc.org/tcl v1.5.5 h1:N03RwthgTR/l/eQvz3UjfYnvVVj1G2sZqzFGfoD4HE4=
modernc.org/tcl v1.5.5/go.mod h1:ADkaTUuwukkrlhqwERyq0SM8OvyXo7+TjFz7yAF56EI=
modernc.org/token v1.0.0 h1:a0jaWiNMDhDUtqOj09wvjWWAqd3q7WpBulmL9H2egsk=
modernc.org/token v1.0.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
modernc.org/z v1.0.1 h1:WyIDpEpAIx4Hel6q/Pcgj/VhaQV5XPJ2I6ryIYbjnpc=
modernc.org/z v1.0.1/go.mod h1:8/SRk5C/HgiQWCgXdfpb+1RvhORdkz5sw72d3jjtyqA=
23 changes: 23 additions & 0 deletions driver/sqliteshim/mattn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Use Cgo sqlite if Cgo is enabled and either modernc is unavailable or Cgo was
// explicitly requested via build tag.

// +build cgo,cgosqlite cgo
// +build cgosqlite !darwin !amd64
// +build cgosqlite !darwin !arm64
// +build cgosqlite !linux !386
// +build cgosqlite !linux !amd64
// +build cgosqlite !linux !arm
// +build cgosqlite !linux !arm64
// +build cgosqlite !windows !amd64

package sqliteshim

import "github.com/mattn/go-sqlite3"

const (
needsCgo = true
usesCgo = true
driverName = "sqlite3"
)

var shimDriver = &sqlite3.SQLiteDriver{}
19 changes: 19 additions & 0 deletions driver/sqliteshim/modernc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Use modernc.org/sqlite on all supported platforms unless Cgo driver
// was explicitly requested.
//
// See also https://pkg.go.dev/modernc.org/sqlite#hdr-Supported_platforms_and_architectures

// +build !cgosqlite
// +build darwin,amd64 darwin,arm64 linux,386 linux,amd64 linux,arm linux,arm64 windows,amd64

package sqliteshim

import "modernc.org/sqlite"

const (
needsCgo = false
usesCgo = false
driverName = "sqlite"
)

var shimDriver = (*sqlite.Driver)(nil)
31 changes: 31 additions & 0 deletions driver/sqliteshim/other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Return error if both Cgo and modernc sqlite implementations are unavailable.
// That includes the case where cgosqlite is set but Cgo is disabled.

// +build !cgo
// +build cgosqlite !darwin !amd64
// +build cgosqlite !darwin !arm64
// +build cgosqlite !linux !386
// +build cgosqlite !linux !amd64
// +build cgosqlite !linux !arm
// +build cgosqlite !linux !arm64
// +build cgosqlite !windows !amd64

package sqliteshim

import "database/sql/driver"

const (
needsCgo = true
usesCgo = false
driverName = ShimName
)

var shimDriver = (*errorDriver)(nil)

type errorDriver struct{}

func (*errorDriver) Open(dsn string) (driver.Conn, error) {
return nil, &errUnsupported
}

var errUnsupported UnsupportedError
42 changes: 42 additions & 0 deletions driver/sqliteshim/sqliteshim.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Package sqliteshim is a shim package that imports an appropriate sqlite
// driver for the build target and registers it under ShimName.
//
// Currently it uses packages in the following order:
// • modernc.org/sqlite on supported platforms,
// • github.com/mattn/go-sqlite3 if Cgo is enabled,
// Otherwise registers a driver that returns an error on unsupported platforms.
//
package sqliteshim

import (
"database/sql"
"database/sql/driver"
)

func init() {
sql.Register(ShimName, shimDriver)
}

const (
// ShimName is the name of the shim database/sql driver registration.
ShimName = "sqliteshim"

// DriverName is the name of the database/sql driver. Note that
// the value depends on the build target.
DriverName = driverName

// HasDriver indicates that SQLite driver implementation is available.
HasDriver = !needsCgo || (needsCgo && usesCgo)
)

// UnsupportedError is returned from driver on unsupported platforms.
type UnsupportedError struct{}

func (e *UnsupportedError) Error() string {
return "sqlite driver is not available on the current platform"
}

// Driver returns the shim driver registered under ShimName name.
func Driver() driver.Driver {
return shimDriver
}
59 changes: 59 additions & 0 deletions driver/sqliteshim/sqliteshim_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package sqliteshim_test

import (
"database/sql"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/uptrace/bun/driver/sqliteshim"
)

func TestShim(t *testing.T) {
sqldb, err := sqlOpen(t, sqliteshim.ShimName)
require.NoError(t, err)
_, err = sqldb.Exec("SELECT 1")
if !sqliteshim.HasDriver {
assert.ErrorAs(t, err, new(*sqliteshim.UnsupportedError))
return
}
assert.NoError(t, err)
}

func TestDriver(t *testing.T) {
if !sqliteshim.HasDriver {
t.SkipNow()
}
sqldb, err := sqlOpen(t, sqliteshim.DriverName)
require.NoError(t, err)
_, err = sqldb.Exec("SELECT 1")
require.NoError(t, err)
}

func TestNoImports(t *testing.T) {
if sqliteshim.HasDriver {
t.SkipNow()
}
drivers := []string{
"sqlite", // modernc
"sqlite3", // mattn
}
for _, driverName := range drivers {
t.Run(driverName, func(t *testing.T) {
_, err := sqlOpen(t, driverName)
require.Error(t, err)
})
}
}

func sqlOpen(t *testing.T, driverName string) (*sql.DB, error) {
sqldb, err := sql.Open(driverName, ":memory:")
if err != nil {
return sqldb, err
}
t.Cleanup(func() {
assert.NoError(t, sqldb.Close())
})
return sqldb, nil
}
8 changes: 8 additions & 0 deletions driver/sqliteshim/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh -eux
CGO_ENABLED=0 go test "$@"
CGO_ENABLED=0 go test -tags cgosqlite "$@"
CGO_ENABLED=1 go test "$@"
CGO_ENABLED=1 go test -tags cgosqlite "$@"

set +x && export PATH="$(go env GOROOT)/misc/wasm:$PATH" && set -x
GOOS=js GOARCH=wasm go test

0 comments on commit 214aa00

Please sign in to comment.