|
|
|
@ -1,29 +1,14 @@
|
|
|
|
|
/*
|
|
|
|
|
* 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.gtask.data;
|
|
|
|
|
|
|
|
|
|
// 导入所需的Android包
|
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
|
import android.content.ContentUris;
|
|
|
|
|
import android.content.ContentValues;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.database.Cursor;
|
|
|
|
|
import android.net.Uri;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
// 导入项目中的自定义类和接口
|
|
|
|
|
import net.micode.notes.data.Notes;
|
|
|
|
|
import net.micode.notes.data.Notes.DataColumns;
|
|
|
|
|
import net.micode.notes.data.Notes.DataConstants;
|
|
|
|
@ -31,46 +16,42 @@ import net.micode.notes.data.Notes.NoteColumns;
|
|
|
|
|
import net.micode.notes.data.NotesDatabaseHelper.TABLE;
|
|
|
|
|
import net.micode.notes.gtask.exception.ActionFailureException;
|
|
|
|
|
|
|
|
|
|
// 导入JSON相关类
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class SqlData {
|
|
|
|
|
// 定义日志标签,用于调试
|
|
|
|
|
private static final String TAG = SqlData.class.getSimpleName();
|
|
|
|
|
|
|
|
|
|
// 定义一个无效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;
|
|
|
|
|
|
|
|
|
|
// 数据成员用于存储内容解析器、是否创建、数据ID、MIME类型、内容等信息
|
|
|
|
|
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对象
|
|
|
|
|
public SqlData(Context context) {
|
|
|
|
|
mContentResolver = context.getContentResolver();
|
|
|
|
|
mIsCreate = true;
|
|
|
|
@ -82,6 +63,7 @@ public class SqlData {
|
|
|
|
|
mDiffDataValues = new ContentValues();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 构造函数,用于从游标加载现有数据
|
|
|
|
|
public SqlData(Context context, Cursor c) {
|
|
|
|
|
mContentResolver = context.getContentResolver();
|
|
|
|
|
mIsCreate = false;
|
|
|
|
@ -89,6 +71,7 @@ public class SqlData {
|
|
|
|
|
mDiffDataValues = new ContentValues();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 从游标加载数据到SqlData对象
|
|
|
|
|
private void loadFromCursor(Cursor c) {
|
|
|
|
|
mDataId = c.getLong(DATA_ID_COLUMN);
|
|
|
|
|
mDataMimeType = c.getString(DATA_MIME_TYPE_COLUMN);
|
|
|
|
@ -97,6 +80,7 @@ public class SqlData {
|
|
|
|
|
mDataContentData3 = c.getString(DATA_CONTENT_DATA_3_COLUMN);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置内容,更新内部状态和ContentValues
|
|
|
|
|
public void setContent(JSONObject js) throws JSONException {
|
|
|
|
|
long dataId = js.has(DataColumns.ID) ? js.getLong(DataColumns.ID) : INVALID_ID;
|
|
|
|
|
if (mIsCreate || mDataId != dataId) {
|
|
|
|
@ -130,6 +114,7 @@ public class SqlData {
|
|
|
|
|
mDataContentData3 = dataContentData3;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取当前数据的内容,封装为JSONObject
|
|
|
|
|
public JSONObject getContent() throws JSONException {
|
|
|
|
|
if (mIsCreate) {
|
|
|
|
|
Log.e(TAG, "it seems that we haven't created this in database yet");
|
|
|
|
@ -144,6 +129,7 @@ public class SqlData {
|
|
|
|
|
return js;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 提交更改到数据库
|
|
|
|
|
public void commit(long noteId, boolean validateVersion, long version) {
|
|
|
|
|
|
|
|
|
|
if (mIsCreate) {
|
|
|
|
@ -167,7 +153,7 @@ public class SqlData {
|
|
|
|
|
Notes.CONTENT_DATA_URI, mDataId), mDiffDataValues, null, null);
|
|
|
|
|
} else {
|
|
|
|
|
result = mContentResolver.update(ContentUris.withAppendedId(
|
|
|
|
|
Notes.CONTENT_DATA_URI, mDataId), mDiffDataValues,
|
|
|
|
|
Notes.CONTENT_DATA_URI, mDataId), mDiffDataValues,
|
|
|
|
|
" ? in (SELECT " + NoteColumns.ID + " FROM " + TABLE.NOTE
|
|
|
|
|
+ " WHERE " + NoteColumns.VERSION + "=?)", new String[] {
|
|
|
|
|
String.valueOf(noteId), String.valueOf(version)
|
|
|
|
@ -183,7 +169,8 @@ public class SqlData {
|
|
|
|
|
mIsCreate = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取当前数据的ID
|
|
|
|
|
public long getId() {
|
|
|
|
|
return mDataId;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|