Skip to main content

Crate sql_values

Crate sql_values 

Source
Expand description

§sql-values

crates.io docs.rs CI MSRV license

sql-values holds the semantics of SQL values. It ports PostgreSQL’s own value rules (input, canonical output, order and arithmetic), 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.

use sql_values::{IntervalError, parse_bool, 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("2024-01-31 10:00:00").unwrap();
let month = parse_interval("1 mon").unwrap();
assert_eq!(month.add_to_timestamp(at).unwrap().to_string(), "2024-02-29 10:00:00");

The crate supports no_std. Decimal parsing is available through the decimal feature, and serde derives for interval values through the serde feature.

Structs§

IntervalSum
The running state of PostgreSQL’s sum(interval) and avg(interval).
IntervalTypmod
The modifier of an interval type, the fields it keeps and the fractional-second digits it rounds to, where the default is plain interval.
PgInterval
A PostgreSQL interval as months, days and microseconds, equal, ordered and hashed by PostgreSQL’s span with a month as 30 days and a day as 24 hours.

Enums§

IntervalError
Why PostgreSQL refuses to read an interval or to compute one, one variant per message it raises.
IntervalFields
The fields an interval type keeps, as in interval day to second.
ShiftError
Why a timestamp or time shifted by an interval has no value here.

Functions§

parse_bool
Parse t, f, 1, or 0 as a boolean.
parse_date
Parse a YYYY-MM-DD date with a year of four or more digits and PostgreSQL’s trailing BC.
parse_decimal
Parse an arbitrary-precision decimal.
parse_f64
Parse a 64-bit float, including PostgreSQL infinity and NaN spellings.
parse_i64
Parse integer text with an optional leading sign.
parse_interval
Parse an interval as PostgreSQL 18’s interval_in reads it under the default IntervalStyle.
parse_interval_typmod
Parse an interval literal of a restricted type as interval '…' <fields> reads it, which differs from casting a text value to that type.
parse_pg_bytea_hex
Parse PostgreSQL \x-prefixed bytea hex with no intermediate allocation.
parse_time
Parse HH:MM:SS[.f] with one to nine fraction digits.
parse_timestamp
Parse YYYY-MM-DD HH:MM:SS[.f] with a space or T separator, a year of four or more digits and PostgreSQL’s trailing BC.
parse_timestamp_tz
Parse a parse_timestamp layout followed by a Z, ±HH, ±HHMM, ±HH:MM or ±HH:MM:SS offset, then PostgreSQL’s optional BC.