update Optimize message queries

This commit is contained in:
SL 2023-12-07 15:31:16 +08:00
parent 65c6eeca1b
commit 9d07754e5f
7 changed files with 43 additions and 65 deletions

View File

@ -209,18 +209,18 @@ public class ChannelDBManager {
* @return List<WKChannel>
*/
public synchronized List<WKChannel> queryWithFollowAndStatus(byte channelType, int follow, int status) {
Object[] args = new Object[3];
args[0] = channelType;
args[1] = follow;
args[2] = status;
String sql = "select * from " + channel + " where " + WKDBColumns.WKChannelColumns.channel_type + "=? and " + WKDBColumns.WKChannelColumns.follow + "=? and " + WKDBColumns.WKChannelColumns.status + "=? and is_deleted=0";
String[] args = new String[3];
args[0] = String.valueOf(channelType);
args[1] = String.valueOf(follow);
args[2] = String.valueOf(status);
String selection = WKDBColumns.WKChannelColumns.channel_type + "=? and " + WKDBColumns.WKChannelColumns.follow + "=? and " + WKDBColumns.WKChannelColumns.status + "=? and is_deleted=0";
List<WKChannel> channels = new ArrayList<>();
if (WKIMApplication
.getInstance()
.getDbHelper() != null) {
try (Cursor cursor = WKIMApplication
.getInstance()
.getDbHelper().rawQuery(sql, args)) {
.getDbHelper().select(channel, selection, args, null)) {
if (cursor == null) {
return channels;
}
@ -241,17 +241,17 @@ public class ChannelDBManager {
* @return List<WKChannel>
*/
public synchronized List<WKChannel> queryWithStatus(byte channelType, int status) {
Object[] args = new Object[2];
args[0] = channelType;
args[1] = status;
String sql = "select * from " + channel + " where " + WKDBColumns.WKChannelColumns.channel_type + "=? and " + WKDBColumns.WKChannelColumns.status + "=?";
String[] args = new String[2];
args[0] = String.valueOf(channelType);
args[1] = String.valueOf(status);
String selection = WKDBColumns.WKChannelColumns.channel_type + "=? and " + WKDBColumns.WKChannelColumns.status + "=?";
List<WKChannel> channels = new ArrayList<>();
if (WKIMApplication.getInstance().getDbHelper() == null) {
return channels;
}
try (Cursor cursor = WKIMApplication
.getInstance()
.getDbHelper().rawQuery(sql, args)) {
.getDbHelper().select(channel, selection, args, null)) {
if (cursor == null) {
return channels;
}
@ -343,14 +343,14 @@ public class ChannelDBManager {
}
public synchronized List<WKChannel> queryWithChannelTypeAndFollow(byte channelType, int follow) {
Object[] args = new Object[2];
args[0] = channelType;
args[1] = follow;
String sql = "select * from " + channel + " where " + WKDBColumns.WKChannelColumns.channel_type + "=? and " + WKDBColumns.WKChannelColumns.follow + "=?";
String[] args = new String[2];
args[0] = String.valueOf(channelType);
args[1] = String.valueOf(follow);
String selection = WKDBColumns.WKChannelColumns.channel_type + "=? and " + WKDBColumns.WKChannelColumns.follow + "=?";
List<WKChannel> channels = new ArrayList<>();
try (Cursor cursor = WKIMApplication
.getInstance()
.getDbHelper().rawQuery(sql, args)) {
.getDbHelper().select(channel, selection, args, null)) {
if (cursor == null) {
return channels;
}
@ -367,7 +367,7 @@ public class ChannelDBManager {
String where = WKDBColumns.WKChannelColumns.channel_id + "=? and " + WKDBColumns.WKChannelColumns.channel_type + "=?";
String[] whereValue = new String[2];
whereValue[0] = channelID;
whereValue[1] = channelType + "";
whereValue[1] = String.valueOf(channelType);
WKIMApplication.getInstance().getDbHelper()
.update(channel, updateKey, updateValue, where, whereValue);
}

View File

@ -24,7 +24,7 @@ import java.util.List;
* 频道成员数据管理
*/
public class ChannelMembersDbManager {
final String channelCols = "" + channel + ".channel_remark," + channel + ".channel_name," + channel + ".avatar," + channel + ".avatar_cache_key";
final String channelCols = channel + ".channel_remark," + channel + ".channel_name," + channel + ".avatar," + channel + ".avatar_cache_key";
private ChannelMembersDbManager() {
}
@ -46,7 +46,7 @@ public class ChannelMembersDbManager {
args[3] = "%" + keyword + "%";
args[4] = "%" + keyword + "%";
args[5] = "%" + keyword + "%";
String sql = "select " + channelMembers + ".*," + channelCols + " from " + channelMembers + " LEFT JOIN " + channel + " on " + channelMembers + ".member_uid=" + channel + ".channel_id and " + channel + ".channel_type=1 where " + channelMembers + ".channel_id=? and " + channelMembers + ".channel_type=? and " + channelMembers + ".is_deleted=0 and " + channelMembers + ".status=1 and (member_name like ? or member_remark like ? or channel_name like ? or channel_remark like ?) order by " + channelMembers + ".role=1 desc," + channelMembers + ".role=2 desc," + channelMembers + "." + WKDBColumns.WKChannelMembersColumns.created_at + " asc limit " + queryPage + "," + size + "";
String sql = "select " + channelMembers + ".*," + channelCols + " from " + channelMembers + " LEFT JOIN " + channel + " on " + channelMembers + ".member_uid=" + channel + ".channel_id and " + channel + ".channel_type=1 where " + channelMembers + ".channel_id=? and " + channelMembers + ".channel_type=? and " + channelMembers + ".is_deleted=0 and " + channelMembers + ".status=1 and (member_name like ? or member_remark like ? or channel_name like ? or channel_remark like ?) order by " + channelMembers + ".role=1 desc," + channelMembers + ".role=2 desc," + channelMembers + "." + WKDBColumns.WKChannelMembersColumns.created_at + " asc limit " + queryPage + "," + size;
Cursor cursor = WKIMApplication
.getInstance()
.getDbHelper().rawQuery(sql, args);
@ -66,7 +66,7 @@ public class ChannelMembersDbManager {
Object[] args = new Object[2];
args[0] = channelId;
args[1] = channelType;
String sql = "select " + channelMembers + ".*," + channelCols + " from " + channelMembers + " LEFT JOIN " + channel + " on " + channelMembers + ".member_uid=" + channel + ".channel_id and " + channel + ".channel_type=1 where " + channelMembers + ".channel_id=? and " + channelMembers + ".channel_type=? and " + channelMembers + ".is_deleted=0 and " + channelMembers + ".status=1 order by " + channelMembers + ".role=1 desc," + channelMembers + ".role=2 desc," + channelMembers + "." + WKDBColumns.WKChannelMembersColumns.created_at + " asc limit " + queryPage + "," + size + "";
String sql = "select " + channelMembers + ".*," + channelCols + " from " + channelMembers + " LEFT JOIN " + channel + " on " + channelMembers + ".member_uid=" + channel + ".channel_id and " + channel + ".channel_type=1 where " + channelMembers + ".channel_id=? and " + channelMembers + ".channel_type=? and " + channelMembers + ".is_deleted=0 and " + channelMembers + ".status=1 order by " + channelMembers + ".role=1 desc," + channelMembers + ".role=2 desc," + channelMembers + "." + WKDBColumns.WKChannelMembersColumns.created_at + " asc limit " + queryPage + "," + size;
Cursor cursor = WKIMApplication
.getInstance()
.getDbHelper().rawQuery(sql, args);
@ -336,7 +336,7 @@ public class ChannelMembersDbManager {
String where = WKDBColumns.WKChannelMembersColumns.channel_id + "=? and " + WKDBColumns.WKChannelMembersColumns.channel_type + "=? and " + WKDBColumns.WKChannelMembersColumns.member_uid + "=?";
String[] whereValue = new String[3];
whereValue[0] = channelID;
whereValue[1] = channelType + "";
whereValue[1] = String.valueOf(channelType);
whereValue[2] = uid;
int row = WKIMApplication.getInstance().getDbHelper()
.update(channelMembers, updateKey, updateValue, where, whereValue);
@ -428,13 +428,10 @@ public class ChannelMembersDbManager {
}
public synchronized List<WKChannelMember> queryRobotMembers(String channelId, byte channelType) {
Object[] args = new Object[2];
args[0] = channelId;
args[1] = channelType;
String sql = "select * from " + channelMembers + " where channel_id=? and channel_type=? and robot=1 and is_deleted=0";
String selection = "channel_id=? and channel_type=? and robot=1 and is_deleted=0";
Cursor cursor = WKIMApplication
.getInstance()
.getDbHelper().rawQuery(sql, args);
.getDbHelper().select(channelMembers, selection, new String[]{channelId, String.valueOf(channelType)}, null);
List<WKChannelMember> list = new ArrayList<>();
if (cursor == null) {
return list;
@ -447,14 +444,10 @@ public class ChannelMembersDbManager {
}
public List<WKChannelMember> queryWithRole(String channelId, byte channelType, int role) {
Object[] args = new Object[3];
args[0] = channelId;
args[1] = channelType;
args[2] = role;
String sql = "SELECT * FROM " + channelMembers + " WHERE channel_id=? AND channel_type=? AND role=? AND is_deleted=0";
String selection = "channel_id=? AND channel_type=? AND role=? AND is_deleted=0";
Cursor cursor = WKIMApplication
.getInstance()
.getDbHelper().rawQuery(sql, args);
.getDbHelper().select(channelMembers, selection, new String[]{channelId, String.valueOf(channelType), String.valueOf(role)}, null);
List<WKChannelMember> list = new ArrayList<>();
if (cursor == null) {
return list;

View File

@ -116,12 +116,11 @@ public class ConversationDbManager {
}
public List<WKConversationMsg> queryWithChannelType(byte channelType) {
String sql = "select * from " + conversation + " where channel_type=?";
List<WKConversationMsg> list = new ArrayList<>();
try (Cursor cursor = WKIMApplication
.getInstance()
.getDbHelper()
.rawQuery(sql, new Object[]{channelType})) {
.select(conversation, "channel_type=?", new String[]{String.valueOf(channelType)}, null)) {
if (cursor == null) {
return list;
}
@ -326,7 +325,7 @@ public class ConversationDbManager {
private synchronized WKConversationMsg queryWithChannelId(String channelId, byte channelType) {
WKConversationMsg msg = null;
String selection = WKDBColumns.WKCoverMessageColumns.channel_id + " = ? and " + WKDBColumns.WKCoverMessageColumns.channel_type + "=?";
String[] selectionArgs = new String[]{channelId, channelType + ""};
String[] selectionArgs = new String[]{channelId, String.valueOf(channelType)};
Cursor cursor = WKIMApplication
.getInstance()
.getDbHelper()
@ -349,10 +348,10 @@ public class ConversationDbManager {
public WKConversationMsgExtra queryMsgExtraWithChannel(String channelID, byte channelType) {
WKConversationMsgExtra msgExtra = null;
String sql = "select * from " + conversationExtra + " where channel_id=? and channel_type=?";
String selection = "channel_id=? and channel_type=?";
Cursor cursor = WKIMApplication
.getInstance()
.getDbHelper().rawQuery(sql, new Object[]{channelID, channelType});
.getDbHelper().select(conversationExtra, selection, new String[]{channelID, String.valueOf(channelType)}, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
msgExtra = serializeMsgExtra(cursor);

View File

@ -416,12 +416,10 @@ public class MsgDbManager {
args[3] = oldestOrderSeq;
sql = "SELECT * FROM (SELECT " + messageCols + "," + extraCols + " FROM " + message + " LEFT JOIN " + messageExtra + " on " + message + ".message_id=" + messageExtra + ".message_id WHERE " + message + ".channel_id=? and " + message + ".channel_type=? and from_uid=? and " + message + ".type<>0 and " + message + ".type<>99 AND " + message + ".order_seq<?) where is_deleted=0 and revoke=0 order by order_seq desc limit 0," + limit;
}
List<WKMsg> wkMsgs = new ArrayList<>();
Cursor cursor = null;
try {
cursor = WKIMApplication.getInstance().getDbHelper().rawQuery(sql, args);
List<WKMsg> wkMsgList = new ArrayList<>();
try (Cursor cursor = WKIMApplication.getInstance().getDbHelper().rawQuery(sql, args)) {
if (cursor == null) {
return wkMsgs;
return wkMsgList;
}
WKChannel wkChannel = ChannelDBManager.getInstance().query(channelID, channelType);
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
@ -432,12 +430,10 @@ public class MsgDbManager {
WKChannelMember member = ChannelMembersDbManager.getInstance().query(channelID, WKChannelType.GROUP, wkMsg.fromUID);
wkMsg.setMemberOfFrom(member);
}
wkMsgs.add(wkMsg);
wkMsgList.add(wkMsg);
}
} finally {
if (cursor != null) cursor.close();
}
return wkMsgs;
return wkMsgList;
}
public long queryOrderSeq(String channelID, byte channelType, long maxOrderSeq, int limit) {
@ -699,7 +695,7 @@ public class MsgDbManager {
updateValue[0] = msg.content;
updateKey[1] = WKDBColumns.WKMessageColumns.status;
updateValue[1] = msg.status + "";
updateValue[1] = String.valueOf(msg.status);
updateKey[2] = WKDBColumns.WKMessageColumns.message_id;
updateValue[2] = msg.messageID;
@ -748,10 +744,9 @@ public class MsgDbManager {
public WKMsg queryWithClientSeq(long clientSeq) {
WKMsg msg = null;
String sql = "select * from " + message + " where " + WKDBColumns.WKMessageColumns.client_seq + "=?";
try (Cursor cursor = WKIMApplication
.getInstance()
.getDbHelper().rawQuery(sql, new Object[]{clientSeq})) {
.getDbHelper().select(message, "client_seq=?", new String[]{String.valueOf(clientSeq)}, null)) {
if (cursor == null) {
return null;
}
@ -1101,7 +1096,7 @@ public class MsgDbManager {
String whereStr = "";
for (int contentType : contentTypes) {
if (TextUtils.isEmpty(whereStr)) {
whereStr = contentType + "";
whereStr = String.valueOf(contentType);
} else {
whereStr = "," + contentType;
}
@ -1466,11 +1461,11 @@ public class MsgDbManager {
String[] updateKey = new String[1];
String[] updateValue = new String[1];
updateKey[0] = WKDBColumns.WKMessageColumns.status;
updateValue[0] = status + "";
updateValue[0] = String.valueOf(status);
String where = WKDBColumns.WKMessageColumns.client_seq + "=?";
String[] whereValue = new String[1];
whereValue[0] = client_seq + "";
whereValue[0] = String.valueOf(client_seq);
int row = WKIMApplication.getInstance().getDbHelper()
.update(message, updateKey, updateValue, where, whereValue);
@ -1520,14 +1515,13 @@ public class MsgDbManager {
}
public WKMsgExtra queryMsgExtraWithMsgID(String msgID) {
String sql = "select * from " + messageExtra + " where message_id=?";
WKMsgExtra extra = null;
try {
if (WKIMApplication.getInstance().getDbHelper() != null) {
Cursor cursor = WKIMApplication
.getInstance()
.getDbHelper()
.rawQuery(sql, new Object[]{msgID});
.select(messageExtra, "message_id=?", new String[]{msgID}, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
extra = serializeMsgExtra(cursor);

View File

@ -9,8 +9,8 @@ import android.text.TextUtils;
import com.xinbida.wukongim.WKIM;
import com.xinbida.wukongim.WKIMApplication;
import com.xinbida.wukongim.entity.WKChannel;
import com.xinbida.wukongim.entity.WKMsgReaction;
import com.xinbida.wukongim.entity.WKChannelType;
import com.xinbida.wukongim.entity.WKMsgReaction;
import java.util.ArrayList;
import java.util.List;
@ -67,11 +67,9 @@ class MsgReactionDBManager {
private boolean isExist(String uid, String messageID) {
boolean isExist = false;
String sql = "select * from " + messageReaction
+ " where message_id=? and uid=?";
try (Cursor cursor = WKIMApplication
.getInstance()
.getDbHelper().rawQuery(sql, new Object[]{messageID, uid})) {
.getDbHelper().select(messageReaction, "message_id=? and uid=?", new String[]{messageID, uid}, null)) {
if (cursor != null && cursor.moveToLast()) {
isExist = true;
}

View File

@ -194,9 +194,7 @@ public class RobotDBManager {
public List<WKRobotMenu> queryRobotMenus(String robotID) {
List<WKRobotMenu> list = new ArrayList<>();
String sql = "select * from " + robotMenu + " where robot_id =?";
try (Cursor cursor = WKIMApplication.getInstance().getDbHelper().rawQuery(sql, new Object[]{robotID})) {
try (Cursor cursor = WKIMApplication.getInstance().getDbHelper().select(robotMenu, "robot_id=?", new String[]{robotID}, null)) {
if (cursor == null) {
return list;
}

View File

@ -56,8 +56,4 @@ public class WKCursor {
}
return placeholders.toString();
}
public static void copyArray(String[] src, String[] dest) {
System.arraycopy(src, 0, dest, 0, src.length);
}
}