Hi all,
I have a situation that I believe a pattern can solve and want to really start understanding how to apply these to real-life (my real life) situations.
I have a QueryBuilder class that needs to build a select statement based off of what has content.
Something like
public class QueryBuilder
public String buildQuery(Query query)
{
StringBuffer buffer = new StringBuffer();
buffer.append("select * from some_table ");
if(Util.textHasContent(query.getField1()))
{
buffer.append(" field1 = '" + query.getField1() + "' ");
}
if(Util.textHasContent(query.getField2()))
{
buffer.append(" field2 = '" + query.getField2() + "' ");
}
etc...
return(buffer.toString());
}
now I need to know when to put in the
"WHERE" and the "AND" keywords but I know there is an elegant way to accomplish what I need to do. Maybe with a String.format function that is new to JDK 1.5 or a Formatter or something. But if there is a pattern I can go look at please let me know. For some reason I keep thinking little language but it seems so overwhelming for me.
Thanks in advance
Brian
Spend like you don't need the money,
love like you've never been hurt and dance like nobody's watching!
I have a situation that I believe a pattern can solve and want to really start understanding how to apply these to real-life (my real life) situations.
I have a QueryBuilder class that needs to build a select statement based off of what has content.
Something like
public class QueryBuilder
public String buildQuery(Query query)
{
StringBuffer buffer = new StringBuffer();
buffer.append("select * from some_table ");
if(Util.textHasContent(query.getField1()))
{
buffer.append(" field1 = '" + query.getField1() + "' ");
}
if(Util.textHasContent(query.getField2()))
{
buffer.append(" field2 = '" + query.getField2() + "' ");
}
etc...
return(buffer.toString());
}
now I need to know when to put in the
"WHERE" and the "AND" keywords but I know there is an elegant way to accomplish what I need to do. Maybe with a String.format function that is new to JDK 1.5 or a Formatter or something. But if there is a pattern I can go look at please let me know. For some reason I keep thinking little language but it seems so overwhelming for me.
Thanks in advance
Brian
Spend like you don't need the money,
love like you've never been hurt and dance like nobody's watching!