如何使用golang在`db.Exec`命令中使用条件参数

时间:2017-12-12 12:06:07

标签: go

我有一个带有可选字段的查询字符串,例如:

func (s *RepairClosesStore) UpdateByCondition(rc RepairClose, userID string, remoteUser string) (err error) {

    log.Println("repairclosesStore.go : UpdateByCondition method started")
    strQry := `
                UPDATE 
                    repaircloses 
                SET 
                    dispatchNumber = ?,
                    runDate = ?,
                    servicePerformed = ?,
                    notes = ? ,
                    repairCode = ?,
                    serialNumber = ? ,
                    repairSuccessful =?,
                    statusCode = ? ,
                    repairCloseFinished = ?,
                    billingFinished = ? ,
                    updateUserID = ? ,
                    updateRemoteUser = ?,
                    updateTimestamp = NOW()`

        //Checks repairCloseFinished
        if rc.RepairCloseFinished {

            //Append strQry query
            strQry += ` ,closeUserID = ?,
                        closeRemoteUser = ?,
                        closeTimeStamp = NOW()`
        }

        //Checks billingFinished
        if rc.BillingFinished {

            //Append strQry query
            strQry += ` ,billingFinishUserID = ?,
                        billingFinishRemoteUser = ?,
                        billingFinishTimeStamp = NOW()`

        }

        //Append strQry query
        strQry += ` WHERE RepairCloseID = ? `



 query, err := s.DB.Prepare(strQry)

queryArgs := []interface{
        rc.DispatchNumber,
        rc.RunDate,
        rc.ServicePerformed,
        rc.Notes,
        rc.RepairCode,
        rc.SerialNumber,
        rc.RepairSuccessful,

    }

    resp, err := query.Exec(queryArgs)
}

这是我使用的整个函数,但问题在于我不知道如何在Exec命令中附加可选参数以使用条件参数执行。

请帮帮我。

0 个答案:

没有答案