com.healthmarketscience.sqlbuilder
Class QueryReader

java.lang.Object
  extended by com.healthmarketscience.sqlbuilder.QueryReader

public class QueryReader
extends Object

Helper class which keeps track of the column positions in dynamically generated select statements so that the query user can easily get the results correctly, especially useful where the code which generates the query is separate from the code which uses the query. A QueryReader may only be used for a single query generation. Also, the Columns will not have a valid position until the query is actually converted to a string. After that, the Columns will never change their stored index(es), so reuse in a new query is impossible. Likewise, the state of the QueryReader is altered by the query generation, so, it cannot be used in a new query either. However, the QueryReader utility is designed so that it is not modified after query generation, so it can safely be used concurrently with the original query string as long as desired (so it can safely be used in a static context along with the associated query string). Examples:

 
   // example where another class is generating the actual query
   QueryReader reader = new QueryReader();
   QueryReader.Column col1 = reader.getNewColumn();
   QueryReader.Column col2 = reader.getNewColumn();
   String queryStr = otherObj.createQuery(col1, col2);
   Statement stmt = conn.createStatement();
   ResultSet rs = stmt.executeQuery(queryStr);
   while(rs.hasNext()) {
     String col1Str = col1.getString(rs);
     int col2Int = col2.getInt(rs);
     // ... handle results ...
   }

  // example query generation
  QueryReader.Column rCol1;
  QueryReader.Column rCol2;
  Column col1, col2, idCol;

  String queryStr = new SelectQuery()
    .addCustomColumns(rCol1.setColumnObject(col1),
                      rCol2.setColumnObject(col2))
    .setCondition(
      UnaryCondition.isNotNull(idCol)).validate().toString();

Author:
James Ahlborn

Nested Class Summary
static class QueryReader.Column
          A SqlObject which outputs the passed in SqlObject, and records the current index at the time the appendTo method is called.
 
Field Summary
static int DEFAULT_START_INDEX
          the default first index that will be assigned to a column
 
Constructor Summary
QueryReader()
           
QueryReader(int startIndex)
          Creates a QueryReader with a different startIndex from the default.
 
Method Summary
 QueryReader.Column getNewColumn()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_START_INDEX

public static final int DEFAULT_START_INDEX
the default first index that will be assigned to a column

See Also:
Constant Field Values
Constructor Detail

QueryReader

public QueryReader()

QueryReader

public QueryReader(int startIndex)
Creates a QueryReader with a different startIndex from the default. This may be useful if there are other columns in the ResultSet which are not owned by this QueryReader.

Method Detail

getNewColumn

public QueryReader.Column getNewColumn()
Returns:
a new Column tied to this QueryReader. Its internal state is not valid until the query is converted to an actual string. Also, it can only be used in one place in the query.


Copyright © 2006–2017 Health Market Science. All rights reserved.