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
use std::fmt::Write;
use crate::create_column::{CreateColumn, CreateColumnImpl};
use crate::error::Error;
use crate::Value;
/**
Representation of operations to execute in the context of an ALTER TABLE statement.
*/
#[derive(Debug)]
pub enum AlterTableOperation<'until_build, 'post_build> {
/// Use this operation to rename a table
RenameTo {
/// New name of the table
name: String,
},
/// Use this operation to rename a column within a table
RenameColumnTo {
/// Current column name
column_name: String,
/// New column name
new_column_name: String,
},
/// Use this operation to add a column to an existing table.
AddColumn {
/// Operation to use for adding the column
operation: CreateColumnImpl<'until_build, 'post_build>,
},
/// Use this operation to drop an existing column.
DropColumn {
/// Name of the column to drop
name: String,
},
}
/**
The trait representing an alter table builder
*/
pub trait AlterTable<'post_build> {
/**
This method is used to build the alter table statement.
*/
fn build(self) -> Result<Vec<(String, Vec<Value<'post_build>>)>, Error>;
}
/**
Representation of the data of an ALTER TABLE statement.
*/
#[derive(Debug)]
pub struct AlterTableData<'until_build, 'post_build> {
/// Name of the table to operate on
pub(crate) name: &'until_build str,
/// Operation to execute
pub(crate) operation: AlterTableOperation<'until_build, 'post_build>,
pub(crate) lookup: Vec<Value<'post_build>>,
pub(crate) statements: Vec<(String, Vec<Value<'post_build>>)>,
}
/**
Implementation of the [AlterTable] trait for the different database dialects.
Should only be constructed via [crate::DBImpl::alter_table].
*/
#[derive(Debug)]
pub enum AlterTableImpl<'until_build, 'post_build> {
/**
SQLite representation of the ALTER TABLE operation.
*/
#[cfg(feature = "sqlite")]
SQLite(AlterTableData<'until_build, 'post_build>),
/**
Postgres representation of the ALTER TABLE operation.
*/
#[cfg(feature = "postgres")]
Postgres(AlterTableData<'until_build, 'post_build>),
}
impl<'post_build> AlterTable<'post_build> for AlterTableImpl<'_, 'post_build> {
fn build(self) -> Result<Vec<(String, Vec<Value<'post_build>>)>, Error> {
match self {
#[cfg(feature = "sqlite")]
AlterTableImpl::SQLite(mut d) => {
let mut s = format!("ALTER TABLE \"{}\" ", d.name);
match d.operation {
AlterTableOperation::RenameTo { name } => {
write!(s, "RENAME TO \"{name}\"").unwrap();
}
AlterTableOperation::RenameColumnTo {
column_name,
new_column_name,
} => write!(
s,
"RENAME COLUMN \"{column_name}\" TO \"{new_column_name}\""
)
.unwrap(),
AlterTableOperation::AddColumn { mut operation } => {
write!(s, "ADD COLUMN ").unwrap();
if let CreateColumnImpl::SQLite(ccd) = &mut operation {
ccd.statements = Some(&mut d.statements);
ccd.lookup = Some(&mut d.lookup);
}
operation.build(&mut s)?;
}
AlterTableOperation::DropColumn { name } => {
write!(s, "DROP COLUMN \"{name}\"").unwrap()
}
};
write!(s, ";").unwrap();
let mut statements = vec![(s, d.lookup)];
statements.extend(d.statements);
Ok(statements)
}
#[cfg(feature = "postgres")]
AlterTableImpl::Postgres(mut d) => {
let mut s = format!("ALTER TABLE \"{}\" ", d.name);
match d.operation {
AlterTableOperation::RenameTo { name } => {
write!(s, "RENAME TO \"{name}\"").unwrap();
}
AlterTableOperation::RenameColumnTo {
column_name,
new_column_name,
} => {
write!(
s,
"RENAME COLUMN \"{column_name}\" TO \"{new_column_name}\""
)
.unwrap();
}
AlterTableOperation::AddColumn { mut operation } => {
write!(s, "ADD COLUMN ").unwrap();
#[allow(irrefutable_let_patterns)]
if let CreateColumnImpl::Postgres(ccd) = &mut operation {
ccd.statements = Some(&mut d.statements);
}
operation.build(&mut s)?;
}
AlterTableOperation::DropColumn { name } => {
write!(s, "DROP COLUMN \"{name}\"").unwrap()
}
};
write!(s, ";").unwrap();
let mut statements = vec![(s, d.lookup)];
statements.extend(d.statements);
Ok(statements)
}
}
}
}