You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

202 lines
8.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* 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;
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;
import net.micode.notes.data.Notes.NoteColumns;
import net.micode.notes.data.NotesDatabaseHelper.TABLE;
import net.micode.notes.gtask.exception.ActionFailureException;
import org.json.JSONException;
import org.json.JSONObject;
// SqlData类用于处理与SQL数据库相关的数据操作可能用于存储和获取任务、笔记等数据
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
};
// 定义数据ID列在投影列数组中的索引
public static final int DATA_ID_COLUMN = 0;
// 定义MIME类型列在投影列数组中的索引
public static final int DATA_MIME_TYPE_COLUMN = 1;
// 定义内容列在投影列数组中的索引
public static final int DATA_CONTENT_COLUMN = 2;
// 定义数据内容中DATA1列在投影列数组中的索引
public static final int DATA_CONTENT_DATA_1_COLUMN = 3;
// 定义数据内容中DATA3列在投影列数组中的索引
public static final int DATA_CONTENT_DATA_3_COLUMN = 4;
// 用于与内容提供器进行交互,以访问和操作数据
private ContentResolver mContentResolver;
// 表示是否为创建操作
private boolean mIsCreate;
// 数据的ID
private long mDataId;
// 数据的MIME类型
private String mDataMimeType;
// 数据的内容
private String mDataContent;
// 数据内容中的特定数据1根据上下文可能有不同含义
private long mDataContentData1;
// 数据内容中的特定数据3根据上下文可能有不同含义
private String mDataContentData3;
// 用于存储要更新的数据值
private ContentValues mDiffDataValues;
// 构造函数用于创建一个新的SqlData实例通常用于插入新数据
public SqlData(Context context) {
// 获取上下文的内容解析器
mContentResolver = context.getContentResolver();
// 标记为创建操作
mIsCreate = true;
// 设置初始数据ID为无效值
mDataId = INVALID_ID;
// 设置默认的MIME类型为NOTE
mDataMimeType = DataConstants.NOTE;
// 设置初始内容为空字符串
mDataContent = "";
// 设置初始数据内容中的特定数据1为0
mDataContentData1 = 0;
// 设置初始数据内容中的特定数据3为空字符串
mDataContentData3 = "";
// 创建一个新的ContentValues实例用于存储数据差异
mDiffDataValues = new ContentValues();
}
// 构造函数用于根据数据库游标创建一个SqlData实例通常用于读取现有数据
public SqlData(Context context, Cursor c) {
mContentResolver = context.getContentResolver();
mIsCreate = false;
// 从游标加载数据到当前实例的属性中
loadFromCursor(c);
mDiffDataValues = new ContentValues();
}
// 从数据库游标加载数据到当前实例的属性中
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);
}
// 设置数据内容根据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;
}
// 获取当前数据的内容以JSON对象形式返回如果是创建操作则返回null并记录错误日志
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;
}
// 将数据提交到数据库,根据是否为创建操作执行插入或更新操作
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
public long getId() {
return mDataId;
}
}