std.sql

PostgreSQL connections, queries, rows, pools, and transactions.

Importuse std.sql

Types

enum SqlValue#

One column value in a SQL row, in the driver's own type space.

PgArray is an internal marker std.sql.pg_array returns; it stays an ordinary variant so the value survives query construction on every backend.

use std.sql
use std.sql.SqlValue

fn main() {
  let query = sql
    .select("videos", ["id", "title"])
    .where(sql.and([
      sql.gte("year", SqlValue.Int(2020)),
      sql.ilike("title", SqlValue.Text("%moon%")),
    ]))
    .order_by("year", "desc", "last")
    .limit(20)
    .offset(0)
  let insert = sql
    .insert_into("videos", ["title", "year"])
    .values([SqlValue.Text("moon"), SqlValue.Int(2026)])
    .returning(["id"])
    .on_conflict_do_nothing(["title"])
  println([query, insert].len())
  println(sql.pg_array([SqlValue.Int(1), SqlValue.Int(2)]))
}
  • Null
  • Bool(bool)
  • Int(int)
  • Float(float)
  • Text(string)
  • Bytes([int])
  • DateTime(DateTime)
  • Json(Json)
  • Decimal(string)
  • Uuid(string)
  • PgArray(string)
struct SqlColumn#

PostgreSQL result-column metadata.

name: string
type_oid: int
type_name: string
struct SqlRow#

One typed SQL result row.

use std.sql.SqlRow

struct User { id: int, name: string, active: bool }

fn decode_user(row: SqlRow) -> Result<User, Error> {
  row.decode<User>()
}

fn main() {}
columns: [SqlColumn]
values: [SqlValue]
struct SqlResult#

Materialized SQL command result.

command: string
affected: int
columns: [SqlColumn]
rows: [SqlRow]
struct SqlNotice#

A bounded PostgreSQL NoticeResponse captured during an operation.

severity: string
code: string
message: string
struct SqlNotification#

A PostgreSQL LISTEN/NOTIFY message captured during an operation.

pid: int
channel: string
payload: string
struct SqlMetrics#

Opt-in redacted SQL driver and pool counters.

operations: int
failures: int
rows: int
duration_ms: int
pool_waits: int
pool_wait_ms: int
pool_timeouts: int
connections: int
in_use: int
struct SqlTrace#

A redacted SQL operation trace; SQL text and parameter values are never retained.

operation: string
duration_ms: int
rows: int
failed: bool
struct SqlPoolOptions#

Bounded PostgreSQL pool configuration.

use std.sql
use std.sql.SqlPoolOptions
use std.sql.SqlValue

async fn main() {
  let pool = (await sql.pool(
    "postgres://moon@127.0.0.1/app",
    SqlPoolOptions {
      min_connections: 1,
      max_connections: 8,
      acquire_timeout_ms: 2_000,
      connect_timeout_ms: 2_000,
      idle_timeout_ms: 30_000,
      max_lifetime_ms: 300_000,
      validation_interval_ms: 10_000,
      prepared_cache_size: 32,
    },
  )).unwrap()
  let result = (await pool.execute(
    sql.raw("select $1::text as name", [SqlValue.Text("moon")]),
  )).unwrap()
  println(result.rows[0].require_text("name"))
  println(pool.metrics().operations)
  (await pool.close()).unwrap()
}
min_connections: int
max_connections: int
acquire_timeout_ms: int
connect_timeout_ms: int
idle_timeout_ms: int
max_lifetime_ms: int
validation_interval_ms: int
prepared_cache_size: int
struct SqlTransactionOptions#

PostgreSQL transaction mode.

isolation: string
read_only: bool
deferrable: bool
struct SqlConnection#

An exclusive PostgreSQL connection handle.

An opaque handle: only std.sql can produce one, and its contents are not reachable from moon source.

use std.sql
use std.sql.SqlConnection
use std.sql.SqlTransactionOptions
use std.sql.SqlValue

async fn inspect(connection: SqlConnection) -> Result<int, Error> {
  let rows = await connection.query(
    sql.raw("select id, name from videos", []),
  )?
  let mut reading = true
  while reading {
    match await rows.next()? {
      some(row) => println(row.require_text("name")?),
      none => { reading = false },
    }
  }
  await rows.close()?

  let transaction = await connection.begin(
    SqlTransactionOptions {
      isolation: "serializable",
      read_only: false,
      deferrable: false,
    },
  )?
  await transaction.execute(sql.raw("select $1", [SqlValue.Int(1)]))?
  await transaction.commit()?
  ok(0)
}

fn main() {}
_handle: NativeHandle

The native resource.

struct SqlPool#

A bounded FIFO PostgreSQL connection pool.

An opaque handle: only std.sql can produce one, and its contents are not reachable from moon source.

_handle: NativeHandle

