You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
773 B
Go
39 lines
773 B
Go
package database
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"net/url"
|
|
|
|
"github.com/doug-martin/goqu/v9"
|
|
goqusqlite3 "github.com/doug-martin/goqu/v9/dialect/sqlite3"
|
|
_ "modernc.org/sqlite"
|
|
)
|
|
|
|
func init() {
|
|
goqu.RegisterDialect("sqlite3", func() *goqu.SQLDialectOptions {
|
|
do := goqusqlite3.DialectOptions()
|
|
do.SupportsReturn = true
|
|
return do
|
|
}())
|
|
}
|
|
|
|
func NewSqliteDB(filename string) (*goqu.Database, error) {
|
|
pragmas := url.Values{
|
|
"_pragma": {
|
|
"journal_mode(WAL)",
|
|
"foreign_keys(1)",
|
|
"synchronous(NORMAL)",
|
|
"busy_timeout(5000)",
|
|
},
|
|
}.Encode()
|
|
dsn := fmt.Sprintf("%s?%s", filename, pragmas)
|
|
|
|
conn, err := sql.Open("sqlite", dsn)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to open database: %w", err)
|
|
}
|
|
|
|
return goqu.New("sqlite3", conn), nil
|
|
}
|