Skip to content

Commit c34533e

Browse files
authored
Merge pull request #86 from vapor/nested-unkeyed-data
fix nested unkeyed encoding data
2 parents d8a17a9 + a335c3d commit c34533e

File tree

3 files changed

+7
-25
lines changed

3 files changed

+7
-25
lines changed

Sources/PostgreSQL/Codable/PostgreSQLValueEncoder.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,13 @@ public struct PostgreSQLDataEncoder {
141141
}
142142

143143
mutating func encode<T>(_ value: T) throws where T : Encodable {
144+
let data: PostgreSQLData
144145
if let convertible = value as? PostgreSQLDataConvertible {
145-
try encoder.array.append(convertible.convertToPostgreSQLData())
146+
data = try convertible.convertToPostgreSQLData()
146147
} else {
147-
try value.encode(to: encoder)
148+
data = try PostgreSQLDataEncoder().encode(value)
148149
}
150+
encoder.array.append(data)
149151
}
150152

151153
mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type) -> KeyedEncodingContainer<NestedKey> where NestedKey : CodingKey {

Sources/PostgreSQL/SQL/PostgreSQLUpsert.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,8 @@ public struct PostgreSQLUpsert: SQLSerializable {
66
public typealias Expression = PostgreSQLExpression
77

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

1713
/// See `SQLUpsert`.
@@ -24,7 +20,7 @@ public struct PostgreSQLUpsert: SQLSerializable {
2420
public func serialize(_ binds: inout [Encodable]) -> String {
2521
var sql: [String] = []
2622
sql.append("ON CONFLICT")
27-
sql.append("(" + columns.serialize(&binds) + ")")
23+
sql.append("(" + columns.map { $0.identifier }.serialize(&binds) + ")")
2824
sql.append("DO UPDATE SET")
2925
sql.append(values.map { $0.0.serialize(&binds) + " = " + $0.1.serialize(&binds) }.joined(separator: ", "))
3026
return sql.joined(separator: " ")

Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -540,21 +540,6 @@ class PostgreSQLConnectionTests: XCTestCase {
540540
print(c)
541541
}
542542
}
543-
544-
func testUpsert() throws {
545-
let values: [(PostgreSQLUpsert.Identifier, PostgreSQLUpsert.Expression)] = []
546-
547-
var upsert: PostgreSQLUpsert
548-
549-
upsert = PostgreSQLUpsert.upsert(nil, values)
550-
XCTAssertEqual(upsert.columns, [PostgreSQLColumnIdentifier.column(nil, .identifier("id"))])
551-
552-
upsert = PostgreSQLUpsert.upsert([], values)
553-
XCTAssertEqual(upsert.columns, [PostgreSQLColumnIdentifier.column(nil, .identifier("id"))])
554-
555-
upsert = PostgreSQLUpsert.upsert([.column(nil, .identifier("field"))], values)
556-
XCTAssertEqual(upsert.columns, [PostgreSQLColumnIdentifier.column(nil, .identifier("field"))])
557-
}
558543

559544
static var allTests = [
560545
("testBenchmark", testBenchmark),
@@ -575,7 +560,6 @@ class PostgreSQLConnectionTests: XCTestCase {
575560
("testOrderBy", testOrderBy),
576561
("testInvalidDate", testInvalidDate),
577562
("testEmptyArray", testEmptyArray),
578-
("testUpsert", testUpsert),
579563
]
580564
}
581565

0 commit comments

Comments
 (0)