pub struct Connection(/* private fields */);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
impl Connection
Sourcepub async fn open(address: impl Into<String>) -> Result<Self, Error>
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.
Sourcepub async fn open_with_options(
address: impl AsRef<str>,
options: OpenOptions,
) -> Result<Self, Error>
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.
Sourcepub async fn query(
&self,
statement: impl Into<String>,
params: impl Into<Vec<ParameterValue>>,
) -> Result<QueryResult, Error>
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.
Sourcepub async fn execute(
&self,
statement: impl Into<String>,
params: impl Into<Vec<ParameterValue>>,
) -> Result<u64, Error>
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.
Sourcepub fn into_inner(self) -> Connection
pub fn into_inner(self) -> Connection
Extracts the underlying Wasm Component Model resource for the connection.