sqlite_fsr/command/sql/
select.rs

1
2use crate::command::sql::parser::sql_statement::{SelectStatement, AggregatorFunction};
3use crate::models::dbfile::dbtable::record::Record;
4use crate::models::dbfile::table::DBTable;
5
6pub fn select(table: &mut DBTable, mut statement: SelectStatement) -> Vec<Record> {
7    // take aggregator out of the statement so we can pass the (now non-aggregating) statement
8    let aggregator = statement.aggregator_function.take();
9
10    let table_rows = table.to_table_rows(statement);
11
12    if let Some(aggregator_function) = aggregator {
13        return aggregate_table_rows(table_rows, aggregator_function);
14    }
15
16    table_rows
17}
18
19
20pub fn aggregate_table_rows(table_rows: Vec<Record>, aggregator_function: AggregatorFunction) -> Vec<Record>{
21    let mut aggregated_rows: Vec<Record> = Vec::new();
22    match aggregator_function {
23        AggregatorFunction::COUNT => {
24            let aggegated_row_count = table_rows.len();
25            let row_id = 1;
26            let column_headers = vec![aggegated_row_count.to_be_bytes().len() as i64];
27            let column_values = vec![aggegated_row_count.to_string().as_bytes().to_vec()];
28            let record = Record { row_id, column_headers, column_values };
29            aggregated_rows.push(record);
30        }
31        _ => panic!()
32    }
33    return aggregated_rows;
34}