Пример #1
0
// Test 5: Database updates
func updateHandler(c *core.Context) {
	n := 1
	if nStr := c.Req.URL.Query().Get("queries"); len(nStr) > 0 {
		n, _ = strconv.Atoi(nStr)
	}

	c.Res.Header().Set("Content-Type", "application/json")

	if n <= 1 {
		var world World
		worldStatement.QueryRow(rand.Intn(worldRowCount)+1).Scan(&world.Id, &world.RandomNumber)
		world.RandomNumber = uint16(rand.Intn(worldRowCount) + 1)
		updateStatement.Exec(world.RandomNumber, world.Id)
		c.Json().Send(&world)
	} else {
		world := make([]World, n)
		for i := 0; i < n; i++ {
			if err := worldStatement.QueryRow(rand.Intn(worldRowCount)+1).Scan(&world[i].Id, &world[i].RandomNumber); err != nil {
				log.Fatalf("Error scanning world row: %s", err.Error())
			}
			world[i].RandomNumber = uint16(rand.Intn(worldRowCount) + 1)
			if _, err := updateStatement.Exec(world[i].RandomNumber, world[i].Id); err != nil {
				log.Fatalf("Error updating world row: %s", err.Error())
			}
		}
		c.Json().Send(world)
	}
}
Пример #2
0
// Test 2: Single database query
func dbHandler(c *core.Context) {
	var world World
	err := worldStatement.QueryRow(rand.Intn(worldRowCount)+1).Scan(&world.Id, &world.RandomNumber)
	if err != nil {
		log.Fatalf("Error scanning world row: %s", err.Error())
	}

	c.Res.Header().Set("Content-Type", "application/json")
	c.Json().Send(&world)
}
Пример #3
0
// Test 3: Multiple database queries
func queriesHandler(c *core.Context) {
	n := 1
	if nStr := c.Req.URL.Query().Get("queries"); len(nStr) > 0 {
		n, _ = strconv.Atoi(nStr)
	}

	if n <= 1 {
		dbHandler(c)
		return
	}

	world := make([]World, n)
	for i := 0; i < n; i++ {
		err := worldStatement.QueryRow(rand.Intn(worldRowCount)+1).Scan(&world[i].Id, &world[i].RandomNumber)
		if err != nil {
			log.Fatalf("Error scanning world row: %s", err.Error())
		}
	}

	c.Res.Header().Set("Content-Type", "application/json")
	c.Json().Send(world)
}
Пример #4
0
// Test 1: JSON serialization
func jsonHandler(c *core.Context) {
	c.Res.Header().Set("Content-Type", "application/javascript")
	c.Json().Send(&Message{helloWorldString})
}