Как говорили другие, в зависимости от используемой вами версии CF, я бы использовал queryExecute()
вместо new Query()
.Есть много причин, и это в значительной степени другая тема сама по себе.
Во всяком случае, теперь, когда у меня есть минута, я собрал пример queryExecute()
для полноты. ПРИМЕЧАНИЕ. Я использую запрос к проверенным данным.Реальный запрос будет использовать фактический источник данных.
<cfscript>
public Query function querySd2 ( Numeric sd_id, String sd_code ) {
// This is my fake query data, thanks to Mockaroo.
local.sd = queryNew("sd_id,sd_code,sd_active,sd_expires,sd_added,sd_dla",
"integer,varchar,bit,date,date,varchar",
[
{ "sd_id":1,"sd_code":"DontPickMe","sd_active":true,"sd_expires":"2019-01-04","sd_added":"2018-05-07","sd_dla":"2M66CAf3" } ,
{ "sd_id":2,"sd_code":"PickMe","sd_active":true,"sd_expires":"2018-03-03","sd_added":"2018-08-18","sd_dla":"8FW4HRm8" } ,
{ "sd_id":3,"sd_code":"DontPickMe","sd_active":true,"sd_expires":"2019-01-01","sd_added":"2018-10-28","sd_dla":"4F6kBUm2" } ,
{ "sd_id":4,"sd_code":"PickMe","sd_active":false,"sd_expires":"2018-10-28","sd_added":"2018-08-22","sd_dla":"2NSlNLr8" } ,
{ "sd_id":5,"sd_code":"DontPickMe","sd_active":false,"sd_expires":"2018-03-07","sd_added":"2019-02-09","sd_dla":"8T0cWQc2" }
]);
////////////////
local.sqlWhere = "1=1" ; // This is our default WHERE condition.
local.qryParams = {} ; // queryExecute expects a struct of params. Or an array.
// First, I check that the given args have a length, then create both
// the SQL and the param. Also "?." is the safe-navigation operator, added
// in CF2016. https://helpx.adobe.com/coldfusion/using/language-enhancements.html
if( len(trim(arguments?.sd_id)) ) {
sqlWHERE &= " AND sd_id = :sdid" ; // This is our SQL string.
qryParams.sdid = { value:arguments.sd_id, cfsqltype:"cf_sql_integer" } ;
}
if( len(trim(arguments?.sd_code)) ) {
sqlWHERE &= " AND sd_code = :sdcode" ;
qryParams.sdcode = { value:arguments.sd_code, cfsqltype:"cf_sql_varchar", maxlength:"20" } ;
}
//writeDump(sqlWhere) ;
// https://cfdocs.org/queryexecute
local.qrySd = queryExecute(
"SELECT sd_id, sd_code, sd_active, sd_expires, sd_added, sd_dla FROM sd WHERE #sqlWhere#"
, qryParams
, { dbtype="query"} //datasource="dsn" } // Replace dbtype with datasource.
) ;
return qrySd ; // return our query object.
}
// TESTS
tests = [
{qry:querySd2(2,"PickMe") , label:"Results from query" , retval:querySd2(2,"PickMe").sd_expires } ,
{qry:querySd2(1,"PickMe") , label:"No Results from query" , retval:querySd2(1,"PickMe").sd_expires } ,
{qry:querySd2(1) , label:"No Param2" , retval:querySd2(1).sd_expires } ,
{qry:querySd2(sd_code = "PickMe") , label:"No Param1 (CF2018+ (named params))" , retval:querySd2(sd_code = "PickMe").sd_expires } ,
{qry:querySd2() , label:"No Params" , retval:querySd2().sd_expires } ,
{qry:querySd2(1," ") , label:"Edge. Empty string." , retval:querySd2(1," ").sd_expires }
] ;
//// Note that the above retval:querySd2().sd_expires only outputs one row. Loop
//// through the results themselves to output the multiple rows.
writeDump(tests) ;
</cfscript>
https://cffiddle.org/app/file?filepath=32c93137-adb1-4f58-8ed4-21bb9e5212b2/ee3d9cac-e25e-46ca-8eec-f4ac8ddd4b41/4d295400-65fa-4b76-a889-a97a805409ea.cfm
ПРИМЕЧАНИЕ: queryExecute()
был добавлен в CF11.Безопасная навигация (?.
) добавлена в CF2016.
РЕДАКТИРОВАТЬ: Я изменил свои данные Mockaroo на статические данные запроса.По-видимому, вы можете быстро просматривать данные Mockaroo.: -)