本文整理汇总了Golang中upper/io/db.Collection类的典型用法代码示例。如果您正苦于以下问题:Golang Collection类的具体用法?Golang Collection怎么用?Golang Collection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Collection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestResultCount
// Attempts to count all rows in our newly defined set.
func TestResultCount(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()
// We should close the database when it's no longer in use.
if artist, err = sess.Collection("artist"); err != nil {
t.Fatal(err)
}
// Defining a set with no conditions.
res = artist.Find()
// Counting all the matching rows.
if total, err = res.Count(); err != nil {
t.Fatal(err)
}
if total == 0 {
t.Fatalf("Counter should not be zero, we've just added some rows!")
}
}
示例2: buildQuery
func (r *result) buildQuery(col db.Collection) (db.Result, error) {
var res db.Result
if r.args.where == nil {
res = col.Find(db.Cond{})
} else {
res = col.Find((*r.args.where)...)
}
if r.args.limit != nil {
res = res.Limit(*r.args.limit)
}
if r.args.skip != nil {
res = res.Skip(*r.args.skip)
}
if r.args.sort != nil {
res = res.Sort((*r.args.sort)...)
}
if r.args.fields != nil {
res = res.Select((*r.args.fields)...)
}
if r.args.group != nil {
res = res.Group((*r.args.group)...)
}
return res, nil
}
示例3: PrintTable
func PrintTable(col db.Collection) {
var users []User
col.Find().All(&users)
fmt.Printf("%+v\n", users)
}
示例4: BenchmarkAppendUpper
// Benchmarking Append().
//
// Contributed by wei2912
// See: https://github.com/gosexy/db/issues/20#issuecomment-20097801
func BenchmarkAppendUpper(b *testing.B) {
var sess db.Database
var artist db.Collection
var err error
if sess, err = db.Open(Adapter, settings); err != nil {
b.Fatal(err)
}
defer sess.Close()
if artist, err = sess.Collection("artist"); err != nil {
b.Fatal(err)
}
artist.Truncate()
item := struct {
Name string `db:"name"`
}{"Hayao Miyazaki"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err = artist.Append(item); err != nil {
b.Fatal(err)
}
}
}
示例5: TestSetterAndConstrainer
// MongoDB: Does not support schemas so it can't has composite keys. We're
// testing db.Constrainer and db.IDSetter interface.
func TestSetterAndConstrainer(t *testing.T) {
var err error
var id interface{}
var sess db.Database
var compositeKeys db.Collection
if sess, err = db.Open(Adapter, settings); err != nil {
t.Fatal(err)
}
defer sess.Close()
if compositeKeys, err = sess.Collection("composite_keys"); err != nil {
if err != db.ErrCollectionDoesNotExist {
t.Fatal(err)
}
}
//n := rand.Intn(100000)
item := itemWithKey{
// "ABCDEF",
// strconv.Itoa(n),
SomeVal: "Some value",
}
if id, err = compositeKeys.Append(&item); err != nil {
t.Fatal(err)
}
// ids := id.([]interface{})
// if ids[0].(string) != item.Code {
// t.Fatal(`Keys must match.`)
// }
//
// if ids[1].(string) != item.UserID {
// t.Fatal(`Keys must match.`)
// }
// Using constraint interface.
res := compositeKeys.Find(itemWithKey{ID: id.(bson.ObjectId)})
var item2 itemWithKey
if item2.SomeVal == item.SomeVal {
t.Fatal(`Values must be different before query.`)
}
if err := res.One(&item2); err != nil {
t.Fatal(err)
}
if item2.SomeVal != item.SomeVal {
t.Fatal(`Values must be equal after query.`)
}
}
示例6: 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()
}
示例7: TestCompositeKeys
// Attempts to test composite keys.
func TestCompositeKeys(t *testing.T) {
var err error
var sess db.Database
var compositeKeys db.Collection
if sess, err = db.Open(Adapter, settings); err != nil {
t.Fatal(err)
}
defer sess.Close()
if compositeKeys, err = sess.Collection("composite_keys"); err != nil {
t.Fatal(err)
}
n := rand.Intn(100000)
item := itemWithKey{
"ABCDEF",
strconv.Itoa(n),
"Some value",
}
if _, err = compositeKeys.Append(&item); err != nil {
t.Fatal(err)
}
// Using constraint interface.
var item2 itemWithKey
if item2.SomeVal == item.SomeVal {
t.Fatal(`Values must be different before query.`)
}
res := compositeKeys.Find(item)
if err := res.One(&item2); err != nil {
t.Fatal(err)
}
if item2.SomeVal != item.SomeVal {
t.Fatal(`Values must be equal after query.`)
}
}
示例8: TestTruncate
// Attempts to get all collections and truncate each one of them.
func TestTruncate(t *testing.T) {
var err error
var sess db.Database
var collections []string
var col db.Collection
// Opening database.
if sess, err = db.Open(Adapter, settings); err != nil {
t.Fatal(err)
}
// We should close the database when it's no longer in use.
defer sess.Close()
// Getting a list of all collections in this database.
if collections, err = sess.Collections(); err != nil {
t.Fatal(err)
}
if len(collections) == 0 {
t.Fatalf("Expecting some collections.")
}
// Walking over collections.
for _, name := range collections {
// Getting a collection.
if col, err = sess.Collection(name); err != nil {
t.Fatal(err)
}
// Table must exists before we can use it.
if col.Exists() == true {
// Truncating the table.
if err = col.Truncate(); err != nil {
t.Fatal(err)
}
}
}
}
示例9: main
func main() {
var (
col db.Collection
)
col = SetupCollection()
//START SETUP OMIT
col.Append(User{
Id: 1,
FirstName: "John",
LastName: "Doe",
Age: 24,
})
col.Append(User{
Id: 2,
FirstName: "Jane",
LastName: "Doe",
Age: 52,
})
col.Append(User{
Id: 3,
FirstName: "Joe",
LastName: "Shmoe",
Age: 10,
})
//END SETUP OMIT
//START CODE OMIT
var users []User
col.Find(db.Or{
db.Cond{"last_name": "Doe"},
db.Cond{"age <": "12"},
}).
Limit(2).
Sort("-age").
All(&users)
fmt.Printf("%+v\n", users)
//END CODE OMIT
}
示例10: TestGroup
func TestGroup(t *testing.T) {
var err error
var sess db.Database
var stats db.Collection
if sess, err = db.Open(Adapter, settings); err != nil {
t.Fatal(err)
}
type statsType struct {
Numeric int `db:"numeric"`
Value int `db:"value"`
}
defer sess.Close()
if stats, err = sess.Collection("stats_test"); err != nil {
t.Fatal(err)
}
// Truncating table.
if err = stats.Truncate(); err != nil {
t.Fatal(err)
}
// Adding row append.
for i := 0; i < 1000; i++ {
numeric, value := rand.Intn(10), rand.Intn(100)
if _, err = stats.Append(statsType{numeric, value}); err != nil {
t.Fatal(err)
}
}
// db.Func{"COUNT", 1},
// db.Func{"SUM", `value`},
// Testing GROUP BY
res := stats.Find().Select(
`numeric`,
db.Raw{`COUNT(1) AS counter`},
db.Raw{`SUM(value) AS total`},
).Group(`numeric`)
var results []map[string]interface{}
if err = res.All(&results); err != nil {
t.Fatal(err)
}
if len(results) != 10 {
t.Fatalf(`Expecting exactly 10 results, this could fail, but it's very unlikely to happen.`)
}
}
示例11: BenchmarkAppendTxUpperMap
// Benchmarking Append() with map.
func BenchmarkAppendTxUpperMap(b *testing.B) {
var sess db.Database
var err error
if sess, err = db.Open(Adapter, settings); err != nil {
b.Fatal(err)
}
defer sess.Close()
var tx db.Tx
if tx, err = sess.Transaction(); err != nil {
b.Fatal(err)
}
var artist db.Collection
if artist, err = tx.Collection("artist"); err != nil {
b.Fatal(err)
}
if err = artist.Truncate(); err != nil {
b.Fatal(err)
}
item := map[string]string{"name": "Hayao Miyazaki"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err = artist.Append(item); err != nil {
b.Fatal(err)
}
}
if err = tx.Commit(); err != nil {
b.Fatal(err)
}
}
示例12: TestRemove
// Attempts to delete previously added rows.
func TestRemove(t *testing.T) {
var err error
var res db.Result
var sess db.Database
var artist db.Collection
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)
}
// Getting the artist with id = 1
res = artist.Find(db.Cond{"id": 1})
// Trying to remove the row.
if err = res.Remove(); err != nil {
t.Fatal(err)
}
}
示例13: 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.")
}
}
示例14: main
func main() {
var (
col db.Collection
user User
)
col = SetupCollection()
//START CODE OMIT
col.Append(User{Id: 1, FirstName: "John", LastName: "Doe"})
PrintTable(col)
col.Find(db.Cond{"id": 1}).One(&user)
user.FirstName = "James"
col.Find(user.Id).Update(user)
PrintTable(col)
col.Find(db.Cond{"id": user.Id}).Remove()
PrintTable(col)
//END CODE OMIT
}
示例15: createDefault
func createDefault(sexes db.Collection) {
sexes.Append(models.Sex{
Name: "man_sex",
Caption: "мужчина",
TypeName: "sex",
})
sexes.Append(models.Sex{
Name: "woman_sex",
Caption: "женщина",
TypeName: "sex",
})
sexes.Append(models.Sex{
Name: "trans_sex",
Caption: "транс",
TypeName: "sex",
})
}