Skip to content

Commit 2afac78

Browse files
authored
fluentkit updates (#109)
1 parent 598bc21 commit 2afac78

File tree

5 files changed

+31
-12
lines changed

5 files changed

+31
-12
lines changed

Sources/FluentPostgresDriver/Postgres+Fluent.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,12 @@ extension PostgresConnection: Database {
6969
sql = PostgresReturning(sql)
7070
default: break
7171
}
72-
return self.sqlQuery(sql) { row in
73-
try onOutput(row.fluentOutput)
72+
var serializer = SQLSerializer(dialect: PostgresDialect())
73+
sql.serialize(to: &serializer)
74+
return try! self.query(serializer.sql, serializer.binds.map { encodable in
75+
return try PostgresDataEncoder().encode(encodable)
76+
}) { row in
77+
try onOutput(row)
7478
}
7579
}
7680

@@ -81,6 +85,16 @@ extension PostgresConnection: Database {
8185
}
8286
}
8387

88+
extension PostgresRow: DatabaseOutput {
89+
public func contains(field: String) -> Bool {
90+
return self.column(field) != nil
91+
}
92+
93+
public func decode<T>(field: String, as type: T.Type) throws -> T where T : Decodable {
94+
return try self.decode(column: field, as: T.self)
95+
}
96+
}
97+
8498
private struct PostgresReturning: SQLExpression {
8599
let base: SQLExpression
86100
init(_ base: SQLExpression) {

Sources/FluentPostgresDriver/PostgresDataDecoder.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Foundation
2+
import NIOPostgres
23

34
#warning("TODO: move to codable kit")
45
struct DecoderUnwrapper: Decodable {
@@ -14,7 +15,11 @@ public struct PostgresDataDecoder {
1415
public func decode<T>(_ type: T.Type, from data: PostgresData) throws -> T
1516
where T: Decodable
1617
{
17-
return try T.init(from: _Decoder(data: data))
18+
if let convertible = T.self as? PostgresDataCustomConvertible.Type {
19+
return convertible.init(postgresData: data)! as! T
20+
} else {
21+
return try T.init(from: _Decoder(data: data))
22+
}
1823
}
1924

2025
#warning("TODO: finish implementing")

Sources/FluentPostgresDriver/PostgresDatabase.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ extension PostgresConnection: ConnectionPoolItem {
111111
extension PostgresRow: SQLRow {
112112
public func decode<D>(column: String, as type: D.Type) throws -> D where D : Decodable {
113113
guard let data = self.column(column) else {
114-
fatalError()
114+
fatalError("Column \(column) is missing")
115115
}
116116
return try PostgresDataDecoder().decode(D.self, from: data)
117117
}

Tests/FluentPostgresDriverTests/FluentTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,8 @@ final class FluentPostgresDriverTests: XCTestCase {
3737
try self.benchmarker.testEagerLoadParentJoin()
3838
}
3939

40-
func testEagerLoadSubqueryJSONEncode() throws {
41-
try self.benchmarker.testEagerLoadSubqueryJSONEncode()
42-
}
43-
44-
func testEagerLoadJoinJSONEncode() throws {
45-
try self.benchmarker.testEagerLoadJoinJSONEncode()
40+
func testEagerLoadJSON() throws {
41+
try self.benchmarker.testEagerLoadJSON()
4642
}
4743

4844
func testMigrator() throws {
@@ -88,6 +84,10 @@ final class FluentPostgresDriverTests: XCTestCase {
8884
func testUniqueFields() throws {
8985
try self.benchmarker.testUniqueFields()
9086
}
87+
88+
func testAsyncCreate() throws {
89+
try self.benchmarker.testAsyncCreate()
90+
}
9191

9292
override func setUp() {
9393
let eventLoop = MultiThreadedEventLoopGroup(numberOfThreads: 1).next()

Tests/FluentPostgresDriverTests/XCTestManifests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ extension FluentPostgresDriverTests {
88
static let __allTests__FluentPostgresDriverTests = [
99
("testAggregates", testAggregates),
1010
("testAll", testAll),
11+
("testAsyncCreate", testAsyncCreate),
1112
("testBatchCreate", testBatchCreate),
1213
("testBatchUpdate", testBatchUpdate),
1314
("testChunkedFetch", testChunkedFetch),
1415
("testCreate", testCreate),
1516
("testDelete", testDelete),
1617
("testEagerLoadChildren", testEagerLoadChildren),
17-
("testEagerLoadJoinJSONEncode", testEagerLoadJoinJSONEncode),
18+
("testEagerLoadJSON", testEagerLoadJSON),
1819
("testEagerLoadParent", testEagerLoadParent),
1920
("testEagerLoadParentJoin", testEagerLoadParentJoin),
20-
("testEagerLoadSubqueryJSONEncode", testEagerLoadSubqueryJSONEncode),
2121
("testIdentifierGeneration", testIdentifierGeneration),
2222
("testJoin", testJoin),
2323
("testMigrator", testMigrator),

0 commit comments

Comments
 (0)