| « Color Me Stupid | Who Needs Connectivity? I Have Service! » |
Data-driven applications need to generate SQL from time to time. Usually, we leverage things like stored procedures or ORM tools to keep our code sane, but from time to time, we might hard code our SQL statements. You sacrifice some flexibility for some transparency into what your code actually does to the database.
Sometimes, however, writing SQL is just too darn readable. Adam’s co-worker has a better way:
private static String getItemInsertSql() { StringBuilder sb = new StringBuilder(); sb.append(INSERT); sb.append(RSS_ITEM_TABLE_NME); sb.append(OPEN_PAREN); sb.append(NME_PUBDATE_COLUMN); sb.append(COMMA); sb.append(RECORDING_ID_COLUMN_NAME); sb.append(COMMA); sb.append(CHANNEL_ID_COLUMN_NAME); sb.append(COMMA); sb.append(NAME_OF_ENABLED_COLUMN); sb.append(COMMA); sb.append(TITLE_COLUMN); sb.append(COMMA); sb.append(SUBTITLE_COLUMN); sb.append(COMMA); sb.append(DESCRIPTION_COLUMN_NAME); sb.append(COMMA); sb.append(AUTHOR_NAME_COLUMN); sb.append(COMMA); sb.append(KEYWORDS_COLUMN); sb.append(CLOSE_PAREN); sb.append(VALUES); sb.append(OPEN_PAREN); sb.append(NOW); sb.append(COMMA); sb.append(QUESTION); sb.append(COMMA); sb.append(QUESTION); sb.append(COMMA); sb.append(QUESTION); sb.append(COMMA); sb.append(QUESTION); sb.append(COMMA); sb.append(QUESTION); sb.append(COMMA); sb.append(QUESTION); sb.append(COMMA); sb.append(QUESTION); sb.append(COMMA); sb.append(QUESTION); sb.append(CLOSE_PAREN); return sb.toString();
|
Not enterprisey enough! It should make a call over the Enterprise Service Bus to a web service to do the construction of the queries to look up the constants for concatenating to make the query string. Like that it'll be fully future-proof.
(And if it's like real enterprise-grade software, it'll have the host IP address of the web service encoded as a number directly in the code. Maybe the MAC address too. It's not enterprisey if it isn't insanely fragile and sclerotic…) |
|
I'm surprised that no one has pointed out that the the StringBuilder and append calls are a complete waste of CPU and memory. In Java, concatenation of String literals occurs at compile time. Instead of being stupid and inefficient, this method could at least be stupid and efficient.
|
| « Color Me Stupid | Who Needs Connectivity? I Have Service! » |