Merge branch 'clippy-formatting' into 'main'
Clippy formatting See merge request x433485/minisql!21
This commit is contained in:
commit
4d45da0cd1
13 changed files with 78 additions and 54 deletions
|
|
@ -7,6 +7,13 @@ pub struct ColumnIndex {
|
|||
index: BTreeMap<IndexableValue, HashSet<Uuid>>,
|
||||
}
|
||||
|
||||
// To satisfy clippy.
|
||||
impl Default for ColumnIndex {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl ColumnIndex {
|
||||
pub fn new() -> Self {
|
||||
let index = BTreeMap::new();
|
||||
|
|
|
|||
|
|
@ -39,6 +39,13 @@ impl FromIterator<Value> for Row {
|
|||
}
|
||||
}
|
||||
|
||||
// To satisfy clippy.
|
||||
impl Default for Row {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Row {
|
||||
pub fn new() -> Self {
|
||||
Self(vec![])
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ impl Table {
|
|||
}
|
||||
|
||||
pub fn table_name(&self) -> &TableName {
|
||||
&self.schema.table_name()
|
||||
self.schema.table_name()
|
||||
}
|
||||
|
||||
// ======Selection======
|
||||
|
|
@ -69,18 +69,18 @@ impl Table {
|
|||
.collect()
|
||||
}
|
||||
|
||||
pub fn select_all_rows<'a>(&'a self, selected_columns: Vec<Column>) -> impl Iterator<Item=RestrictedRow> + 'a {
|
||||
pub fn select_all_rows(&self, selected_columns: Vec<Column>) -> impl Iterator<Item=RestrictedRow> + '_ {
|
||||
self.rows
|
||||
.values()
|
||||
.map(move |row| row.restrict_columns(&selected_columns))
|
||||
}
|
||||
|
||||
pub fn select_rows_where_eq<'a>(
|
||||
&'a self,
|
||||
pub fn select_rows_where_eq(
|
||||
&self,
|
||||
selected_columns: Vec<Column>,
|
||||
column: Column,
|
||||
value: Value,
|
||||
) -> DbResult<impl Iterator<Item=RestrictedRow> + 'a> {
|
||||
) -> DbResult<impl Iterator<Item=RestrictedRow> + '_> {
|
||||
let restrict_columns_of_row = move |row: Row| row.restrict_columns(&selected_columns);
|
||||
match value {
|
||||
Value::Indexable(value) => match self.fetch_ids_from_index(column, &value)? {
|
||||
|
|
@ -116,10 +116,7 @@ impl Table {
|
|||
}
|
||||
|
||||
for (column, column_index) in &mut self.indexes {
|
||||
match &row[*column] {
|
||||
Value::Indexable(val) => column_index.add(val.clone(), id),
|
||||
_ => {},
|
||||
}
|
||||
if let Value::Indexable(val) = &row[*column] { column_index.add(val.clone(), id) }
|
||||
}
|
||||
|
||||
let _ = self.rows.insert(id, row);
|
||||
|
|
|
|||
|
|
@ -47,6 +47,13 @@ impl std::fmt::Debug for Response<'_> {
|
|||
}
|
||||
}
|
||||
|
||||
// To satisfy clippy.
|
||||
impl Default for State {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
|
|
@ -55,7 +62,7 @@ impl State {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn db_schema<'a>(&'a self) -> DbSchema {
|
||||
pub fn db_schema(&self) -> DbSchema {
|
||||
let mut schema: DbSchema = Vec::new();
|
||||
for (table_name, &table_position) in &self.table_name_position_mapping {
|
||||
let table_schema = self.tables[table_position].schema();
|
||||
|
|
@ -64,11 +71,11 @@ impl State {
|
|||
schema
|
||||
}
|
||||
|
||||
fn table_at<'a>(&'a self, table_position: TablePosition) -> &'a Table {
|
||||
fn table_at(&self, table_position: TablePosition) -> &Table {
|
||||
&self.tables[table_position]
|
||||
}
|
||||
|
||||
fn table_at_mut<'a>(&'a mut self, table_position: TablePosition) -> &'a mut Table {
|
||||
fn table_at_mut(&mut self, table_position: TablePosition) -> &mut Table {
|
||||
&mut self.tables[table_position]
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -28,6 +28,10 @@ impl RestrictedRow {
|
|||
self.0.len()
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.0.is_empty()
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> impl Iterator<Item=&(Column, Value)> {
|
||||
self.0.iter()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use minisql::{operation::Operation, interpreter::DbSchema};
|
||||
use crate::syntax::RawQuerySyntax;
|
||||
use nom::{branch::alt, multi::many0, IResult};
|
||||
use nom::{branch::alt, IResult};
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::{parsing::{create::parse_create, delete::parse_delete, index::parse_create_index, insert::parse_insert, select::parse_select}, validation::{validate_operation, ValidationError}};
|
||||
|
|
@ -13,7 +13,7 @@ pub enum Error {
|
|||
ValidationError(#[from] ValidationError)
|
||||
}
|
||||
|
||||
pub fn parse_statement<'a>(input: &'a str) -> IResult<&str, RawQuerySyntax> {
|
||||
fn parse_statement(input: &str) -> IResult<&str, RawQuerySyntax> {
|
||||
alt((
|
||||
parse_insert,
|
||||
parse_create,
|
||||
|
|
@ -25,10 +25,6 @@ pub fn parse_statement<'a>(input: &'a str) -> IResult<&str, RawQuerySyntax> {
|
|||
))(input)
|
||||
}
|
||||
|
||||
pub fn parse_statements<'a>(input: &'a str) -> IResult<&str, Vec<RawQuerySyntax>> {
|
||||
many0(parse_statement)(input)
|
||||
}
|
||||
|
||||
pub fn parse_and_validate(str_query: String, db_schema: &DbSchema) -> Result<Operation, Error> {
|
||||
let (_, op) = parse_statement(str_query.as_str())
|
||||
.map_err(|err| {
|
||||
|
|
|
|||
|
|
@ -28,9 +28,10 @@ pub fn parse_number(input: &str) -> IResult<&str, Value> {
|
|||
Some((_fsign, fdigits)) => {
|
||||
// Combine integer and fractional parts
|
||||
let combined_parts = format!(
|
||||
"{}{}",
|
||||
format!("{}{}", sign.unwrap_or('+'), digits),
|
||||
format!(".{}", fdigits)
|
||||
"{}{}.{}",
|
||||
sign.unwrap_or('+'),
|
||||
digits,
|
||||
fdigits
|
||||
);
|
||||
// Parse the combined parts as a floating-point number
|
||||
let value = combined_parts.parse::<f64>()
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ pub fn try_parse_column_selection(input: &str) -> IResult<&str, ColumnSelection>
|
|||
});
|
||||
let columns_parser = map(
|
||||
separated_list0(terminated(char(','), multispace0), parse_column_name),
|
||||
|names| ColumnSelection::Columns(names),
|
||||
ColumnSelection::Columns,
|
||||
);
|
||||
alt((all_parser, columns_parser))(input)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -61,7 +61,7 @@ fn validate_table_exists<'a>(db_schema: &DbSchema<'a>, table_name: &'a TableName
|
|||
|
||||
fn validate_create_table(raw_table_schema: RawTableSchema, db_schema: &DbSchema) -> Result<Operation, ValidationError> {
|
||||
let table_name: &TableName = &raw_table_schema.table_name;
|
||||
if let Some(_) = get_table_schema(db_schema, &table_name) {
|
||||
if get_table_schema(db_schema, table_name).is_some() {
|
||||
return Err(ValidationError::TableAlreadyExists(table_name.to_string()));
|
||||
}
|
||||
|
||||
|
|
@ -89,10 +89,11 @@ fn validate_table_schema(raw_table_schema: RawTableSchema) -> Result<TableSchema
|
|||
}
|
||||
|
||||
// Ensure it has exactly one primary key that has correct type.
|
||||
if primary_keys.len() == 0 {
|
||||
return Err(ValidationError::PrimaryKeyMissing(raw_table_schema.table_name.clone()))
|
||||
} else if primary_keys.len() > 1 {
|
||||
return Err(ValidationError::MultiplePrimaryKeysFound(raw_table_schema.table_name.clone()))
|
||||
let number_of_primary_keys = primary_keys.len();
|
||||
if number_of_primary_keys == 0 {
|
||||
Err(ValidationError::PrimaryKeyMissing(raw_table_schema.table_name.clone()))
|
||||
} else if number_of_primary_keys > 1 {
|
||||
Err(ValidationError::MultiplePrimaryKeysFound(raw_table_schema.table_name.clone()))
|
||||
} else {
|
||||
let (primary_column_name, primary_key_type) = primary_keys[0].clone();
|
||||
if primary_key_type == DbType::Uuid {
|
||||
|
|
@ -113,18 +114,18 @@ fn validate_select(table_name: TableName, column_selection: syntax::ColumnSelect
|
|||
syntax::ColumnSelection::Columns(columns) => {
|
||||
let non_existant_columns: Vec<ColumnName> =
|
||||
columns.iter().filter_map(|column|
|
||||
if schema.does_column_exist(&column) {
|
||||
if schema.does_column_exist(column) {
|
||||
None
|
||||
} else {
|
||||
Some(column.clone())
|
||||
}).collect();
|
||||
if non_existant_columns.len() > 0 {
|
||||
Err(ValidationError::ColumnsDoNotExist(non_existant_columns))
|
||||
} else {
|
||||
if non_existant_columns.is_empty() {
|
||||
let selection: operation::ColumnSelection =
|
||||
columns.iter().filter_map(|column_name| schema.get_column(column_name)).collect();
|
||||
let validated_condition = validate_condition(condition, schema)?;
|
||||
Ok(Operation::Select(table_position, selection, validated_condition))
|
||||
} else {
|
||||
Err(ValidationError::ColumnsDoNotExist(non_existant_columns))
|
||||
}
|
||||
}
|
||||
syntax::ColumnSelection::All => {
|
||||
|
|
@ -149,11 +150,11 @@ fn validate_insert(table_name: TableName, insertion_values: syntax::InsertionVal
|
|||
let columns_in_query: HashSet<&ColumnName> = HashSet::from_iter(columns_in_query_vec);
|
||||
let columns_in_schema: HashSet<&ColumnName> = HashSet::from_iter(schema.get_columns());
|
||||
let non_existant_columns = Vec::from_iter(columns_in_query.difference(&columns_in_schema));
|
||||
if non_existant_columns.len() > 0 {
|
||||
if !non_existant_columns.is_empty() {
|
||||
return Err(ValidationError::ColumnsDoNotExist(non_existant_columns.iter().map(|column_name| column_name.to_string()).collect()));
|
||||
}
|
||||
let missing_required_columns = Vec::from_iter(columns_in_schema.difference(&columns_in_query));
|
||||
if missing_required_columns.len() > 0 {
|
||||
if !missing_required_columns.is_empty() {
|
||||
return Err(ValidationError::RequiredColumnsAreMissing(missing_required_columns.iter().map(|str| str.to_string()).collect()));
|
||||
}
|
||||
|
||||
|
|
@ -194,7 +195,7 @@ fn validate_condition(condition: Option<syntax::Condition>, schema: &TableSchema
|
|||
if expected_type.eq(&value_type) {
|
||||
Ok(Some(operation::Condition::Eq(column, value)))
|
||||
} else {
|
||||
return Err(ValidationError::TypeMismatch { column_name: column_name.to_string(), received_type: value_type, expected_type });
|
||||
Err(ValidationError::TypeMismatch { column_name: column_name.to_string(), received_type: value_type, expected_type })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -228,7 +229,7 @@ where T: Eq + std::hash::Hash
|
|||
if already_seen_elements.contains(t) {
|
||||
return Some(t);
|
||||
} else {
|
||||
already_seen_elements.insert(&t);
|
||||
already_seen_elements.insert(t);
|
||||
}
|
||||
}
|
||||
None
|
||||
|
|
|
|||
|
|
@ -24,15 +24,15 @@ where
|
|||
R: AsyncBufRead + Unpin + Send,
|
||||
{
|
||||
async fn peek_special_message(&mut self) -> Result<Option<SpecialMessage>, ProtoPeekError> {
|
||||
if let Some(cancel) = try_get_cancel_request(&mut self).await? {
|
||||
if let Some(cancel) = try_get_cancel_request(self).await? {
|
||||
return Ok(Some(cancel));
|
||||
}
|
||||
|
||||
if let Some(ssl) = try_get_ssl_request(&mut self).await? {
|
||||
if let Some(ssl) = try_get_ssl_request(self).await? {
|
||||
return Ok(Some(ssl));
|
||||
}
|
||||
|
||||
if let Some(startup) = try_get_startup_message(&mut self).await? {
|
||||
if let Some(startup) = try_get_startup_message(self).await? {
|
||||
return Ok(Some(startup));
|
||||
}
|
||||
|
||||
|
|
@ -43,11 +43,12 @@ where
|
|||
&mut self,
|
||||
msg: &SpecialMessage,
|
||||
) -> Result<(), ProtoConsumeError> {
|
||||
Ok(match msg {
|
||||
match msg {
|
||||
SpecialMessage::CancelRequest(_) => consume_cancel_request(self),
|
||||
SpecialMessage::SSLRequest => consume_ssl_request(self),
|
||||
SpecialMessage::StartupMessage(_) => consume_startup_message(self).await?,
|
||||
})
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -20,12 +20,12 @@ where
|
|||
{
|
||||
async fn write_proto(&mut self, message: T) -> Result<(), ProtoWriteError> {
|
||||
let variant = message.variant();
|
||||
let mut data = message.serialize()?;
|
||||
let data = message.serialize()?;
|
||||
let length = data.len() as i32 + 4;
|
||||
|
||||
self.inner.write_u8(variant).await?;
|
||||
self.inner.write_i32(length).await?;
|
||||
self.inner.write_all(&mut data).await?;
|
||||
self.inner.write_all(&data).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -110,8 +110,10 @@ async fn create_token(tokens: &TokenStore) -> anyhow::Result<(i32, i32, ResetCan
|
|||
let mut tokens = tokens.lock().await;
|
||||
loop {
|
||||
let pid_key = random_pid_key();
|
||||
if !tokens.contains_key(&pid_key) {
|
||||
tokens.insert(pid_key, token.clone());
|
||||
|
||||
use std::collections::hash_map;
|
||||
if let hash_map::Entry::Vacant(token_entry) = tokens.entry(pid_key) {
|
||||
token_entry.insert(token.clone());
|
||||
|
||||
let (pid, key) = pid_key;
|
||||
return Ok((pid, key, token));
|
||||
|
|
@ -190,7 +192,7 @@ async fn handle_query<W>(writer: &mut W, state: &SharedDbState, query: String, t
|
|||
true
|
||||
}
|
||||
Response::Selected(schema, columns, mut rows) => {
|
||||
writer.write_table_header(&schema, &columns).await?;
|
||||
writer.write_table_header(schema, &columns).await?;
|
||||
match rows.next() {
|
||||
Some(row) => {
|
||||
writer.write_table_row(&row).await?;
|
||||
|
|
@ -226,7 +228,7 @@ async fn handle_query<W>(writer: &mut W, state: &SharedDbState, query: String, t
|
|||
|
||||
if need_write {
|
||||
let state = state.read().await;
|
||||
state_to_file(&state, &config.get_file_path()).await?;
|
||||
state_to_file(&state, config.get_file_path()).await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ use async_trait::async_trait;
|
|||
use rand::Rng;
|
||||
use rand_pcg::Pcg64;
|
||||
use rand_seeder::Seeder;
|
||||
use std::fmt;
|
||||
use minisql::operation::ColumnSelection;
|
||||
use minisql::restricted_row::RestrictedRow;
|
||||
use minisql::schema::{Column, TableSchema};
|
||||
|
|
@ -20,14 +21,14 @@ pub enum CompleteStatus {
|
|||
CreateIndex,
|
||||
}
|
||||
|
||||
impl CompleteStatus {
|
||||
fn to_string(&self) -> String {
|
||||
impl fmt::Display for CompleteStatus {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
CompleteStatus::Insert { oid, rows } => format!("INSERT {} {}", oid, rows),
|
||||
CompleteStatus::Delete(rows) => format!("DELETE {}", rows),
|
||||
CompleteStatus::Select(rows) => format!("SELECT {}", rows),
|
||||
CompleteStatus::CreateTable => "CREATE TABLE".to_string(),
|
||||
CompleteStatus::CreateIndex => "CREATE INDEX".to_string(),
|
||||
CompleteStatus::Insert { oid, rows } => write!(f, "INSERT {} {}", oid, rows),
|
||||
CompleteStatus::Delete(rows) => write!(f, "DELETE {}", rows),
|
||||
CompleteStatus::Select(rows) => write!(f, "SELECT {}", rows),
|
||||
CompleteStatus::CreateTable => write!(f, "CREATE TABLE"),
|
||||
CompleteStatus::CreateIndex => write!(f, "CREATE INDEX"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue