Skip to content

Instantly share code, notes, and snippets.

@guitrspaz
Last active July 26, 2017 14:49
Show Gist options
  • Save guitrspaz/f51505b03432a1af7c6ff771cea14b68 to your computer and use it in GitHub Desktop.
Save guitrspaz/f51505b03432a1af7c6ff771cea14b68 to your computer and use it in GitHub Desktop.
Creates a query function to return the resulting ID or other numeric value
<snippet>
<content><![CDATA[/**
* @name: ${TM_FILENAME/(.+)\..+|.*/$1/:name}.${1:functionName}
* @hint: ${2:Description}
* @returns: Numeric record ID
* @date: ${3:Date}
* @author: $TM_FULLNAME ($TM_EMAIL)
*/
${4:public} Numeric function ${1:functionName}(
${5}
){
var errorStruct={
'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,'IDENTITYCOL') ){
errorStruct.result=errorStruct.sql.prefix.IDENTITYCOL;
}
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.numeric</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