Skip to content

Commit 1c22eb4

Browse files
committed
add PostgresDriver type
1 parent bc8e1f4 commit 1c22eb4

File tree

2 files changed

+43
-39
lines changed

2 files changed

+43
-39
lines changed

Sources/FluentPostgresDriver/Postgres+Fluent.swift

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,60 @@
11
import FluentSQL
22

3-
extension PostgresConnection: FluentDatabase {
4-
public func execute(_ query: FluentQuery, _ onOutput: @escaping (FluentOutput) throws -> ()) -> EventLoopFuture<Void> {
5-
return FluentSQLDatabase(delegate: PostgresConnectionSQLDelegate(self))
6-
.execute(query, onOutput)
7-
}
3+
#warning("TODO: fix to allow conformance")
4+
struct SQLOutput: FluentOutput {
5+
let row: SQLRow
86

9-
public func execute(_ schema: FluentSchema) -> EventLoopFuture<Void> {
10-
return FluentSQLDatabase(delegate: PostgresConnectionSQLDelegate(self))
11-
.execute(schema)
12-
}
13-
}
14-
15-
extension ConnectionPool: FluentDatabase where Source.Connection: FluentDatabase {
16-
public var eventLoop: EventLoop {
17-
return self.source.eventLoop
7+
var description: String {
8+
return "\(self.row)"
189
}
1910

20-
public func execute(_ query: FluentQuery, _ onOutput: @escaping (FluentOutput) throws -> ()) -> EventLoopFuture<Void> {
21-
return self.withConnection { conn in
22-
return conn.execute(query, onOutput)
23-
}
11+
public init(row: SQLRow) {
12+
self.row = row
2413
}
2514

26-
public func execute(_ schema: FluentSchema) -> EventLoopFuture<Void> {
27-
return self.withConnection { conn in
28-
return conn.execute(schema)
29-
}
15+
func decode<T>(field: String, as type: T.Type) throws -> T where T : Decodable {
16+
return try self.row.decode(column: field, as: T.self)
3017
}
3118
}
3219

33-
34-
private struct PostgresConnectionSQLDelegate: FluentSQLDatabaseDelegate {
35-
var eventLoop: EventLoop {
36-
return self.connection.eventLoop
20+
public struct PostgresDriver: FluentDatabase {
21+
public var eventLoop: EventLoop {
22+
return self.connectionPool.source.eventLoop
3723
}
3824

39-
let connection: PostgresConnection
25+
public let connectionPool: ConnectionPool<PostgresDatabase>
4026

41-
var database: SQLDatabase {
42-
return self.connection
27+
public init(connectionPool: ConnectionPool<PostgresDatabase>) {
28+
self.connectionPool = connectionPool
4329
}
4430

45-
init(_ connection: PostgresConnection) {
46-
self.connection = connection
31+
public func execute(_ query: FluentQuery, _ onOutput: @escaping (FluentOutput) throws -> ()) -> EventLoopFuture<Void> {
32+
return connectionPool.withConnection { connection in
33+
var sql = SQLQueryConverter().convert(query)
34+
switch query.action {
35+
case .create:
36+
sql = PostgresReturning(sql)
37+
default: break
38+
}
39+
return connection.sqlQuery(sql) { row in
40+
let output = SQLOutput(row: row)
41+
try onOutput(output)
42+
}
43+
}
4744
}
4845

49-
func convert(_ fluent: FluentQuery, _ sql: SQLExpression) -> SQLExpression {
50-
switch fluent.action {
51-
case .create:
52-
return PostgresReturning(sql)
53-
default:
54-
return sql
46+
public func execute(_ schema: FluentSchema) -> EventLoopFuture<Void> {
47+
return self.connectionPool.withConnection { connection in
48+
return connection.sqlQuery(SQLSchemaConverter().convert(schema)) { row in
49+
fatalError("unexpected output")
50+
}
5551
}
5652
}
53+
54+
public func close() -> EventLoopFuture<Void> {
55+
#warning("TODO: implement connectionPool.close()")
56+
fatalError("")
57+
}
5758
}
5859

5960
private struct PostgresReturning: SQLExpression {

Tests/FluentPostgresDriverTests/FluentPostgresDriverTests.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ final class FluentPostgresDriverTests: XCTestCase {
3838
}
3939

4040
func testEagerLoadSubqueryJSONEncode() throws {
41+
#warning("TODO: fix connection pool alg")
4142
try self.benchmarker.testEagerLoadSubqueryJSONEncode()
4243
}
4344

4445
func testEagerLoadJoinJSONEncode() throws {
46+
#warning("TODO: fix connection pool alg")
4547
try self.benchmarker.testEagerLoadJoinJSONEncode()
4648
}
4749

@@ -89,8 +91,9 @@ final class FluentPostgresDriverTests: XCTestCase {
8991
database: "vapor_database",
9092
tlsConfig: nil
9193
)
92-
let conn = try! PostgresDatabase(config: config, on: eventLoop).makeConnection().wait()
93-
self.benchmarker = FluentBenchmarker(database: conn)
94+
let pool = PostgresDatabase(config: config, on: eventLoop).makeConnectionPool(config: .init(maxConnections: 1))
95+
let driver = PostgresDriver(connectionPool: pool)
96+
self.benchmarker = FluentBenchmarker(database: driver)
9497
}
9598

9699
static let allTests = [

0 commit comments

Comments
 (0)