Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add purego driver #93

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions driver_cgo.go
@@ -0,0 +1,11 @@
//go:build !purego
// +build !purego

package sqlite

import (
_ "github.com/mattn/go-sqlite3"
)

// DriverName is the default driver name for SQLite.
const DriverName = "sqlite3"
11 changes: 11 additions & 0 deletions driver_purego.go
@@ -0,0 +1,11 @@
//go:build purego
// +build purego

package sqlite

import (
_ "modernc.org/sqlite"
)

// DriverName is the default driver name for SQLite.
const DriverName = "sqlite"
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -8,4 +8,5 @@ require (
github.com/mattn/go-sqlite3 v1.14.12
github.com/stretchr/testify v1.7.0
gorm.io/gorm v1.23.4
modernc.org/sqlite v1.17.0
)
192 changes: 189 additions & 3 deletions go.sum

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions sqlite.go
Expand Up @@ -8,17 +8,13 @@ import (

"gorm.io/gorm/callbacks"

_ "github.com/mattn/go-sqlite3"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/logger"
"gorm.io/gorm/migrator"
"gorm.io/gorm/schema"
)

// DriverName is the default driver name for SQLite.
const DriverName = "sqlite3"

type Dialector struct {
DriverName string
DSN string
Expand Down
3 changes: 3 additions & 0 deletions sqlite_test.go → sqlite_cgo_test.go
@@ -1,3 +1,6 @@
//go:build !purego
// +build !purego

package sqlite

import (
Expand Down
120 changes: 120 additions & 0 deletions sqlite_purego_test.go
@@ -0,0 +1,120 @@
//go:build purego
// +build purego

package sqlite

import (
"database/sql"
"database/sql/driver"
"fmt"
"testing"

"gorm.io/gorm"
"modernc.org/sqlite"
)

func TestDialector(t *testing.T) {
// This is the DSN of the in-memory SQLite database for these tests.
const InMemoryDSN = "file:testdatabase?mode=memory&cache=shared"

// This is the custom SQLite driver name.
const CustomDriverName = "my_custom_driver"

// Register the custom SQlite3 driver.
sql.Register(CustomDriverName, &sqlite.Driver{})

sqlite.MustRegisterDeterministicScalarFunction(
"my_custom_function",
-1,
func(ctx *sqlite.FunctionContext, args []driver.Value) (driver.Value, error) {
return "my-result", nil
},
)

rows := []struct {
description string
dialector *Dialector
openSuccess bool
query string
querySuccess bool
}{
{
description: "Default driver",
dialector: &Dialector{
DSN: InMemoryDSN,
},
openSuccess: true,
query: "SELECT 1",
querySuccess: true,
},
{
description: "Explicit default driver",
dialector: &Dialector{
DriverName: DriverName,
DSN: InMemoryDSN,
},
openSuccess: true,
query: "SELECT 1",
querySuccess: true,
},
{
description: "Bad driver",
dialector: &Dialector{
DriverName: "not-a-real-driver",
DSN: InMemoryDSN,
},
openSuccess: false,
},
{
description: "Explicit default driver, custom function",
dialector: &Dialector{
DriverName: DriverName,
DSN: InMemoryDSN,
},
openSuccess: true,
query: "SELECT my_custom_function()",
querySuccess: true,
},
{
description: "Custom driver",
dialector: &Dialector{
DriverName: CustomDriverName,
DSN: InMemoryDSN,
},
openSuccess: true,
query: "SELECT 1",
querySuccess: true,
},
}
for rowIndex, row := range rows {
t.Run(fmt.Sprintf("%d/%s", rowIndex, row.description), func(t *testing.T) {
db, err := gorm.Open(row.dialector, &gorm.Config{})
if !row.openSuccess {
if err == nil {
t.Errorf("Expected Open to fail.")
}
return
}

if err != nil {
t.Errorf("Expected Open to succeed; got error: %v", err)
}
if db == nil {
t.Errorf("Expected db to be non-nil.")
}
if row.query != "" {
err = db.Exec(row.query).Error
if !row.querySuccess {
if err == nil {
t.Errorf("Expected query to fail.")
}
return
}

if err != nil {
t.Errorf("Expected query to succeed; got error: %v", err)
}
}
})
}
}