ColdFusion9 and scripted components and hql syntax - coldfusion

How can this be rewritten using the new scripting syntax along with the hibernate query language in CF9?
<cfcomponent output="no">
<cffunction name="Login" output="no">
<cfargument name="UsrName">
<cfargument name="UsrPassword">
<cfquery name="local.qry">
SELECT * FROM UsrView
WHERE UsrName = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.UsrName#">
AND UsrPassword = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.UsrPassword#">
</cfquery>
<cfreturn local.qry>
</cffunction>
</cfcomponent>

Is this what you are after?
component{
public query function Login(UsrName,UsrPassword){
// get array of entities matching filter
var arrayOfUsers = EntityLoad('User',{
UsrName=arguments.UsrName, UsrPassword=arguments.UsrPassword
});
// convert entity array to a query object
return EntitytoQuery(arrayOfUsers);
}
}

Related

Coldfusion and Related Selects

For several years, I have used dropdowns to determine the requirements for a SQL query and have never needed to use dropdowns with ‘related selects’ until now. I found a sample on how to do this and I can create related selects (with static data) which works well, see below using my data relating to vehicles and makes.
However, when I come to apply this to my environment, and use dynamic SQL query data, I cannot get it to work. I am confident that all the pieces of code are working together (as it works perfectly for the static data), but if I try and build the data automatically and then create the string to pass to the queryNew function, it fails.
This is the working Content.cfc file with STATIC query strings/data array
<cfcomponent>
<cfset tblMake = queryNew("name,id", "varchar,varchar", [{name:'RENAULT',id:'RENAULT'},{name:'RENAULT',id:'RENAULT'},{name:'RENAULT',id:'RENAULT'}])>
<cfset tblModel = queryNew("name,code,continent_id", "varchar,varchar,varchar", [{name:"TSERIES",code:"TSERIES",continent_id:"RENAULT"},{name:"MASTER",code:"MASTER",continent_id:"RENAULT"}])>
<cfset tblVoltage = queryNew("name,code", "varchar,varchar", [{name:"24 volt",code:"MASTER"},{name:"12 volt",code:"TSERIES"}])>
<cffunction name="getContent" access="remote" returntype="query" output="true">
<cfargument name="strTableName" type="string" required="true">
<cfargument name="strID" type="string" required="true">
<cfargument name="strName" type="string" required="true">
<cfargument name="intDistinct" type="numeric" required="false" default="0">
<cfargument name="selectedCol" type="string" required="false" default="0">
<cfargument name="selectedID" type="string" required="false" default="0">
<cfquery name="qryContent" dbtype="query">
select
<cfif arguments.intDistinct eq 1>distinct</cfif>
#arguments.strID# as theID,
#arguments.strName# as theValue
from #arguments.strTableName#
<cfif arguments.selectedID neq 0>
where #arguments.selectedCol# = '#arguments.selectedID#'
</cfif>
order by #arguments.strName#
</cfquery>
<cfreturn qryContent />
</cffunction>
</cfcomponent>
When I try and create the query using this method, nothing appears to work. I am looping through the database (from a query) and then creating a string using the required structure and the passing it to the function.
<cfcomponent>
<cfset XSTATIC = ""/>
<cfquery name="NOXVehicleModels" datasource="EBSNOX" >
SELECT DISTINCT VehicleMake
FROM [dbo].[NOX-Master]
WHERE VehicleMake IS NOT NULL
</cfquery>
<cfloop query="NOXVehicleModels">
<cfset XSTATIC = XSTATIC & "{name:'" & #Trim(NOXVehicleModels.VehicleMake)# & "',id:'"& #Trim(NOXVehicleModels.VehicleMake)# & "'},"/>
</cfloop>
<cfset XLEN=LEN(#XSTATIC#)/>
<cfset XSTATIC = MID(XSTATIC,1,XLEN-1)/>
<cfoutput>#XSTATIC#</cfoutput>
<cfset tblMake = queryNew("name,id", "varchar,varchar", [#XSTATIC#])>
I have created a separate cfoutput to test the string for structure etc and it appears to be correct, but it is just not passing to the querynew function. This is what the output looks like:-
{name:'CUMMINS',id:'CUMMINS'},{name:'DAF',id:'DAF'},{name:'IVECO',id:'IVECO'},{name:'MAN',id:'MAN'},{name:'MERCEDES',id:'MERCEDES'},{name:'RENAULT',id:'RENAULT'},{name:'SCANIA',id:'SCANIA'},{name:'VOLVO',id:'VOLVO'}
{name:'RENAULT',id:'RENAULT'},{name:'RENAULT',id:'RENAULT'},{name:'RENAULT',id:'RENAULT'}
What I have done so far:-
I have checked that the quotes are not important i.e. single/double.
I have tried to build the query outside of the tag.
Again, all appears (?) to be in order and I cannot understand that could be the problem. Possibly the structure is missing something before parsed to the function?
Any help would be greatly appreciated.
Thanks,
Jack

ColdFusion session variables not setting correctly

I have a few session variables I need setup for permissions and only the userID, userName and sessionAdmin variables are making it through. I do a cfdump and see all of the variables set to 0, which is the cfparam default. I also can not get the page to redirect to my sample address.
login.cfm
<cfif isDefined("FORM.login")>
<cfset Encryptpwd = Encrypt(FORM.password, application.PsswrdKy)>
<cfset loginInfo = CFCmain.getLogin(FORM.username,Encryptpwd)>
<cflock timeout=20 scope="session" type="exclusive">
<cfset session.UserName = loginInfo.username>
<cfset session.userid = loginInfo.id>
<cfset session.Access_admin = loginInfo.superadmin>
<cfset session.Access_var1 = loginInfo.var1>
<cfset session.Access_var2 = loginInfo.var2>
<cfset session.Access_var3 = loginInfo.var3>
<cfset session.Access_var4= loginInfo.var4>
<cfset session.Access_agency = loginInfo.agency>
</cflock>
<cflocation url="http://www.google.com">
</cfif>
and main.cfc where my variables are pulled from the db
<cffunction name= "getLogin" access="remote" returntype="any" >
<cfargument name="uname" type="string">
<cfargument name="pwd" type="string">
<cfquery name="getdata" datasource="#application.db#">
select * from users2 where username = '#arguments.uname#' and password = '#arguments.pwd#'
</cfquery>
<cfreturn getdata>
</cffunction>
For me it looks like the variable FORM.login is not defined!?
try:
<cfif isDefined("FORM.login")>
<cfdump var="hello">
<cfabort>
....
if you don't see the "hello", FORM.login is not defined...
try also:
<cfdump var="#form#" abort>
to see what is defined in the form scope.
And how others said, use cfqueryparam!! (security)
<cfquery name="getdata" datasource="#application.db#">
select *
from users2
where username = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.uname#">
and password = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.pwd#">
</cfquery>

How to run a cfquery within the onRequestStart() function?

I want to run a query on every page that a user requests. This query is needed to get back preferences set by the user's organisation for the application. This is what I have tried:
<cffunction name="onRequestStart" access="public" returntype="boolean">
<cfargument type="String" name="TargetPage" required="true"/>
<cfquery name="rsSettings">
SELECT *
FROM
dbo.Settings
</cfquery>
<cfreturn true>
</cffunction>
</component>
However each pages that looks for the rsSettings recordset says that its not defined. If I put the same query within each page that needs it then it works fine.
Does onRequestStart() not handle cfquery?
<cfquery name="request.rsSettings">
SELECT *
FROM
dbo.Settings
</cfquery>
Then in the page use:
request.rsSettings.columName

Access function arguments from another function

I am trying to create a custom debug tool and I need to use a component with two separate functions in it. The first function (startTimer) has some arguments such as startCount and the other one (endTimer) has endCount. What I am trying to accomplish is something like the following code:
<cffunction name="startTimer" access="public" returntype="void">
<cfargument name="startActionTime" type="string" required="no">
</cffunction>
<cffunction name="endTimer" returntype="void" access="public">
<cfargument name="endActionTime" type="string" required="no">
<cfset finalTime = endActionTime - startTimer.startActionTime>
<!---Some SQL will go here to record the data in a db table --->
</cffunction>
And this is how I am calling the function
<cfscript>
location = CreateObject("component","timer");
loc =location.startTimer(
startActionTime = getTickCount()
);
end = location.endTimer(
endActionTime = getTickCount()
);
</cfscript>
I guess I am having scope issues because when I am trying to run the code I am getting an undefined error on startTimer.startActionTime. What is the correct way to do something like this?
You can use the variables scope like so:
<cfcomponent>
<cfset variables.startActionTime = 0>
<cffunction name="startTimer" access="public" returntype="void">
<cfargument name="startActionTime" type="numeric" required="no">
<cfset variables.startActionTime = arguments.startActionTime>
</cffunction>
<cffunction name="endTimer" returntype="string" access="public">
<cfargument name="endActionTime" type="numeric" required="no">
<cfset finalTime = endActionTime - variables.startActionTime>
<!---Some SQL will go here to record the data in a db table --->
<Cfreturn finaltime>
</cffunction>
</cfcomponent>
From the Adobe Docs: Variables scope variables created in a CFC are available only to the component and its functions, and not to the page that instantiates the component or calls its functions.

Coldfusion cfselect bind only works when cfdebug is enabled

I have a cfselect statement that is bound to a remote cfc. It passes the value of another element in the form to the cfc in order to populate the cfselect. I was running this code with cfdebug enabled and it seemed to be working fine, but I found that if I do not have cfdebug enabled nothing is populated. I am not getting any other errors that I know of, and I have tried it in chrome and mozilla - same problem.
Here is my cfselect:
<cfselect name="intlRepID" id="intlRepID" required="yes" value="userID" display="businessName" bind="cfc:nsmg.extensions.components.user.getIntlRepRemote({programID})" bindonload="true" />
Here is my remote function:
<cffunction name="getIntlRepRemote" access="remote" returntype="array" output="false" hint="Gets a list of Intl. Reps. assigned to a candidate">
<cfargument name="programID" default="" hint="Get Intl. Reps. Based on a list of program ids">
<cfscript>
// Define variables
var qGetIntlRepRemote='';
var result=ArrayNew(2);
var i=0;
</cfscript>
<cfquery
name="qGetIntlRepRemote"
datasource="#APPLICATION.DSN#">
SELECT
u.userID,
u.businessName
FROM
smg_users u
INNER JOIN
smg_students s ON s.intRep = u.userID
WHERE
s.companyid = <cfqueryparam cfsqltype="cf_sql_integer" value="#CLIENT.companyid#">
<cfif LEN(ARGUMENTS.programID)>
AND
s.programID IN ( <cfqueryparam cfsqltype="cf_sql_integer" value="#ARGUMENTS.programID#" list="yes"> )
</cfif>
AND
s.active = <cfqueryparam cfsqltype="cf_sql_integer" value="1">
GROUP BY
u.userID
ORDER BY
u.businessName
</cfquery>
<cfscript>
// Add default value
result[1][1]=0;
result[1][2]="---- All - Total of " & qGetIntlRepRemote.recordCount & " International Representatives ----" ;
// Convert results to array
For (i=1;i LTE qGetIntlRepRemote.Recordcount; i=i+1) {
result[i+1][1]=qGetIntlRepRemote.userID[i];
result[i+1][2]=qGetIntlRepRemote.businessName[i];
}
return result;
</cfscript>
</cffunction>