|
|
@ -33,7 +33,10 @@ import net.micode.notes.gtask.exception.ActionFailureException;
|
|
|
|
|
|
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* SqlData类代表可以从数据库存储和检索的数据实体。
|
|
|
|
|
|
|
|
* 它提供从Cursor加载数据的方法,以及将数据内容提交到数据库的方法。
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
public class SqlData {
|
|
|
|
public class SqlData {
|
|
|
|
private static final String TAG = SqlData.class.getSimpleName();
|
|
|
|
private static final String TAG = SqlData.class.getSimpleName();
|
|
|
@ -70,7 +73,10 @@ public class SqlData {
|
|
|
|
private String mDataContentData3;
|
|
|
|
private String mDataContentData3;
|
|
|
|
|
|
|
|
|
|
|
|
private ContentValues mDiffDataValues;
|
|
|
|
private ContentValues mDiffDataValues;
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 构造一个新的SqlData实例。
|
|
|
|
|
|
|
|
* @param context 上下文对象。
|
|
|
|
|
|
|
|
*/
|
|
|
|
public SqlData(Context context) {
|
|
|
|
public SqlData(Context context) {
|
|
|
|
mContentResolver = context.getContentResolver();
|
|
|
|
mContentResolver = context.getContentResolver();
|
|
|
|
mIsCreate = true;
|
|
|
|
mIsCreate = true;
|
|
|
@ -81,7 +87,11 @@ public class SqlData {
|
|
|
|
mDataContentData3 = "";
|
|
|
|
mDataContentData3 = "";
|
|
|
|
mDiffDataValues = new ContentValues();
|
|
|
|
mDiffDataValues = new ContentValues();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 从Cursor构造SqlData实例。
|
|
|
|
|
|
|
|
* @param context 上下文对象。
|
|
|
|
|
|
|
|
* @param c 数据游标。
|
|
|
|
|
|
|
|
*/
|
|
|
|
public SqlData(Context context, Cursor c) {
|
|
|
|
public SqlData(Context context, Cursor c) {
|
|
|
|
mContentResolver = context.getContentResolver();
|
|
|
|
mContentResolver = context.getContentResolver();
|
|
|
|
mIsCreate = false;
|
|
|
|
mIsCreate = false;
|
|
|
@ -89,6 +99,10 @@ public class SqlData {
|
|
|
|
mDiffDataValues = new ContentValues();
|
|
|
|
mDiffDataValues = new ContentValues();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 从游标加载数据。
|
|
|
|
|
|
|
|
* @param c 数据游标。
|
|
|
|
|
|
|
|
*/
|
|
|
|
private void loadFromCursor(Cursor c) {
|
|
|
|
private void loadFromCursor(Cursor c) {
|
|
|
|
mDataId = c.getLong(DATA_ID_COLUMN);
|
|
|
|
mDataId = c.getLong(DATA_ID_COLUMN);
|
|
|
|
mDataMimeType = c.getString(DATA_MIME_TYPE_COLUMN);
|
|
|
|
mDataMimeType = c.getString(DATA_MIME_TYPE_COLUMN);
|
|
|
@ -96,7 +110,11 @@ public class SqlData {
|
|
|
|
mDataContentData1 = c.getLong(DATA_CONTENT_DATA_1_COLUMN);
|
|
|
|
mDataContentData1 = c.getLong(DATA_CONTENT_DATA_1_COLUMN);
|
|
|
|
mDataContentData3 = c.getString(DATA_CONTENT_DATA_3_COLUMN);
|
|
|
|
mDataContentData3 = c.getString(DATA_CONTENT_DATA_3_COLUMN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 根据JSON对象设置数据内容。
|
|
|
|
|
|
|
|
* @param js JSON对象。
|
|
|
|
|
|
|
|
* @throws JSONException JSON异常。
|
|
|
|
|
|
|
|
*/
|
|
|
|
public void setContent(JSONObject js) throws JSONException {
|
|
|
|
public void setContent(JSONObject js) throws JSONException {
|
|
|
|
long dataId = js.has(DataColumns.ID) ? js.getLong(DataColumns.ID) : INVALID_ID;
|
|
|
|
long dataId = js.has(DataColumns.ID) ? js.getLong(DataColumns.ID) : INVALID_ID;
|
|
|
|
if (mIsCreate || mDataId != dataId) {
|
|
|
|
if (mIsCreate || mDataId != dataId) {
|
|
|
@ -129,7 +147,11 @@ public class SqlData {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mDataContentData3 = dataContentData3;
|
|
|
|
mDataContentData3 = dataContentData3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 获取数据内容的JSON对象。
|
|
|
|
|
|
|
|
* @return 数据内容的JSON对象。
|
|
|
|
|
|
|
|
* @throws JSONException JSON异常。
|
|
|
|
|
|
|
|
*/
|
|
|
|
public JSONObject getContent() throws JSONException {
|
|
|
|
public JSONObject getContent() throws JSONException {
|
|
|
|
if (mIsCreate) {
|
|
|
|
if (mIsCreate) {
|
|
|
|
Log.e(TAG, "it seems that we haven't created this in database yet");
|
|
|
|
Log.e(TAG, "it seems that we haven't created this in database yet");
|
|
|
@ -144,6 +166,12 @@ public class SqlData {
|
|
|
|
return js;
|
|
|
|
return js;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 提交数据内容到数据库。
|
|
|
|
|
|
|
|
* @param noteId 笔记ID。
|
|
|
|
|
|
|
|
* @param validateVersion 是否验证版本。
|
|
|
|
|
|
|
|
* @param version 版本号。
|
|
|
|
|
|
|
|
*/
|
|
|
|
public void commit(long noteId, boolean validateVersion, long version) {
|
|
|
|
public void commit(long noteId, boolean validateVersion, long version) {
|
|
|
|
|
|
|
|
|
|
|
|
if (mIsCreate) {
|
|
|
|
if (mIsCreate) {
|
|
|
@ -182,7 +210,10 @@ public class SqlData {
|
|
|
|
mDiffDataValues.clear();
|
|
|
|
mDiffDataValues.clear();
|
|
|
|
mIsCreate = false;
|
|
|
|
mIsCreate = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 获取数据ID。
|
|
|
|
|
|
|
|
* @return 数据ID。
|
|
|
|
|
|
|
|
*/
|
|
|
|
public long getId() {
|
|
|
|
public long getId() {
|
|
|
|
return mDataId;
|
|
|
|
return mDataId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|