Skip to content

Instantly share code, notes, and snippets.

@guitrspaz
Last active July 25, 2017 17:55
Show Gist options
  • Save guitrspaz/67abcabdaff4edc014b7a98792be64c6 to your computer and use it in GitHub Desktop.
Save guitrspaz/67abcabdaff4edc014b7a98792be64c6 to your computer and use it in GitHub Desktop.
Query function to return a boolean value. Example use case: find if item exists.
<snippet>
<content><![CDATA[/**
* @name: ${TM_FILENAME/(.+)\..+|.*/$1/:name}.${1:functionName}
* @hint: ${2:Description}
* @returns: Boolean for whether a record exists
* @date: ${3:Date}
* @author: $TM_FULLNAME ($TM_EMAIL)
*/
${4:public} Boolean function ${1:functionName}(
${5}
){
var errorStruct={
arguments:arguments,
logType:"warning",
start:Now(),
stackTrace:getStackTrace(),
sql:{
string:"",
prefix:{}
},
result:0
};
try{
transaction action="begin" isolation="read_uncommitted" {
var queryService=new Query(
name="q${1}",
datasource=this.getdsn()
);
savecontent variable="errorStruct.sql.string"{
WriteOutput('
${6}
');
}
var queryData=queryService.execute(sql=errorStruct.sql.string);
errorStruct.sql.prefix=queryData.getPrefix();
}
transaction action="commit";
if( structKeyExists(errorStruct.sql.prefix,'RECORDCOUNT')
&& isValid('numeric',errorStruct.sql.prefix.RECORDCOUNT)
&& errorStruct.sql.prefix.RECORDCOUNT
){
errorStruct.result=1;
}
errorStruct.logType="information";
} catch( Any e ){
transaction action="rollback";
errorStruct.logType="error";
errorStruct.cfcatch=e;
}
errorStruct.end=Now();
errorStruct.diff=DateDiff('s',errorStruct.start,errorStruct.end);
if( errorStruct.logType!='information' || this.getdebugMode() ){
createLog(
logName=this.getdefaultLog(),
logType=errorStruct.logType,
functName=this.getcaller()&".${1:functionName}()",
args=errorStruct
);
}
return errorStruct.result;
}
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>dao.boolean</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope>source.python</scope> -->
</snippet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment