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));
	}
}

@@ -871,33 +871,33 @@
 (String.format(%22
-C
+c
 reate table %25s %22
@@ -932,33 +932,32 @@
 uilder.append(%22(
- 
 %22);%0A%09%09builder.ap
@@ -1147,33 +1147,32 @@
 ng.format(%22%25s %25s
- 
 %22, DbFieldNames.
@@ -1248,33 +1248,32 @@
 uilder.append(%22)
- 
 %22);%0A%0A%09%09return bu
@@ -1415,33 +1415,33 @@
 (String.format(%22
-C
+c
 reate table %25s %22
@@ -1513,33 +1513,32 @@
 (String.format(%22
- 
 %25s %25s, %22, DbFiel
@@ -1577,20 +1577,16 @@
 %09builder
-%0A%09%09%09
 .append(
@@ -1592,33 +1592,32 @@
 (String.format(%22
- 
 %25s %25s, %22, DbFiel
@@ -1691,33 +1691,32 @@
 (String.format(%22
- 
 %25s %25s, %22, DbFiel
@@ -1813,25 +1813,24 @@
 ing.format(%22
- 
 %25s %25s, %22, Db
@@ -1937,15 +1937,13 @@
 at(%22
- 
 %25s %25s
- 
 %22, D
@@ -2205,17 +2205,17 @@
 format(%22
-C
+c
 reate ta
@@ -2280,17 +2280,16 @@
 ppend(%22(
- 
 %22);%0A%09%09bu
@@ -2321,17 +2321,16 @@
 t(%22%25s %25s
- 
 %22, DbFie
@@ -2417,17 +2417,16 @@
 ppend(%22)
- 
 %22);%0A%0A%09%09r

Improve queries in GambitDbOpenHelper.