Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions crates/cli/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use std::iter::Sum;
use std::ops::Add;

use reqwest::{header, Client, RequestBuilder};
use serde::Deserialize;
use serde_json::value::RawValue;

use spacetimedb::json::client_api::StmtStatsJson;
use spacetimedb_lib::db::raw_def::v9::RawModuleDefV9;
use spacetimedb_lib::de::serde::DeserializeWrapper;
use spacetimedb_lib::sats::ProductType;
Expand Down Expand Up @@ -82,11 +86,56 @@ impl ClientApi {
}
}

// Sync with spacetimedb::json::client_api::StmtResultJson
#[derive(Debug, Clone, Deserialize)]
pub struct StmtResultJson<'a> {
pub schema: ProductType,
#[serde(borrow)]
pub rows: Vec<&'a RawValue>,
pub total_duration_micros: u64,
#[serde(default)]
pub stats: StmtStatsJson,
}

#[derive(Debug, Clone, Deserialize, Default)]
pub struct StmtStats {
pub total_duration_micros: u64,
pub rows_inserted: u64,
pub rows_updated: u64,
pub rows_deleted: u64,
pub total_rows: usize,
}

impl Sum<StmtStats> for StmtStats {
fn sum<I: Iterator<Item = StmtStats>>(iter: I) -> Self {
iter.fold(StmtStats::default(), Add::add)
}
}

impl Add for StmtStats {
type Output = Self;

fn add(self, rhs: Self) -> Self::Output {
Self {
total_duration_micros: self.total_duration_micros + rhs.total_duration_micros,
rows_inserted: self.rows_inserted + rhs.rows_inserted,
rows_deleted: self.rows_deleted + rhs.rows_deleted,
rows_updated: self.rows_updated + rhs.rows_updated,
total_rows: self.total_rows + rhs.total_rows,
}
}
}

impl From<&StmtResultJson<'_>> for StmtStats {
fn from(value: &StmtResultJson<'_>) -> Self {
Self {
total_duration_micros: value.total_duration_micros,
rows_inserted: value.stats.rows_inserted,
rows_deleted: value.stats.rows_deleted,
rows_updated: value.stats.rows_updated,
total_rows: value.rows.len(),
}
}
}

pub fn from_json_seed<'de, T: serde::de::DeserializeSeed<'de>>(
Expand Down
Loading
Loading