Esempio n. 1
0
// Set data
func scanValue(value interface{}, v reflect.Value) {
	val := reflect.ValueOf(value)

	switch val.Kind() {
	case reflect.Invalid:
	//nil: for now, it skips.
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		v.Set(reflect.ValueOf(u.Itoi(value)))
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		v.Set(reflect.ValueOf(u.ItoUI(value)))
	case reflect.Bool:
		v.Set(reflect.ValueOf(u.Itob(value)))
	case reflect.String:
		v.Set(reflect.ValueOf(u.Itos(value)))
	case reflect.Slice:
		if u.CheckInterface(value) == "[]uint8" {
			v.Set(reflect.ValueOf(u.ItoBS(value)))
		}
	//case reflect.Chan, reflect.Func, reflect.Ptr, reflect.Map:
	case reflect.Struct:
		//time.Time
		if u.CheckInterface(value) == "time.Time" {
			v.Set(reflect.ValueOf(u.ItoT(value).Format(tFomt)))
		}
	default: // reflect.Array, reflect.Struct, reflect.Interface
		v.Set(reflect.ValueOf(value))
	}
	return
}
Esempio n. 2
0
// Set data
func scanStruct(values []interface{}, v reflect.Value) {
	structType := v.Type()
	for i := 0; i < structType.NumField(); i++ {
		//val := reflect.ValueOf(values[i])
		val := reflect.TypeOf(values[i])
		switch val.Kind() {
		case reflect.Invalid:
		//nil: for now, it skips.
		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
			v.Field(i).Set(reflect.ValueOf(u.Itoi(values[i])))
		case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
			v.Field(i).Set(reflect.ValueOf(u.ItoUI(values[i])))
		case reflect.Bool:
			v.Field(i).Set(reflect.ValueOf(u.Itob(values[i])))
		case reflect.String:
			v.Field(i).Set(reflect.ValueOf(u.Itos(values[i])))
		case reflect.Slice:
			if u.CheckInterface(values[i]) == "[]uint8" {
				v.Field(i).Set(reflect.ValueOf(u.ItoBS(values[i])))
			}
		//case reflect.Chan, reflect.Func, reflect.Ptr, reflect.Map:
		case reflect.Struct:
			//time.Time
			if u.CheckInterface(values[i]) == "time.Time" {
				v.Field(i).Set(reflect.ValueOf(u.ItoT(values[i]).Format(tFomt)))
			}
		default: // reflect.Array, reflect.Struct, reflect.Interface
			v.Field(i).Set(reflect.ValueOf(values[i]))
		}
	}
	return
}
Esempio n. 3
0
// Convert result of select into Map[] type. Return multiple array map and interface(plural lines)
func (ms *MS) convertRowsToMaps(rows *sql.Rows) ([]map[string]interface{}, []string, error) {
	defer tm.Track(time.Now(), "convertRowsToMaps()")
	//convertRowsToMaps() took 85.191µs

	// Get column name
	columns, err := rows.Columns()
	if err != nil {
		return nil, nil, err
	}

	values := make([]interface{}, len(columns))

	// rows.Scan requires `[]interface{}` as parameter
	scanArgs := make([]interface{}, len(values))

	for i := range values {
		scanArgs[i] = &values[i]
	}

	retMaps := []map[string]interface{}{}
	//
	for rows.Next() { //true or false
		//Get data into scanArgs
		err = rows.Scan(scanArgs...)

		if err != nil {
			return nil, columns, err
		}

		rowData := map[string]interface{}{}

		//var v string
		for i, value := range values {
			if u.CheckInterface(value) == "[]uint8" {
				value = u.ItoBS(value)
			} else if u.CheckInterface(value) == "time.Time" {
				value = u.ItoT(value).Format(tFomt)
			}

			// Here we can check if the value is nil (NULL value)
			//if value == nil {
			//	v = "NULL"
			//} else {
			//	v = string(value)
			//}

			//if b, ok := value.([]byte); ok{
			//	v = string(b)
			//} else {
			//	v = "NULL"
			//}

			//rowdata[columns[i]] = v
			rowData[columns[i]] = value
		}
		retMaps = append(retMaps, rowData)
	}
	return retMaps, columns, nil
}