管理数据的创建和版本更新 更新笔记内容 版本更新

chq_branch
Ssr 2 years ago
parent cefa9bec0c
commit 04d48015e0

@ -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提供的一个用于存储数据库表中的键值对数据的类。
@ -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);

Loading…
Cancel
Save