예제 #1
0
  private boolean executeMigrations(SQLiteDatabase db, int oldVersion, int newVersion) {
    boolean migrationExecuted = false;
    try {
      final List<String> files = Arrays.asList(Cache.getContext().getAssets().list(MIGRATION_PATH));
      Collections.sort(files, new NaturalOrderComparator());

      db.beginTransaction();
      try {
        for (String file : files) {
          try {
            final int version = Integer.valueOf(file.replace(".sql", ""));

            if (version > oldVersion && version <= newVersion) {
              executeSqlScript(db, file);
              migrationExecuted = true;

              Log.i(file + " executed succesfully.");
            }
          } catch (NumberFormatException e) {
            Log.w("Skipping invalidly named file: " + file, e);
          }
        }
        db.setTransactionSuccessful();
      } finally {
        db.endTransaction();
      }
    } catch (IOException e) {
      Log.e("Failed to execute migrations.", e);
    }

    return migrationExecuted;
  }
예제 #2
0
  public void copyAttachedDatabase(Context context, String databaseName) {
    final File dbPath = context.getDatabasePath(databaseName);

    // If the database already exists, return
    if (dbPath.exists()) {
      return;
    }

    // Make sure we have a path to the file
    dbPath.getParentFile().mkdirs();

    // Try to copy database file
    try {
      final InputStream inputStream = context.getAssets().open(databaseName);
      final OutputStream output = new FileOutputStream(dbPath);

      byte[] buffer = new byte[1024];
      int length;

      while ((length = inputStream.read(buffer)) > 0) {
        output.write(buffer, 0, length);
      }

      output.flush();
      output.close();
      inputStream.close();
    } catch (IOException e) {
      Log.e("Failed to open file", e);
    }
  }
예제 #3
0
  public static synchronized void initialize(Application application) {
    if (sIsInitialized) {
      Log.v("ActiveAndroid already initialized.");
      return;
    }

    sContext = application;

    sModelInfo = new ModelInfo(application);
    sDatabaseHelper = new DatabaseHelper(sContext);

    sEntities = new HashSet<Model>();

    sIsInitialized = true;

    openDatabase();

    Log.v("ActiveAndroid initialized succesfully.");
  }
예제 #4
0
  public static synchronized void dispose() {
    checkInitialization();
    closeDatabase();

    sEntities = null;
    sModelInfo = null;
    sDatabaseHelper = null;

    sIsInitialized = false;

    Log.v("ActiveAndroid disposed. Call initialize to use library.");
  }
예제 #5
0
  private void executeSqlScript(SQLiteDatabase db, String file) {

    InputStream stream = null;

    try {
      stream = Cache.getContext().getAssets().open(MIGRATION_PATH + "/" + file);

      if (Configuration.SQL_PARSER_DELIMITED.equalsIgnoreCase(mSqlParser)) {
        executeDelimitedSqlScript(db, stream);

      } else {
        executeLegacySqlScript(db, stream);
      }

    } catch (IOException e) {
      Log.e("Failed to execute " + file, e);

    } finally {
      IOUtils.closeQuietly(stream);
    }
  }
예제 #6
0
 public static synchronized void clear() {
   sEntities = new HashSet<Model>();
   Log.v("Cache cleared.");
 }
예제 #7
0
 private void executePragmas(SQLiteDatabase db) {
   if (SQLiteUtils.FOREIGN_KEYS_SUPPORTED) {
     db.execSQL("PRAGMA foreign_keys=ON;");
     Log.i("Foreign Keys supported. Enabling foreign key features.");
   }
 }