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.
git-xiaomibianqian/lhy/SqlData.java

190 lines
9.5 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();//调用getSimpleName ()函数来得到类的简写名称存入字符串TAG中
private static final int INVALID_ID = -99999;//将得到类的简写名称存入字符串TAG中为mDataId置初始值-99999
public static final String[] PROJECTION_DATA = new String[] {//新建一个字符串数组集合了interface DataColumns中所有SF常量
DataColumns.ID, DataColumns.MIME_TYPE, DataColumns.CONTENT, DataColumns.DATA1,//获得数据列idmime类型内容1类型数据3类型数据
DataColumns.DATA3
};
public static final int DATA_ID_COLUMN = 0;//以下五个变量作为sql表中5列的编号
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;//定义的一些私有全局变量可以与sqlNote中的变量相对应分析
private boolean mIsCreate;
private long mDataId;
private String mDataMimeType;
private String mDataContent;
private long mDataContentData1;
private String mDataContentData3;
private ContentValues mDiffDataValues;
public SqlData(Context context) {//第一种SQLData的构造方式只从上下文获取初始化其中的变量
mContentResolver = context.getContentResolver();
mIsCreate = true;
mDataId = INVALID_ID;
mDataMimeType = DataConstants.NOTE;
mDataContent = "";
mDataContentData1 = 0;
mDataContentData3 = "";//数据类型
mDiffDataValues = new ContentValues();//创建内容
}
public SqlData(Context context, Cursor c) {//构造函数,初始化数据,参数类型分别为 Context 和 Cursor
mContentResolver = context.getContentResolver();
mIsCreate = false;
loadFromCursor(c);
mDiffDataValues = new ContentValues();
}
private void loadFromCursor(Cursor c) {//构造函数,初始化数据,参数类型分别为 Context 和 Cursor
mDataId = c.getLong(DATA_ID_COLUMN);//调用cursor类的方法获取数据id参数为id长度
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 {//设置共享数据并且抛出JSON类型的异常与处理机制
long dataId = js.has(DataColumns.ID) ? js.getLong(DataColumns.ID) : INVALID_ID;//设置数据 id如果传入的 JSONObject 对象中存在DataColumns.ID则获取并设置否则设为INVALID_ID
if (mIsCreate || mDataId != dataId) {//如果是根据目录创建的或者当前数据的ID与元数据的ID不符那么发送更新此ID 的请求
mDiffDataValues.put(DataColumns.ID, dataId);
}
mDataId = dataId;//与共享数据库同步后共享数据ID就等于数据ID
String dataMimeType = js.has(DataColumns.MIME_TYPE) ? js.getString(DataColumns.MIME_TYPE)//若json中有MIME_TYPE这一项则将其获取否则将其定义为notes类中定义的文本类型
: 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)) {//对比DataContent并更新contentValue中的DataContent
mDiffDataValues.put(DataColumns.CONTENT, dataContent);
}
mDataContent = dataContent;//共享数据同步后,共享数据内容等于该数据内容
long dataContentData1 = js.has(DataColumns.DATA1) ? js.getLong(DataColumns.DATA1) : 0;// 如果传入的JSONObject对象有DataColumn.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();//将相关数据放入新创建的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) {//commit 函数用于把当前所做的修改保存到数据库
if (mIsCreate) {//判断是否是第一种SqlData构造方式
if (mDataId == INVALID_ID && mDiffDataValues.containsKey(DataColumns.ID)) {//如果该id是无效id且在共享数据中不存在该数据id对应的键则从共享数据中移除
mDiffDataValues.remove(DataColumns.ID);//删除数据
}
mDiffDataValues.put(DataColumns.NOTE_ID, noteId);//加入的ID有效也就是操作有效则数据库加入这个note的ID这条data对应在这个note的ID下
Uri uri = mContentResolver.insert(Notes.CONTENT_DATA_URI, mDiffDataValues);//在note的资源标识下加入data数据
try {//上一句实现的是URI到Uri的转换/将路径转换为Long型附识给当前id
mDataId = Long.valueOf(uri.getPathSegments().get(1));//获取有效便签id并创建
} catch (NumberFormatException e) {//如果转换出错则日志中显示错误“获取note的ID出错”
Log.e(TAG, "Get note id error :" + e.toString());//获取note id错误e.toString()获取异常类型和异常详细消息
throw new ActionFailureException("create note failed");
}
} else {
if (mDiffDataValues.size() > 0) {//若共享数据存在则通过内容解析器更新关于新URI的共享数据
int result = 0;
if (!validateVersion) {
result = mContentResolver.update(ContentUris.withAppendedId(//如果版本已确认则结果还记录下所在note的Id以及版本号
Notes.CONTENT_DATA_URI, mDataId), mDiffDataValues, null, null);
} else {//如果版本确认了则从数据库中选取对应版本的id进行更新
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;
}
public long getId() {
return mDataId;
}
}