2019-02-02 16:18:25 +08:00
|
|
|
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2017-12-29 16:03:30 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2017-12-31 18:19:58 +08:00
|
|
|
|
2017-12-14 17:32:51 +08:00
|
|
|
package gdb
|
|
|
|
|
|
|
|
import (
|
2019-06-19 09:06:52 +08:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
2019-09-24 23:41:18 +08:00
|
|
|
"time"
|
2019-06-29 10:45:50 +08:00
|
|
|
|
2019-08-26 19:39:04 +08:00
|
|
|
"github.com/gogf/gf/os/glog"
|
2017-12-14 17:32:51 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-02-10 20:37:53 +08:00
|
|
|
DEFAULT_GROUP_NAME = "default" // Default group name.
|
2017-12-14 17:32:51 +08:00
|
|
|
)
|
|
|
|
|
2020-02-10 20:37:53 +08:00
|
|
|
// Config is the configuration management object.
|
2019-06-19 09:06:52 +08:00
|
|
|
type Config map[string]ConfigGroup
|
2017-12-14 17:32:51 +08:00
|
|
|
|
2020-02-10 20:37:53 +08:00
|
|
|
// ConfigGroup is a slice of configuration node for specified named group.
|
2017-12-14 17:32:51 +08:00
|
|
|
type ConfigGroup []ConfigNode
|
|
|
|
|
2020-02-10 20:37:53 +08:00
|
|
|
// ConfigNode is configuration for one node.
|
2019-06-19 09:06:52 +08:00
|
|
|
type ConfigNode struct {
|
2020-02-10 20:37:53 +08:00
|
|
|
Host string // Host of server, ip or domain like: 127.0.0.1, localhost
|
|
|
|
Port string // Port, it's commonly 3306.
|
|
|
|
User string // Authentication username.
|
|
|
|
Pass string // Authentication password.
|
|
|
|
Name string // Default used database name.
|
|
|
|
Type string // Database type: mysql, sqlite, mssql, pgsql, oracle.
|
|
|
|
Role string // (Optional, "master" in default) Node role, used for master-slave mode: master, slave.
|
|
|
|
Debug bool // (Optional) Debug mode enables debug information logging and output.
|
|
|
|
Prefix string // (Optional) Table prefix.
|
|
|
|
Weight int // (Optional) Weight for load balance calculating, it's useless if there's just one node.
|
|
|
|
Charset string // (Optional, "utf8mb4" in default) Custom charset when operating on database.
|
|
|
|
LinkInfo string // (Optional) Custom link information, when it is used, configuration Host/Port/User/Pass/Name are ignored.
|
|
|
|
MaxIdleConnCount int // (Optional) Max idle connection configuration for underlying connection pool.
|
|
|
|
MaxOpenConnCount int // (Optional) Max open connection configuration for underlying connection pool.
|
|
|
|
MaxConnLifetime time.Duration // (Optional) Max connection TTL configuration for underlying connection pool.
|
|
|
|
}
|
|
|
|
|
|
|
|
// configs is internal used configuration object.
|
2019-04-02 14:37:46 +08:00
|
|
|
var configs struct {
|
2020-02-10 20:37:53 +08:00
|
|
|
sync.RWMutex
|
|
|
|
config Config // All configurations.
|
|
|
|
group string // Default configuration group.
|
2019-04-02 14:37:46 +08:00
|
|
|
}
|
|
|
|
|
2017-12-14 17:32:51 +08:00
|
|
|
func init() {
|
2019-06-19 09:06:52 +08:00
|
|
|
configs.config = make(Config)
|
2020-02-10 20:37:53 +08:00
|
|
|
configs.group = DEFAULT_GROUP_NAME
|
2017-12-14 17:32:51 +08:00
|
|
|
}
|
|
|
|
|
2020-02-10 20:37:53 +08:00
|
|
|
// SetConfig sets the global configuration for package.
|
|
|
|
// It will overwrite the old configuration of package.
|
2019-06-19 09:06:52 +08:00
|
|
|
func SetConfig(config Config) {
|
|
|
|
defer instances.Clear()
|
|
|
|
configs.Lock()
|
|
|
|
defer configs.Unlock()
|
|
|
|
configs.config = config
|
2017-12-14 17:32:51 +08:00
|
|
|
}
|
|
|
|
|
2020-02-10 20:37:53 +08:00
|
|
|
// SetConfigGroup sets the configuration for given group.
|
2019-09-02 20:35:29 +08:00
|
|
|
func SetConfigGroup(group string, nodes ConfigGroup) {
|
2019-06-19 09:06:52 +08:00
|
|
|
defer instances.Clear()
|
|
|
|
configs.Lock()
|
|
|
|
defer configs.Unlock()
|
|
|
|
configs.config[group] = nodes
|
2017-12-14 17:32:51 +08:00
|
|
|
}
|
|
|
|
|
2020-02-10 20:37:53 +08:00
|
|
|
// AddConfigNode adds one node configuration to configuration of given group.
|
2019-06-19 09:06:52 +08:00
|
|
|
func AddConfigNode(group string, node ConfigNode) {
|
|
|
|
defer instances.Clear()
|
|
|
|
configs.Lock()
|
|
|
|
defer configs.Unlock()
|
|
|
|
configs.config[group] = append(configs.config[group], node)
|
2017-12-14 17:32:51 +08:00
|
|
|
}
|
|
|
|
|
2020-02-10 20:37:53 +08:00
|
|
|
// AddDefaultConfigNode adds one node configuration to configuration of default group.
|
2019-06-19 09:06:52 +08:00
|
|
|
func AddDefaultConfigNode(node ConfigNode) {
|
|
|
|
AddConfigNode(DEFAULT_GROUP_NAME, node)
|
2017-12-14 17:32:51 +08:00
|
|
|
}
|
|
|
|
|
2020-02-10 20:37:53 +08:00
|
|
|
// AddDefaultConfigGroup adds multiple node configurations to configuration of default group.
|
2019-06-19 09:06:52 +08:00
|
|
|
func AddDefaultConfigGroup(nodes ConfigGroup) {
|
2019-09-02 20:35:29 +08:00
|
|
|
SetConfigGroup(DEFAULT_GROUP_NAME, nodes)
|
2017-12-14 17:32:51 +08:00
|
|
|
}
|
|
|
|
|
2020-02-10 20:37:53 +08:00
|
|
|
// GetConfig retrieves and returns the configuration of given group.
|
2019-06-19 09:06:52 +08:00
|
|
|
func GetConfig(group string) ConfigGroup {
|
|
|
|
configs.RLock()
|
|
|
|
defer configs.RUnlock()
|
|
|
|
return configs.config[group]
|
2018-12-19 18:35:44 +08:00
|
|
|
}
|
|
|
|
|
2020-02-10 20:37:53 +08:00
|
|
|
// SetDefaultGroup sets the group name for default configuration.
|
2019-06-19 09:06:52 +08:00
|
|
|
func SetDefaultGroup(name string) {
|
|
|
|
defer instances.Clear()
|
|
|
|
configs.Lock()
|
|
|
|
defer configs.Unlock()
|
2020-02-10 20:37:53 +08:00
|
|
|
configs.group = name
|
2019-04-02 14:37:46 +08:00
|
|
|
}
|
|
|
|
|
2020-02-10 20:37:53 +08:00
|
|
|
// GetDefaultGroup returns the { name of default configuration.
|
2019-04-02 14:37:46 +08:00
|
|
|
func GetDefaultGroup() string {
|
2019-06-19 09:06:52 +08:00
|
|
|
defer instances.Clear()
|
2019-06-29 10:45:50 +08:00
|
|
|
configs.RLock()
|
|
|
|
defer configs.RUnlock()
|
2020-02-10 20:37:53 +08:00
|
|
|
return configs.group
|
2017-12-14 17:32:51 +08:00
|
|
|
}
|
2018-10-13 20:29:27 +08:00
|
|
|
|
2020-02-10 20:37:53 +08:00
|
|
|
// SetLogger sets the logger for orm.
|
2019-08-26 19:39:04 +08:00
|
|
|
func (bs *dbBase) SetLogger(logger *glog.Logger) {
|
|
|
|
bs.logger = logger
|
|
|
|
}
|
|
|
|
|
2020-02-10 20:37:53 +08:00
|
|
|
// GetLogger returns the logger of the orm.
|
2019-12-04 16:04:52 +08:00
|
|
|
func (bs *dbBase) GetLogger() *glog.Logger {
|
|
|
|
return bs.logger
|
|
|
|
}
|
|
|
|
|
2020-02-10 20:37:53 +08:00
|
|
|
// SetMaxIdleConnCount sets the max idle connection count for underlying connection pool.
|
2019-06-29 10:45:50 +08:00
|
|
|
func (bs *dbBase) SetMaxIdleConnCount(n int) {
|
2019-08-26 19:39:04 +08:00
|
|
|
bs.maxIdleConnCount = n
|
2018-10-13 20:29:27 +08:00
|
|
|
}
|
|
|
|
|
2020-02-10 20:37:53 +08:00
|
|
|
// SetMaxOpenConnCount sets the max open connection count for underlying connection pool.
|
2019-06-29 10:45:50 +08:00
|
|
|
func (bs *dbBase) SetMaxOpenConnCount(n int) {
|
2019-08-26 19:39:04 +08:00
|
|
|
bs.maxOpenConnCount = n
|
2018-10-13 20:29:27 +08:00
|
|
|
}
|
|
|
|
|
2020-02-10 20:37:53 +08:00
|
|
|
// SetMaxConnLifetime sets the connection TTL for underlying connection pool.
|
|
|
|
// If parameter <d> <= 0, it means the connection never expires.
|
2019-09-24 23:41:18 +08:00
|
|
|
func (bs *dbBase) SetMaxConnLifetime(d time.Duration) {
|
|
|
|
bs.maxConnLifetime = d
|
2018-12-12 20:01:10 +08:00
|
|
|
}
|
|
|
|
|
2020-02-10 20:37:53 +08:00
|
|
|
// String returns the node as string.
|
2018-12-12 20:01:10 +08:00
|
|
|
func (node *ConfigNode) String() string {
|
2019-06-19 09:06:52 +08:00
|
|
|
if node.LinkInfo != "" {
|
|
|
|
return node.LinkInfo
|
|
|
|
}
|
2019-11-28 23:19:37 +08:00
|
|
|
return fmt.Sprintf(
|
|
|
|
`%s@%s:%s,%s,%s,%s,%s,%v,%d-%d-%d`,
|
|
|
|
node.User, node.Host, node.Port,
|
2019-07-06 16:51:50 +08:00
|
|
|
node.Name, node.Type, node.Role, node.Charset, node.Debug,
|
2020-02-10 20:37:53 +08:00
|
|
|
node.MaxIdleConnCount,
|
|
|
|
node.MaxOpenConnCount,
|
|
|
|
node.MaxConnLifetime,
|
2019-06-19 09:06:52 +08:00
|
|
|
)
|
2018-12-14 18:35:51 +08:00
|
|
|
}
|
|
|
|
|
2020-02-10 20:37:53 +08:00
|
|
|
// SetDebug enables/disables the debug mode.
|
2018-12-15 15:50:39 +08:00
|
|
|
func (bs *dbBase) SetDebug(debug bool) {
|
2019-07-15 17:40:21 +08:00
|
|
|
if bs.debug.Val() == debug {
|
|
|
|
return
|
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
bs.debug.Set(debug)
|
2018-12-14 18:35:51 +08:00
|
|
|
}
|
|
|
|
|
2020-02-10 20:37:53 +08:00
|
|
|
// getDebug returns the debug value.
|
2018-12-15 15:50:39 +08:00
|
|
|
func (bs *dbBase) getDebug() bool {
|
2019-06-19 09:06:52 +08:00
|
|
|
return bs.debug.Val()
|
|
|
|
}
|