import net.micode.notes.gtask.exception.ActionFailureException; import org.json.JSONException; import org.json.JSONObject; /** * SqlData类用于处理与数据库相关的操作,主要负责将JSON数据与数据库记录进行相互转换和更新 */ public class SqlData { // 日志标签,用于标识日志来源 private static final String TAG = SqlData.class.getSimpleName(); // 无效的ID标识,用于表示未初始化或无效的数据库ID private static final int INVALID_ID = -99999; // 数据库查询投影,定义了需要查询的列 public static final String[] PROJECTION_DATA = new String[] { DataColumns.ID, DataColumns.MIME_TYPE, DataColumns.CONTENT, DataColumns.DATA1, DataColumns.DATA3 }; // 下面是数据库查询结果中各列的索引位置 public static final int DATA_ID_COLUMN = 0; public static final int DATA_MIME_TYPE_COLUMN = 1; public static final int DATA_CONTENT_COLUMN = 2; public static final int DATA_CONTENT_DATA_1_COLUMN = 3; public static final int DATA_CONTENT_DATA_3_COLUMN = 4; // ContentResolver用于访问内容提供者 private ContentResolver mContentResolver; // 标识当前对象是否是新创建的 private boolean mIsCreate; // 下面是数据库记录的各项字段 private long mDataId; private String mDataMimeType; private String mDataContent; private long mDataContentData1; private String mDataContentData3; // 用于存储待更新的数据库记录值 private ContentValues mDiffDataValues; /** * 构造函数,用于创建新的SqlData对象 * @param context 上下文环境,用于获取ContentResolver */ public SqlData(Context context) { mContentResolver = context.getContentResolver(); mIsCreate = true; mDataId = INVALID_ID; mDataMimeType = DataConstants.NOTE; mDataContent = ""; mDataContentData1 = 0; mDataContentData3 = ""; mDiffDataValues = new ContentValues(); } /** * 构造函数,用于从现有的数据库记录创建SqlData对象 * @param context 上下文环境,用于获取ContentResolver * @param c 游标对象,指向数据库记录 */ public SqlData(Context context, Cursor c) { mContentResolver = context.getContentResolver(); mIsCreate = false; loadFromCursor(c); mDiffDataValues = new ContentValues(); } /** * 从游标中加载数据库记录 * @param c 游标对象,指向数据库记录 */ private void loadFromCursor(Cursor c) { mDataId = c.getLong(DATA_ID_COLUMN); mDataMimeType = c.getString(DATA_MIME_TYPE_COLUMN); mDataContent = c.getString(DATA_CONTENT_COLUMN); mDataContentData1 = c.getLong(DATA_CONTENT_DATA_1_COLUMN); mDataContentData3 = c.getString(DATA_CONTENT_DATA_3_COLUMN); } /** * 设置内容,从JSONObject中提取数据并更新到SqlData对象 * @param js 包含数据的JSONObject对象 * @throws JSONException 当JSON操作失败时抛出 */ public void setContent(JSONObject js) throws JSONException { long dataId = js.has(DataColumns.ID) ? js.getLong(DataColumns.ID) : INVALID_ID; if (mIsCreate || mDataId != dataId) { mDiffDataValues.put(DataColumns.ID, dataId); } mDataId = dataId; String dataMimeType = js.has(DataColumns.MIME_TYPE) ? js.getString(DataColumns.MIME_TYPE) : DataConstants.NOTE; if (mIsCreate || !mDataMimeType.equals(dataMimeType)) { mDiffDataValues.put(DataColumns.MIME_TYPE, dataMimeType); } mDataMimeType = dataMimeType; String dataContent = js.has(DataColumns.CONTENT) ? js.getString(DataColumns.CONTENT) : ""; if (mIsCreate || !mDataContent.equals(dataContent)) { mDiffDataValues.put(DataColumns.CONTENT, dataContent); } mDataContent = dataContent; long dataContentData1 = js.has(DataColumns.DATA1) ? js.getLong(DataColumns.DATA1) : 0; if (mIsCreate || mDataContentData1 != dataContentData1) { mDiffDataValues.put(DataColumns.DATA1, dataContentData1); } mDataContentData1 = dataContentData1; String dataContentData3 = js.has(DataColumns.DATA3) ? js.getString(DataColumns.DATA3) : ""; if (mIsCreate || !mDataContentData3.equals(dataContentData3)) { mDiffDataValues.put(DataColumns.DATA3, dataContentData3); } mDataContentData3 = dataContentData3; } /** * 获取内容,将SqlData对象的数据封装到JSONObject中 * @return 包含数据的JSONObject对象 * @throws JSONException 当JSON操作失败时抛出 */ public JSONObject getContent() throws JSONException { if (mIsCreate) { Log.e(TAG, "it seems that we haven't created this in database yet"); return null; } JSONObject js = new JSONObject(); js.put(DataColumns.ID, mDataId); js.put(DataColumns.MIME_TYPE, mDataMimeType); js.put(DataColumns.CONTENT, mDataContent); js.put(DataColumns.DATA1, mDataContentData1); js.put(DataColumns.DATA3, mDataContentData3); return js; } /** * 提交更改,将SqlData对象的数据更新到数据库中 * @param noteId 笔记的ID * @param validateVersion 是否需要验证版本号 * @param version 版本号 */ public void commit(long noteId, boolean validateVersion, long version) { if (mIsCreate) { if (mDataId == INVALID_ID && mDiffDataValues.containsKey(DataColumns.ID)) { mDiffDataValues.remove(DataColumns.ID); } mDiffDataValues.put(DataColumns.NOTE_ID, noteId); Uri uri = mContentResolver.insert(Notes.CONTENT_DATA_URI, mDiffDataValues); try { mDataId = Long.valueOf(uri.getPathSegments().get(1)); } catch (NumberFormatException e) { Log.e(TAG, "Get note id error :" + e.toString()); throw new ActionFailureException("create note failed"); } } else { if (mDiffDataValues.size() > 0) { int result = 0; if (!validateVersion) { result = mContentResolver.update(ContentUris.withAppendedId( Notes.CONTENT_DATA_URI, mDataId), mDiffDataValues, null, null); } else { result = mContentResolver.update(ContentUris.withAppendedId( Notes.CONTENT_DATA_URI, mDataId), mDiffDataValues, " ? in (SELECT " + NoteColumns.ID + " FROM " + TABLE.NOTE + " WHERE " + NoteColumns.VERSION + "=?)", new String[] { String.valueOf(noteId), String.valueOf(version) }); } if (result == 0) { Log.w(TAG, "there is no update. maybe user updates note when syncing"); } } } mDiffDataValues.clear(); mIsCreate = false; } /** * 获取数据库记录的ID * @return 数据库记录的ID */ public long getId() { return mDataId; } }