sqlite
sqlite copied to clipboard
How to register custom functions? Can't import modernc lib
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,
})
}
I'm facing the same problem, I want to use the backup methods from modernc.org/sqlite, but I got the same error message.
This PR solved my issue. If you add a replace directive to your go.mod file with my repo, it should work.