GlobalState stores an array of prepared statements instead of separate fields
This commit is contained in:
15
src/db.rs
15
src/db.rs
@@ -3,20 +3,25 @@ use tokio_postgres::{Client, Error, Statement};
|
||||
|
||||
use crate::GlobalState;
|
||||
|
||||
pub const STATEMENT_GET_LINK: usize = 0;
|
||||
pub const STATEMENT_ADD_LINK: usize = 1;
|
||||
pub const N_STATEMENTS: usize = 2;
|
||||
|
||||
pub async fn get_link(state: &Mutex<GlobalState>, link: &str) -> Result<Option<String>, Error> {
|
||||
let lock = state.lock().await;
|
||||
Ok(lock.db_client.query_opt(&lock.stmt_get_link, &[&link]).await?.map(|row| row.get(0)))
|
||||
Ok(lock.db_client.query_opt(&lock.statements[STATEMENT_GET_LINK], &[&link]).await?.map(|row| row.get(0)))
|
||||
}
|
||||
|
||||
pub async fn add_link(state: &Mutex<GlobalState>, link: &str, url: &str) -> Result<(), Error> {
|
||||
let lock = state.lock().await;
|
||||
lock.db_client.execute(&lock.stmt_add_link, &[&link, &url]).await?;
|
||||
lock.db_client.execute(&lock.statements[STATEMENT_ADD_LINK], &[&link, &url]).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn prepare_statements(db: &Client) -> Result<(Statement, Statement), Error> {
|
||||
Ok((db.prepare("SELECT url FROM links WHERE id = $1").await?,
|
||||
db.prepare("INSERT INTO links (id, url) VALUES ($1, $2)").await?))
|
||||
pub async fn prepare_statements(db: &Client) -> Result<[Statement; N_STATEMENTS], Error> {
|
||||
Ok([db.prepare("SELECT url FROM links WHERE id = $1").await?,
|
||||
db.prepare("INSERT INTO links (id, url) VALUES ($1, $2)").await?,
|
||||
])
|
||||
}
|
||||
|
||||
pub async fn prepare_tables(db: &Client) -> Result<(), Error> {
|
||||
|
||||
Reference in New Issue
Block a user