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
6 changes: 4 additions & 2 deletions Sources/PostgreSQL/Codable/PostgreSQLValueEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,13 @@ public struct PostgreSQLDataEncoder {
}

mutating func encode<T>(_ value: T) throws where T : Encodable {
let data: PostgreSQLData
if let convertible = value as? PostgreSQLDataConvertible {
try encoder.array.append(convertible.convertToPostgreSQLData())
data = try convertible.convertToPostgreSQLData()
} else {
try value.encode(to: encoder)
data = try PostgreSQLDataEncoder().encode(value)
}
encoder.array.append(data)
}

mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type) -> KeyedEncodingContainer<NestedKey> where NestedKey : CodingKey {
Expand Down
10 changes: 3 additions & 7 deletions Sources/PostgreSQL/SQL/PostgreSQLUpsert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ public struct PostgreSQLUpsert: SQLSerializable {
public typealias Expression = PostgreSQLExpression

/// See `SQLUpsert`.
public static func upsert(_ columns: [PostgreSQLColumnIdentifier]?, _ values: [(Identifier, Expression)]) -> PostgreSQLUpsert {
if let columns = columns, !columns.isEmpty {
return self.init(columns: columns, values: values)
} else {
return self.init(columns: [.column(nil, .identifier("id"))], values: values)
}
public static func upsert(_ columns: [PostgreSQLColumnIdentifier], _ values: [(Identifier, Expression)]) -> PostgreSQLUpsert {
return self.init(columns: columns, values: values)
}

/// See `SQLUpsert`.
Expand All @@ -24,7 +20,7 @@ public struct PostgreSQLUpsert: SQLSerializable {
public func serialize(_ binds: inout [Encodable]) -> String {
var sql: [String] = []
sql.append("ON CONFLICT")
sql.append("(" + columns.serialize(&binds) + ")")
sql.append("(" + columns.map { $0.identifier }.serialize(&binds) + ")")
sql.append("DO UPDATE SET")
sql.append(values.map { $0.0.serialize(&binds) + " = " + $0.1.serialize(&binds) }.joined(separator: ", "))
return sql.joined(separator: " ")
Expand Down
16 changes: 0 additions & 16 deletions Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -540,21 +540,6 @@ class PostgreSQLConnectionTests: XCTestCase {
print(c)
}
}

func testUpsert() throws {
let values: [(PostgreSQLUpsert.Identifier, PostgreSQLUpsert.Expression)] = []

var upsert: PostgreSQLUpsert

upsert = PostgreSQLUpsert.upsert(nil, values)
XCTAssertEqual(upsert.columns, [PostgreSQLColumnIdentifier.column(nil, .identifier("id"))])

upsert = PostgreSQLUpsert.upsert([], values)
XCTAssertEqual(upsert.columns, [PostgreSQLColumnIdentifier.column(nil, .identifier("id"))])

upsert = PostgreSQLUpsert.upsert([.column(nil, .identifier("field"))], values)
XCTAssertEqual(upsert.columns, [PostgreSQLColumnIdentifier.column(nil, .identifier("field"))])
}

static var allTests = [
("testBenchmark", testBenchmark),
Expand All @@ -575,7 +560,6 @@ class PostgreSQLConnectionTests: XCTestCase {
("testOrderBy", testOrderBy),
("testInvalidDate", testInvalidDate),
("testEmptyArray", testEmptyArray),
("testUpsert", testUpsert),
]
}

Expand Down