Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Commit 830ca48

Browse files
kelvichJakub Wieczorek
authored and
Jakub Wieczorek
committed
Allow passing null params in query_raw_txt()
Previous coding only allowed passing vector of text values as params, but that does not allow to distinguish between nulls and 4-byte strings with "null" written in them. Change query_raw_txt params argument to accept Vec<Option<String>> instead.
1 parent f4aa454 commit 830ca48

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

tokio-postgres/src/client.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ impl Client {
375375
pub async fn query_raw_txt<'a, S, I>(&self, query: S, params: I) -> Result<RowStream, Error>
376376
where
377377
S: AsRef<str>,
378-
I: IntoIterator<Item = S>,
378+
I: IntoIterator<Item = Option<S>>,
379379
I::IntoIter: ExactSizeIterator,
380380
{
381381
let params = params.into_iter();
@@ -390,9 +390,12 @@ impl Client {
390390
"", // empty string selects the unnamed prepared statement
391391
std::iter::empty(), // all parameters use the default format (text)
392392
params,
393-
|param, buf| {
394-
buf.put_slice(param.as_ref().as_bytes());
395-
Ok(postgres_protocol::IsNull::No)
393+
|param, buf| match param {
394+
Some(param) => {
395+
buf.put_slice(param.as_ref().as_bytes());
396+
Ok(postgres_protocol::IsNull::No)
397+
}
398+
None => Ok(postgres_protocol::IsNull::Yes),
396399
},
397400
Some(0), // all text
398401
buf,

tokio-postgres/tests/test/main.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ async fn query_raw_txt() {
254254
let client = connect("user=postgres").await;
255255

256256
let rows: Vec<tokio_postgres::Row> = client
257-
.query_raw_txt("SELECT 55 * $1", ["42"])
257+
.query_raw_txt("SELECT 55 * $1", [Some("42")])
258258
.await
259259
.unwrap()
260260
.try_collect()
@@ -266,7 +266,7 @@ async fn query_raw_txt() {
266266
assert_eq!(res, 55 * 42);
267267

268268
let rows: Vec<tokio_postgres::Row> = client
269-
.query_raw_txt("SELECT $1", ["42"])
269+
.query_raw_txt("SELECT $1", [Some("42")])
270270
.await
271271
.unwrap()
272272
.try_collect()
@@ -278,6 +278,36 @@ async fn query_raw_txt() {
278278
assert!(rows[0].body_len() > 0);
279279
}
280280

281+
#[tokio::test]
282+
async fn query_raw_txt_nulls() {
283+
let client = connect("user=postgres").await;
284+
285+
let rows: Vec<tokio_postgres::Row> = client
286+
.query_raw_txt(
287+
"SELECT $1 as str, $2 as n, 'null' as str2, null as n2",
288+
[Some("null"), None],
289+
)
290+
.await
291+
.unwrap()
292+
.try_collect()
293+
.await
294+
.unwrap();
295+
296+
assert_eq!(rows.len(), 1);
297+
298+
let res = rows[0].as_text(0).unwrap();
299+
assert_eq!(res, Some("null"));
300+
301+
let res = rows[0].as_text(1).unwrap();
302+
assert_eq!(res, None);
303+
304+
let res = rows[0].as_text(2).unwrap();
305+
assert_eq!(res, Some("null"));
306+
307+
let res = rows[0].as_text(3).unwrap();
308+
assert_eq!(res, None);
309+
}
310+
281311
#[tokio::test]
282312
async fn limit_max_backend_message_size() {
283313
let client = connect("user=postgres max_backend_message_size=10000").await;

0 commit comments

Comments
 (0)