-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from vapor/dbkit-gm
dbkit 1.0.0 gm
- Loading branch information
Showing
12 changed files
with
292 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
Sources/PostgreSQL/Connection/PostgreSQLConnection+NotifyAndListen.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import Async | ||
|
||
|
||
extension PostgreSQLConnection { | ||
/// Note: after calling `listen'` on a connection, it can no longer handle other database operations. Do not try to send other SQL commands through this connection afterwards. | ||
/// IAlso, notifications will only be sent for as long as this connection remains open; you are responsible for opening a new connection to listen on when this one closes. | ||
internal func listen(_ channelName: String, handler: @escaping (String) throws -> ()) throws -> Future<Void> { | ||
closeHandlers.append({ conn in | ||
let query = PostgreSQLQuery(query: "UNLISTEN \"\(channelName)\";") | ||
return conn.send([.query(query)], onResponse: { _ in }) | ||
}) | ||
|
||
notificationHandlers[channelName] = { message in | ||
try handler(message) | ||
} | ||
let query = PostgreSQLQuery(query: "LISTEN \"\(channelName)\";") | ||
return queue.enqueue([.query(query)], onInput: { message in | ||
switch message { | ||
case let .notificationResponse(notification): | ||
try self.notificationHandlers[notification.channel]?(notification.message) | ||
default: | ||
break | ||
} | ||
return false | ||
}) | ||
} | ||
|
||
internal func notify(_ channelName: String, message: String) throws -> Future<Void> { | ||
let query = PostgreSQLQuery(query: "NOTIFY \"\(channelName)\", '\(message)';") | ||
return send([.query(query)]).map(to: Void.self, { _ in }) | ||
} | ||
|
||
internal func unlisten(_ channelName: String, unlistenHandler: (() -> Void)? = nil) throws -> Future<Void> { | ||
notificationHandlers.removeValue(forKey: channelName) | ||
let query = PostgreSQLQuery(query: "UNLISTEN \"\(channelName)\";") | ||
return send([.query(query)], onResponse: { _ in unlistenHandler?() }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
Sources/PostgreSQL/Message/PostgreSQLNotificationResponse.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import Foundation | ||
|
||
struct PostgreSQLNotificationResponse: Decodable { | ||
/// The message coming from PSQL | ||
let channel: String | ||
let message: String | ||
init(from decoder: Decoder) throws { | ||
let container = try decoder.singleValueContainer() | ||
_ = try container.decode(Int32.self) | ||
channel = try container.decode(String.self) | ||
message = try container.decode(String.self) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.