mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
改进gdb.Map/List及g.Map/List的类型定义,改用别名特性以便支持原生类型输入(map/slice),并修复gdb.Model.Update方法参数处理问题
This commit is contained in:
@ -97,11 +97,13 @@ type Record map[string]Value
|
||||
// 返回数据表记录List
|
||||
type Result []Record
|
||||
|
||||
// 关联数组,绑定一条数据表记录
|
||||
type Map map[string]interface{}
|
||||
|
||||
// 关联数组列表(索引从0开始的数组),绑定多条记录
|
||||
type List []Map
|
||||
// 关联数组,绑定一条数据表记录(使用别名)
|
||||
type Map = map[string]interface{}
|
||||
|
||||
// 关联数组列表(索引从0开始的数组),绑定多条记录(使用别名)
|
||||
type List = []Map
|
||||
|
||||
|
||||
// MySQL接口对象
|
||||
var linkMysql = &dbmysql{}
|
||||
|
||||
@ -228,11 +228,11 @@ func (db *Db) getInsertOperationByOption(option uint8) string {
|
||||
// 2: save: 如果数据存在(主键或者唯一索引),那么更新,否则写入一条新数据
|
||||
// 3: ignore: 如果数据存在(主键或者唯一索引),那么什么也不做
|
||||
func (db *Db) insert(table string, data Map, option uint8) (sql.Result, error) {
|
||||
var keys []string
|
||||
var fields []string
|
||||
var values []string
|
||||
var params []interface{}
|
||||
for k, v := range data {
|
||||
keys = append(keys, db.charl + k + db.charr)
|
||||
fields = append(fields, db.charl + k + db.charr)
|
||||
values = append(values, "?")
|
||||
params = append(params, v)
|
||||
}
|
||||
@ -247,7 +247,7 @@ func (db *Db) insert(table string, data Map, option uint8) (sql.Result, error) {
|
||||
}
|
||||
return db.Exec(
|
||||
fmt.Sprintf("%s INTO %s%s%s(%s) VALUES(%s) %s",
|
||||
operation, db.charl, table, db.charr, strings.Join(keys, ","), strings.Join(values, ","), updatestr), params...
|
||||
operation, db.charl, table, db.charr, strings.Join(fields, ","), strings.Join(values, ","), updatestr), params...
|
||||
)
|
||||
}
|
||||
|
||||
@ -345,10 +345,10 @@ func (db *Db) Update(table string, data interface{}, condition interface{}, args
|
||||
var fields []string
|
||||
keys := refValue.MapKeys()
|
||||
for _, k := range keys {
|
||||
fields = append(fields, fmt.Sprintf("%s%s%s=?", db.charl, k, db.charr))
|
||||
params = append(params, gconv.String(refValue.MapIndex(k).Interface()))
|
||||
updates = strings.Join(fields, ",")
|
||||
fields = append(fields, fmt.Sprintf("%s%s%s=?", db.charl, k, db.charr))
|
||||
params = append(params, gconv.String(refValue.MapIndex(k).Interface()))
|
||||
}
|
||||
updates = strings.Join(fields, ",")
|
||||
} else {
|
||||
updates = gconv.String(data)
|
||||
}
|
||||
|
||||
@ -137,6 +137,9 @@ func (md *Model) Data(data...interface{}) (*Model) {
|
||||
|
||||
// 链式操作, CURD - Insert/BatchInsert
|
||||
func (md *Model) Insert() (sql.Result, error) {
|
||||
if md.data == nil {
|
||||
return nil, errors.New("inserting into table with empty data")
|
||||
}
|
||||
// 批量操作
|
||||
if list, ok := md.data.(List); ok {
|
||||
batch := 10
|
||||
@ -148,12 +151,7 @@ func (md *Model) Insert() (sql.Result, error) {
|
||||
} else {
|
||||
return md.tx.BatchInsert(md.tables, list, batch)
|
||||
}
|
||||
}
|
||||
// 记录操作
|
||||
if md.data == nil {
|
||||
return nil, errors.New("inserting into table with empty data")
|
||||
}
|
||||
if dataMap, ok := md.data.(Map); ok {
|
||||
} else if dataMap, ok := md.data.(Map); ok {
|
||||
if md.tx == nil {
|
||||
return md.db.Insert(md.tables, dataMap)
|
||||
} else {
|
||||
@ -165,6 +163,9 @@ func (md *Model) Insert() (sql.Result, error) {
|
||||
|
||||
// 链式操作, CURD - Replace/BatchReplace
|
||||
func (md *Model) Replace() (sql.Result, error) {
|
||||
if md.data == nil {
|
||||
return nil, errors.New("replacing into table with empty data")
|
||||
}
|
||||
// 批量操作
|
||||
if list, ok := md.data.(List); ok {
|
||||
batch := 10
|
||||
@ -176,12 +177,7 @@ func (md *Model) Replace() (sql.Result, error) {
|
||||
} else {
|
||||
return md.tx.BatchReplace(md.tables, list, batch)
|
||||
}
|
||||
}
|
||||
// 记录操作
|
||||
if md.data == nil {
|
||||
return nil, errors.New("replacing into table with empty data")
|
||||
}
|
||||
if dataMap, ok := md.data.(Map); ok {
|
||||
} else if dataMap, ok := md.data.(Map); ok {
|
||||
if md.tx == nil {
|
||||
return md.db.Insert(md.tables, dataMap)
|
||||
} else {
|
||||
@ -193,6 +189,9 @@ func (md *Model) Replace() (sql.Result, error) {
|
||||
|
||||
// 链式操作, CURD - Save/BatchSave
|
||||
func (md *Model) Save() (sql.Result, error) {
|
||||
if md.data == nil {
|
||||
return nil, errors.New("replacing into table with empty data")
|
||||
}
|
||||
// 批量操作
|
||||
if list, ok := md.data.(List); ok {
|
||||
batch := 10
|
||||
@ -204,12 +203,7 @@ func (md *Model) Save() (sql.Result, error) {
|
||||
} else {
|
||||
return md.tx.BatchSave(md.tables, list, batch)
|
||||
}
|
||||
}
|
||||
// 记录操作
|
||||
if md.data == nil {
|
||||
return nil, errors.New("saving into table with empty data")
|
||||
}
|
||||
if dataMap, ok := md.data.(Map); ok {
|
||||
} else if dataMap, ok := md.data.(Map); ok {
|
||||
if md.tx == nil {
|
||||
return md.db.Save(md.tables, dataMap)
|
||||
} else {
|
||||
|
||||
8
g/g.go
8
g/g.go
@ -26,11 +26,11 @@ const (
|
||||
gIS_DATABASE_CONFIG_CACHED = "gf.core.component.database.cached"
|
||||
)
|
||||
|
||||
// 常用map数据结构
|
||||
type Map map[string]interface{}
|
||||
// 常用map数据结构(使用别名)
|
||||
type Map = map[string]interface{}
|
||||
|
||||
// 常用list数据结构
|
||||
type List []Map
|
||||
// 常用list数据结构(使用别名)
|
||||
type List = []Map
|
||||
|
||||
|
||||
// 阻塞等待HTTPServer执行完成(同一进程多HTTPServer情况下)
|
||||
|
||||
@ -477,7 +477,9 @@ func mapToStruct() {
|
||||
}
|
||||
|
||||
func main() {
|
||||
r, err := db.Table("user").Where("name = ?", "'jack'; delete from `user`").All()
|
||||
r, err := db.Table("user").Data([]map[string]interface{}{
|
||||
map[string]interface{}{"name" : "john11111"},
|
||||
}).Insert()
|
||||
fmt.Println(r)
|
||||
fmt.Println(err)
|
||||
//create()
|
||||
@ -498,7 +500,9 @@ func main() {
|
||||
//linkopUpdate2()
|
||||
//linkopUpdate3()
|
||||
//linkopUpdate4()
|
||||
//keepPing()
|
||||
//
|
||||
//transaction1()
|
||||
//transaction2()
|
||||
//
|
||||
//keepPing()
|
||||
}
|
||||
Reference in New Issue
Block a user