gpt4 book ai didi

ColdFusion 在基于脚本的 cfc 函数中使用 if 条件

转载 作者:行者123 更新时间:2023-12-04 01:00:09 24 4
gpt4 key购买 nike

我正在努力提高我的 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>

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 数据。 :-)

关于ColdFusion 在基于脚本的 cfc 函数中使用 if 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54693519/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com