package app.android.gambit.local;


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class DbOpenHelper extends SQLiteOpenHelper
{
	private static final int DATABASE_VERSION = 1;
	private static final String DATABASE_NAME = "gambit.db";

	public DbOpenHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.beginTransaction();

		try {
			db.execSQL(buildDecksTableCreationQuery());
			db.execSQL(buildCardsTableCreationQuery());
			db.execSQL(buildLastUpdateTimeTableCreationQuery());

			db.setTransactionSuccessful();
		}
		finally {
			db.endTransaction();
		}
	}

	private String buildDecksTableCreationQuery() {
		StringBuilder builder = new StringBuilder();

		builder.append(String.format("create table %s ", DbTableNames.DECKS));

		builder.append("(");
		builder.append(String.format("%s %s, ", DbFieldNames.ID, DbFieldParams.ID));
		builder.append(String.format("%s %s, ", DbFieldNames.DECK_TITLE, DbFieldParams.DECK_TITLE));
		builder.append(String.format("%s %s", DbFieldNames.DECK_CURRENT_CARD_INDEX,
			DbFieldParams.DECK_NEXT_CARD_INDEX));
		builder.append(")");

		return builder.toString();
	}

	private String buildCardsTableCreationQuery() {
		StringBuilder builder = new StringBuilder();

		builder.append(String.format("create table %s ", DbTableNames.CARDS));

		builder.append("(");

		builder.append(String.format("%s %s, ", DbFieldNames.ID, DbFieldParams.ID));
		builder.append(String.format("%s %s, ", DbFieldNames.CARD_DECK_ID, DbFieldParams.CARD_DECK_ID));
		builder.append(String.format("%s %s, ", DbFieldNames.CARD_FRONT_SIDE_TEXT,
			DbFieldParams.CARD_FRONT_SIDE_TEXT));
		builder.append(String.format("%s %s, ", DbFieldNames.CARD_BACK_SIDE_TEXT,
			DbFieldParams.CARD_BACK_SIDE_TEXT));
		builder.append(String.format("%s %s", DbFieldNames.CARD_ORDER_INDEX,
			DbFieldParams.CARD_ORDER_INDEX));

		builder.append(")");

		return builder.toString();
	}

	private String buildLastUpdateTimeTableCreationQuery() {
		StringBuilder builder = new StringBuilder();

		builder.append(String.format("create table %s ", DbTableNames.DB_LAST_UPDATE_TIME));

		builder.append("(");
		builder.append(String.format("%s %s", DbFieldNames.DB_LAST_UPDATE_TIME,
			DbFieldParams.DB_LAST_UPDATE_TIME));
		builder.append(")");

		return builder.toString();
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		throw new DbException(String.format(
			"'%s' database is currently not intended to be upgraded", DATABASE_NAME));
	}

	@Override
	public synchronized SQLiteDatabase getReadableDatabase() {
		// See getWritableDatabase() for details
		SQLiteDatabase database = super.getReadableDatabase();
		database.setLockingEnabled(false);
		return database;
	}

	@Override
	public synchronized SQLiteDatabase getWritableDatabase() {
		/*
		 * We need to turn off database locking in order to avoid “HeapWorker is wedged:
		 * 1XXXXms spent inside Landroid/database/sqlite/SQLiteCursor;.finalize()V” error.
		 */
		SQLiteDatabase database = super.getWritableDatabase();
		database.setLockingEnabled(false);
		return database;
	}
}

@@ -505,24 +505,175 @@
 ;%0A%0A%09%09try %7B%0A%09
+%09%09createTables(db);%0A%0A%09%09%09db.setTransactionSuccessful();%0A%09%09%7D%0A%09%09finally %7B%0A%09%09%09db.endTransaction();%0A%09%09%7D%0A%09%7D%0A%0A%09private void createTables(SQLiteDatabase db) %7B%0A
 %09%09db.execSQL
@@ -698,33 +698,32 @@
 eationQuery());%0A
-%09
 %09%09db.execSQL(bui
@@ -752,17 +752,16 @@
 ery());%0A
-%09
 %09%09db.exe
@@ -810,87 +810,8 @@
 ());
-%0A%0A%09%09%09db.setTransactionSuccessful();%0A%09%09%7D%0A%09%09finally %7B%0A%09%09%09db.endTransaction();%0A%09%09%7D
 %0A%09%7D%0A
@@ -2576,16 +2576,24 @@
  int old
+Database
 Version,
@@ -2600,16 +2600,24 @@
  int new
+Database
 Version)
@@ -2818,47 +2818,183 @@
 %0A%09%09/
-/ See getWritableDatabase() for details
+*%0A%09%09 * We need to turn off database locking in order to avoid %E2%80%9CHeapWorker is wedged:%0A%09%09 * 1XXXXms spent inside Landroid/database/sqlite/SQLiteCursor;.finalize()V%E2%80%9D error.%0A%09%09 */
 %0A%09%09S
@@ -3182,187 +3182,8 @@
 ) %7B%0A
-%09%09/*%0A%09%09 * We need to turn off database locking in order to avoid %E2%80%9CHeapWorker is wedged:%0A%09%09 * 1XXXXms spent inside Landroid/database/sqlite/SQLiteCursor;.finalize()V%E2%80%9D error.%0A%09%09 */%0A
 %09%09SQ

Improve cosmetic look of database open helper.