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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//! Data Control Language (DCL) support for reinhardt-query
//!
//! This module provides type-safe builders for DCL statements including:
//! - GRANT and REVOKE privileges
//! - CREATE, DROP, and ALTER for roles and users
//! - Session management (SET ROLE, RESET ROLE, SET DEFAULT ROLE)
//! - User renaming (MySQL only)
//!
//! # Role and User Management
//!
//! ## Creating Roles and Users
//!
//! ```
//! use reinhardt_query::dcl::{CreateRoleStatement, RoleAttribute};
//! use reinhardt_query::backend::{PostgresQueryBuilder, QueryBuilder};
//!
//! // PostgreSQL: Create a role with attributes
//! let stmt = CreateRoleStatement::new()
//! .role("app_user")
//! .attribute(RoleAttribute::Login)
//! .attribute(RoleAttribute::CreateDb);
//!
//! let builder = PostgresQueryBuilder::new();
//! let (sql, _values) = builder.build_create_role(&stmt);
//! // Generates: CREATE ROLE "app_user" WITH LOGIN CREATEDB
//! ```
//!
//! ## Session Management
//!
//! ```
//! use reinhardt_query::dcl::{SetRoleStatement, RoleTarget};
//! use reinhardt_query::backend::{PostgresQueryBuilder, QueryBuilder};
//!
//! // Set current session role
//! let stmt = SetRoleStatement::new()
//! .role(RoleTarget::Named("admin".to_string()));
//!
//! let builder = PostgresQueryBuilder::new();
//! let (sql, _values) = builder.build_set_role(&stmt);
//! // Generates: SET ROLE "admin"
//! ```
//!
//! # Privileges and Permissions
//!
//! ```
//! use reinhardt_query::dcl::{Privilege, ObjectType, Grantee};
//!
//! // Create privilege
//! let privilege = Privilege::Select;
//! assert_eq!(privilege.as_sql(), "SELECT");
//!
//! // Create object type
//! let object_type = ObjectType::Table;
//! assert_eq!(object_type.as_sql(), "TABLE");
//!
//! // Create grantee
//! let grantee = Grantee::role("app_user");
//! ```
//!
//! # Security Considerations
//!
//! ## Password Handling
//!
//! **IMPORTANT**: Passwords are stored in plain text within statement structures
//! and are only escaped during SQL generation. For production use:
//!
//! - Never log or display statement structures containing passwords
//! - Use encrypted password variants when possible (`` `RoleAttribute::EncryptedPassword` ``)
//! - Ensure secure transmission of SQL statements to the database
//! - Consider using external authentication plugins (`` `UserOption::AuthPlugin` ``)
//! - Rotate passwords regularly using ALTER ROLE/USER statements
//!
//! ## SQL Injection Prevention
//!
//! This module automatically prevents SQL injection through:
//!
//! - **Identifier escaping**: All role names, user names, and identifiers are
//! properly quoted using backend-specific quoting (e.g., `"role"` for PostgreSQL,
//! `` `role` `` for MySQL)
//! - **Value parameterization**: All values (passwords, timestamps, etc.) are
//! converted to parameterized placeholders (`$1`, `$2` for PostgreSQL; `?` for MySQL)
//! - **Type safety**: Rust's type system prevents invalid combinations of attributes
//! and options
//!
//! However, you should still:
//!
//! - Validate user input before constructing statements
//! - Use the provided builder methods instead of constructing raw SQL
//! - Review generated SQL in development to ensure correctness
//!
//! # Database Support
//!
//! | Feature | PostgreSQL | MySQL | SQLite |
//! |---------|-----------|-------|--------|
//! | CREATE/DROP/ALTER ROLE | ✓ | ✓ | ✗ (panics) |
//! | CREATE/DROP/ALTER USER | ✓ | ✓ | ✗ (panics) |
//! | RENAME USER | ✗ (use ALTER ROLE) | ✓ | ✗ (panics) |
//! | SET ROLE | ✓ | ✓ | ✗ (panics) |
//! | RESET ROLE | ✓ | ✗ (panics) | ✗ (panics) |
//! | SET DEFAULT ROLE | ✗ (panics) | ✓ | ✗ (panics) |
//! | GRANT/REVOKE | ✓ | ✓ | ✗ (panics) |
//!
//! SQLite does not support DCL operations. Attempting to use DCL builders
//! with `` `SqliteQueryBuilder` `` will panic with a descriptive error message.
/// Validate that a name is not empty or whitespace only.
pub
pub use AlterRoleStatement;
pub use AlterUserStatement;
pub use CreateRoleStatement;
pub use CreateUserStatement;
pub use DropRoleStatement;
pub use DropUserStatement;
pub use GrantStatement;
pub use GrantRoleStatement;
pub use Grantee;
pub use ObjectType;
pub use Privilege;
pub use RenameUserStatement;
pub use ResetRoleStatement;
pub use RevokeStatement;
pub use RevokeRoleStatement;
pub use RoleAttribute;
pub use ;
pub use ;
pub use ;
pub use UserOption;