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
18 changes: 16 additions & 2 deletions Sources/FluentPostgresDriver/Postgres+Fluent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,12 @@ extension PostgresConnection: Database {
sql = PostgresReturning(sql)
default: break
}
return self.sqlQuery(sql) { row in
try onOutput(row.fluentOutput)
var serializer = SQLSerializer(dialect: PostgresDialect())
sql.serialize(to: &serializer)
return try! self.query(serializer.sql, serializer.binds.map { encodable in
return try PostgresDataEncoder().encode(encodable)
}) { row in
try onOutput(row)
}
}

Expand All @@ -81,6 +85,16 @@ extension PostgresConnection: Database {
}
}

extension PostgresRow: DatabaseOutput {
public func contains(field: String) -> Bool {
return self.column(field) != nil
}

public func decode<T>(field: String, as type: T.Type) throws -> T where T : Decodable {
return try self.decode(column: field, as: T.self)
}
}

private struct PostgresReturning: SQLExpression {
let base: SQLExpression
init(_ base: SQLExpression) {
Expand Down
7 changes: 6 additions & 1 deletion Sources/FluentPostgresDriver/PostgresDataDecoder.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import NIOPostgres

#warning("TODO: move to codable kit")
struct DecoderUnwrapper: Decodable {
Expand All @@ -14,7 +15,11 @@ public struct PostgresDataDecoder {
public func decode<T>(_ type: T.Type, from data: PostgresData) throws -> T
where T: Decodable
{
return try T.init(from: _Decoder(data: data))
if let convertible = T.self as? PostgresDataCustomConvertible.Type {
return convertible.init(postgresData: data)! as! T
} else {
return try T.init(from: _Decoder(data: data))
}
}

#warning("TODO: finish implementing")
Expand Down
2 changes: 1 addition & 1 deletion Sources/FluentPostgresDriver/PostgresDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ extension PostgresConnection: ConnectionPoolItem {
extension PostgresRow: SQLRow {
public func decode<D>(column: String, as type: D.Type) throws -> D where D : Decodable {
guard let data = self.column(column) else {
fatalError()
fatalError("Column \(column) is missing")
}
return try PostgresDataDecoder().decode(D.self, from: data)
}
Expand Down
12 changes: 6 additions & 6 deletions Tests/FluentPostgresDriverTests/FluentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,8 @@ final class FluentPostgresDriverTests: XCTestCase {
try self.benchmarker.testEagerLoadParentJoin()
}

func testEagerLoadSubqueryJSONEncode() throws {
try self.benchmarker.testEagerLoadSubqueryJSONEncode()
}

func testEagerLoadJoinJSONEncode() throws {
try self.benchmarker.testEagerLoadJoinJSONEncode()
func testEagerLoadJSON() throws {
try self.benchmarker.testEagerLoadJSON()
}

func testMigrator() throws {
Expand Down Expand Up @@ -88,6 +84,10 @@ final class FluentPostgresDriverTests: XCTestCase {
func testUniqueFields() throws {
try self.benchmarker.testUniqueFields()
}

func testAsyncCreate() throws {
try self.benchmarker.testAsyncCreate()
}

override func setUp() {
let eventLoop = MultiThreadedEventLoopGroup(numberOfThreads: 1).next()
Expand Down
4 changes: 2 additions & 2 deletions Tests/FluentPostgresDriverTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ extension FluentPostgresDriverTests {
static let __allTests__FluentPostgresDriverTests = [
("testAggregates", testAggregates),
("testAll", testAll),
("testAsyncCreate", testAsyncCreate),
("testBatchCreate", testBatchCreate),
("testBatchUpdate", testBatchUpdate),
("testChunkedFetch", testChunkedFetch),
("testCreate", testCreate),
("testDelete", testDelete),
("testEagerLoadChildren", testEagerLoadChildren),
("testEagerLoadJoinJSONEncode", testEagerLoadJoinJSONEncode),
("testEagerLoadJSON", testEagerLoadJSON),
("testEagerLoadParent", testEagerLoadParent),
("testEagerLoadParentJoin", testEagerLoadParentJoin),
("testEagerLoadSubqueryJSONEncode", testEagerLoadSubqueryJSONEncode),
("testIdentifierGeneration", testIdentifierGeneration),
("testJoin", testJoin),
("testMigrator", testMigrator),
Expand Down