- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在努力提高我的 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="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>
但是,在 cfscript 中尝试类似的方法会引发错误:
public query function querySd( numeric sd_id, string sd_code ){
local.querySd = new Query(datasource = variables.dsn);
if( isDefined('arguments.sd_id') ){
local.querySd.addParam( name = 'sdid', value = arguments.sd_id, cfsqltype = 'cf_sql_int');
};
if( isDefined('arguments.sd_code') ){
local.querySd.addParam( name = 'sdcode', value = arguments.sd_code, cfsqltype = 'cf_sql_varchar', maxlength = '20');
};
local.querySd.setSql('
SELECT sd_id, sd_code, sd_active, sd_expires, sd_added, sd_dla
FROM sd
WHERE 0 = 0
if( isDefined('arguments.sd_id') ){
AND sd_id = :sdid
};
if( isDefined('arguments.sd_code') ){
AND sd_code = :sdcode
};
');
local.qrySd = local.querySd.execute().getResult();
在 cfc 内基于 cfscript 的查询中使用可选参数的正确方法是什么?
最佳答案
正如其他人所说,根据您使用的 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>
注意: queryExecute()
已在 CF11 中添加。 CF2016 中添加了安全导航(?.
)。
编辑:我将 Mockaroo 数据更改为静态查询数据。显然,您可以很快地浏览 Mockaroo 数据。 :-)
关于ColdFusion 在基于脚本的 cfc 函数中使用 if 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54693519/
我正在编写一个我想经常运行的计划任务。 问题是,如果服务器遇到高流量负载,我不希望运行此任务。 除了从 java 获取空闲/总/最大内存之外,还有什么方法可以尝试确定此任务是否应该继续? 最佳答案 G
我知道 coldfusion builder 已发布用于开发 coldfusion 9 应用程序。现在我的问题是 coldfusion builder 是否支持 COLDFUSION 8 开发?非常感
我正在尝试出于学习目的测试 Coldfusion,但我真的不知道非免费企业版添加到免费开发者版的附加功能是什么。 谢谢! 最佳答案 从法律上讲,Enterprise 是供公众消费的,而 Dev 不是。
我一直在玩这个问题一段时间了。我想要实现的是保存到MySQL数据库,然后输出到窗口,一般确认的表情符号如😀😁😂(是的,21世纪问题) 经过大量的修补和遵循本网站上的各种教程和答案之后,我设法实现
嗨伙计!我有一个关于coldfusion用户的简单问题,有没有人知道如何将动态数字转换为小数,例如我有一个代码:#number#,它等于,例如 10 但我需要将它写为 0.10 我该怎么做做吗? 试过
尝试使用方括号表示法来引用动态变量。 (我正在遍历由查询创建的一组产品,为每个与其唯一 SKU 相关联的字段创建字段,如果您对应用程序感到疑惑) 我已将其缩小到这段代码,当我尝试运行它时会抛出“无效表
我刚刚下载了 ColdFusion Builder (CFB),现在我正在尝试编写一个简单的“Hello World”应用程序。但我首先需要某种服务器,不是吗?现在我到底在这里寻找什么? “ColdF
我们正在运行 ColdFusion MX7。 我们遇到的一个问题是,我们在很多页面中都使用了很多功能。将它们放在“全局”ColdFusion 范围内会很好,而不是必须将它们包含在我们所有的页面中。 有
我已经尝试使用 key 工具将证书从 First Data 导入到我的 ColdFusion 9 设置中: keytool -importcert -keystore MYCF9Dir\runtime
在 ColdFusion 组件中,我声明了一个这样的函数: string function render(required Array actions) output=false { //... }
背景 : 我有一个处于设计阶段的新站点,正在考虑使用 ColdFusion。服务器目前正在使用 ColdFusion 和 Python 进行设置(为我完成)。 使用什么是我的选择,ColdFusion
我计划在服务器上从 ColdFusion MX7(Server 2003)迁移到 ColdFusion 11(Server 2012)。有一个其他服务器,我需要从 ColdFusion 8(服务器 2
一些背景知识:我正在从事一个将另一种脚本语言的网络应用程序转换为 ColdFusion 的项目。我遇到的一个问题是旧技术有一些函数名称与 CF 中的函数相同,但工作方式略有不同。不幸的是,在大多数情况
在 this page ,它谈到了 Windows NT、2000、XP 和 2003。幸运的是,我有一台 Windows 7 机器。 第一行说: In User Manager for Domain
ColdFusion 中获取季度第一天和最后一天的最快方法是什么? 似乎没有为此内置功能。 最佳答案 季度第一天: FirstDayOfQuarter = CreateDate(year, (quar
我正在尝试在新的 Windows 服务器 (Windows Server 2012 R2) 上安装 ColdFusion 11(标准版)。安装显然是成功的,但是当我进入管理员安装最新更新时,我不能。当
我有: 服务器详情 服务器产品 ColdFusion 版本 9,0,1,274733 版本标准 操作系统 Windows Server 2008 操作系统版本 6.0 Adobe 驱动程序版本 4.0
我们有一组文件需要 ColdFusion 复制到网络共享。但是,我们无法更改运行 ColdFusion 服务的用户,这意味着 ColdFusion 没有足够的权限访问任何网络共享。我们确实有可以访问的
这对我来说是新的:我正在查看 ColdFusion 网站。 问题是我什至找不到连接字符串。在一些 qryXXX.cfm 文件中,我发现 但我就是找不到这个数据源的存储位置。 最佳答案 数据源在 Co
停止ColdFusion输出的空白的正确方法是什么? 我知道有cfcontent和cfsetting enableCFoutputOnly。正确的方法是什么? 最佳答案 除了,和是。您可以使用它删
我是一名优秀的程序员,十分优秀!