referencing columns

Douglas Surber douglas.surber at oracle.com
Wed May 2 17:42:19 UTC 2018


Here is another way to approach referencing columns. Replace ResultMap with  Column as defined below. This is not a fully thought out proposal. It is instead an attempt to look at the problem from another direction and not get stuck with the same old JDBC ResultSet model.

Douglas

/**
 * A mutable handle to one value of an ordered sequence of columns or of 
 * out parameters. Columns have a 1-based index and optionally an identifier.
 * Identifiers are not guaranteed to be unique.
 */
public interface Column extends Iterable<Column>, Iterator<Column>, Cloneable {

  /**
   * Return the value of this column as an instance of the given type.
   * 
   * @param <T>
   * @param type
   * @return 
   */
  public <T> T get(Class<T> type);
    
  /**
   * Return the identifier of this column. May be null.
   * 
   * @return 
   */
  public String identifier();
  
  /**
   * Return the 1-based index of this Column.
   * 
   * @return 
   */
  public int index();
  
  /**
   * Return the SQL type of the value of this column.
   * 
   * @return 
   */
  public SqlType type();
  
  /**
   * Modify this Column to point to the next value identified by id.
   * 
   * @param id
   * @return this Column
   */
  public Column at(String id);
  
  /**
   * Modify this Column to point to the value at index. The first value is at index 1.
   * Negative numbers count back from the last value. The last value is at
   * index -1.
   * 
   * @param index
   * @return this Column
   */
  public Column at(int index);
  
  /**
   * Return a new Column that is a handle to a subsequence of sequence of values
   * referenced by this Column. The subsequence consists of the length values in
   * order starting with the value this Column points to when this method is
   * called. This Column is not modified.
   *
   * @param length
   * @return a new Column.
   */
  public Column slice(int length);
  
  /**
   * Return a new Column that is a duplicate of this Column.
   * 
   * @return a new Column
   */
  public Column clone();
  
  /**
   * Modify this Column to point to the next value in the sequence.
   * 
   * @return this Column
   */
  @Override
  public Column next();
  
  @Override
  public boolean hasNext();
  

  static void sample(Column columns) {
    
    Map map = new HashMap();
    for (Column m : columns) {
      map.put(m.identifier(), m.get(Integer.class));
    }
      
    List l = new ArrayList();
    for (Column m : columns.at(3).slice(5)) {
      l.add(m.get(String.class));
    }
    
    Column addr = columns.clone().at("street_address_1");
    String streetAddress1 = addr.get(String.class);
    String streetAddress2 = addr.next().get(String.class);
    String city = addr.next().get(String.class);
    String state = addr.next().get(String.class);
    String postalCode = addr.next().get(String.class);
    
    String givenName = columns.at("GIVEN_NAME").get(String.class);
    String familyName = columns.at("FAMILY_NAME").get(String.class);
    
  }
}



More information about the jdbc-spec-discuss mailing list