Skip to content

Instantly share code, notes, and snippets.

@spillsthrills
Last active August 6, 2021 21:50
Show Gist options
  • Save spillsthrills/f6aeb04b558bd3358a710075796db2d6 to your computer and use it in GitHub Desktop.
Save spillsthrills/f6aeb04b558bd3358a710075796db2d6 to your computer and use it in GitHub Desktop.
Query Service Wrapper Proof of Concept
<cfscript>
//try to force update on tryCF
private struct function results( struct optionalReturnKeys={} ) {
var result = {
errors : [],
success : false
};
return result.append(arguments.optionalReturnKeys);
}
private struct function resultsQuery() {
return results(
{
data : {},
recordCount : 0
}
);
}
private struct function resultsQueryPopulate( required string sql, struct params={}, required struct options, errors=[] ) {
var result = resultsQuery();
var qryRes = "";
arguments.options.result="qryRes";
result.errors = arguments.errors;
try {
result.data = queryExecute( arguments.sql, arguments.params, arguments.options );
if ( isQuery(result.data) ) {
result.success = true;
result.recordCount = qryRes.recordCount;
} else {
throw( "Result was not a query.");
}
}
catch(any excpt) {
result.errors.append( excpt.message & " || " & excpt.detail );
result.success = false;
} finally {
return result;
}
}
news = queryNew("id,title", "integer,varchar");
news.addRow({"id"=1,"title"="Dewey defeats Truman"});
sql = "SELECT * FROM news";
params = {};
options = {dbtype="query"};
writeDump(var=results(), label="results raw");
writedump(var=resultsQuery(), label="resutlsQuery raw");
//test with no existing result
writeDump(var=resultsQueryPopulate(sql, params, options), label="resultsQueryPopulate success");
//create existing results like it would be in function
result = results();
result.customKey = "myCustomKey";
// test existing result keys
writeDump(var=result.append(resultsQueryPopulate(sql, params, options)), label="resultsQueryPopulate with existing result");
//error tests
writeDump(var=result.append(resultsQueryPopulate("Bad SQL", params, options)), label="resultsQueryPopulate with error");
result.errors = ["Existing Error!"];
writeDump(var=result.append(resultsQueryPopulate("Bad SQL", params, options, result.errors)), label="resultsQueryPopulate error with existing result error");
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment