PostgreSQLConnection constructor

PostgreSQLConnection(
  1. String host,
  2. int port,
  3. String databaseName, {
  4. Encoding encoding = utf8,
  5. String? username,
  6. String? password,
  7. int timeoutInSeconds = 30,
  8. int queryTimeoutInSeconds = 30,
  9. Object timeZone = 'UTC',
  10. bool useSSL = false,
  11. bool isUnixSocket = false,
  12. bool allowClearTextPassword = false,
  13. ReplicationMode replicationMode = ReplicationMode.none,
  14. DecodingSettings decodingSettings = const DecodingSettings(),
  15. String? applicationName,
  16. Map<String, String> startupParameters = const {},
  17. PgSslMode? sslMode,
  18. SecurityContext? securityContext,
  19. bool cancelQueryOnTimeout = false,
  20. int? maxPreparedStatements,
})

Creates an instance of PostgreSQLConnection.

host must be a hostname, e.g. "foobar.com" or IP address. Do not include scheme or port. port is the port to connect to the database on. It is typically 5432 for default PostgreSQL settings. databaseName is the name of the database to connect to. username and password are optional if the database requires user authentication. timeoutInSeconds refers to the amount of time PostgreSQLConnection will wait while establishing a connection before it gives up. queryTimeoutInSeconds refers to the default timeout for PostgreSQLExecutionContext's execute and query methods. timeZone is the timeZone the connection is in. Defaults to 'UTC'. useSSL when true, uses a secure socket when connecting to a PostgreSQL database. allowClearTextPassword when true, allows sending the password during authentication in clear text. Use only when required by the database server and under encrypted connections, this feature may lead to security issues. timeZone = default = TimeZoneSettings('UTC') | String 'UTC' decodingSettings controls how some types (interval, time, timetz, inet and cidr) are decoded; see DecodingSettings. applicationName and startupParameters are session parameters sent in the startup message (for example search_path or statement_timeout), avoiding SET commands after connecting. sslMode chooses how SSL is used (see PgSslMode); when null, useSSL decides as before. securityContext holds the CA certificates that verify the server with PgSslMode.verifyFull (the system roots by default) and the client certificate, when the server asks for one. It requires SSL: with PgSslMode.disable it throws ArgumentError. cancelQueryOnTimeout also cancels the query on the server when it exceeds its timeout (see cancelQuery). maxPreparedStatements limits the prepared statements kept by this connection (least recently used are closed); the server may keep one extra statement until the next extended-protocol query. Default: no limit.

Implementation

PostgreSQLConnection(
  this.host,
  this.port,
  this.databaseName, {
  this.encoding = utf8,
  this.username,
  this.password,
  this.timeoutInSeconds = 30,
  this.queryTimeoutInSeconds = 30,
  Object timeZone = 'UTC',
  this.useSSL = false,
  this.isUnixSocket = false,
  this.allowClearTextPassword = false,
  this.replicationMode = ReplicationMode.none,
  this.decodingSettings = const DecodingSettings(),
  this.applicationName,
  Map<String, String> startupParameters = const {},
  this.sslMode,
  this.securityContext,
  this.cancelQueryOnTimeout = false,
  int? maxPreparedStatements,
})  : startupParameters = Map.unmodifiable(startupParameters),
      _cache = QueryCache(maxSize: maxPreparedStatements) {
  if (maxPreparedStatements != null && maxPreparedStatements < 1) {
    throw ArgumentError.value(maxPreparedStatements, 'maxPreparedStatements',
        'Must be at least 1');
  }
  if (securityContext != null && effectiveSslMode == PgSslMode.disable) {
    throw ArgumentError.value(securityContext, 'securityContext',
        'Requires SSL, but the SSL mode is disable');
  }
  _checkStartupParameters(this.startupParameters);
  if (timeZone is String) {
    this.timeZone = TimeZoneSettings(timeZone);
  } else if (timeZone is TimeZoneSettings) {
    this.timeZone = timeZone;
  }

  _connectionState = _PostgreSQLConnectionStateClosed();
  _connectionState.connection = this;
}