Parse a SET command: `SET [SESSION|LOCAL] key = value` or `SET key TO value`. Returns (key, value) on success, or None if not a valid SET command.
(sql: &str)
| 38 | /// |
| 39 | /// Returns (key, value) on success, or None if not a valid SET command. |
| 40 | pub fn parse_set_command(sql: &str) -> Option<(String, String)> { |
| 41 | let trimmed = sql.trim(); |
| 42 | let upper = trimmed.to_uppercase(); |
| 43 | |
| 44 | // Strip SET prefix. |
| 45 | let rest = if upper.starts_with("SET SESSION ") { |
| 46 | &trimmed[12..] |
| 47 | } else if upper.starts_with("SET LOCAL ") { |
| 48 | &trimmed[10..] |
| 49 | } else if upper.starts_with("SET ") { |
| 50 | &trimmed[4..] |
| 51 | } else { |
| 52 | return None; |
| 53 | }; |
| 54 | |
| 55 | let rest = rest.trim(); |
| 56 | |
| 57 | // Split on = or TO. |
| 58 | let (key, value) = if let Some(eq_pos) = rest.find('=') { |
| 59 | let k = rest[..eq_pos].trim(); |
| 60 | let v = rest[eq_pos + 1..].trim(); |
| 61 | (k, v) |
| 62 | } else { |
| 63 | // Try TO separator. |
| 64 | let upper_rest = rest.to_uppercase(); |
| 65 | if let Some(to_pos) = upper_rest.find(" TO ") { |
| 66 | let k = rest[..to_pos].trim(); |
| 67 | let v = rest[to_pos + 4..].trim(); |
| 68 | (k, v) |
| 69 | } else { |
| 70 | return None; |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | if key.is_empty() { |
| 75 | return None; |
| 76 | } |
| 77 | |
| 78 | // Strip quotes from value. |
| 79 | let value = value.trim_matches('\'').trim_matches('"').to_string(); |
| 80 | |
| 81 | Some((key.to_lowercase(), value)) |
| 82 | } |
| 83 | |
| 84 | /// Known PostgreSQL runtime parameters that `SHOW <name>` is allowed to |
| 85 | /// resolve through the session-parameter fallback. |