use super::{ColumnType, ResultRow, SQLResult, SQLResultKind};
use crate::SQLError;
use uqa_core::{
memory::{MemoryReservation, Produced, ProductionControl, ProductionVec},
Value,
};
struct Rows<'a> {
output: ProductionVec<'a, ResultRow>,
remaining: std::vec::IntoIter<Value>,
cells: Option<MemoryReservation>,
}
impl SQLResult {
pub fn from_column_with_control(
name: &str,
ty: &ColumnType,
cells: Produced<Vec<Value>>,
control: &ProductionControl<'_>,
) -> Result<Produced<Self>, SQLError> {
let mut columns = ProductionVec::new(*control);
columns.push_produced(control.copy_text(name)?)?;
let mut column_types = ProductionVec::new(*control);
let (ty, memory) = ty.clone_with_control(control)?.into_parts();
column_types.push_produced(control.finish(Some(ty), memory)?)?;
let (cells, cell_memory) = cells.into_parts();
let mut rows = Rows {
output: ProductionVec::new(*control),
remaining: cells.into_iter(),
cells: cell_memory,
};
for cell in rows.remaining.by_ref() {
let entry = control.reserve(size_of::<(String, Value)>())?;
let (key, key_memory) = control.copy_text(name)?.into_parts();
let row = ResultRow::from([(key, cell)]);
rows.output
.push_produced(control.finish(row, control.combine(entry, key_memory))?)?;
}
let columns = columns.finish()?;
let column_types = column_types.finish()?;
let output = rows.output.finish()?;
drop(rows.remaining);
let (columns, column_memory) = columns.into_parts();
let (column_types, type_memory) = column_types.into_parts();
let (output, row_memory) = output.into_parts();
let memory = control.combine(
control.combine(rows.cells, row_memory),
control.combine(column_memory, type_memory),
);
Ok(control.finish(
Self {
kind: SQLResultKind::Rows,
command_tag: None,
columns,
column_types,
row_schema: None,
rows: output,
positional_rows: None,
affected_rows: 0,
},
memory,
)?)
}
}
#[cfg(test)]
mod tests;