go-xorm / xorm

Simple and Powerful ORM for Go, support mysql,postgres,tidb,sqlite3,mssql,oracle, Moved to https://gitea.com/xorm/xorm
BSD 3-Clause "New" or "Revised" License
6.67k stars 754 forks source link

Use custom functions/extensions with go-sqlite3 driver #1130

Open gunnsth opened 6 years ago

gunnsth commented 6 years ago

go-sqlite3 supports easy definition of custom functions: https://godoc.org/github.com/mattn/go-sqlite3#hdr-Go_SQlite3_Extensions

Currently cannot use the approach with xorm as:

regex = func(re, s string) (bool, error) {
    return regexp.MatchString(re, s)
}
sql.Register("sqlite3_with_go_func",
        &sqlite3.SQLiteDriver{
                ConnectHook: func(conn *sqlite3.SQLiteConn) error {
                    return conn.RegisterFunc("regexp", regex, true)
                },
        })
orm, err := xorm.NewEngine("sqlite3_with_go_func", vulnDbPath)
if err != nil {
    return err
}

Fails as xorm appears only to work with "sqlite3". However, I cannot use "sqlite3" as that gives the error:

panic: sql: Register called twice for driver sqlite3

Any good way around this?

g13013 commented 5 years ago

the library should implement https://golang.org/src/database/sql/sql.go?s=19056:19091#L649

also see https://github.com/golang/go/issues/27152