(
env: &mut Environment,
tokens: &[Token],
position: &mut usize,
)
| 177 | } |
| 178 | |
| 179 | fn parse_describe_query( |
| 180 | env: &mut Environment, |
| 181 | tokens: &[Token], |
| 182 | position: &mut usize, |
| 183 | ) -> Result<Query, Box<Diagnostic>> { |
| 184 | // Consume `DESCRIBE` keyword |
| 185 | *position += 1; |
| 186 | |
| 187 | if *position >= tokens.len() || !matches!(tokens[*position].kind, TokenKind::Symbol(_)) { |
| 188 | return Err( |
| 189 | Diagnostic::error("Expect table name after DESCRIBE Statement") |
| 190 | .with_location(calculate_safe_location(tokens, *position)) |
| 191 | .as_boxed(), |
| 192 | ); |
| 193 | } |
| 194 | |
| 195 | // Make sure table name is valid |
| 196 | let table_name = tokens[*position].to_string(); |
| 197 | if !env |
| 198 | .schema |
| 199 | .tables_fields_names |
| 200 | .contains_key(table_name.as_str()) |
| 201 | { |
| 202 | let mut diagnostic = |
| 203 | Diagnostic::error(&format!("Cannot find table with name `{table_name}`")) |
| 204 | .add_note("You can use the `SHOW TABLES` query to get list of current tables") |
| 205 | .add_note("Check the documentations to see available tables") |
| 206 | .with_location(calculate_safe_location(tokens, *position)); |
| 207 | |
| 208 | let canditates: Vec<&&str> = env.schema.tables_fields_names.keys().collect(); |
| 209 | if let Some(closest_valid_name) = find_closeest_string(&table_name, &canditates) { |
| 210 | let message = &format!("a table with a similar name exists: `{closest_valid_name}`"); |
| 211 | diagnostic = diagnostic.add_help(message); |
| 212 | } |
| 213 | |
| 214 | return Err(diagnostic.as_boxed()); |
| 215 | } |
| 216 | |
| 217 | // Consume Table Name |
| 218 | *position += 1; |
| 219 | |
| 220 | Ok(Query::DescribeTable(DescribeQuery { table_name })) |
| 221 | } |
| 222 | |
| 223 | fn parse_show_query(tokens: &[Token], position: &mut usize) -> Result<Query, Box<Diagnostic>> { |
| 224 | // Consume SHOW keyword |
no test coverage detected
searching dependent graphs…