org.openanzo.sqlscribe
Class SqlQuery

java.lang.Object
  extended by org.openanzo.sqlscribe.SqlQuery

public class SqlQuery
extends java.lang.Object

Dynamic SQL query builder. Capable of constructing simple queries or the form: SELECT FROM WHERE Projections and constraint predicates must be added explicitly to an instance of this class to construct an instance of this class and the sources will be infered. It is recommended that static Column and Table instance be created for the database schema in use and then the Match class be used to construct predicates. JDBC style bound variables are supported in predicates (see Match.varEquals, etc..) and are passed in when calling getQueryString or toCallableStatement, which dynamically construct the SQL for the instance of this class.

Author:
Joe Betz

Constructor Summary
SqlQuery()
          Create a new SQL query
 
Method Summary
 void addPredicate(Constraint constraint)
          Add a constraint to the SQL query
 void addProjection(Column column)
          Add a projection to the SQL query
 java.lang.String getConstraintSql()
          Get the constraint strings within this query
 java.util.Set<Constraint> getPredicates()
          Get the constraints for this query
 java.util.Set<Column> getProjections()
          Get all projections for this query
 java.lang.String getProjectionSql()
          Get the sql string for the projections within this query
 java.lang.String getQueryString(java.util.List<java.lang.String> unboundVariables)
          Build a query string, using the provided variables
 java.lang.String getQueryString(java.util.Map<java.lang.String,java.lang.Object> variables)
          Build a query string, using the provided variables
 java.util.Set<ITable> getSources()
          Get the set of tables within this query
 java.sql.PreparedStatement toPreparedStatement(PreparedStatementCache cache, java.util.Map<java.lang.String,java.lang.Object> variables)
          convert this SqlQuery to a preparedStatement
 java.util.List<java.lang.String> writeConstraintSql(java.lang.StringBuffer buffer)
          Write the constraint strings within this query to the provided buffer
 void writeProjectionSql(java.lang.StringBuffer buffer)
          Append the sql string for the projections within this query to a buffer
 void writeSourceSql(java.lang.StringBuffer buffer, java.util.Map<java.lang.String,java.lang.Object> variables)
          Append the sql string for the sources within this query to a buffer
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SqlQuery

public SqlQuery()
Create a new SQL query

Method Detail

addProjection

public void addProjection(Column column)
Add a projection to the SQL query

Parameters:
column - to add to SQL query

getProjections

public java.util.Set<Column> getProjections()
Get all projections for this query

Returns:
all projections for this query

addPredicate

public void addPredicate(Constraint constraint)
Add a constraint to the SQL query

Parameters:
constraint - to add to SQL query

getPredicates

public java.util.Set<Constraint> getPredicates()
Get the constraints for this query

Returns:
the constraints for this query

getProjectionSql

public java.lang.String getProjectionSql()
Get the sql string for the projections within this query

Returns:
the sql string for the projections within this query

writeProjectionSql

public void writeProjectionSql(java.lang.StringBuffer buffer)
Append the sql string for the projections within this query to a buffer

Parameters:
buffer - to upon which projection string is to be appended

writeSourceSql

public void writeSourceSql(java.lang.StringBuffer buffer,
                           java.util.Map<java.lang.String,java.lang.Object> variables)
Append the sql string for the sources within this query to a buffer

Parameters:
buffer - upon which projection string is to be appended
variables - set of variables to use within queries

getConstraintSql

public java.lang.String getConstraintSql()
Get the constraint strings within this query

Returns:
the constraint strings within this query

writeConstraintSql

public java.util.List<java.lang.String> writeConstraintSql(java.lang.StringBuffer buffer)
Write the constraint strings within this query to the provided buffer

Parameters:
buffer - upon which constraint string is to be appended
Returns:
the set of variables used within the constraints

getSources

public java.util.Set<ITable> getSources()
Get the set of tables within this query

Returns:
the set of tables within this query

getQueryString

public java.lang.String getQueryString(java.util.Map<java.lang.String,java.lang.Object> variables)
Build a query string, using the provided variables

Parameters:
variables - used to build query string
Returns:
a query string

getQueryString

public java.lang.String getQueryString(java.util.List<java.lang.String> unboundVariables)
Build a query string, using the provided variables

Parameters:
unboundVariables - used to build query string
Returns:
a query string

toPreparedStatement

public java.sql.PreparedStatement toPreparedStatement(PreparedStatementCache cache,
                                                      java.util.Map<java.lang.String,java.lang.Object> variables)
                                               throws java.sql.SQLException
convert this SqlQuery to a preparedStatement

Parameters:
cache - source of prepared statements
variables - used within prepared statement
Returns:
prepared statement for this query
Throws:
java.sql.SQLException - if there was an error building query


Copyright © 2007 Cambridge Semantics Inc.. All Rights Reserved.