From 0253ee01364dfc208b88ce96bdd76277cff8a4ea Mon Sep 17 00:00:00 2001 From: SL Date: Tue, 5 Dec 2023 23:09:20 +0800 Subject: [PATCH] Optimize message queries --- CHANGELOG.md | 4 +++- README.md | 2 +- lib/db/channel.dart | 18 ++++++---------- lib/db/channel_member.dart | 36 +++++++++++++------------------ lib/db/const.dart | 11 ++++++++++ lib/db/conversation.dart | 14 +++---------- lib/db/message.dart | 43 +++++++------------------------------- lib/db/reaction.dart | 15 +++---------- lib/db/reminder.dart | 25 +++++----------------- pubspec.yaml | 2 +- 10 files changed, 54 insertions(+), 116 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c50d61..9f5c1ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,4 +43,6 @@ ### 1.2.1 * Update channel info refresh listener and optimize data insertion issues ### 1.2.2 - * Modifying the issue of reconnecting disconnected objects without destroying them \ No newline at end of file + * Modifying the issue of reconnecting disconnected objects without destroying them + ### 1.2.3 + * Optimize message queries \ No newline at end of file diff --git a/README.md b/README.md index de259f3..1a7293b 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ #### 安装 ``` dependencies: - wukongimfluttersdk: ^1.2.2 + wukongimfluttersdk: ^1.2.3 ``` #### 引入 ```dart diff --git a/lib/db/channel.dart b/lib/db/channel.dart index 6773a9e..09a0d6a 100644 --- a/lib/db/channel.dart +++ b/lib/db/channel.dart @@ -96,21 +96,15 @@ class ChannelDB { if (channelIDs.isEmpty) { return []; } - StringBuffer sb = StringBuffer(); - for (int i = 0, size = channelIDs.length; i < size; i++) { - if (i != 0) { - sb.write(","); - } - sb.write("'"); - sb.write(channelIDs[i]); - sb.write("'"); - } - String channelIds = sb.toString(); + List args = []; + args.addAll(channelIDs); + args.add(channelType); List list = []; List> results = await WKDBHelper.shared.getDB().query( WKDBConst.tableChannel, - where: "channel_id in (?) and channel_type=?", - whereArgs: [channelIds, channelType]); + where: + "channel_id in (${WKDBConst.getPlaceholders(channelIDs.length)}) and channel_type=?", + whereArgs: args); if (results.isNotEmpty) { for (Map data in results) { list.add(WKDBConst.serializeChannel(data)); diff --git a/lib/db/channel_member.dart b/lib/db/channel_member.dart index e9e1f0e..d638f11 100644 --- a/lib/db/channel_member.dart +++ b/lib/db/channel_member.dart @@ -14,20 +14,16 @@ class ChannelMemberDB { Future> queryMemberWithUIDs( String channelID, int channelType, List uidList) async { - StringBuffer sb = StringBuffer(); - for (int i = 0, size = uidList.length; i < size; i++) { - if (i != 0) { - sb.write(","); - } - sb.write("'"); - sb.write(uidList[i]); - sb.write("'"); - } + List args = []; + args.add(channelID); + args.add(channelType); + args.addAll(uidList); List list = []; List> results = await WKDBHelper.shared.getDB().query( WKDBConst.tableChannelMember, - where: "channel_id=? and channel_type=? and member_uid in (?)", - whereArgs: [channelID, channelType, sb.toString()]); + where: + "channel_id=? and channel_type=? and member_uid in (${WKDBConst.getPlaceholders(uidList.length)})", + whereArgs: args); if (results.isNotEmpty) { for (Map data in results) { list.add(WKDBConst.serializeChannelMember(data)); @@ -81,21 +77,17 @@ class ChannelMemberDB { Future> queryWithUIDs( String channelID, int channelType, List uidList) async { - StringBuffer sb = StringBuffer(); - for (int i = 0, size = uidList.length; i < size; i++) { - if (i != 0) { - sb.write(","); - } - sb.write("'"); - sb.write(uidList[i]); - sb.write("'"); - } + List args = []; + args.add(channelID); + args.add(channelType); + args.addAll(uidList); List list = []; List> results = await WKDBHelper.shared.getDB().query( WKDBConst.tableChannelMember, - where: "channel_id=? and channel_type=? and member_uid in (?) ", - whereArgs: [channelID, channelType, sb.toString()]); + where: + "channel_id=? and channel_type=? and member_uid in (${WKDBConst.getPlaceholders(uidList.length)}) ", + whereArgs: args); if (results.isNotEmpty) { for (Map data in results) { list.add(WKDBConst.serializeChannelMember(data)); diff --git a/lib/db/const.dart b/lib/db/const.dart index d7eb785..546eb5c 100644 --- a/lib/db/const.dart +++ b/lib/db/const.dart @@ -245,4 +245,15 @@ class WKDBConst { } return result.toString(); } + + static String getPlaceholders(int count) { + StringBuffer placeholders = StringBuffer(); + for (int i = 0; i < count; i++) { + if (i != 0) { + placeholders.write(", "); + } + placeholders.write("?"); + } + return placeholders.toString(); + } } diff --git a/lib/db/conversation.dart b/lib/db/conversation.dart index d431468..5a4fc04 100644 --- a/lib/db/conversation.dart +++ b/lib/db/conversation.dart @@ -113,20 +113,12 @@ class ConversationDB { Future> queryWithChannelIds( List channelIds) async { - StringBuffer sb = StringBuffer(); - for (var i = 0; i < channelIds.length; i++) { - if (i != 0) { - sb.write(","); - } - sb.write("'"); - sb.write(channelIds[i]); - sb.write("'"); - } List list = []; List> results = await WKDBHelper.shared.getDB().query( WKDBConst.tableConversation, - where: "channel_id in (?)", - whereArgs: [sb.toString()]); + where: + "channel_id in (${WKDBConst.getPlaceholders(channelIds.length)})", + whereArgs: channelIds); if (results.isNotEmpty) { for (Map data in results) { list.add(WKDBConst.serializeCoversation(data)); diff --git a/lib/db/message.dart b/lib/db/message.dart index 5c53349..2d9f9a9 100644 --- a/lib/db/message.dart +++ b/lib/db/message.dart @@ -103,21 +103,11 @@ class MessageDB { } Future> queryWithMessageIds(List messageIds) async { - StringBuffer sb = StringBuffer(); - for (int i = 0, size = messageIds.length; i < size; i++) { - if (i != 0) { - sb.write(","); - } - sb.write("'"); - sb.write(messageIds[i]); - sb.write("'"); - } - String sql = - "select $messageCols,$extraCols from ${WKDBConst.tableMessage} LEFT JOIN ${WKDBConst.tableMessageExtra} ON ${WKDBConst.tableMessage}.message_id=${WKDBConst.tableMessageExtra}.message_id WHERE ${WKDBConst.tableMessage}.message_id in (?)"; + "select $messageCols,$extraCols from ${WKDBConst.tableMessage} LEFT JOIN ${WKDBConst.tableMessageExtra} ON ${WKDBConst.tableMessage}.message_id=${WKDBConst.tableMessageExtra}.message_id WHERE ${WKDBConst.tableMessage}.message_id in (${WKDBConst.getPlaceholders(messageIds.length)}})"; List list = []; List> results = - await WKDBHelper.shared.getDB().rawQuery(sql, [sb.toString()]); + await WKDBHelper.shared.getDB().rawQuery(sql, messageIds); if (results.isNotEmpty) { for (Map data in results) { list.add(WKDBConst.serializeWKMsg(data)); @@ -582,21 +572,11 @@ class MessageDB { Future> queryWithClientMsgNos(List clientMsgNos) async { List msgs = []; - StringBuffer sb = StringBuffer(); - - for (int i = 0, size = clientMsgNos.length; i < size; i++) { - if (i != 0) { - sb.write(","); - } - sb.write("'"); - sb.write(clientMsgNos[i]); - sb.write("'"); - } - List> results = await WKDBHelper.shared.getDB().query( WKDBConst.tableMessage, - where: "client_msg_no in (?)", - whereArgs: [sb.toString()]); + where: + "client_msg_no in (${WKDBConst.getPlaceholders(clientMsgNos.length)})", + whereArgs: clientMsgNos); if (results.isNotEmpty) { for (Map data in results) { msgs.add(WKDBConst.serializeWKMsg(data)); @@ -690,20 +670,11 @@ class MessageDB { } Future> queryMsgExtrasWithMsgIds(List msgIds) async { - StringBuffer sb = StringBuffer(); - for (int i = 0, size = msgIds.length; i < size; i++) { - if (i != 0) { - sb.write(","); - } - sb.write("'"); - sb.write(msgIds[i]); - sb.write("'"); - } List list = []; List> results = await WKDBHelper.shared.getDB().query( WKDBConst.tableMessageExtra, - where: "message_id in (?)", - whereArgs: [sb.toString()]); + where: "message_id in (${WKDBConst.getPlaceholders(msgIds.length)})", + whereArgs: msgIds); if (results.isNotEmpty) { for (Map data in results) { list.add(WKDBConst.serializeMsgExtra(data)); diff --git a/lib/db/reaction.dart b/lib/db/reaction.dart index 954a67f..2e01483 100644 --- a/lib/db/reaction.dart +++ b/lib/db/reaction.dart @@ -42,21 +42,12 @@ class ReactionDB { Future> queryWithMessageIds( List messageIds) async { - StringBuffer sb = StringBuffer(); - for (int i = 0, size = messageIds.length; i < size; i++) { - if (i != 0) { - sb.write(","); - } - sb.write("'"); - sb.write(messageIds[i]); - sb.write("'"); - } - List list = []; List> results = await WKDBHelper.shared.getDB().query( WKDBConst.tableMessageReaction, - where: "message_id in (?) and is_deleted=0", - whereArgs: [sb.toString()], + where: + "message_id in (${WKDBConst.getPlaceholders(messageIds.length)}) and is_deleted=0", + whereArgs: messageIds, orderBy: "created_at desc"); if (results.isNotEmpty) { for (Map data in results) { diff --git a/lib/db/reminder.dart b/lib/db/reminder.dart index 432af13..0b57b22 100644 --- a/lib/db/reminder.dart +++ b/lib/db/reminder.dart @@ -118,20 +118,12 @@ class ReminderDB { } Future> queryWithChannelIds(List channelIds) async { - StringBuffer sb = StringBuffer(); - for (int i = 0, size = channelIds.length; i < size; i++) { - if (i != 0) { - sb.write(","); - } - sb.write("'"); - sb.write(channelIds[i]); - sb.write("'"); - } List list = []; List> results = await WKDBHelper.shared.getDB().query( WKDBConst.tableReminders, - where: "channel_id in (?)", - whereArgs: [sb.toString()]); + where: + "channel_id in (${WKDBConst.getPlaceholders(channelIds.length)})", + whereArgs: channelIds); if (results.isNotEmpty) { for (Map data in results) { list.add(WKDBConst.serializeReminder(data)); @@ -141,18 +133,11 @@ class ReminderDB { } Future> queryWithIds(List ids) async { - StringBuffer sb = StringBuffer(); - for (int i = 0, size = ids.length; i < size; i++) { - if (sb.length != 0) { - sb.write(','); - } - sb.write(ids[i]); - } List list = []; List> results = await WKDBHelper.shared.getDB().query( WKDBConst.tableReminders, - where: "reminder_id in (?)", - whereArgs: [sb.toString()]); + where: "reminder_id in (${WKDBConst.getPlaceholders(ids.length)})", + whereArgs: ids); if (results.isNotEmpty) { for (Map data in results) { list.add(WKDBConst.serializeReminder(data)); diff --git a/pubspec.yaml b/pubspec.yaml index 23633a3..e93eef5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -15,7 +15,7 @@ description: wukong IM flutter sdk # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # In Windows, build-name is used as the major, minor, and patch parts # of the product and file versions while build-number is used as the build suffix. -version: 1.2.2 +version: 1.2.3 homepage: https://github.com/WuKongIM/WuKongIMFlutterSDK environment: