Connection
public final class Connection
Represents a connection to a PostgreSQL server
-
designated initializer.
Declaration
Swift
public init(connectInfo: ConnectInfo)Parameters
connectInfothe arguments that will be used to open a connection
-
Initializes with a fixed set of connection parameters
Declaration
Swift
public convenience init(host: String = "localhost", port: String = "5432", user: String, password: String = "", dbname: String, sslMode: ConnectParam.SSLMode = .prefer)Parameters
hostthe host to connect to. Defaults to
localhost
portThe port to connect to, as a string. Defaults to
5432
userthe user to connect as
passwordthe password for user. Defauls to an empty string
dbnamethe name of the ddatabase to connect to
sslModethe SSL Mode to use. Defaults to .prefer
-
Creates a connection using the same parameters as the specified connection
Declaration
Swift
public convenience init(cloning: Connection)Parameters
cloningThe connection to clone
-
opens the connection to the database server
Declaration
Swift
public func open() throws -
closes the connection to the database server
Declaration
Swift
public func close()
-
the currently reported last error message from the server
Declaration
Swift
public var lastErrorMessage: String { get } -
true if the connection is currently open
Declaration
Swift
public var isConnected: Bool { get } -
Returns the version of the server. Only works if a connection is open.
Declaration
Swift
public func serverVersion() throws -> StringReturn Value
the server version string
-
Wraps a closure in a transaction. If an error is thrown, performs a rollback. Otherwise, performs a commit.
Throws
any errors executing the queryDeclaration
Swift
public func withTransaction<T>(body: (Connection) throws -> T?) throws -> T?Parameters
bodyclosure to execute. Passed the connection to use
Return Value
the value returned from the body closure
-
Executes the query and returns the results
Throws
if connection isn’t open, or don’t get a valid responseDeclaration
Swift
@discardableResult public func execute(query: String) throws -> PGResultParameters
queryquery to perform
Return Value
the results of that query
-
Execute a query with parameters
Throws
if connection not open, don’t get a valid response, query parameter mismatchDeclaration
Swift
@discardableResult public func execute(query: String, parameters: [QueryParameter?]) throws -> PGResultParameters
querythe query string with placeholders ($1,$2, etc.) for each parameter
parametersarray of QueryParameters
Return Value
the results of the query
-
Execute the query and returns the results. Internally transfers data in binary format
Throws
if connection isn’t open, or don’t get a valid responseDeclaration
Swift
@discardableResult public func executeBinary(query: String) throws -> PGResultParameters
queryquery to perform
Return Value
the results of that query
-
Returns the value of row 0, column 0 of a query that returns 1 row and 1 column
Throws
if the data types don’t match, or an error executing queryDeclaration
Swift
public func getSingleRowValue<T>(query: String) throws -> T?Parameters
querya query that should return 1 row with 1 column
Return Value
the value
-
Creates a dispatch read source for this connection that will call
callbackonqueuewhen a notification is receivedThrows
if fails to get the socket for the connectionDeclaration
Swift
public func listen(toChannel channel: String, queue: DispatchQueue, callback: @escaping (_ notification: PGNotification?, _ error: Error?) -> Void) throws -> DispatchSourceReadParameters
channelthe channel to register for
queuethe queue to create the DispatchSource on
callbackthe callback
notificationThe notification received from the database
errorAny error while reading the notification. If not nil, the source will have been canceled
Return Value
the dispatch socket to activate
View on GitHub
Connection Class Reference