/*
 * 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;


/**
 * 笔记数据库帮助类
 * <p>
 * 继承自SQLiteOpenHelper，负责笔记应用SQLite数据库的创建、升级和管理。
 * 管理两个主要数据表：note表（存储笔记和文件夹信息）和data表（存储笔记的详细内容）。
 * 使用数据库触发器自动维护笔记计数、内容同步等关联关系。
 * </p>
 * <p>
 * 主要功能：
 * <ul>
 * <li>创建和升级数据库表结构</li>
 * <li>创建和管理数据库触发器</li>
 * <li>维护系统文件夹（通话记录、根文件夹、临时文件夹、回收站）</li>
 * <li>支持数据库版本升级（当前版本：4）</li>
 * <li>提供单例模式访问数据库帮助类实例</li>
 * </ul>
 * </p>
 * <p>
 * 数据库版本历史：
 * <ul>
 * <li>V1: 初始版本</li>
 * <li>V2: 重构表结构</li>
 * <li>V3: 添加GTASK_ID列和回收站文件夹</li>
 * <li>V4: 添加VERSION列</li>
 * </ul>
 * </p>
 * 
 * @see SQLiteOpenHelper
 * @see Notes
 */
public class NotesDatabaseHelper extends SQLiteOpenHelper {
    /**
     * 数据库文件名
     */
    private static final String DB_NAME = "note.db";

    /**
     * 数据库版本号
     * <p>
     * 当前数据库版本为4，用于跟踪数据库结构变更。
     * 当数据库版本变更时，onUpgrade方法会被调用以执行升级逻辑。
     * </p>
     */
    private static final int DB_VERSION = 4;

    /**
     * 数据库表名常量接口
     */
    public interface TABLE {
        /**
         * 笔记表名
         * <p>
         * 存储笔记和文件夹的基本信息，包括ID、父文件夹ID、创建时间、修改时间、
         * 背景颜色、提醒时间、附件状态、笔记数量、摘要、类型、Widget信息、
         * 同步ID、本地修改状态、原始父文件夹ID、GTASK ID、版本等字段。
         * </p>
         */
        public static final String NOTE = "note";

        /**
         * 数据表名
         * <p>
         * 存储笔记的详细内容，支持多种MIME类型（文本、图片、附件等）。
         * 每条数据记录关联到一条笔记，包含MIME类型、内容、以及5个通用数据字段。
         * </p>
         */
        public static final String DATA = "data";
    }

    /**
     * 日志标签
     */
    private static final String TAG = "NotesDatabaseHelper";

    /**
     * 数据库帮助类单例实例
     * <p>
     * 使用单例模式确保全局只有一个数据库帮助类实例，
     * 避免多个实例同时操作数据库导致的数据不一致问题。
     * </p>
     */
    private static NotesDatabaseHelper mInstance;

