Exemple #1
0
func (sei *session) limitSelectResult(r *mysql.Resultset, count, offset int64) {
	// rewrite count, remove bad index range
	if offset+count > int64(len(r.Values)) {
		count = int64(len(r.Values)) - offset
	}
	r.Values = r.Values[offset : offset+count]
	r.RowDatas = r.RowDatas[offset : offset+count]
}
Exemple #2
0
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
}
Exemple #3
0
func (sei *session) sortResultSet(set *mysql.Resultset, stmt *sqlparser.Select) error {
	if len(stmt.OrderBy) > 0 {
		glog.Info("Sort select results")
		ks := make([]mysql.SortKey, 0, len(stmt.OrderBy))
		for _, o := range stmt.OrderBy {
			ks = append(ks, mysql.SortKey{
				Direction: o.Direction,
				Name:      sqlparser.String(o.Expr),
			})
		}
		return set.Sort(ks)
	}
	return nil
}
Exemple #4
0
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
}