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, PostgresTransaction};
4use derive_more::Debug;
5use futures::executor::block_on;
6use rdf_store::Store;
7use tokio_postgres::{Client, Connection, NoTls, Socket, tls::NoTlsStream};
8
9/// The default localhost connection URL for PostgreSQL.
10///
11/// See: <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING-URIS>
12pub const DEFAULT_URL: &str = "postgres://postgres@localhost:5432";
13
14#[cfg_attr(doc, aquamarine::aquamarine)]
15/// A quad store backed by a PostgreSQL database.
16///
17/// # Examples
18///
19/// ```rust,no_run
20/// # #[tokio::main]
21/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
22/// # use rdf_store_postgres::PostgresStore;
23/// let mut store = PostgresStore::open("postgres://postgres@localhost:5432").await?;
24/// # Ok(())
25/// # }
26/// ```
27#[derive(Debug)]
28pub struct PostgresStore {
29    pub client: Client,
30
31    #[debug(skip)]
32    pub connection: Connection<Socket, NoTlsStream>,
33}
34
35impl PostgresStore {
36    pub async fn open(url: impl AsRef<str>) -> Result<Self, PostgresError> {
37        let (client, connection) = tokio_postgres::connect(url.as_ref(), NoTls).await?;
38        Ok(Self { client, connection })
39    }
40}
41
42impl Default for PostgresStore {
43    /// Connects to `postgres://postgres@localhost:5432` by default.
44    fn default() -> Self {
45        block_on(Self::open(DEFAULT_URL))
46            .expect("should connect to postgres://postgres@localhost:5432")
47    }
48}
49
50impl Store for PostgresStore {
51    type Error = PostgresError;
52    type Read = PostgresTransaction;
53    type Write = PostgresTransaction;
54
55    async fn read(&mut self) -> Result<Self::Read, Self::Error> {
56        todo!() // FIXME: PostgresTransaction::begin(self, false).await
57    }
58
59    async fn write(&mut self) -> Result<Self::Write, Self::Error> {
60        todo!() // FIXME: PostgresTransaction::begin(self, true).await
61    }
62}