This commit is contained in:
parent
8e9efaca63
commit
1e819b3a24
68
src/main.rs
68
src/main.rs
@ -6,6 +6,44 @@ struct InputBuffer {
|
||||
buffer: Option<String>,
|
||||
}
|
||||
|
||||
enum MetaCommandResult {
|
||||
Success,
|
||||
Unrecognized,
|
||||
}
|
||||
|
||||
enum PrepareResult {
|
||||
Success,
|
||||
Unrecognized,
|
||||
}
|
||||
|
||||
enum StatementType {
|
||||
Insert,
|
||||
Select,
|
||||
}
|
||||
|
||||
struct Statement {
|
||||
statement_type: StatementType,
|
||||
}
|
||||
|
||||
impl Statement {
|
||||
fn new(statement_type: StatementType) -> Self {
|
||||
Self {
|
||||
statement_type
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn execute(&self) {
|
||||
match self.statement_type {
|
||||
StatementType::Insert => {
|
||||
println!("This is where you do an insert")
|
||||
}
|
||||
StatementType::Select => {
|
||||
println!("This is where you do an select")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl InputBuffer {
|
||||
pub fn new() -> Self {
|
||||
return Self {
|
||||
@ -38,7 +76,32 @@ impl InputBuffer {
|
||||
pub(crate) fn parse(&self) {
|
||||
match &self.buffer {
|
||||
Some(command) => {
|
||||
match command.as_str().replace("\n", "").trim() {
|
||||
if command.starts_with(".") {
|
||||
Self::handle_meta_statement(command);
|
||||
} else {
|
||||
if let Ok(statement) = Self::prepare_statement(command) {
|
||||
statement.execute()
|
||||
} else {
|
||||
println!("could not recognize statement");
|
||||
}
|
||||
}
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn prepare_statement(command: &String) -> Result<Statement, String> {
|
||||
if command.starts_with("insert") {
|
||||
Ok(Statement::new(StatementType::Insert))
|
||||
} else if command.starts_with("select") {
|
||||
Ok(Statement::new(StatementType::Select))
|
||||
} else {
|
||||
Err(String::from("Unrecognized statement"))
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_meta_statement(command: &String) {
|
||||
match command.replace("\n", "").trim() {
|
||||
".exit" => {
|
||||
exit(0);
|
||||
}
|
||||
@ -47,9 +110,6 @@ impl InputBuffer {
|
||||
}
|
||||
}
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user