コード例 #1
0
ファイル: mapper.go プロジェクト: abualsamid/tinyblog
// LoadRecord loads a mysql into a struct
func LoadRecord(r interface{}, res mysql.Result, row mysql.Row) {
	s := structs.New(r)
	var err error
	for _, f := range s.Fields() {

		for index, field := range res.Fields() {

			if strings.ToLower(field.Name) == strings.ToLower(f.Name()) {
				unsigned := (field.Flags & _FLAG_UNSIGNED) != 0
				switch field.Type {
				default:
					err = f.Set(row.Str(index))
				case MYSQL_TYPE_BIT:
					err = f.Set(row.Bin(index)[0] == 1)

				case MYSQL_TYPE_STRING, MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VARCHAR,
					MYSQL_TYPE_BLOB, MYSQL_TYPE_TINY_BLOB,
					MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_SET,
					MYSQL_TYPE_ENUM, MYSQL_TYPE_GEOMETRY:
					err = f.Set(row.Str(index))
				case MYSQL_TYPE_TINY, MYSQL_TYPE_SHORT, MYSQL_TYPE_YEAR, MYSQL_TYPE_LONG, MYSQL_TYPE_INT24:
					if unsigned {
						err = f.Set(row.Uint(index))
						if err != nil {
							err = f.Set(row.Uint64(index))
							if err != nil {
								err = f.Set(row.Int(index))
							}
						}
					} else {
						err = f.Set(row.Int(index))
					}
				case MYSQL_TYPE_LONGLONG:
					if unsigned {
						err = f.Set(row.Uint64(index))
						if err != nil {
							err = f.Set(int64(row.Uint64(index)))
						}
					} else {
						err = f.Set(int64(row.Uint64(index)))
					}
				case MYSQL_TYPE_FLOAT:
					err = f.Set(math.Float32frombits(uint32(row.Uint(index))))

				case MYSQL_TYPE_DOUBLE:
					err = f.Set(math.Float64frombits(row.Uint64(index)))
				case MYSQL_TYPE_DECIMAL, MYSQL_TYPE_NEWDECIMAL:
					if row.Str(index) != "" {
						r, err := strconv.ParseFloat(row.Str(index), 64)
						if err == nil {
							err = f.Set(decimal.NewFromFloat(r))
							if err != nil {
								fmt.Printf("error parsing Decimal field %s %s,  %v \n", field.Name, row.Str(index), err)
							}
						} else {
							fmt.Printf("error parsing Decimal field %s %s,  %v \n", field.Name, row.Str(index), err)
						}
					}

				case MYSQL_TYPE_DATE, MYSQL_TYPE_NEWDATE, MYSQL_TYPE_DATETIME, MYSQL_TYPE_TIMESTAMP:
					tmp := row.Str(index)
					if tmp != "" {
						t, err := time.Parse("2006-01-02 15:04:05", row.Str(index))
						// check out http://stackoverflow.com/questions/14106541/go-parsing-date-time-strings-which-are-not-standard-formats
						// and https://golang.org/src/time/format.go the string 2006-01-02 15:04:05 is not arbitrary and is not a date, it is the
						// actual format string.

						if err != nil {
							t, err = time.Parse("2006-01-02", row.Str(index))
							if err != nil {
								fmt.Printf("error parsing date field %s %s,  %v \n", field.Name, row.Str(index), err)
							}
						}
						err = f.Set(t)
						if err != nil {
							fmt.Printf("Error setting date %s %s => %v \n", field.Name, row.Str(index), t)
						}
					}

				case MYSQL_TYPE_TIME:
					err = f.Set(row.Duration(index))
				}

				if err != nil {
					fmt.Printf("Error setting value on %s => %v\n", field.Name, err)
				}
				break
			}
		}
	}
}