The native resource.

struct SqlRows#

A single-pass SQL row stream.

An opaque handle: only std.sql can produce one, and its contents are not reachable from moon source.

_handle: NativeHandle

The native resource.

struct SqlTransaction#

A transaction owning one physical connection.

An opaque handle: only std.sql can produce one, and its contents are not reachable from moon source.

_handle: NativeHandle

The native resource.

struct SqlStatement#

A prepared-statement handle.

An opaque handle: only std.sql can produce one, and its contents are not reachable from moon source.

_handle: NativeHandle

The native resource.

struct SqlQuery#

An immutable parameterized SQL query.

An opaque handle: only std.sql can produce one, and its contents are not reachable from moon source.

_handle: NativeHandle

The native resource.

struct SqlExpr#

An immutable parameterized SQL expression.

An opaque handle: only std.sql can produce one, and its contents are not reachable from moon source.

_handle: NativeHandle

The native resource.

struct SqlCopyIn#

A PostgreSQL COPY FROM STDIN stream.

An opaque handle: only std.sql can produce one, and its contents are not reachable from moon source.

_handle: NativeHandle

The native resource.

struct SqlCopyOut#

A PostgreSQL COPY TO STDOUT stream.

An opaque handle: only std.sql can produce one, and its contents are not reachable from moon source.

_handle: NativeHandle

The native resource.

struct SqlExecutor#

Type-level capability accepted by ORM operations; runtime values are a SQL connection, pool, or transaction.

Deliberately field-less: a type-level capability, never a runtime value of its own — an executor argument is always one of the three wrappers above.

struct Decimal#

Exact decimal text wrapper used by explicit SQL/ORM codecs.

value: string
struct Uuid#

Canonical UUID text wrapper used by explicit SQL/ORM codecs.

value: string
struct SqlCodec#

A registered SQL column map from sql.codec<T>.

_type: string

The runtime name of the type the codec was declared for.

Functions

