Skip to content

Commit 91087fc

Browse files
authored
Support CREATE ROLE and DROP ROLE (#598)
* Parse GRANT ROLE and DROP ROLE * Gate create role on dialect * cargo fmt * clippy * no-std * clippy again
1 parent 604f755 commit 91087fc

File tree

7 files changed

+543
-42
lines changed

7 files changed

+543
-42
lines changed

src/ast/mod.rs

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,13 @@ impl fmt::Display for CommentObject {
954954
}
955955
}
956956

957+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
958+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
959+
pub enum Password {
960+
Password(Expr),
961+
NullPassword,
962+
}
963+
957964
/// A top-level statement (SELECT, INSERT, CREATE, etc.)
958965
#[allow(clippy::large_enum_variant)]
959966
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -1109,6 +1116,27 @@ pub enum Statement {
11091116
unique: bool,
11101117
if_not_exists: bool,
11111118
},
1119+
/// CREATE ROLE
1120+
CreateRole {
1121+
names: Vec<ObjectName>,
1122+
if_not_exists: bool,
1123+
// Postgres
1124+
login: Option<bool>,
1125+
inherit: Option<bool>,
1126+
bypassrls: Option<bool>,
1127+
password: Option<Password>,
1128+
superuser: Option<bool>,
1129+
create_db: Option<bool>,
1130+
create_role: Option<bool>,
1131+
replication: Option<bool>,
1132+
connection_limit: Option<Expr>,
1133+
valid_until: Option<Expr>,
1134+
in_role: Vec<Ident>,
1135+
role: Vec<Ident>,
1136+
admin: Vec<Ident>,
1137+
// MSSQL
1138+
authorization_owner: Option<ObjectName>,
1139+
},
11121140
/// ALTER TABLE
11131141
AlterTable {
11141142
/// Table name
@@ -1963,6 +1991,90 @@ impl fmt::Display for Statement {
19631991
table_name = table_name,
19641992
columns = display_separated(columns, ",")
19651993
),
1994+
Statement::CreateRole {
1995+
names,
1996+
if_not_exists,
1997+
inherit,
1998+
login,
1999+
bypassrls,
2000+
password,
2001+
create_db,
2002+
create_role,
2003+
superuser,
2004+
replication,
2005+
connection_limit,
2006+
valid_until,
2007+
in_role,
2008+
role,
2009+
admin,
2010+
authorization_owner,
2011+
} => {
2012+
write!(
2013+
f,
2014+
"CREATE ROLE {if_not_exists}{names}{superuser}{create_db}{create_role}{inherit}{login}{replication}{bypassrls}",
2015+
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" },
2016+
names = display_separated(names, ", "),
2017+
superuser = match *superuser {
2018+
Some(true) => " SUPERUSER",
2019+
Some(false) => " NOSUPERUSER",
2020+
None => ""
2021+
},
2022+
create_db = match *create_db {
2023+
Some(true) => " CREATEDB",
2024+
Some(false) => " NOCREATEDB",
2025+
None => ""
2026+
},
2027+
create_role = match *create_role {
2028+
Some(true) => " CREATEROLE",
2029+
Some(false) => " NOCREATEROLE",
2030+
None => ""
2031+
},
2032+
inherit = match *inherit {
2033+
Some(true) => " INHERIT",
2034+
Some(false) => " NOINHERIT",
2035+
None => ""
2036+
},
2037+
login = match *login {
2038+
Some(true) => " LOGIN",
2039+
Some(false) => " NOLOGIN",
2040+
None => ""
2041+
},
2042+
replication = match *replication {
2043+
Some(true) => " REPLICATION",
2044+
Some(false) => " NOREPLICATION",
2045+
None => ""
2046+
},
2047+
bypassrls = match *bypassrls {
2048+
Some(true) => " BYPASSRLS",
2049+
Some(false) => " NOBYPASSRLS",
2050+
None => ""
2051+
}
2052+
)?;
2053+
if let Some(limit) = connection_limit {
2054+
write!(f, " CONNECTION LIMIT {}", limit)?;
2055+
}
2056+
match password {
2057+
Some(Password::Password(pass)) => write!(f, " PASSWORD {}", pass),
2058+
Some(Password::NullPassword) => write!(f, " PASSWORD NULL"),
2059+
None => Ok(()),
2060+
}?;
2061+
if let Some(until) = valid_until {
2062+
write!(f, " VALID UNTIL {}", until)?;
2063+
}
2064+
if !in_role.is_empty() {
2065+
write!(f, " IN ROLE {}", display_comma_separated(in_role))?;
2066+
}
2067+
if !role.is_empty() {
2068+
write!(f, " ROLE {}", display_comma_separated(role))?;
2069+
}
2070+
if !admin.is_empty() {
2071+
write!(f, " ADMIN {}", display_comma_separated(admin))?;
2072+
}
2073+
if let Some(owner) = authorization_owner {
2074+
write!(f, " AUTHORIZATION {}", owner)?;
2075+
}
2076+
Ok(())
2077+
}
19662078
Statement::AlterTable { name, operation } => {
19672079
write!(f, "ALTER TABLE {} {}", name, operation)
19682080
}
@@ -2701,6 +2813,7 @@ pub enum ObjectType {
27012813
View,
27022814
Index,
27032815
Schema,
2816+
Role,
27042817
}
27052818

27062819
impl fmt::Display for ObjectType {
@@ -2710,6 +2823,7 @@ impl fmt::Display for ObjectType {
27102823
ObjectType::View => "VIEW",
27112824
ObjectType::Index => "INDEX",
27122825
ObjectType::Schema => "SCHEMA",
2826+
ObjectType::Role => "ROLE",
27132827
})
27142828
}
27152829
}

