|
1 |
| -use sqlx::any::AnyRow; |
| 1 | +use sqlx::any::{install_default_drivers, AnyRow}; |
2 | 2 | use sqlx::{Any, Connection, Executor, Row};
|
| 3 | +use sqlx_core::error::BoxDynError; |
3 | 4 | use sqlx_core::sql_str::AssertSqlSafe;
|
| 5 | +use sqlx_core::Error; |
4 | 6 | use sqlx_test::new;
|
5 | 7 |
|
6 | 8 | #[sqlx_macros::test]
|
@@ -142,3 +144,65 @@ async fn it_can_fail_and_recover_with_pool() -> anyhow::Result<()> {
|
142 | 144 |
|
143 | 145 | Ok(())
|
144 | 146 | }
|
| 147 | + |
| 148 | +#[sqlx_macros::test] |
| 149 | +async fn it_can_query_by_string_args() -> sqlx::Result<()> { |
| 150 | + install_default_drivers(); |
| 151 | + |
| 152 | + let mut conn = new::<Any>().await?; |
| 153 | + |
| 154 | + let string = "Hello, world!".to_string(); |
| 155 | + let ref tuple = ("Hello, world!".to_string(),); |
| 156 | + |
| 157 | + #[cfg(feature = "postgres")] |
| 158 | + const SQL: &str = |
| 159 | + "SELECT 'Hello, world!' as string where 'Hello, world!' in ($1, $2, $3, $4, $5, $6, $7)"; |
| 160 | + |
| 161 | + #[cfg(not(feature = "postgres"))] |
| 162 | + const SQL: &str = |
| 163 | + "SELECT 'Hello, world!' as string where 'Hello, world!' in (?, ?, ?, ?, ?, ?, ?)"; |
| 164 | + |
| 165 | + { |
| 166 | + let query = sqlx::query(SQL) |
| 167 | + // validate flexibility of lifetimes |
| 168 | + .bind(&string) |
| 169 | + .bind(&string[..]) |
| 170 | + .bind(Some(&string)) |
| 171 | + .bind(Some(&string[..])) |
| 172 | + .bind(&Option::<String>::None) |
| 173 | + .bind(&string.clone()) |
| 174 | + .bind(&tuple.0); // should not get "temporary value is freed at the end of this statement" here |
| 175 | + |
| 176 | + let result = query.fetch_one(&mut conn).await?; |
| 177 | + |
| 178 | + let column_0: String = result.try_get(0)?; |
| 179 | + |
| 180 | + assert_eq!(column_0, string); |
| 181 | + } |
| 182 | + |
| 183 | + { |
| 184 | + let mut query = sqlx::query(SQL); |
| 185 | + |
| 186 | + let query = || -> Result<_, BoxDynError> { |
| 187 | + // validate flexibility of lifetimes |
| 188 | + query.try_bind(&string)?; |
| 189 | + query.try_bind(&string[..])?; |
| 190 | + query.try_bind(Some(&string))?; |
| 191 | + query.try_bind(Some(&string[..]))?; |
| 192 | + query.try_bind(&Option::<String>::None)?; |
| 193 | + query.try_bind(&string.clone())?; |
| 194 | + query.try_bind(&tuple.0)?; |
| 195 | + |
| 196 | + Ok(query) |
| 197 | + }() |
| 198 | + .map_err(Error::Encode)?; |
| 199 | + |
| 200 | + let result = query.fetch_one(&mut conn).await?; |
| 201 | + |
| 202 | + let column_0: String = result.try_get(0)?; |
| 203 | + |
| 204 | + assert_eq!(column_0, string); |
| 205 | + } |
| 206 | + |
| 207 | + Ok(()) |
| 208 | +} |
0 commit comments