Skip to main content

rdf_store_postgres/
store.rs

1// This is free and unencumbered software released into the public domain.
2
3use crate::PostgresError;
4use derive_more::Debug;
5use futures::executor::block_on;
6use tokio_postgres::{Client, Connection, NoTls, Socket, tls::NoTlsStream};
7
8/// The default localhost connection URL for PostgreSQL.
9///
10/// See: <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING-URIS>
11pub const DEFAULT_URL: &str = "postgres://postgres@localhost:5432";
12
13#[cfg_attr(doc, aquamarine::aquamarine)]
14/// A quad store backed by a PostgreSQL database.
15///
16/// # Examples
17///
18/// ```rust,no_run
19/// # #[tokio::main]
20/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
21/// # use rdf_store_postgres::PostgresStore;
22/// let mut store = PostgresStore::open("postgres://postgres@localhost:5432").await?;
23/// # Ok(())
24/// # }
25/// ```
26#[derive(Debug)]
27pub struct PostgresStore {
28    pub client: Client,
29
30    #[debug(skip)]
31    pub connection: Connection<Socket, NoTlsStream>,
32}
33
34impl PostgresStore {
35    pub async fn open(url: impl AsRef<str>) -> Result<Self, PostgresError> {
36        let (client, connection) = tokio_postgres::connect(url.as_ref(), NoTls).await?;
37        Ok(Self { client, connection })
38    }
39}
40
41impl Default for PostgresStore {
42    fn default() -> Self {
43        block_on(Self::open(DEFAULT_URL))
44            .expect("should connect to postgres://postgres@localhost:5432")
45    }
46}