Navigation

RSS 2.0 New Entries Syndication Feed Atom 0.3 New Entries Syndication Feed

Show blog menu v

 

General

Use it

Documentation

Support

Sibling projects

RIFE powered

Valid XHTML 1.0 Transitional

Valid CSS!

Blogs : Archives

avatar
< RIFE 0.6.58 with integrated web continuations   RIFE on java.net frontpage >
RE: Query objects vs. SQL

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 WebsiteData defined like this of course:

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.

posted by Geert Bevin in Java on Dec 3, 2003 4:04 PM : 2 comments [permalink]
 

Comments

Re: RE: Query objects vs. SQL

I do like the ability to remove syntax mistakes at compile-time vs. waiting for them to blow up at runtime. Unit tests would alleviate this, and we all write them all the time, right?

I don't see the benefit of passing the query object around and having multiple areas of code modify it. I think that's difficult to maintain and creates complexity where none is needed. Of course, YMMV...

Re: RE: Query objects vs. SQL

One bennie of "passing the query object around" is that you can build it incrementally in parent class and subclasses and pass it up and down the class hierarchy.

Another is that you can build it and then (in those frustrating exceptional cases) tweak it.

Not sure if these have general appeal, but they work for me.

Add a new comment

:) ;)
=) :-)
:'( :(
:/ :D
:| :p
:o 8)
Your email address will not be displayed at anytime on any page.
Only provide your email address if you'd like updates on this entry
and it's comments by email.
Please answer this simple math question:
14 - 13 = 
 
 
  

Manage subscription

Remove email:
 

< RIFE 0.6.58 with integrated web continuations   RIFE on java.net frontpage >
 
 
 
Google
rifers.org web