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.
test1/SqlData.java

229 lines
10 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;
public class SqlData {
private static final String TAG = SqlData.class.getSimpleName();
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;
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的构造函数它接受一个Context对象作为参数
public SqlData(Context context) {
// 通过context获取ContentResolver对象用于访问Android系统的内容提供者Content Provider
mContentResolver = context.getContentResolver();
// 初始化一个标志变量mIsCreate表示这个SqlData对象对应的数据是否是新创建的
// true表示新创建false可能表示已存在这个变量的具体用途可能在类的其他部分定义
mIsCreate = true;
// 初始化mDataId为INVALID_ID表示这个SqlData对象当前没有关联的数据ID
// INVALID_ID可能是一个在类中定义的常量用于表示无效或未设置的数据ID
mDataId = INVALID_ID;
// 设置默认的数据MIME类型这里假设为DataConstants.NOTE可能表示这是一个笔记类型的数据
mDataMimeType = DataConstants.NOTE;
// 初始化mDataContent为一个空字符串可能用于存储数据的具体内容
mDataContent = "";
// 初始化mDataContentData1为0这个变量可能用于存储与mDataContent相关的某种整型数据
// 具体用途取决于类的业务逻辑
mDataContentData1 = 0;
// 初始化mDataContentData3为一个空字符串这个变量可能用于存储与mDataContent相关的某种字符串数据
// 同样,具体用途取决于类的业务逻辑
mDataContentData3 = "";
// 初始化mDiffDataValues为一个新的ContentValues对象
// ContentValues是一个用于存储一组数据键值对的类常用于与Content Provider进行交互时传递数据
// 在这个SqlData类中mDiffDataValues可能用于存储与数据变化相关的信息
mDiffDataValues = new ContentValues();
}
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);
}
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;
}
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;
}
// 定义一个名为commit的方法用于提交数据到内容提供者Content Provider
// 参数noteId表示笔记的唯一标识符validateVersion表示是否验证版本version表示当前数据的版本
public void commit(long noteId, boolean validateVersion, long version) {
// 如果mIsCreate为true表示这是一个新创建的数据对象
if (mIsCreate) {
// 如果mDataId为无效ID且mDiffDataValues中包含ID键则移除该键
// 这是因为新创建的数据不应该在提交时包含已存在的ID
if (mDataId == INVALID_ID && mDiffDataValues.containsKey(DataColumns.ID)) {
mDiffDataValues.remove(DataColumns.ID);
}
// 将noteId放入mDiffDataValues中作为笔记的唯一标识符
mDiffDataValues.put(DataColumns.NOTE_ID, noteId);
// 使用mContentResolver的insert方法将数据插入到内容提供者中
// Notes.CONTENT_DATA_URI是内容提供者的URI
Uri uri = mContentResolver.insert(Notes.CONTENT_DATA_URI, mDiffDataValues);
// 从返回的Uri中提取数据ID并赋值给mDataId
// Uri的getPathSegments().get(1)通常用于获取URI路径中的第二段这里假设它是数据ID
try {
mDataId = Long.valueOf(uri.getPathSegments().get(1));
} catch (NumberFormatException e) {
// 如果提取数据ID时发生数字格式异常则记录错误日志并抛出自定义异常
Log.e(TAG, "Get note id error :" + e.toString());
throw new ActionFailureException("create note failed");
}
} else {
// 如果mIsCreate为false表示这是一个已存在的数据对象需要更新数据
if (mDiffDataValues.size() > 0) {
// 根据validateVersion的值决定使用哪种更新方式
int result = 0;
if (!validateVersion) {
// 如果不验证版本,则直接更新数据
result = mContentResolver.update(
ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, mDataId),
mDiffDataValues,
null,
null
);
} else {
// 如果验证版本,则在更新时添加版本验证条件
// 这里假设NoteColumns.ID和NoteColumns.VERSION是数据库中的列名
// TABLE.NOTE是数据库表名
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)
}
);
}
// 如果更新结果为0表示没有数据被更新可能是用户在其他地方如同步时已经更新了数据
if (result == 0) {
Log.w(TAG, "there is no update. maybe user updates note when syncing");
}
}
}
// 清空mDiffDataValues因为它已经用于提交数据
mDiffDataValues.clear();
// 将mIsCreate设置为false表示数据已经提交不再是新创建的状态
mIsCreate = false;
}
public long getId() {
return mDataId;
}
}