Android Open Source - DKO Query Snapshot






From Project

Back to project page DKO.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project DKO 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.kered.dko;
// ww w  . j a va 2 s. c o m
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.sql.DataSource;

import org.kered.dko.Constants.DB_TYPE;
import org.kered.dko.Constants.JOIN_TYPE;
import org.kered.dko.datasource.JDBCDriverDataSource;
import org.kered.dko.datasource.SingleThreadedDataSource;

class QuerySnapshot<T extends Table> implements Iterable<T> {

  private static final String PREFIX = "dko_snapshot_";
  private static final String EXT = ".sqlite.db";

  private final File f;
  private final boolean delete;
  private SingleThreadedDataSource ds;
  private final Query<T> q;
  private final String tmpTableName = genTmpTableName();
  private String sql;
  private List<Field<?>> fields;

  public QuerySnapshot(Query<T> q) {
    this.q = q;
    try {
      f = File.createTempFile(PREFIX, EXT);
      delete = true;
      f.deleteOnExit();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    init();
  }

  private String genTmpTableName() {
    StringBuffer sb = new StringBuffer();
    sb.append(PREFIX);
    for (int i=0; i<8; ++i) {
      sb.append((char) ('A' + Math.random()*('Z'-'A')));
    }
    return sb.toString();
  }

  public QuerySnapshot(Query<T> q, File f) {
    this.q = q;
    if (f.isDirectory()) {
      try {
        this.f = File.createTempFile(PREFIX, EXT, f);
        delete = true;
        f.deleteOnExit();
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    } else {
      this.f = f;
      delete = false;
    }
    init();
  }

  private void init() {
    // make sure sqlite driver is loaded...
    org.kered.dko.persistence.Util.getDS();
    String url = "jdbc:sqlite:" + f.getPath();
    ds = new SingleThreadedDataSource(new JDBCDriverDataSource(Constants.DB_TYPE.SQLITE3, url), 10000, false);

    Map<Field, String> fieldNameOverrides = LocalJoin.load(q, tmpTableName, ds);
    
    StringBuffer sb = new StringBuffer();
    sb.append("select ");
    fields = q.getSelectFields();
    for (Field<?> field : fields) {
      String fieldName = fieldNameOverrides.get(field);
      if (fieldName==null) fieldName = field.NAME;
      sb.append(tmpTableName).append(".").append(fieldName).append(", ");
    }
    sb.delete(sb.length()-2, sb.length()).append(" "); // delete the last comma
    sb.append("from ").append(tmpTableName);
    sql = sb.toString();
  }

  @Override
  public Iterator<T> iterator() {
    if (q instanceof DBQuery) {
      return new SelectFromOAI<T>((DBQuery<T>) q, buildSrcIterator());
    } else {
      throw new RuntimeException("not implemented (yet) for this query implementation: "+ q.getClass().getName());
    }
  }

  private PeekableClosableIterator<Object[]> buildSrcIterator() {
    return new PeekableClosableIterator<Object[]>() {
      Connection conn = null;
      private ResultSet rs = null;
      private Statement stmt = null;
      {
        try {
          conn = ds.getConnection();
          stmt = conn.createStatement();
          Util.log(sql, null);
          rs = stmt.executeQuery(sql);
        } catch (SQLException e) {
          throw new RuntimeException(e);
        }
      }
      private Object[] next = null;
      @Override
      public Object[] peek() {
        if (next!=null) return next;
        try {
          if (!rs.next()) return null;
          int leftSize = fields.size();
          next = new Object[leftSize];
          for (int i=0; i<leftSize; ++i) {
            next[i] = Util.getTypedValueFromRS(rs, i+1, fields.get(i));
          }
          return next;
        } catch (SQLException e) {
          throw new RuntimeException(e);
        }
      }
      @Override
      public boolean hasNext() {
        if (next==null) peek();
        return next != null;
      }
      @Override
      public Object[] next() {
        Object[] ret = next;
        next = null;
        return ret;
      }
      @Override
      public void remove() {
        throw new UnsupportedOperationException();
      }
      @Override
      public void close() {
        try {
          if (rs!=null && !rs.isClosed()) {
            rs.close();
            rs = null;
          }
        } catch (SQLException e) {
          e.printStackTrace();
        }
        try {
          if (stmt!=null && !stmt.isClosed()) {
            stmt.close();
            stmt = null;
          }
        } catch (SQLException e) {
          e.printStackTrace();
        }
        try {
          if (conn!=null && !conn.isClosed()) {
            conn.close();
            conn = null;
          }
        } catch (SQLException e) {
          e.printStackTrace();
        }
      }
      @Override
      protected void finalize() throws Throwable {
        close();
        super.finalize();
      }
    };
  }

  @Override
  protected void finalize() throws Throwable {
    if (delete && f.exists()) f.delete();
    super.finalize();
  }

}




Java Source Code List

.HelloWorld.java
org.kered.contactlensfinder.DB.java
org.kered.contactlensfinder.MainActivity.java
org.kered.contactlensfinder.ViewManufacturersActivity.java
org.kered.contactlensfinder.ViewProductsActivity.java
org.kered.contactlensfinder.ViewPropertiesActivity.java
org.kered.dko.AbstractQuery.java
org.kered.dko.Bulk.java
org.kered.dko.CSV.java
org.kered.dko.ClosableIterator.java
org.kered.dko.Condition.java
org.kered.dko.Constants.java
org.kered.dko.Context.java
org.kered.dko.DBQuery.java
org.kered.dko.DBRowIterator.java
org.kered.dko.Diff.java
org.kered.dko.DualIterator.java
org.kered.dko.Expression.java
org.kered.dko.Field.java
org.kered.dko.FilteringQuery.java
org.kered.dko.Function.java
org.kered.dko.InMemoryQuery.java
org.kered.dko.Join.java
org.kered.dko.LazyCacheIterable.java
org.kered.dko.LocalJoin.java
org.kered.dko.M.java
org.kered.dko.Main.java
org.kered.dko.MatryoshkaQuery.java
org.kered.dko.PeekableClosableIterator.java
org.kered.dko.PeekableIterator.java
org.kered.dko.QueryAddField.java
org.kered.dko.QueryFactory.java
org.kered.dko.QuerySnapshot.java
org.kered.dko.Query.java
org.kered.dko.SQLFunction.java
org.kered.dko.SelectAsMapIterable.java
org.kered.dko.SelectFromOAI.java
org.kered.dko.SelectSingleColumn.java
org.kered.dko.SoftJoinUtil.java
org.kered.dko.SoftJoin.java
org.kered.dko.SqlContext.java
org.kered.dko.Statistics.java
org.kered.dko.SubQueryField.java
org.kered.dko.TableInfo.java
org.kered.dko.TableWrapper.java
org.kered.dko.Table.java
org.kered.dko.TemporaryTableFactory.java
org.kered.dko.TmpTableBuilder.java
org.kered.dko.Tuple.java
org.kered.dko.UsageMonitor.java
org.kered.dko.UsageStats.java
org.kered.dko.Util.java
org.kered.dko.ant.ClassGenerator.java
org.kered.dko.ant.CodeGeneratorBase.java
org.kered.dko.ant.CodeGenerator.java
org.kered.dko.ant.DataSourceGenerator.java
org.kered.dko.ant.GsonGenerator.java
org.kered.dko.ant.JoinGenerator.java
org.kered.dko.ant.Main.java
org.kered.dko.ant.SchemaExtractorBase.java
org.kered.dko.ant.SchemaExtractor.java
org.kered.dko.ant.Util.java
org.kered.dko.datasource.CheapConnectionPoolingDataSource.java
org.kered.dko.datasource.ConnectionCountingDataSource.java
org.kered.dko.datasource.JDBCDriverDataSource.java
org.kered.dko.datasource.MatryoshkaDataSource.java
org.kered.dko.datasource.MirroredDataSource.java
org.kered.dko.datasource.ReflectedDataSource.java
org.kered.dko.datasource.SingleConnectionDataSource.java
org.kered.dko.datasource.SingleThreadedDataSource.java
org.kered.dko.datasource.UnClosableConnection.java
org.kered.dko.datasource.Util.java
org.kered.dko.json.CDL.java
org.kered.dko.json.CookieList.java
org.kered.dko.json.Cookie.java
org.kered.dko.json.HTTPTokener.java
org.kered.dko.json.HTTP.java
org.kered.dko.json.JSONArray.java
org.kered.dko.json.JSONException.java
org.kered.dko.json.JSONML.java
org.kered.dko.json.JSONObject.java
org.kered.dko.json.JSONString.java
org.kered.dko.json.JSONStringer.java
org.kered.dko.json.JSONTokener.java
org.kered.dko.json.JSONWriter.java
org.kered.dko.json.Pickle.java
org.kered.dko.json.XMLTokener.java
org.kered.dko.json.XML.java
org.kered.dko.junk.DerbyLoadTestSchema.java
org.kered.dko.junk.OracleCreateTestUser.java
org.kered.dko.junk.OracleLoadTestSchema.java
org.kered.dko.persistence.Util.java
org.kered.dko.util.DumpDatabase.java
sakila.Example0.java
sakila.Example1.java
sakila.Example2.java
sakila.Util.java