Beispiel #1
0
func CreateMapsFromRows(rows *sql.Rows, types ...interface{}) (RecordMapList, error) {
	var err error

	columnNames, err := rows.Columns()
	if err != nil {
		return nil, err
	}

	// create interface
	var values []interface{}
	var reflectValues []reflect.Value

	var results RecordMapList
	values, reflectValues = sqlutils.CreateReflectValuesFromTypes(types)

	for rows.Next() {
		var result = RecordMap{}
		if err = rows.Scan(values...); err != nil {
			return nil, err
		}
		for i, name := range columnNames {
			result[name] = reflectValues[i].Elem().Interface()
		}
		results = append(results, result)
	}
	return results, nil
}
Beispiel #2
0
func CreateMapFromRows(rows *sql.Rows, types ...interface{}) (RecordMap, error) {
	var err error
	var values []interface{}
	var reflectValues []reflect.Value
	var result = RecordMap{}

	values, reflectValues = sqlutils.CreateReflectValuesFromTypes(types)
	if err = rows.Scan(values...); err != nil {
		return nil, err
	}

	columnNames, err := rows.Columns()
	if err != nil {
		return nil, err
	}

	for i, n := range columnNames {
		result[n] = reflectValues[i].Elem().Interface()
	}
	return result, nil
}