fix(db): migrate to modernc sqlite driver

This commit is contained in:
hexxa 2023-02-11 12:37:48 +08:00 committed by Hexxa
parent dd3ef8f908
commit cc7840516d
10 changed files with 415 additions and 10 deletions

View file

@ -8,7 +8,7 @@ import (
"github.com/ihexxa/quickshare/src/db"
"github.com/ihexxa/quickshare/src/db/rdb/base"
_ "github.com/mattn/go-sqlite3"
_ "modernc.org/sqlite"
)
type SQLite struct {

View file

@ -0,0 +1,21 @@
package sqlitecgo
import (
"context"
"github.com/ihexxa/quickshare/src/db"
)
func (st *SQLiteStore) SetClientCfg(ctx context.Context, cfg *db.ClientConfig) error {
st.Lock()
defer st.Unlock()
return st.store.SetClientCfg(ctx, cfg)
}
func (st *SQLiteStore) GetCfg(ctx context.Context) (*db.SiteConfig, error) {
st.RLock()
defer st.RUnlock()
return st.store.GetCfg(ctx)
}

View file

@ -0,0 +1,49 @@
package sqlitecgo
import (
"context"
"github.com/ihexxa/quickshare/src/db"
)
func (st *SQLiteStore) GetFileInfo(ctx context.Context, itemPath string) (*db.FileInfo, error) {
st.RLock()
defer st.RUnlock()
return st.store.GetFileInfo(ctx, itemPath)
}
func (st *SQLiteStore) ListFileInfos(ctx context.Context, itemPaths []string) (map[string]*db.FileInfo, error) {
st.RLock()
defer st.RUnlock()
return st.store.ListFileInfos(ctx, itemPaths)
}
func (st *SQLiteStore) AddFileInfo(ctx context.Context, infoId, userId uint64, itemPath string, info *db.FileInfo) error {
st.Lock()
defer st.Unlock()
return st.store.AddFileInfo(ctx, infoId, userId, itemPath, info)
}
func (st *SQLiteStore) SetSha1(ctx context.Context, itemPath, sign string) error {
st.Lock()
defer st.Unlock()
return st.store.SetSha1(ctx, itemPath, sign)
}
func (st *SQLiteStore) DelFileInfo(ctx context.Context, userID uint64, itemPath string) error {
st.Lock()
defer st.Unlock()
return st.store.DelFileInfo(ctx, userID, itemPath)
}
func (st *SQLiteStore) MoveFileInfo(ctx context.Context, userId uint64, oldPath, newPath string, isDir bool) error {
st.Lock()
defer st.Unlock()
return st.store.MoveFileInfo(ctx, userId, oldPath, newPath, isDir)
}

View file

@ -0,0 +1,40 @@
package sqlitecgo
import (
"context"
)
func (st *SQLiteStore) IsSharing(ctx context.Context, dirPath string) (bool, error) {
st.RLock()
defer st.RUnlock()
return st.store.IsSharing(ctx, dirPath)
}
func (st *SQLiteStore) GetSharingDir(ctx context.Context, hashID string) (string, error) {
st.RLock()
defer st.RUnlock()
return st.store.GetSharingDir(ctx, hashID)
}
func (st *SQLiteStore) AddSharing(ctx context.Context, infoId, userId uint64, dirPath string) error {
st.Lock()
defer st.Unlock()
return st.store.AddSharing(ctx, infoId, userId, dirPath)
}
func (st *SQLiteStore) DelSharing(ctx context.Context, userId uint64, dirPath string) error {
st.Lock()
defer st.Unlock()
return st.store.DelSharing(ctx, userId, dirPath)
}
func (st *SQLiteStore) ListSharingsByLocation(ctx context.Context, location string) (map[string]string, error) {
st.RLock()
defer st.RUnlock()
return st.store.ListSharingsByLocation(ctx, location)
}

View file

@ -0,0 +1,49 @@
package sqlitecgo
import (
"context"
"github.com/ihexxa/quickshare/src/db"
)
func (st *SQLiteStore) AddUploadInfos(ctx context.Context, uploadId, userId uint64, tmpPath, filePath string, info *db.FileInfo) error {
st.Lock()
defer st.Unlock()
return st.store.AddUploadInfos(ctx, uploadId, userId, tmpPath, filePath, info)
}
func (st *SQLiteStore) DelUploadingInfos(ctx context.Context, userId uint64, realPath string) error {
st.Lock()
defer st.Unlock()
return st.store.DelUploadingInfos(ctx, userId, realPath)
}
func (st *SQLiteStore) MoveUploadingInfos(ctx context.Context, infoId, userId uint64, uploadPath, itemPath string) error {
st.Lock()
defer st.Unlock()
return st.store.MoveUploadingInfos(ctx, infoId, userId, uploadPath, itemPath)
}
func (st *SQLiteStore) SetUploadInfo(ctx context.Context, userId uint64, filePath string, newUploaded int64) error {
st.Lock()
defer st.Unlock()
return st.store.SetUploadInfo(ctx, userId, filePath, newUploaded)
}
func (st *SQLiteStore) GetUploadInfo(ctx context.Context, userId uint64, filePath string) (string, int64, int64, error) {
st.RLock()
defer st.RUnlock()
return st.store.GetUploadInfo(ctx, userId, filePath)
}
func (st *SQLiteStore) ListUploadInfos(ctx context.Context, userId uint64) ([]*db.UploadInfo, error) {
st.RLock()
defer st.RUnlock()
return st.store.ListUploadInfos(ctx, userId)
}

View file

@ -0,0 +1,84 @@
package sqlitecgo
import (
"context"
"database/sql"
"sync"
"github.com/ihexxa/quickshare/src/db"
"github.com/ihexxa/quickshare/src/db/rdb/base"
_ "modernc.org/sqlite"
)
type SQLite struct {
db.IDB
dbPath string
}
func NewSQLite(dbPath string) (*SQLite, error) {
db, err := sql.Open("sqlite", dbPath)
if err != nil {
return nil, err
}
return &SQLite{
IDB: db,
dbPath: dbPath,
}, nil
}
type SQLiteStore struct {
store *base.BaseStore
mtx *sync.RWMutex
}
func NewSQLiteStore(db db.IDB) (*SQLiteStore, error) {
return &SQLiteStore{
store: base.NewBaseStore(db),
mtx: &sync.RWMutex{},
}, nil
}
func (st *SQLiteStore) Close() error {
return st.store.Close()
}
func (st *SQLiteStore) Lock() {
st.mtx.Lock()
}
func (st *SQLiteStore) Unlock() {
st.mtx.Unlock()
}
func (st *SQLiteStore) RLock() {
st.mtx.RLock()
}
func (st *SQLiteStore) RUnlock() {
st.mtx.RUnlock()
}
func (st *SQLiteStore) IsInited() bool {
// always try to init the db
return false
}
func (st *SQLiteStore) Init(ctx context.Context, rootName, rootPwd string, cfg *db.SiteConfig) error {
st.Lock()
defer st.Unlock()
return st.store.Init(ctx, rootName, rootPwd, cfg)
}
func (st *SQLiteStore) InitUserTable(ctx context.Context, tx *sql.Tx, rootName, rootPwd string) error {
return st.store.InitUserTable(ctx, tx, rootName, rootPwd)
}
func (st *SQLiteStore) InitFileTables(ctx context.Context, tx *sql.Tx) error {
return st.store.InitFileTables(ctx, tx)
}
func (st *SQLiteStore) InitConfigTable(ctx context.Context, tx *sql.Tx, cfg *db.SiteConfig) error {
return st.store.InitConfigTable(ctx, tx, cfg)
}

View file

@ -0,0 +1,100 @@
package sqlitecgo
import (
"context"
"github.com/ihexxa/quickshare/src/db"
)
func (st *SQLiteStore) AddUser(ctx context.Context, user *db.User) error {
st.Lock()
defer st.Unlock()
return st.store.AddUser(ctx, user)
}
func (st *SQLiteStore) DelUser(ctx context.Context, id uint64) error {
st.Lock()
defer st.Unlock()
return st.store.DelUser(ctx, id)
}
func (st *SQLiteStore) GetUser(ctx context.Context, id uint64) (*db.User, error) {
st.RLock()
defer st.RUnlock()
return st.store.GetUser(ctx, id)
}
func (st *SQLiteStore) GetUserByName(ctx context.Context, name string) (*db.User, error) {
st.RLock()
defer st.RUnlock()
return st.store.GetUserByName(ctx, name)
}
func (st *SQLiteStore) SetPwd(ctx context.Context, id uint64, pwd string) error {
st.Lock()
defer st.Unlock()
return st.store.SetPwd(ctx, id, pwd)
}
// role + quota
func (st *SQLiteStore) SetInfo(ctx context.Context, id uint64, user *db.User) error {
st.Lock()
defer st.Unlock()
return st.store.SetInfo(ctx, id, user)
}
func (st *SQLiteStore) SetPreferences(ctx context.Context, id uint64, prefers *db.Preferences) error {
st.Lock()
defer st.Unlock()
return st.store.SetPreferences(ctx, id, prefers)
}
func (st *SQLiteStore) SetUsed(ctx context.Context, id uint64, incr bool, capacity int64) error {
st.Lock()
defer st.Unlock()
return st.store.SetUsed(ctx, id, incr, capacity)
}
func (st *SQLiteStore) ResetUsed(ctx context.Context, id uint64, used int64) error {
st.Lock()
defer st.Unlock()
return st.store.ResetUsed(ctx, id, used)
}
func (st *SQLiteStore) ListUsers(ctx context.Context) ([]*db.User, error) {
st.RLock()
defer st.RUnlock()
return st.store.ListUsers(ctx)
}
func (st *SQLiteStore) ListUserIDs(ctx context.Context) (map[string]string, error) {
st.RLock()
defer st.RUnlock()
return st.store.ListUserIDs(ctx)
}
func (st *SQLiteStore) AddRole(role string) error {
// TODO: implement this after adding grant/revoke
panic("not implemented")
}
func (st *SQLiteStore) DelRole(role string) error {
// TODO: implement this after adding grant/revoke
panic("not implemented")
}
func (st *SQLiteStore) ListRoles() (map[string]bool, error) {
// TODO: implement this after adding grant/revoke
panic("not implemented")
}

View file

@ -5,6 +5,7 @@ import "errors"
var ErrLocked = errors.New("already locked")
var ErrNoLock = errors.New("no lock to unlock")
// Deprecated: no longer supported
type IKVStore interface {
AddNamespace(nsName string) error
DelNamespace(nsName string) error