数据库的字段
type PerformanceConfig struct {
gorm.Model
InterfaceName string `gorm:"column:interfacename;primary_key;unique"`
TestId string `gorm:"column:testid"`
MaxUsers string `gorm:"column:maxusers"`
AvgThroughput string `gorm:"column:avgthroughput"`
Errors string `gorm:"column:errors"`
AvgResponseTime string `gorm:"column:avgResponsetime"`
ResponseTime90 string `gorm:"column:responsetime90"`
AvgBandwidth string `gorm:"column:avgbandwidth"`
AvgTransactions string `gorm:"column:avgtransactions"`
}
判断是否主键已有记录,有则更新,没有就创建
func (*PerformanceConfig) Create(config PerformanceConfig) {
plan := PerformanceConfig{}
InterfaceName := config.InterfaceName
err := db.Where("interfacename=?",InterfaceName).First(&plan).Error
if err != nil {
// error handling...
fmt.Println(err)
if gorm.IsRecordNotFoundError(err){
db.Create(&config) // newUser not user
}
}else{
db.Model(&config).Where("interfacename = ?", InterfaceName).Updates(&config)
}
}
网友评论