Proyectos de Subversion Android Microlearning - Nuevo Interface

Rev

Rev 65 | | Comparar con el anterior | Ultima modificación | Ver Log |

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