Skip to content

Commit b2034bd

Browse files
committed
Convert PostgreSQLBinaryOperator to struct
Structs are easily extendable if the user needs operators that aren't there already. They also do not risk breaking source compatibilty when adding new cases in the future.
1 parent 1f635d3 commit b2034bd

File tree

1 file changed

+108
-191
lines changed

1 file changed

+108
-191
lines changed
+108-191
Original file line numberDiff line numberDiff line change
@@ -1,195 +1,112 @@
11
/// PostgreSQL specific `SQLBinaryOperator`.
2-
public enum PostgreSQLBinaryOperator: SQLBinaryOperator, Equatable {
3-
/// See `SQLBinaryOperator`.
4-
public static var equal: PostgreSQLBinaryOperator { return ._equal }
5-
6-
/// See `SQLBinaryOperator`.
7-
public static var notEqual: PostgreSQLBinaryOperator { return ._notEqual }
8-
9-
/// See `SQLBinaryOperator`.
10-
public static var greaterThan: PostgreSQLBinaryOperator { return ._greaterThan }
11-
12-
/// See `SQLBinaryOperator`.
13-
public static var lessThan: PostgreSQLBinaryOperator { return ._lessThan }
14-
15-
/// See `SQLBinaryOperator`.
16-
public static var greaterThanOrEqual: PostgreSQLBinaryOperator { return ._greaterThanOrEqual }
17-
18-
/// See `SQLBinaryOperator`.
19-
public static var lessThanOrEqual: PostgreSQLBinaryOperator { return ._lessThanOrEqual }
20-
21-
/// See `SQLBinaryOperator`.
22-
public static var like: PostgreSQLBinaryOperator { return ._like }
23-
24-
/// See `SQLBinaryOperator`.
25-
public static var notLike: PostgreSQLBinaryOperator { return ._notLike }
26-
27-
/// See `SQLBinaryOperator`.
28-
public static var `in`: PostgreSQLBinaryOperator { return ._in }
29-
30-
/// See `SQLBinaryOperator`.
31-
public static var `notIn`: PostgreSQLBinaryOperator { return ._notIn }
32-
33-
/// See `SQLBinaryOperator`.
34-
public static var and: PostgreSQLBinaryOperator { return ._and }
35-
36-
/// See `SQLBinaryOperator`.
37-
public static var or: PostgreSQLBinaryOperator { return ._or }
38-
39-
/// See `SQLBinaryOperator`.
40-
public static var concatenate: PostgreSQLBinaryOperator { return ._concatenate }
41-
42-
/// See `SQLBinaryOperator`.
43-
public static var multiply: PostgreSQLBinaryOperator { return ._multiply }
44-
45-
/// See `SQLBinaryOperator`.
46-
public static var divide: PostgreSQLBinaryOperator { return ._divide }
47-
48-
/// See `SQLBinaryOperator`.
49-
public static var modulo: PostgreSQLBinaryOperator { return ._modulo }
50-
51-
/// See `SQLBinaryOperator`.
52-
public static var add: PostgreSQLBinaryOperator { return ._add }
53-
54-
/// See `SQLBinaryOperator`.
55-
public static var subtract: PostgreSQLBinaryOperator { return ._subtract }
56-
57-
58-
/// `||`
59-
case _concatenate
60-
61-
/// `*`
62-
case _multiply
63-
64-
/// `/`
65-
case _divide
66-
67-
/// `%`
68-
case _modulo
69-
70-
/// `+`
71-
case _add
72-
73-
/// `-`
74-
case _subtract
75-
76-
/// `<<`
77-
case _bitwiseShiftLeft
78-
79-
/// `>>`
80-
case _bitwiseShiftRight
81-
82-
/// `&`
83-
case _bitwiseAnd
84-
85-
/// `|`
86-
case _bitwiseOr
87-
88-
/// `<`
89-
case _lessThan
90-
91-
/// `<=`
92-
case _lessThanOrEqual
93-
94-
/// `>`
95-
case _greaterThan
96-
97-
/// `>=`
98-
case _greaterThanOrEqual
99-
100-
/// `=` or `==`
101-
case _equal
102-
103-
/// `!=` or `<>`
104-
case _notEqual
105-
106-
/// `AND`
107-
case _and
108-
109-
/// `OR`
110-
case _or
111-
112-
/// `IS`
113-
case _is
114-
115-
/// `IS NOT`
116-
case _isNot
117-
118-
/// `IN`
119-
case _in
120-
121-
/// `NOT IN`
122-
case _notIn
123-
124-
/// `LIKE`
125-
case _like
126-
127-
/// `NOT LIKE`
128-
case _notLike
129-
130-
/// `GLOB`
131-
case _glob
132-
133-
/// `NOT GLOB`
134-
case _notGlob
135-
136-
/// `MATCH`
137-
case _match
138-
139-
/// `NOT MATCH`
140-
case _notMatch
141-
142-
/// `REGEXP`
143-
case _regexp
144-
145-
/// `NOT REGEXP`
146-
case _notRegexp
147-
148-
/// `ILIKE`
149-
case ilike
150-
151-
/// `NOT ILIKE`
152-
case notILike
153-
154-
/// Custom operator
155-
case custom(String)
156-
157-
/// See `SQLSerializable`.
2+
public struct PostgreSQLBinaryOperator: SQLBinaryOperator, Equatable, ExpressibleByStringLiteral {
3+
/// See `SQLBinaryOperator`.
4+
public static let add = PostgreSQLBinaryOperator("+")
5+
6+
/// See `SQLBinaryOperator`.
7+
public static let bitwiseAnd = PostgreSQLBinaryOperator("&")
8+
9+
/// See `SQLBinaryOperator`.
10+
public static let bitwiseOr = PostgreSQLBinaryOperator("|")
11+
12+
/// See `SQLBinaryOperator`.
13+
public static let bitwiseShiftLeft = PostgreSQLBinaryOperator("<<")
14+
15+
/// See `SQLBinaryOperator`.
16+
public static let bitwiseShiftRight = PostgreSQLBinaryOperator(">>")
17+
18+
/// See `SQLBinaryOperator`.
19+
public static let concatenate = PostgreSQLBinaryOperator("||")
20+
21+
/// See `SQLBinaryOperator`.
22+
public static let divide = PostgreSQLBinaryOperator("/")
23+
24+
/// See `SQLBinaryOperator`.
25+
public static let equal = PostgreSQLBinaryOperator("=")
26+
27+
/// See `SQLBinaryOperator`.
28+
public static let greaterThan = PostgreSQLBinaryOperator(">")
29+
30+
/// See `SQLBinaryOperator`.
31+
public static let greaterThanOrEqual = PostgreSQLBinaryOperator(">=")
32+
33+
/// See `SQLBinaryOperator`.
34+
public static let lessThan = PostgreSQLBinaryOperator("<")
35+
36+
/// See `SQLBinaryOperator`.
37+
public static let lessThanOrEqual = PostgreSQLBinaryOperator("<=")
38+
39+
/// See `SQLBinaryOperator`.
40+
public static let modulo = PostgreSQLBinaryOperator("%")
41+
42+
/// See `SQLBinaryOperator`.
43+
public static let multiply = PostgreSQLBinaryOperator("*")
44+
45+
/// See `SQLBinaryOperator`.
46+
public static let notEqual = PostgreSQLBinaryOperator("!=")
47+
48+
/// See `SQLBinaryOperator`.
49+
public static let subtract = PostgreSQLBinaryOperator("-")
50+
51+
/// See `SQLBinaryOperator`.
52+
public static let and = PostgreSQLBinaryOperator("AND")
53+
54+
/// See `SQLBinaryOperator`.
55+
public static let or = PostgreSQLBinaryOperator("OR")
56+
57+
/// See `SQLBinaryOperator`.
58+
public static let `in` = PostgreSQLBinaryOperator("IN")
59+
60+
/// See `SQLBinaryOperator`.
61+
public static let notIn = PostgreSQLBinaryOperator("NOT IN")
62+
63+
/// See `SQLBinaryOperator`.
64+
public static let `is` = PostgreSQLBinaryOperator("IS")
65+
66+
/// See `SQLBinaryOperator`.
67+
public static let isNot = PostgreSQLBinaryOperator("IS NOT")
68+
69+
/// See `SQLBinaryOperator`.
70+
public static let like = PostgreSQLBinaryOperator("LIKE")
71+
72+
/// See `SQLBinaryOperator`.
73+
public static let glob = PostgreSQLBinaryOperator("GLOB")
74+
75+
/// See `SQLBinaryOperator`.
76+
public static let match = PostgreSQLBinaryOperator("MATCH")
77+
78+
/// See `SQLBinaryOperator`.
79+
public static let regexp = PostgreSQLBinaryOperator("~")
80+
81+
/// See `SQLBinaryOperator`.
82+
public static let notLike = PostgreSQLBinaryOperator("NOT LIKE")
83+
84+
/// See `SQLBinaryOperator`.
85+
public static let notGlob = PostgreSQLBinaryOperator("NOT GLOB")
86+
87+
/// See `SQLBinaryOperator`.
88+
public static let notMatch = PostgreSQLBinaryOperator("NOT MATCH")
89+
90+
/// See `SQLBinaryOperator`.
91+
public static let notRegexp = PostgreSQLBinaryOperator("NOT REGEXP")
92+
93+
/// See `SQLBinaryOperator`.
94+
public static let ilike = PostgreSQLBinaryOperator("ILIKE")
95+
96+
/// See `SQLBinaryOperator`.
97+
public static let notILike = PostgreSQLBinaryOperator("NOT ILIKE")
98+
99+
public let op: String
100+
101+
public init(_ op: String) {
102+
self.op = op
103+
}
104+
105+
public init(stringLiteral value: String) {
106+
self.init(value)
107+
}
108+
158109
public func serialize(_ binds: inout [Encodable]) -> String {
159-
switch self {
160-
case ._add: return "+"
161-
case ._bitwiseAnd: return "&"
162-
case ._bitwiseOr: return "|"
163-
case ._bitwiseShiftLeft: return "<<"
164-
case ._bitwiseShiftRight: return ">>"
165-
case ._concatenate: return "||"
166-
case ._divide: return "/"
167-
case ._equal: return "="
168-
case ._greaterThan: return ">"
169-
case ._greaterThanOrEqual: return ">="
170-
case ._lessThan: return "<"
171-
case ._lessThanOrEqual: return "<="
172-
case ._modulo: return "%"
173-
case ._multiply: return "*"
174-
case ._notEqual: return "!="
175-
case ._subtract: return "-"
176-
case ._and: return "AND"
177-
case ._or: return "OR"
178-
case ._in: return "IN"
179-
case ._notIn: return "NOT IN"
180-
case ._is: return "IS"
181-
case ._isNot: return "IS NOT"
182-
case ._like: return "LIKE"
183-
case ._glob: return "GLOB"
184-
case ._match: return "MATCH"
185-
case ._regexp: return "~"
186-
case ._notLike: return "NOT LIKE"
187-
case ._notGlob: return "NOT GLOB"
188-
case ._notMatch: return "NOT MATCH"
189-
case ._notRegexp: return "NOT REGEXP"
190-
case .ilike: return "ILIKE"
191-
case .notILike: return "NOT ILIKE"
192-
case .custom(let op): return op
193-
}
110+
return op
194111
}
195112
}

0 commit comments

Comments
 (0)