You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.1 KiB
55 lines
1.1 KiB
package comms
|
|
|
|
import (
|
|
"github.com/sirupsen/logrus"
|
|
"xorm.io/xorm"
|
|
)
|
|
|
|
// columns 第一个参数为where条件,(可选:第二个参数为表结构体或名称,第三个参数为单独获取的列名)
|
|
func XormOne(id, data interface{}, columns ...interface{}) bool {
|
|
ses := DbSysHelper.GetDB().NewSession()
|
|
defer ses.Close()
|
|
return XormOneDb(ses, id, data, columns...)
|
|
}
|
|
func XormOneDb(ses *xorm.Session, id, data interface{}, columns ...interface{}) bool {
|
|
if id == nil {
|
|
return false
|
|
}
|
|
|
|
col := "id"
|
|
if len(columns) > 0 {
|
|
tabed := false
|
|
cols, ok := columns[0].(string)
|
|
if ok {
|
|
if cols != "" {
|
|
col = cols
|
|
}
|
|
} else {
|
|
ses = ses.Table(columns[0])
|
|
tabed = true
|
|
}
|
|
|
|
if len(columns) > 1 {
|
|
for _, v := range columns[1:] {
|
|
cols, ok := v.(string)
|
|
if ok {
|
|
ses = ses.Cols(cols)
|
|
} else {
|
|
if !tabed {
|
|
ses = ses.Table(v)
|
|
tabed = true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
has, err := ses.Where(col+" = ?", id).Get(data)
|
|
if err != nil {
|
|
logrus.Errorf("XormOneDb db err:%v", err)
|
|
return false
|
|
}
|
|
if !has {
|
|
return false
|
|
}
|
|
return true
|
|
}
|