mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
fix bug #IISWI、#IISMY
This commit is contained in:
@ -148,6 +148,7 @@ func New(groupName...string) (*Db, error) {
|
||||
if len(masterList) < 1 {
|
||||
return nil, errors.New("at least one master node configuration's need to make sense")
|
||||
}
|
||||
|
||||
masterNode := getConfigNodeByPriority(&masterList)
|
||||
var slaveNode *ConfigNode
|
||||
if len(slaveList) > 0 {
|
||||
|
||||
@ -207,17 +207,17 @@ func (db *Db) insert(table string, data Map, option uint8) (sql.Result, error) {
|
||||
|
||||
// CURD操作:单条数据写入, 仅仅执行写入操作,如果存在冲突的主键或者唯一索引,那么报错返回
|
||||
func (db *Db) Insert(table string, data Map) (sql.Result, error) {
|
||||
return db.link.insert(table, data, OPTION_INSERT)
|
||||
return db.insert(table, data, OPTION_INSERT)
|
||||
}
|
||||
|
||||
// CURD操作:单条数据写入, 如果数据存在(主键或者唯一索引),那么删除后重新写入一条
|
||||
func (db *Db) Replace(table string, data Map) (sql.Result, error) {
|
||||
return db.link.insert(table, data, OPTION_REPLACE)
|
||||
return db.insert(table, data, OPTION_REPLACE)
|
||||
}
|
||||
|
||||
// CURD操作:单条数据写入, 如果数据存在(主键或者唯一索引),那么更新,否则写入一条新数据
|
||||
func (db *Db) Save(table string, data Map) (sql.Result, error) {
|
||||
return db.link.insert(table, data, OPTION_SAVE)
|
||||
return db.insert(table, data, OPTION_SAVE)
|
||||
}
|
||||
|
||||
// 批量写入数据
|
||||
@ -276,17 +276,17 @@ func (db *Db) batchInsert(table string, list List, batch int, option uint8) (sql
|
||||
|
||||
// CURD操作:批量数据指定批次量写入
|
||||
func (db *Db) BatchInsert(table string, list List, batch int) (sql.Result, error) {
|
||||
return db.link.batchInsert(table, list, batch, OPTION_INSERT)
|
||||
return db.batchInsert(table, list, batch, OPTION_INSERT)
|
||||
}
|
||||
|
||||
// CURD操作:批量数据指定批次量写入, 如果数据存在(主键或者唯一索引),那么删除后重新写入一条
|
||||
func (db *Db) BatchReplace(table string, list List, batch int) (sql.Result, error) {
|
||||
return db.link.batchInsert(table, list, batch, OPTION_REPLACE)
|
||||
return db.batchInsert(table, list, batch, OPTION_REPLACE)
|
||||
}
|
||||
|
||||
// CURD操作:批量数据指定批次量写入, 如果数据存在(主键或者唯一索引),那么更新,否则写入一条新数据
|
||||
func (db *Db) BatchSave(table string, list List, batch int) (sql.Result, error) {
|
||||
return db.link.batchInsert(table, list, batch, OPTION_SAVE)
|
||||
return db.batchInsert(table, list, batch, OPTION_SAVE)
|
||||
}
|
||||
|
||||
// CURD操作:数据更新,统一采用sql预处理
|
||||
|
||||
@ -16,7 +16,7 @@ func init () {
|
||||
Host : "127.0.0.1",
|
||||
Port : "3306",
|
||||
User : "root",
|
||||
Pass : "123456",
|
||||
Pass : "8692651",
|
||||
Name : "test",
|
||||
Type : "mysql",
|
||||
Role : "master",
|
||||
@ -438,23 +438,23 @@ func instance() {
|
||||
|
||||
func main() {
|
||||
|
||||
//create()
|
||||
//create()
|
||||
//insert()
|
||||
//query()
|
||||
//replace()
|
||||
//save()
|
||||
//batchInsert()
|
||||
//update1()
|
||||
//update2()
|
||||
//update3()
|
||||
//linkopSelect1()
|
||||
//linkopSelect2()
|
||||
//linkopSelect3()
|
||||
//linkopUpdate1()
|
||||
//linkopUpdate2()
|
||||
//linkopUpdate3()
|
||||
create()
|
||||
create()
|
||||
insert()
|
||||
query()
|
||||
replace()
|
||||
save()
|
||||
batchInsert()
|
||||
update1()
|
||||
update2()
|
||||
update3()
|
||||
linkopSelect1()
|
||||
linkopSelect2()
|
||||
linkopSelect3()
|
||||
linkopUpdate1()
|
||||
linkopUpdate2()
|
||||
linkopUpdate3()
|
||||
//keepPing()
|
||||
transaction1()
|
||||
transaction2()
|
||||
//transaction1()
|
||||
//transaction2()
|
||||
}
|
||||
@ -1,38 +1,56 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"gitee.com/johng/gf/g/database/gdb"
|
||||
"fmt"
|
||||
"gitee.com/johng/gf/g/util/gidgen"
|
||||
)
|
||||
|
||||
func main() {
|
||||
g := gidgen.New(2)
|
||||
for i := 0; i < 11; i++ {
|
||||
fmt.Println(g.Int())
|
||||
}
|
||||
g.Close()
|
||||
fmt.Println(g.Uint())
|
||||
//events2 := make(chan int, 100)
|
||||
//go func() {
|
||||
// for{
|
||||
// v := <- events1
|
||||
// fmt.Println(v)
|
||||
// }
|
||||
//
|
||||
//}()
|
||||
type Model struct {
|
||||
TableName string
|
||||
}
|
||||
|
||||
//go func() {
|
||||
// time.Sleep(2*time.Second)
|
||||
// events1 <- 1
|
||||
// events2 <- 2
|
||||
// time.Sleep(2*time.Second)
|
||||
// close(events1)
|
||||
// close(events2)
|
||||
// events1 <- 1
|
||||
// events2 <- 2
|
||||
//}()
|
||||
//
|
||||
//select {
|
||||
//
|
||||
//}
|
||||
var Db *gdb.Db
|
||||
|
||||
func init() {
|
||||
gdb.AddDefaultConfigNode(gdb.ConfigNode {
|
||||
Host : "127.0.0.1",
|
||||
Port : "3306",
|
||||
User : "root",
|
||||
Pass : "123456",
|
||||
Name : "test",
|
||||
Type : "mysql",
|
||||
Role : "master",
|
||||
Charset : "utf8",
|
||||
})
|
||||
var err error
|
||||
Db, err = gdb.Instance()
|
||||
checkErr(err)
|
||||
}
|
||||
|
||||
func checkErr(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
type UserModel struct {
|
||||
Model
|
||||
}
|
||||
|
||||
func (u *UserModel) Get() (user gdb.Map){
|
||||
user, _ = Db.Table("user").Fields("uid, nickname, email").Where("uid = ?", 15).One()
|
||||
return
|
||||
}
|
||||
|
||||
func (u *UserModel) Insert(data gdb.Map) (id int64) {
|
||||
ret, _ := Db.Table("user").Data(data).Insert()
|
||||
id, _ = ret.LastInsertId()
|
||||
return
|
||||
}
|
||||
|
||||
func main() {
|
||||
u := &UserModel{}
|
||||
user := u.Get()
|
||||
fmt.Println(user)
|
||||
u.Insert(gdb.Map{"uid": 100, "name": "jack"})
|
||||
}
|
||||
Reference in New Issue
Block a user