查找未在DB表中引用的图像

时间:2013-12-20 17:16:32

标签: coldfusion

我查询目录中的所有文件(图像)

<cfset local.teamBase = "#GetDirectoryFromPath(GetBaseTemplatePath())#teamlogo\">
<cfdirectory action="LIST" directory="#local.teamBase#" name="rc.qryTeamLogo">

我稍后遍历所有这些文件名,跳过正在使用的文件名

   <select name="Logo" <cfif deleted>disabled="disabled"</cfif>>
      <option></option>
      <cfloop query="rc.qryTeamLogo">
         <cfif name EQ mylogo>
            <option value="#name#" selected>#name#</option> 
         <cfelseif request.objTeam.isUsed(name) EQ 1 OR name EQ "thumbs.db">
            <!--- Do not show --->
         <cfelse>
            <option value="#name#">#name#</option>  
         </cfif>
    </cfloop>
  </select>

isUsed()函数看起来像

<cffunction name="isUsed"  returntype="boolean" output="false">
<cfargument name="logo" required="true" type="string">

      <cfquery name="qryUsed" datasource="#application.DSN#">
         SELECT logo
         FROM   CCF.team
         WHERE  logo = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#arguments.logo#">
         AND        Deleted = 0
    </cfquery>

    <cfif qryUsed.recordcount EQ 0>
        <cfreturn false>
    </cfif> 

    <cfreturn true>
</cffunction>

这个问题是随着越来越多的文件被添加,这会越来越慢。

我真的想要一个可以组装整个事物的单个查询

2 个答案:

答案 0 :(得分:0)

将文件信息加载到XML中。将XML重新放入表中并加入其中。

<cfset  local.teamBase = "#GetDirectoryFromPath(GetBaseTemplatePath())#teamlogo\">

<cfset  local.qryTeamLogo = DirectoryList(local.teamBase, false, "query")>

<cfsavecontent variable="local.xmlTeamLogo">    
<ul class="xoxo">
    <cfoutput query="local.qryTeamLogo">    
        <li><code>#xmlformat(name)#</code></li>
    </cfoutput>
</ul>
</cfsavecontent>


<cfquery name="local.qryResult">
    DECLARE @xmlTeamLogo xml = <cfqueryparam cfsqltype="CF_SQL_varchar" value="#local.xmlTeamLogo#">

    SELECT Value AS Name
    FROM dbo.udfXOXORead(@xmlTeamLogo)
    WHERE   NOT Value IN    (
        SELECT  Logo
        FROM    CCF.Team WITH (NOLOCK)
        WHERE   Deleted = 0
        )
</cfquery>

表值函数

create function [dbo].[udfXOXORead](@xmlData xml)

returns @tblmessage TABLE (
    [value]     nvarchar(max) NULL
    )
as
begin
  INSERT INTO @tblMessage

  SELECT Tbl.Col.value('(code)[1]', 'nvarchar(max)')        AS [value]
  FROM @xmlData.nodes('/ul/li') Tbl(Col)

RETURN
end

答案 1 :(得分:0)

您可以在select

之前运行一个查询
<cfquery name="qryLogos" datasource="#application.DSN#">
SELECT logo
FROM CCF.team
WHERE logo IN (<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#valueList(rc.qryTeamLogo.name)#" list="true">)
AND Deleted = 0
</cfquery>

然后将所有这些徽标设置为一个数组并添加thumbs.db,这样您就可以少做一些检查。

<cfset allLogs = listToArray(valueList(qryLogos.logo))>
<cfset arrayAppend(allLogs,'thumbs.db')>

然后将你的elseif更改为此

<cfelseif arrayFind(allLogs,name)>
  <!--- don't display --->