Пример #1
0
func row2mapStr(rows *core.Rows, fields []string) (resultsMap map[string]string, err error) {
	result := make(map[string]string)
	scanResultContainers := make([]interface{}, len(fields))
	for i := 0; i < len(fields); i++ {
		var scanResultContainer interface{}
		scanResultContainers[i] = &scanResultContainer
	}
	if err := rows.Scan(scanResultContainers...); err != nil {
		return nil, err
	}

	for ii, key := range fields {
		rawValue := reflect.Indirect(reflect.ValueOf(scanResultContainers[ii]))
		//if row is null then ignore
		if rawValue.Interface() == nil {
			//fmt.Println("ignore ...", key, rawValue)
			continue
		}

		if data, err := value2String(&rawValue); err == nil {
			result[key] = data
		} else {
			return nil, err // !nashtsai! REVIEW, should return err or just error log?
		}
	}
	return result, nil
}
Пример #2
0
func rows2maps(rows *core.Rows) (resultsSlice []map[string][]byte, err error) {
	fields, err := rows.Columns()
	if err != nil {
		return nil, err
	}
	for rows.Next() {
		result, err := row2map(rows, fields)
		if err != nil {
			return nil, err
		}
		resultsSlice = append(resultsSlice, result)
	}

	return resultsSlice, nil
}
Пример #3
0
func arrangeResult(rows *core.Rows, name string) (interface{}, error) {
	columnNames, err := rows.Columns()

	series := seriesStruct{}
	series.Columns = columnNames
	series.Name = name

	for rows.Next() {
		columnValues := make([]interface{}, len(columnNames))

		err = rows.ScanSlice(&columnValues)
		if err != nil {
			return nil, err
		}

		// bytes -> string
		for i := range columnValues {
			switch columnValues[i].(type) {
			case []byte:
				columnValues[i] = fmt.Sprintf("%s", columnValues[i])
			}
		}

		series.Values = append(series.Values, columnValues)
	}

	return series, err
}