Skip to content

Commit 77b868b

Browse files
committed
improvements after review
1 parent 1b916b8 commit 77b868b

File tree

4 files changed

+124
-96
lines changed

4 files changed

+124
-96
lines changed

src/ast/mod.rs

Lines changed: 63 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ use helpers::{
2929
};
3030

3131
use core::cmp::Ordering;
32+
use core::fmt::Formatter;
3233
use core::ops::Deref;
3334
use core::{
3435
fmt::{self, Display},
3536
hash,
3637
};
37-
3838
#[cfg(feature = "serde")]
3939
use serde::{Deserialize, Serialize};
4040

@@ -3315,18 +3315,8 @@ pub enum Statement {
33153315
secret_type: Ident,
33163316
options: Vec<SecretOption>,
33173317
},
3318-
/// ```sql
3319-
/// CREATE SERVER
3320-
/// ```
3321-
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-createserver.html)
3322-
CreateServer {
3323-
name: ObjectName,
3324-
if_not_exists: bool,
3325-
server_type: Option<Ident>,
3326-
version: Option<Ident>,
3327-
foreign_data_wrapper: ObjectName,
3328-
options: Option<Vec<ServerOption>>,
3329-
},
3318+
/// A `CREATE SERVER` statement.
3319+
CreateServer(CreateServerStatement),
33303320
/// ```sql
33313321
/// CREATE POLICY
33323322
/// ```
@@ -5187,35 +5177,8 @@ impl fmt::Display for Statement {
51875177
write!(f, " )")?;
51885178
Ok(())
51895179
}
5190-
Statement::CreateServer {
5191-
name,
5192-
if_not_exists,
5193-
server_type,
5194-
version,
5195-
foreign_data_wrapper,
5196-
options,
5197-
} => {
5198-
write!(
5199-
f,
5200-
"CREATE SERVER {if_not_exists}{name} ",
5201-
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" },
5202-
)?;
5203-
5204-
if let Some(st) = server_type {
5205-
write!(f, "TYPE {st} ")?;
5206-
}
5207-
5208-
if let Some(v) = version {
5209-
write!(f, "VERSION {v} ")?;
5210-
}
5211-
5212-
write!(f, "FOREIGN DATA WRAPPER {foreign_data_wrapper}")?;
5213-
5214-
if let Some(o) = options {
5215-
write!(f, " OPTIONS ({o})", o = display_comma_separated(o))?;
5216-
}
5217-
5218-
Ok(())
5180+
Statement::CreateServer(stmt) => {
5181+
write!(f, "{stmt}")
52195182
}
52205183
Statement::CreatePolicy {
52215184
name,
@@ -8015,6 +7978,63 @@ impl fmt::Display for SecretOption {
80157978
}
80167979
}
80177980

7981+
/// A `CREATE SERVER` statement.
7982+
///
7983+
/// Examples:
7984+
/// ```sql
7985+
/// CREATE SERVER [ IF NOT EXISTS ] server_name [ TYPE 'server_type' ] [ VERSION 'server_version' ]
7986+
/// FOREIGN DATA WRAPPER fdw_name
7987+
/// [ OPTIONS ( option 'value' [, ... ] ) ]
7988+
/// ```
7989+
///
7990+
/// [PostgreSQL Documentation](https://www.postgresql.org/docs/current/sql-createserver.html)
7991+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
7992+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
7993+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
7994+
pub struct CreateServerStatement {
7995+
pub name: ObjectName,
7996+
pub if_not_exists: bool,
7997+
pub server_type: Option<Ident>,
7998+
pub version: Option<Ident>,
7999+
pub foreign_data_wrapper: ObjectName,
8000+
pub options: Option<Vec<CreateServerOption>>,
8001+
}
8002+
8003+
impl fmt::Display for CreateServerStatement {
8004+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
8005+
let CreateServerStatement {
8006+
name,
8007+
if_not_exists,
8008+
server_type,
8009+
version,
8010+
foreign_data_wrapper,
8011+
options,
8012+
} = self;
8013+
8014+
write!(
8015+
f,
8016+
"CREATE SERVER {if_not_exists}{name} ",
8017+
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" },
8018+
)?;
8019+
8020+
if let Some(st) = server_type {
8021+
write!(f, "TYPE {st} ")?;
8022+
}
8023+
8024+
if let Some(v) = version {
8025+
write!(f, "VERSION {v} ")?;
8026+
}
8027+
8028+
write!(f, "FOREIGN DATA WRAPPER {foreign_data_wrapper}")?;
8029+
8030+
if let Some(o) = options {
8031+
write!(f, " OPTIONS ({o})", o = display_comma_separated(o))?;
8032+
}
8033+
8034+
Ok(())
8035+
}
8036+
}
8037+
80188038
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
80198039
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
80208040
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
@@ -8023,7 +8043,7 @@ pub struct CreateServerOption {
80238043
pub value: Ident,
80248044
}
80258045

