Query with a cached statement
(
&self,
conn: &Connection,
query: &str,
params: P,
)
| 112 | |
| 113 | /// Query with a cached statement |
| 114 | pub fn query_cached<P: Params>( |
| 115 | &self, |
| 116 | conn: &Connection, |
| 117 | query: &str, |
| 118 | params: P, |
| 119 | ) -> Result<(Vec<String>, crate::session::db_handler::DbRows), rusqlite::Error> { |
| 120 | let (mut stmt, metadata) = self.prepare_and_cache(conn, query)?; |
| 121 | |
| 122 | // Execute query and collect results |
| 123 | let rows = stmt.query_map(params, |row| { |
| 124 | let mut row_data = Vec::new(); |
| 125 | for i in 0..metadata.column_names.len() { |
| 126 | match row.get_ref(i)? { |
| 127 | rusqlite::types::ValueRef::Null => row_data.push(None), |
| 128 | rusqlite::types::ValueRef::Integer(int_val) => { |
| 129 | // Check if this should be a boolean conversion |
| 130 | let is_boolean = metadata.column_types.get(i) |
| 131 | .and_then(|opt| opt.as_ref()) |
| 132 | .map(|pg_type| { |
| 133 | let type_lower = pg_type.to_lowercase(); |
| 134 | type_lower == "boolean" || type_lower == "bool" |
| 135 | }) |
| 136 | .unwrap_or(false); |
| 137 | |
| 138 | if is_boolean { |
| 139 | let bool_str = if int_val == 0 { "f" } else { "t" }; |
| 140 | row_data.push(Some(bool_str.as_bytes().to_vec())); |
| 141 | } else { |
| 142 | row_data.push(Some(int_val.to_string().into_bytes())); |
| 143 | } |
| 144 | }, |
| 145 | rusqlite::types::ValueRef::Real(f) => { |
| 146 | row_data.push(Some(f.to_string().into_bytes())); |
| 147 | }, |
| 148 | rusqlite::types::ValueRef::Text(s) => { |
| 149 | row_data.push(Some(s.to_vec())); |
| 150 | }, |
| 151 | rusqlite::types::ValueRef::Blob(b) => { |
| 152 | row_data.push(Some(b.to_vec())); |
| 153 | }, |
| 154 | } |
| 155 | } |
| 156 | Ok(row_data) |
| 157 | })?; |
| 158 | |
| 159 | let mut result_rows = Vec::new(); |
| 160 | for row in rows { |
| 161 | result_rows.push(row?); |
| 162 | } |
| 163 | |
| 164 | Ok((metadata.column_names.clone(), result_rows)) |
| 165 | } |
| 166 | |
| 167 | /// Get cached metadata for a query |
| 168 | fn get_metadata(&self, query: &str) -> Option<StatementMetadata> { |