For the purpose of building a database system I am using a simple builder to generate selection query based on user choices. It has a couple of booleans and then it progresses as follows
StringBuilder builder = new StringBuilder();
builder.append("SELECT ");
if(addOpen)
builder.append("Open ");
if(addHigh)
builder.append("High ");
if(addLow)
builder.append("Low ");
if(addSettle)
builder.append("Settle ");
builder.append("FROM " + tableName);
Now, my problem is trivial – I need to include commas but if I include a comma there must be a value coming after it, so I cannot do Open, or Open, Close, etc. Is there a neat solution to this trivial, yet surprisingly hard for me problem?
The trick I use (in a generic semi-untyped pseudo-code) is:
An alternative I’ve seen is to always include the comma (or comma space) after the terms, but trim the last two characters off the value before adding the FROM clause. Your choice…the ‘pad’ technique works even if you’re doing output and can’t undo an append.