Execute a SQL statement, returning every row as a `HashMap` keyed by the column name reported in the row description. Useful for tests that need to assert on specific projected columns regardless of projection order. NULL columns are stored as the empty string.
(
&self,
sql: &str,
)
| 58 | /// that need to assert on specific projected columns regardless of |
| 59 | /// projection order. NULL columns are stored as the empty string. |
| 60 | pub async fn query_named_rows( |
| 61 | &self, |
| 62 | sql: &str, |
| 63 | ) -> Result<Vec<std::collections::HashMap<String, String>>, String> { |
| 64 | let client = self.client.as_ref(); |
| 65 | match client.simple_query(sql).await { |
| 66 | Ok(msgs) => { |
| 67 | let mut rows: Vec<std::collections::HashMap<String, String>> = Vec::new(); |
| 68 | for msg in msgs { |
| 69 | if let tokio_postgres::SimpleQueryMessage::Row(row) = msg { |
| 70 | // `SimpleColumn` is not `Clone`; collect names by |
| 71 | // borrowing the column slice directly. |
| 72 | let names: Vec<String> = |
| 73 | row.columns().iter().map(|c| c.name().to_string()).collect(); |
| 74 | let mut map = std::collections::HashMap::with_capacity(names.len()); |
| 75 | for (i, name) in names.into_iter().enumerate() { |
| 76 | map.insert(name, row.get(i).unwrap_or("").to_string()); |
| 77 | } |
| 78 | rows.push(map); |
| 79 | } |
| 80 | } |
| 81 | Ok(rows) |
| 82 | } |
| 83 | Err(e) => Err(pg_error_detail(&e)), |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /// Execute a SQL statement, returning every row as a Vec of its column |
| 88 | /// values (in projection order). Column count is taken from the first |