Classes
The following classes are available globally.
-
A connection to a Postgres server.
The
ConnectionConfigurationused to create aConnectionspecifies 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
Cursorto retrieve the rows in the result. - Close the
Cursorand theStatementto release resources on the Postgres server.
A
Statementcan 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
Connectionperforms no more than one SQL statement at a time. WhenConnection.prepareStatement(text:)orStatement.execute(parameterValues:retrieveColumnMetadata:)is called, any previousCursorfor theConnectionis closed.The following methods also close any previous
Cursorfor 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
Statementis 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
Cursoris closed (in any of the ways listed above)
Alternately, transactions can be explicitly demarcated by performing the SQL
BEGIN,COMMIT, andROLLBACKcommands (or equivalently, theConnection.beginTransaction(),Connection.commitTransaction(), andConnection.rollbackTransaction()methods).No more than one thread may concurrently operate against a
Connection(including itsStatementandCursorinstances). However, multiple threads may concurrently operate against different connections.When a
See moreConnectionis no longer required, callConnection.close()to release its Postgres server resources and close the network socket. AConnectionis automatically closed by its deinitializer, or if an unrecoverable error occurs (for example, if the Postgres server closes the network socket). UseConnection.isClosedto test whether aConnectionis closed.Declaration
Swift
public class Connection : CustomStringConvertible - Call
-
A pool of re-usable, interchangeable
Connectioninstances.Using a
ConnectionPoolallows an application to acquire an existingConnectionfrom the pool, use that connection to perform one or more SQL statements, and then release it for use elsewhere in the application.The
ConnectionPoolConfigurationused to create aConnectionPoolspecifies 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
ConnectionPoolare 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 SQLBEGINcommand with either aCOMMITorROLLBACKcommand (or equivalently, use theConnection.beginTransaction(),Connection.commitTransaction(), andConnection.rollbackTransaction()methods).In general, do not close a
Connectionacquired 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 moreConnectionPoolclass is threadsafe: multiple threads may concurrently operate against aConnectionPoolinstance. 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 thisCursorclass with the SQL cursors created by the SQL DECLARE command. TheCursorclass 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 SQLFETCHcommand.When a
Cursoris no longer required, callCursor.close()to release its Postgres server resources. ACursoris implicitly closed as a side effect of certain operations on a connection; refer to theConnectiondocumentation for a full list. ACursoris also automatically closed by its deinitializer.A
See moreCursorin 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
LogRecorddescribes 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 thelevelof 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.levelLoggeris threadsafe.See moreSee also
Postgres.loggerDeclaration
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
Statementcan be repeatedly executed, and the values of its parameters can be different each time.When a
Statementis no longer required, callStatement.close()to release its Postgres server resources. AStatementis automatically closed by its deinitializer.A
See moreStatementin PostgresClientKit corresponds to a prepared statement on the Postgres server whose name is theidof theStatement.Declaration
Swift
public class Statement : CustomStringConvertible