Skip to content

Commit e5ed267

Browse files
committed
Update query
1 parent 8f2ad17 commit e5ed267

File tree

7 files changed

+87
-209
lines changed

7 files changed

+87
-209
lines changed

Mixin/Service/DeviceTransfer/DeviceTransferServerDataSource.swift

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ extension DeviceTransferServerDataSource {
131131
var fileCount = 0
132132
while let location = nextLocation {
133133
let (databaseItemCount, transferItems, nextPrimaryID, nextSecondaryID) = items(on: location)
134-
if transferItems.isEmpty && !(needsFilterData && location.type == .transcriptMessage) {
134+
if transferItems.isEmpty {
135135
Logger.general.info(category: "DeviceTransferServerDataSource", message: "\(location.type) is empty")
136136
}
137137
recordCount += transferItems.count
@@ -154,13 +154,7 @@ extension DeviceTransferServerDataSource {
154154
nextLocation = nil
155155
}
156156
if needsFilterData, location.type == .message {
157-
let message: String
158-
if transcriptMessageCount == 0 {
159-
message = "\(DeviceTransferRecordType.transcriptMessage) is empty"
160-
} else {
161-
message = "Send \(DeviceTransferRecordType.transcriptMessage) \(transcriptMessageCount)"
162-
}
163-
Logger.general.info(category: "DeviceTransferServerDataSource", message: message)
157+
Logger.general.info(category: "DeviceTransferServerDataSource", message: "Send \(DeviceTransferRecordType.transcriptMessage) \(transcriptMessageCount)")
164158
}
165159
if !needsFilterData || location.type != .transcriptMessage {
166160
Logger.general.info(category: "DeviceTransferServerDataSource", message: "Send \(location.type) \(recordCount)")
@@ -445,7 +439,7 @@ extension DeviceTransferServerDataSource {
445439
return false
446440
}
447441
#if DEBUG
448-
//Logger.general.debug(category: "DeviceTransferServerDataSource", message: "Send File: \(url)")
442+
Logger.general.debug(category: "DeviceTransferServerDataSource", message: "Send File: \(url)")
449443
#endif
450444

451445
let encryptor = try AESCryptor(operation: .encrypt, iv: iv, key: key.aes)

MixinServices/MixinServices/Database/User/DAO/ConversationDAO.swift

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -660,45 +660,27 @@ public final class ConversationDAO: UserDatabaseDAO {
660660
}
661661

662662
public func conversations(limit: Int, after conversationId: String?, matching conversationIDs: [String]?) -> [Conversation] {
663+
var sql = "SELECT * FROM conversations"
663664
if let conversationIDs {
664-
var totalConversations = [Conversation]()
665-
for i in stride(from: 0, to: conversationIDs.count, by: Self.strideForDeviceTransfer) {
666-
let endIndex = min(i + Self.strideForDeviceTransfer, conversationIDs.count)
667-
let ids = Array(conversationIDs[i..<endIndex]).joined(separator: "', '")
668-
var sql = "SELECT * FROM conversations WHERE conversation_id in ('\(ids)')"
669-
if let conversationId {
670-
sql += " AND ROWID > IFNULL((SELECT ROWID FROM conversations WHERE conversation_id = '\(conversationId)'), 0)"
671-
}
672-
sql += " ORDER BY ROWID LIMIT ?"
673-
let conversations: [Conversation] = db.select(with: sql, arguments: [limit])
674-
totalConversations += conversations
675-
}
676-
return totalConversations
677-
} else {
678-
var sql = "SELECT * FROM conversations"
679-
if let conversationId {
680-
sql += " WHERE ROWID > IFNULL((SELECT ROWID FROM conversations WHERE conversation_id = '\(conversationId)'), 0)"
681-
}
682-
sql += " ORDER BY ROWID LIMIT ?"
683-
return db.select(with: sql, arguments: [limit])
665+
let ids = conversationIDs.joined(separator: "', '")
666+
sql += " WHERE conversation_id in ('\(ids)')"
684667
}
668+
if let conversationId {
669+
sql += conversationIDs == nil ? " WHERE " : " AND "
670+
sql += "ROWID > IFNULL((SELECT ROWID FROM conversations WHERE conversation_id = '\(conversationId)'), 0)"
671+
}
672+
sql += " ORDER BY ROWID LIMIT ?"
673+
return db.select(with: sql, arguments: [limit])
685674
}
686675

687676
public func conversationsCount(matching conversationIDs: [String]?) -> Int {
677+
var sql = "SELECT COUNT(*) FROM conversations"
688678
if let conversationIDs {
689-
var totalCount = 0
690-
for i in stride(from: 0, to: conversationIDs.count, by: Self.strideForDeviceTransfer) {
691-
let endIndex = min(i + Self.strideForDeviceTransfer, conversationIDs.count)
692-
let ids = Array(conversationIDs[i..<endIndex]).joined(separator: "', '")
693-
let sql = "SELECT COUNT(*) FROM conversations WHERE conversation_id in ('\(ids)')"
694-
let count: Int? = db.select(with: sql)
695-
totalCount += (count ?? 0)
696-
}
697-
return totalCount
698-
} else {
699-
let count: Int? = db.select(with: "SELECT COUNT(*) FROM conversations")
700-
return count ?? 0
679+
let ids = conversationIDs.joined(separator: "', '")
680+
sql += " WHERE conversation_id in ('\(ids)')"
701681
}
682+
let count: Int? = db.select(with: sql)
683+
return count ?? 0
702684
}
703685

704686
public func save(conversation: Conversation) {

MixinServices/MixinServices/Database/User/DAO/MessageDAO.swift

Lines changed: 27 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -981,20 +981,13 @@ extension MessageDAO {
981981
}
982982

983983
public func messages(limit: Int, after rowID: Int, matching conversationIDs: [String]?) -> [Message] {
984+
var sql = "SELECT * FROM messages WHERE ROWID > ?"
984985
if let conversationIDs {
985-
var totalMessages = [Message]()
986-
for i in stride(from: 0, to: conversationIDs.count, by: Self.strideForDeviceTransfer) {
987-
let endIndex = min(i + Self.strideForDeviceTransfer, conversationIDs.count)
988-
let ids = Array(conversationIDs[i..<endIndex]).joined(separator: "', '")
989-
let sql = "SELECT * FROM messages WHERE ROWID > ? AND conversation_id in ('\(ids)') ORDER BY ROWID LIMIT ?"
990-
let messages: [Message] = db.select(with: sql, arguments: [rowID, limit])
991-
totalMessages += messages
992-
}
993-
return totalMessages
994-
} else {
995-
let sql = "SELECT * FROM messages WHERE ROWID > ? ORDER BY ROWID LIMIT ?"
996-
return db.select(with: sql, arguments: [rowID, limit])
986+
let ids = conversationIDs.joined(separator: "', '")
987+
sql += " AND conversation_id in ('\(ids)')"
997988
}
989+
sql += " ORDER BY ROWID LIMIT ?"
990+
return db.select(with: sql, arguments: [rowID, limit])
998991
}
999992

1000993
public func messageRowID(createdAt: String) -> Int? {
@@ -1006,71 +999,42 @@ extension MessageDAO {
1006999
}
10071000

10081001
public func messagesCount(matching conversationIDs: [String]?, sinceDate date: String?) -> Int {
1002+
var sql = "SELECT COUNT(*) FROM messages"
1003+
if let date {
1004+
sql += " WHERE created_at >= '\(date)'"
1005+
}
10091006
if let conversationIDs {
1010-
var totalCount = 0
1011-
for i in stride(from: 0, to: conversationIDs.count, by: Self.strideForDeviceTransfer) {
1012-
let endIndex = min(i + Self.strideForDeviceTransfer, conversationIDs.count)
1013-
let ids = Array(conversationIDs[i..<endIndex]).joined(separator: "', '")
1014-
var sql = "SELECT COUNT(*) FROM messages WHERE conversation_id in ('\(ids)')"
1015-
if let date {
1016-
sql += " AND created_at >= '\(date)'"
1017-
}
1018-
let count: Int? = db.select(with: sql)
1019-
totalCount += (count ?? 0)
1020-
}
1021-
return totalCount
1022-
} else {
1023-
var sql = "SELECT COUNT(*) FROM messages"
1024-
if let date {
1025-
sql += " WHERE created_at >= '\(date)'"
1026-
}
1027-
let count: Int? = db.select(with: sql)
1028-
return count ?? 0
1007+
let ids = conversationIDs.joined(separator: "', '")
1008+
sql += date == nil ? " WHERE " : " AND "
1009+
sql += "conversation_id in ('\(ids)')"
10291010
}
1011+
let count: Int? = db.select(with: sql)
1012+
return count ?? 0
10301013
}
10311014

10321015
public func mediaMessagesCount(matching conversationIDs: [String]?) -> Int {
10331016
let categories = MessageCategory.allMediaCategories.map(\.rawValue).joined(separator: "', '")
1017+
var sql = "SELECT COUNT(*) FROM messages WHERE category in ('\(categories)')"
10341018
if let conversationIDs {
1035-
var totalCount = 0
1036-
for i in stride(from: 0, to: conversationIDs.count, by: Self.strideForDeviceTransfer) {
1037-
let endIndex = min(i + Self.strideForDeviceTransfer, conversationIDs.count)
1038-
let ids = Array(conversationIDs[i..<endIndex]).joined(separator: "', '")
1039-
var sql = "SELECT COUNT(*) FROM messages WHERE conversation_id in ('\(ids)') AND category in ('\(categories)')"
1040-
let count: Int? = db.select(with: sql)
1041-
totalCount += (count ?? 0)
1042-
}
1043-
return totalCount
1044-
} else {
1045-
var sql = "SELECT COUNT(*) FROM messages WHERE category in ('\(categories)')"
1046-
let count: Int? = db.select(with: sql)
1047-
return count ?? 0
1019+
let ids = conversationIDs.joined(separator: "', '")
1020+
sql += " AND conversation_id in ('\(ids)')"
10481021
}
1022+
let count: Int? = db.select(with: sql)
1023+
return count ?? 0
10491024
}
10501025

10511026
public func transcriptMessageCount(matching conversationIDs: [String]?, sinceDate date: String?) -> Int {
10521027
let categories = MessageCategory.transcriptCategories.map(\.rawValue).joined(separator: "', '")
1028+
var sql = "SELECT COUNT(*) FROM messages WHERE category in ('\(categories)')"
1029+
if let date {
1030+
sql += " AND created_at >= '\(date)'"
1031+
}
10531032
if let conversationIDs {
1054-
var totalCount = 0
1055-
for i in stride(from: 0, to: conversationIDs.count, by: Self.strideForDeviceTransfer) {
1056-
let endIndex = min(i + Self.strideForDeviceTransfer, conversationIDs.count)
1057-
let ids = Array(conversationIDs[i..<endIndex]).joined(separator: "', '")
1058-
var sql = "SELECT COUNT(*) FROM messages WHERE conversation_id in ('\(ids)') AND category in ('\(categories)')"
1059-
if let date {
1060-
sql += " AND created_at >= '\(date)'"
1061-
}
1062-
let count: Int? = db.select(with: sql)
1063-
totalCount += (count ?? 0)
1064-
}
1065-
return totalCount
1066-
} else {
1067-
var sql = "SELECT COUNT(*) FROM messages WHERE category in ('\(categories)')"
1068-
if let date {
1069-
sql += " AND created_at >= '\(date)'"
1070-
}
1071-
let count: Int? = db.select(with: sql)
1072-
return count ?? 0
1033+
let ids = conversationIDs.joined(separator: "', '")
1034+
sql += " AND conversation_id in ('\(ids)')"
10731035
}
1036+
let count: Int? = db.select(with: sql)
1037+
return count ?? 0
10741038
}
10751039

10761040
public func lastMessageCreatedAt() -> String? {

MixinServices/MixinServices/Database/User/DAO/MessageMentionDAO.swift

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,45 +16,27 @@ public final class MessageMentionDAO: UserDatabaseDAO {
1616
}
1717

1818
public func messageMentions(limit: Int, after messageId: String?, matching conversationIDs: [String]?) -> [MessageMention] {
19+
var sql = "SELECT * FROM message_mentions"
1920
if let conversationIDs {
20-
var totalMessageMentions = [MessageMention]()
21-
for i in stride(from: 0, to: conversationIDs.count, by: Self.strideForDeviceTransfer) {
22-
let endIndex = min(i + Self.strideForDeviceTransfer, conversationIDs.count)
23-
let ids = Array(conversationIDs[i..<endIndex]).joined(separator: "', '")
24-
var sql = "SELECT * FROM message_mentions WHERE conversation_id in ('\(ids)')"
25-
if let messageId {
26-
sql += " AND ROWID > IFNULL((SELECT ROWID FROM message_mentions WHERE message_id = '\(messageId)'), 0)"
27-
}
28-
sql += " ORDER BY ROWID LIMIT ?"
29-
let messageMentions: [MessageMention] = db.select(with: sql, arguments: [limit])
30-
totalMessageMentions += messageMentions
31-
}
32-
return totalMessageMentions
33-
} else {
34-
var sql = "SELECT * FROM message_mentions"
35-
if let messageId {
36-
sql += " WHERE ROWID > IFNULL((SELECT ROWID FROM message_mentions WHERE message_id = '\(messageId)'), 0)"
37-
}
38-
sql += " ORDER BY ROWID LIMIT ?"
39-
return db.select(with: sql, arguments: [limit])
21+
let ids = conversationIDs.joined(separator: "', '")
22+
sql += " WHERE conversation_id in ('\(ids)')"
4023
}
24+
if let messageId {
25+
sql += conversationIDs == nil ? " WHERE " : " AND "
26+
sql += "ROWID > IFNULL((SELECT ROWID FROM message_mentions WHERE message_id = '\(messageId)'), 0)"
27+
}
28+
sql += " ORDER BY ROWID LIMIT ?"
29+
return db.select(with: sql, arguments: [limit])
4130
}
4231

4332
public func messageMentionsCount(matching conversationIDs: [String]?) -> Int {
33+
var sql = "SELECT COUNT(*) FROM message_mentions"
4434
if let conversationIDs {
45-
var totalCount = 0
46-
for i in stride(from: 0, to: conversationIDs.count, by: Self.strideForDeviceTransfer) {
47-
let endIndex = min(i + Self.strideForDeviceTransfer, conversationIDs.count)
48-
let ids = Array(conversationIDs[i..<endIndex]).joined(separator: "', '")
49-
let sql = "SELECT COUNT(*) FROM message_mentions WHERE conversation_id in ('\(ids)')"
50-
let count: Int? = db.select(with: sql)
51-
totalCount += (count ?? 0)
52-
}
53-
return totalCount
54-
} else {
55-
let count: Int? = db.select(with: "SELECT COUNT(*) FROM message_mentions")
56-
return count ?? 0
35+
let ids = conversationIDs.joined(separator: "', '")
36+
sql += " WHERE conversation_id in ('\(ids)')"
5737
}
38+
let count: Int? = db.select(with: sql)
39+
return count ?? 0
5840
}
5941

6042
public func save(messageMention: MessageMention) {

MixinServices/MixinServices/Database/User/DAO/ParticipantDAO.swift

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -175,46 +175,28 @@ public final class ParticipantDAO: UserDatabaseDAO {
175175
.map({ ParticipantRequest(userId: $0.userId, role: $0.role) })
176176
}
177177

178-
public func participants(limit: Int, after conversationId: String?, with userId: String?, matching conversationIDs: [String]?) -> [Participant] {
178+
public func participants(limit: Int, after conversationId: String?, with userId: String?, matching conversationIDs: [String]?) -> [Participant] {
179+
var sql = "SELECT * FROM participants"
179180
if let conversationIDs {
180-
var totalParticipants = [Participant]()
181-
for i in stride(from: 0, to: conversationIDs.count, by: Self.strideForDeviceTransfer) {
182-
let endIndex = min(i + Self.strideForDeviceTransfer, conversationIDs.count)
183-
let ids = Array(conversationIDs[i..<endIndex]).joined(separator: "', '")
184-
var sql = "SELECT * FROM participants WHERE conversation_id in ('\(ids)')"
185-
if let conversationId, let userId {
186-
sql += " AND ROWID > IFNULL((SELECT ROWID FROM participants WHERE conversation_id = '\(conversationId)' AND user_id = '\(userId)'), 0)"
187-
}
188-
sql += " ORDER BY ROWID LIMIT ?"
189-
let participants: [Participant] = db.select(with: sql, arguments: [limit])
190-
totalParticipants += participants
191-
}
192-
return totalParticipants
193-
} else {
194-
var sql = "SELECT * FROM participants"
195-
if let conversationId, let userId {
196-
sql += " WHERE ROWID > IFNULL((SELECT ROWID FROM participants WHERE conversation_id = '\(conversationId)' AND user_id = '\(userId)'), 0)"
197-
}
198-
sql += " ORDER BY ROWID LIMIT ?"
199-
return db.select(with: sql, arguments: [limit])
181+
let ids = conversationIDs.joined(separator: "', '")
182+
sql += " WHERE conversation_id in ('\(ids)')"
200183
}
184+
if let conversationId, let userId {
185+
sql += conversationIDs == nil ? " WHERE " : " AND "
186+
sql += "ROWID > IFNULL((SELECT ROWID FROM participants WHERE conversation_id = '\(conversationId)' AND user_id = '\(userId)'), 0)"
187+
}
188+
sql += " ORDER BY ROWID LIMIT ?"
189+
return db.select(with: sql, arguments: [limit])
201190
}
202191

203192
public func participantsCount(matching conversationIDs: [String]?) -> Int {
193+
var sql = "SELECT COUNT(*) FROM participants"
204194
if let conversationIDs {
205-
var totalCount = 0
206-
for i in stride(from: 0, to: conversationIDs.count, by: Self.strideForDeviceTransfer) {
207-
let endIndex = min(i + Self.strideForDeviceTransfer, conversationIDs.count)
208-
let ids = Array(conversationIDs[i..<endIndex]).joined(separator: "', '")
209-
let sql = "SELECT COUNT(*) FROM participants WHERE conversation_id in ('\(conversationIDs)')"
210-
let count: Int? = db.select(with: sql)
211-
totalCount += (count ?? 0)
212-
}
213-
return totalCount
214-
} else {
215-
let count: Int? = db.select(with: "SELECT COUNT(*) FROM participants")
216-
return count ?? 0
195+
let ids = conversationIDs.joined(separator: "', '")
196+
sql += " WHERE conversation_id in ('\(ids)')"
217197
}
198+
let count: Int? = db.select(with: sql)
199+
return count ?? 0
218200
}
219201

220202
public func save(participant: Participant) {

0 commit comments

Comments
 (0)