Repository navigation
Expand file tree
/
Copy pathSQLStatementScanner.swift
More file actions
498 lines (453 loc) · 22.5 KB
/
Copy pathSQLStatementScanner.swift
File metadata and controls
498 lines (453 loc) · 22.5 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
import Foundation
public enum SQLStatementScanner {
public struct LocatedStatement: Sendable {
public let sql: String
public let offset: Int
public let hasContent: Bool
public let terminator: SQLStatementTerminator
public let acceptsBindParameters: Bool
private let isAllContent: Bool
public init(
sql: String,
offset: Int,
hasContent: Bool = true,
terminator: SQLStatementTerminator = .separator,
acceptsBindParameters: Bool = true
) {
self.sql = sql
self.offset = offset
self.hasContent = hasContent
self.terminator = terminator
self.acceptsBindParameters = acceptsBindParameters
isAllContent = false
}
// A Redis command starts and ends on an argument byte, which can be one StatementBlank trims.
public init(wholeText sql: String, offset: Int) {
self.sql = sql
self.offset = offset
hasContent = true
terminator = .separator
acceptsBindParameters = true
isAllContent = true
}
/// The statement's whole span in the document, in UTF-16 units.
public var range: NSRange {
NSRange(location: offset, length: (sql as NSString).length)
}
/// The span of the statement's own text, with the inherited whitespace trimmed off both ends.
///
/// `offset` is the index just past the previous semicolon, so in a script written one statement per line it
/// lands on the newline that ended the previous line. A decoration or a gutter anchor placed from ``range``
/// therefore starts a line early, and uses this instead.
public var contentRange: NSRange {
guard !isAllContent else { return range }
let content = StatementBlank.contentRange(of: sql)
return NSRange(location: offset + content.location, length: content.length)
}
}
/// One statement as the driver will receive it, with the span of the text it was taken from.
///
/// `sql` is the trimmed form, with the terminating `;` stripped unless it belongs to the statement, as it does
/// after a PL/SQL unit's `END` and after a T-SQL `MERGE`; `range` covers exactly those characters, so a caller that
/// keeps the range can find its way back to the statement it ran.
///
/// The range is relative to the text the scan was given. A run started from a selection or from a single
/// statement scans a fragment, so those callers shift the range onto the tab's whole query with ``offset(by:)``
/// before it travels any further. Everything downstream may then assume tab coordinates.
public struct ExecutableStatement: Sendable {
public let sql: String
public let range: NSRange
/// False for a definition, whose `:name` is never a bind parameter; see
/// ``SQLStatementBoundaryTracking/acceptsBindParameters``.
public let acceptsBindParameters: Bool
public init(sql: String, range: NSRange, acceptsBindParameters: Bool = true) {
self.sql = sql
self.range = range
self.acceptsBindParameters = acceptsBindParameters
}
public func offset(by delta: Int) -> ExecutableStatement {
guard delta != 0 else { return self }
return ExecutableStatement(
sql: sql,
range: NSRange(location: range.location + delta, length: range.length),
acceptsBindParameters: acceptsBindParameters
)
}
}
/// Every statement in the document, with its span, in document order.
///
/// Unlike ``allStatements(in:grammar:)`` this keeps the empty and comment-only segments, flagged by
/// ``LocatedStatement/hasContent``, because a caller drawing per-statement decorations has to be able to tell a
/// segment that carries nothing from one that was never scanned.
public static func locatedStatements(in sql: String, grammar: SQLLexicalGrammar) -> [LocatedStatement] {
var results: [LocatedStatement] = []
scan(sql: sql, cursorPosition: nil, grammar: grammar) { statement in
results.append(statement)
return true
}
return results
}
/// The statements a reader can act on, in document order.
///
/// Everything the editor offers per statement is drawn from this one filter: the gutter's run controls, the
/// caret-statement band and the navigation commands. A segment that carries nothing, meaning a comment or trailing
/// whitespace, is not somewhere a caret should be sent and not something worth offering to run, so it is dropped
/// here rather than at each call site where the three could drift apart.
public static func navigableStatements(in sql: String, grammar: SQLLexicalGrammar) -> [LocatedStatement] {
locatedStatements(in: sql, grammar: grammar)
.filter { $0.hasContent && $0.contentRange.length > 0 }
}
/// Where the caret goes when the reader asks for the statement after the one it is in.
///
/// Returns `nil` at the end of the document rather than wrapping. Wrapping a caret to the other end of a script is
/// a jump the reader did not ask for and cannot take back with the opposite key.
///
/// A caret sitting in the trivia between two statements belongs to neither, so this answers with the next
/// statement that starts after it.
public static func statementStart(
after offset: Int,
in sql: String,
grammar: SQLLexicalGrammar
) -> Int? {
var found: Int?
scan(sql: sql, cursorPosition: nil, grammar: grammar) { statement in
guard statement.hasContent, statement.contentRange.length > 0 else { return true }
guard statement.contentRange.location > offset else { return true }
found = statement.contentRange.location
return false
}
return found
}
/// How far `Option+Shift+Down` reaches.
///
/// Selection wants the far edge of the text, not the start of the next statement, or the last statement's own body
/// could never be selected: past its start there is no next statement to reach for.
public static func statementSelectionEnd(
after offset: Int,
in sql: String,
grammar: SQLLexicalGrammar
) -> Int? {
if let next = statementStart(after: offset, in: sql, grammar: grammar) {
return next
}
let end = navigableStatements(in: sql, grammar: grammar).last?.contentRange.upperBound
return end.flatMap { $0 > offset ? $0 : nil }
}
/// Where the caret goes when the reader asks for the statement before the one it is in.
///
/// A caret already past the start of its own statement goes to that statement's start first, which is how a
/// reader steps back through a script without overshooting the statement they were reading.
public static func statementStart(
before offset: Int,
in sql: String,
grammar: SQLLexicalGrammar
) -> Int? {
var found: Int?
scan(sql: sql, cursorPosition: nil, grammar: grammar) { statement in
guard statement.hasContent, statement.contentRange.length > 0 else { return true }
guard statement.contentRange.location < offset else { return false }
found = statement.contentRange.location
return true
}
return found
}
/// Every `GO` line in the document, in document order, each ending the batch before it.
///
/// The scan that finds them is the one that divides statements, so a separator can never fall inside a statement
/// and no statement can run across one. Empty for an engine whose scripts have no batches.
public static func batchSeparators(in sql: String, grammar: SQLLexicalGrammar) -> [SQLBatchSeparator] {
guard grammar.contains(.batchSeparatorLines) else { return [] }
var separators: [SQLBatchSeparator] = []
scan(sql: sql, cursorPosition: nil, grammar: grammar, onBatchSeparator: { separators.append($0) }) { _ in
true
}
return separators
}
/// Returns statements as the driver receives them, for driver execution.
public static func allStatements(in sql: String, grammar: SQLLexicalGrammar) -> [String] {
executableStatements(in: sql, grammar: grammar).map(\.sql)
}
/// The same statements ``allStatements(in:grammar:)`` returns, each with its span in the document.
///
/// One enumeration produces both, because the alternative is two filters that have to agree and that nothing
/// checks. ``navigableStatements(in:grammar:)`` is deliberately not that second filter: it keeps the terminating
/// semicolon, while execution strips one, so pointing execution at it would change which text reaches the driver.
public static func executableStatements(in sql: String, grammar: SQLLexicalGrammar) -> [ExecutableStatement] {
var results: [ExecutableStatement] = []
scan(sql: sql, cursorPosition: nil, grammar: grammar) { located in
guard located.hasContent, let statement = executableStatement(from: located) else { return true }
results.append(statement)
return true
}
return results
}
/// `text` as a driver receives it when it is sent whole: starting where its first statement starts and ending where
/// its last statement's executable form ends, so a separator on either side comes off and a terminator that belongs
/// to the statement stays. A `GO` line ahead of the first statement is the client's word: SQL Server reads one it
/// receives as a call to a procedure named `GO`. Empty when nothing but separators, comments or blanks is left.
public static func executableText(of text: String, grammar: SQLLexicalGrammar) -> String {
let trimmed = StatementBlank.trimming(text)
let statements = executableStatements(in: trimmed, grammar: grammar)
guard let first = statements.first, let last = statements.last else { return "" }
let span = NSRange(location: first.range.location, length: last.range.upperBound - first.range.location)
return (trimmed as NSString).substring(with: span)
}
/// The text the driver receives for `located`, or nil when nothing but a separator is left.
///
/// A `;` that belongs to the statement stays, but only when there is something before it: a unit reduced to its
/// terminator is as empty as any other.
public static func executableStatement(from located: LocatedStatement) -> ExecutableStatement? {
let rawSQL = located.sql
var content = StatementBlank.trimming(rawSQL[...])
if content.last == ";" {
let body = StatementBlank.trimming(content.dropLast())
guard !body.isEmpty else { return nil }
if located.terminator == .separator {
content = body
}
}
guard !content.isEmpty else { return nil }
let range = NSRange(content.startIndex..<content.endIndex, in: rawSQL)
return ExecutableStatement(
sql: String(content),
range: NSRange(location: located.offset + range.location, length: range.length),
acceptsBindParameters: located.acceptsBindParameters
)
}
/// Whether the text ends inside a block it opened, so the last statement swallowed everything after its opener.
/// An engine that reads that opener as a name runs the rest as statements of their own.
public static func endsInsideOpenBlock(_ sql: String, grammar: SQLLexicalGrammar) -> Bool {
var holdsOpenBlock = false
scan(sql: sql, cursorPosition: nil, grammar: grammar, onFinish: { holdsOpenBlock = $0 }) { _ in true }
return holdsOpenBlock
}
/// Returns statements preserving trailing semicolons, for display/history/favorites.
public static func allStatementsPreservingSemicolons(in sql: String, grammar: SQLLexicalGrammar) -> [String] {
var results: [String] = []
scan(sql: sql, cursorPosition: nil, grammar: grammar) { statement in
guard statement.hasContent else { return true }
let trimmed = StatementBlank.trimming(statement.sql)
let withoutSemicolon = trimmed.hasSuffix(";")
? StatementBlank.trimming(String(trimmed.dropLast()))
: trimmed
if !withoutSemicolon.isEmpty {
results.append(trimmed)
}
return true
}
return results
}
public static func statementAtCursor(in sql: String, cursorPosition: Int, grammar: SQLLexicalGrammar) -> String {
let located = locatedStatementAtCursor(in: sql, cursorPosition: cursorPosition, grammar: grammar)
return executableStatement(from: located)?.sql ?? ""
}
public static func locatedStatementAtCursor(in sql: String, cursorPosition: Int, grammar: SQLLexicalGrammar) -> LocatedStatement {
var result = LocatedStatement(sql: "", offset: 0, hasContent: false)
scan(sql: sql, cursorPosition: cursorPosition, grammar: grammar) { statement in
result = statement
return false
}
return result
}
// MARK: - Resuming
// The span of a statement that had content, kept without its text so a checkpoint stays small.
struct EndedStatement: Sendable, Equatable {
let range: NSRange
let terminator: SQLStatementTerminator
let acceptsBindParameters: Bool
func located(in text: NSString) -> LocatedStatement {
LocatedStatement(
sql: text.substring(with: range),
offset: range.location,
hasContent: true,
terminator: terminator,
acceptsBindParameters: acceptsBindParameters
)
}
}
// A point where the scan holds no state but the last statement with content, so it can start again there.
struct Checkpoint: Sendable, Equatable {
let offset: Int
// The last offset whose text decided this boundary: an edit there or earlier moves it.
let decidedThrough: Int
let endedStatement: EndedStatement?
}
// MARK: - Scanning
// Segments tile the document: each runs from the end of the previous one to its terminator, whitespace included.
// With a cursor only the segment holding it is reported, or the last one when it sits past every terminator.
// Where a statement ends is the tracker's decision, except at a `GO` line, which ends the batch whatever the
// tracker thinks and is a segment of its own that runs nothing.
static func scan(
sql: String,
cursorPosition: Int?,
grammar: SQLLexicalGrammar,
resumingAt resume: Checkpoint? = nil,
onCheckpoint: (Checkpoint) -> Void = { _ in },
onBatchSeparator: (SQLBatchSeparator) -> Void = { _ in },
onFinish: (_ holdsOpenBlock: Bool) -> Void = { _ in },
onStatement: (LocatedStatement) -> Bool
) {
let nsQuery = sql as NSString
let length = nsQuery.length
guard length > 0 else { return }
let safePosition = cursorPosition.map { min(max(0, $0), length) }
var tracker = SQLStatementBoundaries.makeTracker(for: grammar)
var currentStart = min(max(0, resume?.offset ?? 0), length)
var hasStatementContent = false
var i = currentStart
var lastEndedStatement = resume?.endedStatement
// A caret on a SQL*Plus `/` line stands for the statement the slash ends, where `Cmd+Enter` is expected to act.
// A caret on a `GO` line stands for the last statement of its batch, and for nothing when the batch is empty.
func finishSegment(at end: Int, hasContent: Bool, standsForEndedStatement: Bool = false) -> Bool {
let range = NSRange(location: currentStart, length: end - currentStart)
if hasContent {
lastEndedStatement = EndedStatement(
range: range,
terminator: tracker.terminator,
acceptsBindParameters: tracker.acceptsBindParameters
)
}
if let cursor = safePosition {
guard cursor >= currentStart, cursor <= end else {
currentStart = end
return true
}
if standsForEndedStatement, let ended = lastEndedStatement {
_ = onStatement(ended.located(in: nsQuery))
return false
}
}
let statement = LocatedStatement(
sql: nsQuery.substring(with: range),
offset: currentStart,
hasContent: hasContent,
terminator: tracker.terminator,
acceptsBindParameters: tracker.acceptsBindParameters
)
if safePosition != nil {
_ = onStatement(statement)
return false
}
currentStart = end
return onStatement(statement)
}
while i < length {
let ch = nsQuery.character(at: i)
if let span = SQLNonCodeSpan.span(at: i, in: nsQuery, grammar: grammar) {
switch span.kind {
case .lineComment, .blockComment:
tracker.observeGap()
case .executableComment:
hasStatementContent = true
case .quoted:
hasStatementContent = true
tracker.observeQuotedToken(opener: nsQuery.character(at: span.start))
case .parameter:
hasStatementContent = true
tracker.observeOpaqueToken()
}
i = max(span.end, i + 1)
continue
}
if grammar.contains(.batchSeparatorLines),
let separator = SQLBatchSeparator.line(at: i, in: nsQuery, length: length, grammar: grammar) {
onBatchSeparator(separator)
if hasStatementContent {
guard finishSegment(at: i, hasContent: true) else { return }
}
let separatorEnd = NSMaxRange(separator.range)
guard finishSegment(at: separatorEnd, hasContent: false, standsForEndedStatement: true) else { return }
lastEndedStatement = nil
tracker.reset()
hasStatementContent = false
onCheckpoint(Checkpoint(offset: separatorEnd, decidedThrough: separatorEnd, endedStatement: nil))
i = separatorEnd
continue
}
if SqlBlockStructure.startsWord(nsQuery, at: i, length: length, grammar: grammar) {
hasStatementContent = true
if tracker.needsWords {
let word = SqlBlockStructure.readKeyword(nsQuery, at: i, length: length, grammar: grammar)
tracker.observeWord(word.text)
i = word.end
} else {
i += 1
while i < length, SqlBlockStructure.continuesWord(nsQuery.character(at: i), grammar: grammar) {
i += 1
}
}
continue
}
if ch == SqlLexer.semicolon {
guard tracker.observeSemicolon() else {
hasStatementContent = true
i += 1
continue
}
guard finishSegment(at: i + 1, hasContent: hasStatementContent) else { return }
tracker.reset()
hasStatementContent = false
onCheckpoint(Checkpoint(offset: i + 1, decidedThrough: i, endedStatement: lastEndedStatement))
i += 1
continue
}
if grammar.contains(.slashLineTerminators), ch == SqlLexer.slash, isSlashLine(nsQuery, at: i, length: length) {
if hasStatementContent {
guard finishSegment(at: i, hasContent: true) else { return }
}
guard finishSegment(at: i + 1, hasContent: false, standsForEndedStatement: true) else { return }
tracker.reset()
hasStatementContent = false
onCheckpoint(Checkpoint(
offset: i + 1,
decidedThrough: endOfSlashLine(nsQuery, at: i, length: length),
endedStatement: lastEndedStatement
))
i += 1
continue
}
let blankLength = StatementBlank.blankLength(in: nsQuery, at: i)
if blankLength > 0 {
tracker.observeGap()
i += blankLength
continue
}
hasStatementContent = true
tracker.observeSymbol(ch)
i += 1
}
if currentStart < length {
let statement = LocatedStatement(
sql: nsQuery.substring(with: NSRange(location: currentStart, length: length - currentStart)),
offset: currentStart,
hasContent: hasStatementContent,
terminator: tracker.terminator,
acceptsBindParameters: tracker.acceptsBindParameters
)
_ = onStatement(statement)
}
onFinish(tracker.holdsOpenBlock)
}
/// Whether the `/` at `offset` stands alone on its line, which is what makes it SQL*Plus's terminator rather than
/// a division.
public static func isSlashLine(_ text: NSString, at offset: Int, length: Int) -> Bool {
var before = offset - 1
while before >= 0, isLineBlank(text.character(at: before)) {
before -= 1
}
guard before < 0 || text.character(at: before) == SqlLexer.newline else { return false }
let after = endOfSlashLine(text, at: offset, length: length)
return after == length || text.character(at: after) == SqlLexer.newline
}
// The last offset `isSlashLine` reads after the slash: its line break, or the end of the text.
private static func endOfSlashLine(_ text: NSString, at offset: Int, length: Int) -> Int {
var after = offset + 1
while after < length, isLineBlank(text.character(at: after)) {
after += 1
}
return after
}
private static func isLineBlank(_ character: UInt16) -> Bool {
character == SqlLexer.space || character == SqlLexer.tab || character == SqlLexer.carriageReturn
}
}