sql-values 0.7.0

SQL values, with PostgreSQL's value rules beside readers of the text PostgreSQL, MySQL and SQLite emit
Documentation

sql-values

crates.io docs.rs CI Codecov MSRV license

sql-values holds the semantics of SQL values. It ports PostgreSQL's own value rules (input, canonical output, to_char templates of dates and times, order, also between values of different types, numeric, date and interval arithmetic, date_trunc, extract and date_part, and the explicit cast of a literal or a value), each checked against PostgreSQL 18, and it reads the text forms PostgreSQL, MySQL and SQLite emit for scalar values, so every wire-text consumer decodes the same spelling to the same Rust value. Dates and times are held as PostgreSQL stores them, over its whole range, and timestamptz arithmetic runs in a named time zone from jiff's bundled IANA database. Arrays are held the way PostgreSQL holds them, with their dimensions and lower bounds, and read and printed as array_in and array_out do. A jsonb is read, printed and ordered as jsonb_in, jsonb_out and its btree operators do, and a uuid as uuid_in, uuid_out and uuid_cmp do.

use sql_values::{
    parse_bool,
    postgres::{
        CastTarget, InputContext, IntervalError, Literal, PgJsonb, PgNumeric, PgTimeZone,
        PgTimestampTz, cast_literal, parse_interval, parse_timestamp,
    },
};

assert_eq!(parse_bool("t"), Some(true));

let span = parse_interval("1.5 days").unwrap();
assert_eq!(span.to_string(), "1 day 12:00:00");
assert_eq!(parse_interval("p1d"), Err(IntervalError::InvalidSyntax));

let at = parse_timestamp("294276-12-31 10:00:00").unwrap();
let day = parse_interval("1 day").unwrap();
assert_eq!(day.subtract_from_timestamp(at).unwrap().to_string(), "294276-12-30 10:00:00");

#[cfg(feature = "tzdb")]
{
    let rome = PgTimeZone::parse("Europe/Rome").unwrap();
    let noon = PgTimestampTz::parse("2026-03-29 12:00:00+02", InputContext::new()).unwrap();
    let before = day.subtract_from_timestamptz(noon, &rome).unwrap();
    assert_eq!(before.to_string(), "2026-03-28 11:00:00+00");
}

let level = cast_literal(Literal::Number("2.7"), CastTarget::Integer).unwrap();
assert_eq!(level.to_string(), "3");

let third = PgNumeric::parse("10").unwrap().checked_div(&PgNumeric::from(3)).unwrap();
assert_eq!(third.to_string(), "3.3333333333333333");

let document = PgJsonb::parse(r#"{"bb": 1.50, "a": [true, null]}"#).unwrap();
assert_eq!(document.to_string(), r#"{"a": [true, null], "bb": 1.50}"#);

The crate supports no_std. IANA time zone names such as Europe/Rome load from jiff's bundled database through the tzdb feature, and without it a zone setting reads as in a PostgreSQL with no time zone files, accepting GMT, offsets and POSIX rules. Decimal parsing is available through the decimal feature, conversions to and from jiff's date and time types through the jiff feature, and serde derives for interval, date and time values through the serde feature.