如何比较cfscript中的列值?

时间:2018-07-26 15:59:18

标签: coldfusion cfml cfquery cfloop

我想遍历查询并比较列值。这是CFML代码的示例:

<cfquery name="qryUserPerm" datasource="#Application.dsn#">
    SELECT AccessType, AccessLevel, State, City, Building
    FROM Permissions
    WHERE AccountID = <cfqueryparam cfsqltype="cf_sql_integer" value="#trim(session.AccountID)#">
</cfquery>

<cfset local.permissionType = "">
<cfset local.permissionLevel = "">
<cfset local.permissionList = "">

<cfif qryUserPerm.AccessLevel EQ "S">
     <cfset local.permissionType = qryUserPerm.AccessType>
     <cfset local.permissionLevel = qryUserPerm.AccessLevel>
     <cfset local.permissionList = qryUserPerm.State>
<cfelseif qryUserPerm.AccessLevel EQ "C">
     <cfset local.permissionType = qryUserPerm.AccessType>
     <cfset local.permissionLevel = qryUserPerm.AccessLevel>
     <cfset local.permissionList = ListRemoveDuplicates(ValueList(permissionList,qryUserPerm.City))>
<cfelseif qryUserPerm.AccessLevel EQ "B">
     <cfset local.permissionType = qryUserPerm.AccessType>
     <cfset local.permissionLevel = qryUserPerm.AccessLevel>
     <cfset local.permissionList = ListRemoveDuplicates(ValueList(permissionList,qryUserPerm.Building))>
</cfif>

上面的代码应该转换为cfscript,我到此为止,但无法弄清楚如何访问列值。

<cfscript>
    public string function permissionList(required string AccountID) {
        local.fnResults = "";
        local.permissionList = "";

        try{
            local.qryPermissions = new Query();
            local.qryPermissions.setDatasource("#Application.dsn#");
            local.qryPermissions.setSQL("SELECT AccessType, AccessLevel, State, City, Building FROM Permissions WHERE AccountID = :AccountID");
            local.qryPermissions.addParam(name="AccountID",value="#trim(arguments.AccountID)#",cfsqltype="cf_sql_idstamp");
            local.qryRes = qryPermissions.execute();

            for ( i = 1 ; i <= qryRes.getResult().recordCount ; i++ ) {
                if(qryRes["AccessLevel"][i] EQ "S"){
                    local.permissionList = "";
                }else if(qryRes["AccessLevel"][i] EQ "S"){
                    local.permissionList = ListRemoveDuplicates(ValueList(qryRes.Agency,","));
                }else if(qryRes["AccessLevel"][i] EQ "C"){
                    local.permissionList = ListRemoveDuplicates(ValueList(qryRes.District,","));
                }else if(qryRes["AccessLevel"][i] EQ "B"){
                    local.permissionList = ListRemoveDuplicates(ValueList(qryRes.Building,","));
                }
            }

            local.fnResults = permissionList;
        }catch(any e){
            local.fnResults = e.message;
            //writeOutput(e.message);    
        }

        return fnResults;
    }

    writeOutput(permissionList(AccountID));
</cfscript>

如果有人可以帮助,请告诉我。

2 个答案:

答案 0 :(得分:2)

(来自评论...)

问题是local.qryRes实际上不包含查询对象。令人困惑的是,调用execute()不会返回查询,但是调用execute()。getResult()会返回查询。尝试从以下位置更改分配:

local.qryRes = qryPermissions.execute();

收件人:

local.qryRes = qryPermissions.execute().getResult();

其他一些观察结果:

  1. local 作用域内所有函数变量(包括循环索引i)很重要。否则,如果组件存储在共享作用域中,则可能会得到一些奇怪且不可预测的结果。

  2. 尽管我认为不需要循环,但如果要循环,请考虑使用更简单的for..in语法,而不是索引循环:

    for (local.row in local.qryPermissions ) {
        if (local.row.AccessType eq "S") {
            //... code here 
        }
        .... 
    }
    
  3. 由于访问字段是如此紧密地相关,所以我可能会让该函数返回一个包含所有三个键(AccessType,AccessLevel,PermissionList)的结构,而不是具有三个单独的函数。

  4. 考虑使用其他线程中的一项建议,而不是使用循环, Best way to store permissions for the user account?

答案 1 :(得分:2)

您也可以使用:

local.qryPermissions = queryExecute(
        "SELECT AccessType, AccessLevel, State, City, Building
        FROM Permissions 
        WHERE AccountID = :AccountID" ,
        {AccountID={value="#trim(arguments.AccountID)#", cfsqltype="cf_sql_idstamp"}}  // Or "?" and "[value=xxx,cfsqltype=xxx]"
    ) ;

然后构建没有循环的权限块:

  local.permissionType = qryPermissions.AccessType ;
  local.permissionLevel = qryPermissions.AccessLevel ;

  switch( qryPermissions.AccessLevel ) {
    case "S" :  local.permissionList = qryPermissions.State ;
      break ;
    case "C" :  local.permissionList = ListRemoveDuplicates(ValueList(qryPermissions.City)) ;
      break ;
    case "B" :  local.permissionList = ListRemoveDuplicates(ValueList(qryPermissions.Building)) ;
      break ;
  }

另请参阅我关于另一个可能产生无意,半相关数据的问题的注释。