2023-09-18 23:57:18 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
_ "embed"
|
2023-09-19 23:29:55 +00:00
|
|
|
"fmt"
|
2023-09-18 23:57:18 +00:00
|
|
|
"path"
|
|
|
|
|
|
|
|
sqlite "github.com/mattn/go-sqlite3"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"reichard.io/bbank/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DBManager struct {
|
|
|
|
DB *sql.DB
|
|
|
|
Ctx context.Context
|
|
|
|
Queries *Queries
|
|
|
|
}
|
|
|
|
|
|
|
|
//go:embed schema.sql
|
|
|
|
var ddl string
|
|
|
|
|
|
|
|
func NewMgr(c *config.Config) *DBManager {
|
|
|
|
// Create Manager
|
|
|
|
dbm := &DBManager{
|
|
|
|
Ctx: context.Background(),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create Database
|
2023-09-19 23:29:55 +00:00
|
|
|
if c.DBType == "sqlite" {
|
2023-09-18 23:57:18 +00:00
|
|
|
sql.Register("sqlite3_custom", &sqlite.SQLiteDriver{
|
2023-09-19 23:29:55 +00:00
|
|
|
ConnectHook: connectHookSQLite,
|
2023-09-18 23:57:18 +00:00
|
|
|
})
|
|
|
|
|
2023-09-19 23:29:55 +00:00
|
|
|
dbLocation := path.Join(c.ConfigPath, fmt.Sprintf("%s.db", c.DBName))
|
2023-09-18 23:57:18 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
dbm.DB, err = sql.Open("sqlite3_custom", dbLocation)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Fatal("Unsupported Database")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create Tables
|
|
|
|
if _, err := dbm.DB.ExecContext(dbm.Ctx, ddl); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dbm.Queries = New(dbm.DB)
|
|
|
|
|
|
|
|
return dbm
|
|
|
|
}
|
2023-09-19 23:29:55 +00:00
|
|
|
|
|
|
|
func connectHookSQLite(conn *sqlite.SQLiteConn) error {
|
|
|
|
if err := conn.RegisterFunc("test_func", func() string {
|
|
|
|
return "FOOBAR"
|
|
|
|
}, false); err != nil {
|
|
|
|
log.Info("Error Registering Function")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|