51 lines
920 B
Go
51 lines
920 B
Go
package db
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"strings"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
)
|
|
|
|
type SqliteConnector struct {
|
|
}
|
|
|
|
func (c SqliteConnector) connect(config *DBConfig) *Connection {
|
|
var params []string
|
|
|
|
if len(config.Journal) > 0 {
|
|
params = append(params, "_journal="+config.Journal)
|
|
}
|
|
if len(config.Locking) > 0 {
|
|
params = append(params, "_locking="+config.Locking)
|
|
}
|
|
if len(config.Mode) > 0 {
|
|
params = append(params, "mode="+config.Mode)
|
|
}
|
|
if config.Synchronous > 0 {
|
|
params = append(params, fmt.Sprintf("_sync=%d", config.Synchronous))
|
|
}
|
|
|
|
var dsn string
|
|
if len(config.Dsn) > 0 {
|
|
dsn = config.Dsn
|
|
} else {
|
|
dsn = fmt.Sprintf("file:%s?%s", config.File, strings.Join(params, "&"))
|
|
}
|
|
db, err := sql.Open(DriverSqlite3, dsn)
|
|
if err != nil {
|
|
panic(err.Error())
|
|
}
|
|
err = db.Ping()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return &Connection{
|
|
RxDB: db,
|
|
DB: db,
|
|
Config: config,
|
|
}
|
|
}
|