Example #1
0
  /**
   * Open a database.
   *
   * @param dbName The name of the database file
   */
  private SQLiteAndroidDatabase openDatabase(String dbname, CallbackContext cbc, boolean old_impl)
      throws Exception {
    try {
      // ASSUMPTION: no db (connection/handle) is already stored in the map
      // [should be true according to the code in DBRunner.run()]

      File dbfile = this.cordova.getActivity().getDatabasePath(dbname);

      if (!dbfile.exists()) {
        dbfile.getParentFile().mkdirs();
      }

      Log.v("info", "Open sqlite db: " + dbfile.getAbsolutePath());

      SQLiteAndroidDatabase mydb = old_impl ? new SQLiteAndroidDatabase() : new SQLiteDatabaseNDK();
      mydb.open(dbfile);

      if (cbc != null) // XXX Android locking/closing BUG workaround
      cbc.success();

      return mydb;
    } catch (Exception e) {
      if (cbc != null) // XXX Android locking/closing BUG workaround
      cbc.error("can't open database " + e);
      throw e;
    }
  }
Example #2
0
    public void run() {
      try {
        this.mydb = openDatabase(dbname, this.openCbc, this.oldImpl);
      } catch (Exception e) {
        Log.e(SQLitePlugin.class.getSimpleName(), "unexpected error, stopping db thread", e);
        dbrmap.remove(dbname);
        return;
      }

      DBQuery dbq = null;

      try {
        dbq = q.take();

        while (!dbq.stop) {
          mydb.executeSqlBatch(dbq.queries, dbq.jsonparams, dbq.queryIDs, dbq.cbc);

          // NOTE: androidLock[Bug]Workaround is not necessary and IGNORED for sqlite4java (NDK
          // version).
          if (this.bugWorkaround && dbq.queries.length == 1 && dbq.queries[0] == "COMMIT")
            mydb.bugWorkaround();

          dbq = q.take();
        }
      } catch (Exception e) {
        Log.e(SQLitePlugin.class.getSimpleName(), "unexpected error", e);
      }

      if (dbq != null && dbq.close) {
        try {
          closeDatabaseNow(dbname);

          dbrmap.remove(dbname); // (should) remove ourself

          if (!dbq.delete) {
            dbq.cbc.success();
          } else {
            try {
              boolean deleteResult = deleteDatabaseNow(dbname);
              if (deleteResult) {
                dbq.cbc.success();
              } else {
                dbq.cbc.error("couldn't delete database");
              }
            } catch (Exception e) {
              Log.e(SQLitePlugin.class.getSimpleName(), "couldn't delete database", e);
              dbq.cbc.error("couldn't delete database: " + e);
            }
          }
        } catch (Exception e) {
          Log.e(SQLitePlugin.class.getSimpleName(), "couldn't close database", e);
          if (dbq.cbc != null) {
            dbq.cbc.error("couldn't close database: " + e);
          }
        }
      }
    }
Example #3
0
  /**
   * Close a database (in the current thread).
   *
   * @param dbname The name of the database file
   */
  private void closeDatabaseNow(String dbname) {
    DBRunner r = dbrmap.get(dbname);

    if (r != null) {
      SQLiteAndroidDatabase mydb = r.mydb;

      if (mydb != null) mydb.closeDatabaseNow();
    }
  }