Blogs : Archives
|
|
| < RIFE 0.6.58 with integrated web continuations | RIFE on java.net frontpage > |
|
I post this answer to Chris Winters' post here since his comments don't allow any formatting. How about this: String website;
int dayHits;
boolean enabled;
Select query = (new Select(mDatasource)
.from("tablename")
.where("website", "=", website)
.whereAnd("dayHits", "=", dayHits)
.whereAnd("userEnabled", "=", enabled)
.orderBy("dayHits", Select.DESC));
The benefit I get from this is I can easily pass the query object around and complete it elsewhere in the code without having to do string manipulation or having to be careful about a correct syntax. Compared to regular sql, the datasource is also aware of the datatype conversions that are necessary in case you don't want to use prepared statements. If you do want to use them, what about this: Select query = (new Select(mDatasource)
.from("tablename")
.whereParameter("website", "=")
.whereParameterAnd("dayHits", "=")
.whereParameterAnd("userEnabled", "=")
.orderBy("dayHits", Select.DESC));
DbPreparedStatement ps = mConnection.getPreparedStatement(query);
ps.setString("website", website);
ps.setInt("dayHits", dayHits);
ps.setBoolean("userEnabled", enabled);
Which allows you to dissociate query building from data entry without having to remember the numeric ids of your parameters. You can even got further and pass it classes and beans directly like this: WebsiteData data;
Select query = (new Select(mDatasource)
.from("tablename")
.whereParameters(data.getClass())
.orderBy("dayHits", Select.DESC));
DbPreparedStatement ps = mConnection.getPreparedStatement(query);
ps.setBean(data);
With class WebsiteData
{
private String mWebsite = null;
private int mDayHits = 0;
private boolean mEnabled = true;
public void setWebsite(String website) { mWebsite = website; }
public String getWebsite() { return mWebsite; }
public void setDayHits(int dayHits) { mDayHits = dayHits; }
public int getDayHits() { return mDayHits; }
public void setEnabled(boolean enabled) { mEnabled = enabled; }
public boolean isEnabled() { return mEnabled; }
}
This gives you very fast query building and data entry with the flexibility to modify the query at any time with specific methods that have nothing to do with the bean you're storing. One last benefit of all this is that any possible syntax mistakes are moved from runtime to compile time. This is particularly handy when you're writing a temporary cheap quick-and-dirty solution for a customer and don't have the resources nor the time to setup a testsuite and do it properly. |
||||||
Comments |
||||||
|
||||||
|
||||||
Add a new comment |
||||||
|
||||||
| < RIFE 0.6.58 with integrated web continuations | RIFE on java.net frontpage > |


