CFLib.org – Common Function Library Project

collectionExists(collection)

Last updated March 10, 2006

Version: 2 | Requires: ColdFusion MX | Library: UtilityLib

 
Rated 0 time(s). Average Rating: 0

Description:
Call this function, passing in a collection name, to see if that named Verity colleciton exists. The previous version of this UDF used the <cfcollection action="list" /> tag to determine if the collection exists. The problem is, this action takes a very long time to return a result if there are a lot of collection registered (appx 1 second for every 3 collections.)

Return Values:
Returns a boolean.

Example:

view plain print about
<cfif NOT collectionExists("catalog")>
    <cfcollection
        action="create"
        collection="catalog"
        path="#pathToVerityCollection#"
        language="english"
            />

</cfif>

Parameters:

Name Description Required
collection Name of collection Yes

Full UDF Source:

view plain print about
<!---
 Call this function, passing in a collection name, to see if that named Verity colleciton exists.
 Version 1 by Pete Ruckelshaus, pruckelshaus@yahoo.com
 Raymond Camden modified version 2 a bit.
 
 @param collection      Name of collection (Required)
 @return Returns a boolean. 
 @author Dan G. Switzer, II (pruckelshaus@yahoo.comdswitzer@pengoworks.com) 
 @version 2, March 10, 2006 
--->

<cffunction name="collectionExists" returnType="boolean" output="false" hint="This returns a yes/no value that checks for the existence of a named collection.">
    <cfargument name="collection" type="string" required="yes">

    <!---// by default return true //--->
    <cfset var bExists = true />
    <cfset var searchItems = "">
    
    <!---// if you can't search the collection, then assume it doesn't exist //--->
    <cftry>
        <cfsearch
            name="searchItems"
            collection="#arguments.collection#"
            type="simple"
            criteria="#createUUID()#"
            />

        <cfcatch type="any">
            <!---// if the message contains the string "does not exist", then the collection can't be found //--->
            <cfif cfcatch.message contains "does not exist">
                <cfset bExists = false />
            <cfelse>
                <cfrethrow>
            </cfif>
        </cfcatch>
    </cftry>

    <!---// returns true if search was successful and false if an error occurred //--->
    <cfreturn bExists />

</cffunction>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
11 day(s) ago

Will Belden Will Belden added
longTime
17 day(s) ago

James Sleeman James Sleeman added
quickSort
27 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
30 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson