Expand description
§sql-values
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§
- Char
Length - The length of a
varchar(n)orchar(n)type, in characters. - Input
Context - The session state PostgreSQL’s input functions read, which the caller supplies.
- Interval
Sum - The running state of PostgreSQL’s
sum(interval)andavg(interval). - Interval
Typmod - The modifier of an interval type, the fields it keeps and the fractional-second digits it
rounds to, where the default is plain
interval. - Numeric
Typmod - 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
intervalas 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 ascmp_numericscompares it, withNaNequal to itself and aboveInfinity. - PgTime
- A PostgreSQL
time, microseconds from midnight to 24:00:00 inclusive. - PgTime
Tz - 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, astimetz_cmporders it. - PgTime
Zone - A time zone as PostgreSQL’s
TimeZonesetting reads it, equal to another whenSHOW timezonenames 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. - PgTimestamp
Tz - A PostgreSQL
timestamptz, the instant as microseconds from 2000-01-01 00:00:00 UTC, over the range ofPgTimestamp, printed as underTimeZoneUTC. - Time
Zone Abbreviations - A
timezone_abbreviationsset, the abbreviations a literal may write for its zone.
Enums§
- Cast
Error - Why PostgreSQL refuses a cast, one variant per message it raises, or why this crate declines to answer for it.
- Cast
Target - The type a literal is cast to, with its modifier.
- Cast
Value - The value a cast produces, printed by
Displayas the type’s output function prints it under the defaultDateStyleofISO. - Date
Time Error - Why PostgreSQL refuses a date or time, one variant per message it raises, or why this crate declines to answer for it.
- Interval
Error - Why PostgreSQL refuses to read an interval or to compute one, one variant per message it raises.
- Interval
Fields - 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.
- Shift
Error - Why a timestamp or time shifted by an interval has no value here.
- Time
Zone Abbreviations Error - Why PostgreSQL refuses a time zone abbreviation file, one variant per message
tzparser.craises. - Time
Zone Error - Why PostgreSQL refuses a
TimeZonesetting, one variant per message or detail it raises.
Functions§
- cast_
literal - The value PostgreSQL 18’s explicit cast
literal::targetproduces, or the error it raises, with a database encoding of UTF-8. - parse_
bool - Parse
t,f,1, or0as a boolean. - parse_
date - Parse a
YYYY-MM-DDdate with a year of four or more digits and PostgreSQL’s trailingBC, orinfinityand-infinity. Year0000, 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
NaNspellings. - parse_
i64 - Parse integer text with an optional leading sign.
- parse_
interval - Parse an interval as PostgreSQL 18’s
interval_inreads it under the defaultIntervalStyle. - 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 orTseparator, a year of four or more digits, PostgreSQL’s trailingBC, andinfinityand-infinity, the fraction rounded to the microsecond. Year0000, as MySQL and SQLite print 1 BC, is year 0. - parse_
timestamp_ tz - Parse a
parse_timestamplayout followed by aZ,±HH,±HHMM,±HH:MMor±HH:MM:SSoffset, then PostgreSQL’s optionalBC, orinfinityand-infinity.