Skip to content

Commit dfc180b

Browse files
Refactored introducing struct CreateDomain
1 parent 2bd50b1 commit dfc180b

File tree

4 files changed

+58
-38
lines changed

4 files changed

+58
-38
lines changed

src/ast/ddl.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2153,6 +2153,50 @@ impl fmt::Display for ClusteredBy {
21532153
}
21542154
}
21552155

2156+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2157+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2158+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2159+
/// ```sql
2160+
/// CREATE DOMAIN name [ AS ] data_type
2161+
/// [ COLLATE collation ]
2162+
/// [ DEFAULT expression ]
2163+
/// [ domain_constraint [ ... ] ]
2164+
///
2165+
/// where domain_constraint is:
2166+
///
2167+
/// [ CONSTRAINT constraint_name ]
2168+
/// { NOT NULL | NULL | CHECK (expression) }
2169+
/// ```
2170+
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-createdomain.html)
2171+
pub struct CreateDomain {
2172+
/// The name of the domain to be created.
2173+
name: ObjectName,
2174+
/// The data type of the domain.
2175+
data_type: DataType,
2176+
/// The collation of the domain.
2177+
collation: Option<Ident>,
2178+
/// The default value of the domain.
2179+
default: Option<Expr>,
2180+
/// The constraints of the domain.
2181+
constraints: Vec<TableConstraint>,
2182+
}
2183+
2184+
impl fmt::Display for CreateDomain {
2185+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2186+
write!(f, "CREATE DOMAIN {name} AS {data_type}")?;
2187+
if let Some(collation) = collation {
2188+
write!(f, " COLLATE {collation}")?;
2189+
}
2190+
if let Some(default) = default {
2191+
write!(f, " DEFAULT {default}")?;
2192+
}
2193+
if !constraints.is_empty() {
2194+
write!(f, " {}", display_separated(constraints, " "))?;
2195+
}
2196+
Ok(())
2197+
}
2198+
}
2199+
21562200
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
21572201
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
21582202
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]

src/ast/mod.rs

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3979,13 +3979,7 @@ pub enum Statement {
39793979
/// { NOT NULL | NULL | CHECK (expression) }
39803980
/// ```
39813981
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-createdomain.html)
3982-
CreateDomain {
3983-
name: ObjectName,
3984-
data_type: DataType,
3985-
collation: Option<Ident>,
3986-
default: Option<Expr>,
3987-
constraints: Vec<TableConstraint>,
3988-
},
3982+
CreateDomain(CreateDomain),
39893983
/// ```sql
39903984
/// CREATE TYPE <name>
39913985
/// ```
@@ -4532,25 +4526,7 @@ impl fmt::Display for Statement {
45324526
Ok(())
45334527
}
45344528
Statement::CreateFunction(create_function) => create_function.fmt(f),
4535-
Statement::CreateDomain {
4536-
name,
4537-
data_type,
4538-
collation,
4539-
default,
4540-
constraints,
4541-
} => {
4542-
write!(f, "CREATE DOMAIN {name} AS {data_type}")?;
4543-
if let Some(collation) = collation {
4544-
write!(f, " COLLATE {collation}")?;
4545-
}
4546-
if let Some(default) = default {
4547-
write!(f, " DEFAULT {default}")?;
4548-
}
4549-
if !constraints.is_empty() {
4550-
write!(f, " {}", display_separated(constraints, " "))?;
4551-
}
4552-
Ok(())
4553-
}
4529+
Statement::CreateDomain(create_domain) => create_domain.fmt(f),
45544530
Statement::CreateTrigger {
45554531
or_replace,
45564532
is_constraint,

src/parser/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5928,13 +5928,13 @@ impl<'a> Parser<'a> {
59285928
constraints.push(constraint);
59295929
}
59305930

5931-
Ok(Statement::CreateDomain {
5931+
Ok(Statement::CreateDomain(CreateDomain {
59325932
name,
59335933
data_type,
59345934
collation,
59355935
default,
59365936
constraints,
5937-
})
5937+
}))
59385938
}
59395939

59405940
/// ```sql

