CFC Best Practise : Using cfif statements in Where Clause
Hi there
Wondering if anyone can advise on the best route to take for handlings cfcs...
I have a function set up as so
<!--- GRAB SONG INFORMATION --->
<cffunction name="getSong" access="remote" output="false" returntype="query" hint="return song information, filtered by user id">
<!---Optional Artist Name Argument--->
<cfargument name="nameArt" type="string" required="false" hint="artist we want to grab song for">
<!---Filter by user ID--->
<cfargument name="idUsr" type="any" required="false" hint="if provided, filter by user id">
<!---Optional Filter by user name--->
<cfargument name="nameUsr" required="false" type="string" hint="if supplied, filter by user name">
<!---Optional song id argument--->
<cfargument name="idSng" required="false" type="numeric" hint="if supplied, grab information for song with this id">
<!--- grab song from database --->
<cfquery name="song" datasource="#APPLICATION.mx#">
// SELECT WHICHEVER FIELDS NECESARRY
SELECT.....
<!---If user id is supplied--->
<cfif isDefined('ARGUMENTS.idUsr')>
<!---Filter results by user id--->
WHERE s.userId = <cfqueryparam value = '#ARGUMENTS.IdUsr#' cfsqltype='CF_SQL_INTEGER'>
</cfif>
<!---If user name is supplied--->
<cfif isDefined('ARGUMENTS.nameUsr')>
<!---Filter results by user id--->
WHERE s.userId = (SELECT iUserID
FROM users
WHERE name = <cfqueryparam value = '#ARGUMENTS.nameUsr#' cfsqltype='CF_SQL_VARCHAR' maxLength="12">)
</cfif>
<cfif isDefined('ARGUMENTS.idSng')>
WHERE s.iSongID = <cfqueryparam value = '#ARGUMENTS.idSng#' cfsqltype='CF_SQL_INTEGER'>
</cfif>
ORDER BY s.iSongID DESC
</cfquery>
<!---Return query output--->
<cfreturn song>
</cffunction>
Now im wondering, is it best to have multiple cfifs in the same function, filtering data dependant on what arguments are supplied, or is it best to seperate the function into seperate functions with different filters, without the cfifs?
Many thanks
