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
use std::collections::HashSet;
use std::io::ErrorKind;
use crate::ast::dml::insert::{InsertData, InsertQuery};
use crate::ast::types::SQLExpression;
use crate::errors::predule::ExecuteError;
use crate::errors::RRDBError;
use crate::executor::config::row::{TableDataField, TableDataRow};
use crate::executor::config::table::TableConfig;
use crate::executor::encoder::storage::StorageEncoder;
use crate::executor::predule::{
ExecuteColumn, ExecuteColumnType, ExecuteField, ExecuteResult, ExecuteRow, Executor,
};
impl Executor {
pub async fn insert(&self, query: InsertQuery) -> Result<ExecuteResult, RRDBError> {
let encoder = StorageEncoder::new();
let into_table = query.into_table.as_ref().unwrap();
let database_name = into_table.clone().database_name.unwrap();
let table_name = into_table.clone().table_name;
let base_path = self.get_data_directory();
let database_path = base_path.clone().join(&database_name);
let table_path = database_path.clone().join("tables").join(&table_name);
// 데이터 행 파일 경로
let rows_path = table_path.clone().join("rows");
// 설정파일 경로
let config_path = table_path.join("table.config");
let table_config = match tokio::fs::read(&config_path).await {
Ok(data) => {
let table_config: Option<TableConfig> = encoder.decode(data.as_slice());
match table_config {
Some(table_config) => table_config,
None => {
return Err(ExecuteError::wrap("invalid config data"));
}
}
}
Err(error) => match error.kind() {
ErrorKind::NotFound => {
return Err(ExecuteError::wrap("table not found"));
}
_ => {
return Err(ExecuteError::wrap(format!("{:?}", error)));
}
},
};
// 입력된 컬럼
let input_columns_set: HashSet<String> = HashSet::from_iter(query.columns.iter().cloned());
// 필수 컬럼
let required_columns = table_config.get_required_columns();
// 테이블 컬럼 맵
let columns_map = table_config.get_columns_map();
// 필수 입력 컬럼값 검증
for required_column in required_columns {
if !input_columns_set.contains(&required_column.name) {
return Err(ExecuteError::wrap(format!(
"column '{}' is required, but it was not provided",
&required_column.name
)));
}
}
let remain_columns = table_config
.columns
.iter()
.filter(|e| !query.columns.contains(&(*e).clone().name))
.map(|e| &e.name);
match &query.data {
InsertData::Values(values) => {
let mut rows = vec![];
for value in values {
let mut fields = vec![];
// 명시적으로 전달된 컬럼값 리스트 처리
for (i, column_name) in query.columns.iter().enumerate() {
let column_config_info = columns_map.get(column_name).unwrap();
let default_value = match &column_config_info.default {
Some(default) => default.to_owned(),
None => SQLExpression::Null,
};
let value = value.list[i].clone().unwrap_or(default_value);
let data = self.reduce_expression(value, Default::default()).await?;
match columns_map.get(column_name) {
Some(column) => {
if column.not_null && data.type_code() == 0 {
return Err(ExecuteError::wrap(format!(
"column '{}' is not null column
",
column_name
)));
}
if column.data_type.type_code() != data.type_code()
&& data.type_code() != 0
{
return Err(ExecuteError::wrap(format!(
"column '{}' type mismatch
",
column_name
)));
}
}
None => {
return Err(ExecuteError::wrap(format!(
"column '{}' not exists",
column_name
)))
}
}
let column_name = column_name.to_owned();
fields.push(TableDataField {
column_name,
data,
table_name: into_table.clone(),
});
}
// 명시되지 않은 컬럼 리스트 처리
for column_name in remain_columns.clone() {
let column_config_info = columns_map.get(column_name).unwrap();
let default_value = match &column_config_info.default {
Some(default) => default.to_owned(),
None => {
if column_config_info.not_null {
return Err(ExecuteError::wrap(format!(
"column '{}' is not null column
",
column_name
)));
}
SQLExpression::Null
}
};
let data = self
.reduce_expression(default_value, Default::default())
.await?;
match columns_map.get(column_name) {
Some(column) => {
if column.data_type.type_code() != data.type_code()
&& data.type_code() != 0
{
return Err(ExecuteError::wrap(format!(
"column '{}' type mismatch
",
column_name
)));
}
}
None => {
return Err(ExecuteError::wrap(format!(
"column '{}' not exists",
column_name
)))
}
}
let column_name = column_name.to_owned();
fields.push(TableDataField {
column_name,
data,
table_name: into_table.clone(),
});
}
let row = TableDataRow { fields };
rows.push(row);
}
for row in rows {
let file_name = uuid::Uuid::new_v4().to_string();
let row_file_path = rows_path.join(file_name);
if let Err(error) = tokio::fs::write(row_file_path, encoder.encode(row)).await {
return Err(ExecuteError::wrap(error.to_string()));
}
}
}
InsertData::Select(_select) => {
todo!("아직 미구현")
}
InsertData::None => {}
}
Ok(ExecuteResult {
columns: (vec![ExecuteColumn {
name: "desc".into(),
data_type: ExecuteColumnType::String,
}]),
rows: (vec![ExecuteRow {
fields: vec![ExecuteField::String(format!(
"inserted into {}",
table_name
))],
}]),
})
}
}