feat(rdb): add sqlite as a dependency

This commit is contained in:
hexxa 2022-08-30 15:54:46 +08:00 committed by Hexxa
parent cb478ca266
commit f95a611204
6 changed files with 90 additions and 19 deletions

25
src/db/rdb/interface.go Normal file
View file

@ -0,0 +1,25 @@
package rdb
import (
"context"
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
// TODO: expose more APIs if needed
type IDB interface {
BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
Close() error
PingContext(ctx context.Context) error
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
// Conn(ctx context.Context) (*Conn, error)
// Driver() driver.Driver
// SetConnMaxIdleTime(d time.Duration)
// SetConnMaxLifetime(d time.Duration)
// SetMaxIdleConns(n int)
// SetMaxOpenConns(n int)
// Stats() DBStats
}

View file

@ -0,0 +1,25 @@
package sqlite
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
"github.com/ihexxa/quickshare/src/db/rdb"
)
type SQLite struct {
rdb.IDB
dbPath string
}
func NewSQLite(dbPath string) (*SQLite, error) {
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
return nil, err
}
return &SQLite{
IDB: db,
dbPath: dbPath,
}, nil
}

View file

@ -8,6 +8,7 @@ import (
"github.com/ihexxa/quickshare/src/cryptoutil"
"github.com/ihexxa/quickshare/src/db/boltstore"
"github.com/ihexxa/quickshare/src/db/fileinfostore"
"github.com/ihexxa/quickshare/src/db/rdb"
"github.com/ihexxa/quickshare/src/db/sitestore"
"github.com/ihexxa/quickshare/src/db/userstore"
"github.com/ihexxa/quickshare/src/fs"
@ -40,6 +41,7 @@ type Deps struct {
boltStore *boltstore.BoltStore
cron cron.ICron
fileIndex fileindex.IFileIndex
db rdb.IDB
}
func NewDeps(cfg gocfg.ICfg) *Deps {
@ -146,6 +148,14 @@ func (deps *Deps) FileIndex() fileindex.IFileIndex {
return deps.fileIndex
}
func (deps *Deps) SetIFileIndex(index fileindex.IFileIndex) {
func (deps *Deps) SetFileIndex(index fileindex.IFileIndex) {
deps.fileIndex = index
}
func (deps *Deps) DB() rdb.IDB {
return deps.db
}
func (deps *Deps) SetDB(db rdb.IDB) {
deps.db = db
}

View file

@ -242,7 +242,7 @@ func initDeps(cfg gocfg.ICfg) *depidx.Deps {
logger.Infof("failed to reindex file index: %s", err)
}
}
deps.SetIFileIndex(fileIndex)
deps.SetFileIndex(fileIndex)
return deps
}