use std::borrow::Cow;
use ruprizzle_dialect::DbDialect;
use sqlx::any::AnyRow;
use sqlx::mysql::MySqlRow;
use sqlx::postgres::PgRow;
use sqlx::sqlite::SqliteRow;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;
use futures_util::StreamExt as _;
use crate::pool::{InFlight, ReadRoute};
static FULL_TABLE_INCLUDE_LIMIT: AtomicU64 = AtomicU64::new(100_000);
static SLOW_QUERY_THRESHOLD_NS: AtomicU64 = AtomicU64::new(0);
pub fn set_slow_query_threshold(threshold: Option<Duration>) {
let ns = threshold.map_or(0, |d| d.as_nanos() as u64);
SLOW_QUERY_THRESHOLD_NS.store(ns, Ordering::Relaxed);
}
#[must_use]
pub fn slow_query_threshold() -> Option<Duration> {
let ns = SLOW_QUERY_THRESHOLD_NS.load(Ordering::Relaxed);
if ns == 0 {
None
} else {
Some(Duration::from_nanos(ns))
}
}
pub(crate) fn set_full_table_include_limit(limit: Option<u64>) {
FULL_TABLE_INCLUDE_LIMIT.store(limit.unwrap_or(0), Ordering::Relaxed);
}
fn full_table_include_limit() -> u64 {
FULL_TABLE_INCLUDE_LIMIT.load(Ordering::Relaxed)
}
use crate::BoxFuture;
use crate::error::Error;
use crate::pool::Pool;
use crate::value::Value;
#[non_exhaustive]
pub enum RowBatch {
Any(Vec<AnyRow>),
Postgres(Vec<PgRow>),
Sqlite(Vec<SqliteRow>),
Mysql(Vec<MySqlRow>),
#[cfg(feature = "sqlite-rusqlite")]
Rusqlite(Vec<crate::rusqlite::Row>),
#[cfg(feature = "postgres-tokio-postgres")]
PostgresNative(Vec<tokio_postgres::Row>),
Edge(Vec<std::collections::HashMap<String, Value>>),
}
impl RowBatch {
#[must_use]
pub fn is_empty(&self) -> bool {
match self {
Self::Any(rows) => rows.is_empty(),
Self::Postgres(rows) => rows.is_empty(),
Self::Sqlite(rows) => rows.is_empty(),
Self::Mysql(rows) => rows.is_empty(),
#[cfg(feature = "sqlite-rusqlite")]
Self::Rusqlite(rows) => rows.is_empty(),
#[cfg(feature = "postgres-tokio-postgres")]
Self::PostgresNative(rows) => rows.is_empty(),
Self::Edge(rows) => rows.is_empty(),
}
}
#[must_use]
pub fn len(&self) -> usize {
match self {
Self::Any(rows) => rows.len(),
Self::Postgres(rows) => rows.len(),
Self::Sqlite(rows) => rows.len(),
Self::Mysql(rows) => rows.len(),
#[cfg(feature = "sqlite-rusqlite")]
Self::Rusqlite(rows) => rows.len(),
#[cfg(feature = "postgres-tokio-postgres")]
Self::PostgresNative(rows) => rows.len(),
Self::Edge(rows) => rows.len(),
}
}
pub fn merge(&mut self, other: Self) -> Result<(), Error> {
match (self, other) {
(Self::Any(a), Self::Any(b)) => a.extend(b),
(Self::Postgres(a), Self::Postgres(b)) => a.extend(b),
(Self::Sqlite(a), Self::Sqlite(b)) => a.extend(b),
(Self::Mysql(a), Self::Mysql(b)) => a.extend(b),
#[cfg(feature = "sqlite-rusqlite")]
(Self::Rusqlite(a), Self::Rusqlite(b)) => a.extend(b),
#[cfg(feature = "postgres-tokio-postgres")]
(Self::PostgresNative(a), Self::PostgresNative(b)) => a.extend(b),
(Self::Edge(a), Self::Edge(b)) => a.extend(b),
_ => {
return Err(Error::Message(
"cannot merge row batches from different backends".into(),
));
}
}
Ok(())
}
#[must_use]
pub fn as_edge_rows(&self) -> Option<&[std::collections::HashMap<String, Value>]> {
match self {
Self::Edge(rows) => Some(rows),
_ => None,
}
}
}
impl std::fmt::Debug for RowBatch {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Any(rows) => f.debug_tuple("Any").field(&rows.len()).finish(),
Self::Postgres(rows) => f.debug_tuple("Postgres").field(&rows.len()).finish(),
Self::Sqlite(rows) => f.debug_tuple("Sqlite").field(&rows.len()).finish(),
Self::Mysql(rows) => f.debug_tuple("Mysql").field(&rows.len()).finish(),
#[cfg(feature = "sqlite-rusqlite")]
Self::Rusqlite(rows) => f.debug_tuple("Rusqlite").field(&rows.len()).finish(),
#[cfg(feature = "postgres-tokio-postgres")]
Self::PostgresNative(rows) => {
f.debug_tuple("PostgresNative").field(&rows.len()).finish()
}
Self::Edge(rows) => f.debug_tuple("Edge").field(&rows.len()).finish(),
}
}
}
pub trait Executor: Send + Sync {
fn dialect(&self) -> &dyn DbDialect;
fn full_table_include_limit(&self) -> u64 {
full_table_include_limit()
}
fn fetch_all_raw(
&self,
sql: Cow<'static, str>,
binds: Vec<Value>,
) -> BoxFuture<'_, Result<RowBatch, Error>>;
fn execute_raw(
&self,
sql: Cow<'static, str>,
binds: Vec<Value>,
) -> BoxFuture<'_, Result<u64, Error>>;
fn stream_raw(&self, sql: Cow<'static, str>, binds: Vec<Value>) -> BoxRowStream<'_>;
fn stream_unbuffered_raw(&self, sql: Cow<'static, str>, binds: Vec<Value>) -> BoxRowStream<'_> {
self.stream_raw(sql, binds)
}
fn on_query(&self) {}
#[cfg(feature = "sqlite-rusqlite")]
fn as_rusqlite(&self) -> Option<&crate::rusqlite::RusqlitePool> {
None
}
}
#[non_exhaustive]
pub enum RawRow {
Any(AnyRow),
Postgres(PgRow),
Sqlite(SqliteRow),
Mysql(MySqlRow),
#[cfg(feature = "sqlite-rusqlite")]
Rusqlite(crate::rusqlite::Row),
#[cfg(feature = "postgres-tokio-postgres")]
PostgresNative(tokio_postgres::Row),
Edge(std::collections::HashMap<String, Value>),
}
pub type BoxRowStream<'a> =
std::pin::Pin<Box<dyn futures_core::Stream<Item = Result<RawRow, Error>> + Send + 'a>>;
fn infer_operation_and_table(sql: &str) -> (&'static str, String) {
let trimmed = sql.trim();
let upper = trimmed.to_ascii_uppercase();
if upper.starts_with("SELECT") {
let op = "SELECT";
let table = if let Some(from_idx) = upper.find(" FROM ") {
let rest = trimmed.get(from_idx + 6..).unwrap_or("").trim();
rest.split(|c: char| c.is_whitespace() || c == '(' || c == ',' || c == ';')
.next()
.unwrap_or("")
.trim_matches(|c| c == '"' || c == '`' || c == '\'')
.to_string()
} else {
String::new()
};
(op, table)
} else if upper.starts_with("INSERT") {
let op = "INSERT";
let table = if let Some(into_idx) = upper.find(" INTO ") {
let rest = trimmed.get(into_idx + 6..).unwrap_or("").trim();
rest.split(|c: char| c.is_whitespace() || c == '(' || c == ';')
.next()
.unwrap_or("")
.trim_matches(|c| c == '"' || c == '`' || c == '\'')
.to_string()
} else {
String::new()
};
(op, table)
} else if upper.starts_with("UPDATE") {
let op = "UPDATE";
let rest = trimmed.get(6..).unwrap_or("").trim();
let table = rest
.split(|c: char| c.is_whitespace() || c == ';')
.next()
.unwrap_or("")
.trim_matches(|c| c == '"' || c == '`' || c == '\'')
.to_string();
(op, table)
} else if upper.starts_with("DELETE") {
let op = "DELETE";
let table = if let Some(from_idx) = upper.find(" FROM ") {
let rest = trimmed.get(from_idx + 6..).unwrap_or("").trim();
rest.split(|c: char| c.is_whitespace() || c == ';')
.next()
.unwrap_or("")
.trim_matches(|c| c == '"' || c == '`' || c == '\'')
.to_string()
} else {
String::new()
};
(op, table)
} else {
("QUERY", String::new())
}
}
pub(crate) async fn trace_and_record_query<F>(
sql: Cow<'static, str>,
bind_count: usize,
fut: F,
) -> Result<RowBatch, Error>
where
F: std::future::Future<Output = Result<RowBatch, Error>>,
{
let (op, table) = infer_operation_and_table(&sql);
let sanitized_sql = crate::compile::sanitize_sql(&sql);
let span = tracing::info_span!(
"db.query",
"db.system" = "rdbms",
"db.statement" = %sanitized_sql,
"db.operation" = %op,
"db.table" = %table,
);
let _guard = span.enter();
crate::metrics::counter(crate::metrics::QUERY_TOTAL, 1);
let started = std::time::Instant::now();
let result = fut.await;
let elapsed = started.elapsed();
crate::metrics::histogram(
crate::metrics::QUERY_DURATION_SECONDS,
elapsed.as_secs_f64(),
);
if let Err(error) = &result {
crate::metrics::counter_with(
crate::metrics::QUERY_ERRORS_TOTAL,
[("kind", error.kind())],
1,
);
emit_connection_events(error);
}
if tracing::enabled!(target: "ruprizzle::query", tracing::Level::DEBUG) {
let elapsed_ms = elapsed.as_millis() as u64;
match &result {
Ok(batch) => tracing::debug!(
target: "ruprizzle::query",
sql = %sql,
binds = bind_count,
rows = batch.len(),
elapsed_ms,
"query"
),
Err(error) => tracing::warn!(
target: "ruprizzle::query",
sql = %sql,
binds = bind_count,
elapsed_ms,
error = error.kind(),
"query failed"
),
}
}
if let Some(threshold) = slow_query_threshold() {
if elapsed > threshold {
crate::metrics::counter(crate::metrics::SLOW_QUERIES_TOTAL, 1);
tracing::warn!(
target: "ruprizzle::slow_query",
sql = %sql,
binds = bind_count,
elapsed_ms = elapsed.as_millis(),
"slow query"
);
}
}
result
}
pub(crate) async fn trace_and_record_execute<F>(
sql: Cow<'static, str>,
bind_count: usize,
fut: F,
) -> Result<u64, Error>
where
F: std::future::Future<Output = Result<u64, Error>>,
{
let (op, table) = infer_operation_and_table(&sql);
let sanitized_sql = crate::compile::sanitize_sql(&sql);
let span = tracing::info_span!(
"db.execute",
"db.system" = "rdbms",
"db.statement" = %sanitized_sql,
"db.operation" = %op,
"db.table" = %table,
);
let _guard = span.enter();
crate::metrics::counter(crate::metrics::QUERY_TOTAL, 1);
let started = std::time::Instant::now();
let result = fut.await;
let elapsed = started.elapsed();
crate::metrics::histogram(
crate::metrics::QUERY_DURATION_SECONDS,
elapsed.as_secs_f64(),
);
if let Ok(rows) = &result {
crate::metrics::counter(crate::metrics::ROWS_AFFECTED_TOTAL, *rows);
}
if let Err(error) = &result {
crate::metrics::counter_with(
crate::metrics::QUERY_ERRORS_TOTAL,
[("kind", error.kind())],
1,
);
emit_connection_events(error);
}
if tracing::enabled!(target: "ruprizzle::query", tracing::Level::DEBUG) {
let elapsed_ms = elapsed.as_millis() as u64;
match &result {
Ok(rows_affected) => tracing::debug!(
target: "ruprizzle::query",
sql = %sql,
binds = bind_count,
rows_affected,
elapsed_ms,
"execute"
),
Err(error) => tracing::warn!(
target: "ruprizzle::query",
sql = %sql,
binds = bind_count,
elapsed_ms,
error = error.kind(),
"execute failed"
),
}
}
if let Some(threshold) = slow_query_threshold() {
if elapsed > threshold {
crate::metrics::counter(crate::metrics::SLOW_QUERIES_TOTAL, 1);
tracing::warn!(
target: "ruprizzle::slow_query",
sql = %sql,
binds = bind_count,
elapsed_ms = elapsed.as_millis(),
"slow query"
);
}
}
result
}
pub(crate) fn emit_connection_events(error: &crate::Error) {
match error {
crate::Error::AcquireTimeout { reason } => {
tracing::warn!(
target: "ruprizzle::connection",
event = "acquire_timeout",
reason,
"connection acquire timed out"
);
}
crate::Error::ConnectionFailure { reason } => {
tracing::warn!(
target: "ruprizzle::connection",
event = "disconnect",
reason,
"connection lost"
);
}
crate::Error::PoolExhausted { backend } => {
tracing::warn!(
target: "ruprizzle::connection",
event = "acquire_timeout",
backend,
"connection pool exhausted"
);
}
_ => {}
}
}
pub(crate) struct DeferredRowStream<'a> {
fut: crate::BoxFuture<'a, Result<RowBatch, Error>>,
done: bool,
buffered: std::vec::IntoIter<RawRow>,
}
impl<'a> DeferredRowStream<'a> {
pub(crate) fn new(fut: crate::BoxFuture<'a, Result<RowBatch, Error>>) -> Self {
Self {
fut,
done: false,
buffered: Vec::new().into_iter(),
}
}
}
impl futures_core::Stream for DeferredRowStream<'_> {
type Item = Result<RawRow, Error>;
fn poll_next(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Option<Self::Item>> {
use std::task::Poll;
let this = self.get_mut();
if !this.done {
match this.fut.as_mut().poll(cx) {
Poll::Pending => return Poll::Pending,
Poll::Ready(Err(e)) => {
this.done = true;
return Poll::Ready(Some(Err(e)));
}
Poll::Ready(Ok(batch)) => {
this.done = true;
this.buffered = match batch {
RowBatch::Any(rows) => {
rows.into_iter().map(RawRow::Any).collect::<Vec<_>>()
}
RowBatch::Postgres(rows) => {
rows.into_iter().map(RawRow::Postgres).collect::<Vec<_>>()
}
RowBatch::Sqlite(rows) => {
rows.into_iter().map(RawRow::Sqlite).collect::<Vec<_>>()
}
RowBatch::Mysql(rows) => {
rows.into_iter().map(RawRow::Mysql).collect::<Vec<_>>()
}
#[cfg(feature = "sqlite-rusqlite")]
RowBatch::Rusqlite(rows) => {
rows.into_iter().map(RawRow::Rusqlite).collect::<Vec<_>>()
}
#[cfg(feature = "postgres-tokio-postgres")]
RowBatch::PostgresNative(rows) => rows
.into_iter()
.map(RawRow::PostgresNative)
.collect::<Vec<_>>(),
RowBatch::Edge(rows) => {
rows.into_iter().map(RawRow::Edge).collect::<Vec<_>>()
}
}
.into_iter();
}
}
}
Poll::Ready(this.buffered.next().map(Ok))
}
}
impl Executor for Pool {
fn dialect(&self) -> &dyn DbDialect {
crate::compile::dialect_for_pool(self)
}
#[cfg(feature = "sqlite-rusqlite")]
fn as_rusqlite(&self) -> Option<&crate::rusqlite::RusqlitePool> {
match self {
Pool::SqliteNative(p) => Some(p),
_ => None,
}
}
fn fetch_all_raw(
&self,
sql: Cow<'static, str>,
binds: Vec<Value>,
) -> BoxFuture<'_, Result<RowBatch, Error>> {
let bind_count = binds.len();
let pool = self.clone();
Box::pin(trace_and_record_query(
sql.clone(),
bind_count,
async move { dispatch_raw_query(&pool, sql, binds).await },
))
}
fn execute_raw(
&self,
sql: Cow<'static, str>,
binds: Vec<Value>,
) -> BoxFuture<'_, Result<u64, Error>> {
let bind_count = binds.len();
let pool = self.clone();
Box::pin(trace_and_record_execute(
sql.clone(),
bind_count,
async move { dispatch_raw_execute(&pool, sql, binds).await },
))
}
fn stream_raw(&self, sql: Cow<'static, str>, binds: Vec<Value>) -> BoxRowStream<'_> {
Box::pin(DeferredRowStream::new(Box::pin(async move {
self.fetch_all_raw(sql, binds).await
})))
}
fn stream_unbuffered_raw(&self, sql: Cow<'static, str>, binds: Vec<Value>) -> BoxRowStream<'_> {
match self {
#[cfg(feature = "postgres-tokio-postgres")]
Pool::PostgresNative(p) => Executor::stream_unbuffered_raw(p, sql, binds),
_ => self.stream_raw(sql, binds),
}
}
}
async fn dispatch_raw_query(
pool: &Pool,
sql: Cow<'static, str>,
binds: Vec<Value>,
) -> Result<RowBatch, Error> {
match pool {
Pool::Any(p) => {
let mut q = sqlx::query::<sqlx::Any>(&sql);
for bind in &binds {
q = q.bind(bind);
}
q.fetch_all(p).await.map(RowBatch::Any).map_err(Error::from)
}
Pool::Postgres(p) => {
let mut q = sqlx::query::<sqlx::Postgres>(&sql);
for bind in &binds {
q = q.bind(bind);
}
q.fetch_all(p)
.await
.map(RowBatch::Postgres)
.map_err(Error::from)
}
Pool::Sqlite(p) => {
let mut q = sqlx::query::<sqlx::Sqlite>(&sql);
for bind in &binds {
q = q.bind(bind);
}
q.fetch_all(p)
.await
.map(RowBatch::Sqlite)
.map_err(Error::from)
}
Pool::Mysql(p) => {
let mut q = sqlx::query::<sqlx::MySql>(&sql);
for bind in &binds {
q = q.bind(bind);
}
q.fetch_all(p)
.await
.map(RowBatch::Mysql)
.map_err(Error::from)
}
#[cfg(feature = "sqlite-rusqlite")]
Pool::SqliteNative(p) => Executor::fetch_all_raw(p, sql, binds).await,
#[cfg(feature = "postgres-tokio-postgres")]
Pool::PostgresNative(p) => Executor::fetch_all_raw(p, sql, binds).await,
}
}
async fn dispatch_raw_execute(
pool: &Pool,
sql: Cow<'static, str>,
binds: Vec<Value>,
) -> Result<u64, Error> {
match pool {
Pool::Any(p) => {
let mut q = sqlx::query::<sqlx::Any>(&sql);
for bind in &binds {
q = q.bind(bind);
}
q.execute(p)
.await
.map(|r| r.rows_affected())
.map_err(Error::from)
}
Pool::Postgres(p) => {
let mut q = sqlx::query::<sqlx::Postgres>(&sql);
for bind in &binds {
q = q.bind(bind);
}
q.execute(p)
.await
.map(|r| r.rows_affected())
.map_err(Error::from)
}
Pool::Sqlite(p) => {
let mut q = sqlx::query::<sqlx::Sqlite>(&sql);
for bind in &binds {
q = q.bind(bind);
}
q.execute(p)
.await
.map(|r| r.rows_affected())
.map_err(Error::from)
}
Pool::Mysql(p) => {
let mut q = sqlx::query::<sqlx::MySql>(&sql);
for bind in &binds {
q = q.bind(bind);
}
q.execute(p)
.await
.map(|r| r.rows_affected())
.map_err(Error::from)
}
#[cfg(feature = "sqlite-rusqlite")]
Pool::SqliteNative(p) => Executor::execute_raw(p, sql, binds).await,
#[cfg(feature = "postgres-tokio-postgres")]
Pool::PostgresNative(p) => Executor::execute_raw(p, sql, binds).await,
}
}
pub fn decode_rows<T>(batch: RowBatch) -> Result<Vec<T>, Error>
where
T: crate::model::RowDecode,
{
match batch {
RowBatch::Any(rows) => rows
.iter()
.map(|r| T::from_row(r).map_err(Error::Sqlx))
.collect(),
RowBatch::Postgres(rows) => rows
.iter()
.map(|r| T::from_row(r).map_err(Error::Sqlx))
.collect(),
RowBatch::Sqlite(rows) => rows
.iter()
.map(|r| T::from_row(r).map_err(Error::Sqlx))
.collect(),
RowBatch::Mysql(rows) => rows
.iter()
.map(|r| T::from_row(r).map_err(Error::Sqlx))
.collect(),
#[cfg(feature = "sqlite-rusqlite")]
RowBatch::Rusqlite(rows) => rows.iter().map(|r| T::from_owned_row(r)).collect(),
#[cfg(feature = "postgres-tokio-postgres")]
RowBatch::PostgresNative(rows) => {
rows.iter().map(|r| T::from_tokio_postgres_row(r)).collect()
}
RowBatch::Edge(_) => Err(Error::Message(
"RowBatch::Edge cannot be decoded via sqlx FromRow; use custom edge decoder or typed edge adapter".into(),
)),
}
}
impl Executor for crate::pool::RoutedPool {
fn dialect(&self) -> &dyn DbDialect {
self.primary().dialect()
}
fn fetch_all_raw(
&self,
sql: Cow<'static, str>,
binds: Vec<Value>,
) -> BoxFuture<'_, Result<RowBatch, Error>> {
if !crate::pool::is_replica_safe(&sql) {
return self.primary().fetch_all_raw(sql, binds);
}
crate::metrics::counter(crate::metrics::REPLICA_ROUTING_TOTAL, 1);
match self.route_read() {
ReadRoute::Replica(replica) => {
let in_flight = InFlight::start(replica);
let pool = &replica.pool;
Box::pin(async move {
let result = pool.fetch_all_raw(sql, binds).await;
drop(in_flight);
result
})
}
ReadRoute::Primary => self.primary().fetch_all_raw(sql, binds),
ReadRoute::NoHealthyReplica => Box::pin(async { Err(no_healthy_replica()) }),
}
}
fn stream_raw<'a>(&'a self, sql: Cow<'static, str>, binds: Vec<Value>) -> BoxRowStream<'a> {
if !crate::pool::is_replica_safe(&sql) {
return self.primary().stream_raw(sql, binds);
}
crate::metrics::counter(crate::metrics::REPLICA_ROUTING_TOTAL, 1);
match self.route_read() {
ReadRoute::Replica(replica) => {
let in_flight = InFlight::start(replica);
Box::pin(replica.pool.stream_raw(sql, binds).map(move |row| {
let _held = &in_flight;
row
}))
}
ReadRoute::Primary => self.primary().stream_raw(sql, binds),
ReadRoute::NoHealthyReplica => Box::pin(futures_util::stream::iter(std::iter::once(
Err(no_healthy_replica()),
))),
}
}
fn execute_raw(
&self,
sql: Cow<'static, str>,
binds: Vec<Value>,
) -> BoxFuture<'_, Result<u64, Error>> {
self.primary().execute_raw(sql, binds)
}
}
fn no_healthy_replica() -> Error {
Error::Message(
"no healthy read replica, and fallback_to_primary is disabled; \
call RoutedPool::check_health() or mark a replica healthy"
.into(),
)
}