use std::borrow::Cow;
use std::sync::atomic::{AtomicUsize, Ordering};
use ruprizzle_dialect::DbDialect;
use crate::BoxFuture;
use crate::error::Error;
use crate::executor::{BoxRowStream, Executor};
use crate::value::Value;
pub struct CountingExecutor<'e> {
inner: &'e dyn Executor,
count: AtomicUsize,
}
impl std::fmt::Debug for CountingExecutor<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CountingExecutor")
.field("count", &self.count())
.finish_non_exhaustive()
}
}
impl<'e> CountingExecutor<'e> {
#[must_use]
pub fn new(inner: &'e dyn Executor) -> Self {
Self {
inner,
count: AtomicUsize::new(0),
}
}
#[must_use]
pub fn count(&self) -> usize {
self.count.load(Ordering::Relaxed)
}
pub fn reset(&self) {
self.count.store(0, Ordering::Relaxed);
}
fn tick(&self) {
self.count.fetch_add(1, Ordering::Relaxed);
}
}
impl Executor for CountingExecutor<'_> {
fn dialect(&self) -> &dyn DbDialect {
self.inner.dialect()
}
fn full_table_include_limit(&self) -> u64 {
self.inner.full_table_include_limit()
}
#[cfg(feature = "sqlite-rusqlite")]
fn as_rusqlite(&self) -> Option<&crate::rusqlite::RusqlitePool> {
self.inner.as_rusqlite()
}
fn on_query(&self) {
self.tick();
}
fn fetch_all_raw(
&self,
sql: Cow<'static, str>,
binds: Vec<Value>,
) -> BoxFuture<'_, Result<crate::executor::RowBatch, Error>> {
self.tick();
self.inner.fetch_all_raw(sql, binds)
}
fn execute_raw(
&self,
sql: Cow<'static, str>,
binds: Vec<Value>,
) -> BoxFuture<'_, Result<u64, Error>> {
self.tick();
self.inner.execute_raw(sql, binds)
}
fn stream_raw(&self, sql: Cow<'static, str>, binds: Vec<Value>) -> BoxRowStream<'_> {
self.tick();
self.inner.stream_raw(sql, binds)
}
fn stream_unbuffered_raw(&self, sql: Cow<'static, str>, binds: Vec<Value>) -> BoxRowStream<'_> {
self.tick();
self.inner.stream_unbuffered_raw(sql, binds)
}
}