コード例 #1
0
ファイル: crud.go プロジェクト: metakeule/pgsql
func NewCRUD(registry *pgsqlfat.Registry, proto interface{}) (c *CRUD) {
	c = &CRUD{
		Registry: registry,
		table:    registry.TableOf(proto),
		// table:       table,
		prototype:   proto,
		type_:       reflect.TypeOf(proto),
		type_string: pgsqlfat.TypeString(proto),
		fields:      map[string]map[string]bool{},
	}
	err := c.scanFields()
	if err != nil {
		panic(fmt.Sprintf("can't scan fields of %#v (%T): %s", proto, proto, err))
	}
	return
}
コード例 #2
0
ファイル: crud_test.go プロジェクト: metakeule/pgsql
func TestCRUDList(t *testing.T) {
	db.Exec("delete from company")
	//	id1, _ := CRUDCompany.Create(db, parseQuery("Name=testlist1&Age=42&UpdatedAt=2013-12-12 02:10:02"))
	id1, err := CRUDCompany.Create(db, b(`
	{
		"Name": "testlist1",
		"Age": 42,
		"UpdatedAt": "2013-12-12T02:10:02Z"
	}
	`), false, "")

	if err != nil {
		panic(err.Error())
	}
	//	id2, _ := CRUDCompany.Create(db, parseQuery("Name=testlist2&Age=43&UpdatedAt=2013-01-30 02:10:02"))
	//id2, _ := CRUDCompany.Create(db, parseQuery("Name=testlist2&Age=43"))
	id2, err2 := CRUDCompany.Create(db, b(`
	{
		"Name": "testlist2",
		"Age": 43
	}
	`), false, "")

	if err2 != nil {
		panic(err2.Error())
	}

	//CRUDCompany.Update(db, id1, parseQuery("Name=testlist1&Age=42&Ratings=[0,6,7]&Tags=[\"a\",\"b\"]&UpdatedAt=2014-01-03 02:10:02"))
	// CRUDCompany.Update(db, id1, b(`
	// {
	// 	"Name": "testlist1",
	// 	"Age": 42,
	// 	"Ratings": [0,6,7],
	// 	"Tags": ["a","b"],
	// 	"UpdatedAt": "2014-01-03 02:10:02"
	// }
	// `))

	CRUDCompany.Update(db, id1, b(`
	{
		"Name": "testlist1",
		"Age": 42,
		"UpdatedAt": "2014-01-03T02:10:02Z"
	}
	`), false, "")

	//CRUDCompany.Update(db, id2, parseQuery("Name=testlist2&Age=43&Ratings=[6,7,8]"))
	// registry.Field

	var c *Company = &Company{}
	// ty := reflect.TypeOf(c)
	// ty.
	tyPath := pgsqlfat.TypeString(c) // ty.PkgPath() + ty.String()
	// println(tyPath, "vs", "*github.com/metakeule/pgsql/rest.Company")
	// "*github.com/metakeule/pgsql/rest.Company"
	companyNameField := registry.Field(tyPath, "Name")

	if companyNameField == nil {
		panic("can't find field for COMPANY.Name")
	}

	total, comps, err := CRUDCompany.List(db, 10, ASC, companyNameField, 0)
	// comps, err := CRUDCompany.List(db, 10, OrderBy(companyNameField, ASC))

	if err != nil {
		t.Errorf("can't list created company with id1 %s and id2 %s: %s", id1, id2, err)
		return
	}

	c1 := comps[0] //.(*Company)
	c2 := comps[1] //.(*Company)

	if len(comps) != 2 {
		t.Errorf("results are not 2 companies, but %d", len(comps))
	}

	if total != 2 {
		t.Errorf("total results are not 2 companies, but %d", total)
	}

	/*
		if !ok1 || !ok2 {
			t.Errorf("results are no *Company, but %T and %T", comps[0], comps[1])
			return
		}
	*/

	if c1["Name"] != "testlist1" {
		t.Errorf("company 1 name is not testlist1, but %#v", c1["Name"])
	}

	if c2["Name"] != "testlist2" {
		t.Errorf("company 2 name is not testlist2, but %#v", c2["Name"])
	}

	if c1["Age"].(int64) != 42 {
		t.Errorf("company 1 age is not 42, but %#v", c1["Age"])
	}

	if c2["Age"].(int64) != 43 {
		t.Errorf("company 2 age is not 43, but %#v", c2["Age"])
	}

	/*
		if c1.Ratings.String() != "[0,6,7]" {
			t.Errorf("company 1 Ratings is not [0,6,7], but %#v", c1.Ratings.String())
		}

		if c1.Tags.String() != `["a","b"]` {
			t.Errorf("company 1 Tags is not [\"a\",\"b\"], but %#v", c1.Tags.String())
		}
	*/
	if c1["UpdatedAt"] != `2014-01-03T02:10:02Z` {
		t.Errorf("company 1 UpdatedAt is not 2014-01-03T02:10:02Z, but %#v", c1["UpdatedAt"])
	}

	// fmt.Printf("updatedat is set: %v\n", c2.UpdatedAt.IsSet)

	// fmt.Println(c2.UpdatedAt.String())
}