示例#1
0
文件: mongo.go 项目: go-crud/crud
func (crud *CRUD) Insert(v interface{}) error {
	session := crud.session.Copy()
	defer session.Close()
	value := reflect.ValueOf(v)

	id := bson.NewObjectId() // bid,_:=id.MarshalJSON()
	if value.Type().Kind() == reflect.Map {
		m := v.(map[string]interface{})
		fv := m["_id"]
		switch fv.(type) {
		case string:
			if len(fv.(string)) < 1 {
				m["_id"] = string(id)
			}
		case bson.ObjectId:
			if len(fv.(bson.ObjectId)) < 1 {
				m["_id"] = id
			}
		}

	} else {
		s := value.Elem()
		f := s.FieldByName("Id")
		fv := f.Interface()
		switch fv.(type) {
		case string:
			if len(fv.(string)) < 1 {
				f.SetString(string(id))
			}
		case bson.ObjectId:
			if len(fv.(bson.ObjectId)) < 1 {
				f.SetString(string(id))
			}
		}
	}

	err := errors2.Mgo(session.DB(crud.db).C(crud.c).Insert(v))
	return err
}
示例#2
0
func TestCRUD(t *testing.T) {
	assert := assert.New(t)

	session, dberr := mgo.Dial("192.168.1.178:27017")
	assert.NoError(dberr)
	defer session.Close()

	db := "test"
	c := "users"
	crud := NewCRUD(session, db, c)

	users := session.DB(db).C(c)

	users.RemoveAll(nil)

	data1 := &testData{Name: "Tom", Age: 44}
	err := crud.Insert(data1)
	assert.NoError(err)

	var data2 *testData
	//get none exist data
	err = users.FindId("2").One(&data2)
	assert.True(errors2.IsNotFound(errors2.Mgo(err)))
	assert.Nil(data2)

	//get exist data
	err = users.FindId(data1.Id).One(&data2)
	assert.NoError(err)
	assert.Equal(data2, data1)

	//check none exist data
	exist, err := crud.Exist("2")
	assert.NoError(err)
	assert.False(exist)

	//check exist with data
	exist, err = crud.Exist(data1.Id)
	assert.NoError(err)
	assert.True(exist)

	//update exist data
	args := make(map[string]interface{})
	args["age"] = 35
	err = crud.Update(data1.Id, args)
	assert.NoError(err)
	var data3 *testData
	err = users.FindId(data1.Id).One(&data3)
	assert.NoError(err)
	assert.NotEqual(data3, data2)

	err = crud.UpdateAll(data1.Id, data2)
	assert.NoError(err)
	err = users.FindId(data1.Id).One(&data3)
	assert.NoError(err)
	assert.Equal(data3, data2)

	//update none exist data
	err = crud.UpdateAll("2", data2)
	assert.True(errors2.IsNotFound(err))

	//delete exist data
	err = crud.Delete(data1.Id)
	assert.NoError(err)
	var data4 *testData
	err = users.FindId(data1.Id).One(&data4)
	assert.True(errors2.IsNotFound(errors2.Mgo(err)))
	assert.Nil(data4)

	//delete none exist data
	err = crud.Delete("2")
	assert.True(errors2.IsNotFound(err))
}
示例#3
0
文件: mongo.go 项目: go-crud/crud
func (crud *CRUD) UpdateAll(id interface{}, v interface{}) error {
	session := crud.session.Copy()
	defer session.Close()
	return errors2.Mgo(session.DB(crud.db).C(crud.c).UpdateId(id, v))
}
示例#4
0
文件: mongo.go 项目: go-crud/crud
func (crud *CRUD) Update(id interface{}, v map[string]interface{}) error {
	session := crud.session.Copy()
	defer session.Close()
	return errors2.Mgo(session.DB(crud.db).C(crud.c).UpdateId(id, bson.M{"$set": v}))
}
示例#5
0
文件: mongo.go 项目: go-crud/crud
func (crud *CRUD) Upsert(id, v interface{}) error {
	session := crud.session.Copy()
	defer session.Close()
	_, err := session.DB(crud.db).C(crud.c).UpsertId(id, v)
	return errors2.Mgo(err)
}
示例#6
0
文件: mongo.go 项目: go-crud/crud
func (crud *CRUD) Delete(id interface{}) error {
	session := crud.session.Copy()
	defer session.Close()
	// bson.ObjectIdHex(id)
	return errors2.Mgo(session.DB(crud.db).C(crud.c).RemoveId(id))
}