mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 09:30:56 +00:00
orm now can specify engine for mysql. add api SetMaxIdleConns/SetMaxOpenConns(go 1.2)
This commit is contained in:
parent
198d6320dd
commit
a616087cde
@ -12,17 +12,6 @@ type dbIndex struct {
|
|||||||
Sql string
|
Sql string
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDbAlias(name string) *alias {
|
|
||||||
if al, ok := dataBaseCache.get(name); ok {
|
|
||||||
return al
|
|
||||||
} else {
|
|
||||||
fmt.Println(fmt.Sprintf("unknown DataBase alias name %s", name))
|
|
||||||
os.Exit(2)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func getDbDropSql(al *alias) (sqls []string) {
|
func getDbDropSql(al *alias) (sqls []string) {
|
||||||
if len(modelCache.cache) == 0 {
|
if len(modelCache.cache) == 0 {
|
||||||
fmt.Println("no Model found, need register your model")
|
fmt.Println("no Model found, need register your model")
|
||||||
@ -180,7 +169,14 @@ func getDbCreateSql(al *alias) (sqls []string, tableIndexes map[string][]dbIndex
|
|||||||
sql += "\n)"
|
sql += "\n)"
|
||||||
|
|
||||||
if al.Driver == DR_MySQL {
|
if al.Driver == DR_MySQL {
|
||||||
sql += " ENGINE=INNODB"
|
var engine string
|
||||||
|
if mi.model != nil {
|
||||||
|
engine = getTableEngine(mi.addrField)
|
||||||
|
}
|
||||||
|
if engine == "" {
|
||||||
|
engine = al.Engine
|
||||||
|
}
|
||||||
|
sql += " ENGINE=" + engine
|
||||||
}
|
}
|
||||||
|
|
||||||
sql += ";"
|
sql += ";"
|
||||||
|
@ -4,11 +4,13 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"reflect"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const defaultMaxIdle = 30
|
const defaultMaxIdleConns = 30
|
||||||
|
const defaultMaxOpenConns = 50
|
||||||
|
|
||||||
type DriverType int
|
type DriverType int
|
||||||
|
|
||||||
@ -76,26 +78,36 @@ func (ac *_dbCache) getDefault() (al *alias) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type alias struct {
|
type alias struct {
|
||||||
Name string
|
Name string
|
||||||
Driver DriverType
|
Driver DriverType
|
||||||
DriverName string
|
DriverName string
|
||||||
DataSource string
|
DataSource string
|
||||||
MaxIdle int
|
MaxIdleConns int
|
||||||
DB *sql.DB
|
MaxOpenConns int
|
||||||
DbBaser dbBaser
|
DB *sql.DB
|
||||||
TZ *time.Location
|
DbBaser dbBaser
|
||||||
|
TZ *time.Location
|
||||||
|
Engine string
|
||||||
}
|
}
|
||||||
|
|
||||||
func RegisterDataBase(name, driverName, dataSource string, maxIdle int) {
|
// Setting the database connect params. Use the database driver self dataSource args.
|
||||||
if maxIdle <= 0 {
|
func RegisterDataBase(aliasName, driverName, dataSource string, params ...int) {
|
||||||
maxIdle = defaultMaxIdle
|
maxIdleConns := defaultMaxIdleConns
|
||||||
|
maxOpenConns := defaultMaxOpenConns
|
||||||
|
|
||||||
|
for i, v := range params {
|
||||||
|
switch i {
|
||||||
|
case 0:
|
||||||
|
maxIdleConns = v
|
||||||
|
case 1:
|
||||||
|
maxOpenConns = v
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
al := new(alias)
|
al := new(alias)
|
||||||
al.Name = name
|
al.Name = aliasName
|
||||||
al.DriverName = driverName
|
al.DriverName = driverName
|
||||||
al.DataSource = dataSource
|
al.DataSource = dataSource
|
||||||
al.MaxIdle = maxIdle
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
@ -109,19 +121,17 @@ func RegisterDataBase(name, driverName, dataSource string, maxIdle int) {
|
|||||||
goto end
|
goto end
|
||||||
}
|
}
|
||||||
|
|
||||||
if dataBaseCache.add(name, al) == false {
|
if dataBaseCache.add(aliasName, al) == false {
|
||||||
err = fmt.Errorf("db name `%s` already registered, cannot reuse", name)
|
err = fmt.Errorf("db name `%s` already registered, cannot reuse", aliasName)
|
||||||
goto end
|
goto end
|
||||||
}
|
}
|
||||||
|
|
||||||
al.DB, err = sql.Open(driverName, dataSource)
|
al.DB, err = sql.Open(driverName, dataSource)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = fmt.Errorf("register db `%s`, %s", name, err.Error())
|
err = fmt.Errorf("register db `%s`, %s", aliasName, err.Error())
|
||||||
goto end
|
goto end
|
||||||
}
|
}
|
||||||
|
|
||||||
al.DB.SetMaxIdleConns(al.MaxIdle)
|
|
||||||
|
|
||||||
// orm timezone system match database
|
// orm timezone system match database
|
||||||
// default use Local
|
// default use Local
|
||||||
al.TZ = time.Local
|
al.TZ = time.Local
|
||||||
@ -137,8 +147,22 @@ func RegisterDataBase(name, driverName, dataSource string, maxIdle int) {
|
|||||||
al.TZ = t.Location()
|
al.TZ = t.Location()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get default engine from current database
|
||||||
|
row = al.DB.QueryRow("SELECT ENGINE, TRANSACTIONS FROM information_schema.engines WHERE SUPPORT = 'DEFAULT'")
|
||||||
|
var engine string
|
||||||
|
var tx bool
|
||||||
|
row.Scan(&engine, &tx)
|
||||||
|
|
||||||
|
if engine != "" {
|
||||||
|
al.Engine = engine
|
||||||
|
} else {
|
||||||
|
engine = "INNODB"
|
||||||
|
}
|
||||||
|
|
||||||
case DR_Sqlite:
|
case DR_Sqlite:
|
||||||
al.TZ = time.UTC
|
al.TZ = time.UTC
|
||||||
|
|
||||||
case DR_Postgres:
|
case DR_Postgres:
|
||||||
row := al.DB.QueryRow("SELECT current_setting('TIMEZONE')")
|
row := al.DB.QueryRow("SELECT current_setting('TIMEZONE')")
|
||||||
var tz string
|
var tz string
|
||||||
@ -149,9 +173,12 @@ func RegisterDataBase(name, driverName, dataSource string, maxIdle int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SetMaxIdleConns(al.Name, maxIdleConns)
|
||||||
|
SetMaxOpenConns(al.Name, maxOpenConns)
|
||||||
|
|
||||||
err = al.DB.Ping()
|
err = al.DB.Ping()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = fmt.Errorf("register db `%s`, %s", name, err.Error())
|
err = fmt.Errorf("register db `%s`, %s", aliasName, err.Error())
|
||||||
goto end
|
goto end
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -162,6 +189,7 @@ end:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Register a database driver use specify driver name, this can be definition the driver is which database type.
|
||||||
func RegisterDriver(driverName string, typ DriverType) {
|
func RegisterDriver(driverName string, typ DriverType) {
|
||||||
if t, ok := drivers[driverName]; ok == false {
|
if t, ok := drivers[driverName]; ok == false {
|
||||||
drivers[driverName] = typ
|
drivers[driverName] = typ
|
||||||
@ -173,11 +201,29 @@ func RegisterDriver(driverName string, typ DriverType) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetDataBaseTZ(name string, tz *time.Location) {
|
// Change the database default used timezone
|
||||||
if al, ok := dataBaseCache.get(name); ok {
|
func SetDataBaseTZ(aliasName string, tz *time.Location) {
|
||||||
|
if al, ok := dataBaseCache.get(aliasName); ok {
|
||||||
al.TZ = tz
|
al.TZ = tz
|
||||||
} else {
|
} else {
|
||||||
fmt.Sprintf("DataBase name `%s` not registered\n", name)
|
fmt.Sprintf("DataBase name `%s` not registered\n", aliasName)
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Change the max idle conns for *sql.DB, use specify database alias name
|
||||||
|
func SetMaxIdleConns(aliasName string, maxIdleConns int) {
|
||||||
|
al := getDbAlias(aliasName)
|
||||||
|
al.MaxIdleConns = maxIdleConns
|
||||||
|
al.DB.SetMaxIdleConns(maxIdleConns)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Change the max open conns for *sql.DB, use specify database alias name
|
||||||
|
func SetMaxOpenConns(aliasName string, maxOpenConns int) {
|
||||||
|
al := getDbAlias(aliasName)
|
||||||
|
al.MaxOpenConns = maxOpenConns
|
||||||
|
// for tip go 1.2
|
||||||
|
if fun := reflect.ValueOf(al.DB).MethodByName("SetMaxOpenConns"); fun.IsValid() {
|
||||||
|
fun.Call([]reflect.Value{reflect.ValueOf(maxOpenConns)})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -6,6 +6,15 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func getDbAlias(name string) *alias {
|
||||||
|
if al, ok := dataBaseCache.get(name); ok {
|
||||||
|
return al
|
||||||
|
} else {
|
||||||
|
panic(fmt.Errorf("unknown DataBase alias name %s", name))
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func getExistPk(mi *modelInfo, ind reflect.Value) (column string, value interface{}, exist bool) {
|
func getExistPk(mi *modelInfo, ind reflect.Value) (column string, value interface{}, exist bool) {
|
||||||
fi := mi.fields.pk
|
fi := mi.fields.pk
|
||||||
|
|
||||||
|
@ -223,4 +223,10 @@ go test -v github.com/astaxie/beego/orm
|
|||||||
}
|
}
|
||||||
|
|
||||||
RegisterDataBase("default", DBARGS.Driver, DBARGS.Source, 20)
|
RegisterDataBase("default", DBARGS.Driver, DBARGS.Source, 20)
|
||||||
|
|
||||||
|
alias := getDbAlias("default")
|
||||||
|
if alias.Driver == DR_MySQL {
|
||||||
|
alias.Engine = "INNODB"
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,20 @@ func getTableName(val reflect.Value) string {
|
|||||||
return snakeString(ind.Type().Name())
|
return snakeString(ind.Type().Name())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getTableEngine(val reflect.Value) string {
|
||||||
|
fun := val.MethodByName("TableEngine")
|
||||||
|
if fun.IsValid() {
|
||||||
|
vals := fun.Call([]reflect.Value{})
|
||||||
|
if len(vals) > 0 {
|
||||||
|
val := vals[0]
|
||||||
|
if val.Kind() == reflect.String {
|
||||||
|
return val.String()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
func getTableIndex(val reflect.Value) [][]string {
|
func getTableIndex(val reflect.Value) [][]string {
|
||||||
fun := val.MethodByName("TableIndex")
|
fun := val.MethodByName("TableIndex")
|
||||||
if fun.IsValid() {
|
if fun.IsValid() {
|
||||||
|
Loading…
Reference in New Issue
Block a user