sea_query/extension/postgres/extension.rs
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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
use crate::{QueryBuilder, QuotedBuilder, SqlWriter};
/// Creates a new "CREATE or DROP EXTENSION" statement for PostgreSQL
///
/// # Exampl
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Extension;
impl Extension {
/// Creates a new [`ExtensionCreateStatement`]
pub fn create() -> ExtensionCreateStatement {
ExtensionCreateStatement::new()
}
/// Creates a new [`ExtensionDropStatement`]
pub fn drop() -> ExtensionDropStatement {
ExtensionDropStatement::new()
}
}
/// Creates a new "CREATE EXTENSION" statement for PostgreSQL
///
/// # Synopsis
///
/// ```ignore
/// CREATE EXTENSION [ IF NOT EXISTS ] extension_name
/// [ WITH ] [ SCHEMA schema_name ]
/// [ VERSION version ]
/// [ CASCADE ]
/// ```
///
/// # Example
///
/// Creates the "ltree" extension if it doesn't exists.
///
/// ```
/// use sea_query::{extension::postgres::Extension, *};
///
/// assert_eq!(
/// Extension::create()
/// .name("ltree")
/// .schema("public")
/// .version("v0.1.0")
/// .cascade()
/// .if_not_exists()
/// .to_string(PostgresQueryBuilder),
/// r#"CREATE EXTENSION IF NOT EXISTS ltree WITH SCHEMA public VERSION v0.1.0 CASCADE"#
/// );
/// ```
///
/// # References
///
/// [Refer to the PostgreSQL Documentation][1]
///
/// [1]: https://www.postgresql.org/docs/current/sql-createextension.html
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct ExtensionCreateStatement {
pub(crate) name: String,
pub(crate) schema: Option<String>,
pub(crate) version: Option<String>,
/// Conditional to execute query based on existance of the extension.
pub(crate) if_not_exists: bool,
/// Determines the presence of the `RESTRICT` statement
pub(crate) cascade: bool,
}
impl ExtensionCreateStatement {
pub fn new() -> Self {
Self::default()
}
/// Sets the name of the extension to be created.
pub fn name<T: Into<String>>(&mut self, name: T) -> &mut Self {
self.name = name.into();
self
}
/// Uses "WITH SCHEMA" on Create Extension Statement.
pub fn schema<T: Into<String>>(&mut self, schema: T) -> &mut Self {
self.schema = Some(schema.into());
self
}
/// Uses "VERSION" on Create Extension Statement.
pub fn version<T: Into<String>>(&mut self, version: T) -> &mut Self {
self.version = Some(version.into());
self
}
/// Uses "CASCADE" on Create Extension Statement.
pub fn cascade(&mut self) -> &mut Self {
self.cascade = true;
self
}
/// Uses "IF NOT EXISTS" on Create Extension Statement.
pub fn if_not_exists(&mut self) -> &mut Self {
self.if_not_exists = true;
self
}
}
/// Creates a new "DROP EXTENSION" statement for PostgreSQL
///
/// # Synopsis
///
/// ```ignore
/// DROP EXTENSION [ IF EXISTS ] name [, ...] [ CASCADE | RESTRICT ]
/// ```
///
/// # Example
///
/// Drops the "ltree" extension if it exists.
///
/// ```
/// use sea_query::{extension::postgres::Extension, *};
///
/// assert_eq!(
/// Extension::drop()
/// .name("ltree")
/// .cascade()
/// .if_exists()
/// .to_string(PostgresQueryBuilder),
/// r#"DROP EXTENSION IF EXISTS ltree CASCADE"#
/// );
/// ```
///
/// # References
///
/// [Refer to the PostgreSQL Documentation][1]
///
/// [1]: https://www.postgresql.org/docs/current/sql-createextension.html
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct ExtensionDropStatement {
pub(crate) name: String,
pub(crate) schema: Option<String>,
pub(crate) version: Option<String>,
/// Conditional to execute query based on existance of the extension.
pub(crate) if_exists: bool,
/// Determines the presence of the `RESTRICT` statement.
pub(crate) restrict: bool,
/// Determines the presence of the `CASCADE` statement
pub(crate) cascade: bool,
}
impl ExtensionDropStatement {
pub fn new() -> Self {
Self::default()
}
/// Sets the name of the extension to be dropped.
pub fn name<T: Into<String>>(&mut self, name: T) -> &mut Self {
self.name = name.into();
self
}
/// Uses "IF EXISTS" on Drop Extension Statement.
pub fn if_exists(&mut self) -> &mut Self {
self.if_exists = true;
self
}
/// Uses "CASCADE" on Drop Extension Statement.
pub fn cascade(&mut self) -> &mut Self {
self.cascade = true;
self
}
/// Uses "RESTRICT" on Drop Extension Statement.
pub fn restrict(&mut self) -> &mut Self {
self.restrict = true;
self
}
}
pub trait ExtensionBuilder: QuotedBuilder {
/// Translate [`ExtensionCreateStatement`] into database specific SQL statement.
fn prepare_extension_create_statement(
&self,
create: &ExtensionCreateStatement,
sql: &mut dyn SqlWriter,
);
/// Translate [`ExtensionDropStatement`] into database specific SQL statement.
fn prepare_extension_drop_statement(
&self,
drop: &ExtensionDropStatement,
sql: &mut dyn SqlWriter,
);
}
macro_rules! impl_extension_statement_builder {
( $struct_name: ident, $func_name: ident ) => {
impl $struct_name {
pub fn build_ref<T: ExtensionBuilder>(&self, extension_builder: &T) -> String {
let mut sql = String::with_capacity(256);
self.build_collect_ref(extension_builder, &mut sql)
}
pub fn build_collect<T: ExtensionBuilder>(
&self,
extension_builder: T,
sql: &mut dyn SqlWriter,
) -> String {
self.build_collect_ref(&extension_builder, sql)
}
pub fn build_collect_ref<T: ExtensionBuilder>(
&self,
extension_builder: &T,
sql: &mut dyn SqlWriter,
) -> String {
extension_builder.$func_name(self, sql);
sql.to_string()
}
/// Build corresponding SQL statement and return SQL string
pub fn to_string<T>(&self, extension_builder: T) -> String
where
T: ExtensionBuilder + QueryBuilder,
{
self.build_ref(&extension_builder)
}
}
};
}
impl_extension_statement_builder!(ExtensionCreateStatement, prepare_extension_create_statement);
impl_extension_statement_builder!(ExtensionDropStatement, prepare_extension_drop_statement);
#[cfg(test)]
mod test {
use super::super::PgLTree;
use super::*;
#[test]
fn creates_a_stmt_for_create_extension() {
let create_extension_stmt = Extension::create()
.name(PgLTree)
.schema("public")
.version("v0.1.0")
.cascade()
.if_not_exists()
.to_owned();
assert_eq!(create_extension_stmt.name, "ltree");
assert_eq!(create_extension_stmt.schema, Some("public".to_string()));
assert_eq!(create_extension_stmt.version, Some("v0.1.0".to_string()));
assert!(create_extension_stmt.cascade);
assert!(create_extension_stmt.if_not_exists);
}
#[test]
fn creates_a_stmt_for_drop_extension() {
let drop_extension_stmt = Extension::drop()
.name(PgLTree)
.cascade()
.if_exists()
.restrict()
.to_owned();
assert_eq!(drop_extension_stmt.name, "ltree");
assert!(drop_extension_stmt.cascade);
assert!(drop_extension_stmt.if_exists);
assert!(drop_extension_stmt.restrict);
}
}