Rust

postgres

Module

[Postgres]: the PostgreSQL adapter.

Rust
use utopia_database::Adapter;
use utopia_database::adapter::postgres::Postgres;

let adapter = Postgres::connect("postgres://root:password@localhost:5432/root").await?;
adapter.set_database("appwrite")?;
adapter.set_namespace("_1")?;
adapter.create("appwrite").await?;
Compiles in CI; needs a running service to execute.

Contents

9 submodules, and what the module defines itself.

Defined here
use utopia_database::adapter::postgres::Postgres;
Rust
pub struct Postgres

The PostgreSQL adapter. See the module docs.

postgres::ddl

Own page

Schemas, tables, columns, relationship columns and indexes.

postgres::documents

Own page

Documents: reads by id, creates, updates, upserts, deletes, sequence lookups and atomic increments, with their permission rows.

postgres::find

Own page

Finds, counts and sums: WHERE conditions for every query method (comparisons, LIKE, POSIX regexes, full-text search, JSONB arrays and objects, PostGIS spatial filters), pgvector distance ordering, cursor pagination and the document-level permission filter.

postgres::operators

Own page

Operators: atomic updates as SQL expressions, and their effect on a default value for documents an upsert creates.

postgres::pdo

Own page

The adapter writes SQL with named placeholders (:_uid) and binds values with a PDO parameter type. PDO (ATTR_EMULATE_PREPARES) substitutes the placeholders client side with quoted literals and sends the statement as plain text with PQexec; with ATTR_STRINGIFY_FETCHES every fetched value is a string. Pdo reproduces that, so the server sees the same text and the adapter reads the same values:

use utopia_database::adapter::postgres::pdo::Executed;
Rust
pub struct Executed

A statement's result: what execute() returned, the rows and the row count.

use utopia_database::adapter::postgres::pdo::Pdo;
Rust
pub struct Pdo

The PDO handle: one connection.

use utopia_database::adapter::postgres::pdo::PdoError;
Rust
pub struct PdoError

A driver error (PDOException): its SQLSTATE, the driver's native code (errorInfo[1], 7 for server errors) and its message.

use utopia_database::adapter::postgres::pdo::Statement;
Rust
pub struct Statement

A prepared statement: SQL with named placeholders and bound values.

use utopia_database::adapter::postgres::pdo::ParamType;
Rust
pub enum ParamType

A PDO parameter type (PDO::PARAM_*).

use utopia_database::adapter::postgres::pdo::PdoFailure;
Rust
pub enum PdoFailure

A failed execute(): a driver error, or another exception (a value that cannot be converted to a string).

Also implements From.
use utopia_database::adapter::postgres::pdo::Rendering;
Rust
pub enum Rendering

Why a statement could not be rendered.

use utopia_database::adapter::postgres::pdo::error_of;
Rust
pub fn error_of(outcome: &Outcome, query: &[u8]) -> PdoError

The PDOException of a failed result (pdo_pgsql_error then pdo_handle_error).

use utopia_database::adapter::postgres::pdo::fetch;
Rust
pub fn fetch(oid: u32, value: Option<&Bytes>) -> Value

