Skip to content

Commit

Permalink
Add ParseTree structures
Browse files Browse the repository at this point in the history
  • Loading branch information
PakhomovAlexander committed Sep 23, 2024
1 parent 5edd91c commit b3d8d63
Showing 1 changed file with 205 additions and 0 deletions.
205 changes: 205 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,206 @@
pub mod lexer;

///
/// TREE STRUCTURE
///
/// *Queries*:
/// select col1, col2 from table1;
/// insert into table1 (col1, col2) values (1, 'valStr');
/// insert into table1 (col1) values (1);
#[derive(Debug, PartialEq)]
struct Queries {
queries: Vec<Query>,
}

#[derive(Debug, PartialEq)]
enum Query {
// select ...
Select(SelectQuery),
// insert ...
Insert(InsertQuery),
// delete ...
Delete(DeleteQuery),
// update ...
Update(UpdateQuery),
// create table ...
CreateTable(CreateTableQuery),
// drop table ...
DropTable(DropTableQuery),
// alter table ... AlterTable(AlterTableQuery),
}

/// *SelectQuery*:
/// select <select_statement>
/// from <from_statement>
/// where <where_statement>
/// order by <order_by_statement>
/// group by <group_by_statement>
/// having <having_statement>
/// limit <limit_statement>
#[derive(Debug, PartialEq)]
struct SelectQuery {
select_statement: SelectStatement,
from_statement: FromStatement,
where_statement: Option<WhereStatement>,
order_by_statement: Option<OrderByStatement>,
group_by_statement: Option<GroupByStatement>,
having_statement: Option<HavingStatement>,
limit_statement: Option<LimitStatement>,
}

/// *InsertQuery*:
/// insert into <table_name> (<columns>) values (<values>)
#[derive(Debug, PartialEq)]
struct InsertQuery {
table_name: String,
columns: Vec<String>,
values: Vec<String>,
}

/// *DeleteQuery*:
/// delete from <table_name> where <where_statement>
#[derive(Debug, PartialEq)]
struct DeleteQuery {
table_name: String,
where_statement: Option<WhereStatement>,
}

/// *UpdateQuery*:
/// update <table_name> set <set_statement> where <where_statement>
#[derive(Debug, PartialEq)]
struct UpdateQuery {
table_name: String,
set_statement: SetStatement,
where_statement: Option<WhereStatement>,
}

/// *CreateTableQuery*:
/// create table <table_name> (<columns_definitions>) <constraints>
#[derive(Debug, PartialEq)]
struct CreateTableQuery {
table_name: String,
// TODO: Design this
columns_definitions: Vec<String>,
// TODO: Design this
constraints: Vec<String>,
}

/// *DropTableQuery*:
/// drop table <table_name>
#[derive(Debug, PartialEq)]
struct DropTableQuery {
table_name: String,
}

/// *AlterTableQuery*:
/// alter table <table_name> <action>
#[derive(Debug, PartialEq)]
struct AlterTableQuery {
table_name: String,
// TODO: Design this
action: String,
}

#[derive(Debug, PartialEq)]
struct SelectStatement {
columns: Vec<ColumnStatement>,
distinct: bool,
}

#[derive(Debug, PartialEq)]
struct FromStatement {
tables: Vec<TableStatement>,
joins: Vec<JoinStatement>,
}

#[derive(Debug, PartialEq)]
struct TableStatement {
table_name: String,
alias: Option<String>,
}

#[derive(Debug, PartialEq)]
struct JoinStatement {
table: TableStatement,
// TODO: Design this
join_type: String,
on: Vec<Condition>,
}

#[derive(Debug, PartialEq)]
struct Condition {
left: ColumnStatement,
operator: Operator,
right: ColumnStatement,
}

#[derive(Debug, PartialEq)]
enum Operator {
Equal,
NotEqual,
}

#[derive(Debug, PartialEq)]
struct WhereStatement {
conditions: Vec<Condition>,
}

#[derive(Debug, PartialEq)]
struct OrderByStatement {
columns: Vec<ColumnStatement>,
order: Order,
}

#[derive(Debug, PartialEq)]
enum Order {
Asc,
Desc,
}

#[derive(Debug, PartialEq)]
struct LimitStatement {
limit: i32,
}

#[derive(Debug, PartialEq)]
struct GroupByStatement {
columns: Vec<ColumnStatement>,
}

#[derive(Debug, PartialEq)]
struct HavingStatement {
conditions: Vec<Condition>,
}

#[derive(Debug, PartialEq)]
struct SetStatement {
// TODO: Design this
columns: Vec<String>,
}

#[derive(Debug, PartialEq)]
enum ColumnStatement {
ColumnStatement(ColumnIdentifier),
ColumnStatementLiteral(Literal),
ColumnStatementFunction(Function),
}

#[derive(Debug, PartialEq)]
struct ColumnIdentifier {
table_name: Option<String>,
column_name: String,
}

#[derive(Debug, PartialEq)]
enum Literal {
Integer(i32),
String(String),
Float(f32),
Boolean(bool),
}

#[derive(Debug, PartialEq)]
struct Function {
name: String,
arguments: Vec<ColumnStatement>,
}

0 comments on commit b3d8d63

Please sign in to comment.