|
|
/*
|
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
|
*
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
* You may obtain a copy of the License at
|
|
|
*
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
*
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
* See the License for the specific language governing permissions and
|
|
|
* limitations under the License.
|
|
|
*/
|
|
|
|
|
|
package net.micode.notes.data;
|
|
|
|
|
|
import android.content.ContentValues;
|
|
|
import android.content.Context;
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
import android.database.sqlite.SQLiteOpenHelper;
|
|
|
import android.util.Log;
|
|
|
|
|
|
import net.micode.notes.data.Notes.DataColumns;
|
|
|
import net.micode.notes.data.Notes.DataConstants;
|
|
|
import net.micode.notes.data.Notes.NoteColumns;
|
|
|
|
|
|
// 笔记数据库助手类,继承自SQLiteOpenHelper
|
|
|
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;
|
|
|
// 创建笔记表的SQL语句常量
|
|
|
private static final String CREATE_NOTE_TABLE_SQL =
|
|
|
"CREATE TABLE " + TABLE.NOTE + "(" +
|
|
|
NoteColumns.ID + " INTEGER PRIMARY KEY," +// 主键,笔记ID
|
|
|
NoteColumns.PARENT_ID + " INTEGER NOT NULL DEFAULT 0," +// 父笔记ID,非空,默认值为0
|
|
|
NoteColumns.ALERTED_DATE + " INTEGER NOT NULL DEFAULT 0," +// 提醒日期,非空,默认值为0
|
|
|
NoteColumns.BG_COLOR_ID + " INTEGER NOT NULL DEFAULT 0," +// 背景颜色ID,非空,默认值为0
|
|
|
NoteColumns.CREATED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," +// 创建日期,非空,默认为当前时间戳
|
|
|
NoteColumns.HAS_ATTACHMENT + " INTEGER NOT NULL DEFAULT 0," + // 是否有附件,非空,默认值为0
|
|
|
NoteColumns.MODIFIED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," +// 修改日期,非空,默认为当前时间戳
|
|
|
NoteColumns.NOTES_COUNT + " INTEGER NOT NULL DEFAULT 0," +// 笔记计数,非空,默认值为0
|
|
|
NoteColumns.SNIPPET + " TEXT NOT NULL DEFAULT ''," +// 笔记摘要,非空,默认为空字符串
|
|
|
NoteColumns.TYPE + " INTEGER NOT NULL DEFAULT 0," + // 笔记类型,非空,默认值为0
|
|
|
NoteColumns.WIDGET_ID + " INTEGER NOT NULL DEFAULT 0," +// 小部件ID,非空,默认值为0
|
|
|
NoteColumns.WIDGET_TYPE + " INTEGER NOT NULL DEFAULT -1," + // 小部件类型,非空,默认值为-1
|
|
|
NoteColumns.SYNC_ID + " INTEGER NOT NULL DEFAULT 0," + // 同步ID,非空,默认值为0
|
|
|
NoteColumns.LOCAL_MODIFIED + " INTEGER NOT NULL DEFAULT 0," + // 本地修改标记,非空,默认值为0
|
|
|
NoteColumns.ORIGIN_PARENT_ID + " INTEGER NOT NULL DEFAULT 0," +// 原始父笔记ID,非空,默认值为0
|
|
|
NoteColumns.GTASK_ID + " TEXT NOT NULL DEFAULT ''," + // GTask ID,非空,默认为空字符串
|
|
|
NoteColumns.VERSION + " INTEGER NOT NULL DEFAULT 0" + // 版本号,整型,非空,默认值0
|
|
|
")";
|
|
|
|
|
|
// 定义创建数据表的SQL语句常量
|
|
|
private static final String CREATE_DATA_TABLE_SQL =
|
|
|
"CREATE TABLE " + TABLE.DATA + "(" +
|
|
|
DataColumns.ID + " INTEGER PRIMARY KEY," + // 数据ID,整型,主键
|
|
|
DataColumns.MIME_TYPE + " TEXT NOT NULL," +// MIME类型,文本,非空
|
|
|
DataColumns.NOTE_ID + " INTEGER NOT NULL DEFAULT 0," +// 关联的笔记ID,整型,非空,默认值0
|
|
|
NoteColumns.CREATED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," +// 创建日期,整型,非空,默认值为当前时间戳(单位:毫秒)
|
|
|
NoteColumns.MODIFIED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," +// 修改日期,整型,非空,默认值为当前时间戳(单位:毫秒)
|
|
|
DataColumns.CONTENT + " TEXT NOT NULL DEFAULT ''," +// 内容,文本,非空,默认值空字符串
|
|
|
DataColumns.DATA1 + " INTEGER," +// 数据1,整型
|
|
|
DataColumns.DATA2 + " INTEGER," + // 数据2,整型
|
|
|
DataColumns.DATA3 + " TEXT NOT NULL DEFAULT ''," +// 数据3,文本,非空,默认值空字符串
|
|
|
DataColumns.DATA4 + " TEXT NOT NULL DEFAULT ''," +// 数据4,文本,非空,默认值空字符串
|
|
|
DataColumns.DATA5 + " TEXT NOT NULL DEFAULT ''" +// 数据5,文本,非空,默认值空字符串
|
|
|
")";
|
|
|
// 定义创建数据表中笔记ID索引的SQL语句常量
|
|
|
private static final String CREATE_DATA_NOTE_ID_INDEX_SQL =
|
|
|
"CREATE INDEX IF NOT EXISTS note_id_index ON " +
|
|
|
TABLE.DATA + "(" + DataColumns.NOTE_ID + ");";// 如果不存在,则创建一个名为note_id_index的索引,基于关联的笔记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 "+ // 创建一个名为increase_folder_count_on_update的触发器
|
|
|
" AFTER UPDATE OF " + NoteColumns.PARENT_ID + " ON " + TABLE.NOTE + // 在更新笔记表中的父ID字段后触发
|
|
|
" BEGIN " +// 触发器执行开始
|
|
|
" UPDATE " + TABLE.NOTE +// 更新笔记表
|
|
|
" SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + " + 1" +// 将笔记计数加1
|
|
|
" WHERE " + NoteColumns.ID + "=new." + NoteColumns.PARENT_ID + ";" +// 条件是当前更新的笔记的父ID
|
|
|
" 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 " +// 创建一个名为decrease_folder_count_on_update的触发器
|
|
|
" AFTER UPDATE OF " + NoteColumns.PARENT_ID + " ON " + TABLE.NOTE + // 在更新笔记表中的父ID字段后触发
|
|
|
" BEGIN " +// 触发器执行开始
|
|
|
" UPDATE " + TABLE.NOTE +// 更新笔记表
|
|
|
" SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + "-1" +// 将笔记计数减1
|
|
|
" WHERE " + NoteColumns.ID + "=old." + NoteColumns.PARENT_ID + // 条件是更新前的笔记的父ID
|
|
|
" AND " + NoteColumns.NOTES_COUNT + ">0" + ";" +// 并且笔记计数大于0
|
|
|
" 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 " + // 创建一个名为increase_folder_count_on_insert的触发器
|
|
|
" AFTER INSERT ON " + TABLE.NOTE + // 在向笔记表中插入新记录后触发
|
|
|
" BEGIN " + // 触发器执行开始
|
|
|
" UPDATE " + TABLE.NOTE +// 更新笔记表
|
|
|
" SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + " + 1" +// 将笔记计数加1
|
|
|
" WHERE " + NoteColumns.ID + "=new." + NoteColumns.PARENT_ID + ";" +// 条件是插入的新笔记的父ID
|
|
|
" 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 " + // 创建一个名为decrease_folder_count_on_delete的触发器
|
|
|
" AFTER DELETE ON " + TABLE.NOTE +// 在从笔记表中删除记录后触发
|
|
|
" BEGIN " + // 触发器执行开始
|
|
|
" UPDATE " + TABLE.NOTE +// 更新笔记表
|
|
|
" SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + "-1" +// 将笔记计数减1
|
|
|
" WHERE " + NoteColumns.ID + "=old." + NoteColumns.PARENT_ID + // 条件是删除的笔记的父I
|
|
|
" AND " + NoteColumns.NOTES_COUNT + ">0;" + // 并且笔记计数大于0
|
|
|
" 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 " + // 创建一个名为update_note_content_on_insert的触发器
|
|
|
" AFTER INSERT ON " + TABLE.DATA +// 在数据表插入新记录后触发
|
|
|
" WHEN new." + DataColumns.MIME_TYPE + "='" + DataConstants.NOTE + "'" + // 当新记录的类型为笔记时
|
|
|
" BEGIN" + // 触发器执行开始
|
|
|
" UPDATE " + TABLE.NOTE +// 更新笔记表
|
|
|
" SET " + NoteColumns.SNIPPET + "=new." + DataColumns.CONTENT + // 将笔记的摘要设置为新的内容
|
|
|
" WHERE " + NoteColumns.ID + "=new." + DataColumns.NOTE_ID + ";" +// 条件是笔记ID与新记录的笔记ID相匹配
|
|
|
" 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 " +// 创建一个名为update_note_content_on_update的触发器
|
|
|
" AFTER UPDATE ON " + TABLE.DATA + // 在数据表更新记录后触发
|
|
|
" WHEN old." + DataColumns.MIME_TYPE + "='" + DataConstants.NOTE + "'" + // 当被更新记录的类型为笔记时
|
|
|
" BEGIN" +// 触发器执行开始
|
|
|
" UPDATE " + TABLE.NOTE +// 更新笔记表
|
|
|
" SET " + NoteColumns.SNIPPET + "=new." + DataColumns.CONTENT +// 将笔记的摘要设置为新的内容
|
|
|
" WHERE " + NoteColumns.ID + "=new." + DataColumns.NOTE_ID + ";" + // 条件是笔记ID与新记录的笔记ID相匹配
|
|
|
" 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 " + // 创建一个名为update_note_content_on_delete的触发器
|
|
|
" AFTER delete ON " + TABLE.DATA +// 在数据表删除记录后触发
|
|
|
" WHEN old." + DataColumns.MIME_TYPE + "='" + DataConstants.NOTE + "'" + // 当被删除记录的类型为笔记时
|
|
|
" BEGIN" +// 触发器执行开始
|
|
|
" UPDATE " + TABLE.NOTE +// 更新笔记表
|
|
|
" SET " + NoteColumns.SNIPPET + "=''" +// 将笔记的摘要设置为空字符串
|
|
|
" WHERE " + NoteColumns.ID + "=old." + DataColumns.NOTE_ID + ";" + // 条件是笔记ID与被删除记录的笔记ID相匹配
|
|
|
" 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 " +
|
|
|
" AFTER DELETE ON " + TABLE.NOTE +
|
|
|
" BEGIN" +
|
|
|
" DELETE FROM " + TABLE.DATA +
|
|
|
" WHERE " + DataColumns.NOTE_ID + "=old." + NoteColumns.ID + ";" +
|
|
|
" 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 " +// 创建一个名为delete_data_on_delete的触发器
|
|
|
" AFTER DELETE ON " + TABLE.NOTE + // 在笔记表删除记录后触发
|
|
|
" BEGIN" +// 触发器执行开始
|
|
|
" DELETE FROM " + TABLE.NOTE + // 从数据表中删除记录
|
|
|
" WHERE " + NoteColumns.PARENT_ID + "=old." + NoteColumns.ID + ";" + // 条件是数据记录的笔记ID与被删除笔记的ID相匹配
|
|
|
" 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 " + // 创建名为folder_move_notes_on_trash的触发器
|
|
|
" AFTER UPDATE ON " + TABLE.NOTE +// 在更新NOTE表后触发
|
|
|
" WHEN new." + NoteColumns.PARENT_ID + "=" + Notes.ID_TRASH_FOLER + // 当新记录的PARENT_ID等于垃圾文件夹的ID时
|
|
|
" BEGIN" + // 开始触发器执行的操作
|
|
|
" UPDATE " + TABLE.NOTE +// 更新NOTE表
|
|
|
" SET " + NoteColumns.PARENT_ID + "=" + Notes.ID_TRASH_FOLER + // 将PARENT_ID设置为垃圾文件夹的ID
|
|
|
" WHERE " + NoteColumns.PARENT_ID + "=old." + NoteColumns.ID + ";" +// 条件是PARENT_ID等于被更新记录的ID
|
|
|
" END";// 结束触发器
|
|
|
|
|
|
public NotesDatabaseHelper(Context context) {
|
|
|
super(context, DB_NAME, null, DB_VERSION);// 调用父类构造函数,初始化数据库帮助器
|
|
|
}
|
|
|
|
|
|
public void createNoteTable(SQLiteDatabase db) {
|
|
|
db.execSQL(CREATE_NOTE_TABLE_SQL); // 执行创建笔记表的SQL语句
|
|
|
reCreateNoteTableTriggers(db);// 重新创建笔记表相关的触发器
|
|
|
createSystemFolder(db);// 创建系统文件夹
|
|
|
Log.d(TAG, "note table has been created");// 在日志中记录笔记表已创建
|
|
|
}
|
|
|
|
|
|
private void reCreateNoteTableTriggers(SQLiteDatabase db) {
|
|
|
db.execSQL("DROP TRIGGER IF EXISTS increase_folder_count_on_update");// 如果存在increase_folder_count_on_update触发器,则删除
|
|
|
db.execSQL("DROP TRIGGER IF EXISTS decrease_folder_count_on_update");// 如果存在decrease_folder_count_on_update触发器,则删除
|
|
|
db.execSQL("DROP TRIGGER IF EXISTS decrease_folder_count_on_delete");// 如果存在decrease_folder_count_on_delete触发器,则删除
|
|
|
db.execSQL("DROP TRIGGER IF EXISTS delete_data_on_delete");// 如果存在delete_data_on_delete触发器,则删除
|
|
|
db.execSQL("DROP TRIGGER IF EXISTS increase_folder_count_on_insert"); // 如果存在increase_folder_count_on_insert触发器,则删除
|
|
|
db.execSQL("DROP TRIGGER IF EXISTS folder_delete_notes_on_delete");// 如果存在folder_delete_notes_on_delete触发器,则删除
|
|
|
db.execSQL("DROP TRIGGER IF EXISTS folder_move_notes_on_trash");// 如果存在folder_move_notes_on_trash触发器,则删除
|
|
|
|
|
|
db.execSQL(NOTE_INCREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER);// 执行增加文件夹计数更新触发器的SQL语句
|
|
|
db.execSQL(NOTE_DECREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER);// 执行减少文件夹计数更新触发器的SQL语句
|
|
|
db.execSQL(NOTE_DECREASE_FOLDER_COUNT_ON_DELETE_TRIGGER);// 执行减少文件夹计数删除触发器的SQL语句
|
|
|
db.execSQL(NOTE_DELETE_DATA_ON_DELETE_TRIGGER); // 执行删除数据删除触发器的SQL语句
|
|
|
db.execSQL(NOTE_INCREASE_FOLDER_COUNT_ON_INSERT_TRIGGER);// 执行增加文件夹计数插入触发器的SQL语句
|
|
|
db.execSQL(FOLDER_DELETE_NOTES_ON_DELETE_TRIGGER); // 执行文件夹删除笔记删除触发器的SQL语句
|
|
|
db.execSQL(FOLDER_MOVE_NOTES_ON_TRASH_TRIGGER);// 执行文件夹移动笔记到垃圾文件夹触发器的SQL语句
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM);
|
|
|
db.insert(TABLE.NOTE, null, values);
|
|
|
|
|
|
/**
|
|
|
* temporary folder which is used for moving note
|
|
|
*/
|
|
|
|
|
|
/**
|
|
|
* 临时文件夹,用于移动笔记
|
|
|
*/
|
|
|
values.clear();
|
|
|
values.put(NoteColumns.ID, Notes.ID_TEMPARAY_FOLDER);
|
|
|
values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM);
|
|
|
db.insert(TABLE.NOTE, null, values);
|
|
|
|
|
|
/**
|
|
|
* create trash folder
|
|
|
*/
|
|
|
/**
|
|
|
* 创建垃圾文件夹
|
|
|
*/
|
|
|
values.clear();
|
|
|
values.put(NoteColumns.ID, Notes.ID_TRASH_FOLER);
|
|
|
values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM);
|
|
|
db.insert(TABLE.NOTE, null, values);
|
|
|
}
|
|
|
/**
|
|
|
* 创建数据表,并重新创建相关触发器,同时创建数据表索引
|
|
|
*/
|
|
|
public void createDataTable(SQLiteDatabase db) {
|
|
|
db.execSQL(CREATE_DATA_TABLE_SQL); // 执行创建数据表的SQL语句
|
|
|
reCreateDataTableTriggers(db);// 重新创建数据表相关的触发器
|
|
|
db.execSQL(CREATE_DATA_NOTE_ID_INDEX_SQL);// 执行创建数据表索引的SQL语句
|
|
|
Log.d(TAG, "data table has been created");// 在日志中记录数据表已创建
|
|
|
}
|
|
|
/**
|
|
|
* 重新创建数据表的触发器
|
|
|
*/
|
|
|
private void reCreateDataTableTriggers(SQLiteDatabase db) {
|
|
|
db.execSQL("DROP TRIGGER IF EXISTS update_note_content_on_insert"); // 如果存在update_note_content_on_insert触发器,则删除
|
|
|
db.execSQL("DROP TRIGGER IF EXISTS update_note_content_on_update"); // 如果存在update_note_content_on_update触发器,则删除
|
|
|
db.execSQL("DROP TRIGGER IF EXISTS update_note_content_on_delete");// 如果存在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);// 执行删除时更新笔记内容的触发器
|
|
|
}
|
|
|
|
|
|
static synchronized NotesDatabaseHelper getInstance(Context context) {// 如果单例实例为空,则创建一个新的实例
|
|
|
if (mInstance == null) {
|
|
|
mInstance = new NotesDatabaseHelper(context);
|
|
|
} // 返回单例实例
|
|
|
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;// 是否需要重新创建触发器
|
|
|
boolean skipV2 = false;// 是否跳过版本2的升级
|
|
|
// 如果当前版本是1,则升级到版本2
|
|
|
if (oldVersion == 1) {
|
|
|
upgradeToV2(db);// 执行版本2的升级操作
|
|
|
skipV2 = true; // this upgrade including the upgrade from v2 to v3// 标记已处理版本2的升级,包括从版本2升级到版本3
|
|
|
oldVersion++;// 更新当前版本号
|
|
|
}
|
|
|
// 如果当前版本是2且没有跳过版本2的升级,则升级到版本3
|
|
|
if (oldVersion == 2 && !skipV2) {
|
|
|
upgradeToV3(db);// 执行版本3的升级操作
|
|
|
reCreateTriggers = true;// 标记需要重新创建触发器
|
|
|
oldVersion++;// 更新当前版本号
|
|
|
}
|
|
|
// 如果当前版本是3,则升级到版本4
|
|
|
if (oldVersion == 3) {
|
|
|
upgradeToV4(db); // 执行版本4的升级操作
|
|
|
oldVersion++;// 更新当前版本号
|
|
|
}
|
|
|
// 如果需要重新创建触发器,则执行相关操作
|
|
|
if (reCreateTriggers) {
|
|
|
reCreateNoteTableTriggers(db);// 重新创建笔记表的触发器
|
|
|
reCreateDataTableTriggers(db);// 重新创建数据表的触发器
|
|
|
}
|
|
|
// 如果当前版本号不等于新版本号,则抛出异常
|
|
|
if (oldVersion != newVersion) {
|
|
|
throw new IllegalStateException("Upgrade notes database to version " + newVersion
|
|
|
+ "fails");
|
|
|
}
|
|
|
}
|
|
|
/**
|
|
|
* 将数据库升级到版本2
|
|
|
*/
|
|
|
private void upgradeToV2(SQLiteDatabase db) {
|
|
|
db.execSQL("DROP TABLE IF EXISTS " + TABLE.NOTE); // 如果存在旧的笔记表,则删除
|
|
|
db.execSQL("DROP TABLE IF EXISTS " + TABLE.DATA);// 如果存在旧的数据表,则删除
|
|
|
createNoteTable(db); // 创建新的笔记表
|
|
|
createDataTable(db); // 创建新的数据表
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 将数据库升级到版本3
|
|
|
*/
|
|
|
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
|
|
|
// 为笔记表添加一个用于Google Task 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
|
|
|
*/
|
|
|
private void upgradeToV4(SQLiteDatabase db) {
|
|
|
// 为笔记表添加一个版本号列
|
|
|
db.execSQL("ALTER TABLE " + TABLE.NOTE + " ADD COLUMN " + NoteColumns.VERSION
|
|
|
+ " INTEGER NOT NULL DEFAULT 0");
|
|
|
}
|
|
|
}
|