xorm支持各种关系数据库,最近使用postgresql时,总是踩到一些坑,在此记录下解决方式。在使用postgresql的array类型时,查询有点问题,xorm的官方文档给出重写的方式,但是不是很清晰:
官方文档链接:http://xorm.io/docs
也就是说碰到基础库不支持的类型,需要我们去重写ToDB、FromDB方法,废话不多说直接上代码:
比如int8[]类型,自定一个Int64Array
type Int64Array []int64
func (s *Int64Array) FromDB(bts []byte) error {
if len(bts) == 0 {
return nil
}
str := string(bts)
if strings.HasPrefix(str, "{") {
str = "[" + str[1:len(str)]
}
if strings.HasSuffix(str, "}") {
str = str[0: len(str)-1] + "]"
}
var ia = &[]int64{}
err := json.Unmarshal([]byte(str), ia)
if err != nil {
return err
}
*s = Int64Array(*ia)
return nil
}
func (s *Int64Array) ToDB() ([]byte, error) {
return serializeBigIntArray(*s, "{", "}"), nil
}
func (arr Int64Array) MarshalJSON() ([]byte, error) {
return serializeBigIntArrayAsString(arr, "[", "]"), nil
}
func (arr *Int64Array) UnmarshalJSON(b []byte) error {
var strarr []string
var intarr []int64
err := json.Unmarshal(b, &strarr)
if err != nil {
return err
}
for _, s := range strarr {
i, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return err
}
intarr = append(intarr, i)
}
*arr = intarr
return nil
}
func serializeBigIntArray(s []int64, prefix string, suffix string) []byte {
var buffer bytes.Buffer
buffer.WriteString(prefix)
for idx, val := range s {
if idx > 0 {
buffer.WriteString(",")
}
buffer.WriteString(strconv.FormatInt(val, 10))
}
buffer.WriteString(suffix)
return buffer.Bytes()
}
func serializeBigIntArrayAsString(s []int64, prefix string, suffix string) []byte {
var buffer bytes.Buffer
buffer.WriteString(prefix)
for idx, val := range s {
if idx > 0 {
buffer.WriteString(",")
}
buffer.WriteString("\"")
buffer.WriteString(strconv.FormatInt(val, 10))
buffer.WriteString("\"")
}
buffer.WriteString(suffix)
return buffer.Bytes()
}
json类型:
type Cover struct {
Id int64 `json:"id,omitempty"`
Fid string `json:"fid,omitempty"`
Type int8 `json:"type,omitempty"`
Url string `json:"url,omitempty"`
}
func (c *Cover) FromDB(bytes []byte) error {
return json.Unmarshal(bytes, c)
}
func (c *Cover) ToDB() (bytes []byte, err error) {
bytes, err = json.Marshal(c)
return
}
具体使用:
type Course struct {
Id int64 `json:"id,string" form:"id"`
Name string `json:"name" form:"name"`
Brief string `json:"brief" form:"brief"`
Description string `json:"description" form:"description"`
Cover common.Cover `xorm:"Text" json:"cover" form:"cover"`
Categories common.Int64Array `xorm:"Text" json:"categories" form:"categories[]"`
Tags common.Int64Array `json:"tags" form:"tags[]"`
Difficulty float64 `json:"difficulty" form:"difficulty"`
Price float64 `json:"price" form:"price"`
Markets common.Int64Array `json:"markets" form:"markets[]"`
StudentAmount int64 `json:"studentAmount" form:"studentAmount"`
SubjectAmount int64 `json:"subjectAmount" form:"subjectAmount"`
Crt time.Time `json:"crt"`
Lut time.Time `json:"lut"`
Status int16 `json:"status" form:"status"`
common.Page `xorm:"-"`
}