|
|
|
|
@ -26,20 +26,31 @@ import net.micode.notes.data.Notes.DataColumns;
|
|
|
|
|
import net.micode.notes.data.Notes.DataConstants;
|
|
|
|
|
import net.micode.notes.data.Notes.NoteColumns;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 笔记数据库帮助类
|
|
|
|
|
* 负责创建和管理SQLite数据库,包括笔记表和数据表
|
|
|
|
|
*/
|
|
|
|
|
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";
|
|
|
|
|
|
|
|
|
|
/** 数据表名 */
|
|
|
|
|
public static final String DATA = "data";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static final String TAG = "NotesDatabaseHelper";
|
|
|
|
|
|
|
|
|
|
/** 数据库帮助类的单例实例 */
|
|
|
|
|
private static NotesDatabaseHelper mInstance;
|
|
|
|
|
|
|
|
|
|
private static final String CREATE_NOTE_TABLE_SQL =
|
|
|
|
|
@ -83,7 +94,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
|
|
|
|
|
TABLE.DATA + "(" + DataColumns.NOTE_ID + ");";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 "+
|
|
|
|
|
@ -95,7 +106,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
|
|
|
|
|
" END";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 " +
|
|
|
|
|
@ -108,7 +119,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
|
|
|
|
|
" END";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 " +
|
|
|
|
|
@ -120,7 +131,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
|
|
|
|
|
" END";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 " +
|
|
|
|
|
@ -133,7 +144,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
|
|
|
|
|
" END";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update note's content when insert data with type {@link DataConstants#NOTE}
|
|
|
|
|
* 触发器:当插入类型为{@link DataConstants#NOTE}的数据时,更新笔记内容
|
|
|
|
|
*/
|
|
|
|
|
private static final String DATA_UPDATE_NOTE_CONTENT_ON_INSERT_TRIGGER =
|
|
|
|
|
"CREATE TRIGGER update_note_content_on_insert " +
|
|
|
|
|
@ -146,7 +157,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
|
|
|
|
|
" END";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update note's content when data with {@link DataConstants#NOTE} type has changed
|
|
|
|
|
* 触发器:当类型为{@link DataConstants#NOTE}的数据更新时,更新笔记内容
|
|
|
|
|
*/
|
|
|
|
|
private static final String DATA_UPDATE_NOTE_CONTENT_ON_UPDATE_TRIGGER =
|
|
|
|
|
"CREATE TRIGGER update_note_content_on_update " +
|
|
|
|
|
@ -159,7 +170,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
|
|
|
|
|
" END";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update note's content when data with {@link DataConstants#NOTE} type has deleted
|
|
|
|
|
* 触发器:当类型为{@link DataConstants#NOTE}的数据删除时,更新笔记内容
|
|
|
|
|
*/
|
|
|
|
|
private static final String DATA_UPDATE_NOTE_CONTENT_ON_DELETE_TRIGGER =
|
|
|
|
|
"CREATE TRIGGER update_note_content_on_delete " +
|
|
|
|
|
@ -172,7 +183,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
|
|
|
|
|
" END";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Delete datas belong to note which has been deleted
|
|
|
|
|
* 触发器:当笔记被删除时,删除属于该笔记的所有数据
|
|
|
|
|
*/
|
|
|
|
|
private static final String NOTE_DELETE_DATA_ON_DELETE_TRIGGER =
|
|
|
|
|
"CREATE TRIGGER delete_data_on_delete " +
|
|
|
|
|
@ -183,7 +194,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
|
|
|
|
|
" END";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 " +
|
|
|
|
|
@ -194,7 +205,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
|
|
|
|
|
" END";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 " +
|
|
|
|
|
@ -206,10 +217,18 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
|
|
|
|
|
" WHERE " + NoteColumns.PARENT_ID + "=old." + NoteColumns.ID + ";" +
|
|
|
|
|
" END";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造函数
|
|
|
|
|
* @param context 上下文对象
|
|
|
|
|
*/
|
|
|
|
|
public NotesDatabaseHelper(Context context) {
|
|
|
|
|
super(context, DB_NAME, null, DB_VERSION);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建笔记表
|
|
|
|
|
* @param db 数据库对象
|
|
|
|
|
*/
|
|
|
|
|
public void createNoteTable(SQLiteDatabase db) {
|
|
|
|
|
db.execSQL(CREATE_NOTE_TABLE_SQL);
|
|
|
|
|
reCreateNoteTableTriggers(db);
|
|
|
|
|
@ -217,6 +236,10 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
|
|
|
|
|
Log.d(TAG, "note table has been created");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 重新创建笔记表的触发器
|
|
|
|
|
* @param 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");
|
|
|
|
|
@ -235,18 +258,22 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
|
|
|
|
|
db.execSQL(FOLDER_MOVE_NOTES_ON_TRASH_TRIGGER);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建系统文件夹
|
|
|
|
|
* @param db 数据库对象
|
|
|
|
|
*/
|
|
|
|
|
private void createSystemFolder(SQLiteDatabase db) {
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* call record foler for call notes
|
|
|
|
|
* 创建通话记录文件夹
|
|
|
|
|
*/
|
|
|
|
|
values.put(NoteColumns.ID, Notes.ID_CALL_RECORD_FOLDER);
|
|
|
|
|
values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM);
|
|
|
|
|
db.insert(TABLE.NOTE, null, values);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* root folder which is default folder
|
|
|
|
|
* 创建根文件夹(默认文件夹)
|
|
|
|
|
*/
|
|
|
|
|
values.clear();
|
|
|
|
|
values.put(NoteColumns.ID, Notes.ID_ROOT_FOLDER);
|
|
|
|
|
@ -254,7 +281,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
|
|
|
|
|
db.insert(TABLE.NOTE, null, values);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* temporary folder which is used for moving note
|
|
|
|
|
* 创建临时文件夹(用于移动笔记)
|
|
|
|
|
*/
|
|
|
|
|
values.clear();
|
|
|
|
|
values.put(NoteColumns.ID, Notes.ID_TEMPARAY_FOLDER);
|
|
|
|
|
@ -262,7 +289,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
|
|
|
|
|
db.insert(TABLE.NOTE, null, values);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* create trash folder
|
|
|
|
|
* 创建回收站文件夹
|
|
|
|
|
*/
|
|
|
|
|
values.clear();
|
|
|
|
|
values.put(NoteColumns.ID, Notes.ID_TRASH_FOLER);
|
|
|
|
|
@ -270,6 +297,10 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
|
|
|
|
|
db.insert(TABLE.NOTE, null, values);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建数据表
|
|
|
|
|
* @param db 数据库对象
|
|
|
|
|
*/
|
|
|
|
|
public void createDataTable(SQLiteDatabase db) {
|
|
|
|
|
db.execSQL(CREATE_DATA_TABLE_SQL);
|
|
|
|
|
reCreateDataTableTriggers(db);
|
|
|
|
|
@ -277,6 +308,10 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
|
|
|
|
|
Log.d(TAG, "data table has been created");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 重新创建数据表的触发器
|
|
|
|
|
* @param db 数据库对象
|
|
|
|
|
*/
|
|
|
|
|
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");
|
|
|
|
|
@ -287,6 +322,11 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
|
|
|
|
|
db.execSQL(DATA_UPDATE_NOTE_CONTENT_ON_DELETE_TRIGGER);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取数据库帮助类的单例实例
|
|
|
|
|
* @param context 上下文对象
|
|
|
|
|
* @return 数据库帮助类实例
|
|
|
|
|
*/
|
|
|
|
|
static synchronized NotesDatabaseHelper getInstance(Context context) {
|
|
|
|
|
if (mInstance == null) {
|
|
|
|
|
mInstance = new NotesDatabaseHelper(context);
|
|
|
|
|
@ -294,12 +334,18 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
|
|
|
|
|
return mInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建数据库
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void onCreate(SQLiteDatabase db) {
|
|
|
|
|
createNoteTable(db);
|
|
|
|
|
createDataTable(db);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 升级数据库
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
|
|
|
|
boolean reCreateTriggers = false;
|
|
|
|
|
@ -333,6 +379,10 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 升级数据库到版本2
|
|
|
|
|
* @param db 数据库对象
|
|
|
|
|
*/
|
|
|
|
|
private void upgradeToV2(SQLiteDatabase db) {
|
|
|
|
|
db.execSQL("DROP TABLE IF EXISTS " + TABLE.NOTE);
|
|
|
|
|
db.execSQL("DROP TABLE IF EXISTS " + TABLE.DATA);
|
|
|
|
|
@ -340,21 +390,29 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
|
|
|
|
|
createDataTable(db);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 升级数据库到版本3
|
|
|
|
|
* @param db 数据库对象
|
|
|
|
|
*/
|
|
|
|
|
private void upgradeToV3(SQLiteDatabase db) {
|
|
|
|
|
// 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
|
|
|
|
|
// 添加gtask_id列
|
|
|
|
|
db.execSQL("ALTER TABLE " + TABLE.NOTE + " ADD COLUMN " + NoteColumns.GTASK_ID
|
|
|
|
|
+ " TEXT NOT NULL DEFAULT ''");
|
|
|
|
|
// add a trash system folder
|
|
|
|
|
// 添加回收站系统文件夹
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
|
values.put(NoteColumns.ID, Notes.ID_TRASH_FOLER);
|
|
|
|
|
values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM);
|
|
|
|
|
db.insert(TABLE.NOTE, null, values);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 升级数据库到版本4
|
|
|
|
|
* @param db 数据库对象
|
|
|
|
|
*/
|
|
|
|
|
private void upgradeToV4(SQLiteDatabase db) {
|
|
|
|
|
db.execSQL("ALTER TABLE " + TABLE.NOTE + " ADD COLUMN " + NoteColumns.VERSION
|
|
|
|
|
+ " INTEGER NOT NULL DEFAULT 0");
|
|
|
|
|
|