postgres
Module[Postgres]: the PostgreSQL adapter.
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?;Contents
9 submodules, and what the module defines itself.
use utopia_database::adapter::postgres::Postgres;pub struct PostgresThe PostgreSQL adapter. See the module docs.
postgres::ddl
Own pageSchemas, tables, columns, relationship columns and indexes.
postgres::documents
Own pageDocuments: reads by id, creates, updates, upserts, deletes, sequence lookups and atomic increments, with their permission rows.
postgres::find
Own pageFinds, 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 pageOperators: atomic updates as SQL expressions, and their effect on a default value for documents an upsert creates.
postgres::pdo
Own pageThe 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;pub struct ExecutedA statement's result: what execute() returned, the rows and the row count.
use utopia_database::adapter::postgres::pdo::Pdo;pub struct PdoThe PDO handle: one connection.
use utopia_database::adapter::postgres::pdo::PdoError;pub struct PdoErrorA 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;pub struct StatementA prepared statement: SQL with named placeholders and bound values.
use utopia_database::adapter::postgres::pdo::ParamType;pub enum ParamTypeA PDO parameter type (PDO::PARAM_*).
use utopia_database::adapter::postgres::pdo::PdoFailure;pub enum PdoFailureA failed execute(): a driver error, or another exception (a value
that cannot be converted to a string).
From.use utopia_database::adapter::postgres::pdo::Rendering;pub enum RenderingWhy a statement could not be rendered.
use utopia_database::adapter::postgres::pdo::error_of;pub fn error_of(outcome: &Outcome, query: &[u8]) -> PdoErrorThe PDOException of a failed result (pdo_pgsql_error then
pdo_handle_error).
use utopia_database::adapter::postgres::pdo::fetch;pub fn fetch(oid: u32, value: Option<&Bytes>) -> ValueOne fetched value (pgsql_stmt_get_col, then PDO's stringify).
use utopia_database::adapter::postgres::pdo::libpq_message;pub fn libpq_message(outcome: &Outcome, query: &[u8]) -> StringThe 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;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;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;pub fn to_long(value: &Value) -> i64Undocumented.
postgres::spatial
Own pageGeometries: 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;pub fn array_to_wkt(geometry: &Array) -> Result<String>POINT(x y), LINESTRING(...) or
POLYGON((...), ...).
use utopia_database::adapter::postgres::spatial::decode_linestring;pub fn decode_linestring(wkb: &[u8]) -> Result<Value>(Postgres): [[x, y], ...].
use utopia_database::adapter::postgres::spatial::decode_point;pub fn decode_point(wkb: &[u8]) -> Result<Value>(Postgres): [x, y] from WKT or hex EWKB.
use utopia_database::adapter::postgres::spatial::decode_polygon;pub fn decode_polygon(wkb: &[u8]) -> Result<Value>(Postgres): [[[x, y], ...], ...].
use utopia_database::adapter::postgres::spatial::type_from_wkt;pub fn type_from_wkt(wkt: &[u8]) -> Result<Vec<u8>>The lowercase type before (.
postgres::sql
Own pageThe 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;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;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;pub fn filter(value: &str) -> StringKeeps [A-Za-z0-9_-].
use utopia_database::adapter::postgres::sql::has_sequence;pub fn has_sequence(document: &Document) -> Result<bool>!empty($document->getSequence()).
use utopia_database::adapter::postgres::sql::id_value;pub fn id_value(document: &Document) -> Value$document->getId().
use utopia_database::adapter::postgres::sql::internal_key;pub fn internal_key(attribute: &str) -> &strUndocumented.
use utopia_database::adapter::postgres::sql::interpolate;pub fn interpolate(value: &Value) -> StringUndocumented.
use utopia_database::adapter::postgres::sql::json_encode;pub fn json_encode(value: &Value) -> ValueThe JSON text, or false when it fails.
use utopia_database::adapter::postgres::sql::json_text;pub fn json_text(value: &Value) -> StringAs text ("" when it fails, as interpolating
false gives).
use utopia_database::adapter::postgres::sql::pdo_type;pub fn pdo_type(value: &Value) -> Result<ParamType>Undocumented.
use utopia_database::adapter::postgres::sql::process_exception;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;pub fn processed(e: PdoFailure) -> ErrorA failed statement through processException.
use utopia_database::adapter::postgres::sql::quote;pub fn quote(s: &str) -> String"string".
use utopia_database::adapter::postgres::sql::raw;pub fn raw(e: PdoFailure) -> ErrorA failed statement as it escapes unprocessed (PDOException).
use utopia_database::adapter::postgres::sql::row_to_document;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;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;pub fn short_key(key: &str) -> StringKeys 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;pub fn spatial_attributes(collection: &Document) -> Vec<String>Ids of the collection's spatial attribute documents.
use utopia_database::adapter::postgres::sql::sql_type;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;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;pub fn tenant_value(document: &Document) -> Value$document->getTenant().
use utopia_database::adapter::postgres::sql::value_name;pub fn value_name(v: &Value) -> &strHow a TypeError names a value's type.
use utopia_database::adapter::postgres::sql::violated_columns;pub fn violated_columns(message: &str) -> Option<Vec<String>>The sorted columns of
Key (a, b)=(...), or None when the message has none.
pub const MAX_IDENTIFIER_NAME: usize = 63Postgres::MAX_IDENTIFIER_NAME.
pub const MAX_VARCHAR_LENGTH: i64 = 16381Undocumented.
postgres::widths
Own pageDisplay 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).
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.
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 pageThe 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;pub struct ColumnA column of a result: its name and type OID.
use utopia_database::adapter::postgres::wire::Config;pub struct ConfigWhere and as whom to connect.
use utopia_database::adapter::postgres::wire::Outcome;pub struct OutcomeOne result (PGresult): the last of a query string, as PQexec keeps.
use utopia_database::adapter::postgres::wire::Wire;pub struct WireA server connection.
use utopia_database::adapter::postgres::wire::Status;pub enum StatusWhat PQresultStatus() reports for a result.
use utopia_database::adapter::postgres::wire::TxStatus;pub enum TxStatusPQtransactionStatus().