Chris Brandt
Chris Brandt

Reputation: 948

How can you get a list of the datasources used to generate a page in coldfusion?

When you add a debug IP to review the debug information from the collection of templates that are parsed to present the page, it includes a list of all queries from that page.

Is it possible to get access to that object for inspection?

I'm looking at trying to automate the documentation which data sources are in use for which page requests. We have a large number of small web apps that access different databases and have different dependencies. I'm hoping to find a way to automate the documentation of these dependencies rather than having to manually review all code for all of the webapps.

Not sure if the object doesn't get created until after the page creation is too far gone to actually do anything with the data, but who knows...

Upvotes: 0

Views: 2063

Answers (3)

John Bartlett
John Bartlett

Reputation: 29

For anyone stumbling across this....

If your [cfroot]/cfusion/lib/neo-datasource.xml file is WDDX encoded and you're not sandboxed, you can use the following (tested on CF2021)

<cflock type="readonly" scope="Server" timeout="5">
    <CFSET LibPath=Server.System.Properties["coldfusion.libPath"]>
</cflock>
<CFFILE action="Read" file="#LibPath#/neo-datasource.xml" variable="DatasourcesWDDX">
<cfwddx action="wddx2cfml" input="#DatasourcesWDDX#" output="Datasources">

<cfoutput>#StructKeyList(Datasources[1])#</cfoutput>
<cfdump var=#Datasources#>

The first position of the Datasources array holds a structure containing information on each configured datasource with the main key being the name of the datasource.

Upvotes: 1

Adrian Wright
Adrian Wright

Reputation: 277

Here's an idea that'll work for each application which uses an Application.cfc.

  1. Enable Request Debugging Output in CF Administrator.
  2. Configure Debugging IP Addresses so that every page receives debugging information.
  3. Assuming that Select Debugging Output Format is set to classic.cfm, short circuit {cfusion 10 home}\cfusion\wwwroot\WEB-INF\debug\classic.cfm by making <cfreturn> the first executable statement in classic.cfm. This will prevent any pages from seeing the debug output.
  4. In Application.cfc::OnRequestEnd() do what Scott Jibben suggested. You can wrap Scott's idea in an <cfif IsDebugMode()>.

Upvotes: 0

Scott Jibben
Scott Jibben

Reputation: 2287

Here is a snippet of code that you can add to the end of your template to get a list of datasources used on the page:

<cfobject action="CREATE" type="JAVA" class="coldfusion.server.ServiceFactory" name="factory">
<cfset cfdebugger = factory.getDebuggingService()>
<cfset qEvents = cfdebugger.getDebugger().getData()>

<cftry>
    <cfquery dbtype="query" name="cfdebug_qryDSN">
        SELECT DISTINCT DATASOURCE FROM qEvents WHERE type = 'SqlQuery'
    </cfquery>
    <cfcatch type="Any">
        <cfset cfdebug_qryDSN = queryNew('DATASOURCE')>
    </cfcatch>
</cftry>

<cfdump var="#cfdebug_qryDSN#" label="cfdebug_qryDSN">

PS: most of the inspiration for this snippet came from {cfusion 10 home}\cfusion\wwwroot\WEB-INF\debug\classic.cfm. You can get some good ideas on how to gain access to debugger objects/data from this file.

Upvotes: 2

Related Questions