0

我有几个参数的函数,然后查询删除将被执行。之后,我调用另一个函数来更新带有时间戳和用户 ID 的表。我使用的代码如下所示:

remote function deleteComment(required string storeID, required string recID) returnFormat="JSON" {
    local.fnResults = structNew();
    local.runProcess = true;

    try {
        if ( !len(arguments.recID) ) {
            local.runProcess = false;
            local.fnResults = {status: 400, message: "Error! Incomplete or invalid data."};
        }

        if ( runProcess ) {
            local.qryParams = {rec_id: {cfsqltype: "cf_sql_numeric", value: "#arguments.recID#"}};
            local.deleteSQL = "DELETE FROM Articles WHERE rec_id = :rec_id";
            queryExecute(deleteSQL, qryParams, {datasource: "#application.datasource#", result: "deleteResult"});
            local.updateResult = updateProfile(arguments.agencyID);

            if ( deleteResult.recordcount && updateResult.recordcount ) {
                local.fnResults = {status: 200, message: "Record successfully removed."};
            } else {
                local.fnResults = {status: 400, message: "Error! Query failed."};
            }
        }
    } catch ( any e) {
        local.fnResults = {status: 400, message: "Error! Please contact your administrator"};
    }

    return fnResults;
}

这是函数的示例updateProfile

public struct function updateAgencyProfile(required string storeID) {
    try {
        local.qryParams = {
            storeID: {cfsqltype: "cf_sql_numeric", value: "#arguments.storeID#"},
            user_id: client.userid
        };

        local.Profile_SQL = "
            UPDATE profile
            SET last_update = getDate(),
                user_id = :user_id
            WHERE store_id = :storeID
        ";

        queryExecute(Profile_SQL, qryParams, {datasource: "#application.datasource#", result: "updateResult"});

        return updateResult;
    } catch ( any e) {
        return {status: 400};
    }
}

正如您在上面的示例中看到的那样,删除查询首先在我调用公共函数之后执行,该函数updateProfile将更新配置文件表并返回查询结果。我使用这段代码来检查两个查询是否成功执行:

if ( deleteResult.recordcount && updateResult.recordcount ) {
    local.fnResults = {status: 200, message: "Record successfully removed."};
} else {
    local.fnResults = {status: 400, message: "Error! Query failed."};
}

我想知道这是否有必要,或者我可以让 try catch 块来检测此函数中的任何错误?另外,这是否适合存储过程?我还有其他需要调用updateProfile来更新记录的函数。如果您有任何问题,请告诉我。

4

0 回答 0