Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ nonisolated public extension LibPQCopyDirection {
var unsupportedMessage: String {
switch self {
case .copyIn:
return String(localized: "The query editor cannot send data to COPY FROM STDIN, so no rows were sent. Use Import to load a file into a table.")
return String(localized: "COPY FROM STDIN takes its rows from the lines after the line its ; ends, so no rows were sent.")
case .copyOut:
return String(localized: "The query editor cannot receive the output of COPY TO STDOUT, so it was discarded. Run a SELECT, or use Export to save the rows to a file.")
case .copyBoth:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,54 @@ public enum SQLPsqlScript {
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? {
Expand Down Expand Up @@ -71,27 +119,6 @@ public enum SQLPsqlScript {
return nameEnd
}

// psql 18 matches `\.` + LF or CRLF at a line's start only; a last line `\.` with no line break ends the rows too.
// The unit after the returned end, when there is one, was read to decide.
private 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
}
}

private static func lineEnd(in text: NSString, from offset: Int, length: Int) -> Int? {
guard offset < length else { return nil }
let found = text.range(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ public enum SQLStatementScanner {
guard !isAllContent else { return range }
let text = sql as NSString
let content = StatementBlank.contentRange(of: text.substring(to: max(0, text.length - delimiterLength)))
return NSRange(location: offset + content.location, length: content.length)
// A COPY's rows run to their end as written: a last row's trailing tab is a column.
let end = copyRows.map { max(NSMaxRange(content), NSMaxRange($0) - offset) } ?? NSMaxRange(content)
return NSRange(location: offset + content.location, length: end - content.location)
}
}

Expand All @@ -103,18 +105,23 @@ public enum SQLStatementScanner {
public let segmentEnd: Int
public let clientDelimiter: SQLClientDelimiter?

// A `COPY ... FROM STDIN`'s rows as psql sends them, empty for a block with none; nil for any other statement.
public let copyData: String?

public init(
sql: String,
range: NSRange,
acceptsBindParameters: Bool = true,
segmentEnd: Int? = nil,
clientDelimiter: SQLClientDelimiter? = nil
clientDelimiter: SQLClientDelimiter? = nil,
copyData: String? = nil
) {
self.sql = sql
self.range = range
self.acceptsBindParameters = acceptsBindParameters
self.segmentEnd = segmentEnd ?? NSMaxRange(range)
self.clientDelimiter = clientDelimiter
self.copyData = copyData
}

public func offset(by delta: Int) -> ExecutableStatement {
Expand All @@ -124,7 +131,8 @@ public enum SQLStatementScanner {
range: NSRange(location: range.location + delta, length: range.length),
acceptsBindParameters: acceptsBindParameters,
segmentEnd: segmentEnd + delta,
clientDelimiter: clientDelimiter
clientDelimiter: clientDelimiter,
copyData: copyData
)
}
}
Expand Down Expand Up @@ -271,7 +279,8 @@ public enum SQLStatementScanner {
/// 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.
///
/// A client delimiter never reaches the server, and a `COPY ... FROM STDIN` is sent without its rows.
/// A client delimiter never reaches the server, and a `COPY ... FROM STDIN` carries its rows apart from its text,
/// in ``ExecutableStatement/copyData``.
public static func executableStatement(from located: LocatedStatement) -> ExecutableStatement? {
guard !located.isClientLine else { return nil }
let rawSQL = located.sql
Expand All @@ -291,10 +300,18 @@ public enum SQLStatementScanner {
range: NSRange(location: located.offset + range.location, length: range.length),
acceptsBindParameters: located.acceptsBindParameters,
segmentEnd: NSMaxRange(located.range),
clientDelimiter: located.clientDelimiter
clientDelimiter: located.clientDelimiter,
copyData: copyData(of: located)
)
}

private static func copyData(of located: LocatedStatement) -> String? {
guard let rows = located.copyRows else { return nil }
let text = located.sql as NSString
let local = NSRange(location: rows.location - located.offset, length: rows.length)
return text.substring(with: SQLPsqlScript.copyData(inRows: local, of: text))
}

/// The statements after `ran`, one of those `sql` was split into, split again under `grammar`, up to `end`: once
/// a statement changes how the session reads literals, the rest no longer splits as planned. The split starts at
/// ``ExecutableStatement/segmentEnd``, so a `COPY`'s rows are never read as statements, and keeps the client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,90 @@ struct SQLPsqlScriptTests {
#expect(SQLStatementScanner.executableStatements(in: rows, grammar: unknown.execution).count == 2)
}

private func copyData(_ sql: String) -> String {
let text = sql as NSString
let semicolon = text.range(of: ";").location
let rows = SQLPsqlScript.copyRows(afterSemicolonAt: semicolon, in: text, length: text.length)
return text.substring(with: SQLPsqlScript.copyData(inRows: rows, of: text))
}

@Test("A COPY's data is the lines after its ; line, up to the \\. line, which is never sent", arguments: [
("COPY t FROM stdin;\n1\tone\n2\ttwo\n\\.\nSELECT 2;", "1\tone\n2\ttwo\n"),
("COPY t FROM stdin;\r\n1\tone\r\n2\ttwo\r\n\\.\r\nSELECT 2;", "1\tone\r\n2\ttwo\r\n"),
("COPY t FROM stdin;\n1\tone\n2\ttwo\n", "1\tone\n2\ttwo\n"),
("COPY t FROM stdin;\n1\tone\n2\ttwo", "1\tone\n2\ttwo"),
("COPY t FROM stdin;\n1\tone\n\\.", "1\tone\n"),
("COPY t FROM stdin;\n1\tone\n\\.\r", "1\tone\n"),
("COPY t FROM stdin;\n\\. \n\\.\n", "\\. \n"),
("COPY t FROM stdin;", ""),
("COPY t FROM stdin;\n\\.\nSELECT 2;", ""),
("COPY t FROM stdin; SELECT 8;\n1\ta\n\\.\nSELECT 7;", "1\ta\n"),
("COPY t FROM stdin; DROP TABLE users;", ""),
("COPY t FROM stdin; DROP TABLE users;\n", ""),
])
func copyDataSpan(sql: String, expected: String) {
#expect(copyData(sql) == expected)
}

@Test("A scanned COPY carries its data, which offset(by:) keeps, and no other statement carries any")
func executableStatementCarriesCopyData() throws {
let sql = "SELECT 1;\nCOPY t (a, b) FROM stdin;\n1\tone\n2\ttwo\n\\.\nSELECT 2;"
let statements = SQLStatementScanner.executableStatements(in: sql, grammar: Self.postgreSQL.execution)
#expect(statements.map { $0.sql } == ["SELECT 1", "COPY t (a, b) FROM stdin", "SELECT 2"])
#expect(statements.map { $0.copyData } == [nil, "1\tone\n2\ttwo\n", nil])

let copy = try #require(statements.dropFirst().first)
let moved = copy.offset(by: 40)
#expect(moved.copyData == "1\tone\n2\ttwo\n")
#expect(moved.range.location == copy.range.location + 40)
#expect(moved.segmentEnd == copy.segmentEnd + 40)
}

@Test("A COPY with no rows carries empty data and still runs as a COPY", arguments: [
"COPY t FROM stdin;", "COPY t FROM stdin;\n\\.\n", "COPY t FROM stdin; DROP TABLE users;\n",
])
func copyWithoutRows(sql: String) {
let statements = SQLStatementScanner.executableStatements(in: sql, grammar: Self.postgreSQL.execution)
#expect(statements.map { $0.sql } == ["COPY t FROM stdin"])
#expect(statements.map { $0.copyData } == [""])
}

@Test("Run Current's text and a split resumed after a statement give the COPY the same data")
func copyDataInFragmentsAndResumedSplits() throws {
let sql = "SET search_path = public;\r\nCOPY t FROM stdin;\r\n1\tone\r\n2\t\\N\r\n\\.\r\nSELECT 2;"
let grammar = Self.postgreSQL.execution
let statements = SQLStatementScanner.executableStatements(in: sql, grammar: grammar)
let set = try #require(statements.first)

let resumed = SQLStatementScanner.executableStatements(
in: sql, after: set, through: (sql as NSString).length, grammar: grammar
)
#expect(resumed.map { $0.copyData } == ["1\tone\r\n2\t\\N\r\n", nil])

let navigable = SQLStatementScanner.navigableStatements(in: sql, grammar: grammar)
let fragment = (sql as NSString).substring(with: navigable[1].contentRange)
let rerun = SQLStatementScanner.executableStatements(in: fragment, grammar: grammar)
#expect(rerun.map { $0.copyData } == ["1\tone\r\n2\t\\N\r\n"])
}

@Test("Run Current's text keeps a last row's trailing tab when no \\. line ends the rows")
func copyContentKeepsTheLastRowAsWritten() throws {
let sql = "SELECT 1;\nCOPY t (a, b) FROM stdin;\n1\tone\n2\t\n"
let grammar = Self.postgreSQL.execution
let navigable = SQLStatementScanner.navigableStatements(in: sql, grammar: grammar)
let fragment = (sql as NSString).substring(with: navigable[1].contentRange)
let rerun = SQLStatementScanner.executableStatements(in: fragment, grammar: grammar)
#expect(rerun.map { $0.copyData } == ["1\tone\n2\t\n"])
}

@Test("An engine that does not read psql scripts carries no copy data")
func noCopyDataOutsidePsqlScripts() {
let mysql = SQLLexicalReadings.resolve(databaseTypeId: "MySQL", declared: nil, session: nil).execution
let statements = SQLStatementScanner.executableStatements(in: "COPY t FROM stdin;\n1\n", grammar: mysql)
#expect(statements.count == 2)
#expect(statements.allSatisfy { $0.copyData == nil })
}

@Test("The text psql holds as no SQL is reported for the formatter")
func scriptText() {
let sql = "\\restrict k\nCOPY t FROM stdin;\n1\ta\n\\.\nSELECT 1;"
Expand Down
Loading
Loading