harbor/dao/base.go

105 lines
2.5 KiB
Go
Raw Normal View History

2016-02-01 12:59:10 +01:00
/*
2016-02-23 05:42:22 +01:00
Copyright (c) 2016 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
2016-02-01 12:59:10 +01:00
*/
2016-02-26 11:54:14 +01:00
2016-02-01 12:59:10 +01:00
package dao
import (
"fmt"
2016-02-01 12:59:10 +01:00
"os"
2016-09-13 11:41:32 +02:00
"strings"
2016-05-24 03:41:34 +02:00
"sync"
2016-02-01 12:59:10 +01:00
"github.com/astaxie/beego/orm"
"github.com/vmware/harbor/utils/log"
2016-02-01 12:59:10 +01:00
)
// NonExistUserID : if a user does not exist, the ID of the user will be 0.
2016-02-26 04:26:54 +01:00
const NonExistUserID = 0
2016-02-01 12:59:10 +01:00
2016-09-13 11:41:32 +02:00
// Database is an interface of different databases
type Database interface {
// Name returns the name of database
Name() string
// String returns the details of database
String() string
// Register registers the database which will be used
Register(alias ...string) error
}
// InitDatabase initializes the database
func InitDatabase() {
database, err := getDatabase()
2016-02-01 12:59:10 +01:00
if err != nil {
2016-09-13 11:41:32 +02:00
panic(err)
2016-02-01 12:59:10 +01:00
}
2016-09-13 11:41:32 +02:00
log.Infof("initializing database: %s", database.String())
if err := database.Register(); err != nil {
panic(err)
}
2016-02-01 12:59:10 +01:00
}
2016-09-13 11:41:32 +02:00
func getDatabase() (db Database, err error) {
switch strings.ToLower(os.Getenv("DATABASE")) {
case "", "mysql":
host, port, usr, pwd, database := getMySQLConnInfo()
db = NewMySQL(host, port, usr, pwd, database)
case "sqlite":
file := getSQLiteConnInfo()
db = NewSQLite(file)
default:
err = fmt.Errorf("invalid database: %s", os.Getenv("DATABASE"))
2016-02-01 12:59:10 +01:00
}
2016-09-13 11:41:32 +02:00
return
}
// TODO read from config
func getMySQLConnInfo() (host, port, username, password, database string) {
host = os.Getenv("MYSQL_HOST")
port = os.Getenv("MYSQL_PORT")
username = os.Getenv("MYSQL_USR")
password = os.Getenv("MYSQL_PWD")
database = os.Getenv("MYSQL_DATABASE")
if len(database) == 0 {
database = "registry"
}
return
}
// TODO read from config
func getSQLiteConnInfo() string {
file := os.Getenv("SQLITE_FILE")
if len(file) == 0 {
file = "registry.db"
2016-02-23 13:49:59 +01:00
}
2016-09-13 11:41:32 +02:00
return file
2016-02-01 12:59:10 +01:00
}
2016-05-20 10:36:10 +02:00
var globalOrm orm.Ormer
2016-05-24 03:41:34 +02:00
var once sync.Once
2016-05-20 10:36:10 +02:00
2016-05-20 10:54:24 +02:00
// GetOrmer :set ormer singleton
2016-05-20 10:36:10 +02:00
func GetOrmer() orm.Ormer {
2016-05-24 03:41:34 +02:00
once.Do(func() {
2016-05-20 10:36:10 +02:00
globalOrm = orm.NewOrm()
2016-05-24 03:41:34 +02:00
})
2016-05-20 10:36:10 +02:00
return globalOrm
}
func paginateForRawSQL(sql string, limit, offset int64) string {
return fmt.Sprintf("%s limit %d offset %d", sql, limit, offset)
}