Added sqlite as database option
the build failed Details

This commit is contained in:
kolaente 2017-12-05 11:49:19 +01:00
父節點 111ffe687d
當前提交 9e6ddafe7e
簽署人: konrad
GPG Key ID: F40E70337AB24C9B
共有 3 個文件被更改,包括 20 次插入5 次删除

查看文件

@ -4,11 +4,14 @@ JWTSecret = blablaGEHEMIN§)!§
Interface = :8080
[Database]
Type = mysql
User = root
Password = supersecret
Host = 127.0.0.1
Database = library
ShowQueries = false
; When using sqlite, this is the path where to store the data
; Path = ./library.db
; First user to be created, on every startup the program checks if he exists, if not it creates it
[User]

查看文件

@ -8,10 +8,12 @@ import (
// ConfigStruct holds the config struct
type ConfigStruct struct {
Database struct {
Type string
Host string
User string
Password string
Database string
Path string
ShowQueries bool
}

查看文件

@ -2,7 +2,7 @@ package models
import (
"fmt"
//_ "github.com/mattn/go-sqlite3" // Because.
_ "github.com/mattn/go-sqlite3" // Because.
_ "github.com/go-sql-driver/mysql" // Because.
"github.com/go-xorm/core"
"github.com/go-xorm/xorm"
@ -11,10 +11,19 @@ import (
var x *xorm.Engine
func getEngine() (*xorm.Engine, error) {
connStr := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=true",
Config.Database.User, Config.Database.Password, Config.Database.Host, Config.Database.Database)
return xorm.NewEngine("mysql", connStr)
//return xorm.NewEngine("sqlite3", "./db.db")
// Use Mysql if set
if Config.Database.Type == "mysql" {
connStr := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=true",
Config.Database.User, Config.Database.Password, Config.Database.Host, Config.Database.Database)
return xorm.NewEngine("mysql", connStr)
} else {
// Otherwise use sqlite
path := Config.Database.Path
if path == "" {
path = "./db.db"
}
return xorm.NewEngine("sqlite3", path)
}
}
// SetEngine sets the xorm.Engine
@ -40,6 +49,7 @@ func SetEngine() (err error) {
x.Sync(&Quantity{})
x.Sync(&quantityRelation{})
x.Sync(&Item{})
x.Sync(&UserLog{})
x.ShowSQL(Config.Database.ShowQueries)