Skip to main content

number_constant

Function number_constant 

Source
pub fn number_constant(text: &str) -> Result<CastValue, CastError>
Expand description

The value PostgreSQL 18 makes of a numeric constant, written as Literal::Number takes it, as its lexer and make_const type it. An integer is an integer when it fits, else a bigint when it fits, else a numeric, and a constant with a point or an exponent is a numeric.

ยงErrors

CastError::Unsupported for text that is not one numeric constant, and the error numeric_in raises for a numeric PostgreSQL cannot store.

use sql_values::postgres::{CastValue, number_constant};

assert_eq!(number_constant("2147483647"), Ok(CastValue::Integer(i32::MAX)));
assert_eq!(number_constant("2147483648"), Ok(CastValue::Bigint(2_147_483_648)));
assert_eq!(number_constant("-0x8000_0000"), Ok(CastValue::Integer(i32::MIN)));
let big = number_constant("9223372036854775808").unwrap();
assert!(matches!(big, CastValue::Numeric(_)));
assert_eq!(number_constant("1.50").unwrap().to_string(), "1.50");