Classes
The following classes are available globally.
-
A connection to a Postgres server.
The
ConnectionConfiguration
used to create aConnection
specifies the hostname and port number of the Postgres server, the username and database to use, and other characteristics of the connection.Connections are used to perform SQL statements. To perform a SQL statement:
- Call
Connection.prepareStatement(text:)
to parse the SQL text and return aStatement
. - Call
Statement.execute(parameterValues:retrieveColumnMetadata:)
to execute the statement and return aCursor
. - Iterate over the
Cursor
to retrieve the rows in the result. - Close the
Cursor
and theStatement
to release resources on the Postgres server.
A
Statement
can be repeatedly executed, and the values of its parameters can be different each time. This is more efficient than having the Postgres server repeatedly parse the same SQL text.A
Connection
performs no more than one SQL statement at a time. WhenConnection.prepareStatement(text:)
orStatement.execute(parameterValues:retrieveColumnMetadata:)
is called, any previousCursor
for theConnection
is closed.The following methods also close any previous
Cursor
for theConnection
:Connection.beginTransaction()
Connection.commitTransaction()
Connection.rollbackTransaction()
Statement.close()
Cursor.close()
To concurrently perform more than one SQL statement, use multiple connections.
By default, each
Statement
is executed in its own transaction. The statement’s transaction is automatically rolled back if an error occurs in executing the statement, and is automatically commited upon any of the following events:- if there are no rows in the result: upon completion of
Statement.execute()
- if the result has one or more rows: after the final row has been retrieved (in other words,
when
Cursor.next()
returnsnil
) - when the
Cursor
is closed (in any of the ways listed above)
Alternately, transactions can be explicitly demarcated by performing the SQL
BEGIN
,COMMIT
, andROLLBACK
commands (or equivalently, theConnection.beginTransaction()
,Connection.commitTransaction()
, andConnection.rollbackTransaction()
methods).No more than one thread may concurrently operate against a
Connection
(including itsStatement
andCursor
instances). However, multiple threads may concurrently operate against different connections.When a
See moreConnection
is no longer required, callConnection.close()
to release its Postgres server resources and close the network socket. AConnection
is automatically closed by its deinitializer, or if an unrecoverable error occurs (for example, if the Postgres server closes the network socket). UseConnection.isClosed
to test whether aConnection
is closed.Declaration
Swift
public class Connection : CustomStringConvertible
- Call
-
A pool of re-usable, interchangeable
Connection
instances.Using a
ConnectionPool
allows an application to acquire an existingConnection
from the pool, use that connection to perform one or more SQL statements, and then release it for use elsewhere in the application.The
ConnectionPoolConfiguration
used to create aConnectionPool
specifies the number of connections in the pool, how long a request for a connection will wait for a connection to become available, and other characteristics of the pool.All connections in a
ConnectionPool
are created from the sameConnectionConfiguration
. They also have the sameConnectionDelegate
(if a delegate is specified). Consequently any connection in a pool is interchangeable with any other.Use
ConnectionPool.acquireConnection(completionHandler:)
to request a connection from aConnectionPool
. This method is non-blocking: its completion handler is asynchronously executed when a connection is successfully allocated to the request or if an error occurs. To release the connection back to the pool, callConnectionPool.releaseConnection(_:)
.Alternately, use
ConnectionPool.withConnection(completionHandler:)
to acquire a connection that is automatically released after execution of the completion handler.When a connection is released to a
ConnectionPool
, there should be no transaction underway. Pair each SQLBEGIN
command with either aCOMMIT
orROLLBACK
command (or equivalently, use theConnection.beginTransaction()
,Connection.commitTransaction()
, andConnection.rollbackTransaction()
methods).In general, do not close a
Connection
acquired from aConnectionPool
. If a connection is closed (whether explicitly or because of an unrecoverable error) then, when that connection is released, it will be discarded from the pool, allowing a new connection to be created and added to the pool.The
See moreConnectionPool
class is threadsafe: multiple threads may concurrently operate against aConnectionPool
instance. Connections acquired from the pool are subject to the threadsafety constraints described by the API documentation forConnection
.Declaration
Swift
public class ConnectionPool
-
The result of successfully executing a
Statement
.Note
Do not confuse thisCursor
class with the SQL cursors created by the SQL DECLARE command. TheCursor
class exposes the result of executing a single SQL command. A SQL cursor, on other other hand, exposes a query’s result by repeated execution of the SQLFETCH
command.When a
Cursor
is no longer required, callCursor.close()
to release its Postgres server resources. ACursor
is implicitly closed as a side effect of certain operations on a connection; refer to theConnection
documentation for a full list. ACursor
is also automatically closed by its deinitializer.A
See moreCursor
in PostgresClientKit corresponds to the unnamed portal of the connection on the Postgres server.Declaration
Swift
public class Cursor : Sequence, IteratorProtocol
-
A log handler that prints log records to standard output.
See moreDeclaration
Swift
public class ConsoleLogHandler : LogHandler
-
Logs events of interest.
A
LogRecord
describes a loggable event. Each record has a level which reflects the importance of the event. If the log level of a record is at least as high as thelevel
of the logger, the logger dispatches the record to the logger’s currenthandler
.Example:
let logger = Logger() logger.level = .warning logger.handler = ConsoleLogHandler() let record = LogRecord(level: .warning, message: "Watch out!", context: "Session-14", timestamp: Date(), file: #file, function: #function, line: #line) logger.log(record) // the record is logged (because LogLevel.warning >= logger.level) // Convenience methods make logging more concise. logger.warning("Watch out!", context: "Session-14") // Examples of other log levels: logger.severe("This is also logged") // because LogLevel.severe >= logger.level logger.info("This is not logged") // because LogLevel.info < logger.level
Logger
is threadsafe.See also
Postgres.logger
Declaration
Swift
public class Logger
-
A prepared SQL statement.
Use
Connection.prepareStatement(text:)
to create aStatement
.Call
Statement.execute(parameterValues:retrieveColumnMetadata:)
to execute theStatement
, specifying the values of any parameters.A
Statement
can be repeatedly executed, and the values of its parameters can be different each time.When a
Statement
is no longer required, callStatement.close()
to release its Postgres server resources. AStatement
is automatically closed by its deinitializer.A
See moreStatement
in PostgresClientKit corresponds to a prepared statement on the Postgres server whose name is theid
of theStatement
.Declaration
Swift
public class Statement : CustomStringConvertible