示例#1
0
func (h VitessHandler) selectQuery(query string) (*mysql.Result, error) {
	rows, err := h.DB.Query(query)
	if err != nil {
		fmt.Printf("query failed: %v\n", err)
		return &mysql.Result{0, 0, 0, nil}, err
	}
	columns, _ := rows.Columns()
	count := len(columns)
	// Read rows and convert them to [][]interface{}
	res := make([][]interface{}, 0)
	for rows.Next() {
		values := make([]interface{}, count)
		valuePtrs := make([]interface{}, count)
		for i, _ := range columns {
			valuePtrs[i] = &values[i]
		}
		if err := rows.Scan(valuePtrs...); err != nil {
			fmt.Printf("scan failed: %v\n", err)
			os.Exit(1)
		}
		res = append(res, values)
	}
	if err := rows.Err(); err != nil {
		fmt.Printf("row iteration failed: %v\n", err)
		os.Exit(1)
	}
	r, err := mysql.BuildSimpleResultset(columns, res, false)
	return &mysql.Result{0, 0, 0, r}, err
}
示例#2
0
func (h *testHandler) handleQuery(query string, binary bool) (*mysql.Result, error) {
	ss := strings.Split(query, " ")
	switch strings.ToLower(ss[0]) {
	case "select":
		var r *mysql.Resultset
		var err error
		//for handle go mysql driver select @@max_allowed_packet
		if strings.Contains(strings.ToLower(query), "max_allowed_packet") {
			r, err = mysql.BuildSimpleResultset([]string{"@@max_allowed_packet"}, [][]interface{}{
				[]interface{}{mysql.MaxPayloadLen},
			}, binary)
		} else {
			r, err = mysql.BuildSimpleResultset([]string{"a", "b"}, [][]interface{}{
				[]interface{}{1, "hello world"},
			}, binary)
		}

		if err != nil {
			return nil, err
		} else {
			return &mysql.Result{0, 0, 0, r}, nil
		}
	case "insert":
		return &mysql.Result{0, 1, 0, nil}, nil
	case "delete":
		return &mysql.Result{0, 0, 1, nil}, nil
	case "update":
		return &mysql.Result{0, 0, 1, nil}, nil
	case "replace":
		return &mysql.Result{0, 0, 1, nil}, nil
	default:
		return nil, fmt.Errorf("invalid query %s", query)
	}

	return nil, nil
}