settings type
This commit is contained in:
parent
1d72b124d1
commit
683c0456e5
@ -7,13 +7,6 @@ import (
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const (
|
||||
SETTING_LAST_MTIME = "last_mtime"
|
||||
SETTING_LASTX = "last_x"
|
||||
SETTING_LASTY = "last_y"
|
||||
SETTING_LASTZ = "last_z"
|
||||
SETTING_INITIAL_RUN = "initial_run"
|
||||
)
|
||||
|
||||
/*
|
||||
sqlite perf: https://stackoverflow.com/questions/1711631/improve-insert-per-second-performance-of-sqlite?rq=1
|
||||
|
@ -25,6 +25,6 @@ func (db *Sqlite3Accessor) GetSetting(key string, defaultvalue string) (string,
|
||||
}
|
||||
|
||||
func (db *Sqlite3Accessor) SetSetting(key string, value string) error {
|
||||
_, err := db.db.Exec(setSettingQuery, value, key)
|
||||
_, err := db.db.Exec(setSettingQuery, key, value)
|
||||
return err
|
||||
}
|
||||
|
@ -96,5 +96,6 @@ select value from settings where key = ?
|
||||
`
|
||||
|
||||
const setSettingQuery = `
|
||||
update settings set value = ? where key = ?
|
||||
insert or replace into settings(key, value)
|
||||
values(?, ?)
|
||||
`
|
||||
|
82
server/settings/settings.go
Normal file
82
server/settings/settings.go
Normal file
@ -0,0 +1,82 @@
|
||||
package settings
|
||||
|
||||
import (
|
||||
"mapserver/mapobjectdb"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const (
|
||||
SETTING_LAST_MTIME = "last_mtime"
|
||||
SETTING_LASTX = "last_x"
|
||||
SETTING_LASTY = "last_y"
|
||||
SETTING_LASTZ = "last_z"
|
||||
SETTING_INITIAL_RUN = "initial_run"
|
||||
)
|
||||
|
||||
type Settings struct {
|
||||
db mapobjectdb.DBAccessor
|
||||
}
|
||||
|
||||
func New(db mapobjectdb.DBAccessor) *Settings{
|
||||
return &Settings{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Settings) GetString(key string, defaultValue string) string {
|
||||
str, err := this.db.GetSetting(key, defaultValue)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return str
|
||||
}
|
||||
|
||||
func (this *Settings) SetString(key string, value string) {
|
||||
err := this.db.SetSetting(key, value)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Settings) GetInt(key string, defaultValue int) int {
|
||||
str, err := this.db.GetSetting(key, strconv.Itoa(defaultValue))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
value, err := strconv.Atoi(str)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
func (this *Settings) SetInt(key string, value int) {
|
||||
err := this.db.SetSetting(key, strconv.Itoa(value))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Settings) GetInt64(key string, defaultValue int64) int64 {
|
||||
str, err := this.db.GetSetting(key, strconv.FormatInt(defaultValue, 10))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
value, err := strconv.ParseInt(str, 10, 64)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
func (this *Settings) SetInt64(key string, value int64) {
|
||||
err := this.db.SetSetting(key, strconv.FormatInt(value, 10))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
58
server/settings/settings_test.go
Normal file
58
server/settings/settings_test.go
Normal file
@ -0,0 +1,58 @@
|
||||
package settings
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"os"
|
||||
"io/ioutil"
|
||||
"mapserver/mapobjectdb/sqlite"
|
||||
)
|
||||
|
||||
func TestStrings(t *testing.T){
|
||||
tmpfile, err := ioutil.TempFile("", "TileDBTest.*.sqlite")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer os.Remove(tmpfile.Name())
|
||||
|
||||
db, err := sqlite.New(tmpfile.Name())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = db.Migrate()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
s := New(db)
|
||||
s.SetString("k", "v")
|
||||
str := s.GetString("k", "v2")
|
||||
if str != "v" {
|
||||
t.Fatal("getstring failed: " + str)
|
||||
}
|
||||
|
||||
if s.GetString("k2", "v3") != "v3" {
|
||||
t.Fatal("getstring with default failed")
|
||||
}
|
||||
|
||||
s.SetInt("i", 123)
|
||||
i := s.GetInt("i", 456)
|
||||
if i != 123 {
|
||||
t.Fatal("getint failed")
|
||||
}
|
||||
|
||||
if s.GetInt("i2", 111) != 111 {
|
||||
t.Fatal("getint with default failed")
|
||||
}
|
||||
|
||||
s.SetInt64("i", 1230000012300056)
|
||||
i2 := s.GetInt64("i", 456)
|
||||
if i2 != 1230000012300056 {
|
||||
t.Fatal("getint64 failed")
|
||||
}
|
||||
|
||||
if s.GetInt64("i2", 12300000123000564) != 12300000123000564 {
|
||||
t.Fatal("getint with default failed")
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user