Execute a raw SQL string and return rows as `Vec >`.
(
&self,
sql: &str,
params: &[&(dyn tokio_postgres::types::ToSql + Sync)],
)
| 62 | |
| 63 | /// Execute a raw SQL string and return rows as `Vec<Vec<Value>>`. |
| 64 | pub(super) async fn query_raw( |
| 65 | &self, |
| 66 | sql: &str, |
| 67 | params: &[&(dyn tokio_postgres::types::ToSql + Sync)], |
| 68 | ) -> NodeDbResult<(Vec<String>, Vec<Vec<Value>>)> { |
| 69 | let client = self.client.lock().await; |
| 70 | let rows = client.query(sql, params).await.map_err(|e| { |
| 71 | NodeDbError::storage(format!("pgwire query failed: {}", pg_error_detail(&e))) |
| 72 | })?; |
| 73 | |
| 74 | if rows.is_empty() { |
| 75 | return Ok((Vec::new(), Vec::new())); |
| 76 | } |
| 77 | |
| 78 | let columns: Vec<String> = rows[0] |
| 79 | .columns() |
| 80 | .iter() |
| 81 | .map(|c| c.name().to_string()) |
| 82 | .collect(); |
| 83 | |
| 84 | let mut result_rows = Vec::with_capacity(rows.len()); |
| 85 | for row in &rows { |
| 86 | let mut vals = Vec::with_capacity(columns.len()); |
| 87 | for (i, col) in row.columns().iter().enumerate() { |
| 88 | let val = pg_value_to_value(row, i, col.type_()); |
| 89 | vals.push(val); |
| 90 | } |
| 91 | result_rows.push(vals); |
| 92 | } |
| 93 | |
| 94 | Ok((columns, result_rows)) |
| 95 | } |
| 96 | |
| 97 | /// Execute a statement that doesn't return rows (INSERT/UPDATE/DELETE). |
| 98 | pub(super) async fn execute_raw( |
no test coverage detected