-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathcreate.rs
More file actions
238 lines (220 loc) · 10.1 KB
/
create.rs
File metadata and controls
238 lines (220 loc) · 10.1 KB
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
use sqlparser::ast::{ColumnOption, CreateTable, DataType, ObjectName, ObjectNamePart, Statement};
use crate::error::{Result, SQLRiteError};
/// True when an `ObjectName` resolves to a single identifier `VECTOR`
/// (case-insensitive). Phase 7a adds the `VECTOR(N)` column type as a
/// sqlparser `DataType::Custom` — the engine recognizes it via this
/// helper so the regular DataType match arm above stays uncluttered.
fn is_vector_type(name: &ObjectName) -> bool {
name.0.len() == 1
&& match &name.0[0] {
ObjectNamePart::Identifier(ident) => ident.value.eq_ignore_ascii_case("VECTOR"),
// Function-form ObjectNamePart shouldn't appear in a CREATE TABLE
// column type position. If it ever does, treat it as not-a-vector
// and the outer match falls through to the "Invalid" arm.
_ => false,
}
}
/// Parses the dimension out of the `Custom` args for `VECTOR(N)`.
/// `args` is the `Vec<String>` sqlparser hands back for parenthesized
/// type arguments — for `VECTOR(384)` that's `["384"]`. Validates that
/// exactly one positive-integer argument was supplied.
fn parse_vector_dim(args: &[String]) -> std::result::Result<usize, String> {
match args {
[] => Err("VECTOR requires a dimension, e.g. `VECTOR(384)`".to_string()),
[single] => {
let trimmed = single.trim();
match trimmed.parse::<usize>() {
Ok(d) if d > 0 => Ok(d),
Ok(_) => Err(format!("VECTOR dimension must be ≥ 1 (got `{trimmed}`)")),
Err(_) => Err(format!(
"VECTOR dimension must be a positive integer (got `{trimmed}`)"
)),
}
}
many => Err(format!(
"VECTOR takes exactly one dimension argument (got {})",
many.len()
)),
}
}
/// The schema for each SQL column in every table is represented by
/// the following structure after parsed and tokenized
#[derive(PartialEq, Debug)]
pub struct ParsedColumn {
/// Name of the column
pub name: String,
/// Datatype of the column in String format
pub datatype: String,
/// Value representing if column is PRIMARY KEY
pub is_pk: bool,
/// Value representing if column was declared with the NOT NULL Constraint
pub not_null: bool,
/// Value representing if column was declared with the UNIQUE Constraint
pub is_unique: bool,
}
/// The following structure represents a CREATE TABLE query already parsed
/// and broken down into name and a Vector of `ParsedColumn` metadata
///
#[derive(Debug)]
pub struct CreateQuery {
/// name of table after parking and tokenizing of query
pub table_name: String,
/// Vector of `ParsedColumn` type with column metadata information
pub columns: Vec<ParsedColumn>,
}
impl CreateQuery {
pub fn new(statement: &Statement) -> Result<CreateQuery> {
match statement {
// Confirming the Statement is sqlparser::ast:Statement::CreateTable
Statement::CreateTable(CreateTable {
name,
columns,
constraints,
..
}) => {
let table_name = name;
let mut parsed_columns: Vec<ParsedColumn> = vec![];
// Iterating over the columns returned form the Parser::parse:sql
// in the mod sql
for col in columns {
let name = col.name.to_string();
// Checks if columm already added to parsed_columns, if so, returns an error
if parsed_columns.iter().any(|col| col.name == name) {
return Err(SQLRiteError::Internal(format!(
"Duplicate column name: {}",
&name
)));
}
// Parsing each column for it data type
// For now only accepting basic data types
let datatype: String = match &col.data_type {
DataType::TinyInt(_)
| DataType::SmallInt(_)
| DataType::Int2(_)
| DataType::Int(_)
| DataType::Int4(_)
| DataType::Int8(_)
| DataType::Integer(_)
| DataType::BigInt(_) => "Integer".to_string(),
DataType::Boolean => "Bool".to_string(),
DataType::Text => "Text".to_string(),
DataType::Varchar(_bytes) => "Text".to_string(),
DataType::Real => "Real".to_string(),
DataType::Float(_precision) => "Real".to_string(),
DataType::Double(_) => "Real".to_string(),
DataType::Decimal(_) => "Real".to_string(),
// Phase 7e — `JSON` parses as a unit variant in
// sqlparser's DataType enum. JSONB is treated as
// an alias (matches PostgreSQL's permissive
// behaviour); both store as text under the hood.
DataType::JSON | DataType::JSONB => "Json".to_string(),
// Phase 7a — `VECTOR(N)` parses as Custom("VECTOR", ["N"]).
// sqlparser's SQLite dialect doesn't have a built-in
// Vector variant; Custom is what unrecognized type
// names + their parenthesized args fall through to.
DataType::Custom(name, args) if is_vector_type(name) => {
match parse_vector_dim(args) {
Ok(dim) => format!("vector({dim})"),
Err(e) => {
return Err(SQLRiteError::General(format!(
"Invalid VECTOR column '{}': {e}",
col.name
)));
}
}
}
other => {
eprintln!("not matched on custom type: {other:?}");
"Invalid".to_string()
}
};
// checking if column is PRIMARY KEY
let mut is_pk: bool = false;
// chekcing if column is UNIQUE
let mut is_unique: bool = false;
// chekcing if column is NULLABLE
let mut not_null: bool = false;
for column_option in &col.options {
match &column_option.option {
ColumnOption::PrimaryKey(_) => {
// For now, only Integer and Text types can be PRIMARY KEY and Unique
// Therefore Indexed.
if datatype != "Real" && datatype != "Bool" {
// Checks if table being created already has a PRIMARY KEY, if so, returns an error
if parsed_columns.iter().any(|col| col.is_pk) {
return Err(SQLRiteError::Internal(format!(
"Table '{}' has more than one primary key",
&table_name
)));
}
is_pk = true;
is_unique = true;
not_null = true;
}
}
ColumnOption::Unique(_) => {
// For now, only Integer and Text types can be UNIQUE
// Therefore Indexed.
if datatype != "Real" && datatype != "Bool" {
is_unique = true;
}
}
ColumnOption::NotNull => {
not_null = true;
}
_ => (),
};
}
parsed_columns.push(ParsedColumn {
name,
datatype: datatype.to_string(),
is_pk,
not_null,
is_unique,
});
}
// TODO: Handle constraints,
// Default value and others.
for constraint in constraints {
println!("{constraint:?}");
}
Ok(CreateQuery {
table_name: table_name.to_string(),
columns: parsed_columns,
})
}
_ => Err(SQLRiteError::Internal("Error parsing query".to_string())),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::sql::*;
#[test]
fn create_table_validate_tablename_test() {
let sql_input = String::from(
"CREATE TABLE contacts (
id INTEGER PRIMARY KEY,
first_name TEXT NOT NULL,
last_name TEXT NOT NULl,
email TEXT NOT NULL UNIQUE
);",
);
let expected_table_name = String::from("contacts");
let dialect = SQLiteDialect {};
let mut ast = Parser::parse_sql(&dialect, &sql_input).unwrap();
assert!(ast.len() == 1, "ast has more then one Statement");
let query = ast.pop().unwrap();
// Initialy only implementing some basic SQL Statements
if let Statement::CreateTable(_) = query {
let result = CreateQuery::new(&query);
match result {
Ok(payload) => {
assert_eq!(payload.table_name, expected_table_name);
}
Err(_) => panic!("an error occured during parsing CREATE TABLE Statement"),
}
}
}
}