This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathFlowDatabaseHelper.java
More file actions
133 lines (116 loc) · 5.12 KB
/
FlowDatabaseHelper.java
File metadata and controls
133 lines (116 loc) · 5.12 KB
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
131
132
133
package com.uwflow.flow_android.dao;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import com.crashlytics.android.Crashlytics;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.uwflow.flow_android.R;
import com.uwflow.flow_android.db_object.*;
import java.sql.SQLException;
public class FlowDatabaseHelper extends OrmLiteSqliteOpenHelper {
private static final String TAG = "FlowDatabaseHelper";
// name of the database file for your application -- change to something appropriate for your app
private static final String DATABASE_NAME = "flowDatabaseAndroid.db";
// any time you make changes to your database objects, you may have to increase the database version
private static final int DATABASE_VERSION = 1;
/**
* NOTE IF YOU CHANGE ANYTHING IN ANY OF THE DATABASE FILES OR ADD NEW DATABASE FILES
* YOU MUST RE-RUN THE MAIN METHOD IN DatabaseConfigUtil class in the util folder
* Use Run as a different java application! Not the same CONTEXT as flow.
*/
private Dao<User, String> userDao;
private Dao<Course, String> userCourseDao;
private Dao<ScheduleCourse, String> userScheduleCourseDao;
private Dao<Exam, Integer> userExamDao;
private Dao<UserCourse, String> userCourseExtraDao;
private Dao<ScheduleImage, String> userSchduleImageDao;
public FlowDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION, R.raw.ormlite_config);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
try {
TableUtils.createTable(connectionSource, User.class);
TableUtils.createTable(connectionSource, Course.class);
TableUtils.createTable(connectionSource, ScheduleCourse.class);
TableUtils.createTable(connectionSource, Exam.class);
TableUtils.createTable(connectionSource, UserCourse.class);
TableUtils.createTable(connectionSource, ScheduleImage.class);
} catch (SQLException e) {
Log.e(TAG, "Unable to create databases", e);
Crashlytics.logException(e);
}
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int oldVer, int newVer) {
try {
TableUtils.dropTable(connectionSource, User.class, true);
TableUtils.dropTable(connectionSource, Course.class, true);
TableUtils.dropTable(connectionSource, ScheduleCourse.class, true);
TableUtils.dropTable(connectionSource, Exam.class, true);
TableUtils.dropTable(connectionSource, UserCourse.class, true);
TableUtils.dropTable(connectionSource, ScheduleImage.class, true);
onCreate(sqLiteDatabase, connectionSource);
} catch (SQLException e) {
Log.e(TAG, "Unable to upgrade database from version " + oldVer + " to new "
+ newVer, e);
Crashlytics.logException(e);
}
}
/**
* Call this method to delete all entries in the database
*
* @param connectionSource
*/
public static void clearDatabase(ConnectionSource connectionSource){
try {
TableUtils.clearTable(connectionSource, User.class);
TableUtils.clearTable(connectionSource, Course.class);
TableUtils.clearTable(connectionSource, ScheduleCourse.class);
TableUtils.clearTable(connectionSource, Exam.class);
TableUtils.clearTable(connectionSource, UserCourse.class);
TableUtils.clearTable(connectionSource, ScheduleImage.class);
} catch (SQLException e) {
e.printStackTrace();
}
}
public Dao<User, String> getUserDao() throws SQLException {
if (userDao == null) {
userDao = getDao(User.class);
}
return userDao;
}
public Dao<Course, String> getUserCourseDao() throws SQLException {
if (userCourseDao == null){
userCourseDao = getDao(Course.class);
}
return userCourseDao;
}
public Dao<ScheduleCourse, String> getUserScheduleCourseDao() throws SQLException {
if (userScheduleCourseDao == null){
userScheduleCourseDao = getDao(ScheduleCourse.class);
}
return userScheduleCourseDao;
}
public Dao<Exam, Integer> getUserExamDao() throws SQLException {
if (userExamDao == null){
userExamDao = getDao(Exam.class);
}
return userExamDao;
}
public Dao<UserCourse, String> getUserCourseExtraDao() throws SQLException {
if (userCourseExtraDao == null){
userCourseExtraDao = getDao(UserCourse.class);
}
return userCourseExtraDao;
}
public Dao<ScheduleImage, String> getUserSchduleImageDao() throws SQLException {
if (userSchduleImageDao == null){
userSchduleImageDao = getDao(ScheduleImage.class);
}
return userSchduleImageDao;
}
}