Skip to main content

Crate sql_values

Crate sql_values 

Source
Expand description

§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, order, arithmetic and the explicit cast of a literal), 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.

use sql_values::{
    CastTarget, InputContext, IntervalError, Literal, PgTimeZone, PgTimestampTz, cast_literal,
    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("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");

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");

The crate supports no_std. 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.

Structs§

CharLength
The length of a varchar(n) or char(n) type, in characters.
InputContext
The session state PostgreSQL’s input functions read, which the caller supplies.
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.
NumericTypmod
The modifier of a numeric(p, s) type, the precision and scale it rounds to.
PgDate
A PostgreSQL date, days from 2000-01-01, from 4714-11-24 BC to 5874897-12-31 and the two infinities, ordered as PostgreSQL orders it.
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.
PgNumeric
A PostgreSQL numeric, equal, ordered and hashed by value as cmp_numerics compares it, with NaN equal to itself and above Infinity.
PgTime
A PostgreSQL time, microseconds from midnight to 24:00:00 inclusive.
PgTimeTz
A PostgreSQL timetz, a time of day with the offset east of UTC it was written with, ordered by the instant and then by the offset, as timetz_cmp orders it.
PgTimeZone
A time zone as PostgreSQL’s TimeZone setting reads it, equal to another when SHOW timezone names both alike.
PgTimestamp
A PostgreSQL timestamp, microseconds from 2000-01-01 00:00:00, from 4714-11-24 BC to 294276-12-31 23:59:59.999999 and the two infinities.
PgTimestampTz
A PostgreSQL timestamptz, the instant as microseconds from 2000-01-01 00:00:00 UTC, over the range of PgTimestamp, printed as under TimeZone UTC.
TimeZoneAbbreviations
A timezone_abbreviations set, the abbreviations a literal may write for its zone.

Enums§

CastError
Why PostgreSQL refuses a cast, one variant per message it raises, or why this crate declines to answer for it.
CastTarget
The type a literal is cast to, with its modifier.
CastValue
The value a cast produces, printed by Display as the type’s output function prints it under the default DateStyle of ISO.
DateOrder
DateOrder, the field order of DateStyle for a date written in numbers alone.
DateTimeError
Why PostgreSQL refuses a date or time, one variant per message it raises, or why this crate cannot answer for it without more session state or a wider zone database.
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.
Literal
A literal as written before ::, its quotes and escapes already decoded.
PgType
A type PostgreSQL names in a cast’s error, printed as format_type names it.
ShiftError
Why a timestamp or time shifted by an interval has no value here.
TimeZoneAbbreviationsError
Why PostgreSQL refuses a time zone abbreviation file, one variant per message tzparser.c raises.
TimeZoneError
Why PostgreSQL refuses a TimeZone setting, one variant per message or detail it raises.

Functions§

cast_literal
The value PostgreSQL 18’s explicit cast literal::target produces, or the error it raises, with a database encoding of UTF-8.
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, or infinity and -infinity. Year 0000, as MySQL and SQLite print 1 BC, is year 0.
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, rounded to the microsecond, up to 24:00:00.
parse_timestamp
Parse YYYY-MM-DD HH:MM:SS[.f] with a space or T separator, a year of four or more digits, PostgreSQL’s trailing BC, and infinity and -infinity, the fraction rounded to the microsecond. Year 0000, as MySQL and SQLite print 1 BC, is year 0.
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, or infinity and -infinity.