Executes a SQL query and returns the results. The query can use any SQL features supported by SQLite, including JOINs, CTEs, window functions, and aggregations.
(&self, query: &str)
| 138 | /// The query can use any SQL features supported by SQLite, including JOINs, |
| 139 | /// CTEs, window functions, and aggregations. |
| 140 | pub fn execute(&self, query: &str) -> Result<QueryResult> { |
| 141 | let mut stmt = self.conn.prepare(query)?; |
| 142 | |
| 143 | let column_names: Vec<String> = stmt.column_names().iter().map(|s| s.to_string()).collect(); |
| 144 | |
| 145 | let rows: Vec<Vec<Value>> = stmt |
| 146 | .query_map([], |row| Ok(row_to_values(row, column_names.len())))? |
| 147 | .filter_map(|r| r.ok()) |
| 148 | .collect(); |
| 149 | |
| 150 | Ok(QueryResult { |
| 151 | columns: column_names, |
| 152 | rows, |
| 153 | }) |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | fn row_to_values(row: &Row, col_count: usize) -> Vec<Value> { |