tests/sqlparser_postgres.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5083,7 +5083,7 @@ fn test_escaped_string_literal() {
50835083
#[test]
50845084
fn parse_create_domain() {
50855085
let sql1 = "CREATE DOMAIN my_domain AS INTEGER CHECK (VALUE > 0)";
5086-
let expected = Statement::CreateDomain {
5086+
let expected = Statement::CreateDomain(CreateDomain {
50875087
name: ObjectName::from(vec![Ident::new("my_domain")]),
50885088
data_type: DataType::Integer(None),
50895089
collation: None,
@@ -5096,15 +5096,15 @@ fn parse_create_domain() {
50965096
right: Box::new(Expr::Value(test_utils::number("0").into())),
50975097
}),
50985098
}],
5099-
};
5099+
});
51005100

51015101
assert_eq!(pg().verified_stmt(sql1), expected);
51025102
}
51035103

51045104
#[test]
51055105
fn parse_create_domain_with_collation() {
51065106
let sql2 = "CREATE DOMAIN my_domain AS INTEGER COLLATE \"en_US\" CHECK (VALUE > 0)";
5107-
let expected = Statement::CreateDomain {
5107+
let expected = Statement::CreateDomain(CreateDomain {
51085108
name: ObjectName::from(vec![Ident::new("my_domain")]),
51095109
data_type: DataType::Integer(None),
51105110
collation: Some(Ident::with_quote('"', "en_US")),
@@ -5117,15 +5117,15 @@ fn parse_create_domain_with_collation() {
51175117
right: Box::new(Expr::Value(test_utils::number("0").into())),
51185118
}),
51195119
}],
5120-
};
5120+
});
51215121

51225122
assert_eq!(pg().verified_stmt(sql2), expected);
51235123
}
51245124

51255125
#[test]
51265126
fn parse_create_domain_with_default() {
51275127
let sql3 = "CREATE DOMAIN my_domain AS INTEGER DEFAULT 1 CHECK (VALUE > 0)";
5128-
let expected = Statement::CreateDomain {
5128+
let expected = Statement::CreateDomain(CreateDomain {
51295129
name: ObjectName::from(vec![Ident::new("my_domain")]),
51305130
data_type: DataType::Integer(None),
51315131
collation: None,
@@ -5138,15 +5138,15 @@ fn parse_create_domain_with_default() {
51385138
right: Box::new(Expr::Value(test_utils::number("0").into())),
51395139
}),
51405140
}],
5141-
};
5141+
});
51425142

51435143
assert_eq!(pg().verified_stmt(sql3), expected);
51445144
}
51455145

51465146
#[test]
51475147
fn parse_create_domain_with_default_and_collation() {
51485148
let sql4 = "CREATE DOMAIN my_domain AS INTEGER COLLATE \"en_US\" DEFAULT 1 CHECK (VALUE > 0)";
5149-
let expected = Statement::CreateDomain {
5149+
let expected = Statement::CreateDomain(CreateDomain {
51505150
name: ObjectName::from(vec![Ident::new("my_domain")]),
51515151
data_type: DataType::Integer(None),
51525152
collation: Some(Ident::with_quote('"', "en_US")),
@@ -5159,15 +5159,15 @@ fn parse_create_domain_with_default_and_collation() {
51595159
right: Box::new(Expr::Value(test_utils::number("0").into())),
51605160
}),
51615161
}],
5162-
};
5162+
});
51635163

51645164
assert_eq!(pg().verified_stmt(sql4), expected);
51655165
}
51665166

51675167
#[test]
51685168
fn parse_create_domain_with_named_constraint() {
51695169
let sql5 = "CREATE DOMAIN my_domain AS INTEGER CONSTRAINT my_constraint CHECK (VALUE > 0)";
5170-
let expected = Statement::CreateDomain {
5170+
let expected = Statement::CreateDomain(CreateDomain {
51715171
name: ObjectName::from(vec![Ident::new("my_domain")]),
51725172
data_type: DataType::Integer(None),
51735173
collation: None,
@@ -5180,7 +5180,7 @@ fn parse_create_domain_with_named_constraint() {
51805180
right: Box::new(Expr::Value(test_utils::number("0").into())),
51815181
}),
51825182
}],
5183-
};
5183+
});
51845184

51855185
assert_eq!(pg().verified_stmt(sql5), expected);
51865186
}

0 commit comments

Comments
 (0)