如何比较 cfscript 中的列值?

How to compare column values in cfscript?

我想遍历查询并比较列值。这是 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>

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

(来自评论...)

问题是 local.qryRes 实际上不包含查询对象。令人困惑的是,调用 execute() 不会 return 查询,但调用 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. 由于访问字段密切相关,我可能会使用函数 return 一个包含所有三个键(AccessType、AccessLevel、PermissionList)的结构,而不是使用三个单独的函数.

  4. 与其使用循环,不如考虑使用其他线程中的建议之一,

您也可以使用:

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 ;
  }

另请参阅我关于另一个问题的笔记,该问题是关于潜在的无意的、半相关的数据。