deprecate function DB.Table; improve the configuration node name case-insensitive and ignoring special chars

This commit is contained in:
John
2020-07-08 20:48:29 +08:00
parent 8b78609412
commit 9b6936a4fb
7 changed files with 157 additions and 55 deletions

View File

@ -25,46 +25,32 @@ import (
// DB defines the interfaces for ORM operations.
type DB interface {
// ===========================================================================
// Model creation.
// ===========================================================================
// Deprecated, use Model instead. The DB interface is designed not only for
// relational databases but also for NoSQL databases in the future. The name
// "Table" is not proper for that purpose any more.
Table(table ...string) *Model
Model(table ...string) *Model
Schema(schema string) *Schema
// Open creates a raw connection object for database with given node configuration.
// Note that it is not recommended using the this function manually.
Open(config *ConfigNode) (*sql.DB, error)
// ===========================================================================
// Query APIs.
// ===========================================================================
Query(sql string, args ...interface{}) (*sql.Rows, error)
Exec(sql string, args ...interface{}) (sql.Result, error)
Prepare(sql string, execOnMaster ...bool) (*sql.Stmt, error)
// Internal APIs for CURD, which can be overwrote for custom CURD implements.
DoQuery(link Link, sql string, args ...interface{}) (rows *sql.Rows, err error)
DoGetAll(link Link, sql string, args ...interface{}) (result Result, err error)
DoExec(link Link, sql string, args ...interface{}) (result sql.Result, err error)
DoPrepare(link Link, sql string) (*sql.Stmt, error)
DoInsert(link Link, table string, data interface{}, option int, batch ...int) (result sql.Result, err error)
DoBatchInsert(link Link, table string, list interface{}, option int, batch ...int) (result sql.Result, err error)
DoUpdate(link Link, table string, data interface{}, condition string, args ...interface{}) (result sql.Result, err error)
DoDelete(link Link, table string, condition string, args ...interface{}) (result sql.Result, err error)
// Query APIs for convenience purpose.
GetAll(sql string, args ...interface{}) (Result, error)
GetOne(sql string, args ...interface{}) (Record, error)
GetValue(sql string, args ...interface{}) (Value, error)
GetArray(sql string, args ...interface{}) ([]Value, error)
GetCount(sql string, args ...interface{}) (int, error)
GetStruct(objPointer interface{}, sql string, args ...interface{}) error
GetStructs(objPointerSlice interface{}, sql string, args ...interface{}) error
GetScan(objPointer interface{}, sql string, args ...interface{}) error
// Master/Slave specification support.
Master() (*sql.DB, error)
Slave() (*sql.DB, error)
// Ping-Pong.
PingMaster() error
PingSlave() error
// Transaction.
Begin() (*TX, error)
Transaction(f func(tx *TX) error) (err error)
// ===========================================================================
// Common APIs for CURD.
// ===========================================================================
Insert(table string, data interface{}, batch ...int) (sql.Result, error)
InsertIgnore(table string, data interface{}, batch ...int) (sql.Result, error)
@ -78,12 +64,57 @@ type DB interface {
Update(table string, data interface{}, condition interface{}, args ...interface{}) (sql.Result, error)
Delete(table string, condition interface{}, args ...interface{}) (sql.Result, error)
// Model creation.
Table(table ...string) *Model
Model(table ...string) *Model
Schema(schema string) *Schema
// ===========================================================================
// Internal APIs for CURD, which can be overwrote for custom CURD implements.
// ===========================================================================
DoQuery(link Link, sql string, args ...interface{}) (rows *sql.Rows, err error)
DoGetAll(link Link, sql string, args ...interface{}) (result Result, err error)
DoExec(link Link, sql string, args ...interface{}) (result sql.Result, err error)
DoPrepare(link Link, sql string) (*sql.Stmt, error)
DoInsert(link Link, table string, data interface{}, option int, batch ...int) (result sql.Result, err error)
DoBatchInsert(link Link, table string, list interface{}, option int, batch ...int) (result sql.Result, err error)
DoUpdate(link Link, table string, data interface{}, condition string, args ...interface{}) (result sql.Result, err error)
DoDelete(link Link, table string, condition string, args ...interface{}) (result sql.Result, err error)
// ===========================================================================
// Query APIs for convenience purpose.
// ===========================================================================
GetAll(sql string, args ...interface{}) (Result, error)
GetOne(sql string, args ...interface{}) (Record, error)
GetValue(sql string, args ...interface{}) (Value, error)
GetArray(sql string, args ...interface{}) ([]Value, error)
GetCount(sql string, args ...interface{}) (int, error)
GetStruct(objPointer interface{}, sql string, args ...interface{}) error
GetStructs(objPointerSlice interface{}, sql string, args ...interface{}) error
GetScan(objPointer interface{}, sql string, args ...interface{}) error
// ===========================================================================
// Master/Slave specification support.
// ===========================================================================
Master() (*sql.DB, error)
Slave() (*sql.DB, error)
// ===========================================================================
// Ping-Pong.
// ===========================================================================
PingMaster() error
PingSlave() error
// ===========================================================================
// Transaction.
// ===========================================================================
Begin() (*TX, error)
Transaction(f func(tx *TX) error) (err error)
// ===========================================================================
// Configuration methods.
// ===========================================================================
GetCache() *gcache.Cache
SetDebug(debug bool)
GetDebug() bool
@ -99,7 +130,10 @@ type DB interface {
SetMaxOpenConnCount(n int)
SetMaxConnLifetime(d time.Duration)
// ===========================================================================
// Utility methods.
// ===========================================================================
GetChars() (charLeft string, charRight string)
GetMaster(schema ...string) (*sql.DB, error)
GetSlave(schema ...string) (*sql.DB, error)
@ -115,7 +149,10 @@ type DB interface {
// arguments <args> as you wish before they're committed to driver.
HandleSqlBeforeCommit(link Link, sql string, args []interface{}) (string, []interface{})
// ===========================================================================
// Internal methods.
// ===========================================================================
filterFields(schema, table string, data map[string]interface{}) map[string]interface{}
convertValue(fieldValue []byte, fieldType string) interface{}
rowsToResult(rows *sql.Rows) (Result, error)

View File

@ -91,13 +91,18 @@ func DB(name ...string) gdb.DB {
return gins.Database(name...)
}
// Table creates and returns a model from specified database or default database configuration.
// The optional parameter <db> specifies the configuration group name of the database,
// which is "default" in default.
// Deprecated, use Model instead.
func Table(tables string, db ...string) *gdb.Model {
return DB(db...).Table(tables)
}
// Model creates and returns a model from specified database or default database configuration.
// The optional parameter <db> specifies the configuration group name of the database,
// which is "default" in default.
func Model(tables string, db ...string) *gdb.Model {
return DB(db...).Model(tables)
}
// Redis returns an instance of redis client with specified configuration group name.
func Redis(name ...string) *gredis.Redis {
return gins.Redis(name...)

View File

@ -19,6 +19,7 @@ import (
const (
gFRAME_CORE_COMPONENT_NAME_DATABASE = "gf.core.component.database"
gDATABASE_NODE_NAME = "database"
)
// Database returns an instance of database ORM object
@ -39,9 +40,15 @@ func Database(name ...string) gdb.DB {
}
return db
}
m := config.GetMap("database")
if m == nil {
panic(`database init failed: "database" node not found, is config file or configuration missing?`)
var m map[string]interface{}
if _, v := gutil.MapPossibleItemByKey(
config.GetMap("."),
gDATABASE_NODE_NAME,
); v != nil {
m = gconv.Map(v)
}
if len(m) == 0 {
panic(fmt.Sprintf(`database init failed: "%s" node not found, is config file or configuration missing?`, gDATABASE_NODE_NAME))
}
// Parse <m> as map-slice and adds it to gdb's global configurations.
for group, groupConfig := range m {
@ -78,11 +85,21 @@ func Database(name ...string) gdb.DB {
if db, err := gdb.New(name...); err == nil {
// Initialize logger for ORM.
m := config.GetMap(fmt.Sprintf("database.%s", gLOGGER_NODE_NAME))
if m == nil {
m = config.GetMap(gLOGGER_NODE_NAME)
var m map[string]interface{}
if _, v := gutil.MapPossibleItemByKey(
config.GetMap("."),
fmt.Sprintf("%s.%s", gDATABASE_NODE_NAME, gLOGGER_NODE_NAME),
); v != nil {
m = gconv.Map(v)
} else {
if _, v := gutil.MapPossibleItemByKey(
Config().GetMap("."),
gLOGGER_NODE_NAME,
); v != nil {
m = gconv.Map(v)
}
}
if m != nil {
if len(m) > 0 {
if err := db.GetLogger().SetConfigWithMap(m); err != nil {
panic(err)
}

View File

@ -9,6 +9,8 @@ package gins
import (
"fmt"
"github.com/gogf/gf/os/glog"
"github.com/gogf/gf/util/gconv"
"github.com/gogf/gf/util/gutil"
)
const (
@ -30,12 +32,22 @@ func Log(name ...string) *glog.Logger {
if Config().Available() {
var m map[string]interface{}
// It firstly searches the configuration of the instance name.
if m = Config().GetMap(fmt.Sprintf(`%s.%s`, gLOGGER_NODE_NAME, instanceName)); m == nil {
if _, v := gutil.MapPossibleItemByKey(
Config().GetMap("."),
fmt.Sprintf(`%s.%s`, gLOGGER_NODE_NAME, instanceName),
); v != nil {
m = gconv.Map(v)
} else {
// If the configuration for the instance does not exist,
// it uses the default logging configuration.
m = Config().GetMap(gLOGGER_NODE_NAME)
if _, v := gutil.MapPossibleItemByKey(
Config().GetMap("."),
gLOGGER_NODE_NAME,
); v != nil {
m = gconv.Map(v)
}
}
if m != nil {
if len(m) > 0 {
if err := logger.SetConfigWithMap(m); err != nil {
panic(err)
}

View File

@ -10,10 +10,12 @@ import (
"fmt"
"github.com/gogf/gf/database/gredis"
"github.com/gogf/gf/util/gconv"
"github.com/gogf/gf/util/gutil"
)
const (
gFRAME_CORE_COMPONENT_NAME_REDIS = "gf.core.component.redis"
gREDIS_NODE_NAME = "redis"
)
// Redis returns an instance of redis client with specified configuration group name.
@ -30,7 +32,11 @@ func Redis(name ...string) *gredis.Redis {
return gredis.Instance(group)
}
// Or else, it parses the default configuration file and returns a new redis instance.
if m := config.GetMap("redis"); m != nil {
var m map[string]interface{}
if _, v := gutil.MapPossibleItemByKey(Config().GetMap("."), gREDIS_NODE_NAME); v != nil {
m = gconv.Map(v)
}
if len(m) > 0 {
if v, ok := m[group]; ok {
redisConfig, err := gredis.ConfigFromStr(gconv.String(v))
if err != nil {

View File

@ -9,10 +9,13 @@ package gins
import (
"fmt"
"github.com/gogf/gf/net/ghttp"
"github.com/gogf/gf/util/gconv"
"github.com/gogf/gf/util/gutil"
)
const (
gFRAME_CORE_COMPONENT_NAME_SERVER = "gf.core.component.server"
gSERVER_NODE_NAME = "server"
)
// Server returns an instance of http server with specified name.
@ -24,12 +27,22 @@ func Server(name ...interface{}) *ghttp.Server {
if Config().Available() {
var m map[string]interface{}
// It firstly searches the configuration of the instance name.
if m = Config().GetMap(fmt.Sprintf(`server.%s`, s.GetName())); m == nil {
if _, v := gutil.MapPossibleItemByKey(
Config().GetMap("."),
fmt.Sprintf(`%s.%s`, gSERVER_NODE_NAME, s.GetName()),
); v != nil {
m = gconv.Map(v)
} else {
// If the configuration for the instance does not exist,
// it uses the default server configuration.
m = Config().GetMap("server")
if _, v := gutil.MapPossibleItemByKey(
Config().GetMap("."),
gSERVER_NODE_NAME,
); v != nil {
m = gconv.Map(v)
}
}
if m != nil {
if len(m) > 0 {
if err := s.SetConfigWithMap(m); err != nil {
panic(err)
}

View File

@ -9,6 +9,8 @@ package gins
import (
"fmt"
"github.com/gogf/gf/os/gview"
"github.com/gogf/gf/util/gconv"
"github.com/gogf/gf/util/gutil"
)
const (
@ -39,12 +41,22 @@ func getViewInstance(name ...string) *gview.View {
if Config().Available() {
var m map[string]interface{}
// It firstly searches the configuration of the instance name.
if m = Config().GetMap(fmt.Sprintf(`%s.%s`, gVIEWER_NODE_NAME, instanceName)); m == nil {
if _, v := gutil.MapPossibleItemByKey(
Config().GetMap("."),
fmt.Sprintf(`%s.%s`, gVIEWER_NODE_NAME, instanceName),
); v != nil {
m = gconv.Map(v)
} else {
// If the configuration for the instance does not exist,
// it uses the default view configuration.
m = Config().GetMap(gVIEWER_NODE_NAME)
if _, v := gutil.MapPossibleItemByKey(
Config().GetMap("."),
gVIEWER_NODE_NAME,
); v != nil {
m = gconv.Map(v)
}
}
if m != nil {
if len(m) > 0 {
if err := view.SetConfigWithMap(m); err != nil {
panic(err)
}