    /**
     * 创建笔记表的SQL语句
     * <p>
     * 创建note表，包含以下字段：
     * <ul>
     * <li>ID: 主键，自增</li>
     * <li>PARENT_ID: 父文件夹ID，默认为0</li>
     * <li>ALERTED_DATE: 提醒时间，默认为0</li>
     * <li>BG_COLOR_ID: 背景颜色ID，默认为0</li>
     * <li>CREATED_DATE: 创建时间，默认为当前时间戳</li>
     * <li>HAS_ATTACHMENT: 是否有附件，默认为0</li>
     * <li>MODIFIED_DATE: 修改时间，默认为当前时间戳</li>
     * <li>NOTES_COUNT: 笔记数量，默认为0（仅文件夹有效）</li>
     * <li>SNIPPET: 笔记摘要，默认为空字符串</li>
     * <li>TYPE: 类型（0=普通笔记，1=文件夹，2=系统），默认为0</li>
     * <li>WIDGET_ID: Widget ID，默认为0</li>
     * <li>WIDGET_TYPE: Widget类型，默认为-1</li>
     * <li>SYNC_ID: 同步ID，默认为0</li>
     * <li>LOCAL_MODIFIED: 本地修改标志，默认为0</li>
     * <li>ORIGIN_PARENT_ID: 原始父文件夹ID，默认为0</li>
     * <li>GTASK_ID: Google Tasks ID，默认为空字符串</li>
     * <li>VERSION: 版本号，默认为0</li>
     * </ul>
     * </p>
     */
    private static final String CREATE_NOTE_TABLE_SQL =
        "CREATE TABLE " + TABLE.NOTE + "(" +
            NoteColumns.ID + " INTEGER PRIMARY KEY," +
            NoteColumns.PARENT_ID + " INTEGER NOT NULL DEFAULT 0," +
            NoteColumns.ALERTED_DATE + " INTEGER NOT NULL DEFAULT 0," +
            NoteColumns.BG_COLOR_ID + " INTEGER NOT NULL DEFAULT 0," +
            NoteColumns.CREATED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," +
            NoteColumns.HAS_ATTACHMENT + " INTEGER NOT NULL DEFAULT 0," +
            NoteColumns.MODIFIED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," +
            NoteColumns.NOTES_COUNT + " INTEGER NOT NULL DEFAULT 0," +
            NoteColumns.SNIPPET + " TEXT NOT NULL DEFAULT ''," +
            NoteColumns.TYPE + " INTEGER NOT NULL DEFAULT 0," +
            NoteColumns.WIDGET_ID + " INTEGER NOT NULL DEFAULT 0," +
            NoteColumns.WIDGET_TYPE + " INTEGER NOT NULL DEFAULT -1," +
            NoteColumns.SYNC_ID + " INTEGER NOT NULL DEFAULT 0," +
            NoteColumns.LOCAL_MODIFIED + " INTEGER NOT NULL DEFAULT 0," +
            NoteColumns.ORIGIN_PARENT_ID + " INTEGER NOT NULL DEFAULT 0," +
            NoteColumns.GTASK_ID + " TEXT NOT NULL DEFAULT ''," +
            NoteColumns.VERSION + " INTEGER NOT NULL DEFAULT 0" +
        ")";

    /**
     * 创建数据表的SQL语句
     * <p>
     * 创建data表，包含以下字段：
     * <ul>
     * <li>ID: 主键，自增</li>
     * <li>MIME_TYPE: MIME类型，不能为空</li>
     * <li>NOTE_ID: 关联的笔记ID，默认为0</li>
     * <li>CREATED_DATE: 创建时间，默认为当前时间戳</li>
     * <li>MODIFIED_DATE: 修改时间，默认为当前时间戳</li>
     * <li>CONTENT: 内容，默认为空字符串</li>
     * <li>DATA1-5: 通用数据字段，用于存储不同类型的数据</li>
     * </ul>
     * </p>
     */
    private static final String CREATE_DATA_TABLE_SQL =
        "CREATE TABLE " + TABLE.DATA + "(" +
            DataColumns.ID + " INTEGER PRIMARY KEY," +
            DataColumns.MIME_TYPE + " TEXT NOT NULL," +
            DataColumns.NOTE_ID + " INTEGER NOT NULL DEFAULT 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," +
            DataColumns.DATA2 + " INTEGER," +
            DataColumns.DATA3 + " TEXT NOT NULL DEFAULT ''," +
            DataColumns.DATA4 + " TEXT NOT NULL DEFAULT ''," +
            DataColumns.DATA5 + " TEXT NOT NULL DEFAULT ''" +
        ")";

