示例#1
0
文件: result.go 项目: Alienero/Rambo
func (sei *session) buildResultset(fields []*mysql.Field, names []string, values [][]interface{}) (*mysql.Resultset, error) {
	var ExistFields bool
	r := new(mysql.Resultset)

	r.Fields = make([]*mysql.Field, len(names))
	//use the field def that get from true database
	if len(fields) != 0 {
		if len(r.Fields) == len(fields) {
			ExistFields = true
		} else {
			return nil, mysql.ErrInvalidArgument
		}
	}

	var b []byte
	var err error

	for i, vs := range values {
		if len(vs) != len(r.Fields) {
			return nil, fmt.Errorf("row %d has %d column not equal %d", i, len(vs), len(r.Fields))
		}

		var row []byte
		for j, value := range vs {
			// column define
			if i == 0 {
				if ExistFields {
					r.Fields[j] = fields[j]
				} else {
					field := &mysql.Field{}
					r.Fields[j] = field
					field.Name = []byte(names[j])
					if err = formatField(field, value); err != nil {
						return nil, err
					}
				}

			}
			b, err = formatValue(value)
			if err != nil {
				return nil, err
			}

			row = append(row, mysql.PutLengthEncodedString(b)...)
		}

		r.RowDatas = append(r.RowDatas, row)
	}

	return r, nil
}
示例#2
0
文件: result.go 项目: Alienero/Rambo
func (sei *session) buildEmptySet(names []string, t []interface{}) (*mysql.Resultset, error) {
	r := new(mysql.Resultset)

	r.Fields = make([]*mysql.Field, len(names))

	for n, name := range names {
		field := &mysql.Field{}
		r.Fields[n] = field
		field.Name = []byte(name)
		if err := formatField(field, t[n]); err != nil {
			return nil, err
		}
	}
	return r, nil
}