fn connect(url: string) -> Future<Result<SqlConnection, Error>>#
fn pool(url: string, options: SqlPoolOptions) -> Future<Result<SqlPool, Error>>#
fn raw(text: string, params: [SqlValue]) -> SqlQuery#
fn select(table: string, columns: [string]) -> SqlQuery#
fn insert_into(table: string, columns: [string]) -> SqlQuery#
fn update(table: string) -> SqlQuery#
fn delete_from(table: string) -> SqlQuery#
fn fragment(text: string, params: [SqlValue]) -> SqlExpr#
fn column(name: string) -> SqlExpr#
fn alias(expr: SqlExpr, name: string) -> SqlExpr#
fn eq(column: string, value: SqlValue) -> SqlExpr#
fn ne(column: string, value: SqlValue) -> SqlExpr#
fn lt(column: string, value: SqlValue) -> SqlExpr#
fn lte(column: string, value: SqlValue) -> SqlExpr#
fn gt(column: string, value: SqlValue) -> SqlExpr#
fn gte(column: string, value: SqlValue) -> SqlExpr#
fn like(column: string, value: SqlValue) -> SqlExpr#
fn ilike(column: string, value: SqlValue) -> SqlExpr#
fn between(column: string, low: SqlValue, high: SqlValue) -> SqlExpr#
fn is_null(column: string) -> SqlExpr#
fn is_not_null(column: string) -> SqlExpr#
fn in_values(column: string, values: [SqlValue]) -> SqlExpr#
fn not_in_values(column: string, values: [SqlValue]) -> SqlExpr#
fn and(exprs: [SqlExpr]) -> SqlExpr#
fn or(exprs: [SqlExpr]) -> SqlExpr#
fn not(expr: SqlExpr) -> SqlExpr#
fn where(self: SqlQuery, expr: SqlExpr) -> SqlQuery#
fn having(self: SqlQuery, expr: SqlExpr) -> SqlQuery#
fn join(self: SqlQuery, kind: string, table: string, on: SqlExpr) -> SqlQuery#
fn distinct(self: SqlQuery) -> SqlQuery#
fn group_by(self: SqlQuery, columns: [string]) -> SqlQuery#
fn order_by(self: SqlQuery, column: string, direction: string, nulls: string) -> SqlQuery#
fn limit(self: SqlQuery, count: int) -> SqlQuery#
fn offset(self: SqlQuery, count: int) -> SqlQuery#
fn values(self: SqlQuery, row: [SqlValue]) -> SqlQuery#
fn set(self: SqlQuery, column: string, value: SqlValue) -> SqlQuery#
fn returning(self: SqlQuery, columns: [string]) -> SqlQuery#
fn on_conflict_do_nothing(self: SqlQuery, columns: [string]) -> SqlQuery#
fn on_conflict_update(self: SqlQuery, columns: [string], assignments: [string]) -> SqlQuery#
fn execute<R>(self: R, query: SqlQuery) -> Future<Result<SqlResult, Error>>#
fn execute_as<T, R>(self: R, query: SqlQuery) -> Future<Result<[T], Error>>#
fn execute_map<T, R>(self: R, query: SqlQuery, key: string) -> Future<Result<{string: T}, Error>>
fn execute_group<T, R>(self: R, query: SqlQuery, key: string) -> Future<Result<{string: [T]}, Error>>
fn query<R>(self: R, query: SqlQuery) -> Future<Result<SqlRows, Error>>#
fn execute_raw(self: SqlConnection, text: string, params: [SqlValue]) -> Future<Result<SqlResult, Error>>#
fn query_raw(self: SqlConnection, text: string, params: [SqlValue]) -> Future<Result<SqlRows, Error>>#
fn close<R>(self: R) -> Future<Result<void, Error>>#
fn prepare(self: SqlConnection, text: string) -> Future<Result<SqlStatement, Error>>#
fn acquire(self: SqlPool) -> Future<Result<SqlConnection, Error>>#
fn next<R>(self: R) -> Future<Result<Optional<SqlRow>, Error>>#
fn collect(self: SqlRows, limit: int) -> Future<Result<[SqlRow], Error>>#
fn commit(self: SqlTransaction) -> Future<Result<void, Error>>#
fn rollback(self: SqlTransaction) -> Future<Result<void, Error>>#
fn savepoint(self: SqlTransaction, name: string) -> Future<Result<void, Error>>#
fn listen(self: SqlConnection, channel: string) -> Future<Result<SqlResult, Error>>#
fn unlisten(self: SqlConnection, channel: string) -> Future<Result<SqlResult, Error>>#
fn notices(self: SqlConnection) -> [SqlNotice]#
fn notifications(self: SqlConnection) -> [SqlNotification]#
fn copy_in(self: SqlConnection, text: string) -> Future<Result<SqlCopyIn, Error>>#
fn copy_out(self: SqlConnection, text: string) -> Future<Result<SqlCopyOut, Error>>#
fn write(self: SqlCopyIn, bytes: [int]) -> Future<Result<void, Error>>#
fn finish(self: SqlCopyIn) -> Future<Result<SqlResult, Error>>#
fn abort(self: SqlCopyIn) -> Future<Result<void, Error>>#
fn pipeline(self: SqlConnection, queries: [SqlQuery]) -> Future<Result<[SqlResult], Error>>#
fn register_text_codec(self: SqlConnection, type_oid: int, type_name: string) -> Result<void, Error>#
fn pg_array(values: [SqlValue]) -> SqlValue#
fn metrics<R>(self: R) -> SqlMetrics#
fn traces(self: SqlConnection) -> [SqlTrace]#
fn get(self: SqlRow, name: string) -> Optional<SqlValue>#
fn at(self: SqlRow, index: int) -> Optional<SqlValue>#
fn text(self: SqlRow, name: string) -> Result<Optional<string>, Error>#
fn int_col(self: SqlRow, name: string) -> Result<Optional<int>, Error>#
fn float_col(self: SqlRow, name: string) -> Result<Optional<float>, Error>#
fn bool_col(self: SqlRow, name: string) -> Result<Optional<bool>, Error>#
fn int(self: SqlRow, name: string) -> Result<Optional<int>, Error>#
fn float(self: SqlRow, name: string) -> Result<Optional<float>, Error>#
fn bool(self: SqlRow, name: string) -> Result<Optional<bool>, Error>#
fn datetime(self: SqlRow, name: string) -> Result<Optional<DateTime>, Error>#
fn json(self: SqlRow, name: string) -> Result<Optional<Json>, Error>#
fn decode<T>(self: SqlRow) -> Result<T, Error>#
fn decode_all<T>(rows: [SqlRow]) -> Result<[T], Error>#
fn codec<T>(rules: [FieldRule]) -> SqlCodec#
fn field(name: string) -> FieldRule#
fn rename_all(style: string) -> FieldRule#
fn require_text(self: SqlRow, name: string) -> Result<string, Error>#
fn require_int(self: SqlRow, name: string) -> Result<int, Error>#
fn require_float(self: SqlRow, name: string) -> Result<float, Error>#
fn require_bool(self: SqlRow, name: string) -> Result<bool, Error>#
fn require_datetime(self: SqlRow, name: string) -> Result<DateTime, Error>#
fn require_json(self: SqlRow, name: string) -> Result<Json, Error>#
fn text_or(self: SqlRow, name: string, fallback: string) -> string#
fn int_or(self: SqlRow, name: string, fallback: int) -> int#
fn float_or(self: SqlRow, name: string, fallback: float) -> float#
fn bool_or(self: SqlRow, name: string, fallback: bool) -> bool#