如何检查查询和函数是否成功执行?

时间:2019-06-27 14:04:49

标签: coldfusion cfml coldfusion-11 cfquery cfqueryparam

我有一些参数很少的函数,然后将执行查询删除。之后,我调用另一个函数,该函数将使用时间戳和用户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."};
}

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

0 个答案:

没有答案