Android Open Source - DKO Util






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.persistence;
/*from   w  ww.j av  a  2s.  c  o  m*/
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Logger;

import javax.sql.DataSource;

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

public class Util {

    static final String CREATE_QS = "CREATE TABLE query_size (last_seen BIGINT, schema_name TEXT, table_name TEXT, id INTEGER PRIMARY KEY, hash_code int, row_count bigint);";
    static final String CREATE_CA = "CREATE TABLE column_access (id INTEGER PRIMARY KEY, query_execution_id long, table_name TEXT, column_name TEXT, last_seen bigint);";
    static final String CREATE_CA_I = "CREATE INDEX caqe ON column_access(query_execution_id ASC);";
    static final String CREATE_QE = "CREATE TABLE query_execution (id INTEGER PRIMARY KEY, query_hash int, stack_hash int, last_seen bigint, description text);";
    static final String CREATE_QE_I1 = "CREATE INDEX qeqh ON query_execution(query_hash ASC);";
    static final String CREATE_QE_I2 = "CREATE INDEX qesh ON query_execution(stack_hash ASC);";

  static DataSource ds = null;
  private static File dbPath = null;
  private static final Logger log = Logger.getLogger("org.kered.dko.persistence.Util");

  public static void setPersistenceDatabasePath(final File f) {
    dbPath = f;
    ds = null;
  }

  static boolean warnedNoSqlite3 = false;

  public static DataSource getDS() {
    if (ds == null) {
      final String[] drivers = {"org.sqlite.JDBC", "org.sqldroid.SQLDroidDriver"};
      Class driver = null;
      for (final String s : drivers) {
        try {
          driver = Class.forName(s);
        } catch (final ClassNotFoundException e) {
          log.fine("could not find "+ s);
        }
      }
      if (driver==null) {
        if (!warnedNoSqlite3) {
          log.warning("Could not find any Sqlite3 JDBC drivers.  All query optimization and snapshot functionality is turned off.");
          warnedNoSqlite3 = true;
        }
        return null;
      }
      final String path = System
          .getProperty(Constants.PROPERTY_PERSISTENCE_DB);
      File PERSISTENCE_DB = null;
      if (dbPath != null) {
        PERSISTENCE_DB = dbPath;
      } else if (path == null) {
        String userHome = System.getProperty("user.home");
        if (thisIsAndroid()) userHome = getAndroidUserHome();
        if (userHome==null || "?".equals(userHome)) {
          throw new RuntimeException("System property 'user.home' not set.  "
            + "This is a known bug in some versions of the JDK.  See JDK-6972329.");
        }
        final File BASE_DIR = new File(userHome);
        PERSISTENCE_DB = new File(BASE_DIR, ".dko_persistence.db");
      } else {
        PERSISTENCE_DB = new File(path);
      }
      final String url = "jdbc:sqlite:" + PERSISTENCE_DB.getPath();
      Connection conn = null;
      try {
        conn = DriverManager.getConnection(url);
        checkQuerySize(conn);
        checkQueryExecution(conn);
        checkColumnAccess(conn);
      } catch (final SQLException e) {
        log.warning("I could not confirm the state of the persistence database ("+ PERSISTENCE_DB.getPath()
            +"), so the usage monitor will be diabled for this query.  This will not effect its output, "
            + "only its speed.  This is the underlying error: "+ e);
        return null;
      } finally {
        try {
          if (conn!=null) conn.close();
        } catch (final SQLException e) {
          e.printStackTrace();
        }
      }
      ds = new SingleThreadedDataSource(new JDBCDriverDataSource(
          Constants.DB_TYPE.SQLITE3, url), 10000, true);

//      try {
//        Package pkg = Class.forName("org.kered.dko.persistence.QuerySize").getPackage();
//        Context[] contexts = {Context.getVMContext(), Context.getThreadContext(), Context.getThreadGroupContext() };
//        for (Context context : contexts) {
//          context.setDataSource(pkg, ds).setAutoUndo(false);
//        }
//      } catch (ClassNotFoundException e) {
//        // TODO Auto-generated catch block
//        e.printStackTrace();
//      }
    }
    return ds;
  }

  private static String getAndroidUserHome() {
    StackTraceElement[] st = Thread.currentThread().getStackTrace();
    int i=0;
    while (!st[i].getClassName().startsWith("org.kered.dko")) ++i;
    while (st[i].getClassName().startsWith("org.kered.dko")) ++i;
    String path = "/data/data/"+ st[i].getClassName();
    while (!new File(path).isDirectory()) {
      path = path.substring(0, path.lastIndexOf('.'));
    }
    return path;
  }

  private static boolean thisIsAndroid() {
    return "Dalvik".equals(System.getProperty("java.vm.name"));
  }

  private static void checkQueryExecution(final Connection conn) throws SQLException {
    final Statement stmt = conn.createStatement();
    try {
      final ResultSet rs = stmt.executeQuery("select count(1) from query_execution");
      rs.next();
      int count = rs.getInt(1);
      rs.close();
    } catch (final SQLException e) {
      log.fine(CREATE_QE);
      stmt.executeUpdate(CREATE_QE);
      stmt.executeUpdate(CREATE_QE_I1);
      stmt.executeUpdate(CREATE_QE_I2);
    }
    stmt.close();
  }

  private static void checkColumnAccess(final Connection conn) throws SQLException {
    final Statement stmt = conn.createStatement();
    try {
      final ResultSet rs = stmt.executeQuery("select count(1) from column_access");
      rs.next();
      int count = rs.getInt(1);
      rs.close();
    } catch (final SQLException e) {
      log.fine(CREATE_CA);
      stmt.executeUpdate(CREATE_CA);
      stmt.executeUpdate(CREATE_CA_I);
    }
    stmt.close();
  }

  private static void checkQuerySize(final Connection conn) throws SQLException {
    final Statement stmt = conn.createStatement();
    try {
      final ResultSet rs = stmt.executeQuery("select count(1) from query_size");
      rs.next();
      int count = rs.getInt(1);
      rs.close();
    } catch (final SQLException e) {
      log.fine(CREATE_QS);
      stmt.executeUpdate(CREATE_QS);
    }
    stmt.close();
  }
}




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