aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/ui/stats/datasets/StatsDatabaseHelper.java
blob: 03670cda8e8e83136c4096e74ab824d36f926157 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package org.wordpress.android.ui.stats.datasets;

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

import org.wordpress.android.util.AppLog;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * database for all tracks information
 */
public class StatsDatabaseHelper extends SQLiteOpenHelper {
    private static final String DB_NAME = "stats.db";
    private static final int DB_VERSION = 1;

    /*
	 *  database singleton
	 */
    private static StatsDatabaseHelper mDatabaseHelper;
    private final static Object mDbLock = new Object();
    private final Context mContext;

    public static StatsDatabaseHelper getDatabase(Context ctx) {
        if (mDatabaseHelper == null) {
            synchronized(mDbLock) {
                if (mDatabaseHelper == null) {
                    mDatabaseHelper = new StatsDatabaseHelper(ctx);
                    // this ensures that onOpen() is called with a writable database (open will fail if app calls getReadableDb() first)
                    mDatabaseHelper.getWritableDatabase();
                }
            }
        }
        return mDatabaseHelper;
    }

    private StatsDatabaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        mContext = context;
    }


    public static SQLiteDatabase getReadableDb(Context ctx) {
        return getDatabase(ctx).getReadableDatabase();
    }
    public static SQLiteDatabase getWritableDb(Context ctx) {
        return getDatabase(ctx).getWritableDatabase();
    }

    @Override
    public void onOpen(SQLiteDatabase db) {
        super.onOpen(db);
        // Used during development to copy database to external storage and read its content.
        // copyDatabase(db);
    }

    /*
     * drop & recreate all tables (essentially clears the db of all data)
     */
    public void reset() {
        SQLiteDatabase db = getWritableDatabase();
        db.beginTransaction();
        try {
            dropAllTables(db);
            createAllTables(db);
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        createAllTables(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // for now just reset the db when upgrading, future versions may want to avoid this
        // and modify table structures, etc., on upgrade while preserving data
        AppLog.i(AppLog.T.STATS, "Upgrading database from version " + oldVersion + " to version " + newVersion);
        reset();
    }

    @Override
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // IMPORTANT: do NOT call super() here - doing so throws a SQLiteException
        AppLog.w(AppLog.T.STATS, "Downgrading database from version " + oldVersion + " to version " + newVersion);
        reset();
    }

    private void createAllTables(SQLiteDatabase db) {
        StatsTable.createTables(db);
    }

    private void dropAllTables(SQLiteDatabase db) {
        StatsTable.dropTables(db);
    }

    /*
     * used during development to copy database to external storage so we can access it via DDMS
    */
    @SuppressWarnings("unused")
    private void copyDatabase(SQLiteDatabase db) {
        String copyFrom = db.getPath();
        String copyTo = mContext.getExternalFilesDir(null).getAbsolutePath() + "/" + DB_NAME;

        try {
            InputStream input = new FileInputStream(copyFrom);
            OutputStream output = new FileOutputStream(copyTo);

            byte[] buffer = new byte[1024];
            int length;
            while ((length = input.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }

            output.flush();
            output.close();
            input.close();
        } catch (IOException e) {
            AppLog.e(AppLog.T.STATS, "failed to copy stats database", e);
        }
    }
}