package app.android.gambit.local;


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


public class GambitDbOpenHelper extends SQLiteOpenHelper
{
	private static final int DATABASE_VERSION = 1;
	private static final String DATABASE_NAME = "Gambit";

	public GambitDbOpenHelper(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 DatabaseException(String.format(
			"'%s' database is currently not intended to be upgraded", DATABASE_NAME));
	}
}

@@ -2683,11 +2683,1498 @@
 ME));%0A%09%7D
+%0A%0A%09@Override%0A%09public synchronized SQLiteDatabase getReadableDatabase() %7B%0A%09%09// See getWritableDatabase() for details%0A%09%09SQLiteDatabase database = super.getReadableDatabase();%0A%09%09database.setLockingEnabled(false);%0A%09%09return database;%0A%09%7D%0A%0A%09@Override%0A%09public synchronized SQLiteDatabase getWritableDatabase() %7B%0A%09%09/* We need to turn off database locking (it's not sqlite stuff, it's part of Android java code).%0A%09%09 *%0A%09%09 * When (1) locking is enabled (by default), (2) database transaction is opened,%0A%09%09 * (3) querying from database is performed and (4) database remains in transaction state for%0A%09%09 * quite a long period (more than 10 secs) the following error occurs and application crashes:%0A%09%09 *   'HeapWorker is wedged: 1XXXXms spent inside%0A%09%09 *   Landroid/database/sqlite/SQLiteCursor;.finalize()V'%0A%09%09 * See http://stackoverflow.com/q/8570864 for some details.%0A%09%09 *%0A%09%09 * It is needed to use transactions when (unit-)testing anything related to database, and%0A%09%09 * synchronization tests take relatively much time for network operations.%0A%09%09 *%0A%09%09 * The only shortcoming from disabling of locking is that SQLiteDatabase client should%0A%09%09 * guarantee to access database only once at a time. We do not perform concurrent database%0A%09%09 * queries so it isn't a problem.%0A%09%09 *%0A%09%09 * As a side effect SQLiteDatabase#setLockingEnabled(false) may increase database access%0A%09%09 * performance.%0A%09%09 */%0A%09%09SQLiteDatabase database = super.getWritableDatabase();%0A%09%09database.setLockingEnabled(false);%0A%09%09return database;%0A%09%7D
 %0A%7D%0A

Disable database locking in GambitDbOpenHelper.