Skip to main content

Connection

Struct Connection 

Source
pub struct Connection(/* private fields */);
Available on crate feature pg only.
Expand description

An open connection to a PostgreSQL database.

§Examples

Load a set of rows from a local PostgreSQL database, and iterate over them.

use spin_sdk::pg::{Connection, Decode};

let db = Connection::open("host=localhost user=postgres password=my_password dbname=mydb").await?;

let mut query_result = db.query(
    "SELECT * FROM users WHERE age >= $1",
    &[min_age.into()]
).await?;

while let Some(row) = query_result.next().await {
    let name = row.get::<String>("name").unwrap();
    println!("Found user {name}");
}

query_result.result().await?;

Perform an aggregate (scalar) operation over a table. The result set contains a single column, with a single row.

use spin_sdk::pg::{Connection, Decode};

let db = Connection::open("host=localhost user=postgres password=my_password dbname=mydb").await?;

let query_result = db.query("SELECT COUNT(*) FROM users", &[]).await?;

assert_eq!(1, query_result.columns().len());
assert_eq!("count", query_result.columns()[0].name);

let rows = query_result.collect().await?;

assert_eq!(1, rows.len());

let count = &rows[0][0];

Delete rows from a PostgreSQL table. This uses Connection::execute() instead of the query method.

use spin_sdk::pg::Connection;

let db = Connection::open("host=localhost user=postgres password=my_password dbname=mydb").await?;

let rows_affected = db.execute(
    "DELETE FROM users WHERE name = $1",
    &["Baldrick".to_owned().into()]
).await?;

Implementations§

Source§

impl Connection

Source

pub async fn open(address: impl Into<String>) -> Result<Self, Error>

Open a connection to a PostgreSQL database.

The address may be in connection string form ("host=... dbname=...") or in URL form ("postgres://<host>/<dbname>?...").

This constructor does not support options such as custom CA roots. See Connection::open_with_options for a more flexible constructor.

Source

pub async fn open_with_options( address: impl AsRef<str>, options: OpenOptions, ) -> Result<Self, Error>

Open a connection to a PostgreSQL database.

The address may be in connection string form ("host=... dbname=...") or in URL form ("postgres://<host>/<dbname>?...").

The options parameter allows for passing options not available in the address string.

Source

pub async fn query( &self, statement: impl Into<String>, params: impl Into<Vec<ParameterValue>>, ) -> Result<QueryResult, Error>

Query the database.

Use this function for queries that return rows (typically SELECT queries). For side-effectful queries, see Connection::execute.

Source

pub async fn execute( &self, statement: impl Into<String>, params: impl Into<Vec<ParameterValue>>, ) -> Result<u64, Error>

Execute a command against the database.

Use this function for side-effectful queries (such as INSERT or DELETE queries). For queries that return row data, see Connection::query.

Source

pub fn into_inner(self) -> Connection

Extracts the underlying Wasm Component Model resource for the connection.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V