@@ -47,67 +47,85 @@ public class IMConversationQuery: LCQuery {
4747
4848 // MARK: Combine
4949
50- /**
51- Get logic AND of another query.
52- Note that it only combine constraints of two queries, the limit and skip option will be discarded.
53-
54- - parameter query: The another query.
55- - returns: The logic AND of two queries.
56- */
57- public func and( _ query: IMConversationQuery ) throws -> IMConversationQuery ? {
58- return try self . combine ( op: " $and " , query: query)
50+ private func validateClient( _ query: IMConversationQuery ) throws {
51+ guard let selfClient = self . client,
52+ let queryClient = query. client,
53+ selfClient === queryClient else {
54+ throw LCError (
55+ code: . inconsistency,
56+ reason: " `self.client` !== query.client, they should be the same instance. " )
57+ }
5958 }
6059
61- /**
62- Get logic OR of another query.
63- Note that it only combine constraints of two queries, the limit and skip option will be discarded.
64-
65- - parameter query: The another query.
66- - returns: The logic OR of two queries.
67- */
68- public func or( _ query: IMConversationQuery ) throws -> IMConversationQuery ? {
69- return try self . combine ( op: " $or " , query: query)
60+ private static func validateClient( _ queries: [ IMConversationQuery ] ) throws {
61+ guard let first = queries. first else {
62+ return
63+ }
64+ for item in queries {
65+ try first. validateClient ( item)
66+ }
7067 }
7168
72- private func combine( op : String , query : IMConversationQuery ) throws -> IMConversationQuery ? {
73- guard let client = self . client else {
74- return nil
75- }
76- guard client === query . client else {
69+ private static func combine(
70+ queries : [ IMConversationQuery ] ,
71+ operation : String ) throws -> IMConversationQuery ?
72+ {
73+ guard let first = queries . first else {
7774 throw LCError (
7875 code: . inconsistency,
79- reason: " `client` !== `query.client`, they should be the same instance. " )
76+ reason: " `queries` is empty. " )
77+ }
78+ guard let client = first. client else {
79+ return nil
8080 }
81- let result = IMConversationQuery ( client: client, eventQueue: self . eventQueue)
82- result. constraintDictionary [ op] = [ self . constraintDictionary, query. constraintDictionary]
83- return result
81+ try self . validateClient ( queries)
82+ let query = IMConversationQuery (
83+ client: client,
84+ eventQueue: first. eventQueue)
85+ query. constraintDictionary [ operation] = queries. map { $0. constraintDictionary }
86+ return query
87+ }
88+
89+ /// Performs a logical AND operation on an array of one or more expressions of query.
90+ /// - Parameter queries: An array of one or more expressions of query.
91+ /// - Throws: `LCError`
92+ /// - Returns: An optional `IMConversationQuery`
93+ public static func and( _ queries: [ IMConversationQuery ] ) throws -> IMConversationQuery ? {
94+ return try self . combine ( queries: queries, operation: " $and " )
95+ }
96+
97+ /// Performs a logical AND operation on self and the query.
98+ /// - Parameter query: The query.
99+ /// - Throws: `LCError`
100+ /// - Returns: An optional `IMConversationQuery`
101+ public func and( _ query: IMConversationQuery ) throws -> IMConversationQuery ? {
102+ return try IMConversationQuery . and ( [ self , query] )
84103 }
85104
86- // MARK: Where Condition
105+ /// Performs a logical OR operation on an array of one or more expressions of query.
106+ /// - Parameter queries: An array of one or more expressions of query.
107+ /// - Throws: `LCError`
108+ /// - Returns: An optional `IMConversationQuery`
109+ public static func or( _ queries: [ IMConversationQuery ] ) throws -> IMConversationQuery ? {
110+ return try self . combine ( queries: queries, operation: " $or " )
111+ }
112+
113+ /// Performs a logical OR operation on self and the query.
114+ /// - Parameter query: The query.
115+ /// - Throws: `LCError`
116+ /// - Returns: An optional `IMConversationQuery`
117+ public func or( _ query: IMConversationQuery ) throws -> IMConversationQuery ? {
118+ return try IMConversationQuery . or ( [ self , query] )
119+ }
120+
121+ // MARK: Where
87122
88- /// Add constraint in query.
89- ///
90- /// - Parameters:
91- /// - key: The key.
92- /// - constraint: The constraint.
93123 public override func `where`( _ key: String , _ constraint: Constraint ) throws {
94- let typeChecker : ( LCQuery ) throws -> Void = { query in
95- guard let _ = query as? IMConversationQuery else {
96- throw LCError ( code: . inconsistency, reason: " \( type ( of: query) ) not support " )
97- }
98- }
99124 switch constraint {
100125 case . included, . selected:
101- throw LCError ( code: . inconsistency, reason: " \( constraint) not support " )
102- /* Query matching. */
103- case let . matchedQuery( query) :
104- try typeChecker ( query)
105- case let . notMatchedQuery( query) :
106- try typeChecker ( query)
107- case let . matchedQueryAndKey( query, _) :
108- try typeChecker ( query)
109- case let . notMatchedQueryAndKey( query, _) :
110- try typeChecker ( query)
126+ throw LCError (
127+ code: . inconsistency,
128+ reason: " \( constraint) not support. " )
111129 default :
112130 break
113131 }
@@ -310,6 +328,16 @@ public class IMConversationQuery: LCQuery {
310328 fatalError ( " not support " )
311329 }
312330
331+ @available ( * , unavailable)
332+ public override class func and( _ queries: [ LCQuery ] ) throws -> LCQuery {
333+ fatalError ( " not support " )
334+ }
335+
336+ @available ( * , unavailable)
337+ public override class func or( _ queries: [ LCQuery ] ) throws -> LCQuery {
338+ fatalError ( " not support " )
339+ }
340+
313341 @available ( * , unavailable)
314342 public override func and( _ query: LCQuery ) throws -> LCQuery {
315343 fatalError ( " not support " )
0 commit comments