sqlite icon indicating copy to clipboard operation
sqlite copied to clipboard

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

Open clarkmcc opened this issue 1 year ago • 2 comments

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,
	})
}

clarkmcc avatar Apr 26 '24 19:04 clarkmcc

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

WinXaito avatar May 03 '24 14:05 WinXaito

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

clarkmcc avatar May 03 '24 16:05 clarkmcc