class CQL::Postgres::Config

Overview

Everything needed to open a connection. The password never appears in #inspect, #to_s or error messages.

Query-string keys other than the ones listed here are sent to the server as startup parameters (?search_path=app). #user, #database, client_encoding and replication cannot be set that way, and libpq-only options the driver does not honour are rejected instead of ignored, so a typo can never silently weaken a connection. crystal-db's pool keys, which CQL.configure adds, are read too: max_pool_size and initial_pool_size as pool_size and pool_min_size, and the rest are ignored.

sslmode=prefer and require encrypt without verifying the server, like libpq, so sslrootcert is only accepted with verify-full, where it adds a CA to the system trust store (Crystal's TLS context cannot drop the system roots). Query values are percent-decoded only; + stays a plus.

Defined in:

postgres/config.cr

Constant Summary

COMPAT_IGNORED_KEYS = {"auth_methods", "max_idle_pool_size", "retry_attempts", "retry_delay", "prepared_statements", "prepared_statements_cache"}

Accepted for configuration written for the pg shard or crystal-db's pool (CQL.configure emits both): this driver negotiates SCRAM/MD5 itself, and its pool has no idle cap, connect retries or unprepared mode.

CRYSTAL_DB_POOL_KEYS = {"max_pool_size" => "pool_size", "initial_pool_size" => "pool_min_size"}

crystal-db pool keys with an equivalent here; the native key wins when the URL names both.

DEFAULT_CONNECT_WAIT = 10.0
DRIVER_OPTION_KEYS = POOL_OPTION_KEYS + {"min_protocol_version", "max_protocol_version", "binary_params"}
POOL_OPTION_KEYS = {"pool_size", "pool_min_size", "checkout_timeout", "health_check_after", "max_queries", "max_idle_lifetime"}
RESERVED_PARAMETERS = {"user", "database", "dbname", "password", "hostaddr", "client_encoding", "replication"}
SCHEMES = {"postgres", "postgresql", "cockroachdb"}
UNSUPPORTED_LIBPQ = {"sslcert", "sslkey", "sslcrl", "sslcompression", "sslsni", "sslpassword", "channel_binding", "require_auth", "target_session_attrs", "passfile", "service", "gssencmode", "krbsrvname"}

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.from_uri(uri : URI, user : String | Nil = uri.user.presence, password : String | Nil = uri.password, ignoring : Enumerable(String) = [] of String) : Config #

For callers that only hold a parsed URI (crystal-db's driver interface). URI.parse has already decoded the user info, and reads + as a space, so write a literal plus as %2B. Query keys in ignoring belong to the caller and are not read as settings; COMPAT_IGNORED_KEYS are always dropped.


[View source]
def self.new(host : String = "localhost", port : Int32 = 5432, user : String = "postgres", password : Nil | String = nil, database : String | Nil = nil, ssl_mode : CQL::Postgres::SSLMode = SSLMode::Prefer, ssl_root_cert : Nil | String = nil, connect_timeout : Time::Span | Nil = DEFAULT_CONNECT_WAIT.seconds, application_name : String = "cql", statement_cache_size : Int32 = 256, startup_options : Hash(String, String) = {} of String => String, driver_options : Hash(String, String) = {} of String => String, cockroachdb : Bool = false) #

[View source]
def self.parse(url : String) : Config #

Parses postgres://user:pass@host:5432/db?sslmode=require&application_name=app. A host starting with / (percent-encoded in the authority, or given as ?host=) is the directory of a Unix socket.


[View source]

Class Method Detail

def self.number(text : String | Nil, name : String) : Int32 | Nil #

[View source]
def self.seconds(text : String | Nil, name : String) : Time::Span | Nil #

A positive or zero count of seconds (at most a year), or nil when absent.


[View source]
def self.whole(text : String | Nil, name : String) : Int64 | Nil #

A whole number that may not fit in 32 bits, or nil when absent.


[View source]

Instance Method Detail

def address : String #

Where the connection goes, for error messages.


[View source]
def application_name : String #

[View source]
def binary_params? : Bool #

Send bind parameters in PostgreSQL's binary format where the server's parameter type has one (off by default; see the README).


[View source]
def cockroachdb? : Bool #

The URL said cockroachdb://.


[View source]
def connect_timeout : Time::Span | Nil #

How long each step of connecting and starting up may block (TCP connect, TLS, every server reply), not a total deadline; nil waits indefinitely.


[View source]
def database : String #

[View source]
def driver_options : Hash(String, String) #

[View source]
def host : String #

[View source]
def inspect(io : IO) : Nil #
Description copied from class Reference

Appends a String representation of this object which includes its class name, its object address and the values of all instance variables.

class Person
  def initialize(@name : String, @age : Int32)
  end
end

Person.new("John", 32).inspect # => #<Person:0x10fd31f20 @name="John", @age=32>

[View source]
def max_protocol_version : ProtocolVersion #

[View source]
def min_protocol_version : ProtocolVersion #

The protocol versions this connection may use. It asks for the newest and settles for what the server offers, unless that is older than the minimum.


[View source]
def password_for_authentication : String #

[View source]
def port : Int32 #

[View source]
def socket_path : String #

[View source]
def ssl_mode : SSLMode #

[View source]
def ssl_root_cert : String | Nil #

[View source]
def startup_options : Hash(String, String) #

[View source]
def startup_parameters : Hash(String, String) #

[View source]
def statement_cache_size : Int32 #

[View source]
def to_s(io : IO) : Nil #
Description copied from class Reference

Appends a short String representation of this object which includes its class name and its object address.

class Person
  def initialize(@name : String, @age : Int32)
  end
end

Person.new("John", 32).to_s # => #<Person:0x10a199f20>

[View source]
def unix_socket? : Bool #

[View source]
def user : String #

[View source]