pgl
PostgreSQL database client
Types
pub type Config {
Config(
application: String,
host: String,
port: Int,
username: String,
password: String,
database: String,
connection_parameters: List(#(String, String)),
ssl: Ssl,
rows_as_dict: Bool,
ip_version: IpVersion,
pool_size: Int,
idle_interval: Int,
queue_target: Int,
)
}
Constructors
-
Config( application: String, host: String, port: Int, username: String, password: String, database: String, connection_parameters: List(#(String, String)), ssl: Ssl, rows_as_dict: Bool, ip_version: IpVersion, pool_size: Int, idle_interval: Int, queue_target: Int, )Arguments
- application
-
Application’s name.
- host
-
(default: 127.0.0.1) Database server hostname.
- port
-
(default: 5432) Database server port.
- username
-
Database username.
- password
-
Database user password.
- database
-
Database to use.
- connection_parameters
-
Other Postgres connection parameters.
- ssl
-
(default: SslDisabled) SSL enabled or disabled.
- rows_as_dict
-
(default: False) Return rows as
Dictor n-tuple. - ip_version
-
(default: Ipv4) The IP version to use
- pool_size
-
(default: 1) Connection pool size.
- idle_interval
-
(default: 1000) Idle connections ping the database every
idle_interval. - queue_target
-
(default: 50) How long checking out a connection should take.
A Connection that can be reference to the connection pool, or a
single connection.
pub opaque type Connection
A configured Db. Must be started via pgl.start or pgl.supervised
before use. Once started, Db can be passed to with_connection.
pub opaque type Db
Error and notice message fields
pub type Field {
Severity
Code
Message
Detail
Hint
Position
InternalPosition
InternalQuery
Where
Schema
Table
Column
DataType
Constraint
File
Line
Routine
Other(BitArray)
}
Constructors
-
Severity -
Code -
Message -
Detail -
Hint -
Position -
InternalPosition -
InternalQuery -
Where -
Schema -
Table -
Column -
DataType -
Constraint -
File -
Line -
Routine -
Other(BitArray)
pub type PglError {
QueryError(message: String)
ConnectionError(message: String)
ConnectionTimeout
AuthenticationError(message: String)
ProtocolError(message: String)
SocketError(message: String)
PostgresError(
code: String,
name: String,
message: String,
fields: dict.Dict(Field, String),
)
}
Constructors
-
QueryError(message: String) -
ConnectionError(message: String) -
ConnectionTimeout -
AuthenticationError(message: String) -
ProtocolError(message: String) -
SocketError(message: String) -
Queried captures the number of rows queried, the fields that
were queried, and rows returned by the query. The rows returned
need to be decoded.
pub type Queried {
Queried(
count: Int,
fields: List(String),
rows: List(dynamic.Dynamic),
)
}
Constructors
-
Queried( count: Int, fields: List(String), rows: List(dynamic.Dynamic), )
Holds a SQL string and a list of query parameters.
pub type Query {
Query(sql: String, params: List(pg_value.Value))
}
Constructors
-
Query(sql: String, params: List(pg_value.Value))
pub type Ssl {
SslDisabled
SslVerified
SslUnverified
}
Constructors
-
SslDisabledDisables SSL leaving connections unsecured. Avoid using this in production.
-
SslVerifiedEnables SSL and checks the CA certificate.
-
SslUnverifiedEnables SSL but does not check the CA certificate.
pub type TransactionError(error) {
RollbackError(cause: error)
NotInTransaction
TransactionError(message: String)
}
Constructors
-
RollbackError(cause: error) -
NotInTransaction -
TransactionError(message: String)
Values
pub fn application(conf: Config, application: String) -> Config
Name of the application connecting to the database.
pub fn batch(
queries: List(Query),
connection: Connection,
) -> Result(List(Queried), PglError)
Uses pipelining to send multiple queries to the database server without waiting for previous queries to complete. Reduces the number of network roundtrips needed for multiple queries.
pub fn begin(
connection: Connection,
) -> Result(Connection, TransactionError(error))
Begins a transaction.
You should typically use pgl.transaction to take care of starting and
ending transactions.
pub fn commit(
connection: Connection,
) -> Result(Connection, TransactionError(error))
Commits a transaction.
You should typically use pgl.transaction to take care of starting and
ending transactions.
pub fn connection_parameter(
conf: Config,
name name: String,
value value: String,
) -> Config
Sets other postgres connection parameters.
pub const default: Config
A default configuration with a connection pool size of 1. At minimum you need to set the username, password, and database values.
pub fn error_to_string(err: PglError) -> String
Returns a formatted string representation of the provided error.
pub fn execute(
sql: String,
on connection: Connection,
) -> Result(Int, PglError)
Perform a query with the given SQL string. This function will send the SQL string as is to the postgres database server.
pub fn from_url(url: String) -> Result(Config, Nil)
Build a Config from a connection url
pub fn idle_interval(conf: Config, idle_interval: Int) -> Config
How often idle connections should ping the database server.
pub fn new(config: Config) -> Db
Creates a new Db record. This must be passed to pgl.start or
pgl.supervised before it can be used.
pub fn params(q: Query, params: List(pg_value.Value)) -> Query
Sets a list of query parameters for the provided Query.
pub fn query(
q: Query,
connection: Connection,
) -> Result(Queried, PglError)
Perform a query with the given SQL string and list of parameters.
pub fn queue_target(conf: Config, queue_target: Int) -> Config
How long it should take to check out a connection from the connection pool.
pub fn rollback(
connection: Connection,
) -> Result(Connection, TransactionError(error))
Rolls back to a savepoint if one exists, otherwise rolls back the transaction.
You should typically use pgl.transaction to take care of starting and
ending transactions.
pub fn rows_as_dict(conf: Config, rows_as_dict: Bool) -> Config
Configures rows to be returns as Dict rather than n-tuples.
pub fn savepoint(
connection: Connection,
next: fn(Connection) -> Result(t, error),
) -> Result(t, TransactionError(error))
Creates a new savepoint.
pub fn start(
db: Db,
) -> Result(
actor.Started(static_supervisor.Supervisor),
actor.StartError,
)
pub fn supervised(
db: Db,
) -> supervision.ChildSpecification(static_supervisor.Supervisor)
pub fn transaction(
connection: Connection,
next: fn(Connection) -> Result(t, error),
) -> Result(t, TransactionError(error))
Starts a transaction and then calls the provided function, passing it the transaction connection. If the given function throws an exception, the transaction will be rolled back the exception propagated up. If the given function returns an error result, the transaction will also be rolled back and an error result returned.