Class
PostgresConnection
public final class PostgresConnection
A Postgres connection. Use it to run queries against a Postgres server.
Relationships
Nested Types
PostgresConnection.Configuration
A configuration object for a connection
Conforms To
PostgresDatabase
Nested Type Aliases
Properties
eventLoop
public var eventLoop: EventLoop
The underlying EventLoop
of both the connection and its channel.
closeFuture
public var closeFuture: EventLoopFuture<Void>
isClosed
public var isClosed: Bool
Methods
connect(on:configuration:id:logger:)
public static func connect(
on eventLoop: EventLoop,
configuration: PostgresConnection.Configuration,
id connectionID: ID,
logger: Logger
) -> EventLoopFuture<PostgresConnection>
Create a new connection to a Postgres server
Parameters
Name | Type | Description |
---|---|---|
eventLoop | EventLoop |
The |
configuration | PostgresConnection.Configuration |
A |
connectionID | ID |
An |
logger | Logger |
A logger to log background events into |
Returns
A SwiftNIO EventLoopFuture
that will provide a PostgresConnection
at a later point in time.
close()
public func close() -> EventLoopFuture<Void>
Closes the connection to the server.
Returns
An EventLoopFuture that is succeeded once the connection is closed.
connect(to:tlsConfiguration:serverHostname:logger:on:)
@available(*, deprecated,
message: "Use the new connect method that allows you to connect and authenticate in a single step",
renamed: "connect(on:configuration:id:logger:)"
)
public static func connect(
to socketAddress: SocketAddress,
tlsConfiguration: TLSConfiguration? = nil,
serverHostname: String? = nil,
logger: Logger = .init(label: "codes.vapor.postgres"),
on eventLoop: EventLoop
) -> EventLoopFuture<PostgresConnection>
authenticate(username:database:password:logger:)
@available(*, deprecated,
message: "Use the new connect method that allows you to connect and authenticate in a single step",
renamed: "connect(on:configuration:id:logger:)"
)
public func authenticate(
username: String,
database: String? = nil,
password: String? = nil,
logger: Logger = .init(label: "codes.vapor.postgres")
) -> EventLoopFuture<Void>
connect(on:configuration:id:logger:)
public static func connect(
on eventLoop: EventLoop,
configuration: PostgresConnection.Configuration,
id connectionID: ID,
logger: Logger
) async throws -> PostgresConnection
Creates a new connection to a Postgres server.
Parameters
Name | Type | Description |
---|---|---|
eventLoop | EventLoop |
The |
configuration | PostgresConnection.Configuration |
A |
connectionID | ID |
An |
logger | Logger |
A logger to log background events into |
Returns
An established PostgresConnection
asynchronously that can be used to run queries.
query(_:logger:file:line:)
@discardableResult
public func query(
_ query: PostgresQuery,
logger: Logger,
file: String = #file,
line: Int = #line
) async throws -> PostgresRowSequence
Run a query on the Postgres server the connection is connected to.
Parameters
Name | Type | Description |
---|---|---|
query | PostgresQuery |
The |
logger | Logger |
The |
file | String |
The file, the query was started in. Used for better error reporting. |
line | Int |
The line, the query was started in. Used for better error reporting. |
Returns
A PostgresRowSequence
containing the rows the server sent as the query result. The sequence be discarded.
query(_:logger:file:line:)
public func query(
_ query: PostgresQuery,
logger: Logger,
file: String = #file,
line: Int = #line
) -> EventLoopFuture<PostgresQueryResult>
Run a query on the Postgres server the connection is connected to and collect all rows.
Parameters
Name | Type | Description |
---|---|---|
query | PostgresQuery |
The |
logger | Logger |
The |
file | String |
The file, the query was started in. Used for better error reporting. |
line | Int |
The line, the query was started in. Used for better error reporting. |
Returns
An EventLoopFuture, that allows access to the future PostgresQueryResult
.
query(_:logger:file:line:_:)
public func query(
_ query: PostgresQuery,
logger: Logger,
file: String = #file,
line: Int = #line,
_ onRow: @escaping (PostgresRow) throws -> ()
) -> EventLoopFuture<PostgresQueryMetadata>
Run a query on the Postgres server the connection is connected to and iterate the rows in a callback.
Parameters
Name | Type | Description |
---|---|---|
query | PostgresQuery |
The |
logger | Logger |
The |
file | String |
The file, the query was started in. Used for better error reporting. |
line | Int |
The line, the query was started in. Used for better error reporting. |
onRow | @escaping (PostgresRow) throws -> () |
A closure that is invoked for every row. |
Returns
An EventLoopFuture, that allows access to the future PostgresQueryMetadata
.
send(_:logger:)
public func send(
_ request: PostgresRequest,
logger: Logger
) -> EventLoopFuture<Void>
withConnection(_:)
public func withConnection<T>(_ closure: (PostgresConnection) -> EventLoopFuture<T>) -> EventLoopFuture<T>
addListener(channel:handler:)
@discardableResult
public func addListener(channel: String, handler notificationHandler: @escaping (PostgresListenContext, PostgresMessage.NotificationResponse) -> Void) -> PostgresListenContext
Add a handler for NotificationResponse messages on a certain channel. This is used in conjunction with PostgreSQL's LISTEN
/NOTIFY
support: to listen on a channel, you add a listener using this method to handle the NotificationResponse messages, then issue a LISTEN
query to instruct PostgreSQL to begin sending NotificationResponse messages.