1 回答
TA贡献1772条经验 获得超6个赞
使用 ExecContext 执行插入/更新查询。试试下面的代码
var (
newServices *models.UpsertIntoServices
companyCode, serviceCode, approvalEmail, approvalName, coApprovalEmail, coApprovalName, technicianEmail, technicianName string
departmentApproval, companyApproval, xapiensApproval, isActual, isMinus bool
id uint
)
tx, txErr := s.db.Begin()
if txErr != nil {
return newServices, txErr
}
qapi := `
insert into company_services
(company_code, service_code, department_approval, company_approval, xapiens_approval, approval_email, approval_name, co_approval_email,technician_email, co_approval_name, technician_name, is_actual, is_minus)
values
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
on conflict (company_code, service_code)
do update set company_code =excluded.company_code, service_code =excluded.service_code;
`
//insert data into new_services
result, sqlErr := tx.ExecContext(ctx, qapi, data.CompanyCode, data.ServiceCode, data.DepartmentApproval, data.CompanyApproval, data.XapiensApproval, data.ApprovalEmail, data.ApprovalName, data.CoApprovalEmail, data.TechnicianEmail, data.CoApprovalName, data.TechnicianName, data.IsActual, data.IsMinus)
// checking if its rows has any errors but no result here
if sqlErr != nil {
tx.Rollback()
log.Println("sql Error on Repository Upsert Flagging", sqlErr)
return newServices, sqlErr
}
//You can know lastInsertId and affectedRows
lastInsertId, _ := result.LastInsertId()
affectedRowsCnt,_ := result.RowsAffected()
fmt.Println(lastInsertId,affectedRowsCnt)
// commit transaction query
txErr = tx.Commit()
if txErr != nil {
return newServices, txErr
}
- 1 回答
- 0 关注
- 131 浏览
添加回答
举报