本文整理匯總了Golang中upper/io/db.Result.Update方法的典型用法代碼示例。如果您正苦於以下問題:Golang Result.Update方法的具體用法?Golang Result.Update怎麽用?Golang Result.Update使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類upper/io/db.Result
的用法示例。
在下文中一共展示了Result.Update方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Update
// Nombre: Update
// Este metodo se encarga de actualizar un registro
// si ya fue creado en nuestra base de datos es llamado por el
// metodo save
func (self *Dh) Update() (interface{}, api.Err) {
var errs api.Err
var res db.Result
id := reflect.ValueOf(self.Model).Elem().FieldByName("Id").Interface().(int64)
res = Collection.Find(db.Cond{"id": id})
err := res.Update(self.Model)
if err != nil {
errs.Push(api.Msg{
Field: "Save",
Error: err.Error(),
})
return nil, errs
}
dataUpdate, errr := self.FindById(id)
return dataUpdate, errr
}
示例2: DeleteLogic
func (self *Dh) DeleteLogic() api.Err {
var errs api.Err
var res db.Result
id := reflect.ValueOf(self.Model).Elem().FieldByName("Id").Interface().(int64)
res = Collection.Find(db.Cond{"id": id})
res.One(self.Model)
reflect.ValueOf(self.Model).Elem().FieldByName("Status").SetBool(false)
// Estatus := reflect.ValueOf(self.Model).Elem().FieldByName("Estatus").Interface().(bool)
err := res.Update(self.Model)
if err != nil {
errs.Push(api.Msg{
Field: "Save",
Error: err.Error(),
})
return errs
}
return errs
}
示例3: TestSimpleCRUD
func TestSimpleCRUD(t *testing.T) {
var err error
var controlItem birthday
for _, wrapper := range wrappers {
if settings[wrapper] == nil {
t.Fatalf(`No such settings entry for wrapper %s.`, wrapper)
} else {
var sess db.Database
sess, err = db.Open(wrapper, *settings[wrapper])
if err != nil {
t.Fatalf(`Test for wrapper %s failed: %q`, wrapper, err)
}
defer sess.Close()
born := time.Date(1941, time.January, 5, 0, 0, 0, 0, time.Local)
controlItem = birthday{
Name: "Hayao Miyazaki",
Born: born,
BornUT: &timeType{born},
}
col, err := sess.Collection(`birthdays`)
if err != nil {
if wrapper == `mongo` && err == db.ErrCollectionDoesNotExist {
// Expected error with mongodb.
} else {
t.Fatalf(`Could not use collection with wrapper %s: %q`, wrapper, err)
}
}
var id interface{}
if id, err = col.Append(controlItem); err != nil {
t.Fatalf(`Could not append item with wrapper %s: %q`, wrapper, err)
}
var res db.Result
switch wrapper {
case `mongo`:
res = col.Find(db.Cond{"_id": id.(bson.ObjectId)})
case `ql`:
res = col.Find(db.Cond{"id()": id})
default:
res = col.Find(db.Cond{"id": id})
}
var total uint64
total, err = res.Count()
if total != 1 {
t.Fatalf("%s: Expecting one row.", wrapper)
}
// No support for Marshaler and Unmarshaler is implemeted for QL and
// MongoDB.
if wrapper == `ql` || wrapper == `mongo` {
continue
}
var testItem birthday
err = res.One(&testItem)
if err != nil {
t.Fatalf("%s One(): %s", wrapper, err)
}
if reflect.DeepEqual(testItem, controlItem) == false {
t.Errorf("%s: testItem: %v (ts: %v)\n", wrapper, testItem, testItem.BornUT.value.Unix())
t.Errorf("%s: controlItem: %v (ts: %v)\n", wrapper, controlItem, controlItem.BornUT.value.Unix())
t.Fatalf("%s: Structs are different", wrapper)
}
var testItems []birthday
err = res.All(&testItems)
if err != nil {
t.Fatalf("%s All(): %s", wrapper, err)
}
if len(testItems) == 0 {
t.Fatalf("%s All(): Expecting at least one row.", wrapper)
}
for _, testItem = range testItems {
if reflect.DeepEqual(testItem, controlItem) == false {
t.Errorf("%s: testItem: %v\n", wrapper, testItem)
t.Errorf("%s: controlItem: %v\n", wrapper, controlItem)
t.Fatalf("%s: Structs are different", wrapper)
}
}
controlItem.Name = `宮崎駿`
err = res.Update(controlItem)
if err != nil {
//.........這裏部分代碼省略.........
示例4: TestSimpleCRUD
func TestSimpleCRUD(t *testing.T) {
var err error
var controlItem Birthday
for _, wrapper := range wrappers {
if settings[wrapper] == nil {
t.Fatalf(`No such settings entry for wrapper %s.`, wrapper)
} else {
var sess db.Database
sess, err = db.Open(wrapper, *settings[wrapper])
if err != nil {
t.Fatalf(`Test for wrapper %s failed: %s`, wrapper, err.Error())
}
controlItem = Birthday{
Name: "Hayao Miyazaki",
Born: time.Date(1941, time.January, 5, 0, 0, 0, 0, time.Local),
}
col, err := sess.Collection(`birthdays`)
if err != nil {
if wrapper == `mongo` && err == db.ErrCollectionDoesNotExists {
// Expected error with mongodb.
} else {
t.Fatalf(`Could not use collection with wrapper %s: %s`, wrapper, err.Error())
}
}
var id interface{}
id, err = col.Append(controlItem)
if err != nil {
t.Fatalf(`Could not append item with wrapper %s: %s`, wrapper, err.Error())
}
var res db.Result
if wrapper == `mongo` {
res = col.Find(db.Cond{"_id": id})
} else {
res = col.Find(db.Cond{"id": id})
}
var testItem Birthday
res.One(&testItem)
if reflect.DeepEqual(testItem, controlItem) == false {
t.Errorf("Struct is different with wrapper %s.", wrapper)
}
controlItem.Name = `宮崎駿`
err = res.Update(controlItem)
if err != nil {
t.Fatalf(`Could not update with wrapper %s: %s`, wrapper, err.Error())
}
res.One(&testItem)
if reflect.DeepEqual(testItem, controlItem) == false {
t.Errorf("Struct is different with wrapper %s.", wrapper)
}
err = res.Remove()
if err != nil {
t.Fatalf(`Could not update with wrapper %s: %s`, wrapper, err.Error())
}
var total uint64
total, err = res.Count()
if total != 0 {
t.Fatalf(`Expecting no items %s: %s`, wrapper, err.Error())
}
err = res.Close()
if err != nil {
t.Errorf("Failed to close result %s: %s.", wrapper, err.Error())
}
err = sess.Close()
if err != nil {
t.Errorf("Failed to close %s: %s.", wrapper, err.Error())
}
}
}
}