One fetched value (pgsql_stmt_get_col, then PDO's stringify).

use utopia_database::adapter::postgres::pdo::libpq_message;
Rust
pub fn libpq_message(outcome: &Outcome, query: &[u8]) -> String

The libpq message of an error result (PQresultErrorMessage), as pqBuildErrorMessage3 writes it with the default verbosity, for the query query it answered.

use utopia_database::adapter::postgres::pdo::quote;
Rust
pub fn quote(s: &[u8]) -> Option<Vec<u8>>

$pdo->quote($string) (PQescapeStringConn in quotes): None when the string, up to its first NUL, is not valid UTF-8.

use utopia_database::adapter::postgres::pdo::rows;
Rust
pub fn rows(outcome: &Outcome) -> Vec<Array>

The rows of a result as fetchAll(PDO::FETCH_ASSOC) returns them with ATTR_STRINGIFY_FETCHES.

use utopia_database::adapter::postgres::pdo::to_long;
Rust
pub fn to_long(value: &Value) -> i64

Undocumented.

postgres::spatial

Own page

Geometries: arrays to WKT for PostGIS, and the hex EWKB PostGIS returns (or WKT) back to coordinate arrays.

use utopia_database::adapter::postgres::spatial::array_to_wkt;
Rust
pub fn array_to_wkt(geometry: &Array) -> Result<String>

POINT(x y), LINESTRING(...) or POLYGON((...), ...).

use utopia_database::adapter::postgres::spatial::decode_linestring;
Rust
pub fn decode_linestring(wkb: &[u8]) -> Result<Value>

(Postgres): [[x, y], ...].

use utopia_database::adapter::postgres::spatial::decode_point;
Rust
pub fn decode_point(wkb: &[u8]) -> Result<Value>

(Postgres): [x, y] from WKT or hex EWKB.

use utopia_database::adapter::postgres::spatial::decode_polygon;
Rust
pub fn decode_polygon(wkb: &[u8]) -> Result<Value>

(Postgres): [[[x, y], ...], ...].

use utopia_database::adapter::postgres::spatial::type_from_wkt;
Rust
pub fn type_from_wkt(wkt: &[u8]) -> Result<Vec<u8>>

The lowercase type before (.

postgres::sql

Own page

The building blocks every statement uses: identifiers, table names, tenant conditions, column types, parameter types, statement execution with the statement timeout, and the mapping of driver errors onto the library's errors.

use utopia_database::adapter::postgres::sql::Binds;
Rust
pub struct Binds(pub Vec<(String, Value)>)

Bound values of a statement in the making, by placeholder, in the order they were added ($binds).

use utopia_database::adapter::postgres::sql::bind_typed;
Rust
pub fn bind_typed(stmt: &mut Statement, key: &str, value: Value) -> Result<()>

$stmt->bindValue($key, $value, $this->getPDOType($value)).

use utopia_database::adapter::postgres::sql::filter;
Rust
pub fn filter(value: &str) -> String

Keeps [A-Za-z0-9_-].

use utopia_database::adapter::postgres::sql::has_sequence;
Rust
pub fn has_sequence(document: &Document) -> Result<bool>

!empty($document->getSequence()).

use utopia_database::adapter::postgres::sql::id_value;
Rust
pub fn id_value(document: &Document) -> Value

$document->getId().

use utopia_database::adapter::postgres::sql::internal_key;
Rust
pub fn internal_key(attribute: &str) -> &str

Undocumented.

use utopia_database::adapter::postgres::sql::interpolate;
Rust
pub fn interpolate(value: &Value) -> String

Undocumented.

use utopia_database::adapter::postgres::sql::json_encode;
Rust
pub fn json_encode(value: &Value) -> Value

The JSON text, or false when it fails.

use utopia_database::adapter::postgres::sql::json_text;
Rust
pub fn json_text(value: &Value) -> String

As text ("" when it fails, as interpolating false gives).

use utopia_database::adapter::postgres::sql::pdo_type;
Rust
pub fn pdo_type(value: &Value) -> Result<ParamType>

Undocumented.

use utopia_database::adapter::postgres::sql::process_exception;
Rust
pub fn process_exception(e: PdoError) -> Error

(Postgres): driver errors with a known SQLSTATE become the library's errors; others stay PDOExceptions.

use utopia_database::adapter::postgres::sql::processed;
Rust
pub fn processed(e: PdoFailure) -> Error

A failed statement through processException.

use utopia_database::adapter::postgres::sql::quote;
Rust
pub fn quote(s: &str) -> String

"string".

use utopia_database::adapter::postgres::sql::raw;
Rust
pub fn raw(e: PdoFailure) -> Error

A failed statement as it escapes unprocessed (PDOException).

use utopia_database::adapter::postgres::sql::row_to_document;
Rust
pub fn row_to_document(mut row: Array, find: bool) -> Result<Document>

A fetched row as a document: internal columns renamed to their $ attributes, each renamed key moving to the end, in the order getDocument() (find = false) or find() renames them.

use utopia_database::adapter::postgres::sql::sequence;
Rust
pub fn sequence(document: &Document) -> Result<Option<Value>>

$document->getSequence(): null, or the value as a string (the return type coerces it; an array is a TypeError).

use utopia_database::adapter::postgres::sql::short_key;
Rust
pub fn short_key(key: &str) -> String

Keys longer than 63 bytes become their MD5, with the part after the last _ kept as a suffix when it fits.

use utopia_database::adapter::postgres::sql::spatial_attributes;
Rust
pub fn spatial_attributes(collection: &Document) -> Vec<String>

Ids of the collection's spatial attribute documents.

use utopia_database::adapter::postgres::sql::sql_type;
Rust
pub fn sql_type(kind: &str, size: i64, _signed: bool, array: bool, _required: bool) -> Result<String>

Undocumented.

use utopia_database::adapter::postgres::sql::string_attribute;
Rust
pub fn string_attribute(document: &Document, key: &str, getter: &str) -> Result<Value>

A ?string getter of a document (getCreatedAt(), getUpdatedAt()): the raw value coerced to a string.

use utopia_database::adapter::postgres::sql::tenant_value;
Rust
pub fn tenant_value(document: &Document) -> Value

$document->getTenant().

use utopia_database::adapter::postgres::sql::value_name;
Rust
pub fn value_name(v: &Value) -> &str

How a TypeError names a value's type.

use utopia_database::adapter::postgres::sql::violated_columns;
Rust
pub fn violated_columns(message: &str) -> Option<Vec<String>>

The sorted columns of Key (a, b)=(...), or None when the message has none.

Rust
pub const MAX_IDENTIFIER_NAME: usize = 63

Postgres::MAX_IDENTIFIER_NAME.

Rust
pub const MAX_VARCHAR_LENGTH: i64 = 16381

Undocumented.

postgres::widths

Own page

Display widths of Unicode characters, as libpq computes them for the caret under an error position (ucs_wcwidth in PostgreSQL 18's src/common/wchar.c, over the tables PostgreSQL generates from Unicode).

Rust
pub const EAST_ASIAN_FW: &[(u32, u32)] = &[ (0x1100, 0x115F), (0x231A, 0x231B), (0x2329, 0x232A), (0x23E9, 0x23EC), (0x23F0, 0x23F0), (0x23F3, 0x23F3), (0x25FD, 0x25FE), (0x2614, 0x2615), (0x2630, 0x2637), (0x2648, 0x2653), (0x267F, 0x267F), (0x268A, 0x268F), (0x2693, 0x2693), (0x26A1, 0x26A1), (0x26AA, 0x26AB), (0x26BD, 0x26BE), (0x26C4, 0x26C5), (0x26CE, 0x26CE), (0x26D4, 0x26D4), (0x26EA, 0x26EA), (0x26F2, 0x26F3), (0x26F5, 0x26F5), (0x26FA, 0x26FA), (0x26FD, 0x26FD), (0x2705, 0x2705), (0x270A, 0x270B), (0x2728, 0x2728), (0x274C, 0x274C), (0x274E, 0x274E),

East Asian wide and fullwidth characters (width 2): unicode_east_asian_fw_table.h.

Rust
pub const NONSPACING: &[(u32, u32)] = &[ (0x00AD, 0x00AD), (0x0300, 0x036F), (0x0483, 0x0489), (0x0591, 0x05BD), (0x05BF, 0x05BF), (0x05C1, 0x05C2), (0x05C4, 0x05C5), (0x05C7, 0x05C7), (0x0600, 0x0605), (0x0610, 0x061A), (0x061C, 0x061C), (0x064B, 0x065F), (0x0670, 0x0670), (0x06D6, 0x06DD), (0x06DF, 0x06E4), (0x06E7, 0x06E8), (0x06EA, 0x06ED), (0x070F, 0x070F), (0x0711, 0x0711), (0x0730, 0x074A), (0x07A6, 0x07B0), (0x07EB, 0x07F3), (0x07FD, 0x07FD), (0x0816, 0x0819), (0x081B, 0x0823), (0x0825, 0x0827), (0x0829, 0x082D), (0x0859, 0x085B), (0x0890, 0x089F),

Non-spacing characters (width 0): unicode_nonspacing_table.h.

postgres::wire

Own page

The PostgreSQL frontend/backend protocol, as libpq's PQexec speaks it.

Wire is one server connection: it authenticates (trust, cleartext, MD5 or SCRAM-SHA-256), then runs statements with the simple query protocol, keeping the transaction status every ReadyForQuery reports. Statements are bytes, sent as-is up to their first NUL (a C string, as libpq sends them), and results keep the bytes the server sent, with the type of each column, so the adapter can read rows exactly as PDO's pgsql driver does.

use utopia_database::adapter::postgres::wire::Column;
Rust
pub struct Column

A column of a result: its name and type OID.

use utopia_database::adapter::postgres::wire::Config;
Rust
pub struct Config

Where and as whom to connect.

use utopia_database::adapter::postgres::wire::Outcome;
Rust
pub struct Outcome

One result (PGresult): the last of a query string, as PQexec keeps.

use utopia_database::adapter::postgres::wire::Wire;
Rust
pub struct Wire

A server connection.

use utopia_database::adapter::postgres::wire::Status;
Rust
pub enum Status

What PQresultStatus() reports for a result.

use utopia_database::adapter::postgres::wire::TxStatus;
Rust
pub enum TxStatus

PQtransactionStatus().