|
|
|
|
@ -0,0 +1,437 @@
|
|
|
|
|
/*
|
|
|
|
|
* 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.data;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.app.SearchManager;
|
|
|
|
|
import android.content.ContentProvider;
|
|
|
|
|
import android.content.ContentUris;
|
|
|
|
|
import android.content.ContentValues;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.content.UriMatcher;
|
|
|
|
|
import android.database.Cursor;
|
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
|
import android.net.Uri;
|
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
import net.micode.notes.R;
|
|
|
|
|
import net.micode.notes.data.Notes.DataColumns;
|
|
|
|
|
import net.micode.notes.data.Notes.NoteColumns;
|
|
|
|
|
import net.micode.notes.data.NotesDatabaseHelper.TABLE;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class NotesProvider extends ContentProvider {
|
|
|
|
|
// 定义一个UriMatcher对象,用于匹配Uri
|
|
|
|
|
private static final UriMatcher mMatcher;
|
|
|
|
|
|
|
|
|
|
// 定义一个NotesDatabaseHelper对象,用于操作数据库
|
|
|
|
|
private NotesDatabaseHelper mHelper;
|
|
|
|
|
|
|
|
|
|
// 定义一个常量,用于标识NotesProvider
|
|
|
|
|
private static final String TAG = "NotesProvider";
|
|
|
|
|
|
|
|
|
|
// 定义一个常量,用于标识URI_NOTE
|
|
|
|
|
private static final int URI_NOTE = 1;
|
|
|
|
|
// 定义一个常量,用于标识URI_NOTE_ITEM
|
|
|
|
|
private static final int URI_NOTE_ITEM = 2;
|
|
|
|
|
// 定义一个常量,用于标识URI_DATA
|
|
|
|
|
private static final int URI_DATA = 3;
|
|
|
|
|
// 定义一个常量,用于标识URI_DATA_ITEM
|
|
|
|
|
private static final int URI_DATA_ITEM = 4;
|
|
|
|
|
|
|
|
|
|
// 定义一个常量,用于标识URI_SEARCH
|
|
|
|
|
private static final int URI_SEARCH = 5;
|
|
|
|
|
// 定义一个常量,用于标识URI_SEARCH_SUGGEST
|
|
|
|
|
private static final int URI_SEARCH_SUGGEST = 6;
|
|
|
|
|
|
|
|
|
|
// 静态代码块,用于初始化UriMatcher对象
|
|
|
|
|
static {
|
|
|
|
|
// 创建UriMatcher对象,NO_MATCH表示没有匹配的URI
|
|
|
|
|
mMatcher = new UriMatcher(UriMatcher.NO_MATCH);
|
|
|
|
|
// 添加URI匹配规则,匹配Notes.AUTHORITY下的note,匹配成功返回URI_NOTE
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "note", URI_NOTE);
|
|
|
|
|
// 添加URI匹配规则,匹配Notes.AUTHORITY下的note/#,匹配成功返回URI_NOTE_ITEM
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "note/#", URI_NOTE_ITEM);
|
|
|
|
|
// 添加URI匹配规则,匹配Notes.AUTHORITY下的data,匹配成功返回URI_DATA
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "data", URI_DATA);
|
|
|
|
|
// 添加URI匹配规则,匹配Notes.AUTHORITY下的data/#,匹配成功返回URI_DATA_ITEM
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "data/#", URI_DATA_ITEM);
|
|
|
|
|
// 添加URI匹配规则,匹配Notes.AUTHORITY下的search,匹配成功返回URI_SEARCH
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "search", URI_SEARCH);
|
|
|
|
|
// 添加URI匹配规则,匹配Notes.AUTHORITY下的search/*,匹配成功返回URI_SEARCH_SUGGEST
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, URI_SEARCH_SUGGEST);
|
|
|
|
|
// 添加URI匹配规则,匹配Notes.AUTHORITY下的search/*/*,匹配成功返回URI_SEARCH_SUGGEST
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", URI_SEARCH_SUGGEST);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* x'0A' represents the '\n' character in sqlite. For title and content in the search result,
|
|
|
|
|
* we will trim '\n' and white space in order to show more information.
|
|
|
|
|
*/
|
|
|
|
|
// 定义一个常量,用于搜索笔记的投影
|
|
|
|
|
private static final String NOTES_SEARCH_PROJECTION = NoteColumns.ID + ","
|
|
|
|
|
// 将笔记的ID作为搜索建议的额外数据
|
|
|
|
|
+ NoteColumns.ID + " AS " + SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA + ","
|
|
|
|
|
// 将笔记的摘要作为搜索建议的文本1
|
|
|
|
|
+ "TRIM(REPLACE(" + NoteColumns.SNIPPET + ", x'0A','')) AS " + SearchManager.SUGGEST_COLUMN_TEXT_1 + ","
|
|
|
|
|
// 将笔记的摘要作为搜索建议的文本2
|
|
|
|
|
+ "TRIM(REPLACE(" + NoteColumns.SNIPPET + ", x'0A','')) AS " + SearchManager.SUGGEST_COLUMN_TEXT_2 + ","
|
|
|
|
|
// 将搜索结果的图标设置为drawable/search_result
|
|
|
|
|
+ R.drawable.search_result + " AS " + SearchManager.SUGGEST_COLUMN_ICON_1 + ","
|
|
|
|
|
// 将搜索建议的意图动作设置为Intent.ACTION_VIEW
|
|
|
|
|
+ "'" + Intent.ACTION_VIEW + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_ACTION + ","
|
|
|
|
|
// 将搜索建议的意图数据设置为Notes.TextNote.CONTENT_TYPE
|
|
|
|
|
+ "'" + Notes.TextNote.CONTENT_TYPE + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_DATA;
|
|
|
|
|
|
|
|
|
|
// 定义一个静态字符串变量,用于查询笔记片段
|
|
|
|
|
private static String NOTES_SNIPPET_SEARCH_QUERY = "SELECT " + NOTES_SEARCH_PROJECTION
|
|
|
|
|
// 查询笔记片段
|
|
|
|
|
+ " FROM " + TABLE.NOTE
|
|
|
|
|
// 从笔记表中查询
|
|
|
|
|
+ " WHERE " + NoteColumns.SNIPPET + " LIKE ?"
|
|
|
|
|
// 笔记片段匹配查询条件
|
|
|
|
|
+ " AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER
|
|
|
|
|
// 笔记的父ID不等于垃圾桶文件夹
|
|
|
|
|
+ " AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
// 重写onCreate方法
|
|
|
|
|
public boolean onCreate() {
|
|
|
|
|
// 获取NotesDatabaseHelper的实例
|
|
|
|
|
mHelper = NotesDatabaseHelper.getInstance(getContext());
|
|
|
|
|
// 返回true
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
// 重写query方法,用于查询数据
|
|
|
|
|
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
|
|
|
|
|
String sortOrder) {
|
|
|
|
|
// 声明一个Cursor对象
|
|
|
|
|
Cursor c = null;
|
|
|
|
|
// 获取可读的数据库
|
|
|
|
|
SQLiteDatabase db = mHelper.getReadableDatabase();
|
|
|
|
|
// 声明一个id变量
|
|
|
|
|
String id = null;
|
|
|
|
|
// 根据传入的uri,执行不同的数据库查询操作
|
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
|
case URI_NOTE:
|
|
|
|
|
// 查询note表
|
|
|
|
|
c = db.query(TABLE.NOTE, projection, selection, selectionArgs, null, null,
|
|
|
|
|
sortOrder);
|
|
|
|
|
break;
|
|
|
|
|
case URI_NOTE_ITEM:
|
|
|
|
|
// 查询note表中指定id的记录
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
c = db.query(TABLE.NOTE, projection, NoteColumns.ID + "=" + id
|
|
|
|
|
+ parseSelection(selection), selectionArgs, null, null, sortOrder);
|
|
|
|
|
break;
|
|
|
|
|
case URI_DATA:
|
|
|
|
|
// 查询data表
|
|
|
|
|
c = db.query(TABLE.DATA, projection, selection, selectionArgs, null, null,
|
|
|
|
|
sortOrder);
|
|
|
|
|
break;
|
|
|
|
|
case URI_DATA_ITEM:
|
|
|
|
|
// 查询data表中指定id的记录
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
// 查询数据库中指定表的数据,返回一个Cursor对象
|
|
|
|
|
c = db.query(TABLE.DATA, projection, DataColumns.ID + "=" + id
|
|
|
|
|
+ parseSelection(selection), selectionArgs, null, null, sortOrder);
|
|
|
|
|
break;
|
|
|
|
|
// 搜索URI
|
|
|
|
|
case URI_SEARCH:
|
|
|
|
|
// 搜索建议URI
|
|
|
|
|
case URI_SEARCH_SUGGEST:
|
|
|
|
|
// 查询搜索结果
|
|
|
|
|
// 如果sortOrder不为空或者projection不为空,则抛出异常
|
|
|
|
|
if (sortOrder != null || projection != null) {
|
|
|
|
|
// 抛出IllegalArgumentException异常,异常信息为:"do not specify sortOrder, selection, selectionArgs, or projection" + "with this query"
|
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
|
"do not specify sortOrder, selection, selectionArgs, or projection" + "with this query");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取搜索字符串
|
|
|
|
|
String searchString = null;
|
|
|
|
|
// 如果匹配到URI_SEARCH_SUGGEST
|
|
|
|
|
if (mMatcher.match(uri) == URI_SEARCH_SUGGEST) {
|
|
|
|
|
// 如果URI的路径段大于1
|
|
|
|
|
if (uri.getPathSegments().size() > 1) {
|
|
|
|
|
// 获取路径段的第二个元素作为搜索字符串
|
|
|
|
|
searchString = uri.getPathSegments().get(1);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 获取uri中的查询参数pattern
|
|
|
|
|
searchString = uri.getQueryParameter("pattern");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果查询参数为空,则返回null
|
|
|
|
|
if (TextUtils.isEmpty(searchString)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 将搜索字符串格式化为包含通配符的字符串
|
|
|
|
|
searchString = String.format("%%%s%%", searchString);
|
|
|
|
|
// 执行查询语句,返回结果集
|
|
|
|
|
c = db.rawQuery(NOTES_SNIPPET_SEARCH_QUERY,
|
|
|
|
|
new String[] { searchString });
|
|
|
|
|
} catch (IllegalStateException ex) {
|
|
|
|
|
// 捕获异常并打印错误日志
|
|
|
|
|
Log.e(TAG, "got exception: " + ex.toString());
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
// 抛出异常,表示未知的URI
|
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
|
}
|
|
|
|
|
// 如果结果集不为空,则设置通知URI
|
|
|
|
|
if (c != null) {
|
|
|
|
|
c.setNotificationUri(getContext().getContentResolver(), uri);
|
|
|
|
|
}
|
|
|
|
|
// 返回结果集
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Uri insert(Uri uri, ContentValues values) {
|
|
|
|
|
// 获取可写的数据库
|
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
|
|
|
// 定义变量,用于存储插入的数据ID
|
|
|
|
|
long dataId = 0, noteId = 0, insertedId = 0;
|
|
|
|
|
// 根据传入的URI进行匹配
|
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
|
// 如果匹配到URI_NOTE
|
|
|
|
|
case URI_NOTE:
|
|
|
|
|
// 在NOTE表中插入数据,并获取插入的数据ID
|
|
|
|
|
insertedId = noteId = db.insert(TABLE.NOTE, null, values);
|
|
|
|
|
break;
|
|
|
|
|
// 如果匹配到URI_DATA
|
|
|
|
|
case URI_DATA:
|
|
|
|
|
// 如果values中包含NOTE_ID
|
|
|
|
|
if (values.containsKey(DataColumns.NOTE_ID)) {
|
|
|
|
|
// 获取NOTE_ID
|
|
|
|
|
noteId = values.getAsLong(DataColumns.NOTE_ID);
|
|
|
|
|
} else {
|
|
|
|
|
// 否则,打印错误日志
|
|
|
|
|
Log.d(TAG, "Wrong data format without note id:" + values.toString());
|
|
|
|
|
}
|
|
|
|
|
// 在DATA表中插入数据,并获取插入的数据ID
|
|
|
|
|
insertedId = dataId = db.insert(TABLE.DATA, null, values);
|
|
|
|
|
break;
|
|
|
|
|
// 如果没有匹配到任何URI
|
|
|
|
|
default:
|
|
|
|
|
// 抛出异常
|
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
|
}
|
|
|
|
|
// Notify the note uri
|
|
|
|
|
if (noteId > 0) {
|
|
|
|
|
getContext().getContentResolver().notifyChange(
|
|
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Notify the data uri
|
|
|
|
|
if (dataId > 0) {
|
|
|
|
|
getContext().getContentResolver().notifyChange(
|
|
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ContentUris.withAppendedId(uri, insertedId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int delete(Uri uri, String selection, String[] selectionArgs) {
|
|
|
|
|
// 初始化删除的记录数
|
|
|
|
|
int count = 0;
|
|
|
|
|
// 初始化ID
|
|
|
|
|
String id = null;
|
|
|
|
|
// 获取可写的数据库
|
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
|
|
|
// 初始化删除数据标志
|
|
|
|
|
boolean deleteData = false;
|
|
|
|
|
// 根据URI匹配不同的删除操作
|
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
|
// 删除笔记
|
|
|
|
|
case URI_NOTE:
|
|
|
|
|
// 添加删除条件
|
|
|
|
|
selection = "(" + selection + ") AND " + NoteColumns.ID + ">0 ";
|
|
|
|
|
// 执行删除操作
|
|
|
|
|
count = db.delete(TABLE.NOTE, selection, selectionArgs);
|
|
|
|
|
break;
|
|
|
|
|
// 删除笔记项
|
|
|
|
|
case URI_NOTE_ITEM:
|
|
|
|
|
// 获取笔记项的ID
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
/**
|
|
|
|
|
* ID that smaller than 0 is system folder which is not allowed to
|
|
|
|
|
* trash
|
|
|
|
|
*/
|
|
|
|
|
// 将id转换为long类型
|
|
|
|
|
long noteId = Long.valueOf(id);
|
|
|
|
|
// 如果noteId小于等于0,则跳出循环
|
|
|
|
|
if (noteId <= 0) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
// 删除指定id的note
|
|
|
|
|
// 删除指定ID的笔记
|
|
|
|
|
count = db.delete(TABLE.NOTE,
|
|
|
|
|
NoteColumns.ID + "=" + id + parseSelection(selection), selectionArgs);
|
|
|
|
|
break;
|
|
|
|
|
// 处理URI_DATA
|
|
|
|
|
case URI_DATA:
|
|
|
|
|
// 删除数据
|
|
|
|
|
count = db.delete(TABLE.DATA, selection, selectionArgs);
|
|
|
|
|
// 标记删除数据
|
|
|
|
|
deleteData = true;
|
|
|
|
|
break;
|
|
|
|
|
// 删除数据项
|
|
|
|
|
case URI_DATA_ITEM:
|
|
|
|
|
// 获取数据项的id
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
// 删除数据项
|
|
|
|
|
count = db.delete(TABLE.DATA,
|
|
|
|
|
DataColumns.ID + "=" + id + parseSelection(selection), selectionArgs);
|
|
|
|
|
// 标记删除数据
|
|
|
|
|
deleteData = true;
|
|
|
|
|
break;
|
|
|
|
|
// 未知URI
|
|
|
|
|
default:
|
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
|
}
|
|
|
|
|
// 如果删除了数据
|
|
|
|
|
if (count > 0) {
|
|
|
|
|
// 如果删除了数据,通知内容解析器
|
|
|
|
|
if (deleteData) {
|
|
|
|
|
getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null);
|
|
|
|
|
}
|
|
|
|
|
// 通知内容解析器
|
|
|
|
|
getContext().getContentResolver().notifyChange(uri, null);
|
|
|
|
|
}
|
|
|
|
|
// 返回删除的条数
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 重写update方法,用于更新数据库中的数据
|
|
|
|
|
@Override
|
|
|
|
|
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
|
|
|
|
|
// 定义一个计数器,用于记录更新的数据条数
|
|
|
|
|
int count = 0;
|
|
|
|
|
// 定义一个字符串,用于存储要更新的数据的id
|
|
|
|
|
String id = null;
|
|
|
|
|
// 获取可写的数据库
|
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
|
|
|
// 定义一个布尔值,用于判断是否更新了数据
|
|
|
|
|
boolean updateData = false;
|
|
|
|
|
// 根据传入的uri进行匹配,根据匹配结果执行不同的操作
|
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
|
case URI_NOTE:
|
|
|
|
|
// 增加note的版本号
|
|
|
|
|
increaseNoteVersion(-1, selection, selectionArgs);
|
|
|
|
|
// 更新note表
|
|
|
|
|
count = db.update(TABLE.NOTE, values, selection, selectionArgs);
|
|
|
|
|
break;
|
|
|
|
|
case URI_NOTE_ITEM:
|
|
|
|
|
// 获取uri中的id
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
// 增加note的版本号
|
|
|
|
|
increaseNoteVersion(Long.valueOf(id), selection, selectionArgs);
|
|
|
|
|
// 更新note表
|
|
|
|
|
count = db.update(TABLE.NOTE, values, NoteColumns.ID + "=" + id
|
|
|
|
|
+ parseSelection(selection), selectionArgs);
|
|
|
|
|
break;
|
|
|
|
|
case URI_DATA:
|
|
|
|
|
// 更新data表
|
|
|
|
|
count = db.update(TABLE.DATA, values, selection, selectionArgs);
|
|
|
|
|
// 标记更新data
|
|
|
|
|
updateData = true;
|
|
|
|
|
break;
|
|
|
|
|
case URI_DATA_ITEM:
|
|
|
|
|
// 获取uri中的id
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
// 更新data表
|
|
|
|
|
count = db.update(TABLE.DATA, values, DataColumns.ID + "=" + id
|
|
|
|
|
+ parseSelection(selection), selectionArgs);
|
|
|
|
|
// 标记更新data
|
|
|
|
|
updateData = true;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
// 抛出异常,未知uri
|
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果count大于0
|
|
|
|
|
if (count > 0) {
|
|
|
|
|
// 如果updateData为true
|
|
|
|
|
if (updateData) {
|
|
|
|
|
// 通知ContentResolver更新Notes.CONTENT_NOTE_URI
|
|
|
|
|
getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null);
|
|
|
|
|
}
|
|
|
|
|
// 通知ContentResolver更新uri
|
|
|
|
|
getContext().getContentResolver().notifyChange(uri, null);
|
|
|
|
|
}
|
|
|
|
|
// 返回count
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 解析selection字符串
|
|
|
|
|
private String parseSelection(String selection) {
|
|
|
|
|
// 如果selection不为空
|
|
|
|
|
return (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void increaseNoteVersion(long id, String selection, String[] selectionArgs) {
|
|
|
|
|
// 创建一个StringBuilder对象,用于拼接SQL语句
|
|
|
|
|
StringBuilder sql = new StringBuilder(120);
|
|
|
|
|
// 拼接UPDATE语句
|
|
|
|
|
sql.append("UPDATE ");
|
|
|
|
|
// 拼接表名
|
|
|
|
|
sql.append(TABLE.NOTE);
|
|
|
|
|
// 拼接SET语句
|
|
|
|
|
sql.append(" SET ");
|
|
|
|
|
// 拼接版本号字段
|
|
|
|
|
sql.append(NoteColumns.VERSION);
|
|
|
|
|
// 拼接版本号增加1的语句
|
|
|
|
|
sql.append("=" + NoteColumns.VERSION + "+1 ");
|
|
|
|
|
|
|
|
|
|
// 如果id大于0或者selection不为空,则拼接WHERE语句
|
|
|
|
|
if (id > 0 || !TextUtils.isEmpty(selection)) {
|
|
|
|
|
sql.append(" WHERE ");
|
|
|
|
|
}
|
|
|
|
|
// 如果id大于0,则拼接id等于指定值的语句
|
|
|
|
|
if (id > 0) {
|
|
|
|
|
sql.append(NoteColumns.ID + "=" + String.valueOf(id));
|
|
|
|
|
}
|
|
|
|
|
// 判断selection是否为空
|
|
|
|
|
if (!TextUtils.isEmpty(selection)) {
|
|
|
|
|
// 如果id大于0,则调用parseSelection方法解析selection,否则直接赋值给selectString
|
|
|
|
|
String selectString = id > 0 ? parseSelection(selection) : selection;
|
|
|
|
|
// 遍历selectionArgs数组,将每个元素替换selectString中的第一个问号
|
|
|
|
|
for (String args : selectionArgs) {
|
|
|
|
|
selectString = selectString.replaceFirst("\\?", args);
|
|
|
|
|
}
|
|
|
|
|
// 将selectString追加到sql中
|
|
|
|
|
sql.append(selectString);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取可写的数据库
|
|
|
|
|
mHelper.getWritableDatabase().execSQL(sql.toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String getType(Uri uri) {
|
|
|
|
|
// 获取Uri的类型
|
|
|
|
|
// TODO Auto-generated method stub
|