8026-
impl fmt::Display for ServerOption {
8046+
impl fmt::Display for CreateServerOption {
80278047
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
80288048
write!(f, "{} {}", self.key, self.value)
80298049
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ extern crate alloc;
163163
#[macro_use]
164164
#[cfg(test)]
165165
extern crate pretty_assertions;
166+
extern crate core;
166167

167168
pub mod ast;
168169
#[macro_use]

src/parser/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15840,19 +15840,19 @@ impl<'a> Parser<'a> {
1584015840
options = Some(self.parse_comma_separated(|p| {
1584115841
let key = p.parse_identifier()?;
1584215842
let value = p.parse_identifier()?;
15843-
Ok(ServerOption { key, value })
15843+
Ok(CreateServerOption { key, value })
1584415844
})?);
1584515845
self.expect_token(&Token::RParen)?;
1584615846
}
1584715847

15848-
Ok(CreateServer {
15848+
Ok(CreateServer(CreateServerStatement {
1584915849
name,
1585015850
if_not_exists: ine,
1585115851
server_type,
1585215852
version,
1585315853
foreign_data_wrapper,
1585415854
options,
15855-
})
15855+
}))
1585615856
}
1585715857

1585815858
/// The index of the first unprocessed token.

tests/sqlparser_postgres.rs

Lines changed: 57 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6234,21 +6234,21 @@ fn parse_ts_datatypes() {
62346234

62356235
#[test]
62366236
fn parse_create_server() {
6237-
assert_eq!(
6238-
pg_and_generic().verified_stmt("CREATE SERVER myserver FOREIGN DATA WRAPPER postgres_fdw"),
6239-
Statement::CreateServer {
6240-
name: ObjectName::from(vec!["myserver".into()]),
6241-
if_not_exists: false,
6242-
server_type: None,
6243-
version: None,
6244-
foreign_data_wrapper: ObjectName::from(vec!["postgres_fdw".into()]),
6245-
options: None,
6246-
}
6247-
);
6248-
6249-
assert_eq!(
6250-
pg_and_generic().verified_stmt("CREATE SERVER IF NOT EXISTS myserver TYPE 'server_type' VERSION 'server_version' FOREIGN DATA WRAPPER postgres_fdw"),
6251-
Statement::CreateServer {
6237+
let test_cases = vec![
6238+
(
6239+
"CREATE SERVER myserver FOREIGN DATA WRAPPER postgres_fdw",
6240+
CreateServerStatement {
6241+
name: ObjectName::from(vec!["myserver".into()]),
6242+
if_not_exists: false,
6243+
server_type: None,
6244+
version: None,
6245+
foreign_data_wrapper: ObjectName::from(vec!["postgres_fdw".into()]),
6246+
options: None,
6247+
},
6248+
),
6249+
(
6250+
"CREATE SERVER IF NOT EXISTS myserver TYPE 'server_type' VERSION 'server_version' FOREIGN DATA WRAPPER postgres_fdw",
6251+
CreateServerStatement {
62526252
name: ObjectName::from(vec!["myserver".into()]),
62536253
if_not_exists: true,
62546254
server_type: Some(Ident {
@@ -6264,42 +6264,49 @@ fn parse_create_server() {
62646264
foreign_data_wrapper: ObjectName::from(vec!["postgres_fdw".into()]),
62656265
options: None,
62666266
}
6267-
);
6268-
6269-
assert_eq!(
6270-
pg_and_generic().verified_stmt("CREATE SERVER myserver2 FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'foo', dbname 'foodb', port '5432')"),
6271-
Statement::CreateServer {
6272-
name: ObjectName::from(vec!["myserver2".into()]),
6273-
if_not_exists: false,
6274-
server_type: None,
6275-
version: None,
6276-
foreign_data_wrapper: ObjectName::from(vec!["postgres_fdw".into()]),
6277-
options: Some(vec![
6278-
ServerOption {
6279-
key: "host".into(),
6280-
value: Ident {
6281-
value: "foo".to_string(),
6282-
quote_style: Some('\''),
6283-
span: Span::empty(),
6267+
),
6268+
(
6269+
"CREATE SERVER myserver2 FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'foo', dbname 'foodb', port '5432')",
6270+
CreateServerStatement {
6271+
name: ObjectName::from(vec!["myserver2".into()]),
6272+
if_not_exists: false,
6273+
server_type: None,
6274+
version: None,
6275+
foreign_data_wrapper: ObjectName::from(vec!["postgres_fdw".into()]),
6276+
options: Some(vec![
6277+
CreateServerOption {
6278+
key: "host".into(),
6279+
value: Ident {
6280+
value: "foo".to_string(),
6281+
quote_style: Some('\''),
6282+
span: Span::empty(),
6283+
},
62846284
},
6285-
},
6286-
ServerOption {
6287-
key: "dbname".into(),
6288-
value: Ident {
6289-
value: "foodb".to_string(),
6290-
quote_style: Some('\''),
6291-
span: Span::empty(),
6285+
CreateServerOption {
6286+
key: "dbname".into(),
6287+
value: Ident {
6288+
value: "foodb".to_string(),
6289+
quote_style: Some('\''),
6290+
span: Span::empty(),
6291+
},
62926292
},
6293-
},
6294-
ServerOption {
6295-
key: "port".into(),
6296-
value: Ident {
6297-
value: "5432".to_string(),
6298-
quote_style: Some('\''),
6299-
span: Span::empty(),
6293+
CreateServerOption {
6294+
key: "port".into(),
6295+
value: Ident {
6296+
value: "5432".to_string(),
6297+
quote_style: Some('\''),
6298+
span: Span::empty(),
6299+
},
63006300
},
6301-
},
6302-
]),
6303-
}
6304-
)
6301+
]),
6302+
}
6303+
)
6304+
];
6305+
6306+
for (sql, expected) in test_cases {
6307+
let Statement::CreateServer(stmt) = pg_and_generic().verified_stmt(sql) else {
6308+
unreachable!()
6309+
};
6310+
assert_eq!(stmt, expected);
6311+
}
63056312
}

0 commit comments

Comments
 (0)