@@ -4,27 +4,54 @@ import AsyncKit
44import Bridges
55import Logging
66
7- public final class MySQLBridge : Bridgeable {
8- public typealias Source = MySQLConnectionSource
9- public typealias Database = MySQLDatabase
10- public typealias Connection = MySQLConnection
7+ public struct MySQLBridge {
8+ let context : BridgeWithContext < _MySQLBridge >
9+
10+ init ( _ context: BridgeWithContext < _MySQLBridge > ) {
11+ self . context = context
12+ }
13+
14+ public func connection< T> ( to db: DatabaseIdentifier ,
15+ _ closure: @escaping ( MySQLConnection ) -> EventLoopFuture < T > ) -> EventLoopFuture < T > {
16+ context. bridge. connection ( to: db, on: context. eventLoop, closure)
17+ }
18+
19+ public func transaction< T> ( to db: DatabaseIdentifier ,
20+ _ closure: @escaping ( MySQLConnection ) -> EventLoopFuture < T > ) -> EventLoopFuture < T > {
21+ context. bridge. transaction ( to: db, on: context. eventLoop, closure)
22+ }
23+
24+ public func register( _ db: DatabaseIdentifier ) {
25+ context. bridge. register ( db)
26+ }
1127
12- public static var dialect : SQLDialect { . mysql }
28+ public func migrator( for db: DatabaseIdentifier ) -> Migrator {
29+ BridgeDatabaseMigrations < _MySQLBridge > ( context. bridge, db: db)
30+ }
31+ }
32+
33+ final class _MySQLBridge : Bridgeable {
34+ typealias Source = MySQLConnectionSource
35+ typealias Database = MySQLDatabase
36+ typealias Connection = MySQLConnection
37+
38+ static var dialect : SQLDialect { . mysql }
1339
14- public var pools : [ String : GroupPool ] = [ : ]
40+ var pools : [ String : GroupPool ] = [ : ]
1541
16- public let logger : Logger
17- public let eventLoopGroup : EventLoopGroup
42+ let logger : Logger
43+ let eventLoopGroup : EventLoopGroup
1844
19- required public init ( eventLoopGroup: EventLoopGroup , logger: Logger ) {
45+ required init ( eventLoopGroup: EventLoopGroup , logger: Logger ) {
2046 self . eventLoopGroup = eventLoopGroup
2147 self . logger = logger
2248 }
2349
2450 /// Gives a connection to the database and closes it automatically in both success and error cases
25- public func connection< T> ( to db: DatabaseIdentifier ,
26- _ closure: @escaping ( MySQLConnection ) -> EventLoopFuture < T > ) -> EventLoopFuture < T > {
27- self . db ( db) . withConnection { conn in
51+ func connection< T> ( to db: DatabaseIdentifier ,
52+ on eventLoop: EventLoop ,
53+ _ closure: @escaping ( MySQLConnection ) -> EventLoopFuture < T > ) -> EventLoopFuture < T > {
54+ self . db ( db, on: eventLoop) . withConnection { conn in
2855 closure ( conn) . flatMap { result in
2956 if conn. isClosed {
3057 return conn. eventLoop. future ( result)
@@ -43,8 +70,8 @@ public final class MySQLBridge: Bridgeable {
4370 }
4471 }
4572
46- public func db( _ db: DatabaseIdentifier ) -> MySQLDatabase {
47- _ConnectionPoolMySQLDatabase ( pool: pool ( db) , logger: logger)
73+ func db( _ db: DatabaseIdentifier , on eventLoop : EventLoop ) -> MySQLDatabase {
74+ _ConnectionPoolMySQLDatabase ( pool: pool ( db, for : eventLoop ) , logger: logger, eventLoop : eventLoop )
4875 }
4976
5077 deinit {
@@ -55,21 +82,18 @@ public final class MySQLBridge: Bridgeable {
5582// MARK: Database on pool
5683
5784extension EventLoopConnectionPool where Source == MySQLConnectionSource {
58- public func database( logger: Logger ) -> MySQLDatabase {
59- _ConnectionPoolMySQLDatabase ( pool: self , logger: logger)
85+ public func database( logger: Logger , on eventLoop : EventLoop ) -> MySQLDatabase {
86+ _ConnectionPoolMySQLDatabase ( pool: self , logger: logger, eventLoop : eventLoop )
6087 }
6188}
6289
6390private struct _ConnectionPoolMySQLDatabase {
6491 let pool : EventLoopConnectionPool < MySQLConnectionSource >
6592 let logger : Logger
93+ let eventLoop : EventLoop
6694}
6795
6896extension _ConnectionPoolMySQLDatabase : MySQLDatabase {
69- var eventLoop : EventLoop {
70- self . pool. eventLoop
71- }
72-
7397 func send( _ command: MySQLCommand , logger: Logger ) -> EventLoopFuture < Void > {
7498 self . pool. withConnection ( logger: logger) {
7599 $0. send ( command, logger: logger)
0 commit comments