diff --git a/Notes-master/src/net/micode/notes/data/NotesDatabaseHelper.java b/Notes-master/src/net/micode/notes/data/NotesDatabaseHelper.java index 0b3568d..33c50d2 100644 --- a/Notes-master/src/net/micode/notes/data/NotesDatabaseHelper.java +++ b/Notes-master/src/net/micode/notes/data/NotesDatabaseHelper.java @@ -14,6 +14,8 @@ * limitations under the License. */ + //管理数据的创建和版本更新 更新笔记内容 版本更新 + package net.micode.notes.data;//表示一个Java包(Package) 表示这个类在data包中 //net.micode.notes.data是包的名称,它通常按照反向域名的方式命名,以确保唯一性和可读性。在这个包中,net是顶级域名的一部分,micode可能是小米,notes.data则是具体的子包结构,用于存放与数据相关的类。 import android.content.ContentValues;//表示导入了Android框架中的 ContentValues 类。这是Android提供的一个用于存储数据库表中的键值对数据的类。 @@ -43,7 +45,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {//NotesDatabaseHelper private static final String TAG = "NotesDatabaseHelper"; private static NotesDatabaseHelper mInstance; - + private static final String CREATE_NOTE_TABLE_SQL = "CREATE TABLE " + TABLE.NOTE + "(" + NoteColumns.ID + " INTEGER PRIMARY KEY," + @@ -86,6 +88,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {//NotesDatabaseHelper /** * Increase folder's note count when move note to the folder + *当将笔记移动到文件夹时,增加文件夹中的笔记数量 */ private static final String NOTE_INCREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER = "CREATE TRIGGER increase_folder_count_on_update "+ @@ -98,6 +101,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {//NotesDatabaseHelper /** * Decrease folder's note count when move note from folder + *当从一个文件夹中移动笔记时,减少该文件夹中的笔记数量 */ private static final String NOTE_DECREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER = "CREATE TRIGGER decrease_folder_count_on_update " + @@ -111,6 +115,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {//NotesDatabaseHelper /** * Increase folder's note count when insert new note to the folder + *当插入新笔记到文件夹时,增加文件夹中的笔记数量 */ private static final String NOTE_INCREASE_FOLDER_COUNT_ON_INSERT_TRIGGER = "CREATE TRIGGER increase_folder_count_on_insert " + @@ -123,6 +128,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {//NotesDatabaseHelper /** * Decrease folder's note count when delete note from the folder + *当从文件夹删除文件时,减少文件夹中笔记的数量 */ private static final String NOTE_DECREASE_FOLDER_COUNT_ON_DELETE_TRIGGER = "CREATE TRIGGER decrease_folder_count_on_delete " + @@ -136,6 +142,8 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {//NotesDatabaseHelper /** * Update note's content when insert data with type {@link DataConstants#NOTE} + *插入数据并且数据类型为 DataConstants#NOTE 时,要更新笔记的内容 + *{@link DataConstants#NOTE}: 它指向名为 DataConstants 的类中的 NOTE 常量 */ private static final String DATA_UPDATE_NOTE_CONTENT_ON_INSERT_TRIGGER = "CREATE TRIGGER update_note_content_on_insert " + @@ -149,6 +157,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {//NotesDatabaseHelper /** * Update note's content when data with {@link DataConstants#NOTE} type has changed + *当数据类型为DataConstants#NOTE改变的时候,更新笔记内容 */ private static final String DATA_UPDATE_NOTE_CONTENT_ON_UPDATE_TRIGGER = "CREATE TRIGGER update_note_content_on_update " + @@ -162,6 +171,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {//NotesDatabaseHelper /** * Update note's content when data with {@link DataConstants#NOTE} type has deleted + *当数据类型为DataConstants#NOTE删掉的时候,更新笔记内容 */ private static final String DATA_UPDATE_NOTE_CONTENT_ON_DELETE_TRIGGER = "CREATE TRIGGER update_note_content_on_delete " + @@ -186,6 +196,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {//NotesDatabaseHelper /** * Delete notes belong to folder which has been deleted + *删除属于已删除注释的数据 */ private static final String FOLDER_DELETE_NOTES_ON_DELETE_TRIGGER = "CREATE TRIGGER folder_delete_notes_on_delete " + @@ -197,6 +208,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {//NotesDatabaseHelper /** * Move notes belong to folder which has been moved to trash folder + *移动笔记属于已移动到垃圾邮件文件夹的文件夹 */ private static final String FOLDER_MOVE_NOTES_ON_TRASH_TRIGGER = "CREATE TRIGGER folder_move_notes_on_trash " + @@ -220,6 +232,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {//NotesDatabaseHelper } 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"); @@ -227,12 +240,13 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {//NotesDatabaseHelper db.execSQL("DROP TRIGGER IF EXISTS increase_folder_count_on_insert"); 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); db.execSQL(NOTE_DELETE_DATA_ON_DELETE_TRIGGER); db.execSQL(NOTE_INCREASE_FOLDER_COUNT_ON_INSERT_TRIGGER); + // 创建文件夹表触发器,用于在删除文件夹时删除相关的笔记或将笔记移动到垃圾箱 db.execSQL(FOLDER_DELETE_NOTES_ON_DELETE_TRIGGER); db.execSQL(FOLDER_MOVE_NOTES_ON_TRASH_TRIGGER); } @@ -242,6 +256,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {//NotesDatabaseHelper /** * call record foler for call notes + *为通话记录创建文件夹 */ values.put(NoteColumns.ID, Notes.ID_CALL_RECORD_FOLDER); values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); @@ -249,6 +264,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {//NotesDatabaseHelper /** * root folder which is default folder + *设置根文件夹为默认文件夹 */ values.clear(); values.put(NoteColumns.ID, Notes.ID_ROOT_FOLDER); @@ -257,6 +273,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {//NotesDatabaseHelper /** * temporary folder which is used for moving note + *创建临时文件夹用于移动笔记 */ values.clear(); values.put(NoteColumns.ID, Notes.ID_TEMPARAY_FOLDER); @@ -265,6 +282,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {//NotesDatabaseHelper /** * create trash folder + *创建垃圾箱文件夹 */ values.clear(); values.put(NoteColumns.ID, Notes.ID_TRASH_FOLER); @@ -280,10 +298,11 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {//NotesDatabaseHelper } private void reCreateDataTableTriggers(SQLiteDatabase db) { + // 在删除数据时,如果相关触发器已存在,则先删除它 db.execSQL("DROP TRIGGER IF EXISTS update_note_content_on_insert"); db.execSQL("DROP TRIGGER IF EXISTS update_note_content_on_update"); db.execSQL("DROP TRIGGER IF EXISTS update_note_content_on_delete"); - + // 重新创建触发器,以便在数据表中进行插入操作时自动更新笔记内容 db.execSQL(DATA_UPDATE_NOTE_CONTENT_ON_INSERT_TRIGGER); db.execSQL(DATA_UPDATE_NOTE_CONTENT_ON_UPDATE_TRIGGER); db.execSQL(DATA_UPDATE_NOTE_CONTENT_ON_DELETE_TRIGGER); @@ -306,29 +325,29 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {//NotesDatabaseHelper public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { boolean reCreateTriggers = false; boolean skipV2 = false; - + // 如果旧版本为1,则执行从版本1升级到版本2的操作 if (oldVersion == 1) { upgradeToV2(db); - skipV2 = true; // this upgrade including the upgrade from v2 to v3 + skipV2 = true; // this upgrade including the upgrade from v2 to v3 此升级包括从v2到v3的升级 oldVersion++; } - + // 如果旧版本为2且不跳过v2,则执行从版本2升级到版本3的操作 if (oldVersion == 2 && !skipV2) { upgradeToV3(db); reCreateTriggers = true; oldVersion++; } - + // 如果旧版本为3,则执行从版本3升级到版本4的操作 if (oldVersion == 3) { upgradeToV4(db); oldVersion++; } - + // 如果需要重新创建触发器,则重新创建笔记表和数据表的触发器 if (reCreateTriggers) { reCreateNoteTableTriggers(db); reCreateDataTableTriggers(db); } - + // 如果旧版本号不等于新版本号,则抛出异常,表示升级失败 if (oldVersion != newVersion) { throw new IllegalStateException("Upgrade notes database to version " + newVersion + "fails"); @@ -343,14 +362,14 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {//NotesDatabaseHelper } private void upgradeToV3(SQLiteDatabase db) { - // drop unused triggers + // drop unused triggers删除已存在的触发器 db.execSQL("DROP TRIGGER IF EXISTS update_note_modified_date_on_insert"); db.execSQL("DROP TRIGGER IF EXISTS update_note_modified_date_on_delete"); db.execSQL("DROP TRIGGER IF EXISTS update_note_modified_date_on_update"); - // add a column for gtask id + // add a column for gtask id 为gtask id添加一列 db.execSQL("ALTER TABLE " + TABLE.NOTE + " ADD COLUMN " + NoteColumns.GTASK_ID + " TEXT NOT NULL DEFAULT ''"); - // add a trash system folder + // add a trash system folder 添加垃圾系统文件夹 ContentValues values = new ContentValues(); values.put(NoteColumns.ID, Notes.ID_TRASH_FOLER); values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM);