添加注释

main
dingxinnan 2 years ago
parent 71c5c3e224
commit e7deb0e3ac

@ -28,10 +28,13 @@ import net.micode.notes.data.Notes.NoteColumns;
public class NotesDatabaseHelper extends SQLiteOpenHelper {
// 数据库名称
private static final String DB_NAME = "note.db";
// 数据库版本号
private static final int DB_VERSION = 4;
// 表名
public interface TABLE {
public static final String NOTE = "note";
@ -42,6 +45,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
private static NotesDatabaseHelper mInstance;
// 创建note表的sql语句
private static final String CREATE_NOTE_TABLE_SQL =
"CREATE TABLE " + TABLE.NOTE + "(" +
NoteColumns.ID + " INTEGER PRIMARY KEY," +
@ -63,7 +67,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
NoteColumns.VERSION + " INTEGER NOT NULL DEFAULT 0" +
")";
private static final String CREATE_DATA_TABLE_SQL =
private static final String CREATE_DATA_TABLE_SQL =
"CREATE TABLE " + TABLE.DATA + "(" +
DataColumns.ID + " INTEGER PRIMARY KEY," +
DataColumns.MIME_TYPE + " TEXT NOT NULL," +
@ -106,7 +110,6 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
" WHERE " + NoteColumns.ID + "=old." + NoteColumns.PARENT_ID +
" AND " + NoteColumns.NOTES_COUNT + ">0" + ";" +
" END";
/**
* Increase folder's note count when insert new note to the folder
*/
@ -206,6 +209,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
" WHERE " + NoteColumns.PARENT_ID + "=old." + NoteColumns.ID + ";" +
" END";
// 创建NotesDatabaseHelper类用于创建数据库并创建note表触发器系统文件夹
public NotesDatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@ -216,8 +220,8 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
createSystemFolder(db);
Log.d(TAG, "note table has been created");
}
private void reCreateNoteTableTriggers(SQLiteDatabase db) {
private void reCreateNoteTableTriggers(SQLiteDatabase db) {
// 删除已存在的触发器,如果它们存在
db.execSQL("DROP TRIGGER IF EXISTS increase_folder_count_on_update");
db.execSQL("DROP TRIGGER IF EXISTS decrease_folder_count_on_update");
db.execSQL("DROP TRIGGER IF EXISTS decrease_folder_count_on_delete");
@ -226,6 +230,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
db.execSQL("DROP TRIGGER IF EXISTS folder_delete_notes_on_delete");
db.execSQL("DROP TRIGGER IF EXISTS folder_move_notes_on_trash");
// 创建新的触发器,使用新的触发器创建触发器函数
db.execSQL(NOTE_INCREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER);
db.execSQL(NOTE_DECREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER);
db.execSQL(NOTE_DECREASE_FOLDER_COUNT_ON_DELETE_TRIGGER);
@ -288,6 +293,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
}
static synchronized NotesDatabaseHelper getInstance(Context context) {
// 获取NotesDatabaseHelper实例如果mInstance为空则创建一个新的实例
if (mInstance == null) {
mInstance = new NotesDatabaseHelper(context);
}
@ -296,33 +302,40 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
@Override
public void onCreate(SQLiteDatabase db) {
// 创建note表和data表
createNoteTable(db);
createDataTable(db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// 判断是否需要重新创建触发器
boolean reCreateTriggers = false;
// 是否跳过v2升级
boolean skipV2 = false;
if (oldVersion == 1) {
// 升级到v2
upgradeToV2(db);
skipV2 = true; // this upgrade including the upgrade from v2 to v3
oldVersion++;
}
if (oldVersion == 2 && !skipV2) {
// 升级到v3
upgradeToV3(db);
reCreateTriggers = true;
oldVersion++;
}
if (oldVersion == 3) {
// 升级到v4
upgradeToV4(db);
oldVersion++;
}
if (reCreateTriggers) {
// 重新创建触发器
reCreateNoteTableTriggers(db);
reCreateDataTableTriggers(db);
}

Loading…
Cancel
Save