|
1 | 1 | import FluentSQL |
2 | 2 |
|
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 |
8 | 6 |
|
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)" |
18 | 9 | } |
19 | 10 |
|
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 |
24 | 13 | } |
25 | 14 |
|
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) |
30 | 17 | } |
31 | 18 | } |
32 | 19 |
|
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 |
37 | 23 | } |
38 | 24 |
|
39 | | - let connection:PostgresConnection |
| 25 | + publiclet connectionPool:ConnectionPool<PostgresDatabase> |
40 | 26 |
|
41 | | - vardatabase:SQLDatabase { |
42 | | - returnself.connection |
| 27 | + publicinit(connectionPool:ConnectionPool<PostgresDatabase>) { |
| 28 | + self.connectionPool = connectionPool |
43 | 29 | } |
44 | 30 |
|
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 | + } |
47 | 44 | } |
48 | 45 |
|
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 | + } |
55 | 51 | } |
56 | 52 | } |
| 53 | + |
| 54 | + public func close() -> EventLoopFuture<Void> { |
| 55 | + #warning("TODO: implement connectionPool.close()") |
| 56 | + fatalError("") |
| 57 | + } |
57 | 58 | } |
58 | 59 |
|
59 | 60 | private struct PostgresReturning: SQLExpression { |
|
0 commit comments