    /**
     * 创建数据表索引的SQL语句
     * <p>
     * 在data表的NOTE_ID字段上创建索引，提高按笔记ID查询数据的效率。
     * </p>
     */
    private static final String CREATE_DATA_NOTE_ID_INDEX_SQL =
        "CREATE INDEX IF NOT EXISTS note_id_index ON " +
        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 "+
        " AFTER UPDATE OF " + NoteColumns.PARENT_ID + " ON " + TABLE.NOTE +
        " BEGIN " +
        "  UPDATE " + TABLE.NOTE +
        "   SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + " + 1" +
        "  WHERE " + NoteColumns.ID + "=new." + NoteColumns.PARENT_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 " +
        " AFTER UPDATE OF " + NoteColumns.PARENT_ID + " ON " + TABLE.NOTE +
        " BEGIN " +
        "  UPDATE " + TABLE.NOTE +
        "   SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + "-1" +
        "  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
     */
    private static final String NOTE_INCREASE_FOLDER_COUNT_ON_INSERT_TRIGGER =
        "CREATE TRIGGER increase_folder_count_on_insert " +
        " AFTER INSERT ON " + TABLE.NOTE +
        " BEGIN " +
        "  UPDATE " + TABLE.NOTE +
        "   SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + " + 1" +
        "  WHERE " + NoteColumns.ID + "=new." + NoteColumns.PARENT_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 " +
        " AFTER DELETE ON " + TABLE.NOTE +
        " BEGIN " +
        "  UPDATE " + TABLE.NOTE +
        "   SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + "-1" +
        "  WHERE " + NoteColumns.ID + "=old." + NoteColumns.PARENT_ID +
        "  AND " + NoteColumns.NOTES_COUNT + ">0;" +
        " END";

    /**
     * Update note's content when insert data with type {@link DataConstants#NOTE}
     */
    private static final String DATA_UPDATE_NOTE_CONTENT_ON_INSERT_TRIGGER =
        "CREATE TRIGGER 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 + ";" +
        " END";

    /**
     * Update note's content when data with {@link DataConstants#NOTE} type has changed
     */
    private static final String DATA_UPDATE_NOTE_CONTENT_ON_UPDATE_TRIGGER =
        "CREATE TRIGGER 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 + ";" +
        " END";

    /**
     * Update note's content when data with {@link DataConstants#NOTE} type has deleted
     */
    private static final String DATA_UPDATE_NOTE_CONTENT_ON_DELETE_TRIGGER =
        "CREATE TRIGGER 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 + ";" +
        " 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 " +
        " AFTER DELETE ON " + TABLE.NOTE +
        " BEGIN" +
        "  DELETE FROM " + TABLE.NOTE +
        "   WHERE " + NoteColumns.PARENT_ID + "=old." + NoteColumns.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 " +
        " AFTER UPDATE ON " + TABLE.NOTE +
        " WHEN new." + NoteColumns.PARENT_ID + "=" + Notes.ID_TRASH_FOLER +
        " BEGIN" +
        "  UPDATE " + TABLE.NOTE +
        "   SET " + NoteColumns.PARENT_ID + "=" + Notes.ID_TRASH_FOLER +
        "  WHERE " + NoteColumns.PARENT_ID + "=old." + NoteColumns.ID + ";" +
        " END";

    /**
     * 构造器
     * 
     * @param context 应用上下文
     */
    public NotesDatabaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    /**
     * 创建笔记表
     * <p>
     * 执行创建note表的SQL语句，创建相关触发器，并初始化系统文件夹。
     * </p>
     * 
     * @param db SQLiteDatabase实例
     */
    public void createNoteTable(SQLiteDatabase db) {
        db.execSQL(CREATE_NOTE_TABLE_SQL);
        reCreateNoteTableTriggers(db);
        createSystemFolder(db);
        Log.d(TAG, "note table has been created");
    }

