Android Open Source - ormada Forward Only Result Set Cursor






From Project

Back to project page ormada.

License

The source code is released under:

Copyright (c) 2012 Jesse Rosalia Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Sof...

If you think the Android project ormada listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.ormada.dialect;
/*from  w ww .j  a v  a 2 s. com*/
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

/**
 * A generic QueryCursor implementation for "forward only" JDBC ResultSet objects.
 * 
 * Forward only ResultSets only support a handful of the functions defined in the ResultSet
 * object.  Sucks to be us.  This class is written accordingly, to build an intelligent cursor
 * using only the next() function.  Note that because of this, the cursor will be "forward only", 
 * in that it cannot be rewound or replayed.  This should be ok for our ORM implementation.
 * 
 * NOTE: JDBC uses 1 based column indexing, and the ORM framework uses 0 based indexing.  This class adjusts for that.
 * @author Jesse Rosalia
 *
 */
public class ForwardOnlyResultSetCursor implements QueryCursor {

    private ResultSet         resultSet;
    private ResultSetMetaData rsMetaData;
    private boolean first;
    private boolean next;

    public ForwardOnlyResultSetCursor(ResultSet resultSet) throws SQLException {
        this.resultSet  = resultSet;
        this.rsMetaData = resultSet.getMetaData();
        
        this.first = this.resultSet.next();
        this.next  = this.first;
    }

    @Override
  public void close() {
        try {
            this.resultSet.close();
        } catch (SQLException e) {
            //we're closing...nothing to do
        }
  }

  @Override
  public boolean isEmpty() {
      //FIXME: this isnt right, as this will return true after moving past the first element
        return !this.first;
  }

  @Override
  public boolean moveToFirst() {
    try {
            return this.first;
        } finally {
            this.first = false;
        }
  }

  @Override
  public boolean isAfterLast() {
        return !this.next;
  }

  @Override
  public boolean moveToNext() {
        try {
            this.next = this.resultSet.next();
        } catch (SQLException e) {
            e.printStackTrace();
            this.next = false;
        }
        return this.next;
  }

  @Override
  public int getColumnCount() throws SQLException {
      return this.rsMetaData.getColumnCount();
  }

  @Override
  public String getColumnName(int col) throws SQLException {
      return this.rsMetaData.getColumnLabel(col + 1);
  }

  @Override
  public long getLong(int col) throws SQLException {
    return this.resultSet.getLong(col + 1);
  }

  @Override
  public int getInt(int col) throws SQLException {
    return this.resultSet.getInt(col + 1);
  }

  @Override
  public short getShort(int col) throws SQLException {
    return this.resultSet.getShort(col + 1);
  }

  @Override
  public float getFloat(int col) throws SQLException {
    return this.resultSet.getFloat(col + 1);
  }

  @Override
  public double getDouble(int col) throws SQLException {
    return this.resultSet.getDouble(col + 1);
  }

  @Override
  public byte[] getBlob(int col) throws SQLException {
    return this.resultSet.getBytes(col + 1);
  }

  @Override
  public String getString(int col) throws SQLException {
    return this.resultSet.getString(col + 1);
  }
}




Java Source Code List

org.andrormeda.dialect.SQLiteCursor.java
org.andrormeda.dialect.SQLiteDialect.java
org.andrormeda.dialect.SQLiteValueSet.java
org.andrormeda.example.AppDataSource.java
org.andrormeda.example.ExampleActivity.java
org.andrormeda.example.model.Cat.java
org.andrormeda.example.model.Kitten.java
org.ormada.ORMDataSource.java
org.ormada.annotations.OneToMany.java
org.ormada.annotations.Owner.java
org.ormada.annotations.Reference.java
org.ormada.annotations.Text.java
org.ormada.annotations.Transient.java
org.ormada.dialect.AStandardSQLDialect.java
org.ormada.dialect.DefaultValueSet.java
org.ormada.dialect.Dialect.java
org.ormada.dialect.ForwardOnlyResultSetCursor.java
org.ormada.dialect.FullResultSetCursor.java
org.ormada.dialect.QueryCursor.java
org.ormada.dialect.ValueSet.java
org.ormada.entity.EntityBuilder.java
org.ormada.entity.EntityCache.java
org.ormada.entity.EntityMetaData.java
org.ormada.entity.Entity.java
org.ormada.exception.MixedCollectionException.java
org.ormada.exception.UnableToOpenException.java
org.ormada.exception.UnsavedReferenceException.java
org.ormada.hsql.dialect.HSQLDialect.java
org.ormada.hsql.example.AppDataSource.java
org.ormada.hsql.example.ExampleMain.java
org.ormada.hsql.example.model.Cat.java
org.ormada.hsql.example.model.Kitten.java
org.ormada.model.ORMeta.java
org.ormada.reflect.DefaultReflector.java
org.ormada.reflect.Reflector.java
org.ormada.util.Profiler.java