add conversion interface; add struct derive support; add single mapping support
This commit is contained in:
parent
5e90d61fa8
commit
2110aa631e
|
@ -0,0 +1,76 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"os"
|
||||
. "xorm"
|
||||
)
|
||||
|
||||
type Status struct {
|
||||
Name string
|
||||
Color string
|
||||
}
|
||||
|
||||
var (
|
||||
Registed Status = Status{"Registed", "white"}
|
||||
Approved Status = Status{"Approved", "green"}
|
||||
Removed Status = Status{"Removed", "red"}
|
||||
Statuses map[string]Status = map[string]Status{
|
||||
Registed.Name: Registed,
|
||||
Approved.Name: Approved,
|
||||
Removed.Name: Removed,
|
||||
}
|
||||
)
|
||||
|
||||
func (s *Status) FromDB(bytes []byte) error {
|
||||
if r, ok := Statuses[string(bytes)]; ok {
|
||||
*s = r
|
||||
return nil
|
||||
} else {
|
||||
return errors.New("no this data")
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Status) ToDB() ([]byte, error) {
|
||||
return []byte(s.Name), nil
|
||||
}
|
||||
|
||||
type User struct {
|
||||
Id int64
|
||||
Name string
|
||||
Status Status `xorm:"varchar(40)"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
f := "conversion.db"
|
||||
os.Remove(f)
|
||||
|
||||
Orm, err := NewEngine("sqlite3", f)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
Orm.ShowSQL = true
|
||||
err = Orm.CreateTables(&User{})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = Orm.Insert(&User{1, "xlw", Registed})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
users := make([]User, 0)
|
||||
err = Orm.Find(&users)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(users)
|
||||
}
|
13
session.go
13
session.go
|
@ -225,6 +225,12 @@ func (session *Session) scanMapIntoStruct(obj interface{}, objMap map[string][]b
|
|||
}
|
||||
|
||||
v = x
|
||||
} else if structConvert, ok := structField.Addr().Interface().(Conversion); ok {
|
||||
err := structConvert.FromDB(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
} else if session.Statement.UseCascade {
|
||||
table := session.Engine.AutoMapType(structField.Type())
|
||||
if table != nil {
|
||||
|
@ -710,6 +716,13 @@ func (session *Session) InsertOne(bean interface{}) (int64, error) {
|
|||
} else {
|
||||
continue
|
||||
}
|
||||
} else if fieldConvert, ok := fieldValue.Addr().Interface().(Conversion); ok {
|
||||
data, err := fieldConvert.ToDB()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
} else {
|
||||
args = append(args, string(data))
|
||||
}
|
||||
} else {
|
||||
args = append(args, val)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue