use crate::builders::traits::{
ConditionalBuilder, InspectableBuilder, SchemaBuilder as SchemaBuilderTrait, ValidatableBuilder,
};
use crate::builders::validation::BuilderValidationError;
#[derive(Debug, Clone)]
pub struct DatabaseSchemaBuilder {
name: String,
comment: Option<String>,
owner: Option<String>,
if_not_exists: bool,
}
impl DatabaseSchemaBuilder {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
comment: None,
owner: None,
if_not_exists: true,
}
}
pub fn comment(mut self, comment: impl Into<String>) -> Self {
self.comment = Some(comment.into());
self
}
pub fn owner(mut self, owner: impl Into<String>) -> Self {
self.owner = Some(owner.into());
self
}
pub fn if_not_exists(mut self, if_not_exists: bool) -> Self {
self.if_not_exists = if_not_exists;
self
}
pub fn name(&self) -> &str {
&self.name
}
pub fn get_comment(&self) -> Option<&str> {
self.comment.as_deref()
}
pub fn get_owner(&self) -> Option<&str> {
self.owner.as_deref()
}
pub fn is_if_not_exists(&self) -> bool {
self.if_not_exists
}
pub fn to_sql(&self) -> String {
let mut sql = String::new();
sql.push_str("CREATE SCHEMA");
if self.if_not_exists {
sql.push_str(" IF NOT EXISTS");
}
sql.push_str(&format!(" \"{}\"", self.name));
if let Some(ref owner) = self.owner {
sql.push_str(&format!(" AUTHORIZATION \"{}\"", owner));
}
if let Some(ref comment) = self.comment {
sql.push_str(&format!(
"; COMMENT ON SCHEMA \"{}\" IS {}",
self.name, comment
));
}
sql.push(';');
sql
}
}
impl SchemaBuilderTrait for DatabaseSchemaBuilder {
type Output = String; type Error = BuilderValidationError;
fn build(self) -> Result<Self::Output, Self::Error> {
if !self.is_valid() {
let errors = self.validation_errors();
return Err(BuilderValidationError::Generic(format!(
"SchemaBuilder validation failed: {}",
errors.join(", ")
)));
}
Ok(self.to_sql())
}
}
impl ValidatableBuilder for DatabaseSchemaBuilder {
fn is_valid(&self) -> bool {
if self.name.is_empty() {
return false;
}
if !self.name.chars().all(|c| c.is_alphanumeric() || c == '_') {
return false;
}
true
}
fn validation_errors(&self) -> Vec<String> {
let mut errors = Vec::new();
if self.name.is_empty() {
errors.push("Schema name cannot be empty".to_string());
}
if !self.name.chars().all(|c| c.is_alphanumeric() || c == '_') {
errors.push("Schema name contains invalid characters".to_string());
}
errors
}
}
impl ConditionalBuilder for DatabaseSchemaBuilder {
fn when<F>(mut self, condition: bool, config_fn: F) -> Self
where
F: FnOnce(&mut Self),
Self: Sized,
{
if condition {
config_fn(&mut self);
}
self
}
}
impl InspectableBuilder for DatabaseSchemaBuilder {
fn debug_info(&self) -> String {
format!(
"SchemaBuilder {{\n name: {:?},\n comment: {:?},\n owner: {:?},\n if_not_exists: {}\n}}",
self.name, self.comment, self.owner, self.if_not_exists
)
}
fn summary(&self) -> String {
format!("Schema '{}'", self.name)
}
}
pub fn schema(name: impl Into<String>) -> DatabaseSchemaBuilder {
DatabaseSchemaBuilder::new(name)
}
pub fn extract_schema_name(qualified_name: &str) -> Option<&str> {
if let Some(dot_pos) = qualified_name.find('.') {
Some(&qualified_name[..dot_pos])
} else {
None
}
}
pub fn extract_table_name(qualified_name: &str) -> &str {
if let Some(dot_pos) = qualified_name.find('.') {
&qualified_name[dot_pos + 1..]
} else {
qualified_name
}
}