Autogenerated CFCs

Current setup...

I'm on MSSQL Server and autogenerate my bean, DAO, and Gateway with the wizard in Adobe's CF extensions. A lot of the code it generates is crap, but still it saves me a LOT of time and keeps some level of consistency to my code.

On an insert, the generated code puts a cftransaction around the insert, then immediately queries the record to get the autogenerated ID. With MSSQL I was able to toss something like the below immediately after the insert statement, in the same cfquery block, to get the auto ID (as queryName.AutoID).

select @@identity as AutoID

Problem is that we might be moving to Oracle, and this code is MSSQL-specific. Crap. But CF8 adds a bunch of data to a query's result structure, namely it'll return the auto generated ID of an inserted row. Problem is that his is result_name.IDENTITYCOL for MSSQL, result_name.ROWID for Oracle, etc. So I made a new application scope variable for the database type and another to tell me what the field in the result structure will be.

<cffunction name="OnApplicationStart">
   <cflock scope="application" type="exclusive" timeout="10">
      <cfscript>
         application.dsn = '$DSN';
         application.dbid = '$DBID';
         application.dbpw = '$DBPW';
         
         application.dsnAttributes = {
            datasource = application.dsn,
            username = application.dbid,
            password = application.dbpw
            };
         
         application.dbServer = "SQL"; /* SQL=IDENTITYCOL | ORACLE=ROWID */
         if( application.dbServer eq "SQL" )
            application.insertResultID = "IDENTITYCOL";
         else if( application.dbServer eq "ORACLE" )
            application.insertResultID = "ROWID";
      </cfscript>
   </cflock>
</cffunction>

So now after a cfquery insert, with result="rCreate", I can set the auto-gen ID in the bean that was passed in, before I pass it back out. If the whole app moves to Oracle, I'll change one variable in Application.cfc and boom, done.

<cfscript>
   arguments.bean.setAOID( Evaluate("rCreate.#application.insertResultID#") );
</cfscript>
<cfreturn arguments.bean />

Comments
I posted a UDF to CFLib a while back that you may find helpful too - you pass it the result struct and it returns the generated key...

http://cflib.org/udf/getGeneratedKey
# Posted By todd sharp | 6/25/08 1:34 PM
The other benefit is that there is no need to store the DB type anywhere - it just looks to see what key was returned.
# Posted By todd sharp | 6/25/08 1:35 PM
Add to the "code is crap" comment: @@identity should never be used! It can return the _wrong_ Id entirely....worth a Googling.

SCOPE_IDENT() is the proper thing to use.
# Posted By Joe Rinehart | 6/25/08 1:58 PM
Have you ever checked out my Illudium PU-36 Code Generator? http://code.google.com/p/cfcgenerator

1) It doesn't generate crap code
2) the code it does generate is completely customizable via the XSL or CFML templates
3) It is free and open source and you can do whatever you will with the code
4) Its got a cooler name than ColdFusion 8 Exlipse Extensions (which fwiw shows a complete lack of creativity).
5) See points 1 through 4

Convinced? ;)
# Posted By Brian Rinaldi | 6/25/08 2:11 PM
@Brian - Yes and I love it. Unfortunately at work, our development environment is like a shared host and we don't have access to the admin, and my laptop is a POS that can't run CF locally without melting, so I can't use PU-36 while at work, /cry. My team's working on getting its own CF server, I'll be sure that's set such that Illudium can work. I use it all the time for projects at home though!! Certainly a big problem I have with the eclipse generators is that the code seems to be coming out of a black box. Illudium you just make more templates and plop them in place, weee.

@Todd - Brilliant CFC! Guess I should look around online more first, eh? Even better to not have to set the result value anywhere. And no ugly Evaluate() syntax. I can see this being extra great if I have MSSQL and Oracle calls in the same app.

@Joe - I didn't know this! I found the @@identity code a long while back and never hit a problem with it. But then, I generally don't do much with the bean once I get it back. I'll stop using it, using Todd's CFC instead, and read up on why I've been bad.

@All - Like Digg, here's proof that all the good content is in the comments, not the post!
# Posted By Murloc Oracle | 6/25/08 3:22 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.002. Contact Blog Owner