src/keywords.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ define_keywords!(
7070
ABSOLUTE,
7171
ACTION,
7272
ADD,
73+
ADMIN,
7374
ALL,
7475
ALLOCATE,
7576
ALTER,
@@ -105,6 +106,7 @@ define_keywords!(
105106
BOOLEAN,
106107
BOTH,
107108
BY,
109+
BYPASSRLS,
108110
BYTEA,
109111
CACHE,
110112
CALL,
@@ -152,6 +154,8 @@ define_keywords!(
152154
COVAR_POP,
153155
COVAR_SAMP,
154156
CREATE,
157+
CREATEDB,
158+
CREATEROLE,
155159
CROSS,
156160
CSV,
157161
CUBE,
@@ -272,6 +276,7 @@ define_keywords!(
272276
IN,
273277
INDEX,
274278
INDICATOR,
279+
INHERIT,
275280
INNER,
276281
INOUT,
277282
INPUTFORMAT,
@@ -313,6 +318,7 @@ define_keywords!(
313318
LOCALTIME,
314319
LOCALTIMESTAMP,
315320
LOCATION,
321+
LOGIN,
316322
LOWER,
317323
MANAGEDLOCATION,
318324
MATCH,
@@ -342,9 +348,16 @@ define_keywords!(
342348
NEW,
343349
NEXT,
344350
NO,
351+
NOBYPASSRLS,
352+
NOCREATEDB,
353+
NOCREATEROLE,
354+
NOINHERIT,
355+
NOLOGIN,
345356
NONE,
357+
NOREPLICATION,
346358
NORMALIZE,
347359
NOSCAN,
360+
NOSUPERUSER,
348361
NOT,
349362
NTH_VALUE,
350363
NTILE,
@@ -380,6 +393,7 @@ define_keywords!(
380393
PARTITION,
381394
PARTITIONED,
382395
PARTITIONS,
396+
PASSWORD,
383397
PERCENT,
384398
PERCENTILE_CONT,
385399
PERCENTILE_DISC,
@@ -432,6 +446,7 @@ define_keywords!(
432446
REPAIR,
433447
REPEATABLE,
434448
REPLACE,
449+
REPLICATION,
435450
RESTRICT,
436451
RESULT,
437452
RETURN,
@@ -492,6 +507,7 @@ define_keywords!(
492507
SUCCEEDS,
493508
SUM,
494509
SUPER,
510+
SUPERUSER,
495511
SYMMETRIC,
496512
SYNC,
497513
SYSTEM,
@@ -538,13 +554,15 @@ define_keywords!(
538554
UNLOGGED,
539555
UNNEST,
540556
UNSIGNED,
557+
UNTIL,
541558
UPDATE,
542559
UPPER,
543560
USAGE,
544561
USE,
545562
USER,
546563
USING,
547564
UUID,
565+
VALID,
548566
VALUE,
549567
VALUES,
550568
VALUE_OF,

0 commit comments

Comments
 (0)