glebarez / sqlite

The pure-Go SQLite driver for GORM
MIT License
569 stars 37 forks source link

How to register custom functions? Can't import modernc lib #133

Open clarkmcc opened 3 months ago

clarkmcc commented 3 months ago

I'm trying to figure out how to add custom functions. When I register the custom functions directly to the modernc lib, then I get the following error because both the glebarez lib and the modernc call sql.Register using the same driver name.

 Register called twice for driver sqlite
import (
    "database/sql/driver"
    "fmt"
    "github.com/glebarez/sqlite"
    "github.com/tidwall/gjson"
    "gorm.io/gorm"
    "gorm.io/gorm/logger"
    sqlitecore "modernc.org/sqlite"
)

func init() {
        sqlitecore.RegisterFunction("FOOBAR", &sqlitecore.FunctionImpl{
        NArgs:         2,
        Deterministic: true,
        Scalar: ...,
    })
}

func open(dsn string) (*gorm.DB, error) {
    return gorm.Open(sqlite.Open(dsn), &gorm.Config{
        Logger: logger.Discard,
    })
}
WinXaito commented 2 months ago

I'm facing the same problem, I want to use the backup methods from modernc.org/sqlite, but I got the same error message.

clarkmcc commented 2 months ago

This PR solved my issue. If you add a replace directive to your go.mod file with my repo, it should work.