Skip to content

Commit 59635e4

Browse files
committed
refactor(plugin-postgresql): route identifier quoting and literal escaping through shared helpers
1 parent d059f19 commit 59635e4

4 files changed

Lines changed: 17 additions & 27 deletions

File tree

Plugins/PostgreSQLDriverPlugin/CockroachPluginDriver.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,11 @@ final class CockroachPluginDriver: LibPQBackedDriver, @unchecked Sendable {
246246
}
247247

248248
func createDatabase(_ request: PluginCreateDatabaseRequest) async throws {
249-
let quotedName = request.name.replacingOccurrences(of: "\"", with: "\"\"")
250-
_ = try await execute(query: "CREATE DATABASE \"\(quotedName)\"")
249+
_ = try await execute(query: "CREATE DATABASE \(quoteIdentifier(request.name))")
251250
}
252251

253252
func dropDatabase(name: String) async throws {
254-
let quotedName = name.replacingOccurrences(of: "\"", with: "\"\"")
255-
_ = try await execute(query: "DROP DATABASE \"\(quotedName)\"")
253+
_ = try await execute(query: "DROP DATABASE \(quoteIdentifier(name))")
256254
}
257255

258256
// MARK: - Query Helpers

Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Columns.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import TableProPluginKit
88

99
extension PostgreSQLPluginDriver {
1010
func fetchColumns(table: String, schema: String?) async throws -> [PluginColumnInfo] {
11-
let safeSchema = escapeLiteralForColumns(currentSchema ?? "public")
12-
let safeTable = escapeLiteralForColumns(table)
11+
let safeSchema = escapeStringLiteral(currentSchema ?? "public")
12+
let safeTable = escapeStringLiteral(table)
1313
let enumMap = try await fetchEnumLabelMap(schema: safeSchema)
1414
let caps = versionedCapabilities
1515
let identityProjection = caps.hasIdentityColumns ? "a.attidentity" : "NULL::text"
@@ -60,7 +60,7 @@ extension PostgreSQLPluginDriver {
6060
}
6161

6262
func fetchAllColumns(schema: String?) async throws -> [String: [PluginColumnInfo]] {
63-
let safeSchema = escapeLiteralForColumns(currentSchema ?? "public")
63+
let safeSchema = escapeStringLiteral(currentSchema ?? "public")
6464
let enumMap = try await fetchEnumLabelMap(schema: safeSchema)
6565
let caps = versionedCapabilities
6666
let identityProjection = caps.hasIdentityColumns ? "a.attidentity" : "NULL::text"
@@ -134,10 +134,6 @@ extension PostgreSQLPluginDriver {
134134
return map
135135
}
136136

137-
fileprivate func escapeLiteralForColumns(_ str: String) -> String {
138-
str.replacingOccurrences(of: "'", with: "''")
139-
}
140-
141137
fileprivate func mapPgColumnRow(
142138
_ row: [PluginCellValue],
143139
tableNameOffset: Int,

Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver.swift

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ final class PostgreSQLPluginDriver: LibPQBackedDriver, @unchecked Sendable {
336336

337337
func fetchTableDDL(table: String, schema: String?) async throws -> String {
338338
let safeTable = escapeLiteral(table)
339-
let quotedTable = "\"\(table.replacingOccurrences(of: "\"", with: "\"\""))\""
339+
let quotedTable = quoteIdentifier(table)
340340
let caps = versionedCapabilities
341341

342342
let identityClause: String = caps.hasIdentityColumns ? """
@@ -431,7 +431,7 @@ final class PostgreSQLPluginDriver: LibPQBackedDriver, @unchecked Sendable {
431431
var parts = columnDefs
432432
parts.append(contentsOf: constraints)
433433

434-
let quotedSchema = "\"\(core.currentSchema.replacingOccurrences(of: "\"", with: "\"\""))\""
434+
let quotedSchema = quoteIdentifier(core.currentSchema)
435435
let ddl = "CREATE TABLE \(quotedSchema).\(quotedTable) (\n " +
436436
parts.joined(separator: ",\n ") +
437437
"\n);"
@@ -599,9 +599,9 @@ final class PostgreSQLPluginDriver: LibPQBackedDriver, @unchecked Sendable {
599599
let incrementBy = row[4].asText ?? "1"
600600
let cycle = row[5].asText == "t" ? " CYCLE" : ""
601601
let lastValue = row.count > 6 ? row[6].asText : nil
602-
let quotedSeqName = "\"\(seqName.replacingOccurrences(of: "\"", with: "\"\""))\""
603-
let escapedSchemaForLiteral = schemaName.replacingOccurrences(of: "'", with: "''")
604-
let escapedSeqForLiteral = seqName.replacingOccurrences(of: "'", with: "''")
602+
let quotedSeqName = quoteIdentifier(seqName)
603+
let escapedSchemaForLiteral = escapeStringLiteral(schemaName)
604+
let escapedSeqForLiteral = escapeStringLiteral(seqName)
605605
var ddl = "CREATE SEQUENCE \(quotedSeqName) INCREMENT BY \(incrementBy)"
606606
+ " MINVALUE \(minVal) MAXVALUE \(maxVal)"
607607
+ " START WITH \(startVal)\(cycle);"
@@ -692,7 +692,7 @@ final class PostgreSQLPluginDriver: LibPQBackedDriver, @unchecked Sendable {
692692
}
693693

694694
func createDatabase(_ request: PluginCreateDatabaseRequest) async throws {
695-
let quotedName = request.name.replacingOccurrences(of: "\"", with: "\"\"")
695+
let quotedName = quoteIdentifier(request.name)
696696

697697
guard let encoding = request.values["encoding"] else {
698698
throw LibPQPluginError(
@@ -709,7 +709,7 @@ final class PostgreSQLPluginDriver: LibPQBackedDriver, @unchecked Sendable {
709709
)
710710
}
711711

712-
var sql = "CREATE DATABASE \"\(quotedName)\" ENCODING '\(encoding)'"
712+
var sql = "CREATE DATABASE \(quotedName) ENCODING '\(encoding)'"
713713

714714
let supportsProvider = versionedCapabilities.hasDatabaseICULocale
715715
let provider = supportsProvider ? (request.values["provider"] ?? "libc") : "libc"
@@ -789,8 +789,7 @@ final class PostgreSQLPluginDriver: LibPQBackedDriver, @unchecked Sendable {
789789
}
790790

791791
func dropDatabase(name: String) async throws {
792-
let escapedName = name.replacingOccurrences(of: "\"", with: "\"\"")
793-
_ = try await execute(query: "DROP DATABASE \"\(escapedName)\"")
792+
_ = try await execute(query: "DROP DATABASE \(quoteIdentifier(name))")
794793
}
795794

796795
private struct Template1Defaults {

Plugins/PostgreSQLDriverPlugin/RedshiftPluginDriver.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,8 @@ final class RedshiftPluginDriver: LibPQBackedDriver, @unchecked Sendable {
295295

296296
func fetchTableDDL(table: String, schema: String?) async throws -> String {
297297
let safeTable = escapeLiteral(table)
298-
let quotedTable = "\"\(table.replacingOccurrences(of: "\"", with: "\"\""))\""
299-
let quotedSchema = "\"\(core.currentSchema.replacingOccurrences(of: "\"", with: "\"\""))\""
298+
let quotedTable = quoteIdentifier(table)
299+
let quotedSchema = quoteIdentifier(core.currentSchema)
300300

301301
do {
302302
let showResult = try await execute(query: "SHOW TABLE \(quotedSchema).\(quotedTable)")
@@ -508,14 +508,12 @@ final class RedshiftPluginDriver: LibPQBackedDriver, @unchecked Sendable {
508508
)
509509
}
510510

511-
let quotedName = request.name.replacingOccurrences(of: "\"", with: "\"\"")
512-
let sql = "CREATE DATABASE \"\(quotedName)\" COLLATE \(collate)"
511+
let sql = "CREATE DATABASE \(quoteIdentifier(request.name)) COLLATE \(collate)"
513512
_ = try await execute(query: sql)
514513
}
515514

516515
func dropDatabase(name: String) async throws {
517-
let escapedName = name.replacingOccurrences(of: "\"", with: "\"\"")
518-
_ = try await execute(query: "DROP DATABASE \"\(escapedName)\"")
516+
_ = try await execute(query: "DROP DATABASE \(quoteIdentifier(name))")
519517
}
520518

521519
// MARK: - All Tables Metadata
@@ -537,5 +535,4 @@ final class RedshiftPluginDriver: LibPQBackedDriver, @unchecked Sendable {
537535
ORDER BY "table"
538536
"""
539537
}
540-
541538
}

0 commit comments

Comments
 (0)