Repository navigation
Expand file tree
/
Copy pathSQLPsqlScript.swift
More file actions
272 lines (238 loc) · 10.4 KB
/
Copy pathSQLPsqlScript.swift
File metadata and controls
272 lines (238 loc) · 10.4 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import Foundation
/// The lines of a psql script that are not SQL, read the way psql 17 and 18 read them.
///
/// `COPY ... FROM STDIN` takes its rows from the script itself: the lines after the line its `;` ends on, up to a line
/// holding only `\.`, or to the end of the script. psql skips them even when the `COPY` fails, since a row run as SQL
/// can be any statement. pg_dump writes a script that opens with `\restrict <key>` and closes with `\unrestrict <key>`,
/// which only limit psql's own meta-commands; nothing here runs one, so both lines run nothing.
public enum SQLPsqlScript {
private static let backslash = SqlLexer.backslash
private static let period = UInt16(UnicodeScalar(".").value)
private static let restrict = Array("restrict".utf16)
private static let unrestrict = Array("unrestrict".utf16)
/// Everything after the `;` at `semicolon` that belongs to a `COPY ... FROM STDIN`: the rest of the `;`'s line, then
/// each line up to and including the one holding only `\.`, its line break excluded, or up to the end of the text.
///
/// psql runs code after the `;` on its own line once the rows are in. pg_dump never writes any, and here it never
/// runs, so a line break after it cannot decide whether it does.
public static func copyRows(afterSemicolonAt semicolon: Int, in text: NSString, length: Int) -> NSRange {
guard var lineStart = lineEnd(in: text, from: semicolon + 1, length: length).map({ $0 + 1 }) else {
return NSRange(location: semicolon + 1, length: length - semicolon - 1)
}
while lineStart < length {
if let markerEnd = endOfDataMarker(in: text, at: lineStart, length: length) {
return NSRange(location: semicolon + 1, length: markerEnd - semicolon - 1)
}
guard let next = lineEnd(in: text, from: lineStart, length: length) else { break }
lineStart = next + 1
}
return NSRange(location: semicolon + 1, length: length - semicolon - 1)
}
/// The rows of the `COPY` block `rows` spans, as ``copyRows(afterSemicolonAt:in:length:)`` found it: the lines
/// after the `;`'s line, each with its line break, up to the `\.` line or the end of the text. The `\.` line is
/// never part of them: psql 18 does not send it, and PostgreSQL 18 reads it as data in CSV.
public static func copyData(inRows rows: NSRange, of text: NSString) -> NSRange {
let end = NSMaxRange(rows)
guard let firstBreak = lineEnd(in: text, from: rows.location, length: end) else {
return NSRange(location: end, length: 0)
}
let start = firstBreak + 1
var lastLineStart = start
if start < end {
let found = text.range(
of: "\n",
options: [.literal, .backwards],
range: NSRange(location: start, length: end - start)
)
if found.location != NSNotFound { lastLineStart = found.location + 1 }
}
let dataEnd = endOfDataMarker(in: text, at: lastLineStart, length: text.length) == end ? lastLineStart : end
return NSRange(location: start, length: max(0, dataEnd - start))
}
/// Where the `\.` line that starts at `lineStart` ends, its line break excluded, or nil when the line is not one.
/// psql 18 matches `\.` and a LF or CRLF at a line's start only; a last line `\.` with no line break ends the rows
/// too. The unit after the marker is read to decide, so `text` has to hold it unless the marker ends the text.
public static func endOfDataMarker(in text: NSString, at lineStart: Int, length: Int) -> Int? {
guard lineStart + 1 < length,
text.character(at: lineStart) == backslash,
text.character(at: lineStart + 1) == period
else {
return nil
}
let after = lineStart + 2
guard after < length else { return after }
switch text.character(at: after) {
case SqlLexer.newline:
return after
case SqlLexer.carriageReturn where after + 1 == length || text.character(at: after + 1) == SqlLexer.newline:
return after
default:
return nil
}
}
/// How many units ``endOfDataMarker(in:at:length:)`` reads from a line's start: `\`, `.`, a carriage return and
/// the line feed after it.
public static let endOfDataMarkerLookahead = 4
/// The `\restrict` or `\unrestrict` line whose backslash is at `offset`, its line break excluded, when only blanks
/// stand before it on its line. psql reads both names case for case.
public static func restrictLine(at offset: Int, in text: NSString, length: Int) -> NSRange? {
var before = offset - 1
while before >= 0, isLineBlank(text.character(at: before)) {
before -= 1
}
guard before < 0 || text.character(at: before) == SqlLexer.newline,
let nameEnd = restrictNameEnd(in: text, at: offset, length: length)
else {
return nil
}
let end = lineEnd(in: text, from: nameEnd, length: length) ?? length
return NSRange(location: offset, length: end - offset)
}
/// Whether a `\restrict` or `\unrestrict` line starts at `offset`, for a reader that already knows only blanks
/// stand before it on its line. `text` has to hold the unit after the name unless the name ends the text.
public static func startsRestrictLine(in text: NSString, at offset: Int, length: Int) -> Bool {
restrictNameEnd(in: text, at: offset, length: length) != nil
}
/// How many units a reader needs from a backslash on to answer ``startsRestrictLine(in:at:length:)``.
public static let restrictLineLookahead = 1 + 10 + 1
private static func restrictNameEnd(in text: NSString, at offset: Int, length: Int) -> Int? {
guard offset < length, text.character(at: offset) == backslash,
let nameEnd = [restrict, unrestrict].lazy.compactMap({ name in
matches(name, in: text, at: offset + 1, length: length) ? offset + 1 + name.count : nil
}).first
else {
return nil
}
if nameEnd < length {
let next = text.character(at: nameEnd)
guard isLineBlank(next) || next == SqlLexer.newline || next == SqlLexer.carriageReturn else { return nil }
}
return nameEnd
}
private static func lineEnd(in text: NSString, from offset: Int, length: Int) -> Int? {
guard offset < length else { return nil }
let found = text.range(
of: "\n",
options: .literal,
range: NSRange(location: offset, length: length - offset)
)
return found.location == NSNotFound ? nil : found.location
}
private static func matches(_ name: [UInt16], in text: NSString, at offset: Int, length: Int) -> Bool {
guard offset + name.count <= length else { return false }
for (index, unit) in name.enumerated() where text.character(at: offset + index) != unit {
return false
}
return true
}
private static func isLineBlank(_ unit: UInt16) -> Bool {
unit == SqlLexer.space || unit == SqlLexer.tab
}
}
/// psql's test for a statement that takes rows from the script: its first word is `COPY`, and among its first eight
/// words outside parentheses a `FROM` comes right before `STDIN` or `STDOUT`, which the server's grammar does not tell
/// apart. A quoted name is no word, and a dotted name is one word per part.
struct SQLCopyFromStandardInput {
private static let wordsRead = 8
private var words = 0
private var parenDepth = 0
private var followsFrom = false
private var settled = false
private(set) var readsRows = false
var needsWords: Bool {
!settled
}
mutating func observeWord(_ word: String) {
guard !settled, parenDepth == 0 else { return }
if words == 0, word != "COPY" {
settled = true
return
}
if followsFrom, word == "STDIN" || word == "STDOUT" {
readsRows = true
settled = true
return
}
followsFrom = word == "FROM"
words += 1
if words == Self.wordsRead {
settled = true
}
}
mutating func observeSymbol(_ symbol: UInt16) {
guard !settled else { return }
switch symbol {
case SqlLexer.openParen:
parenDepth += 1
case SqlLexer.closeParen:
parenDepth = max(0, parenDepth - 1)
default:
break
}
if words == 0 {
settled = true
}
}
mutating func observeOpaqueToken() {
if words == 0 {
settled = true
}
}
}
/// A statement tracker that also tells a `COPY ... FROM STDIN`, for a grammar with ``SQLLexicalGrammar/psqlScripts``.
struct SQLPsqlStatementTracker: SQLStatementBoundaryTracking {
private var base: any SQLStatementBoundaryTracking
private var copy = SQLCopyFromStandardInput()
init(base: any SQLStatementBoundaryTracking) {
self.base = base
}
var needsWords: Bool {
base.needsWords || copy.needsWords
}
var terminator: SQLStatementTerminator {
base.terminator
}
var acceptsBindParameters: Bool {
base.acceptsBindParameters
}
var holdsOpenBlock: Bool {
base.holdsOpenBlock
}
var awaitsContinuation: Bool {
base.awaitsContinuation
}
var copiesFromStandardInput: Bool {
copy.readsRows
}
mutating func observeWord(_ word: String) {
copy.observeWord(word)
if base.needsWords {
base.observeWord(word)
}
}
mutating func observeSymbol(_ symbol: UInt16) {
copy.observeSymbol(symbol)
base.observeSymbol(symbol)
}
mutating func observeOpaqueToken() {
copy.observeOpaqueToken()
base.observeOpaqueToken()
}
mutating func observeQuotedToken(opener: UInt16) {
copy.observeOpaqueToken()
base.observeQuotedToken(opener: opener)
}
mutating func observeGap() {
base.observeGap()
}
mutating func observeSemicolon() -> Bool {
base.observeSemicolon()
}
mutating func continuesStatement(with word: String?) -> Bool {
base.continuesStatement(with: word)
}
mutating func reset() {
base.reset()
copy = SQLCopyFromStandardInput()
}
}