Creating Readable SQL Statements In AIR (Adobe Integrated Runtime)

Posted July 24, 2009 at 5:32 PM by Ben Nadel

Tags: Javascript / DHTML

Last night, Jason Dean was awesome enough to hold a Connect session with Andy Matthews, Todd Rafferty, and myself in which he demonstrated how to build an HTML and jQuery powered AIR application. It was an awesome presentation and really opened my eyes. Jason has gotten me totally excited about playing with AIR. One thing that I mentioned during the presentation that I figured I would blog about quickly is a technique that I picked up for formatting structure string data.

For those of you who have followed my blog for any amount of time, you've probably come to realize that I love white space in my code (not my rendered output, my code). Too much of anything is a bad thing, but I find that the right amount of white space leads to superior scanning and readability. This is one of the main reasons that the CFQuery tags rocks the party that rocks the body. When moving to a script-based language, like Javascript or Action Script, often times this kind of readability is hard to keep when dealing with structured string data like a SQL statement.

Luckily, in script-based languages, often times you can use the back slash, "\", to escape special characters. When dealing with string data where the structure is complicated (think SQL statements, think building HTML strings), I like to leverage this feature to escape line breaks within string data:


 
 
 

 
You Can Use The Back Slash To Escape Line Breaks In String Data To Create Structure Character Data (Such As In A SQL Statement).  
 
 
 

As you can see, by placing a back slash as the very last character in a piece of string data, it escapes the line break, allowing the string value to continue on the next line without throwing any unterminated string errors. In doing so, it frees us up to format the string data in a much more human-readable way. Having to tab out the slashes can be annoying; but, the benefit to readability and maintenance is well worth the minor inconvenience.



Reader Comments

Jul 24, 2009 at 6:07 PM // reply »
34 Comments

I don't understand why people don't use whitespace. It makes it so much more eye friendly. I think that my eyes need a break after looking over hundreds of thousands of lines of code, and using whitespace gives it a little bit of a rest instead of it all being clobbered together.

Hey Ben

Have you ever thought about creating a community CMS like joomla? I'm currently working on the Framework for something similar.


Jul 24, 2009 at 9:30 PM // reply »
113 Comments

@Ben, that seems just .. ew.

Any time you want to modify your statement in one small place (another join and another selected field?), you have to go through and re-align the whitespace every single line of that statement.

And ... while I'm agnostic on the lovely tabs-vs-spaces holy war ... I find that I only like tabs when they are used to align the *beginnings* of lines, and very much dislike them when they are used to align characters within lines. Tab has no definite width whatsoever, and changes from editor to editor. Some editors show it as two, some as three, some as four, some as eight spaces. Unless someone else is using a compatible editor, the alignment will be very off for him. And then if he is also working on the project, sooner or later you both will end up with horribly misaligned blocks of code all over the place.

So, I don't think I could use that style.

In further news, I tend to prefer using a function to generate SQL statements in all the trivial cases. Merging multiple languages into the same code at the same time (a single page with all CF, SQL, HTML, CSS, JS) seems to me to be a very bad habit with adverse outcomes in all but the simplest cases: it's ok to have a 1000 line application with mixed languages in fifteen files, but it's not a good idea to have a 1,000,000 line application with mixed languages all across 5,000 files.

The function to generate a SQL statement for an insert is usually rather trivial. Plus, if you implement a provider model, you could easily write some simple providers for different SQL dialects (Oracle, MySQL, PostgreSQL, SQL Server, Informix, DB2, SQLite, Firebird, Apache Derby, etc.). The provider interface would be the same across all providers, but each provider would emit slightly different SQL depending on the database (e.g. how to quote names, how to quote values, any differences in statement syntax, etc.). You could select which provider to use once in your application, write all of your trivial SQL queries and statements against that provider model, and only use native SQL when you need to because you can't come up with a good way to implement what you need in a generic way. You could then develop most of your application against an SQLite database, while using an expensive Oracle cluster in test and production, using exactly the same function calls in most places to generate the SQL that you need.


Jul 26, 2009 at 4:26 PM // reply »
65 Comments

@Jody,

Regarding the community CMS, have you looked at Mura CMS? It's all on top of ColdFusion: http://www.getmura.com/


Jul 27, 2009 at 8:36 AM // reply »
10,640 Comments

@Jody,

I have not thought about this, at least not much. But @Steve is right, Mura CMS looks really awesome; they came to our CFUG this past meeting to present and it looks like a really powerful app.

@Justice,

You make a good point about the spacing of tabs across various editors. I suppose in that case, you could simply put the back slash after the last character on the line (after a space) rather than tabbing it out. It might be a bit more distracting that way, but the updates would be much easier.

As far as taking this one tiny concept and raising the question of a whole SQL creation framework, I like your big thinking :) However, I think we can all agree that creating abstract factories to create our SQL statements is probably fodder for another blog post (probably even a book) unto itself.


Aug 17, 2009 at 8:49 PM // reply »
1 Comments

Spotted the 'Mura' mention and wanted to give it a quick thumbs up. I'm more designer than programmer but have really come to enjoy the power and ease of use of this CMS. They've added an architecture for plugins and I am looking forward to seeing what evolves.


Mar 22, 2010 at 5:31 PM // reply »
1 Comments

The only language that i have ever seen that gets the treatment of SQL right is CFML! Long live CFQUERY!


Mar 22, 2010 at 5:35 PM // reply »
10,640 Comments

@Patrick,

Ha ha, word up - ColdFusion CFQuery tag is just awesome.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »
Feb 9, 2012 at 10:29 PM
Learning ColdFusion 9: Application-Specific Data Sources
@Ben, No offence, but if people were really wanting advanced features they would be using a platform like ASP.NET MVC. CFML is so structurally compromised as a tag-based scripting language that ... read »
Feb 9, 2012 at 10:03 PM
Subversion - Cleanup Failed To Process The Following Paths
@Leviaguirre, do you still have problems with this? ... read »