1use crate::error::FireboltError;
2use crate::types::{Column, ColumnRef, TypeConversion};
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct ResultSet {
7 pub columns: Vec<Column>,
8 pub rows: Vec<Row>,
9}
10
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct Row {
13 data: Vec<serde_json::Value>,
14 columns: Vec<Column>,
15}
16
17impl Row {
18 pub fn new(data: Vec<serde_json::Value>, columns: Vec<Column>) -> Self {
19 Self { data, columns }
20 }
21
22 pub fn get<T>(&self, column_ref: impl Into<ColumnRef>) -> Result<T, FireboltError>
23 where
24 T: TypeConversion,
25 {
26 let column_ref = column_ref.into();
27 let (index, column) = match column_ref {
28 ColumnRef::Index(i) => {
29 let column = self.columns.get(i).ok_or_else(|| {
30 FireboltError::Query(format!("Column index {i} out of bounds"))
31 })?;
32 (i, column)
33 }
34 ColumnRef::Name(name) => {
35 let (index, column) = self
36 .columns
37 .iter()
38 .enumerate()
39 .find(|(_, col)| col.name == name)
40 .ok_or_else(|| FireboltError::Query(format!("Column '{name}' not found")))?;
41 (index, column)
42 }
43 };
44
45 let value = self
46 .data
47 .get(index)
48 .ok_or_else(|| FireboltError::Query(format!("Column index {index} out of bounds")))?;
49
50 T::convert_from_json(value, &column.r#type)
51 }
52}