vikunja/pkg/models/models.go
kolaente e4c5bfc42b
Some checks failed
continuous-integration/drone/push Build is failing
Moved db handling to own package
2019-03-25 08:23:56 +01:00

99 lines
2.2 KiB
Go

// 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/>.
package models
import (
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/migration"
_ "github.com/go-sql-driver/mysql" // Because.
"github.com/go-xorm/xorm"
_ "github.com/mattn/go-sqlite3" // Because.
"github.com/spf13/viper"
)
var (
x *xorm.Engine
tables []interface{}
tablesWithPointer []interface{}
)
func init() {
tables = append(tables,
new(User),
new(List),
new(ListTask),
new(Team),
new(TeamMember),
new(TeamList),
new(TeamNamespace),
new(Namespace),
new(ListUser),
new(NamespaceUser),
new(ListTaskAssginee),
new(Label),
new(LabelTask),
)
tablesWithPointer = append(tables,
&User{},
&List{},
&ListTask{},
&Team{},
&TeamMember{},
&TeamList{},
&TeamNamespace{},
&Namespace{},
&ListUser{},
&NamespaceUser{},
&ListTaskAssginee{},
&Label{},
&LabelTask{},
)
}
// SetEngine sets the xorm.Engine
func SetEngine() (err error) {
x, err = db.CreateDBEngine()
if err != nil {
log.Log.Criticalf("Could not connect to db: %v", err.Error())
return
}
migration.Migrate(x)
return nil
}
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)
}