Exemple #1
0
func (c *Conn) buildSimpleSelectResult(value interface{}, name []byte, asName []byte) (*mysql.Resultset, error) {
	field := &mysql.Field{Name: name, OrgName: name}
	if asName != nil {
		field.Name = asName
	}

	formatField(field, value)

	r := &mysql.Resultset{Fields: []*mysql.Field{field}}
	row := mysql.Raw(byte(field.Type), value, false)
	r.RowDatas = append(r.RowDatas, mysql.PutLengthEncodedString(row, c.alloc))

	return r, nil
}
Exemple #2
0
func (c *Conn) buildResultset(nameTypes []schema.TableColumn, values []mysql.RowValue) (*mysql.Resultset, error) {
	r := &mysql.Resultset{Fields: make([]*mysql.Field, len(nameTypes))}

	var b []byte
	var err error

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

		var row []byte
		for j, value := range vs {
			field := &mysql.Field{}
			if i == 0 {
				r.Fields[j] = field
				//log.Warningf("%+v", nameTypes[i])
				field.Name = hack.Slice(nameTypes[j].Name)
				if err = formatField(field, value); err != nil {
					return nil, errors.Trace(err)
				}
				field.Type = nameTypes[j].SqlType
				field.Charset = uint16(mysql.CollationNames[nameTypes[j].Collation])
				field.IsUnsigned = nameTypes[j].IsUnsigned
			}

			if value == nil {
				row = append(row, "\xfb"...)
			} else {
				b = mysql.Raw(byte(field.Type), value, field.IsUnsigned)
				row = append(row, mysql.PutLengthEncodedString(b, c.alloc)...)
			}
		}

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

	return r, nil
}