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
//! CREATE VIEW statement builder
//!
//! This module provides the `CreateViewStatement` type for building SQL CREATE VIEW queries.
use crate::{
backend::QueryBuilder,
query::SelectStatement,
types::{DynIden, IntoIden},
};
use super::traits::{QueryBuilderTrait, QueryStatementBuilder, QueryStatementWriter};
/// CREATE VIEW statement builder
///
/// This struct provides a fluent API for constructing CREATE VIEW queries.
///
/// # Examples
///
/// ```rust,ignore
/// use reinhardt_query::prelude::*;
///
/// let select = Query::select()
/// .column(Expr::col("name"))
/// .column(Expr::col("email"))
/// .from("users")
/// .and_where(Expr::col("active").eq(true));
///
/// let query = Query::create_view()
/// .name("active_users")
/// .as_select(select)
/// .if_not_exists();
/// ```
#[derive(Debug, Clone)]
pub struct CreateViewStatement {
pub(crate) name: Option<DynIden>,
pub(crate) select: Option<SelectStatement>,
pub(crate) if_not_exists: bool,
pub(crate) or_replace: bool,
pub(crate) columns: Vec<DynIden>,
pub(crate) materialized: bool,
}
impl CreateViewStatement {
/// Create a new CREATE VIEW statement
pub fn new() -> Self {
Self {
name: None,
select: None,
if_not_exists: false,
or_replace: false,
columns: Vec::new(),
materialized: false,
}
}
/// Take the ownership of data in the current [`CreateViewStatement`]
pub fn take(&mut self) -> Self {
Self {
name: self.name.take(),
select: self.select.take(),
if_not_exists: self.if_not_exists,
or_replace: self.or_replace,
columns: std::mem::take(&mut self.columns),
materialized: self.materialized,
}
}
/// Set the view name
///
/// # Examples
///
/// ```rust,ignore
/// use reinhardt_query::prelude::*;
///
/// let query = Query::create_view()
/// .name("active_users");
/// ```
pub fn name<N>(&mut self, name: N) -> &mut Self
where
N: IntoIden,
{
self.name = Some(name.into_iden());
self
}
/// Set the SELECT statement for the view
///
/// # Examples
///
/// ```rust,ignore
/// use reinhardt_query::prelude::*;
///
/// let select = Query::select()
/// .column(Expr::col("name"))
/// .from("users");
///
/// let query = Query::create_view()
/// .name("user_names")
/// .as_select(select);
/// ```
pub fn as_select(&mut self, select: SelectStatement) -> &mut Self {
self.select = Some(select);
self
}
/// Add IF NOT EXISTS clause
///
/// # Examples
///
/// ```rust,ignore
/// use reinhardt_query::prelude::*;
///
/// let query = Query::create_view()
/// .name("active_users")
/// .if_not_exists();
/// ```
pub fn if_not_exists(&mut self) -> &mut Self {
self.if_not_exists = true;
self
}
/// Add OR REPLACE clause
///
/// Note: Cannot be used with IF NOT EXISTS
///
/// # Examples
///
/// ```rust,ignore
/// use reinhardt_query::prelude::*;
///
/// let query = Query::create_view()
/// .name("active_users")
/// .or_replace();
/// ```
pub fn or_replace(&mut self) -> &mut Self {
self.or_replace = true;
self
}
/// Set column names for the view
///
/// # Examples
///
/// ```rust,ignore
/// use reinhardt_query::prelude::*;
///
/// let query = Query::create_view()
/// .name("active_users")
/// .columns(["user_name", "user_email"]);
/// ```
pub fn columns<I, C>(&mut self, cols: I) -> &mut Self
where
I: IntoIterator<Item = C>,
C: IntoIden,
{
for col in cols {
self.columns.push(col.into_iden());
}
self
}
/// Set MATERIALIZED flag (PostgreSQL only)
///
/// # Examples
///
/// ```rust,ignore
/// use reinhardt_query::prelude::*;
///
/// let query = Query::create_view()
/// .name("active_users")
/// .materialized(true);
/// ```
pub fn materialized(&mut self, materialized: bool) -> &mut Self {
self.materialized = materialized;
self
}
}
impl Default for CreateViewStatement {
fn default() -> Self {
Self::new()
}
}
impl QueryStatementBuilder for CreateViewStatement {
fn build_any(&self, query_builder: &dyn QueryBuilderTrait) -> (String, crate::value::Values) {
// Downcast to concrete QueryBuilder type
use std::any::Any;
if let Some(builder) =
(query_builder as &dyn Any).downcast_ref::<crate::backend::PostgresQueryBuilder>()
{
return builder.build_create_view(self);
}
if let Some(builder) =
(query_builder as &dyn Any).downcast_ref::<crate::backend::MySqlQueryBuilder>()
{
return builder.build_create_view(self);
}
if let Some(builder) =
(query_builder as &dyn Any).downcast_ref::<crate::backend::SqliteQueryBuilder>()
{
return builder.build_create_view(self);
}
panic!("Unsupported query builder type");
}
}
impl QueryStatementWriter for CreateViewStatement {}