vikunja-api/pkg/models/models.go

146 lines
3.7 KiB
Go
Raw Normal View History

2018-11-26 21:17:33 +01:00
// Vikunja is a todo-list application to facilitate your life.
// Copyright 2018 Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-06-10 11:11:41 +02:00
package models
import (
2019-01-25 12:40:54 +01:00
"code.vikunja.io/api/pkg/log"
"encoding/gob"
2018-06-10 11:11:41 +02:00
"fmt"
_ "github.com/go-sql-driver/mysql" // Because.
"github.com/go-xorm/core"
"github.com/go-xorm/xorm"
2018-09-13 19:53:03 +02:00
xrc "github.com/go-xorm/xorm-redis-cache"
2018-09-13 22:14:53 +02:00
_ "github.com/mattn/go-sqlite3" // Because.
"github.com/spf13/viper"
2018-06-10 11:11:41 +02:00
)
2018-06-10 14:14:10 +02:00
var (
x *xorm.Engine
2018-12-14 17:45:31 +01:00
tables []interface{}
tablesWithPointer []interface{}
2018-06-10 14:14:10 +02:00
)
2018-06-10 11:11:41 +02:00
func getEngine() (*xorm.Engine, error) {
// Use Mysql if set
if viper.GetString("database.type") == "mysql" {
connStr := fmt.Sprintf(
"%s:%s@tcp(%s)/%s?charset=utf8&parseTime=true",
viper.GetString("database.user"),
viper.GetString("database.password"),
viper.GetString("database.host"),
viper.GetString("database.database"))
e, err := xorm.NewEngine("mysql", connStr)
e.SetMaxOpenConns(viper.GetInt("database.openconnections"))
return e, err
2018-06-10 11:11:41 +02:00
}
// Otherwise use sqlite
path := viper.GetString("database.path")
2018-06-10 11:11:41 +02:00
if path == "" {
path = "./db.db"
}
return xorm.NewEngine("sqlite3", path)
}
2018-06-10 14:14:10 +02:00
func init() {
tables = append(tables,
new(User),
new(List),
2018-08-30 08:09:17 +02:00
new(ListTask),
new(Team),
new(TeamMember),
new(TeamList),
new(TeamNamespace),
new(Namespace),
new(ListUser),
2018-09-04 20:15:24 +02:00
new(NamespaceUser),
2018-12-29 15:29:50 +01:00
new(ListTaskAssginee),
2018-12-31 02:18:41 +01:00
new(Label),
new(LabelTask),
2018-06-10 14:14:10 +02:00
)
2018-12-14 17:45:31 +01:00
tablesWithPointer = append(tables,
&User{},
&List{},
&ListTask{},
&Team{},
&TeamMember{},
&TeamList{},
&TeamNamespace{},
&Namespace{},
&ListUser{},
&NamespaceUser{},
2018-12-29 15:29:50 +01:00
&ListTaskAssginee{},
2018-12-31 02:18:41 +01:00
&Label{},
&LabelTask{},
2018-12-14 17:45:31 +01:00
)
2018-06-10 14:14:10 +02:00
}
2018-06-10 11:11:41 +02:00
// SetEngine sets the xorm.Engine
func SetEngine() (err error) {
x, err = getEngine()
if err != nil {
2019-02-18 20:32:41 +01:00
return fmt.Errorf("failed to connect to database: %v", err)
2018-06-10 11:11:41 +02:00
}
// Cache
2018-09-13 19:53:03 +02:00
if viper.GetBool("cache.enabled") {
switch viper.GetString("cache.type") {
case "memory":
cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), viper.GetInt("cache.maxelementsize"))
x.SetDefaultCacher(cacher)
case "redis":
cacher := xrc.NewRedisCacher(viper.GetString("redis.host"), viper.GetString("redis.password"), xrc.DEFAULT_EXPIRATION, x.Logger())
2018-09-13 19:53:03 +02:00
x.SetDefaultCacher(cacher)
gob.Register(tables)
2018-12-14 17:45:31 +01:00
gob.Register(tablesWithPointer) // Need to register tables with pointer as well...
2018-09-13 19:53:03 +02:00
default:
2019-01-25 12:40:54 +01:00
log.Log.Info("Did not find a valid cache type. Caching disabled. Please refer to the docs for poosible cache types.")
2018-09-13 19:53:03 +02:00
}
}
2018-06-10 11:11:41 +02:00
x.SetMapper(core.GonicMapper{})
// Sync dat shit
2018-06-10 14:14:10 +02:00
if err = x.StoreEngine("InnoDB").Sync2(tables...); err != nil {
return fmt.Errorf("sync database struct error: %v", err)
}
2018-06-10 11:11:41 +02:00
2019-01-25 12:40:54 +01:00
x.ShowSQL(viper.GetString("log.database") != "off")
x.SetLogger(xorm.NewSimpleLogger(log.GetLogWriter("database")))
2018-06-10 11:11:41 +02:00
return nil
}
2018-11-09 11:30:17 +01:00
func getLimitFromPageIndex(page int) (limit, start int) {
// Get everything when page index is -1
if page < 0 {
return 0, 0
}
limit = viper.GetInt("service.pagecount")
start = limit * (page - 1)
return
}
// GetTotalCount returns the total amount of something
func GetTotalCount(counting interface{}) (count int64, err error) {
return x.Count(counting)
}