|
| 1 | +//! Raw SQL queries without parameter binding. |
| 2 | +//! |
| 3 | +//! [`QueryRaw`] executes SQL as-is without parameter binding support. |
| 4 | +//! Question marks are escaped using an odd/even strategy: |
| 5 | +//! - `?` → `??` (odd count, add one more) |
| 6 | +//! - `??` → `??` (even count, unchanged) |
| 7 | +use crate::{error::Result, row::Row, sql::Bind, Client}; |
| 8 | +use serde::Serialize; |
| 9 | + |
| 10 | +pub use crate::cursors::{BytesCursor, RowCursor}; |
| 11 | +use crate::query::Query; |
| 12 | + |
| 13 | +#[must_use] |
| 14 | +#[derive(Clone)] |
| 15 | +pub struct QueryRaw { |
| 16 | + query: Query, |
| 17 | +} |
| 18 | + |
| 19 | +impl QueryRaw { |
| 20 | + pub(crate) fn new(client: &Client, template: &str) -> Self { |
| 21 | + // Raw queries apply odd/even question mark strategy: |
| 22 | + // - Consecutive odd number of ? (1,3,5...) -> add one more ? |
| 23 | + // - Consecutive even number of ? (2,4,6...) -> keep as is |
| 24 | + Self { |
| 25 | + query: Query::new_raw(client, template), |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + /// Display the SQL query with question mark escaping applied. |
| 30 | + pub fn sql_display(&self) -> &impl std::fmt::Display { |
| 31 | + self.query.sql_display() |
| 32 | + } |
| 33 | + |
| 34 | + /// Attempts to bind a value to the query. |
| 35 | + /// |
| 36 | + /// # Panics |
| 37 | + /// Always panics - raw queries don't support parameter binding. |
| 38 | + #[track_caller] |
| 39 | + pub fn bind(self, _value: impl Bind) -> Self { |
| 40 | + panic!("cannot bind parameters to raw query - use regular query() for parameter binding"); |
| 41 | + } |
| 42 | + |
| 43 | + /// Attempts to set server-side parameters for the query. |
| 44 | + /// |
| 45 | + /// # Panics |
| 46 | + /// Always panics - raw queries don't support parameter binding. |
| 47 | + #[track_caller] |
| 48 | + pub fn param(self, _name: &str, _value: impl Serialize) -> Self { |
| 49 | + panic!("cannot set parameters on raw query - use regular query() for parameter binding"); |
| 50 | + } |
| 51 | + |
| 52 | + /// Executes the query. |
| 53 | + pub async fn execute(self) -> Result<()> { |
| 54 | + self.query.execute().await |
| 55 | + } |
| 56 | + |
| 57 | + /// Executes the query, returning a [`RowCursor`] to obtain results. |
| 58 | + pub fn fetch<T: Row>(self) -> Result<RowCursor<T>> { |
| 59 | + self.query.fetch() |
| 60 | + } |
| 61 | + |
| 62 | + /// Executes the query and returns just a single row. |
| 63 | + pub async fn fetch_one<T>(self) -> Result<T> |
| 64 | + where |
| 65 | + T: Row + for<'b> serde::Deserialize<'b>, |
| 66 | + { |
| 67 | + self.query.fetch_one().await |
| 68 | + } |
| 69 | + |
| 70 | + /// Executes the query and returns at most one row. |
| 71 | + pub async fn fetch_optional<T>(self) -> Result<Option<T>> |
| 72 | + where |
| 73 | + T: Row + for<'b> serde::Deserialize<'b>, |
| 74 | + { |
| 75 | + self.query.fetch_optional().await |
| 76 | + } |
| 77 | + |
| 78 | + /// Executes the query and returns all the generated results, |
| 79 | + /// collected into a Vec. |
| 80 | + pub async fn fetch_all<T>(self) -> Result<Vec<T>> |
| 81 | + where |
| 82 | + T: Row + for<'b> serde::Deserialize<'b>, |
| 83 | + { |
| 84 | + self.query.fetch_all().await |
| 85 | + } |
| 86 | + |
| 87 | + /// Executes the query, returning a [`BytesCursor`] to obtain results as raw |
| 88 | + /// bytes containing data in the [provided format]. |
| 89 | + pub fn fetch_bytes(self, format: impl Into<String>) -> Result<BytesCursor> { |
| 90 | + self.query.fetch_bytes(format) |
| 91 | + } |
| 92 | + |
| 93 | + /// Similar to [`Client::with_option`], but for this particular query only. |
| 94 | + pub fn with_option(mut self, name: impl Into<String>, value: impl Into<String>) -> Self { |
| 95 | + self.query = self.query.with_option(name, value); |
| 96 | + self |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +impl std::fmt::Display for QueryRaw { |
| 101 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 102 | + write!(f, "{}", self.query.sql_display()) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +#[cfg(test)] |
| 107 | +mod tests { |
| 108 | + use crate::Client; |
| 109 | + use serde::{Deserialize, Serialize}; |
| 110 | + |
| 111 | + // XXX: need for `derive(Row)`. Provide `row(crate = ..)` instead. |
| 112 | + use crate as clickhouse; |
| 113 | + use clickhouse_derive::Row; |
| 114 | + |
| 115 | + #[derive(Row, Debug, Serialize, Deserialize)] |
| 116 | + struct TestRow { |
| 117 | + id: u32, |
| 118 | + name: String, |
| 119 | + } |
| 120 | + |
| 121 | + #[test] |
| 122 | + fn test_question_mark_escaping() { |
| 123 | + let client = Client::default(); |
| 124 | + let query = client.query_raw("SELECT * FROM test WHERE name LIKE 'test%?'"); |
| 125 | + // Single ? (odd) becomes ?? |
| 126 | + assert_eq!( |
| 127 | + query.sql_display().to_string(), |
| 128 | + "SELECT * FROM test WHERE name LIKE 'test%??'" |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + #[test] |
| 133 | + fn test_multiple_question_marks() { |
| 134 | + let client = Client::default(); |
| 135 | + let query = client.query_raw("SELECT * FROM test WHERE a = ? AND b = ?"); |
| 136 | + // Each single ? (odd) becomes ?? |
| 137 | + assert_eq!( |
| 138 | + query.sql_display().to_string(), |
| 139 | + "SELECT * FROM test WHERE a = ?? AND b = ??" |
| 140 | + ); |
| 141 | + } |
| 142 | + |
| 143 | + #[test] |
| 144 | + fn test_already_escaped_question_marks() { |
| 145 | + let client = Client::default(); |
| 146 | + let query = client.query_raw("SELECT * FROM test WHERE name LIKE 'test%??'"); |
| 147 | + // Double ?? (even) stays ?? |
| 148 | + assert_eq!( |
| 149 | + query.sql_display().to_string(), |
| 150 | + "SELECT * FROM test WHERE name LIKE 'test%??'" |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + #[test] |
| 155 | + #[should_panic(expected = "cannot bind parameters to raw query")] |
| 156 | + fn test_bind_panics() { |
| 157 | + let client = Client::default(); |
| 158 | + let query = client.query_raw("SELECT * FROM test WHERE id = ?"); |
| 159 | + let _ = query.bind(42); // This should panic |
| 160 | + } |
| 161 | + |
| 162 | + #[test] |
| 163 | + #[should_panic(expected = "cannot set parameters on raw query")] |
| 164 | + fn test_param_panics() { |
| 165 | + let client = Client::default(); |
| 166 | + let query = client.query_raw("SELECT * FROM test WHERE id = {val: Int32}"); |
| 167 | + let _ = query.param("val", 42); // This should panic |
| 168 | + } |
| 169 | + |
| 170 | + #[test] |
| 171 | + fn test_with_option() { |
| 172 | + let client = Client::default(); |
| 173 | + let query = client |
| 174 | + .query_raw("SELECT * FROM test") |
| 175 | + .with_option("max_execution_time", "60"); |
| 176 | + assert_eq!(query.sql_display().to_string(), "SELECT * FROM test"); |
| 177 | + } |
| 178 | + |
| 179 | + #[test] |
| 180 | + fn test_display_implementation() { |
| 181 | + let client = Client::default(); |
| 182 | + let query = client.query_raw("SELECT * FROM test WHERE name LIKE 'test%?'"); |
| 183 | + let display = format!("{}", query); |
| 184 | + assert_eq!(display, "SELECT * FROM test WHERE name LIKE 'test%??'"); |
| 185 | + } |
| 186 | + |
| 187 | + #[test] |
| 188 | + fn test_complex_escaping() { |
| 189 | + let client = Client::default(); |
| 190 | + let query = client.query_raw( |
| 191 | + "SELECT '?', '??', '???', 'a?b', 'a??b', 'a???b' FROM test WHERE pattern LIKE '%?%'", |
| 192 | + ); |
| 193 | + // ? -> ??, ?? -> ??, ??? -> ????, a?b -> a??b, a??b -> a??b, a???b -> a????b, %?% -> %??% |
| 194 | + let expected = "SELECT '??', '??', '????', 'a??b', 'a??b', 'a????b' FROM test WHERE pattern LIKE '%??%'"; |
| 195 | + assert_eq!(query.sql_display().to_string(), expected); |
| 196 | + } |
| 197 | + |
| 198 | + #[test] |
| 199 | + fn test_mixed_patterns() { |
| 200 | + let client = Client::default(); |
| 201 | + // Test odd/even strategy: ? -> ??, ?? -> ??, ??? -> ????, ???? -> ???? |
| 202 | + let query = client.query_raw("SELECT '?', '??', '???', '????' FROM test"); |
| 203 | + let expected = "SELECT '??', '??', '????', '????' FROM test"; |
| 204 | + assert_eq!(query.sql_display().to_string(), expected); |
| 205 | + } |
| 206 | + |
| 207 | + #[test] |
| 208 | + fn test_consecutive_question_marks() { |
| 209 | + let client = Client::default(); |
| 210 | + // Test various consecutive patterns |
| 211 | + let query = client.query_raw("SELECT '?????', '??????', '???????' FROM test"); |
| 212 | + // ????? (5, odd) -> ??????, ?????? (6, even) -> ??????, ??????? (7, odd) -> ???????? |
| 213 | + let expected = "SELECT '??????', '??????', '????????' FROM test"; |
| 214 | + assert_eq!(query.sql_display().to_string(), expected); |
| 215 | + } |
| 216 | + |
| 217 | + #[test] |
| 218 | + fn test_no_question_marks() { |
| 219 | + let client = Client::default(); |
| 220 | + let query = client.query_raw("SELECT * FROM test WHERE id = 42"); |
| 221 | + assert_eq!( |
| 222 | + query.sql_display().to_string(), |
| 223 | + "SELECT * FROM test WHERE id = 42" |
| 224 | + ); |
| 225 | + } |
| 226 | +} |
0 commit comments