Expand description
SQL-shaped result sets (#235): a query’s rows as a database shell shows them.
A ResultSet is column names, their types (a Schema, the model a
row source carries) and rows, rendered with:
- typed alignment: integers, floats and decimals right-justified, booleans centred, text, dates, timestamps and everything else left. A column with no type in the schema is right-justified when every sampled non-null cell is a number.
- NULL styling: a null cell reads
NULLin thetable.nullstyle (dim italic), so it never looks like an empty string, which stays empty. - a row count:
(3 rows),(1 row), with the elapsed time when the caller gives one:(3 rows, 12ms).
There is no database connection: rows come from an adapter (Rows,
a RowWindow of any RowSource) or from the caller through
VirtualRows. Rendering goes through rich_ext’s
VirtualTable, so a large result shows one window
(limit rows from offset,
default the first DEFAULT_LIMIT) with its position above the count.
use std::time::Duration;
use rich::Console;
use rich_data::sql::ResultSet;
use rich_data::{DataType, Field, Rows, Schema, Value};
let schema = Schema::new([
Field::new("id", DataType::Integer),
Field::new("name", DataType::String),
Field::new("active", DataType::Boolean),
Field::new("balance", DataType::decimal()),
]);
let mut rows = Rows::new(["id", "name", "active", "balance"]).with_schema(schema);
rows.push([Value::Int(1), "ada".into(), "true".into(), Value::Float(12.5)]);
rows.push([Value::Int(2), "".into(), "false".into(), Value::Null]);
rows.push([Value::Int(10), Value::Null, "true".into(), Value::Float(-3.0)]);
let result = ResultSet::new(rows).elapsed(Duration::from_millis(12));
let out = Console::builder().width(50).build().render_export(&result);
assert_eq!(
out,
"\
┏━━━━┳━━━━━━┳━━━━━━━━┳━━━━━━━━━┓
┃ id ┃ name ┃ active ┃ balance ┃
┡━━━━╇━━━━━━╇━━━━━━━━╇━━━━━━━━━┩
│ 1 │ ada │ true │ 12.5 │
│ 2 │ │ false │ NULL │
│ 10 │ NULL │ true │ -3 │
└────┴──────┴────────┴─────────┘
(3 rows, 12ms)
"
);Structs§
- Result
Set - A query result: columns, their types and rows; see the module docs.
Constants§
- DEFAULT_
LIMIT - Rows a
ResultSetshows by default.
Functions§
- alignment
- How a column of
data_typeis justified: numbers right, booleans centred, everything else left. - typed_
columns - A column per name, justified by its type in
schema(by name, else by position), or, without one, right-justified when every non-null cell ofsamplein that column is a number.