sql/statements/
comment.rs1use std::fmt::{self, Display, Formatter};
16
17use serde::Serialize;
18use sqlparser_derive::{Visit, VisitMut};
19
20use crate::ast::{Ident, ObjectName};
21
22#[derive(Debug, Clone, PartialEq, Eq, Visit, VisitMut, Serialize)]
32pub struct Comment {
33 pub object: CommentObject,
34 pub comment: Option<String>,
35}
36
37#[derive(Debug, Clone, PartialEq, Eq, Visit, VisitMut, Serialize)]
38pub enum CommentObject {
39 Table(ObjectName),
40 Column { table: ObjectName, column: Ident },
41 Flow(ObjectName),
42}
43
44impl Display for Comment {
45 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
46 write!(f, "COMMENT ON {} IS ", self.object)?;
47 match &self.comment {
48 Some(comment) => {
49 let escaped = comment.replace('\'', "''");
50 write!(f, "'{}'", escaped)
51 }
52 None => f.write_str("NULL"),
53 }
54 }
55}
56
57impl Display for CommentObject {
58 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
59 match self {
60 CommentObject::Table(name) => write!(f, "TABLE {}", name),
61 CommentObject::Column { table, column } => {
62 write!(f, "COLUMN {}.{}", table, column)
63 }
64 CommentObject::Flow(name) => write!(f, "FLOW {}", name),
65 }
66 }
67}