mirror of
https://gitee.com/johng/gf.git
synced 2024-12-02 04:07:47 +08:00
rename constant names
This commit is contained in:
parent
751a567e84
commit
cb2c9c43a8
@ -289,7 +289,7 @@ func Register(name string, driver Driver) error {
|
||||
|
||||
// New creates and returns an ORM object with global configurations.
|
||||
// The parameter <name> specifies the configuration group name,
|
||||
// which is DEFAULT_GROUP_NAME in default.
|
||||
// which is DefaultGroupName in default.
|
||||
func New(group ...string) (db DB, err error) {
|
||||
groupName := configs.group
|
||||
if len(group) > 0 && group[0] != "" {
|
||||
@ -330,7 +330,7 @@ func New(group ...string) (db DB, err error) {
|
||||
|
||||
// Instance returns an instance for DB operations.
|
||||
// The parameter <name> specifies the configuration group name,
|
||||
// which is DEFAULT_GROUP_NAME in default.
|
||||
// which is DefaultGroupName in default.
|
||||
func Instance(name ...string) (db DB, err error) {
|
||||
group := configs.group
|
||||
if len(name) > 0 && name[0] != "" {
|
||||
|
@ -16,7 +16,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
DEFAULT_GROUP_NAME = "default" // Default group name.
|
||||
DEFAULT_GROUP_NAME = "default" // Deprecated, use DefaultGroupName instead.
|
||||
DefaultGroupName = "default" // Default group name.
|
||||
)
|
||||
|
||||
// Config is the configuration management object.
|
||||
@ -58,7 +59,7 @@ var configs struct {
|
||||
|
||||
func init() {
|
||||
configs.config = make(Config)
|
||||
configs.group = DEFAULT_GROUP_NAME
|
||||
configs.group = DefaultGroupName
|
||||
}
|
||||
|
||||
// SetConfig sets the global configuration for package.
|
||||
@ -88,12 +89,12 @@ func AddConfigNode(group string, node ConfigNode) {
|
||||
|
||||
// AddDefaultConfigNode adds one node configuration to configuration of default group.
|
||||
func AddDefaultConfigNode(node ConfigNode) {
|
||||
AddConfigNode(DEFAULT_GROUP_NAME, node)
|
||||
AddConfigNode(DefaultGroupName, node)
|
||||
}
|
||||
|
||||
// AddDefaultConfigGroup adds multiple node configurations to configuration of default group.
|
||||
func AddDefaultConfigGroup(nodes ConfigGroup) {
|
||||
SetConfigGroup(DEFAULT_GROUP_NAME, nodes)
|
||||
SetConfigGroup(DefaultGroupName, nodes)
|
||||
}
|
||||
|
||||
// GetConfig retrieves and returns the configuration of given group.
|
||||
|
@ -57,7 +57,7 @@ func init() {
|
||||
nodePrefix.Prefix = PREFIX1
|
||||
gdb.AddConfigNode("test", configNode)
|
||||
gdb.AddConfigNode("prefix", nodePrefix)
|
||||
gdb.AddConfigNode(gdb.DEFAULT_GROUP_NAME, configNode)
|
||||
gdb.AddConfigNode(gdb.DefaultGroupName, configNode)
|
||||
// Default db.
|
||||
if r, err := gdb.New(); err != nil {
|
||||
gtest.Error(err)
|
||||
|
@ -47,7 +47,7 @@ func init() {
|
||||
MaxOpenConnCount: 10,
|
||||
MaxConnLifetime: 600,
|
||||
}
|
||||
AddConfigNode(DEFAULT_GROUP_NAME, configNode)
|
||||
AddConfigNode(DefaultGroupName, configNode)
|
||||
// Default db.
|
||||
if r, err := New(); err != nil {
|
||||
gtest.Error(err)
|
||||
|
@ -19,8 +19,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
DEFAULT_GROUP_NAME = "default" // Default configuration group name.
|
||||
DEFAULT_REDIS_PORT = 6379 // Default redis port configuration if not passed.
|
||||
DefaultGroupName = "default" // Default configuration group name.
|
||||
DefaultRedisPort = 6379 // Default redis port configuration if not passed.
|
||||
)
|
||||
|
||||
var (
|
||||
@ -31,7 +31,7 @@ var (
|
||||
// SetConfig sets the global configuration for specified group.
|
||||
// If <name> is not passed, it sets configuration for the default group name.
|
||||
func SetConfig(config Config, name ...string) {
|
||||
group := DEFAULT_GROUP_NAME
|
||||
group := DefaultGroupName
|
||||
if len(name) > 0 {
|
||||
group = name[0]
|
||||
}
|
||||
@ -44,7 +44,7 @@ func SetConfig(config Config, name ...string) {
|
||||
// SetConfigByStr sets the global configuration for specified group with string.
|
||||
// If <name> is not passed, it sets configuration for the default group name.
|
||||
func SetConfigByStr(str string, name ...string) error {
|
||||
group := DEFAULT_GROUP_NAME
|
||||
group := DefaultGroupName
|
||||
if len(name) > 0 {
|
||||
group = name[0]
|
||||
}
|
||||
@ -60,7 +60,7 @@ func SetConfigByStr(str string, name ...string) error {
|
||||
// GetConfig returns the global configuration with specified group name.
|
||||
// If <name> is not passed, it returns configuration of the default group name.
|
||||
func GetConfig(name ...string) (config Config, ok bool) {
|
||||
group := DEFAULT_GROUP_NAME
|
||||
group := DefaultGroupName
|
||||
if len(name) > 0 {
|
||||
group = name[0]
|
||||
}
|
||||
@ -73,7 +73,7 @@ func GetConfig(name ...string) (config Config, ok bool) {
|
||||
// RemoveConfig removes the global configuration with specified group.
|
||||
// If <name> is not passed, it removes configuration of the default group name.
|
||||
func RemoveConfig(name ...string) {
|
||||
group := DEFAULT_GROUP_NAME
|
||||
group := DefaultGroupName
|
||||
if len(name) > 0 {
|
||||
group = name[0]
|
||||
}
|
||||
@ -96,7 +96,7 @@ func ConfigFromStr(str string) (config Config, err error) {
|
||||
Pass: array[4],
|
||||
}
|
||||
if config.Port == 0 {
|
||||
config.Port = DEFAULT_REDIS_PORT
|
||||
config.Port = DefaultRedisPort
|
||||
}
|
||||
if v, ok := parse["maxIdle"]; ok {
|
||||
config.MaxIdle = gconv.Int(v)
|
||||
@ -127,7 +127,7 @@ func ConfigFromStr(str string) (config Config, err error) {
|
||||
Pass: array[4],
|
||||
}
|
||||
if config.Port == 0 {
|
||||
config.Port = DEFAULT_REDIS_PORT
|
||||
config.Port = DefaultRedisPort
|
||||
}
|
||||
} else {
|
||||
err = gerror.Newf(`invalid redis configuration: "%s"`, str)
|
||||
|
@ -17,7 +17,7 @@ var (
|
||||
// The <name> param is unnecessary, if <name> is not passed,
|
||||
// it returns a redis instance with default configuration group.
|
||||
func Instance(name ...string) *Redis {
|
||||
group := DEFAULT_GROUP_NAME
|
||||
group := DefaultGroupName
|
||||
if len(name) > 0 && name[0] != "" {
|
||||
group = name[0]
|
||||
}
|
||||
|
@ -18,18 +18,18 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
gFRAME_CORE_COMPONENT_NAME_DATABASE = "gf.core.component.database"
|
||||
gDATABASE_NODE_NAME = "database"
|
||||
frameCoreComponentNameDatabase = "gf.core.component.database"
|
||||
configNodeNameDatabase = "database"
|
||||
)
|
||||
|
||||
// Database returns an instance of database ORM object
|
||||
// with specified configuration group name.
|
||||
func Database(name ...string) gdb.DB {
|
||||
group := gdb.DEFAULT_GROUP_NAME
|
||||
group := gdb.DefaultGroupName
|
||||
if len(name) > 0 && name[0] != "" {
|
||||
group = name[0]
|
||||
}
|
||||
instanceKey := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_DATABASE, group)
|
||||
instanceKey := fmt.Sprintf("%s.%s", frameCoreComponentNameDatabase, group)
|
||||
db := instances.GetOrSetFuncLock(instanceKey, func() interface{} {
|
||||
var (
|
||||
configMap map[string]interface{}
|
||||
@ -37,14 +37,17 @@ func Database(name ...string) gdb.DB {
|
||||
)
|
||||
// It firstly searches the configuration of the instance name.
|
||||
if Config().Available() {
|
||||
configNodeKey, _ = gutil.MapPossibleItemByKey(Config().GetMap("."), gDATABASE_NODE_NAME)
|
||||
configNodeKey, _ = gutil.MapPossibleItemByKey(
|
||||
Config().GetMap("."),
|
||||
configNodeNameDatabase,
|
||||
)
|
||||
if configNodeKey == "" {
|
||||
configNodeKey = gDATABASE_NODE_NAME
|
||||
configNodeKey = configNodeNameDatabase
|
||||
}
|
||||
configMap = Config().GetMap(configNodeKey)
|
||||
}
|
||||
if len(configMap) == 0 && !gdb.IsConfigured() {
|
||||
panic(fmt.Sprintf(`database init failed: "%s" node not found, is config file or configuration missing?`, gDATABASE_NODE_NAME))
|
||||
panic(fmt.Sprintf(`database init failed: "%s" node not found, is config file or configuration missing?`, configNodeNameDatabase))
|
||||
}
|
||||
if len(configMap) == 0 {
|
||||
configMap = make(map[string]interface{})
|
||||
@ -84,11 +87,11 @@ func Database(name ...string) gdb.DB {
|
||||
|
||||
if len(cg) > 0 {
|
||||
if gdb.GetConfig(group) == nil {
|
||||
intlog.Printf("add configuration for group: %s, %#v", gdb.DEFAULT_GROUP_NAME, cg)
|
||||
gdb.SetConfigGroup(gdb.DEFAULT_GROUP_NAME, cg)
|
||||
intlog.Printf("add configuration for group: %s, %#v", gdb.DefaultGroupName, cg)
|
||||
gdb.SetConfigGroup(gdb.DefaultGroupName, cg)
|
||||
} else {
|
||||
intlog.Printf("ignore configuration as it already exists for group: %s, %#v", gdb.DEFAULT_GROUP_NAME, cg)
|
||||
intlog.Printf("%s, %#v", gdb.DEFAULT_GROUP_NAME, cg)
|
||||
intlog.Printf("ignore configuration as it already exists for group: %s, %#v", gdb.DefaultGroupName, cg)
|
||||
intlog.Printf("%s, %#v", gdb.DefaultGroupName, cg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ const (
|
||||
// Redis returns an instance of redis client with specified configuration group name.
|
||||
func Redis(name ...string) *gredis.Redis {
|
||||
config := Config()
|
||||
group := gredis.DEFAULT_GROUP_NAME
|
||||
group := gredis.DefaultGroupName
|
||||
if len(name) > 0 && name[0] != "" {
|
||||
group = name[0]
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user