mirror of
https://github.com/hay-kot/homebox.git
synced 2025-07-25 20:10:27 +00:00
drop cgo requirement
This commit is contained in:
parent
22cd088657
commit
24ccb2b4e7
8 changed files with 110 additions and 72 deletions
40
backend/pkgs/cgofreesqlite/sqlite.go
Normal file
40
backend/pkgs/cgofreesqlite/sqlite.go
Normal file
|
@ -0,0 +1,40 @@
|
|||
// sqlite package provides a CGO free implementation of the sqlite3 driver. This wraps the
|
||||
// modernc.org/sqlite driver and adds the PRAGMA foreign_keys = ON; statement to the connection
|
||||
// initialization as well as registering the driver with the sql package as "sqlite3" for compatibility
|
||||
// with entgo.io
|
||||
//
|
||||
// NOTE: This does come with around a 30% performance hit compared to the CGO version of the driver.
|
||||
// however it greatly simplifies the build process and allows for cross compilation.
|
||||
package cgofreesqlite
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"database/sql/driver"
|
||||
|
||||
"modernc.org/sqlite"
|
||||
)
|
||||
|
||||
type CGOFreeSqliteDriver struct {
|
||||
*sqlite.Driver
|
||||
}
|
||||
|
||||
type sqlite3DriverConn interface {
|
||||
Exec(string, []driver.Value) (driver.Result, error)
|
||||
}
|
||||
|
||||
func (d CGOFreeSqliteDriver) Open(name string) (conn driver.Conn, err error) {
|
||||
conn, err = d.Driver.Open(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = conn.(sqlite3DriverConn).Exec("PRAGMA foreign_keys = ON;", nil)
|
||||
if err != nil {
|
||||
_ = conn.Close()
|
||||
return nil, err
|
||||
}
|
||||
return conn, err
|
||||
}
|
||||
|
||||
func init() {
|
||||
sql.Register("sqlite3", CGOFreeSqliteDriver{Driver: &sqlite.Driver{}})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue