Android Open Source - BaseAndroid Base Database Open Helper






From Project

Back to project page BaseAndroid.

License

The source code is released under:

MIT License

If you think the Android project BaseAndroid 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

/**
 * File: BaseDatabaseOpenHelper//w  w w . j  a v a2 s.  c om
 * CreationDate: 29/07/13
 * Author: "M. en C. Javier Silva Perez (JSP)"
 * Description: 
 *
 */
package com.cmovil.baseandroid.dao.db.helper;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.TextUtils;
import android.util.Log;

import com.cmovil.baseandroid.dao.db.DatabaseDictionary;
import com.cmovil.baseandroid.util.KeyDictionary;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Base class which handles data base tables creation and upgrade and using SQLiteOpenHelper
 *
 * @author "M. en C. Javier Silva Perez (JSP)"
 * @version 1.0
 * @since 29/07/13
 */
public abstract class BaseDatabaseOpenHelper extends SQLiteOpenHelper {

  protected final Context dbHelperContext;

  public BaseDatabaseOpenHelper(Context context) {
    super(context, DatabaseDictionary.DATABASE_NAME, null, DatabaseDictionary.DATABASE_VERSION);
    this.dbHelperContext = context;
  }

  /**
   * Get a list of column base_dictionary for the selected table
   *
   * @param db
   *   Database that contains the table
   * @param tableName
   *   Table name to be used
   * @return A List of column name
   */
  public static List<String> getColumns(SQLiteDatabase db, String tableName) {
    List<String> ar = null;
    Cursor c = null;
    try {
      c = db.rawQuery("select * from " + tableName + " limit 1", null);
      if (c != null) {
        ar = new ArrayList<String>(Arrays.asList(c.getColumnNames()));
      }
    } catch (Exception e) {
      Log.v(tableName, e.getMessage(), e);
      e.printStackTrace();
    } finally {
      if (c != null) c.close();
    }
    return ar;
  }

  /**
   * Called when the database is created for the first time. This is where the creation of tables and the initial
   * population of the tables should happen.
   *
   * @param db
   *   The database.
   */
  public abstract void create(SQLiteDatabase db);

  /**
   * Called when the database needs to be upgraded. The implementation should use this method to drop tables,
   * add tables,
   * or do anything else it needs to upgrade to the new schema version. <p/> <p> The SQLite ALTER TABLE
   * documentation can
   * be found <a href="http://sqlite.org/lang_altertable.html">here</a> . If you add new columns you can use ALTER
   * TABLE
   * to insert them into a live table. If you rename or remove columns you can use ALTER TABLE to rename the old
   * table,
   * then create the new table and then populate the new table with the contents of the old table. </p><p> This
   * method
   * executes within a transaction.  If an exception is thrown, all changes will automatically be rolled back. </p>
   *
   * @param db
   *   The database.
   */
  protected void upgrade(String tableName, String sqlCreate, String sqlBackUp, SQLiteDatabase db) {
    Log.i(KeyDictionary.TAG, "Upgrading State");

    //Update State table
    db.beginTransaction();
    // run a table creation with if not exists (we are doing an upgrade, so the table might not exists yet,
    // it will fail alter and drop)
    db.execSQL(sqlCreate);
    //Get a list the existing columns
    List<String> columns = getColumns(db, tableName);
    //Backup table
    db.execSQL(sqlBackUp);
    //create new table (the newest table creation schema)
    db.execSQL(sqlCreate);
    //get the intersection with the new columns, this time columns taken from the upgraded table
    columns.retainAll(getColumns(db, tableName));
    //Restore data
    String cols = TextUtils.join(",", columns);
    db.execSQL(String.format("INSERT INTO %s (%s) SELECT %s from temp_%s", tableName, cols, cols, tableName));
    //remove backup table
    db.execSQL("DROP table 'temp_" + tableName + "'");
    //Close transaction
    db.setTransactionSuccessful();
    db.endTransaction();
  }

  /**
   * Called when the database is created for the first time. This is where the creation of tables and the initial
   * population of the tables should happen.
   *
   * @param db
   *   The database.
   */
  @Override
  public void onCreate(SQLiteDatabase db) {

    //TODO: Add each table open helper create function
    //Call create function for each table
    (new SampleOpenHelper(dbHelperContext)).create(db);
  }

  /**
   * Called when the database needs to be upgraded. The implementation should use this method to drop tables,
   * add tables,
   * or do anything else it needs to upgrade to the new schema version. <p/> <p> The SQLite ALTER TABLE
   * documentation can
   * be found <a href="http://sqlite.org/lang_altertable.html">here</a> . If you add new columns you can use ALTER
   * TABLE
   * to insert them into a live table. If you rename or remove columns you can use ALTER TABLE to rename the old
   * table,
   * then create the new table and then populate the new table with the contents of the old table. </p><p> This
   * method
   * executes within a transaction.  If an exception is thrown, all changes will automatically be rolled back. </p>
   *
   * @param db
   *   The database.
   * @param oldVersion
   *   The old database version.
   * @param newVersion
   *   The new database version.
   */
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.i(KeyDictionary.TAG, "Upgrading DB from version " + oldVersion + " to " + newVersion);

    //TODO: Add each table open helper upgrade function
    //Call each table upgrade function
    (new SampleOpenHelper(dbHelperContext))
      .upgrade(DatabaseDictionary.State.NAME, DatabaseDictionary.State.SQL_CREATE,
        DatabaseDictionary.State.SQL_BACKUP, db);
  }

}




Java Source Code List

com.cmovil.baseandroid.controller.BaseDBController.java
com.cmovil.baseandroid.controller.SampleController.java
com.cmovil.baseandroid.dao.db.BaseDBDAO.java
com.cmovil.baseandroid.dao.db.DBException.java
com.cmovil.baseandroid.dao.db.DatabaseDictionary.java
com.cmovil.baseandroid.dao.db.SampleDAO.java
com.cmovil.baseandroid.dao.db.helper.BaseDatabaseOpenHelper.java
com.cmovil.baseandroid.dao.db.helper.SampleOpenHelper.java
com.cmovil.baseandroid.dao.ws.BaseMessageWS.java
com.cmovil.baseandroid.dao.ws.InvalidResponseException.java
com.cmovil.baseandroid.dao.ws.WSClient.java
com.cmovil.baseandroid.model.db.BaseModel.java
com.cmovil.baseandroid.model.db.State.java
com.cmovil.baseandroid.model.ws.MessageErrorCode.java
com.cmovil.baseandroid.model.ws.Parser.java
com.cmovil.baseandroid.util.CMUtils.java
com.cmovil.baseandroid.util.CustomCatalogComparator.java
com.cmovil.baseandroid.util.KeyDictionary.java
com.cmovil.baseandroid.view.BaseActionBarActivity.java
com.cmovil.baseandroid.view.BaseDrawerActivity.java
com.cmovil.baseandroid.view.SplashActivity.java