1+ import GRDB
12import IMessageCore
23import Logging
34
@@ -6,19 +7,16 @@ private let log = Logger(label: "imdb.chats")
67public extension IMDatabase {
78 // TODO: replace with overload that takes `GUID`
89 func chat( withGUID chatGUID: String ) throws -> Chat ? {
9- let statement = try cachedStatement ( forEscapedSQL: """
10- SELECT ROWID, display_name, service_name
11- FROM chat
12- WHERE guid = ?
13- """ )
14-
15- try statement. reset ( )
16- try statement. bind ( chatGUID)
17-
18- let chats = try statement. mapRowsUntilDone { row in
19- let displayName = try row [ 1 ] . optional ( String . self) ? . nonEmpty
20- let serviceName = try Chat . ServiceName ( rawValue: row [ 2 ] . expect ( String . self) )
21- return try Chat ( id: row [ 0 ] . expect ( Int . self) , guid: GUID ( chatGUID) , displayName: displayName, serviceName: serviceName)
10+ let chats = try read { db in
11+ try Row . fetchAll ( db, sql: """
12+ SELECT ROWID, display_name, service_name
13+ FROM chat
14+ WHERE guid = ?
15+ """ , arguments: [ chatGUID] ) . map { row in
16+ let displayName = row. optionalString ( at: 1 ) ? . nonEmpty
17+ let serviceName = Chat . ServiceName ( rawValue: row. requiredString ( at: 2 ) )
18+ return Chat ( id: row. requiredInt ( at: 0 ) , guid: GUID ( chatGUID) , displayName: displayName, serviceName: serviceName)
19+ }
2220 }
2321
2422 if chats. count > 1 {
@@ -32,41 +30,36 @@ public extension IMDatabase {
3230 }
3331
3432 func chats( ) throws -> [ Chat ] {
35- let statement = try cachedStatement ( forEscapedSQL: """
36- SELECT ROWID, guid, display_name, service_name
37- FROM chat
38- """ )
39-
40- try statement. reset ( )
41-
42- return try statement. mapRowsUntilDone { row -> Chat ? in
43- let id = try row [ 0 ] . expect ( Int . self)
44- guard let guid = try row [ 1 ] . optional ( String . self) else {
45- log. error ( " chat \( id) has no GUID, very spooky. dropping it on the ground " )
46- return nil
33+ try read { db in
34+ try Row . fetchAll ( db, sql: """
35+ SELECT ROWID, guid, display_name, service_name
36+ FROM chat
37+ """ ) . compactMap { row -> Chat ? in
38+ let id = row. requiredInt ( at: 0 )
39+ guard let guid = row. optionalString ( at: 1 ) else {
40+ log. error ( " chat \( id) has no GUID, very spooky. dropping it on the ground " )
41+ return nil
42+ }
43+ let displayName = row. optionalString ( at: 2 ) ? . nonEmpty
44+ let serviceName = Chat . ServiceName ( rawValue: row. optionalString ( at: 3 ) ?? " NONE " )
45+ return Chat ( id: id, guid: GUID ( guid) , displayName: displayName, serviceName: serviceName)
4746 }
48- let displayName = try row [ 2 ] . optional ( String . self) ? . nonEmpty
49- let serviceName = try Chat . ServiceName ( rawValue: row [ 3 ] . optional ( String . self) ?? " NONE " )
50- return Chat ( id: id, guid: GUID ( guid) , displayName: displayName, serviceName: serviceName)
51- } . compactMap ( \. self)
47+ }
5248 }
5349
5450 // this doesn't include the user themselves, just everyone else in the group chat,
5551 // UNLESS the user went out of their way to redundantly add themselves, which is possible when initially creating the chat
5652 func handles( inChatWithGUID chatGUID: String ) throws -> [ Handle ] {
57- let statement = try cachedStatement ( forEscapedSQL: """
58- SELECT handle.ROWID, handle.id
59- FROM chat
60- INNER JOIN chat_handle_join ON chat_handle_join.chat_id = chat.ROWID
61- INNER JOIN handle ON handle.ROWID = chat_handle_join.handle_id
62- WHERE chat.guid = ?
63- """ )
64-
65- try statement. reset ( )
66- try statement. bind ( chatGUID)
67-
68- return try statement. mapRowsUntilDone { row in
69- try Handle ( rowid: row [ 0 ] . expect ( Int . self) , id: row [ 1 ] . expect ( String . self) )
53+ try read { db in
54+ try Row . fetchAll ( db, sql: """
55+ SELECT handle.ROWID, handle.id
56+ FROM chat
57+ INNER JOIN chat_handle_join ON chat_handle_join.chat_id = chat.ROWID
58+ INNER JOIN handle ON handle.ROWID = chat_handle_join.handle_id
59+ WHERE chat.guid = ?
60+ """ , arguments: [ chatGUID] ) . map { row in
61+ Handle ( rowid: row. requiredInt ( at: 0 ) , id: row. requiredString ( at: 1 ) )
62+ }
7063 }
7164 }
7265}
0 commit comments