本文整理汇总了Golang中github.com/ziutek/mymysql/mysql.Result.Fields方法的典型用法代码示例。如果您正苦于以下问题:Golang Result.Fields方法的具体用法?Golang Result.Fields怎么用?Golang Result.Fields使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/ziutek/mymysql/mysql.Result
的用法示例。
在下文中一共展示了Result.Fields方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: GenerateStructure
// GenerateStructure generates the GO struct from a MySQL Result set
func GenerateStructure(res mysql.Result, structName string) {
fmt.Printf("type %s struct {\n", structName)
for _, field := range res.Fields() {
fmt.Printf("\t%s", strings.Title(field.Name))
switch field.Type {
default:
fmt.Printf("\t%s\t`db:\"%s\" json:\"%s\"`\n", "string", field.Name, field.Name)
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:
fmt.Printf("\t%s\t`db:\"%s\" json:\"%s\"`\n", "string", field.Name, field.Name)
case MYSQL_TYPE_BIT:
fmt.Printf("\t%s\t`db:\"%s\" json:\"%s\"`\n", "bool", field.Name, field.Name)
case MYSQL_TYPE_TINY, MYSQL_TYPE_SHORT, MYSQL_TYPE_YEAR, MYSQL_TYPE_LONG, MYSQL_TYPE_INT24:
fmt.Printf("\t%s\t`db:\"%s\" json:\"%s\"`\n", "int", field.Name, field.Name)
case MYSQL_TYPE_LONGLONG:
fmt.Printf("\t%s\t`db:\"%s\" json:\"%s\"`\n", "uint64", field.Name, field.Name)
case MYSQL_TYPE_FLOAT:
fmt.Printf("\t%s\t`db:\"%s\" json:\"%s\"`\n", "float64", field.Name, field.Name)
case MYSQL_TYPE_DOUBLE:
fmt.Printf("\t%s\t`db:\"%s\" json:\"%s\"`\n", "float64", field.Name, field.Name)
case MYSQL_TYPE_DECIMAL, MYSQL_TYPE_NEWDECIMAL:
fmt.Printf("\t%s\t`db:\"%s\" json:\"%s\"`\n", "decimal.Decimal", field.Name, field.Name)
case MYSQL_TYPE_DATE, MYSQL_TYPE_NEWDATE:
fmt.Printf("\t%s\t`db:\"%s\" json:\"%s\"`\n", "time.Time", field.Name, field.Name)
case MYSQL_TYPE_DATETIME, MYSQL_TYPE_TIMESTAMP:
fmt.Printf("\t%s\t`db:\"%s\" json:\"%s\"`\n", "time.Time", field.Name, field.Name)
case MYSQL_TYPE_TIME:
fmt.Printf("\t%s\t`db:\"%s\" json:\"%s\"`\n", "time.Duration", field.Name, field.Name)
}
}
fmt.Println("}")
}
示例2: LoadRecord
// 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
}
}
}
}