QueryFilter

説明

渡されたクエリの各行で指定された関数を呼び出し、関数が false を返した場合はその行をクエリから削除します。

戻り値

フィルターされたクエリ。

カテゴリ

クエリ関数

履歴

ColdFusion(2021 リリース):次のパラメーターが導入されました。

  • parallel
  • maxThreadCount

また、このリリースでは、QueryFilter によって元のクエリが変更されることはありません。下位互換性を維持するために、JVM フラグ coldfusion.query.filter.mutateinputquery が用意されています。

Adobe ColdFusion(2018 リリース):名前付きパラメーターが導入されました。

Adobe ColdFusion(2016 リリース):この関数が追加されました。

関連項目

QueryEachQuerySortQueryKeyExists

シンタックス

queryFilter(query, function(row [, currentRow] [, query] ){} [, parallel] [, maxThreadCount])
queryFilter(query, function(row [, currentRow] [, query] ){} [, parallel] [, maxThreadCount])
queryFilter(query, function(row [, currentRow] [, query] ){} [, parallel] [, maxThreadCount])

パラメーター

パラメーター

説明

query

(必須)反復処理するクエリ。

filter

(必須)クエリの各行と共に呼び出す関数。ブール値を返します。

parallel

(オプション)並列プログラミングを有効にする場合は true を指定します。

maxThreadCount

(オプション)この関数が実行できるスレッドの数です。スレッド数は 1~50 にする必要があります。値が 50 を超えると、例外が発生します。

例1

<cfscript>
myQuery = queryNew("id,name,amount","Integer,Varchar,Integer",
[
{id=1,name="One",amount=15},
{id=2,name="Two",amount=18},
{id=3,name="Three",amount=32},
{id=4,name="Four",amount=27},
{id=5,name="Five",amount=43},
{id=6,name="Six",amount=71}
]);
filteredQuery=QueryFilter(myQuery,function(obj){
return obj.amount>=30
})
writeOutput("The filtered query is:")
writeDump(filteredQuery)
</cfscript>
<cfscript> myQuery = queryNew("id,name,amount","Integer,Varchar,Integer", [ {id=1,name="One",amount=15}, {id=2,name="Two",amount=18}, {id=3,name="Three",amount=32}, {id=4,name="Four",amount=27}, {id=5,name="Five",amount=43}, {id=6,name="Six",amount=71} ]); filteredQuery=QueryFilter(myQuery,function(obj){ return obj.amount>=30 }) writeOutput("The filtered query is:") writeDump(filteredQuery) </cfscript>
<cfscript>
    myQuery = queryNew("id,name,amount","Integer,Varchar,Integer", 
                [ 
                        {id=1,name="One",amount=15}, 
                        {id=2,name="Two",amount=18}, 
                        {id=3,name="Three",amount=32},
                        {id=4,name="Four",amount=27},
                        {id=5,name="Five",amount=43},
                        {id=6,name="Six",amount=71}
                ]);
    filteredQuery=QueryFilter(myQuery,function(obj){
       return obj.amount>=30
    })
    writeOutput("The filtered query is:")
    writeDump(filteredQuery)
</cfscript>

例 2

<cfscript>
qoptions = {result="myresult", datasource="cfbookclub", fetchclientinfo="yes"};
sampleQuery = QueryExecute("select * from books order by bookid", [] ,qoptions);
function filterQuery(any Obj){
return (Obj.ISSPOTLIGHT == "Y" ? true : false);
}
WriteDump(QueryFilter(sampleQuery, filterQuery));
</cfscript>
<cfscript> qoptions = {result="myresult", datasource="cfbookclub", fetchclientinfo="yes"}; sampleQuery = QueryExecute("select * from books order by bookid", [] ,qoptions); function filterQuery(any Obj){ return (Obj.ISSPOTLIGHT == "Y" ? true : false); } WriteDump(QueryFilter(sampleQuery, filterQuery)); </cfscript>
<cfscript>
               qoptions = {result="myresult", datasource="cfbookclub", fetchclientinfo="yes"};
               sampleQuery = QueryExecute("select * from books order by bookid", [] ,qoptions);
               
               function filterQuery(any Obj){
                              return (Obj.ISSPOTLIGHT == "Y" ? true : false);
               }

               WriteDump(QueryFilter(sampleQuery, filterQuery));
</cfscript>

このスクリプトは、クエリをフィルターして、IsSpotlight が「Y」であるオブジェクトを表示します。このスクリプトは、構造体の配列を返します。 

メンバー関数の使用例

<cfscript>
myResult=QueryExecute("SELECT * FROM EMPLOYEES",[],{datasource="cfdocexamples"});
// Execute the function to call each row and return the values of Location
status=myResult.filter(function (city){
return (city.LOCATION=="Newton" || city.LOCATION=="San Francisco" ? true : false);
});
// Display the values that meet the filter requirements
WriteDump(status);
</cfscript>
<cfscript> myResult=QueryExecute("SELECT * FROM EMPLOYEES",[],{datasource="cfdocexamples"}); // Execute the function to call each row and return the values of Location status=myResult.filter(function (city){ return (city.LOCATION=="Newton" || city.LOCATION=="San Francisco" ? true : false); }); // Display the values that meet the filter requirements WriteDump(status); </cfscript>
<cfscript>
       myResult=QueryExecute("SELECT * FROM EMPLOYEES",[],{datasource="cfdocexamples"});
       // Execute the function to call each row and return the values of Location
       status=myResult.filter(function (city){
             return (city.LOCATION=="Newton" || city.LOCATION=="San Francisco" ? true : false);
                    
       });
       // Display the values that meet the filter requirements
       WriteDump(status);
