Skip to main content

Module sql

Module sql 

Source
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 NULL in the table.null style (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§

ResultSet
A query result: columns, their types and rows; see the module docs.

Constants§

DEFAULT_LIMIT
Rows a ResultSet shows by default.

Functions§

alignment
How a column of data_type is 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 of sample in that column is a number.