-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathPostgreSQLSerializer.swift
202 lines (174 loc) · 5.98 KB
/
PostgreSQLSerializer.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import Fluent
// PostgreSQL flavored SQL serializer.
public final class PostgreSQLSerializer<E: Entity>: GeneralSQLSerializer<E> {
private var positionalIndex = 0
public override func serialize() -> (String, [Node]) {
positionalIndex = 0
return super.serialize()
}
// MARK: Data
public override func insert() -> (String, [Node]) {
var statement: [String] = []
statement += "INSERT INTO"
statement += escape(E.entity)
let bind: [Node]
// Remove ID from the query data to avoid constraint violation error
var data = query.data
if case .some(.some(let id)) = data[.some(E.idKey)], id.isNull {
data.removeValue(forKey: .some(E.idKey))
}
if !data.isEmpty {
statement += keys(data.keys.array)
statement += "VALUES"
let (fragment, nodes) = values(data.values.array)
statement += fragment
bind = nodes
} else {
bind = []
}
statement += "RETURNING"
statement += E.idKey
return (
concatenate(statement),
bind
)
}
// MARK: Schema
public override func drop() -> (String, [Node]) {
var statement: [String] = []
statement += "DROP TABLE IF EXISTS"
statement += escape(E.entity)
statement += "CASCADE" // added for PostgreSQL
return (
concatenate(statement),
[]
)
}
public override func type(_ type: Field.DataType, primaryKey: Bool) -> String {
switch type {
case .id(let type):
let typeString: String
switch type {
case .int:
if primaryKey {
typeString = "SERIAL PRIMARY KEY"
} else {
typeString = "INT4"
}
case .uuid:
if primaryKey {
typeString = "UUID PRIMARY KEY"
} else {
typeString = "UUID"
}
case .custom(let custom):
typeString = custom
}
return typeString
case .int:
return "INT8"
case .string(let length):
if let length = length {
return "VARCHAR(\(length))"
} else {
return "VARCHAR"
}
case .double:
return "FLOAT8"
case .bool:
return "BOOLEAN"
case .bytes:
return "BYTEA"
case .date:
return "TIMESTAMP WITH TIME ZONE"
case .custom(let type):
return type
}
}
// MARK: Query Types
public override func limit(_ limit: RawOr<Limit>) -> String {
var statement: [String] = []
statement += "LIMIT"
switch limit {
case .raw(let raw, _):
statement += raw
case .some(let some):
statement += "\(some.count) OFFSET \(some.offset)"
}
return statement.joined(separator: " ")
}
public override func filter(_ filter: Filter) -> (String, [Node]) {
var statement: [String] = []
var values: [Node] = []
switch filter.method {
case .compare(let key, let c, let value):
// `.null` needs special handling in the case of `.equals` or `.notEquals`.
if c == .equals && value == .null {
statement += escape(filter.entity.entity) + "." + escape(key) + " IS NULL"
}
else if c == .notEquals && value == .null {
statement += escape(filter.entity.entity) + "." + escape(key) + " IS NOT NULL"
}
else {
// Augment pos
self.positionalIndex += 1
statement += escape(filter.entity.entity) + "." + escape(key)
statement += comparison(c)
// Use the positionalIndex instead of "?"
statement += "$\(self.positionalIndex)"
// `.like` comparison operator requires additional
// processing of `value`
switch c {
case .hasPrefix:
values += hasPrefix(value)
case .hasSuffix:
values += hasSuffix(value)
case .contains:
values += contains(value)
default:
values += value
}
}
case .subset(let key, let s, let subValues):
if subValues.count == 0 {
switch s {
case .in:
// where in empty set should
// not match any rows
statement += "false"
case .notIn:
// where not in empty set should
// match all rows
statement += "true"
}
} else {
statement += escape(filter.entity.entity) + "." + escape(key)
statement += scope(s)
statement += placeholders(subValues)
values += subValues
}
case .group(let relation, let f):
if f.count == 0 {
// empty subqueries should result to false to protect
// unfiltered data from being returned
statement += "false"
} else {
let (clause, subvals) = filters(f, relation)
statement += "(\(clause))"
values += subvals
}
}
return (
concatenate(statement),
values
)
}
// MARK: - Convenience
public override func placeholder(_ value: Node) -> String {
positionalIndex += 1
return "$\(positionalIndex)"
}
public override func escape(_ string: String) -> String {
return "\"\(string)\""
}
}