gluesql_core/ast/
literal.rs1use {
2 crate::ast::ToSql,
3 bigdecimal::BigDecimal,
4 serde::{Deserialize, Serialize},
5 strum_macros::Display,
6};
7
8#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
9pub enum Literal {
10 Number(BigDecimal),
11 QuotedString(String),
12}
13
14impl ToSql for Literal {
15 fn to_sql(&self) -> String {
16 match self {
17 Literal::Number(n) => n.to_string(),
18 Literal::QuotedString(qs) => {
19 let escaped = qs.replace('\'', "''");
20 format!("'{escaped}'")
21 }
22 }
23 }
24}
25
26#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Display)]
27#[strum(serialize_all = "SCREAMING_SNAKE_CASE")]
28pub enum DateTimeField {
29 Year,
30 Month,
31 Day,
32 Hour,
33 Minute,
34 Second,
35}
36
37#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Display)]
38#[strum(serialize_all = "SCREAMING_SNAKE_CASE")]
39pub enum TrimWhereField {
40 Both,
41 Leading,
42 Trailing,
43}
44
45#[cfg(test)]
46mod tests {
47 use {
48 crate::ast::{Literal, ToSql},
49 bigdecimal::BigDecimal,
50 };
51
52 #[test]
53 fn to_sql() {
54 assert_eq!("123", Literal::Number(BigDecimal::from(123)).to_sql());
55 assert_eq!(
56 "'hello'",
57 Literal::QuotedString("hello".to_owned()).to_sql()
58 );
59 assert_eq!(
60 "'can''t'",
61 Literal::QuotedString("can't".to_owned()).to_sql()
62 );
63 }
64}