本文整理汇总了Golang中upper/io/db.Result类的典型用法代码示例。如果您正苦于以下问题:Golang Result类的具体用法?Golang Result怎么用?Golang Result使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Result类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: main
func main() {
sess, err := db.Open("mongo", settings)
if err != nil {
fmt.Println("Unable to connect:", err.Error())
return
}
defer sess.Close()
birthdayCollection, err := sess.Collection("birthdays")
if err != nil {
if err != db.ErrCollectionDoesNotExists {
fmt.Println("Could not use collection:", err.Error())
return
}
} else {
err = birthdayCollection.Truncate()
if err != nil {
fmt.Println(err.Error())
return
}
}
birthdayCollection.Append(Birthday{
Name: "Hayao Miyazaki",
Born: time.Date(1941, time.January, 5, 0, 0, 0, 0, time.UTC),
})
birthdayCollection.Append(Birthday{
Name: "Nobuo Uematsu",
Born: time.Date(1959, time.March, 21, 0, 0, 0, 0, time.UTC),
})
birthdayCollection.Append(Birthday{
Name: "Hironobu Sakaguchi",
Born: time.Date(1962, time.November, 25, 0, 0, 0, 0, time.UTC),
})
var res db.Result
res = birthdayCollection.Find()
var birthdays []Birthday
err = res.All(&birthdays)
if err != nil {
panic(err.Error())
return
}
for _, birthday := range birthdays {
fmt.Printf("%s was born in %s.\n", birthday.Name, birthday.Born.Format("January 2, 2006"))
}
}
示例2: TestResultCount
// This test tries to use an empty filter and count how many elements were
// added into the artist collection.
func TestResultCount(t *testing.T) {
var err error
var res db.Result
// Opening database.
sess, err := db.Open(Adapter, settings)
if err != nil {
t.Fatal(err)
}
defer sess.Close()
// We should close the database when it's no longer in use.
artist, _ := sess.Collection("artist")
res = artist.Find()
// Counting all the matching rows.
total, err := res.Count()
if err != nil {
t.Fatal(err)
}
if total == 0 {
t.Fatalf("Should not be empty, we've just added some rows!")
}
}
示例3: RemoveForeverByConditions
func (self *Dh) RemoveForeverByConditions(conditions interface{}) api.Err {
var res db.Result
var errs api.Err
res = Collection.Find(conditions)
exist, err := res.Count()
if exist == 0 {
errs.Push(api.Msg{
Field: "Delete: ",
Error: api.ErrNoSuchRow,
})
return errs
}
err = res.Remove()
if err == nil {
return errs
} else {
errs.Push(api.Msg{
Field: "Database delete error: ",
Error: err.Error(),
})
}
return errs
}
示例4: Delete
func (self *Dh) Delete() api.Err {
var res db.Result
var errs api.Err
id := reflect.ValueOf(self.Model).Elem().FieldByName("Id").Interface().(int64)
res = Collection.Find(db.Cond{"id": id})
exist, err := res.Count()
if exist == 0 {
errs.Push(api.Msg{
Field: "Delete: ",
Error: api.ErrNoSuchRow,
})
return errs
}
err = res.Remove()
if err == nil {
return errs
} else {
errs.Push(api.Msg{
Field: "Database delete error: ",
Error: err.Error(),
})
}
return errs
}
示例5: main
func main() {
sess, err := db.Open("sqlite", settings)
if err != nil {
fmt.Println("Please create the `example.db` sqlite3 database.")
return
}
defer sess.Close()
birthdayCollection, err := sess.Collection("birthdays")
if err != nil {
fmt.Println(err.Error())
return
}
err = birthdayCollection.Truncate()
if err != nil {
fmt.Println(err.Error())
return
}
birthdayCollection.Append(Birthday{
Name: "Hayao Miyazaki",
Born: time.Date(1941, time.January, 5, 0, 0, 0, 0, time.UTC),
})
birthdayCollection.Append(Birthday{
Name: "Nobuo Uematsu",
Born: time.Date(1959, time.March, 21, 0, 0, 0, 0, time.UTC),
})
birthdayCollection.Append(Birthday{
Name: "Hironobu Sakaguchi",
Born: time.Date(1962, time.November, 25, 0, 0, 0, 0, time.UTC),
})
var res db.Result
res = birthdayCollection.Find()
var birthdays []Birthday
err = res.All(&birthdays)
if err != nil {
panic(err.Error())
return
}
for _, birthday := range birthdays {
fmt.Printf("%s was born in %s.\n", birthday.Name, birthday.Born.Format("January 2, 2006"))
}
}
示例6: FindAllByConditionsSortBy
func (self *Dh) FindAllByConditionsSortBy(bar interface{}, conditions interface{}, sort string) api.Err {
var res db.Result
var errs api.Err
res = Collection.Find(conditions).Sort(sort)
err := res.All(bar)
if err != nil {
errs.Push(api.Msg{
Field: "Not found",
Error: api.ErrNoSuchRow,
})
}
return errs
}
示例7: TestDataTypes
// Attempts to add many different datatypes to a single row in a collection,
// then it tries to get the stored datatypes and check if the stored and the
// original values match.
func TestDataTypes(t *testing.T) {
var res db.Result
var sess db.Database
var dataTypes db.Collection
var err error
var id interface{}
var exists uint64
if sess, err = db.Open(Adapter, settings); err != nil {
t.Fatal(err)
}
defer sess.Close()
// Getting a pointer to the "data_types" collection.
if dataTypes, err = sess.Collection("data_types"); err != nil {
t.Fatal(err)
}
// Removing all data.
if err = dataTypes.Truncate(); err != nil {
t.Fatal(err)
}
// Appending our test subject.
if id, err = dataTypes.Append(testValues); err != nil {
t.Fatal(err)
}
// Defining our set.
res = dataTypes.Find(db.Cond{"id": id})
if exists, err = res.Count(); err != nil {
t.Fatal(err)
}
if exists == 0 {
t.Fatalf("Expecting an item.")
}
// Trying to dump the subject into an empty structure of the same type.
var item testValuesStruct
res.One(&item)
// The original value and the test subject must match.
if reflect.DeepEqual(item, testValues) == false {
t.Fatalf("Struct is different.")
}
}
示例8: FindAllSortBy
func (self *Dh) FindAllSortBy(bar interface{}, sort string) api.Err {
var res db.Result
var errs api.Err
res = Collection.Find().Sort(sort)
count, _ := res.Count()
if count == 0 {
errs.Push(api.Msg{
Field: "Select All",
Error: api.ErrSelectAll,
})
}
res.All(bar)
return errs
}
示例9: TestDataTypes
// This test tries to add many different datatypes to a single row in a
// collection, then it tries to get the stored datatypes and check if the
// stored and the original values match.
func TestDataTypes(t *testing.T) {
var res db.Result
// Opening database.
sess, err := db.Open(wrapperName, settings)
if err != nil {
t.Fatalf(err.Error())
}
// We should close the database when it's no longer in use.
defer sess.Close()
// Getting a pointer to the "data_types" collection.
dataTypes, err := sess.Collection("data_types")
dataTypes.Truncate()
// Appending our test subject.
id, err := dataTypes.Append(testValues)
if err != nil {
t.Fatalf(err.Error())
}
// Trying to get the same subject we added.
res, err = dataTypes.Find(db.Cond{"id": id})
if err != nil {
t.Fatalf(err.Error())
}
exists, err := res.Count()
if err != nil {
t.Fatalf(err.Error())
}
if exists == 0 {
t.Errorf("Expecting an item.")
}
// Trying to dump the subject into an empty structure of the same type.
var item testValuesStruct
res.One(&item)
// The original value and the test subject must match.
if reflect.DeepEqual(item, testValues) == false {
t.Errorf("Struct is different.")
}
}
示例10: FindByConditions
func (self *Dh) FindByConditions(conditions interface{}) (uint64, api.Err) {
var errs api.Err
var res db.Result
res = Collection.Find(conditions)
count, _ := res.Count()
if count == 0 {
errs.Push(api.Msg{
Field: "Not found",
Error: api.ErrNoSuchRow,
})
return count, errs
}
res.One(self.Model)
return count, errs
}
示例11: FindByFieldValue
func (self *Dh) FindByFieldValue(field string, value interface{}) (interface{}, api.Err) {
var errs api.Err
var res db.Result
res = Collection.Find(db.Cond{field: value})
count, _ := res.Count()
if count == 0 {
errs.Push(api.Msg{
Field: "Not found",
Error: api.ErrNoSuchRow,
})
return nil, errs
}
res.One(self.Model)
return self.Model, errs
}
示例12: Exist
// Nombre: Exist
// Verifica que exista un elemento en nuestra base de datos
// La busca por su PK
func (self *Dh) Exist() bool {
s := reflect.ValueOf(self.Model).Elem().Field(0)
id := s.Interface().(int64)
// change the field id
// fmt.Println(s.Interface().(int64))
// fmt.Println(reflect.ValueOf(self.Model).Elem().FieldByName("Id"),"data")
// id := reflect.ValueOf(self.Model).Elem().FieldByName("Id").Interface().(int64)
var res db.Result
res = Collection.Find(db.Cond{"id": id})
count, _ := res.Count()
if count == 0 {
return false
}
return true
}
示例13: FindById
// FindById
// Podemos hacer busquedas por PK Id este metodo
// recibe como parametro la pk de la tabla y trae todos sus
// elementos.
func (self *Dh) FindById(id int64) (interface{}, api.Err) {
var errs api.Err
var res db.Result
res = Collection.Find(db.Cond{"id": id})
count, _ := res.Count()
if count == 0 {
errs.Push(api.Msg{
Field: "Not found",
Error: api.ErrNoSuchRow,
})
return nil, errs
}
res.One(self.Model)
return self.Model, errs
}
示例14: 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
}
示例15: 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
}