A problem that we need to solve regularly at my workplace is how to build sql statements based on user supplied table/column names. The issue I am trying to address is the commas between column names.
One technique looks something like this.
selectSql = 'SELECT '; for (z = 0; z < columns.size(); z++) { selectSql += columns[z]._name; selectSql += ', '; } selectSql = selectSql(0, selectSql.len() - 2); selectSql += 'FROM some-table';
Another technique looks something like this
selectSql = 'SELECT '; for (z = 0; z < columns.size(); z++) { selectSql += columns[z]._name; if (z < columns.size() - 1) selectSql += ', '; } selectSql += 'FROM some-table';
I am not particularly enthralled by either of these implementations.
I am interesting in hearing ideas for other ways to address this issue, with an eye toward making the code easier to read/understand/maintain.
What alternate techniques are available?
In your case it is probably safe to assume that there is at least one column since otherwise there is no point in doing the select. In that case you could do: