fix: empty select returns header

This commit is contained in:
Jindřich Moravec 2024-01-28 20:45:09 +01:00
parent 25bb21c29c
commit 8fc271695a
6 changed files with 99 additions and 65 deletions

View file

@ -1,7 +1,7 @@
use crate::internals::row::ColumnPosition;
use crate::schema::{TableName, TableSchema};
use crate::internals::table::Table;
use crate::operation::{Operation, Condition};
use crate::operation::{Operation, Condition, ColumnSelection};
use crate::result::DbResult;
use bimap::BiMap;
use serde::{Deserialize, Serialize};
@ -20,7 +20,7 @@ pub struct State {
// #[derive(Debug)]
pub enum Response<'a> {
Selected(&'a TableSchema, Box<dyn Iterator<Item=RestrictedRow> + 'a + Send>),
Selected(&'a TableSchema, ColumnSelection, Box<dyn Iterator<Item=RestrictedRow> + 'a + Send>),
Inserted,
Deleted(usize), // how many were deleted
TableCreated,
@ -33,7 +33,7 @@ impl std::fmt::Debug for Response<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
use Response::*;
match self {
Selected(_schema, _rows) =>
Selected(_schema, _columns, _rows) =>
// TODO: How can we iterate through the rows without having to take ownership of
// them?
f.write_str("Some rows... trust me"),
@ -91,14 +91,14 @@ impl State {
let selected_rows = match maybe_condition {
None => {
let rows = table.select_all_rows(column_selection);
let rows = table.select_all_rows(column_selection.clone());
Box::new(rows) as Box<dyn Iterator<Item=RestrictedRow> + 'a + Send>
},
Some(Condition::Eq(eq_column, value)) => {
let x =
table.select_rows_where_eq(
column_selection,
column_selection.clone(),
eq_column,
value,
)?;
@ -106,7 +106,7 @@ impl State {
}
};
Ok(Response::Selected(table.schema(), selected_rows))
Ok(Response::Selected(table.schema(), column_selection, selected_rows))
},
Insert(table_position, values) => {
let table: &mut Table = self.table_at_mut(table_position);
@ -192,8 +192,8 @@ mod tests {
let response: Response = state
.interpret(Operation::Select(users_position, users_schema.all_selection(), None))
.unwrap();
assert!(matches!(response, Response::Selected(_, _)));
let Response::Selected(_schema, rows) = response else {
assert!(matches!(response, Response::Selected(_, _, _)));
let Response::Selected(_, _, rows) = response else {
panic!()
};
let rows: Vec<_> = rows.collect();
@ -234,8 +234,8 @@ mod tests {
.interpret(Operation::Select(users, users_schema.all_selection(), None))
.unwrap();
assert!(matches!(response, Response::Selected(_, _)));
let Response::Selected(_schema, rows) = response else {
assert!(matches!(response, Response::Selected(_, _, _)));
let Response::Selected(_, _, rows) = response else {
panic!()
};
let rows: Vec<_> = rows.collect();
@ -301,8 +301,8 @@ mod tests {
{
let response: Response = state.interpret(Select(users_position, users_schema.all_selection(), None)).unwrap();
assert!(matches!(response, Response::Selected(_, _)));
let Response::Selected(_, rows) = response else {
assert!(matches!(response, Response::Selected(_, _, _)));
let Response::Selected(_, _, rows) = response else {
panic!()
};
@ -330,8 +330,8 @@ mod tests {
Some(Eq(id_column, id0.clone())),
))
.unwrap();
assert!(matches!(response, Response::Selected(_, _)));
let Response::Selected(_, rows) = response else {
assert!(matches!(response, Response::Selected(_, _, _)));
let Response::Selected(_, _, rows) = response else {
panic!()
};
let rows: Vec<_> = rows.collect();
@ -352,8 +352,8 @@ mod tests {
Some(Eq(id_column, id0.clone())),
))
.unwrap();
assert!(matches!(response, Response::Selected(_, _)));
let Response::Selected(_, rows) = response else {
assert!(matches!(response, Response::Selected(_, _, _)));
let Response::Selected(_, _, rows) = response else {
panic!()
};
let rows: Vec<_> = rows.collect();
@ -427,8 +427,8 @@ mod tests {
let response: Response = state.interpret(Select(users_position, users_schema.all_selection(), None)).unwrap();
assert!(matches!(response, Response::Selected(_, _)));
let Response::Selected(_, rows) = response else {
assert!(matches!(response, Response::Selected(_, _, _)));
let Response::Selected(_, _, rows) = response else {
panic!()
};
let rows: Vec<_> = rows.collect();

View file

@ -41,6 +41,23 @@ impl DbType {
}
}
pub fn type_oid(&self) -> i32 {
match self {
Self::String => 25,
Self::Int => 23,
Self::Number => 701,
Self::Uuid => 2950,
}
}
pub fn type_size(&self) -> i16 {
match self {
Self::String => -2, // null terminated string
Self::Int => 8,
Self::Number => 8,
Self::Uuid => 16,
}
}
}
impl Value {
@ -54,29 +71,6 @@ impl Value {
},
}
}
pub fn type_oid(&self) -> i32 {
match self {
Self::Number(_) => 701,
Self::Indexable(val) => match val {
IndexableValue::String(_) => 25,
IndexableValue::Int(_) => 23,
IndexableValue::Uuid(_) => 2950,
},
}
}
pub fn type_size(&self) -> i16 {
match self {
Self::Number(_) => 8,
Self::Indexable(val) => match val {
IndexableValue::String(_) => -2, // null terminated string
IndexableValue::Int(_) => 8,
IndexableValue::Uuid(_) => 16,
},
}
}
pub fn as_text_bytes(&self) -> Vec<u8> {
match self {
Self::Number(n) => format!("{n}").into_bytes(),
@ -123,8 +117,9 @@ mod tests {
#[test]
fn test_encode_number() {
let value = Value::Number(123.456);
let oid = value.type_oid();
let size = value.type_size();
let vtype = value.to_type();
let oid = vtype.type_oid();
let size = vtype.type_size();
let bytes = value.as_text_bytes();
let from_bytes = Value::from_text_bytes(&bytes, oid, size).unwrap();
@ -138,8 +133,9 @@ mod tests {
#[test]
fn test_encode_string() {
let value = Value::Indexable(IndexableValue::String("hello".to_string()));
let oid = value.type_oid();
let size = value.type_size();
let vtype = value.to_type();
let oid = vtype.type_oid();
let size = vtype.type_size();
let bytes = value.as_text_bytes();
let from_bytes = Value::from_text_bytes(&bytes, oid, size).unwrap();
@ -153,8 +149,9 @@ mod tests {
#[test]
fn test_encode_string_utf8() {
let value = Value::Indexable(IndexableValue::String("#速度与激情9 早上好中国 现在我有冰激淋 我很喜欢冰激淋 但是《速度与激情9》比冰激淋 🍧🍦🍨".to_string()));
let oid = value.type_oid();
let size = value.type_size();
let vtype = value.to_type();
let oid = vtype.type_oid();
let size = vtype.type_size();
let bytes = value.as_text_bytes();
let from_bytes = Value::from_text_bytes(&bytes, oid, size).unwrap();
@ -168,8 +165,9 @@ mod tests {
#[test]
fn test_encode_int() {
let value = Value::Indexable(IndexableValue::Int(123));
let oid = value.type_oid();
let size = value.type_size();
let vtype = value.to_type();
let oid = vtype.type_oid();
let size = vtype.type_size();
let bytes = value.as_text_bytes();
let from_bytes = Value::from_text_bytes(&bytes, oid, size).unwrap();
@ -183,8 +181,9 @@ mod tests {
#[test]
fn test_encode_uuid() {
let value = Value::Indexable(IndexableValue::Uuid(123));
let oid = value.type_oid();
let size = value.type_size();
let vtype = value.to_type();
let oid = vtype.type_oid();
let size = vtype.type_size();
let bytes = value.as_text_bytes();
let from_bytes = Value::from_text_bytes(&bytes, oid, size).unwrap();
@ -198,7 +197,8 @@ mod tests {
#[test]
fn test_mismatched_size() {
let value = Value::Indexable(IndexableValue::Uuid(123));
let oid = value.type_oid();
let vtype = value.to_type();
let oid = vtype.type_oid();
let size = 8;
let bytes = value.as_text_bytes();