</cfscript>

並列処理の使用例

<cfscript>
myQuery = queryNew("id,name,amount","Integer,Varchar,Integer",
[
{id=1,name="One",amount=15},
{id=2,name="Two",amount=18},
{id=3,name="Three",amount=32},
{id=4,name="Four",amount=27},
{id=5,name="Five",amount=43},
{id=6,name="Six",amount=71}
]);
t_start=GetTickCount()
QueryFilter(myQuery,function(obj){
return obj.amount>=30
},true,20)
t_end=GetTickCount()
writeOutput("<br>Time taken with 20 threads:" & t_end-t_start)
</cfscript>
<cfscript> myQuery = queryNew("id,name,amount","Integer,Varchar,Integer", [ {id=1,name="One",amount=15}, {id=2,name="Two",amount=18}, {id=3,name="Three",amount=32}, {id=4,name="Four",amount=27}, {id=5,name="Five",amount=43}, {id=6,name="Six",amount=71} ]); t_start=GetTickCount() QueryFilter(myQuery,function(obj){ return obj.amount>=30 },true,20) t_end=GetTickCount() writeOutput("<br>Time taken with 20 threads:" & t_end-t_start) </cfscript>
<cfscript> 
    myQuery = queryNew("id,name,amount","Integer,Varchar,Integer",  
                [  
                        {id=1,name="One",amount=15},  
                        {id=2,name="Two",amount=18},  
                        {id=3,name="Three",amount=32}, 
                        {id=4,name="Four",amount=27}, 
                        {id=5,name="Five",amount=43}, 
                        {id=6,name="Six",amount=71} 
                ]); 
    t_start=GetTickCount() 
    QueryFilter(myQuery,function(obj){ 
       return obj.amount>=30 
    },true,20) 
    t_end=GetTickCount() 
    writeOutput("<br>Time taken with 20 threads:" &  t_end-t_start) 
</cfscript>

並列処理の使用例 - 例 2

<cfscript>
// Install the package derby before executing this code. cfpm>install derby
qoptions = {result="myresult", datasource="cfbookclub", fetchclientinfo="yes"};
sampleQuery = QueryExecute("select * from books order by bookid", [] ,qoptions);
function filterQuery(any Obj){
return (Obj.ISSPOTLIGHT == "Y" ? true : false);
}
t_start=GetTickCount()
QueryFilter(query=sampleQuery, callback=filterQuery,parallel=true,maxthreadcount=20)
t_end=GetTickCount()
writeOutput("<br>Time taken with 20 threads:" & t_end-t_start)
</cfscript>
<cfscript> // Install the package derby before executing this code. cfpm>install derby qoptions = {result="myresult", datasource="cfbookclub", fetchclientinfo="yes"}; sampleQuery = QueryExecute("select * from books order by bookid", [] ,qoptions); function filterQuery(any Obj){ return (Obj.ISSPOTLIGHT == "Y" ? true : false); } t_start=GetTickCount() QueryFilter(query=sampleQuery, callback=filterQuery,parallel=true,maxthreadcount=20) t_end=GetTickCount() writeOutput("<br>Time taken with 20 threads:" & t_end-t_start) </cfscript>
<cfscript> 
    // Install the package derby before executing this code. cfpm>install derby 
    qoptions = {result="myresult", datasource="cfbookclub", fetchclientinfo="yes"}; 
    sampleQuery = QueryExecute("select * from books order by bookid", [] ,qoptions); 
      
    function filterQuery(any Obj){ 
                   return (Obj.ISSPOTLIGHT == "Y" ? true : false); 
    } 
    t_start=GetTickCount() 
    QueryFilter(query=sampleQuery, callback=filterQuery,parallel=true,maxthreadcount=20) 
    t_end=GetTickCount() 
    writeOutput("<br>Time taken with 20 threads:" &  t_end-t_start) 
</cfscript>

並列処理の使用例 - 例 3

<cfscript>
myResult=QueryExecute("SELECT * FROM EMPLOYEES",[],{datasource="cfdocexamples"});
// Execute the function to call each row and return the values of Location
// Display the values that meet the filter requirements
t_start=GetTickCount()
status=myResult.filter(function (city){
return (city.LOCATION=="Newton" || city.LOCATION=="San Francisco" ? true : false);
},true,30)
t_end=GetTickCount()
writeOutput("<br>Time taken with 30 threads:" & t_end-t_start)
</cfscript>
<cfscript> myResult=QueryExecute("SELECT * FROM EMPLOYEES",[],{datasource="cfdocexamples"}); // Execute the function to call each row and return the values of Location // Display the values that meet the filter requirements t_start=GetTickCount() status=myResult.filter(function (city){ return (city.LOCATION=="Newton" || city.LOCATION=="San Francisco" ? true : false); },true,30) t_end=GetTickCount() writeOutput("<br>Time taken with 30 threads:" & t_end-t_start) </cfscript>
<cfscript> 
    myResult=QueryExecute("SELECT * FROM EMPLOYEES",[],{datasource="cfdocexamples"}); 
    // Execute the function to call each row and return the values of Location 
    // Display the values that meet the filter requirements 
    t_start=GetTickCount() 
    status=myResult.filter(function (city){ 
          return (city.LOCATION=="Newton" || city.LOCATION=="San Francisco" ? true : false); 
                   
    },true,30) 
    t_end=GetTickCount() 
    writeOutput("<br>Time taken with 30 threads:" &  t_end-t_start) 
</cfscript>

ヘルプをすばやく簡単に入手

新規ユーザーの場合