pub struct CreateRoleStatement {
pub role_name: String,
pub if_not_exists: bool,
pub attributes: Vec<RoleAttribute>,
pub options: Vec<UserOption>,
}Expand description
CREATE ROLE statement builder
This struct provides a fluent API for building CREATE ROLE statements. It supports both PostgreSQL attributes and MySQL options.
§PostgreSQL
PostgreSQL CREATE ROLE accepts various attributes that control role privileges
and settings. Use the `attribute()` method to add attributes.
§MySQL
MySQL CREATE ROLE supports IF NOT EXISTS clause and user options.
Use the `if_not_exists()` and `option()` methods.
§Examples
Create a simple role:
use reinhardt_query::dcl::CreateRoleStatement;
let stmt = CreateRoleStatement::new()
.role("developer");Create a login role with password (PostgreSQL):
use reinhardt_query::dcl::{CreateRoleStatement, RoleAttribute};
let stmt = CreateRoleStatement::new()
.role("app_user")
.attribute(RoleAttribute::Login)
.attribute(RoleAttribute::Password("password123".to_string()))
.attribute(RoleAttribute::ConnectionLimit(5));Create a role with IF NOT EXISTS (MySQL):
use reinhardt_query::dcl::{CreateRoleStatement, UserOption};
let stmt = CreateRoleStatement::new()
.role("app_role")
.if_not_exists(true)
.option(UserOption::Comment("My application role".to_string()));Fields§
§role_name: StringRole name
if_not_exists: boolIF NOT EXISTS clause (MySQL only)
attributes: Vec<RoleAttribute>PostgreSQL role attributes
options: Vec<UserOption>MySQL user options
Implementations§
Source§impl CreateRoleStatement
impl CreateRoleStatement
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new CREATE ROLE statement
§Examples
use reinhardt_query::dcl::CreateRoleStatement;
let stmt = CreateRoleStatement::new();Sourcepub fn role(self, name: impl Into<String>) -> Self
pub fn role(self, name: impl Into<String>) -> Self
Set the role name
§Examples
use reinhardt_query::dcl::CreateRoleStatement;
let stmt = CreateRoleStatement::new()
.role("developer");Sourcepub fn if_not_exists(self, flag: bool) -> Self
pub fn if_not_exists(self, flag: bool) -> Self
Set IF NOT EXISTS flag (MySQL only)
§Examples
use reinhardt_query::dcl::CreateRoleStatement;
let stmt = CreateRoleStatement::new()
.role("app_role")
.if_not_exists(true);Sourcepub fn attribute(self, attr: RoleAttribute) -> Self
pub fn attribute(self, attr: RoleAttribute) -> Self
Add a single PostgreSQL role attribute
§Examples
use reinhardt_query::dcl::{CreateRoleStatement, RoleAttribute};
let stmt = CreateRoleStatement::new()
.role("app_user")
.attribute(RoleAttribute::Login)
.attribute(RoleAttribute::CreateDb);Sourcepub fn attributes(self, attrs: Vec<RoleAttribute>) -> Self
pub fn attributes(self, attrs: Vec<RoleAttribute>) -> Self
Set all PostgreSQL role attributes at once
§Examples
use reinhardt_query::dcl::{CreateRoleStatement, RoleAttribute};
let stmt = CreateRoleStatement::new()
.role("app_user")
.attributes(vec![
RoleAttribute::Login,
RoleAttribute::CreateDb,
RoleAttribute::ConnectionLimit(10),
]);Sourcepub fn option(self, opt: UserOption) -> Self
pub fn option(self, opt: UserOption) -> Self
Add a single MySQL user option
§Examples
use reinhardt_query::dcl::{CreateRoleStatement, UserOption};
let stmt = CreateRoleStatement::new()
.role("app_role")
.option(UserOption::Comment("Application role".to_string()));Sourcepub fn options(self, opts: Vec<UserOption>) -> Self
pub fn options(self, opts: Vec<UserOption>) -> Self
Set all MySQL user options at once
§Examples
use reinhardt_query::dcl::{CreateRoleStatement, UserOption};
let stmt = CreateRoleStatement::new()
.role("app_role")
.options(vec![
UserOption::Comment("Application role".to_string()),
UserOption::AccountLock,
]);Sourcepub fn validate(&self) -> Result<(), String>
pub fn validate(&self) -> Result<(), String>
Validate the CREATE ROLE statement
§Validation Rules
- Role name cannot be empty
§Examples
use reinhardt_query::dcl::CreateRoleStatement;
let stmt = CreateRoleStatement::new()
.role("developer");
assert!(stmt.validate().is_ok());use reinhardt_query::dcl::CreateRoleStatement;
let stmt = CreateRoleStatement::new();
assert!(stmt.validate().is_err());Trait Implementations§
Source§impl Clone for CreateRoleStatement
impl Clone for CreateRoleStatement
Source§fn clone(&self) -> CreateRoleStatement
fn clone(&self) -> CreateRoleStatement
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more