MCPcopy Create free account
hub / github.com/clockworklabs/SpacetimeDB / build_table

Function build_table

crates/cli/src/subcommands/sql.rs:252–284  ·  view source on GitHub ↗

Generates a [`tabled::Table`] from a schema and rows, using the style of a psql table.

(
    client: PsqlClient,
    schema: &ProductType,
    rows: impl Iterator<Item = Result<ProductValue, E>>,
)

Source from the content-addressed store, hash-verified

250
251/// Generates a [`tabled::Table`] from a schema and rows, using the style of a psql table.
252fn build_table<E>(
253 client: PsqlClient,
254 schema: &ProductType,
255 rows: impl Iterator<Item = Result<ProductValue, E>>,
256) -> Result<tabled::Table, E> {
257 let mut builder = tabled::builder::Builder::default();
258 builder.set_header(schema.elements.iter().enumerate().map(|(i, e)| {
259 e.name
260 .as_ref()
261 .map(|n| n.to_string())
262 .unwrap_or_else(|| format!("column {i}"))
263 }));
264
265 let ty = Typespace::EMPTY.with_type(schema);
266 for row in rows {
267 let row = row?;
268 builder.push_record(ty.with_values(&row).enumerate().map(|(idx, value)| {
269 let ty = satn::PsqlType {
270 client,
271 tuple: ty.ty(),
272 field: &ty.ty().elements[idx],
273 idx,
274 };
275
276 satn::PsqlWrapper { ty, value }.to_string()
277 }));
278 }
279
280 let mut table = builder.build();
281 table.with(tabled::settings::Style::psql());
282
283 Ok(table)
284}
285
286#[cfg(test)]
287mod tests {

Callers 2

stmt_result_to_tableFunction · 0.85
expect_psql_tableFunction · 0.85

Calls 10

with_valuesMethod · 0.80
iterMethod · 0.65
OkFunction · 0.50
mapMethod · 0.45
as_refMethod · 0.45
to_stringMethod · 0.45
with_typeMethod · 0.45
tyMethod · 0.45
buildMethod · 0.45
withMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…