Added sqlite as database option
Some checks failed
the build failed

This commit is contained in:
kolaente 2017-12-05 11:49:19 +01:00
parent 111ffe687d
commit 9e6ddafe7e
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
3 changed files with 20 additions and 5 deletions

View File

@ -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]

View File

@ -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
}

View File

@ -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)