pikapika/go/main/database/properties/properties.go

128 lines
2.6 KiB
Go

package properties
import (
"errors"
"fmt"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"path"
"pikapika/main/utils"
"strconv"
"sync"
)
var mutex = sync.Mutex{}
var db *gorm.DB
func InitDBConnect(databaseDir string) {
mutex.Lock()
defer mutex.Unlock()
var err error
db, err = gorm.Open(sqlite.Open(path.Join(databaseDir, "properties.db")), utils.GormConfig)
if err != nil {
panic("failed to connect database")
}
db.AutoMigrate(&Property{})
}
type Property struct {
gorm.Model
K string `gorm:"index:uk_k,unique"`
V string
}
func LoadProperty(name string, defaultValue string) (string, error) {
mutex.Lock()
defer mutex.Unlock()
var property Property
err := db.First(&property, "k", name).Error
if err == nil {
return property.V, nil
}
if gorm.ErrRecordNotFound == err {
return defaultValue, nil
}
panic(errors.New("?"))
}
func SaveProperty(name string, value string) error {
mutex.Lock()
defer mutex.Unlock()
return db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "k"}},
DoUpdates: clause.AssignmentColumns([]string{"created_at", "updated_at", "v"}),
}).Create(&Property{
K: name,
V: value,
}).Error
}
func LoadBoolProperty(name string, defaultValue bool) (bool, error) {
stringValue, err := LoadProperty(name, strconv.FormatBool(defaultValue))
if err != nil {
return false, err
}
return strconv.ParseBool(stringValue)
}
func LoadIntProperty(name string, defaultValue int) (int, error) {
str, err := LoadProperty(name, fmt.Sprintf("%d", defaultValue))
if err != nil {
return 0, err
}
return strconv.Atoi(str)
}
func SaveIntProperty(name string, value int) error {
return SaveProperty(name, strconv.Itoa(value))
}
func SaveBoolProperty(name string, value bool) error {
return SaveProperty(name, strconv.FormatBool(value))
}
func SaveProxy(value string) error {
return SaveProperty("proxy", value)
}
func LoadProxy() (string, error) {
return LoadProperty("proxy", "")
}
func SaveUsername(value string) error {
return SaveProperty("username", value)
}
func LoadUsername() (string, error) {
return LoadProperty("username", "")
}
func SavePassword(value string) error {
return SaveProperty("password", value)
}
func LoadPassword() (string, error) {
return LoadProperty("password", "")
}
func SaveToken(value string) {
SaveProperty("token", value)
}
func LoadToken() (string, error) {
return LoadProperty("token", "")
}
func SaveTokenTime(value int64) {
SaveProperty("token_time", strconv.FormatInt(value, 10))
}
func LoadTokenTime() (int64, error) {
str, err := LoadProperty("token_time", "0")
if err != nil {
return 0, err
}
return strconv.ParseInt(str, 10, 64)
}