
package com.uwetrottmann.shopr.provider;

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

import com.uwetrottmann.shopr.provider.ShoprContract.Items;
import com.uwetrottmann.shopr.provider.ShoprContract.Shops;

public class ShoprDatabase extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "shopr.db";

    public static final int DBVER_INITIAL = 1;

    public static final int DBVER_ITEM_COLUMNS = 2;

    public static final int DATABASE_VERSION = DBVER_ITEM_COLUMNS;

    public interface Tables {
        String ITEMS = "items";

        String SHOPS = "shops";
    }

    public interface References {
        String SHOP_ID = "REFERENCES " + Tables.SHOPS + "(" + Shops._ID + ")";
    }

    private static final String CREATE_ITEMS_TABLE = "CREATE TABLE "
            + Tables.ITEMS + " ("

            + Items._ID + " INTEGER PRIMARY KEY,"

            + Shops.REF_SHOP_ID + " INTEGER " + References.SHOP_ID + ","

            + Items.BRAND + " TEXT,"

            + Items.CLOTHING_TYPE + " TEXT,"

            + Items.COLOR + " TEXT,"

            + Items.PRICE + " REAL,"

            + Items.SEX + " TEXT,"

            + Items.IMAGE_URL + " TEXT"

            + ");";

    private static final String CREATE_SHOPS_TABLE = "CREATE TABLE "
            + Tables.SHOPS + " ("

            + Shops._ID + " INTEGER PRIMARY KEY,"

            + Shops.NAME + " TEXT NOT NULL,"

            + Shops.OPENING_HOURS + " TEXT,"

            + Shops.LAT + " REAL,"

            + Shops.LONG + " REAL"

            + ");";

    private static final String TAG = "ShoprDatabase";

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

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

        // items refs shop ids, so create shops table first
        db.execSQL(CREATE_ITEMS_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // always start from scratch
        onResetDatabase(db);
    }

    /**
     * Drops all tables and creates an empty database.
     */
    private void onResetDatabase(SQLiteDatabase db) {
        Log.w(TAG, "Database has incompatible version, starting from scratch");

        db.execSQL("DROP TABLE IF EXISTS " + Tables.ITEMS);
        db.execSQL("DROP TABLE IF EXISTS " + Tables.SHOPS);

        onCreate(db);
    }

}

@@ -309,16 +309,76 @@
 t.Shops;
+%0Aimport com.uwetrottmann.shopr.provider.ShoprContract.Stats;
 %0A%0Apublic
@@ -587,16 +587,62 @@
 S = 2;%0A%0A
+    public static final int DBVER_STATS = 3;%0A%0A
     publ
@@ -686,27 +686,20 @@
 = DBVER_
-ITEM_COLUMN
+STAT
 S;%0A%0A    
@@ -788,16 +788,49 @@
 %22shops%22;
+%0A%0A        String STATS = %22stats%22;
 %0A    %7D%0A%0A
@@ -1768,32 +1768,381 @@
        + %22);%22;%0A%0A
+    private static final String CREATE_STATS_TABLE = %22CREATE TABLE %22%0A            + Tables.STATS + %22 (%22%0A%0A            + Stats._ID + %22 INTEGER PRIMARY KEY,%22%0A%0A            + Stats.USERNAME + %22 TEXT,%22%0A%0A            + Stats.DURATION + %22 INTEGER,%22%0A%0A            + Stats.CYCLE_COUNT + %22 INTEGER,%22%0A%0A            + Stats.TASK_TYPE + %22 TEXT%22%0A%0A            + %22);%22;%0A%0A
     private stat
@@ -2495,16 +2495,57 @@
 _TABLE);
+%0A%0A        db.execSQL(CREATE_STATS_TABLE);
 %0A    %7D%0A%0A
@@ -2976,16 +2976,16 @@
 ITEMS);%0A
-
         
@@ -3035,16 +3035,76 @@
 .SHOPS);
+%0A        db.execSQL(%22DROP TABLE IF EXISTS %22 + Tables.STATS);
 %0A%0A      

Add stats to database.