sql_values/postgres/
const_expr.rs1use core::fmt::{self, Write};
4
5use super::BuiltinType;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum ShowType {
10 Never,
12 IfNeeded,
14 Always,
16}
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub struct ConstExprContext<'a> {
21 pub standard_conforming_strings: bool,
23 pub collation: Option<&'a str>,
25}
26
27impl Default for ConstExprContext<'_> {
28 fn default() -> Self {
29 Self {
30 standard_conforming_strings: true,
31 collation: None,
32 }
33 }
34}
35
36#[derive(Debug, Clone, Copy)]
51pub struct ConstExpr<'a> {
52 output: Option<&'a str>,
53 target: BuiltinType,
54 show_type: ShowType,
55 context: ConstExprContext<'a>,
56}
57
58impl<'a> ConstExpr<'a> {
59 #[must_use]
61 pub fn new(output: Option<&'a str>, target: BuiltinType, show_type: ShowType) -> Self {
62 Self {
63 output,
64 target,
65 show_type,
66 context: ConstExprContext::default(),
67 }
68 }
69
70 #[must_use]
72 pub const fn with_context(mut self, context: ConstExprContext<'a>) -> Self {
73 self.context = context;
74 self
75 }
76
77 #[must_use]
88 pub fn needs_type_label(&self) -> bool {
89 self.decoration().1
90 }
91
92 fn decoration(&self) -> (bool, bool) {
94 let (quoted, needed) = match self.output {
95 Some(output) => match self.target {
96 BuiltinType::Boolean => (false, false),
97 BuiltinType::Integer if !output.starts_with('-') => (false, false),
98 BuiltinType::Numeric(modifier)
99 if output.as_bytes().first().is_some_and(u8::is_ascii_digit)
100 && output.contains(['e', 'E', '.']) =>
101 {
102 (false, modifier.is_some())
103 }
104 _ => (true, self.target != BuiltinType::Unknown),
105 },
106 None => (false, true),
107 };
108 (
109 quoted,
110 match self.show_type {
111 ShowType::Never => false,
112 ShowType::IfNeeded => needed,
113 ShowType::Always => true,
114 },
115 )
116 }
117}
118
119impl fmt::Display for ConstExpr<'_> {
120 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
121 let (quoted, needs_label) = self.decoration();
122 if let Some(output) = self.output {
123 if quoted {
124 quote_literal(formatter, output, self.context.standard_conforming_strings)?;
125 } else if self.target == BuiltinType::Boolean {
126 formatter.write_str(if output == "t" { "true" } else { "false" })?;
127 } else {
128 formatter.write_str(output)?;
129 }
130 } else {
131 formatter.write_str("NULL")?;
132 }
133 if needs_label {
134 write!(formatter, "::{}", self.target)?;
135 }
136 if self.show_type != ShowType::Never
137 && let Some(collation) = self.context.collation
138 {
139 write!(formatter, " COLLATE {collation}")?;
140 }
141 Ok(())
142 }
143}
144
145fn quote_literal(
147 formatter: &mut fmt::Formatter<'_>,
148 output: &str,
149 standard_strings: bool,
150) -> fmt::Result {
151 formatter.write_char('\'')?;
152 let mut start = 0;
153 for (index, character) in output.char_indices() {
154 if character == '\'' || (character == '\\' && !standard_strings) {
155 formatter.write_str(&output[start..index])?;
156 formatter.write_char(character)?;
157 start = index;
158 }
159 }
160 formatter.write_str(&output[start..])?;
161 formatter.write_char('\'')
162}