1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//! DROP ROLE statement builder
//!
//! # Examples
//!
//! ```
//! use reinhardt_query::dcl::DropRoleStatement;
//!
//! let stmt = DropRoleStatement::new()
//! .role("app_user")
//! .if_exists(true);
//! ```
/// DROP ROLE statement builder
///
/// # Examples
///
/// ```
/// use reinhardt_query::dcl::DropRoleStatement;
///
/// let stmt = DropRoleStatement::new()
/// .roles(vec!["user1".to_string(), "user2".to_string()])
/// .if_exists(true);
/// ```
#[derive(Debug, Clone, Default)]
pub struct DropRoleStatement {
/// Role names to drop
pub role_names: Vec<String>,
/// IF EXISTS clause
pub if_exists: bool,
}
impl DropRoleStatement {
/// Create a new empty DROP ROLE statement
///
/// # Examples
///
/// ```
/// use reinhardt_query::dcl::DropRoleStatement;
///
/// let stmt = DropRoleStatement::new();
/// ```
pub fn new() -> Self {
Self::default()
}
/// Add a single role name to drop
///
/// # Examples
///
/// ```
/// use reinhardt_query::dcl::DropRoleStatement;
///
/// let stmt = DropRoleStatement::new()
/// .role("app_user");
/// ```
pub fn role(mut self, name: impl Into<String>) -> Self {
self.role_names.push(name.into());
self
}
/// Set all role names to drop at once
///
/// # Examples
///
/// ```
/// use reinhardt_query::dcl::DropRoleStatement;
///
/// let stmt = DropRoleStatement::new()
/// .roles(vec!["user1".to_string(), "user2".to_string()]);
/// ```
pub fn roles(mut self, names: Vec<String>) -> Self {
self.role_names = names;
self
}
/// Set the IF EXISTS flag
///
/// # Examples
///
/// ```
/// use reinhardt_query::dcl::DropRoleStatement;
///
/// let stmt = DropRoleStatement::new()
/// .role("app_user")
/// .if_exists(true);
/// ```
pub fn if_exists(mut self, flag: bool) -> Self {
self.if_exists = flag;
self
}
/// Validate the DROP ROLE statement
///
/// # Validation Rules
///
/// 1. At least one role name is required
///
/// # Examples
///
/// Valid statement:
/// ```
/// use reinhardt_query::dcl::DropRoleStatement;
///
/// let stmt = DropRoleStatement::new()
/// .role("app_user");
///
/// assert!(stmt.validate().is_ok());
/// ```
///
/// Invalid statement (no role names):
/// ```
/// use reinhardt_query::dcl::DropRoleStatement;
///
/// let stmt = DropRoleStatement::new();
/// assert!(stmt.validate().is_err());
/// ```
pub fn validate(&self) -> Result<(), String> {
if self.role_names.is_empty() {
return Err("At least one role name is required".to_string());
}
// Validate each role name is non-empty after trimming whitespace
for (idx, role_name) in self.role_names.iter().enumerate() {
let trimmed = role_name.trim();
if trimmed.is_empty() {
return Err(format!(
"Role name at index {} cannot be empty or whitespace only",
idx
));
}
}
Ok(())
}
}