proof_of_sql/base/database/
literal_value.rs1use crate::base::{
2    database::ColumnType,
3    math::{decimal::Precision, i256::I256},
4    posql_time::{PoSQLTimeUnit, PoSQLTimeZone},
5    scalar::{Scalar, ScalarExt},
6};
7use alloc::{string::String, vec::Vec};
8use serde::{Deserialize, Serialize};
9
10#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
16#[non_exhaustive]
17pub enum LiteralValue {
18    Boolean(bool),
20    Uint8(u8),
22    TinyInt(i8),
24    SmallInt(i16),
26    Int(i32),
28    BigInt(i64),
30
31    VarChar(String),
35    VarBinary(Vec<u8>),
38    Int128(i128),
40    Decimal75(Precision, i8, I256),
43    Scalar([u64; 4]),
45    TimeStampTZ(PoSQLTimeUnit, PoSQLTimeZone, i64),
48}
49
50impl LiteralValue {
51    #[must_use]
53    pub fn column_type(&self) -> ColumnType {
54        match self {
55            Self::Boolean(_) => ColumnType::Boolean,
56            Self::Uint8(_) => ColumnType::Uint8,
57            Self::TinyInt(_) => ColumnType::TinyInt,
58            Self::SmallInt(_) => ColumnType::SmallInt,
59            Self::Int(_) => ColumnType::Int,
60            Self::BigInt(_) => ColumnType::BigInt,
61            Self::VarChar(_) => ColumnType::VarChar,
62            Self::VarBinary(_) => ColumnType::VarBinary,
63            Self::Int128(_) => ColumnType::Int128,
64            Self::Scalar(_) => ColumnType::Scalar,
65            Self::Decimal75(precision, scale, _) => ColumnType::Decimal75(*precision, *scale),
66            Self::TimeStampTZ(tu, tz, _) => ColumnType::TimestampTZ(*tu, *tz),
67        }
68    }
69
70    pub(crate) fn to_scalar<S: Scalar>(&self) -> S {
72        match self {
73            Self::Boolean(b) => b.into(),
74            Self::Uint8(i) => i.into(),
75            Self::TinyInt(i) => i.into(),
76            Self::SmallInt(i) => i.into(),
77            Self::Int(i) => i.into(),
78            Self::BigInt(i) => i.into(),
79            Self::VarChar(str) => str.into(),
80            Self::VarBinary(bytes) => S::from_byte_slice_via_hash(bytes),
81            Self::Decimal75(_, _, i) => i.into_scalar(),
82            Self::Int128(i) => i.into(),
83            Self::Scalar(limbs) => (*limbs).into(),
84            Self::TimeStampTZ(_, _, time) => time.into(),
85        }
86    }
87}