1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-23 01:20:54 +00:00
Beego/orm/db_alias.go

184 lines
3.3 KiB
Go
Raw Normal View History

2013-07-30 12:32:38 +00:00
package orm
import (
"database/sql"
"fmt"
"os"
"sync"
"time"
2013-07-30 12:32:38 +00:00
)
const defaultMaxIdle = 30
2013-08-07 11:11:44 +00:00
type DriverType int
2013-07-30 12:32:38 +00:00
const (
2013-08-07 11:11:44 +00:00
_ DriverType = iota
2013-07-30 12:32:38 +00:00
DR_MySQL
DR_Sqlite
DR_Oracle
DR_Postgres
)
2013-08-07 11:11:44 +00:00
type driver string
func (d driver) Type() DriverType {
a, _ := dataBaseCache.get(string(d))
return a.Driver
}
func (d driver) Name() string {
return string(d)
}
var _ Driver = new(driver)
2013-07-30 12:32:38 +00:00
var (
dataBaseCache = &_dbCache{cache: make(map[string]*alias)}
2013-08-07 11:11:44 +00:00
drivers = map[string]DriverType{
2013-08-01 01:23:32 +00:00
"mysql": DR_MySQL,
"postgres": DR_Postgres,
"sqlite3": DR_Sqlite,
}
2013-08-07 11:11:44 +00:00
dbBasers = map[DriverType]dbBaser{
2013-07-30 12:32:38 +00:00
DR_MySQL: newdbBaseMysql(),
DR_Sqlite: newdbBaseSqlite(),
DR_Oracle: newdbBaseMysql(),
DR_Postgres: newdbBasePostgres(),
}
)
type _dbCache struct {
mux sync.RWMutex
cache map[string]*alias
}
func (ac *_dbCache) add(name string, al *alias) (added bool) {
ac.mux.Lock()
defer ac.mux.Unlock()
if _, ok := ac.cache[name]; ok == false {
ac.cache[name] = al
added = true
}
return
}
func (ac *_dbCache) get(name string) (al *alias, ok bool) {
ac.mux.RLock()
defer ac.mux.RUnlock()
al, ok = ac.cache[name]
return
}
func (ac *_dbCache) getDefault() (al *alias) {
al, _ = ac.get("default")
return
}
type alias struct {
Name string
2013-08-07 11:11:44 +00:00
Driver DriverType
2013-07-30 12:32:38 +00:00
DriverName string
DataSource string
MaxIdle int
DB *sql.DB
DbBaser dbBaser
TZ *time.Location
2013-07-30 12:32:38 +00:00
}
func RegisterDataBase(name, driverName, dataSource string, maxIdle int) {
if maxIdle <= 0 {
maxIdle = defaultMaxIdle
}
al := new(alias)
al.Name = name
al.DriverName = driverName
al.DataSource = dataSource
al.MaxIdle = maxIdle
var (
err error
)
if dr, ok := drivers[driverName]; ok {
al.DbBaser = dbBasers[dr]
2013-08-07 11:11:44 +00:00
al.Driver = dr
2013-07-30 12:32:38 +00:00
} else {
err = fmt.Errorf("driver name `%s` have not registered", driverName)
goto end
}
if dataBaseCache.add(name, al) == false {
err = fmt.Errorf("db name `%s` already registered, cannot reuse", name)
goto end
}
al.DB, err = sql.Open(driverName, dataSource)
if err != nil {
err = fmt.Errorf("register db `%s`, %s", name, err.Error())
goto end
}
2013-08-12 05:24:45 +00:00
al.DB.SetMaxIdleConns(al.MaxIdle)
// orm timezone system match database
// default use Local
al.TZ = time.Local
switch al.Driver {
case DR_MySQL:
row := al.DB.QueryRow("SELECT @@session.time_zone")
var tz string
row.Scan(&tz)
if tz != "SYSTEM" {
t, err := time.Parse("-07:00", tz)
if err == nil {
al.TZ = t.Location()
}
}
case DR_Sqlite:
al.TZ = time.UTC
case DR_Postgres:
row := al.DB.QueryRow("SELECT current_setting('TIMEZONE')")
var tz string
row.Scan(&tz)
loc, err := time.LoadLocation(tz)
if err == nil {
al.TZ = loc
}
}
2013-07-30 12:32:38 +00:00
err = al.DB.Ping()
if err != nil {
err = fmt.Errorf("register db `%s`, %s", name, err.Error())
goto end
}
end:
if err != nil {
fmt.Println(err.Error())
os.Exit(2)
}
}
func RegisterDriver(driverName string, typ DriverType) {
if t, ok := drivers[driverName]; ok == false {
drivers[driverName] = typ
2013-07-30 12:32:38 +00:00
} else {
2013-07-31 14:11:22 +00:00
if t != typ {
2013-08-13 11:33:43 +00:00
fmt.Sprintf("driverName `%s` db driver already registered and is other type\n", driverName)
2013-07-31 14:11:22 +00:00
os.Exit(2)
}
2013-07-30 12:32:38 +00:00
}
}
func SetDataBaseTZ(name string, tz *time.Location) {
if al, ok := dataBaseCache.get(name); ok {
al.TZ = tz
} else {
2013-08-13 11:33:43 +00:00
fmt.Sprintf("DataBase name `%s` not registered\n", name)
os.Exit(2)
}
}