    /**
     * 重新创建笔记表触发器
     * <p>
     * 先删除所有已存在的note表相关触发器，然后重新创建所有触发器。
     * 用于在数据库升级时更新触发器逻辑。
     * </p>
     * 
     * @param db SQLiteDatabase实例
     */
    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");
        db.execSQL("DROP TRIGGER IF EXISTS delete_data_on_delete");
        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);
    }

    /**
     * 创建系统文件夹
     * <p>
     * 在note表中创建四个系统文件夹：
     * <ul>
     * <li>通话记录文件夹（ID_CALL_RECORD_FOLDER）</li>
     * <li>根文件夹（ID_ROOT_FOLDER）</li>
     * <li>临时文件夹（ID_TEMPARAY_FOLDER）</li>
     * <li>回收站文件夹（ID_TRASH_FOLER）</li>
     * </ul>
     * </p>
     * 
     * @param db SQLiteDatabase实例
     */
    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);
    }

    /**
     * 创建数据表
     * <p>
     * 执行创建data表的SQL语句，创建相关触发器，并创建索引。
     * </p>
     * 
     * @param db SQLiteDatabase实例
     */
    public void createDataTable(SQLiteDatabase db) {
        db.execSQL(CREATE_DATA_TABLE_SQL);
        reCreateDataTableTriggers(db);
        db.execSQL(CREATE_DATA_NOTE_ID_INDEX_SQL);
        Log.d(TAG, "data table has been created");
    }

    /**
     * 重新创建数据表触发器
     * <p>
     * 先删除所有已存在的data表相关触发器，然后重新创建所有触发器。
     * 用于在数据库升级时更新触发器逻辑。
     * </p>
     * 
     * @param db SQLiteDatabase实例
     */
    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);
    }

    /**
     * 获取数据库帮助类单例实例
     * <p>
     * 使用双重检查锁定模式确保线程安全的单例实现。
     * </p>
     * 
     * @param context 应用上下文
     * @return NotesDatabaseHelper单例实例
     */
    static synchronized NotesDatabaseHelper getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new NotesDatabaseHelper(context);
        }
        return mInstance;
    }

    /**
     * 创建数据库
     * <p>
     * 当数据库文件不存在时调用，创建note表和data表。
     * </p>
     * 
     * @param db SQLiteDatabase实例
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        createNoteTable(db);
        createDataTable(db);
    }

    /**
     * 升级数据库
     * <p>
     * 当数据库版本号增加时调用，执行从旧版本到新版本的升级逻辑。
     * 支持增量升级，从当前版本逐步升级到目标版本。
     * </p>
     * 
     * @param db SQLiteDatabase实例
     * @param oldVersion 当前数据库版本号
     * @param newVersion 目标数据库版本号
     * @throws IllegalStateException 如果升级失败
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        boolean reCreateTriggers = false;
        boolean skipV2 = false;

        // 从V1升级到V2（包括V2到V3）
        if (oldVersion == 1) {
            upgradeToV2(db);
            skipV2 = true; // this upgrade including the upgrade from v2 to v3
            oldVersion++;
        }

        // 从V2升级到V3
        if (oldVersion == 2 && !skipV2) {
            upgradeToV3(db);
            reCreateTriggers = true;
            oldVersion++;
        }

        // 从V3升级到V4
        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");
        }
    }

    /**
     * 升级数据库到V2版本
     * <p>
     * 删除旧表并重新创建note表和data表。
     * </p>
     * 
     * @param db SQLiteDatabase实例
     */
    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);
    }

    /**
     * 升级数据库到V3版本
     * <p>
     * 添加GTASK_ID列到note表，并创建回收站系统文件夹。
     * </p>
     * 
     * @param db SQLiteDatabase实例
     */
    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);
    }

    /**
     * 升级数据库到V4版本
     * <p>
     * 添加VERSION列到note表，用于跟踪笔记版本。
     * </p>
     * 
     * @param db SQLiteDatabase实例
     */
    private void upgradeToV4(SQLiteDatabase db) {
        db.execSQL("ALTER TABLE " + TABLE.NOTE + " ADD COLUMN " + NoteColumns.VERSION
                + " INTEGER NOT NULL DEFAULT 0");
    }
}
