本文整理汇总了Golang中upper/io/db.Result.Count方法的典型用法代码示例。如果您正苦于以下问题:Golang Result.Count方法的具体用法?Golang Result.Count怎么用?Golang Result.Count使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类upper/io/db.Result
的用法示例。
在下文中一共展示了Result.Count方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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!")
}
}
示例2: 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
}
示例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: TestFunction
// Attempts to use functions within database queries.
func TestFunction(t *testing.T) {
var err error
var res db.Result
var sess db.Database
var artist db.Collection
var total uint64
if sess, err = db.Open(Adapter, settings); err != nil {
t.Fatal(err)
}
defer sess.Close()
if artist, err = sess.Collection("artist"); err != nil {
t.Fatal(err)
}
row_s := struct {
Id uint64
Name string
}{}
res = artist.Find(db.Cond{"id NOT IN": []int{0, -1}})
if err = res.One(&row_s); err != nil {
t.Fatal(err)
}
if total, err = res.Count(); err != nil {
t.Fatal(err)
}
if total != 3 {
t.Fatalf("Expecting 3 items.")
}
res = artist.Find(db.Cond{"id": db.Func{"NOT IN", []int{0, -1}}})
if err = res.One(&row_s); err != nil {
t.Fatal(err)
}
if total, err = res.Count(); err != nil {
t.Fatal(err)
}
if total != 3 {
t.Fatalf("Expecting 3 items.")
}
res.Close()
}
示例5: 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
}
示例6: 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.")
}
}
示例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: 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
}
示例9: 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
}
示例10: 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
}
示例11: 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
}
示例12: TestFunction
// Attempts to use functions within database queries.
func TestFunction(t *testing.T) {
var err error
var res db.Result
var sess db.Database
var artist db.Collection
var total uint64
var rowMap map[string]interface{}
if sess, err = db.Open(Adapter, settings); err != nil {
t.Fatal(err)
}
defer sess.Close()
if artist, err = sess.Collection("artist"); err != nil {
t.Fatal(err)
}
rowStruct := struct {
ID uint64
Name string
}{}
res = artist.Find(db.Cond{"id NOT IN": []int{0, -1}})
if err = res.One(&rowStruct); err != nil {
t.Fatal(err)
}
if total, err = res.Count(); err != nil {
t.Fatal(err)
}
if total != 4 {
t.Fatalf("Expecting 4 items.")
}
// Testing conditions
res = artist.Find(db.Cond{"id": db.Func{"NOT IN", []int{0, -1}}})
if err = res.One(&rowStruct); err != nil {
t.Fatal(err)
}
if total, err = res.Count(); err != nil {
t.Fatal(err)
}
if total != 4 {
t.Fatalf("Expecting 4 items.")
}
// Testing DISTINCT (function)
res = artist.Find().Select(
db.Func{`DISTINCT`, `name`},
)
if err = res.One(&rowMap); err != nil {
t.Fatal(err)
}
if total, err = res.Count(); err != nil {
t.Fatal(err)
}
if total != 4 {
t.Fatalf("Expecting 4 items.")
}
// Testing DISTINCT (raw)
res = artist.Find().Select(
db.Raw{`DISTINCT(name)`},
)
if err = res.One(&rowMap); err != nil {
t.Fatal(err)
}
if total, err = res.Count(); err != nil {
t.Fatal(err)
}
if total != 4 {
t.Fatalf("Expecting 4 items.")
}
res.Close()
}
示例13: FindAllCountFieldValue
func (self *Dh) FindAllCountFieldValue(field string, value interface{}) uint64 {
var res db.Result
res = Collection.Find(db.Cond{field: value})
count, _ := res.Count()
return count
}
示例14: TestFibonacci
func TestFibonacci(t *testing.T) {
var err error
var res db.Result
var total uint64
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()
var col db.Collection
col, err = sess.Collection("fibonacci")
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)
}
}
// Adding some items.
var i uint64
for i = 0; i < 10; i++ {
item := fibonacci{Input: i, Output: fib(i)}
_, err = col.Append(item)
if err != nil {
t.Fatalf(`Could not append item with wrapper %s: %q`, wrapper, err)
}
}
// Testing sorting by function.
res = col.Find(
// 5, 6, 7, 3
db.Or{
db.And{
db.Cond{"input >=": 5},
db.Cond{"input <=": 7},
},
db.Cond{"input": 3},
},
)
// Testing sort by function.
switch wrapper {
case `postgresql`:
res = res.Sort(db.Raw{`RANDOM()`})
case `sqlite`:
res = res.Sort(db.Raw{`RANDOM()`})
case `mysql`:
res = res.Sort(db.Raw{`RAND()`})
}
total, err = res.Count()
if err != nil {
t.Fatalf(`%s: %q`, wrapper, err)
}
if total != 4 {
t.Fatalf("%s: Expecting a count of 4, got %d.", wrapper, total)
}
// Find() with IN/$in
var whereIn db.Cond
switch wrapper {
case `mongo`:
whereIn = db.Cond{"input": db.Func{"$in", []int{3, 5, 6, 7}}}
default:
whereIn = db.Cond{"input": db.Func{"IN", []int{3, 5, 6, 7}}}
}
res = col.Find(whereIn).Sort("input")
total, err = res.Count()
if err != nil {
t.Fatalf(`%s: %q`, wrapper, err)
}
if total != 4 {
t.Fatalf(`Expecting a count of 4.`)
}
res = res.Skip(1).Limit(2)
for {
var item fibonacci
err = res.Next(&item)
if err == nil {
switch item.Input {
case 5:
//.........这里部分代码省略.........
示例15: 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 {
//.........这里部分代码省略.........