|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectca.krasnay.sqlbuilder.AbstractSqlBuilder
ca.krasnay.sqlbuilder.SelectBuilder
public class SelectBuilder
Tool for programmatically constructing SQL select statements. This class aims to simplify the task of juggling commas and SQL keywords when building SQL statements from scratch, but doesn't attempt to do much beyond that. Here are some relatively complex examples:
String sql = new SelectBuilder()
.column("e.id")
.column("e.name as empname")
.column("d.name as deptname")
.column("e.salary")
.from(("Employee e")
.join("Department d on e.dept_id = d.id")
.where("e.salary > 100000")
.orderBy("e.salary desc")
.toString();
String sql = new SelectBuilder()
.column("d.id")
.column("d.name")
.column("sum(e.salary) as total")
.from("Department d")
.join("Employee e on e.dept_id = d.id")
.groupBy("d.id")
.groupBy("d.name")
.having("total > 1000000").toString();
Note that the methods can be called in any order. This is handy when a base
class wants to create a simple query but allow subclasses to augment it.
It's similar to the Squiggle SQL library
(http://code.google.com/p/squiggle-sql/), but makes fewer assumptions about
the internal structure of the SQL statement, which I think makes for simpler,
cleaner code. For example, in Squiggle you would write...
select.addCriteria(new MatchCriteria(orders, "status", MatchCriteria.EQUALS, "processed"));With SelectBuilder, we assume you know how to write SQL expressions, so instead you would write...
select.where("status = 'processed'");
To include parameters, it's highly recommended to use the
ParameterizedPreparedStatementCreatorTest, like this:
String sql = new SelectBuilder("Employee e")
.where("name like :name")
.toString();
PreparedStatement ps = new ParameterizedPreparedStatementCreator(sql)
.setParameter("name", "Bob%")
.createPreparedStatement(conn);
| Constructor Summary | |
|---|---|
|
SelectBuilder()
|
protected |
SelectBuilder(SelectBuilder other)
Copy constructor. |
|
SelectBuilder(String table)
|
| Method Summary | |
|---|---|
SelectBuilder |
and(String expr)
Alias for where(String). |
SelectBuilder |
clone()
|
SelectBuilder |
column(String name)
|
SelectBuilder |
column(String name,
boolean groupBy)
|
SelectBuilder |
column(SubSelectBuilder subSelect)
|
SelectBuilder |
distinct()
|
SelectBuilder |
forUpdate()
|
SelectBuilder |
from(String table)
|
List<SelectBuilder> |
getUnions()
|
SelectBuilder |
groupBy(String expr)
|
SelectBuilder |
having(String expr)
|
SelectBuilder |
join(String join)
|
SelectBuilder |
leftJoin(String join)
|
SelectBuilder |
noWait()
|
SelectBuilder |
orderBy(String name)
|
SelectBuilder |
orderBy(String name,
boolean ascending)
Adds an ORDER BY item with a direction indicator. |
String |
toString()
|
SelectBuilder |
union(SelectBuilder unionBuilder)
Adds a "union" select builder. |
SelectBuilder |
where(String expr)
|
| Methods inherited from class ca.krasnay.sqlbuilder.AbstractSqlBuilder |
|---|
appendList |
| Methods inherited from class java.lang.Object |
|---|
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
|---|
public SelectBuilder()
public SelectBuilder(String table)
protected SelectBuilder(SelectBuilder other)
clone().
other - SelectBuilder being cloned.| Method Detail |
|---|
public SelectBuilder and(String expr)
where(String).
public SelectBuilder column(String name)
public SelectBuilder column(SubSelectBuilder subSelect)
public SelectBuilder column(String name,
boolean groupBy)
public SelectBuilder clone()
clone in class Objectpublic SelectBuilder distinct()
public SelectBuilder forUpdate()
public SelectBuilder from(String table)
public List<SelectBuilder> getUnions()
public SelectBuilder groupBy(String expr)
public SelectBuilder having(String expr)
public SelectBuilder join(String join)
public SelectBuilder leftJoin(String join)
public SelectBuilder noWait()
public SelectBuilder orderBy(String name)
public SelectBuilder orderBy(String name,
boolean ascending)
name - Name of the column by which to sort.ascending - If true, specifies the direction "asc", otherwise, specifies
the direction "desc".public String toString()
toString in class Objectpublic SelectBuilder union(SelectBuilder unionBuilder)
public SelectBuilder where(String expr)
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||