當前位置: 首頁>>代碼示例>>Golang>>正文


Golang BindVariable.Value方法代碼示例

本文整理匯總了Golang中github.com/youtube/vitess/go/vt/proto/query.BindVariable.Value方法的典型用法代碼示例。如果您正苦於以下問題:Golang BindVariable.Value方法的具體用法?Golang BindVariable.Value怎麽用?Golang BindVariable.Value使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/youtube/vitess/go/vt/proto/query.BindVariable的用法示例。


在下文中一共展示了BindVariable.Value方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: BindVariablesToProto3

// BindVariablesToProto3 converts internal type to proto3 BindVariable array
func BindVariablesToProto3(bindVars map[string]interface{}) (map[string]*pb.BindVariable, error) {
	if len(bindVars) == 0 {
		return nil, nil
	}

	result := make(map[string]*pb.BindVariable)
	for k, v := range bindVars {
		bv := new(pb.BindVariable)
		switch v := v.(type) {
		case []interface{}:
			// This is how the list variables will normally appear.
			if len(v) == 0 {
				return nil, fmt.Errorf("empty list not allowed: %s", k)
			}
			bv.Type = sqltypes.Tuple
			bv.Values = make([]*pb.Value, len(v))
			values := make([]pb.Value, len(v))
			for i, lv := range v {
				val, err := BindVariableToValue(lv)
				if err != nil {
					return nil, fmt.Errorf("key: %s: %v", k, err)
				}
				if val.Type != sqltypes.Null {
					values[i] = val
					bv.Values[i] = &values[i]
				}
			}
		case []string:
			if len(v) == 0 {
				return nil, fmt.Errorf("empty list not allowed: %s", k)
			}
			bv.Type = sqltypes.Tuple
			bv.Values = make([]*pb.Value, len(v))
			values := make([]pb.Value, len(v))
			for i, lv := range v {
				values[i].Type = sqltypes.VarChar
				values[i].Value = []byte(lv)
				bv.Values[i] = &values[i]
			}
		case [][]byte:
			if len(v) == 0 {
				return nil, fmt.Errorf("empty list not allowed: %s", k)
			}
			bv.Type = sqltypes.Tuple
			bv.Values = make([]*pb.Value, len(v))
			values := make([]pb.Value, len(v))
			for i, lv := range v {
				values[i].Type = sqltypes.VarBinary
				values[i].Value = lv
				bv.Values[i] = &values[i]
			}
		case []int:
			if len(v) == 0 {
				return nil, fmt.Errorf("empty list not allowed: %s", k)
			}
			bv.Type = sqltypes.Tuple
			bv.Values = make([]*pb.Value, len(v))
			values := make([]pb.Value, len(v))
			for i, lv := range v {
				values[i].Type = sqltypes.Int64
				values[i].Value = strconv.AppendInt(nil, int64(lv), 10)
				bv.Values[i] = &values[i]
			}
		case []int64:
			if len(v) == 0 {
				return nil, fmt.Errorf("empty list not allowed: %s", k)
			}
			bv.Type = sqltypes.Tuple
			bv.Values = make([]*pb.Value, len(v))
			values := make([]pb.Value, len(v))
			for i, lv := range v {
				values[i].Type = sqltypes.Int64
				values[i].Value = strconv.AppendInt(nil, lv, 10)
				bv.Values[i] = &values[i]
			}
		case []uint64:
			if len(v) == 0 {
				return nil, fmt.Errorf("empty list not allowed: %s", k)
			}
			bv.Type = sqltypes.Tuple
			bv.Values = make([]*pb.Value, len(v))
			values := make([]pb.Value, len(v))
			for i, lv := range v {
				values[i].Type = sqltypes.Uint64
				values[i].Value = strconv.AppendUint(nil, lv, 10)
				bv.Values[i] = &values[i]
			}
		default:
			val, err := BindVariableToValue(v)
			if err != nil {
				return nil, fmt.Errorf("key: %s: %v", k, err)
			}
			bv.Type = val.Type
			bv.Value = val.Value
		}
		result[k] = bv
	}
	return result, nil
}
開發者ID:hadmagic,項目名稱:vitess,代碼行數:100,代碼來源:proto3.go


注:本文中的github.com/youtube/vitess/go/vt/proto/query.BindVariable.Value方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。