ca.krasnay.sqlbuilder
Class SelectBuilder

java.lang.Object
  extended by ca.krasnay.sqlbuilder.AbstractSqlBuilder
      extended by ca.krasnay.sqlbuilder.SelectBuilder
All Implemented Interfaces:
Serializable, Cloneable
Direct Known Subclasses:
SubSelectBuilder

public class SelectBuilder
extends AbstractSqlBuilder
implements Cloneable, Serializable

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);
 

Author:
John Krasnay
See Also:
Serialized Form

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

SelectBuilder

public SelectBuilder()

SelectBuilder

public SelectBuilder(String table)

SelectBuilder

protected SelectBuilder(SelectBuilder other)
Copy constructor. Used by clone().

Parameters:
other - SelectBuilder being cloned.
Method Detail

and

public SelectBuilder and(String expr)
Alias for where(String).


column

public SelectBuilder column(String name)

column

public SelectBuilder column(SubSelectBuilder subSelect)

column

public SelectBuilder column(String name,
                            boolean groupBy)

clone

public SelectBuilder clone()
Overrides:
clone in class Object

distinct

public SelectBuilder distinct()

forUpdate

public SelectBuilder forUpdate()

from

public SelectBuilder from(String table)

getUnions

public List<SelectBuilder> getUnions()

groupBy

public SelectBuilder groupBy(String expr)

having

public SelectBuilder having(String expr)

join

public SelectBuilder join(String join)

leftJoin

public SelectBuilder leftJoin(String join)

noWait

public SelectBuilder noWait()

orderBy

public SelectBuilder orderBy(String name)

orderBy

public SelectBuilder orderBy(String name,
                             boolean ascending)
Adds an ORDER BY item with a direction indicator.

Parameters:
name - Name of the column by which to sort.
ascending - If true, specifies the direction "asc", otherwise, specifies the direction "desc".

toString

public String toString()
Overrides:
toString in class Object

union

public SelectBuilder union(SelectBuilder unionBuilder)
Adds a "union" select builder. The generated SQL will union this query with the result of the main query. The provided builder must have the same columns as the parent select builder and must not use "order by" or "for update".


where

public SelectBuilder where(String expr)


Copyright © 2014. All rights reserved.