use sqlx::postgres::PgRow;
use sqlx::{Decode, FromRow, Postgres, Type};
use futures_core::stream::BoxStream;
use crate::exec;
use crate::sql::Sql;
use crate::{Executor, Result, Value};
pub fn raw(sql: impl Into<String>) -> Raw {
Raw {
sql: sql.into(),
params: Vec::new(),
writes: Vec::new(),
replica: false,
}
}
#[derive(Debug, Clone)]
pub struct Raw {
sql: String,
params: Vec<Value>,
writes: Vec<&'static str>,
replica: bool,
}
impl Raw {
pub fn bind(mut self, value: impl Into<Value>) -> Self {
self.params.push(value.into());
self
}
pub fn on_replica(mut self) -> Self {
self.replica = true;
self
}
pub fn invalidates(mut self, table: &'static str) -> Self {
self.writes.push(table);
self
}
pub fn to_sql(&self) -> Sql {
let mut sql = Sql::new();
sql.push_raw_params(&self.sql, &self.params);
sql
}
pub async fn fetch_all<'e, T, E>(self, executor: E) -> Result<Vec<T>>
where
T: for<'r> FromRow<'r, PgRow> + Send + Unpin,
E: Executor<'e>,
{
exec::fetch_all(executor, &self.to_sql(), &self.writes, self.replica).await
}
pub async fn fetch_optional<'e, T, E>(self, executor: E) -> Result<Option<T>>
where
T: for<'r> FromRow<'r, PgRow> + Send + Unpin,
E: Executor<'e>,
{
exec::fetch_optional(executor, &self.to_sql(), &self.writes, self.replica).await
}
pub async fn fetch_one<'e, T, E>(self, executor: E) -> Result<T>
where
T: for<'r> FromRow<'r, PgRow> + Send + Unpin,
E: Executor<'e>,
{
exec::fetch_optional(executor, &self.to_sql(), &self.writes, self.replica)
.await?
.ok_or_else(|| sqlx::Error::RowNotFound.into())
}
pub async fn scalar<'e, T, E>(self, executor: E) -> Result<T>
where
T: Type<Postgres> + for<'r> Decode<'r, Postgres> + Send + Unpin,
E: Executor<'e>,
{
exec::fetch_scalar(executor, &self.to_sql(), self.replica).await
}
pub fn stream<'e, T, E>(self, executor: E) -> BoxStream<'e, Result<T>>
where
T: for<'r> FromRow<'r, PgRow> + Send + Unpin + 'e,
E: Executor<'e> + 'e,
{
exec::stream(executor, self.to_sql(), self.replica)
}
pub async fn execute<'e, E: Executor<'e>>(self, executor: E) -> Result<u64> {
exec::execute(executor, &self.to_sql(), &self.writes).await
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn placeholder_styles() {
let q = raw("SELECT * FROM t WHERE a = ? AND b = ?")
.bind(1)
.bind("x");
assert_eq!(
q.to_sql().as_str(),
"SELECT * FROM t WHERE a = $1 AND b = $2"
);
let q = raw("SELECT * FROM t WHERE a = $1").bind(1);
assert_eq!(q.to_sql().as_str(), "SELECT * FROM t WHERE a = $1");
assert_eq!(q.to_sql().params().len(), 1);
}
}