Dave Merrill
02/21/2023, 3:08 PMwebsolete
02/21/2023, 3:22 PMwebsolete
02/21/2023, 3:23 PMPatrick
02/21/2023, 3:57 PMthisOldDave
02/21/2023, 4:52 PMcomponent rest="true" restPath="/datasource"{
/**
* Verify one or all datasource connections defined in the administrator or datasources.yml
*
* @dsn string default "all" the datasource name for the test
* @output false
* @return struct a json structure containing each datasource with the connection status any error message returned during the test and the timing of the test
*/
remote struct function verify(required string dsn restargsource="Path") httpmethod="GET" produces="application/json" restpath="verify/{dsn}"{
var result = {"success"=true};
local.pc = getPageContext();
local.dsm = local.pc.getDataSourceManager();
local.amd = getApplicationMetadata();
result["result"] = {};
local.dataSourceArray = StructKeyArray(local.pc.getConfig().getDatasourcesAsMap());
if (structKeyExists(local.amd, "datasources")){
local.dataSourceArray.addAll(StructKeyArray(local.amd.datasources));
}
if( arguments.dsn == "all"){
for( var key in local.dataSourceArray){
structInsert(result.result, key, verifydsn(local.pc, local.dsm, key, local.amd));
}
} else if (local.dataSourceArray.containsNoCase(dsn)){
structInsert(result.result, dsn, verifydsn(local.pc, local.dsm, arguments.dsn, local.amd));
} else {
result.result[dsn] = {"con" = false,"message" = "no datasource definition"};
result.success=false;
}
return result;
}
/**
* helper function verifies the connection to a datasource
*
* @pageContext the coldfusion page context
* @manager the datasource manager
* @dsn the datasource to verify
* @metadata the application metadata
* @output false
* @return struct containing the connection status elapsed time and any message generated during the test
*/
private struct function verifydsn(required any pageContext, required any manager, required string dsn, required struct metadata){
var db = {}
if (structKeyExists(metadata.datasources, dsn)){
db = metadata.datasources[dsn]
} else {
var ds = pageContext.getConfig().getDatasource(dsn);
db.username = ds.getUsername();
db.password = ds.getPassword();
}
local.start = gettickcount();
try{
manager.releaseConnection(pageContext, manager.getConnection(pageContext, dsn, db.username, db.password));
return {"con" = true,"message" = "connection OK", "time" = getTickCount() - local.start}
} catch (any e){
return {"con" = false,"message" = e.message, "time" = getTickCount() - local.start};
}
}
}Dave Merrill
02/21/2023, 5:28 PMDave Merrill
02/21/2023, 5:42 PMPatrick
02/21/2023, 5:57 PMDave Merrill
02/21/2023, 7:26 PMPatrick
02/21/2023, 7:28 PMEvil Ware
02/21/2023, 8:48 PM