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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
use crate::{ logic::{read_args, read_map_as_str}, select::{select_all, select_args}, }; use super::{read_map, read_match_args, FromStr, MatchCondition, Uuid, Wql}; pub(crate) fn read_symbol(a: char, chars: &mut std::str::Chars) -> Result<Wql, String> { let symbol = chars.take_while(|c| !c.is_whitespace()).collect::<String>(); match (a, &symbol.to_uppercase()[..]) { ('c', "REATE") | ('C', "REATE") => create_entity(chars), ('i', "NSERT") | ('I', "NSERT") => insert(chars), ('u', "PDATE") | ('U', "PDATE") => update(chars), ('d', "ELETE") | ('D', "ELETE") => delete(chars), ('m', "ATCH") | ('M', "ATCH") => match_update(chars), ('e', "VICT") | ('E', "VICT") => evict(chars), ('s', "ELECT") | ('S', "ELECT") => select(chars), ('c', "HECK") | ('C', "HECK") => check(chars), _ => Err(format!("Symbol `{}{}` not implemented", a, symbol)), } } fn create_entity(chars: &mut std::str::Chars) -> Result<Wql, String> { let entity_symbol = chars.take_while(|c| !c.is_whitespace()).collect::<String>(); if entity_symbol.to_uppercase() != "ENTITY" { return Err(String::from("Keyword ENTITY is required for CREATE")); } let entity_name = chars .take_while(|c| c.is_alphanumeric() || c == &'_') .collect::<String>() .trim() .to_string(); let next_symbol = chars.take_while(|c| !c.is_whitespace()).collect::<String>(); if next_symbol.to_uppercase() == "UNIQUES" { let mut encrypts = Vec::new(); if chars.next() != Some('#') { return Err(String::from( "Arguments set should start with `#{` and end with `}`", )); } let unique_vec = read_args(chars)?; let encrypt_symbol = chars .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect::<String>(); if encrypt_symbol.to_uppercase() == "ENCRYPT" { if chars.next() != Some('#') { return Err(String::from( "Arguments set should start with `#{` and end with `}`", )); } encrypts = read_args(chars)?; } if encrypts.iter().any(|e| unique_vec.contains(e)) { return Err(String::from("Encrypted arguments cannot be set to UNIQUE")); } Ok(Wql::CreateEntity(entity_name, unique_vec, encrypts)) } else if next_symbol.to_uppercase() == "ENCRYPT" { let mut unique_vec = Vec::new(); if chars.next() != Some('#') { return Err(String::from( "Arguments set should start with `#{` and end with `}`", )); } let encrypt_vec = read_args(chars)?; let unique_symbol = chars .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect::<String>(); if unique_symbol.to_uppercase() == "UNIQUES" { if chars.next() != Some('#') { return Err(String::from( "Arguments set should start with `#{` and end with `}`", )); } unique_vec = read_args(chars)?; } if encrypt_vec.iter().any(|e| unique_vec.contains(e)) { return Err(String::from("Encrypted arguments cannot be set to UNIQUE")); } Ok(Wql::CreateEntity(entity_name, unique_vec, encrypt_vec)) } else { Ok(Wql::CreateEntity(entity_name, Vec::new(), Vec::new())) } } fn select(chars: &mut std::str::Chars) -> Result<Wql, String> { loop { match chars.next() { Some(' ') => (), Some('*') => return select_all(chars), Some('#') => return select_args(chars), _ => return Err(String::from("SELECT expression should be followed by `*` for ALL keys or `#{key_names...}` for some keys")) } } } fn delete(chars: &mut std::str::Chars) -> Result<Wql, String> { let entity_id = chars .take_while(|c| c.is_alphanumeric() || c == &'-') .collect::<String>() .trim() .to_string(); if entity_id.is_empty() || entity_id == "FROM" { return Err(String::from("Entity UUID is required for DELETE")); } let entity_symbol = chars .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect::<String>(); if entity_symbol.to_uppercase() != "FROM" { return Err(String::from("Keyword FROM is required for DELETE")); } let entity_name = chars .take_while(|c| c.is_alphanumeric() || c == &'_') .collect::<String>() .trim() .to_string(); if entity_name.is_empty() { return Err(String::from("Entity name is required after FROM")); } Ok(Wql::Delete(entity_name, entity_id)) } fn insert(chars: &mut std::str::Chars) -> Result<Wql, String> { let entity_map = read_map(chars)?; let entity_symbol = chars .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect::<String>(); if entity_symbol.to_uppercase() != "INTO" { return Err(String::from("Keyword INTO is required for INSERT")); } let entity_name = chars .take_while(|c| c.is_alphanumeric() || c == &'_') .collect::<String>() .trim() .to_string(); if entity_name.is_empty() { return Err(String::from("Entity name is required after INTO")); } Ok(Wql::Insert(entity_name, entity_map)) } fn check(chars: &mut std::str::Chars) -> Result<Wql, String> { let entity_map = read_map_as_str(chars)?; let entity_symbol = chars .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect::<String>(); if entity_symbol.to_uppercase() != "FROM" { return Err(String::from("Keyword FROM is required for CHECK")); } let entity_name = chars .take_while(|c| c.is_alphanumeric() || c == &'_') .collect::<String>() .trim() .to_string(); if entity_name.is_empty() { return Err(String::from("Entity name is required after FROM")); } let id_symbol = chars .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect::<String>(); if id_symbol.to_uppercase() != "ID" { return Err(String::from("Keyword FROM is required for CHECK")); } let entity_id = chars .take_while(|c| c.is_alphanumeric() || c == &'-') .collect::<String>() .trim() .to_owned(); let id = Uuid::from_str(&entity_id).map_err(|e| format!("{:?}", e))?; Ok(Wql::CheckValue(entity_name, id, entity_map)) } fn update(chars: &mut std::str::Chars) -> Result<Wql, String> { let entity_name = chars .take_while(|c| c.is_alphanumeric() || c == &'_') .collect::<String>() .trim() .to_string(); if entity_name.is_empty() { return Err(String::from("Entity name is required for UPDATE")); }; let entity_symbol = chars .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect::<String>(); if entity_symbol.to_uppercase() != "SET" && entity_symbol.to_uppercase() != "CONTENT" { return Err(String::from( "UPDATE type is required after entity. Keywords are SET or CONTENT", )); }; let entity_map = read_map(chars)?; let into_symbol = chars .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect::<String>(); if into_symbol.to_uppercase() != "INTO" { return Err(String::from("Keyword INTO is required for UPDATE")); }; let uuid_str = chars .take_while(|c| c.is_alphanumeric() || c == &'-') .collect::<String>() .trim() .to_string(); let uuid = Uuid::from_str(&uuid_str) .map_err(|e| format!("Couldn't create uuid from {}. Error: {:?}", uuid_str, e))?; match &entity_symbol.to_uppercase()[..] { "SET" => Ok(Wql::UpdateSet(entity_name, entity_map, uuid)), "CONTENT" => Ok(Wql::UpdateContent(entity_name, entity_map, uuid)), _ => Err("Couldn't parse UPDATE query".to_string()), } } fn match_update(chars: &mut std::str::Chars) -> Result<Wql, String> { let match_arg_symbol = chars .skip_while(|c| c.is_whitespace()) .take_while(|c| c.is_alphabetic()) .collect::<String>(); if &match_arg_symbol.to_uppercase() != "ALL" && &match_arg_symbol.to_uppercase() != "ANY" { return Err(String::from("MATCH requires ALL or ANY symbols")); } let logical_args = read_match_args(chars)?; let match_args = if match_arg_symbol.to_uppercase().eq("ALL") { Ok(MatchCondition::All(logical_args)) } else if match_arg_symbol.to_uppercase().eq("ANY") { Ok(MatchCondition::Any(logical_args)) } else { Err(String::from("MATCH requires ALL or ANY symbols")) }; let update_symbol = chars .skip_while(|c| c.is_whitespace()) .take_while(|c| c.is_alphabetic()) .collect::<String>(); if update_symbol.to_uppercase() != "UPDATE" { return Err(String::from("UPDATE keyword is required for MATCH UPDATE")); }; let entity_name = chars .take_while(|c| c.is_alphanumeric() || c == &'_') .collect::<String>() .trim() .to_string(); if entity_name.is_empty() { return Err(String::from("Entity name is required for MATCH UPDATE")); }; let entity_symbol = chars .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect::<String>(); if entity_symbol.to_uppercase() != "SET" { return Err(String::from( "MATCH UPDATE type is required after entity. Keyword is SET", )); }; let entity_map = read_map(chars)?; let into_symbol = chars .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect::<String>(); if into_symbol.to_uppercase() != "INTO" { return Err(String::from("Keyword INTO is required for MATCH UPDATE")); }; let uuid_str = chars .take_while(|c| c.is_alphanumeric() || c == &'-') .collect::<String>() .trim() .to_string(); let uuid = Uuid::from_str(&uuid_str) .map_err(|e| format!("Couldn't create uuid from {}, Error: {:?}", uuid_str, e))?; match &entity_symbol.to_uppercase()[..] { "SET" => Ok(Wql::MatchUpdate(entity_name, entity_map, uuid, match_args?)), _ => Err("Couldn't parse UPDATE query".to_string()), } } fn evict(chars: &mut std::str::Chars) -> Result<Wql, String> { let info = chars .take_while(|c| c.is_alphanumeric() || c == &'-' || c == &'_') .collect::<String>() .trim() .to_string(); let uuid = Uuid::from_str(&info); if uuid.is_err() { if info.chars().any(|c| c == '-') { return Err("Entity name cannot contain `-`".to_string()); } Ok(Wql::Evict(info, None)) } else { let from_symbol = chars .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect::<String>() .trim() .to_string(); if from_symbol.to_uppercase() != "FROM" { return Err(String::from("FROM keyword is required to EVICT an UUID")); } let name = chars .take_while(|c| c.is_alphanumeric() || c == &'_') .collect::<String>() .trim() .to_string(); if name.is_empty() { return Err(String::from("Entity name is required")); } Ok(Wql::Evict(name, uuid.ok())) } }