Esempio n. 1
0
func Test(w rest.ResponseWriter, r *rest.Request) {
	log.Println("hogehogfukga")

	ctx := r.Env["context"].(context.Context)

	// データをselect
	test := model.Find(3)
	log.Println(test)

	// use redis
	redisTest(ctx)

	// データをupdate
	var h *xorm.Engine
	h = hoge.GetDBShardConnection("user", 1)

	session := h.NewSession()
	defer session.Close()

	var err error

	err = session.Begin()
	if checkErr(err, "begin error") {
		w.WriteJson(err)
		return
	}

	var u []model.User
	err = session.Where("id = ?", 3).ForUpdate().Find(&u)
	if checkErr(err, "user not found") {
		w.WriteJson(err)
		return
	}

	user := u[0]
	user.Score += 1

	time.Sleep(6 * time.Second)

	//res, e := session.Id(user.Id).Cols("score").Update(&user) // 単一 PK
	_, err = session.Id(core.PK{user.Id, user.Name}).Update(&user) // 複合PK
	if checkErr(err, "update error") {
		w.WriteJson(err)
		return
	}

	err = session.Commit()
	if checkErr(err, "commit error") {
		w.WriteJson(err)
		return
	}

	w.WriteJson(user)
}
Esempio n. 2
0
func Test(c *gin.Context) {

	var json PostJSON
	err := c.BindJSON(&json)
	if checkErr(c, err, "json error") {
		return
	}

	ctx := c.Value("gContext").(context.Context)

	// データをselect
	model.Find(3)

	// use redis
	redisTest(ctx)

	// データをupdate
	var h *xorm.Engine
	h = hoge.GetDBShardConnection("user", 1)

	session := h.NewSession()
	defer session.Close()

	err = session.Begin()
	if checkErr(c, err, "begin error") {
		return
	}

	var u []model.User
	err = session.Where("id = ?", 3).ForUpdate().Find(&u)
	if checkErr(c, err, "user not found") {
		return
	}

	user := u[0]
	user.Score += 1

	//time.Sleep(3 * time.Second)

	//res, e := session.Id(user.Id).Cols("score").Update(&user) // 単一 PK
	_, err = session.Id(core.PK{user.Id, user.Name}).Update(&user) // 複合PK
	if checkErr(c, err, "update error") {
		return
	}

	err = session.Commit()
	if checkErr(c, err, "commit error") {
		return
	}

	c.JSON(http.StatusOK, &user)
}
Esempio n. 3
0
func Find(userId int) User {
	var h *xorm.Engine
	h = hoge.GetDBShardConnection("user", 1)

	// データをselect
	var user = User{Id: userId}
	_, err := h.Get(&user)

	//var user User
	//_, err := h.Id(userId).Get(&user)

	checkErr(err, "not found data!")
	return user

}