databse.go 1.2 KB
Newer Older
Y
Your Name 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
package main

import (
	"fmt"
	"github.com/eolinker/goku-api-gateway/common/database"
	"github.com/eolinker/goku-api-gateway/common/pdao"
	console_sqlite3 "github.com/eolinker/goku-api-gateway/server/dao/console-sqlite3"
	"github.com/eolinker/goku-api-gateway/server/entity"
)



//ClusterDatabaseConfig 集群数据库配置
type ClusterDatabaseConfig entity.ClusterDB

//GetDriver 获取驱动类型
func (c *ClusterDatabaseConfig) GetDriver() string {
	return c.Driver
}

//GetSource 获取连接字符串
func (c *ClusterDatabaseConfig) GetSource() string {

	switch c.Driver {
	case database.MysqlDriver:
		{
			return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8", c.UserName, c.Password, c.Host, c.Port, c.Database)
		}
	case database.Sqlite3Driver:
		{
			return c.Path
		}
	default:
		{
			return ""
		}
	}

}

//InitDatabase 初始化数据库
func InitDatabase() {
	console_sqlite3.DoRegister()

	def, err := getDefaultDatabase()
	if err != nil {
		panic(err)
	}
	c := ClusterDatabaseConfig(*def)
	db,e := database.InitConnection(&c)
	if e != nil {
		panic(e)
	}
	err =pdao.Build(c.GetDriver(),db)
	if err!=nil{
		panic(err)
	}

	err =pdao.Check()
	if err!=nil{
		panic(err)
	}
}