| « Divisive Placeholder | The Russian Plan » |
I can see it now. Everybody will say how the following bit of Java code that Shawn C. has sent in isn't actually a WTF and is, in fact, quite brilliant.
To a point, I agree - the code works, and leaves the door open for accomodating changes to the structure of the SQL queries themselves. Especially if, some day, in a far flung future when, from out of space, a runaway planet comes hurtling between the Earth and the Moon, unleashing cosmic destruction casting civilization into ruin, forcing mankind to re-invent the standard keywords used to retrieve information from databases.
This, dear readers, is what I would consider planning for the future, or at least one of them.
public class DBQueryString
{
protected String mstrSelect;
protected String mstrTable;
protected String mstrWhere;
protected String mstrOrder;
public static final String SELECT = "select ";
public static final String INSERT = "insert into ";
public static final String UPDATE = "update ";
public static final String DELETE = "delete ";
public static final String SET = " set ";
public static final String FROM = " from ";
public static final String WHERE = " where ";
public static final String AND = " and ";
public static final String VALUES = " values ";
public static final String ORDER = " order by ";
[other code snipped]
public String buildQuery()
{
String strQuery = "";
strQuery = SELECT + this.mstrSelect;
strQuery = strQuery + FROM + this.mstrTable;
if (this.mstrWhere != null)
{
strQuery = strQuery + WHERE + this.mstrWhere;
}
if (this.mstrOrder != null)
{
strQuery = strQuery + ORDER + this.mstrOrder;
}
LoggingSystem.getLogger().inform(3, strQuery);
return strQuery;
}
}
Re: Piecemeal SQL
2010-01-18 09:13
•
by
Quastiophor
(unregistered)
|
|
Loading the strings from XML would be ideal, but perhaps this was on an embedded platform where there was no file system.
|
Re: Piecemeal SQL
2010-01-18 09:24
•
by
Anonymous
(unregistered)
|
|
Looks like a previous incarnation of our SQL builder library
|
|
We have lots of code like this in the codebase where I work. There are several advantages:
1. Your IDE doesn't warn you about strings that need localization, since those strings are now constants. 2. You get auto-completion and typo-avoidance. Granted, most of our devs don't write code this way. But it's not wrong. |
|
Here are my results and now I want them in DESCENDING order. Oh Crap!
|
| « Divisive Placeholder | The Russian Plan » |