|
| 1 | +use pgt_text_size::{TextRange, TextSize}; |
| 2 | + |
| 3 | +#[derive(Clone, Debug, PartialEq, Eq)] |
| 4 | +pub(crate) struct WordWithIndex { |
| 5 | + word: String, |
| 6 | + start: usize, |
| 7 | + end: usize, |
| 8 | +} |
| 9 | + |
| 10 | +impl WordWithIndex { |
| 11 | + pub(crate) fn is_under_cursor(&self, cursor_pos: usize) -> bool { |
| 12 | + self.start <= cursor_pos && self.end > cursor_pos |
| 13 | + } |
| 14 | + |
| 15 | + pub(crate) fn get_range(&self) -> TextRange { |
| 16 | + let start: u32 = self.start.try_into().expect("Text too long"); |
| 17 | + let end: u32 = self.end.try_into().expect("Text too long"); |
| 18 | + TextRange::new(TextSize::from(start), TextSize::from(end)) |
| 19 | + } |
| 20 | + |
| 21 | + pub(crate) fn get_word_without_quotes(&self) -> String { |
| 22 | + self.word.replace('"', "") |
| 23 | + } |
| 24 | + |
| 25 | + pub(crate) fn get_word(&self) -> String { |
| 26 | + self.word.clone() |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +/// Note: A policy name within quotation marks will be considered a single word. |
| 31 | +pub(crate) fn sql_to_words(sql: &str) -> Result<Vec<WordWithIndex>, String> { |
| 32 | + let mut words = vec![]; |
| 33 | + |
| 34 | + let mut start_of_word: Option<usize> = None; |
| 35 | + let mut current_word = String::new(); |
| 36 | + let mut in_quotation_marks = false; |
| 37 | + |
| 38 | + for (current_position, current_char) in sql.char_indices() { |
| 39 | + if (current_char.is_ascii_whitespace() || current_char == ';') |
| 40 | + && !current_word.is_empty() |
| 41 | + && start_of_word.is_some() |
| 42 | + && !in_quotation_marks |
| 43 | + { |
| 44 | + words.push(WordWithIndex { |
| 45 | + word: current_word, |
| 46 | + start: start_of_word.unwrap(), |
| 47 | + end: current_position, |
| 48 | + }); |
| 49 | + |
| 50 | + current_word = String::new(); |
| 51 | + start_of_word = None; |
| 52 | + } else if (current_char.is_ascii_whitespace() || current_char == ';') |
| 53 | + && current_word.is_empty() |
| 54 | + { |
| 55 | + // do nothing |
| 56 | + } else if current_char == '"' && start_of_word.is_none() { |
| 57 | + in_quotation_marks = true; |
| 58 | + current_word.push(current_char); |
| 59 | + start_of_word = Some(current_position); |
| 60 | + } else if current_char == '"' && start_of_word.is_some() { |
| 61 | + current_word.push(current_char); |
| 62 | + in_quotation_marks = false; |
| 63 | + } else if start_of_word.is_some() { |
| 64 | + current_word.push(current_char) |
| 65 | + } else { |
| 66 | + start_of_word = Some(current_position); |
| 67 | + current_word.push(current_char); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + if let Some(start_of_word) = start_of_word { |
| 72 | + if !current_word.is_empty() { |
| 73 | + words.push(WordWithIndex { |
| 74 | + word: current_word, |
| 75 | + start: start_of_word, |
| 76 | + end: sql.len(), |
| 77 | + }); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if in_quotation_marks { |
| 82 | + Err("String was not closed properly.".into()) |
| 83 | + } else { |
| 84 | + Ok(words) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +#[cfg(test)] |
| 89 | +mod tests { |
| 90 | + use crate::context::parser_helper::{WordWithIndex, sql_to_words}; |
| 91 | + |
| 92 | + #[test] |
| 93 | + fn determines_positions_correctly() { |
| 94 | + let query = "\ncreate policy \"my cool pol\"\n\ton auth.users\n\tas permissive\n\tfor select\n\t\tto public\n\t\tusing (true);".to_string(); |
| 95 | + |
| 96 | + let words = sql_to_words(query.as_str()).unwrap(); |
| 97 | + |
| 98 | + assert_eq!(words[0], to_word("create", 1, 7)); |
| 99 | + assert_eq!(words[1], to_word("policy", 8, 14)); |
| 100 | + assert_eq!(words[2], to_word("\"my cool pol\"", 15, 28)); |
| 101 | + assert_eq!(words[3], to_word("on", 30, 32)); |
| 102 | + assert_eq!(words[4], to_word("auth.users", 33, 43)); |
| 103 | + assert_eq!(words[5], to_word("as", 45, 47)); |
| 104 | + assert_eq!(words[6], to_word("permissive", 48, 58)); |
| 105 | + assert_eq!(words[7], to_word("for", 60, 63)); |
| 106 | + assert_eq!(words[8], to_word("select", 64, 70)); |
| 107 | + assert_eq!(words[9], to_word("to", 73, 75)); |
| 108 | + assert_eq!(words[10], to_word("public", 78, 84)); |
| 109 | + assert_eq!(words[11], to_word("using", 87, 92)); |
| 110 | + assert_eq!(words[12], to_word("(true)", 93, 99)); |
| 111 | + } |
| 112 | + |
| 113 | + #[test] |
| 114 | + fn handles_schemas_in_quotation_marks() { |
| 115 | + let query = r#"grant select on "public"."users""#.to_string(); |
| 116 | + |
| 117 | + let words = sql_to_words(query.as_str()).unwrap(); |
| 118 | + |
| 119 | + assert_eq!(words[0], to_word("grant", 0, 5)); |
| 120 | + assert_eq!(words[1], to_word("select", 6, 12)); |
| 121 | + assert_eq!(words[2], to_word("on", 13, 15)); |
| 122 | + assert_eq!(words[3], to_word(r#""public"."users""#, 16, 32)); |
| 123 | + } |
| 124 | + |
| 125 | + fn to_word(word: &str, start: usize, end: usize) -> WordWithIndex { |
| 126 | + WordWithIndex { |
| 127 | + word: word.into(), |
| 128 | + start, |
| 129 | + end, |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments