We are gradually migrating over to ColdFusion 9.
With any future development we will use script based code as much as possible.
I've gotten used to it, love the new keyword, love ternary operations and lots more.
Writing my first bit of query code using full script, I was loving it, until that is I got to addParam.
There are two problems with addParam that I have
1. You have to used named parameters, which makes the code overly verbose
2. You have to still prefix the types with cf_sql_ which again is overly verbose.
Here is an example.
So what you can see here is both the named parameters on line 6 and the cf_sql_ on the data type.
Luckily the Query component is written itself in ColdFusion, so you can just change it.
I considered just overwriting the method addParam but thought it might get me into trouble down the path, so I decided to add a new method setParam which is in Query.cfc and looks like this
So as you can see very simple, just has the same parameters, name, value cfsqltype in a specific order. It also prepends the cf_sql_ if it doesn't exist in the type. So my original code can now look like this
Now I'm sure that not everyone will agree, but I don't like writing unnecessarily long verbose code for no good reason
6 comments:
If I'm not mistaken, I believe you can leave off the cf_sql_ part of the type param, and it still works :)
Actually No,
While it appears to work, after a bit of testing it doesn't. Seems like everything goes to varchar, timestamps definatly dont work.
I'm pretty darn sure that ordered parameters work along with named parameters.
Not sure how,
I looked at the addParam method which is in base.cfc and extended from Query, it just takes an attribute collection.
You can use position parameters, this works for me:
q = new query();
q.setDatasource( "cfartgallery" );
q.setSQL( "select * from Art where artistid =:? and issold =:? " );
q.addParam( value=2, cfsqltype="CF_SQL_INTEGER" );
q.addParam( value=1, cfsqltype="CF_SQL_BIT" );
result = q.execute();
writeDump(result);
Not having to use named arguments it nice. Also Adobe should remove the requirement for cf_slqtype
Post a Comment