Proyectos de Subversion Android Microlearning - Inconcert

Rev

Rev 1 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 gabriel 1
package com.cesams.twogetskills.inconcert.dao;
2
 
3
import android.content.Context;
4
 
5
import androidx.room.Room;
6
import androidx.room.migration.Migration;
7
import androidx.sqlite.db.SupportSQLiteDatabase;
8
 
9
import com.cesams.twogetskills.inconcert.Constants;
10
 
11
public class DatabaseHelper {
12
 
13
 
14
    private static DatabaseHelper _INSTANCE = null;
15
    private AppDatabase appDatabase;
16
 
17
    // other instance variables can be here
18
 
19
    private DatabaseHelper(Context context) {
20
        appDatabase = Room.databaseBuilder(
21
                context.getApplicationContext(),
22
                AppDatabase.class, Constants.DATABASE_FILENAME
14 gabriel 23
        ).allowMainThreadQueries().addMigrations(MIGRATION_1_2).fallbackToDestructiveMigration().addMigrations(MIGRATION_2_3).fallbackToDestructiveMigration()
1 gabriel 24
                .build();
25
    };
26
 
27
    static final Migration MIGRATION_1_2 = new Migration(1, 2) {
28
        @Override
29
        public void migrate(SupportSQLiteDatabase database) {
30
            database.execSQL("CREATE TABLE `tb_notification` (`id` INTEGER NOT NULL, "
31
                    + "`title` TEXT,`date` TEXT,`description` TEXT, viewed TEXT, url TEXT, PRIMARY KEY(`id`))");
32
 
33
            database.execSQL("ALTER TABLE `tb_capsules`"
34
                    + "ADD COLUMN added_on TEXT");
35
 
36
            database.execSQL("ALTER TABLE `tb_capsules`"
37
                    + "ADD COLUMN updated_on TEXT");
38
 
39
            database.execSQL("ALTER TABLE `tb_topics`"
40
                    + "ADD COLUMN added_on TEXT");
41
 
42
            database.execSQL("ALTER TABLE `tb_topics`"
43
                    + "ADD COLUMN updated_on TEXT");
44
 
45
 
46
        }
47
    };
48
 
14 gabriel 49
    static final Migration MIGRATION_2_3 = new Migration(2, 3) {
50
        @Override
51
        public void migrate(SupportSQLiteDatabase database) {
52
            database.execSQL("ALTER TABLE `tb_capsules`"
53
                    + "ADD COLUMN link_comments TEXT");
1 gabriel 54
 
14 gabriel 55
            database.execSQL("ALTER TABLE `tb_capsules`"
56
                    + "ADD COLUMN link_comment_add TEXT");
57
 
58
            database.execSQL("ALTER TABLE `tb_capsules`"
59
                    + "ADD COLUMN total_comments INTEGER");
60
 
61
            database.execSQL("ALTER TABLE `tb_capsules`"
62
                    + "ADD COLUMN total_rating INTEGER");
63
 
64
            database.execSQL("CREATE TABLE `learning_config` (`id` INTEGER NOT NULL, "
65
                    + "`title` TEXT,`now` TEXT, PRIMARY KEY(`id`))");
66
 
67
 
68
        }
69
    };
70
 
71
 
1 gabriel 72
    public static DatabaseHelper getInstance(Context context) {
73
        if (_INSTANCE == null) {
74
            _INSTANCE = new DatabaseHelper(context);
75
        }
76
        return(_INSTANCE);
77
    }
78
 
79
    public AppDatabase getAppDatabase() {
80
        return appDatabase;
81
    }
82
}