Skip to content

Commit 2e9988f

Browse files
authored
Merge pull request #1 from SwifQL/request-connection
🏀 Implement `requestConnection` method
2 parents 2d43606 + b524b2e commit 2e9988f

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

Sources/MySQLBridge/MySQLBridge.swift

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@ public struct MySQLBridge: ContextBridgeable {
1111
self.context = context
1212
}
1313

14+
/// Gives a connection to the database and releases it automatically in both success and error cases
1415
public func connection<T>(to db: DatabaseIdentifier,
1516
_ closure: @escaping (MySQLConnection) -> EventLoopFuture<T>) -> EventLoopFuture<T> {
1617
context.bridge.connection(to: db, on: context.eventLoop, closure)
1718
}
1819

20+
/// Gives a connection to the database and you should close it by yourself
21+
public func requestConnection(to db: DatabaseIdentifier) -> EventLoopFuture<MySQLConnection> {
22+
context.bridge.requestConnection(to: db, on: context.eventLoop)
23+
}
24+
1925
public func transaction<T>(to db: DatabaseIdentifier,
2026
_ closure: @escaping (MySQLConnection) -> EventLoopFuture<T>) -> EventLoopFuture<T> {
2127
context.bridge.transaction(to: db, on: context.eventLoop, closure)
@@ -47,14 +53,23 @@ public final class MBR: Bridgeable {
4753
self.logger = logger
4854
}
4955

50-
/// Gives a connection to the database and closes it automatically in both success and error cases
56+
/// Gives a connection to the database and releases it automatically in both success and error cases
5157
public func connection<T>(to db: DatabaseIdentifier,
5258
on eventLoop: EventLoop,
5359
_ closure: @escaping (MySQLConnection) -> EventLoopFuture<T>) -> EventLoopFuture<T> {
5460
self.db(db, on: eventLoop).withConnection { closure($0) }
5561
}
5662

63+
/// Gives a connection to the database and you should close it by yourself
64+
public func requestConnection(to db: DatabaseIdentifier, on eventLoop: EventLoop) -> EventLoopFuture<MySQLConnection> {
65+
_db(db, on: eventLoop).requestConnection()
66+
}
67+
5768
public func db(_ db: DatabaseIdentifier, on eventLoop: EventLoop) -> MySQLDatabase {
69+
_db(db, on: eventLoop)
70+
}
71+
72+
private func _db(_ db: DatabaseIdentifier, on eventLoop: EventLoop) -> _ConnectionPoolMySQLDatabase {
5873
_ConnectionPoolMySQLDatabase(pool: pool(db, for: eventLoop), logger: logger, eventLoop: eventLoop)
5974
}
6075

@@ -79,12 +94,16 @@ private struct _ConnectionPoolMySQLDatabase {
7994

8095
extension _ConnectionPoolMySQLDatabase: MySQLDatabase {
8196
func send(_ command: MySQLCommand, logger: Logger) -> EventLoopFuture<Void> {
82-
self.pool.withConnection(logger: logger) {
97+
pool.withConnection(logger: logger) {
8398
$0.send(command, logger: logger)
8499
}
85100
}
86101

87102
func withConnection<T>(_ closure: @escaping (MySQLConnection) -> EventLoopFuture<T>) -> EventLoopFuture<T> {
88-
self.pool.withConnection(logger: self.logger, closure)
103+
pool.withConnection(logger: logger, closure)
104+
}
105+
106+
func requestConnection() -> EventLoopFuture<MySQLConnection> {
107+
pool.requestConnection(logger: logger)
89108
}
90109
}

0 commit comments

Comments
 (0)