當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Result.All方法代碼示例

本文整理匯總了Golang中menteslibres/net/gosexy/db.Result.All方法的典型用法代碼示例。如果您正苦於以下問題:Golang Result.All方法的具體用法?Golang Result.All怎麽用?Golang Result.All使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在menteslibres/net/gosexy/db.Result的用法示例。


在下文中一共展示了Result.All方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: TestDataTypes

// Tests datatype conversions.
func TestDataTypes(t *testing.T) {
	var res db.Result
	var items []db.Item

	sess, err := db.Open(wrapperName, settings)

	if err != nil {
		t.Fatalf(err.Error())
	}

	defer sess.Close()

	dataTypes := sess.ExistentCollection("data_types")

	dataTypes.Truncate()

	ids, err := dataTypes.Append(testValues)

	if err != nil {
		t.Fatalf(err.Error())
	}

	found, err := dataTypes.Count(db.Cond{"id": db.Id(ids[0])})

	if err != nil {
		t.Fatalf(err.Error())
	}

	if found == 0 {
		t.Errorf("Expecting an item.")
	}

	// Getting and reinserting (a db.Item).
	item, _ := dataTypes.Find()

	_, err = dataTypes.Append(item)

	if err == nil {
		t.Fatalf("Expecting duplicated-key error.")
	}

	delete(item, "id")

	_, err = dataTypes.Append(item)

	if err != nil {
		t.Fatalf(err.Error())
	}

	// Testing date ranges
	items, err = dataTypes.FindAll(db.Cond{
		"_date": time.Now(),
	})

	if err != nil {
		t.Fatalf(err.Error())
	}

	if len(items) > 0 {
		t.Fatalf("Expecting no results.")
	}

	items, err = dataTypes.FindAll(db.Cond{
		"_date <=": time.Now(),
	})

	if err != nil {
		t.Fatalf(err.Error())
	}

	if len(items) != 2 {
		t.Fatalf("Expecting some results.")
	}

	// Testing struct
	sresults := []testValuesStruct{}

	res, err = dataTypes.Query()

	if err != nil {
		t.Fatalf(err.Error())
	}

	err = res.All(&sresults)

	if err != nil {
		t.Fatalf(err.Error())
	}

	// Testing struct equality
	for _, item := range sresults {
		if reflect.DeepEqual(item, testValues) == false {
			t.Errorf("Struct is different.")
		}
	}

	// Testing maps
	results, _ := dataTypes.FindAll()

//.........這裏部分代碼省略.........
開發者ID:supermouseno1,項目名稱:db,代碼行數:101,代碼來源:postgresql_test.go

示例2: TestFind

// Tries to find and fetch rows.
func TestFind(t *testing.T) {

	var err error
	var res db.Result

	sess, err := db.Open(wrapperName, settings)

	if err != nil {
		t.Fatalf(err.Error())
	}

	defer sess.Close()

	people, _ := sess.Collection("people")

	// Testing Find()
	item, _ := people.Find(db.Cond{"name": "José"})

	if item["name"] != "José" {
		t.Fatalf("Could not find a recently appended item.")
	}

	// Fetch into map slice.
	dst := []map[string]string{}

	res, err = people.Query(db.Cond{"name": "José"})

	if err != nil {
		t.Fatalf(err.Error())
	}

	err = res.All(&dst)

	if err != nil {
		t.Fatalf(err.Error())
	}

	if len(dst) != 1 {
		t.Fatalf("Could not find a recently appended item.")
	}

	if dst[0]["name"] != "José" {
		t.Fatalf("Could not find a recently appended item.")
	}

	// Fetch into struct slice.
	dst2 := []struct{ Name string }{}

	res, err = people.Query(db.Cond{"name": "José"})

	if err != nil {
		t.Fatalf(err.Error())
	}

	err = res.All(&dst2)

	if err != nil {
		t.Fatalf(err.Error())
	}

	if len(dst2) != 1 {
		t.Fatalf("Could not find a recently appended item.")
	}

	if dst2[0].Name != "José" {
		t.Fatalf("Could not find a recently appended item.")
	}

	// Fetch into map.
	dst3 := map[string]interface{}{}

	res, err = people.Query(db.Cond{"name": "José"})

	if err != nil {
		t.Fatalf(err.Error())
	}

	err = res.One(&dst3)

	if err != nil {
		t.Fatalf(err.Error())
	}

	// Fetch into struct.
	dst4 := struct{ Name string }{}

	res, err = people.Query(db.Cond{"name": "José"})

	if err != nil {
		t.Fatalf(err.Error())
	}

	err = res.One(&dst4)

	if err != nil {
		t.Fatalf(err.Error())
	}

	if dst4.Name != "José" {
//.........這裏部分代碼省略.........
開發者ID:supermouseno1,項目名稱:db,代碼行數:101,代碼來源:postgresql_test.go

示例3: TestRelationStruct

// Tests relations between collections using structs.
func TestRelationStruct(t *testing.T) {
	var err error
	var res db.Result

	sess, err := db.Open(wrapperName, settings)

	if err != nil {
		t.Errorf(err.Error())
	}

	defer sess.Close()

	people := sess.ExistentCollection("people")

	results := []struct {
		Id          int
		Name        string
		PlaceCodeId int
		LivesIn     struct {
			Name string
		}
		HasChildren []struct {
			Name string
		}
		HasVisited []struct {
			PlaceId int
			Place   struct {
				Name string
			}
		}
	}{}

	res, err = people.Query(
		db.Relate{
			"LivesIn": db.On{
				sess.ExistentCollection("places"),
				db.Cond{"code_id": "{PlaceCodeId}"},
			},
		},
		db.RelateAll{
			"HasChildren": db.On{
				sess.ExistentCollection("children"),
				db.Cond{"parent_id": "{Id}"},
			},
			"HasVisited": db.On{
				sess.ExistentCollection("visits"),
				db.Cond{"person_id": "{Id}"},
				db.Relate{
					"Place": db.On{
						sess.ExistentCollection("places"),
						db.Cond{"id": "{PlaceId}"},
					},
				},
			},
		},
	)

	if err != nil {
		t.Fatalf(err.Error())
	}

	err = res.All(&results)

	if err != nil {
		t.Fatalf(err.Error())
	}

	fmt.Printf("relations (2) %# v\n", pretty.Formatter(results))
}
開發者ID:supermouseno1,項目名稱:db,代碼行數:70,代碼來源:postgresql_test.go

示例4: TestFind

// Tries to find and fetch rows.
func TestFind(t *testing.T) {

	var err error
	var res db.Result

	sess, err := db.Open(wrapperName, settings)

	if err != nil {
		t.Fatalf(err.Error())
	}

	defer sess.Close()

	/*
		// Testing simultaneous connection.
		sess2, err := db.Open(wrapperName, db.DataSource {
			Database: "mysql",
			Socket: socket,
			User: "root",
		})

		if err != nil {
			t.Fatalf(err.Error())
		}

		defer sess2.Close()
	*/

	people, _ := sess.Collection("people")

	// Testing Find()
	item, _ := people.Find(db.Cond{"name": "José"})

	if item["name"] != "José" {
		t.Fatalf("Could not find a recently appended item.")
	}

	// Fetch into map slice.
	dst := []map[string]string{}

	res, err = people.Query(db.Cond{"name": "José"})

	if err != nil {
		t.Fatalf(err.Error())
	}

	err = res.All(&dst)

	if err != nil {
		t.Fatalf(err.Error())
	}

	if len(dst) != 1 {
		t.Fatalf("Could not find a recently appended item.")
	}

	if dst[0]["name"] != "José" {
		t.Fatalf("Could not find a recently appended item.")
	}

	// Fetch into struct slice.
	dst2 := []struct {
		foo        string
		PersonName string `field:"name"`
		none       string
	}{}

	res, err = people.Query(db.Cond{"name": "José"})

	if err != nil {
		t.Fatalf(err.Error())
	}

	err = res.All(&dst2)

	if err != nil {
		t.Fatalf(err.Error())
	}

	if len(dst2) != 1 {
		t.Fatalf("Could not find a recently appended item.")
	}

	if dst2[0].PersonName != "José" {
		t.Fatalf("Could not find a recently appended item.")
	}

	// Fetch into map.
	dst3 := map[string]interface{}{}

	res, err = people.Query(db.Cond{"name": "José"})

	if err != nil {
		t.Fatalf(err.Error())
	}

	err = res.One(&dst3)

	if err != nil {
//.........這裏部分代碼省略.........
開發者ID:supermouseno1,項目名稱:db,代碼行數:101,代碼來源:mysql_test.go


注:本文中的menteslibres/net/gosexy/db.Result.All方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。