在基于脚本的cfc函数中使用if条件的ColdFusion
我正在努力提高我的cfscript,但无法理解这一点。在基于标签的CFC中,我可以这样做:在基于脚本的cfc函数中使用if条件的ColdFusion,coldfusion,cfml,Coldfusion,Cfml,我正在努力提高我的cfscript,但无法理解这一点。在基于标签的CFC中,我可以这样做: <cffunction name="querySd" access="public" returnType="query" output="false"> <cfargument name="sd_id" type="numeric" required="No"/> <cfargument name="sd_code" type="string" required="
<cffunction name="querySd" access="public" returnType="query" output="false">
<cfargument name="sd_id" type="numeric" required="No"/>
<cfargument name="sd_code" type="string" required="No"/>
<cfquery name="LOCAL.qrySd" datasource="#variables.dsn#">
SELECT sd_id, sd_code, sd_active, sd_expires, sd_added, sd_dla
FROM sd
WHERE 0=0
<cfif isDefined("ARGUMENTS.sd_id")>
AND sd_id = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#ARGUMENTS.sd_id#"/>
</cfif>
<cfif isDefined("ARGUMENTS.sd_code")>
AND sd_code = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#ARGUMENTS.sd_code#" maxlength="20"/>
</cfif>
</cfquery>
<cfreturn LOCAL.qrySd>
</cffunction>
在cfc内部基于cfscript的查询中,使用可选参数的正确方法是什么?您可以将查询的部分放在变量中,并在查询字符串中使用它
local.queryPart = '';
if( isDefined('arguments.sd_id') ){
local.queryPart &= ' AND sd_id = :sdid ';
};
if( isDefined('arguments.sd_code') ){
local.queryPart &= ' AND sd_code = :sdcode ';
};
local.querySd.setSql('
SELECT sd_id, sd_code, sd_active, sd_expires, sd_added, sd_dla
FROM sd
WHERE 0 = 0
#local.queryPart#
');
也可以使用三元运算符:
local.querySd.setSql('
SELECT sd_id, sd_code, sd_active, sd_expires, sd_added, sd_dla
FROM sd
WHERE 0 = 0
# !isNull( arguments.sd_id ) ? ' AND sd_id = :sdid' : '' #
# !isNull( arguments.sd_code ) ? ' AND sd_code = :sdcode' : '' #
');
就个人而言,我觉得这更具可读性。正如其他人所说,根据您使用的CF版本,我会在新查询上使用queryExecute。原因有很多,而这本身就是一个完全不同的话题 总之,现在我有一分钟的时间,我已经为完整性准备了一个queryExecute示例。注意:我在这里使用的是对模拟数据的查询。真正的查询将使用实际的数据源 注:在CF11中添加了queryExecute。安全航行?。添加于2016年财务报告中
编辑:我将Mockaroo数据更改为静态查询数据。显然,您可以非常快速地浏览Mockaroo数据:- 谢谢-我最终创建了一个查询字符串并在setSql之外构建了它。这与问题无关,但structKeyExist通常比IsDefined更受欢迎,以获得更好的精度。良好的点代码更新-谢谢!什么版本的CF?OT:如果我正在转换,我会使用queryexecute。它有一个更简单的语法。在旧的查询中使用queryExecute也是如此。我不知道为什么有人否决了这个答案。我在字符串构造中一直使用三元运算符,它们在查询构造中工作得非常好。我对此投了赞成票。我认为它更具可读性,但我不喜欢查询字符串内部的字符串逻辑。我不投赞成票,也不投反对票。没过多久,我的莫卡罗津贴就被打爆了。当我到家时,我会将.csv移动到一个可以通过HTTPS连接承载它的地方。这非常有用!Adobe的文档可能非常缺乏,或者有时对用户不友好。在cfscript中有一个包含cfquery示例的git页面-您的示例应该添加到该页面中。@Steve如果您要查找函数信息,请点击CFDocs。它比一些Adobe文档更容易导航。关于CF的最佳信息来源是谷歌。有很多优秀的博主都写过好东西。你只需要注意过时的信息。不幸的是,有很多。而且,作为一名web开发人员,我强烈建议您熟悉OWASP上的内容,尤其是他们的前10条信息。但在标签上看cfscript绝对是一大荣誉-
local.querySd.setSql('
SELECT sd_id, sd_code, sd_active, sd_expires, sd_added, sd_dla
FROM sd
WHERE 0 = 0
# !isNull( arguments.sd_id ) ? ' AND sd_id = :sdid' : '' #
# !isNull( arguments.sd_code ) ? ' AND sd_code = :sdcode' : '' #
');
<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>