|
| 1 | +// |
| 2 | +// SQLRowToStatementConverter.swift |
| 3 | +// TablePro |
| 4 | + |
| 5 | +import Foundation |
| 6 | + |
| 7 | +internal struct SQLRowToStatementConverter { |
| 8 | + internal let tableName: String |
| 9 | + internal let columns: [String] |
| 10 | + internal let primaryKeyColumn: String? |
| 11 | + internal let databaseType: DatabaseType |
| 12 | + |
| 13 | + private static let maxRows = 50_000 |
| 14 | + |
| 15 | + internal func generateInserts(rows: [[String?]]) -> String { |
| 16 | + let capped = rows.prefix(Self.maxRows) |
| 17 | + let quotedTable = quoteColumn(tableName) |
| 18 | + let quotedColumns = columns.map { quoteColumn($0) }.joined(separator: ", ") |
| 19 | + |
| 20 | + return capped.map { row in |
| 21 | + let values = row.map { formatValue($0) }.joined(separator: ", ") |
| 22 | + return "INSERT INTO \(quotedTable) (\(quotedColumns)) VALUES (\(values));" |
| 23 | + }.joined(separator: "\n") |
| 24 | + } |
| 25 | + |
| 26 | + internal func generateUpdates(rows: [[String?]]) -> String { |
| 27 | + let capped = rows.prefix(Self.maxRows) |
| 28 | + |
| 29 | + return capped.map { row in |
| 30 | + buildUpdateStatement(row: row) |
| 31 | + }.joined(separator: "\n") |
| 32 | + } |
| 33 | + |
| 34 | + // MARK: - Private Helpers |
| 35 | + |
| 36 | + private func buildUpdateStatement(row: [String?]) -> String { |
| 37 | + let quotedTable = quoteColumn(tableName) |
| 38 | + |
| 39 | + let setClause: String |
| 40 | + let whereClause: String |
| 41 | + |
| 42 | + if let pkColumn = primaryKeyColumn, |
| 43 | + let pkIndex = columns.firstIndex(of: pkColumn), |
| 44 | + row.indices.contains(pkIndex) { |
| 45 | + let pkValue = row[pkIndex] |
| 46 | + |
| 47 | + let setClauses = columns.enumerated().compactMap { index, col -> String? in |
| 48 | + guard col != pkColumn else { return nil } |
| 49 | + let value = row.indices.contains(index) ? row[index] : nil |
| 50 | + return "\(quoteColumn(col)) = \(formatValue(value))" |
| 51 | + } |
| 52 | + setClause = setClauses.joined(separator: ", ") |
| 53 | + if pkValue == nil { |
| 54 | + whereClause = "\(quoteColumn(pkColumn)) IS NULL" |
| 55 | + } else { |
| 56 | + whereClause = "\(quoteColumn(pkColumn)) = \(formatValue(pkValue))" |
| 57 | + } |
| 58 | + } else { |
| 59 | + let allClauses = columns.enumerated().map { index, col -> String in |
| 60 | + let value = row.indices.contains(index) ? row[index] : nil |
| 61 | + return "\(quoteColumn(col)) = \(formatValue(value))" |
| 62 | + } |
| 63 | + setClause = allClauses.joined(separator: ", ") |
| 64 | + |
| 65 | + let whereParts = columns.enumerated().map { index, col -> String in |
| 66 | + let value = row.indices.contains(index) ? row[index] : nil |
| 67 | + if value == nil { |
| 68 | + return "\(quoteColumn(col)) IS NULL" |
| 69 | + } |
| 70 | + return "\(quoteColumn(col)) = \(formatValue(value))" |
| 71 | + } |
| 72 | + whereClause = whereParts.joined(separator: " AND ") |
| 73 | + } |
| 74 | + |
| 75 | + switch databaseType { |
| 76 | + case .clickhouse: |
| 77 | + return "ALTER TABLE \(quotedTable) UPDATE \(setClause) WHERE \(whereClause);" |
| 78 | + default: |
| 79 | + return "UPDATE \(quotedTable) SET \(setClause) WHERE \(whereClause);" |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private func formatValue(_ value: String?) -> String { |
| 84 | + guard let value else { |
| 85 | + return "NULL" |
| 86 | + } |
| 87 | + var escaped = value.replacingOccurrences(of: "'", with: "''") |
| 88 | + if databaseType == .mysql || databaseType == .mariadb { |
| 89 | + escaped = escaped.replacingOccurrences(of: "\\", with: "\\\\") |
| 90 | + } |
| 91 | + return "'\(escaped)'" |
| 92 | + } |
| 93 | + |
| 94 | + private func quoteColumn(_ name: String) -> String { |
| 95 | + databaseType.quoteIdentifier(name) |
| 96 | + } |
| 97 | +} |
0 commit comments