Browse Source

Added sqlite as database option

tags/v0.1
kolaente 1 year ago
parent
commit
9e6ddafe7e
Signed by: kolaente <konrad@kola-entertainments.de> GPG Key ID: F40E70337AB24C9B
3 changed files with 20 additions and 5 deletions
  1. 3
    0
      config.ini.sample
  2. 2
    0
      models/config.go
  3. 15
    5
      models/models.go

+ 3
- 0
config.ini.sample 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]

+ 2
- 0
models/config.go 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
}


+ 15
- 5
models/models.go 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)


Loading…
Cancel
Save