some fixes
This commit is contained in:
		
							parent
							
								
									77b7eb90e7
								
							
						
					
					
						commit
						91193cf03e
					
				
					 3 changed files with 29 additions and 46 deletions
				
			
		
							
								
								
									
										14
									
								
								squirrel-server/Cargo.lock
									
										
									
										generated
									
									
									
								
							
							
						
						
									
										14
									
								
								squirrel-server/Cargo.lock
									
										
									
										generated
									
									
									
								
							| 
						 | 
					@ -2,15 +2,15 @@
 | 
				
			||||||
# It is not intended for manual editing.
 | 
					# It is not intended for manual editing.
 | 
				
			||||||
version = 3
 | 
					version = 3
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[[package]]
 | 
					 | 
				
			||||||
name = "SQUIRREL"
 | 
					 | 
				
			||||||
version = "0.1.0"
 | 
					 | 
				
			||||||
dependencies = [
 | 
					 | 
				
			||||||
 "anyhow",
 | 
					 | 
				
			||||||
]
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
[[package]]
 | 
					[[package]]
 | 
				
			||||||
name = "anyhow"
 | 
					name = "anyhow"
 | 
				
			||||||
version = "1.0.72"
 | 
					version = "1.0.72"
 | 
				
			||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
					source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
				
			||||||
checksum = "3b13c32d80ecc7ab747b80c3784bce54ee8a7a0cc4fbda9bf4cda2cf6fe90854"
 | 
					checksum = "3b13c32d80ecc7ab747b80c3784bce54ee8a7a0cc4fbda9bf4cda2cf6fe90854"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[[package]]
 | 
				
			||||||
 | 
					name = "squirrel-server"
 | 
				
			||||||
 | 
					version = "0.1.0"
 | 
				
			||||||
 | 
					dependencies = [
 | 
				
			||||||
 | 
					 "anyhow",
 | 
				
			||||||
 | 
					]
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,3 +1,4 @@
 | 
				
			||||||
 | 
					use anyhow::anyhow;
 | 
				
			||||||
use std::fs;
 | 
					use std::fs;
 | 
				
			||||||
use std::io::{BufRead, BufReader, Read, Write};
 | 
					use std::io::{BufRead, BufReader, Read, Write};
 | 
				
			||||||
use std::net::{Shutdown, TcpListener, TcpStream};
 | 
					use std::net::{Shutdown, TcpListener, TcpStream};
 | 
				
			||||||
| 
						 | 
					@ -29,7 +30,7 @@ fn handle_create(command: CreateCommand) -> ::anyhow::Result<TableDefinition> {
 | 
				
			||||||
        file.write_all(line.as_bytes()).unwrap();
 | 
					        file.write_all(line.as_bytes()).unwrap();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return Ok(command.table_definition);
 | 
					    Ok(command.table_definition)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn read_tabledef(table_name: String) -> ::anyhow::Result<TableDefinition> {
 | 
					fn read_tabledef(table_name: String) -> ::anyhow::Result<TableDefinition> {
 | 
				
			||||||
| 
						 | 
					@ -39,7 +40,7 @@ fn read_tabledef(table_name: String) -> ::anyhow::Result<TableDefinition> {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for line in BufReader::new(file).lines() {
 | 
					    for line in BufReader::new(file).lines() {
 | 
				
			||||||
        let line_str = line?;
 | 
					        let line_str = line?;
 | 
				
			||||||
        let parts: Vec<&str> = line_str.split(" ").collect();
 | 
					        let parts: Vec<&str> = line_str.split(' ').collect();
 | 
				
			||||||
        let col_def = ColumnDefinition {
 | 
					        let col_def = ColumnDefinition {
 | 
				
			||||||
            name: parts[0].to_string(),
 | 
					            name: parts[0].to_string(),
 | 
				
			||||||
            data_type: Datatype::from_str(parts[1]).unwrap(),
 | 
					            data_type: Datatype::from_str(parts[1]).unwrap(),
 | 
				
			||||||
| 
						 | 
					@ -48,10 +49,10 @@ fn read_tabledef(table_name: String) -> ::anyhow::Result<TableDefinition> {
 | 
				
			||||||
        column_defs.push(col_def);
 | 
					        column_defs.push(col_def);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return Ok(TableDefinition {
 | 
					    Ok(TableDefinition {
 | 
				
			||||||
        name: table_name,
 | 
					        name: table_name,
 | 
				
			||||||
        column_defs,
 | 
					        column_defs,
 | 
				
			||||||
    });
 | 
					    })
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn handle_insert(command: InsertCommand) -> ::anyhow::Result<()> {
 | 
					fn handle_insert(command: InsertCommand) -> ::anyhow::Result<()> {
 | 
				
			||||||
| 
						 | 
					@ -83,7 +84,7 @@ fn handle_insert(command: InsertCommand) -> ::anyhow::Result<()> {
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return Ok(());
 | 
					    Ok(())
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn handle_select(command: SelectCommand) -> ::anyhow::Result<String> {
 | 
					fn handle_select(command: SelectCommand) -> ::anyhow::Result<String> {
 | 
				
			||||||
| 
						 | 
					@ -114,47 +115,29 @@ fn handle_select(command: SelectCommand) -> ::anyhow::Result<String> {
 | 
				
			||||||
        response += "\n";
 | 
					        response += "\n";
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return Ok(response);
 | 
					    Ok(response)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn run_command(query: String) -> String {
 | 
					fn run_command(query: String) -> ::anyhow::Result<String> {
 | 
				
			||||||
    let response: String;
 | 
					 | 
				
			||||||
    if query.starts_with('\\') {
 | 
					    if query.starts_with('\\') {
 | 
				
			||||||
        // handle PSQL's slash commands e.g.: \dt \d
 | 
					        // handle PSQL's slash commands e.g.: \dt \d
 | 
				
			||||||
        return String::from("Slash commands are not yet supported in SQUIRREL");
 | 
					        return Err(anyhow!("Slash commands are not yet supported in SQUIRREL"));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let command_result: ::anyhow::Result<Command> = Command::from_string(query);
 | 
					    let command: Command = Command::from_string(query)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if command_result.is_ok() {
 | 
					    match command {
 | 
				
			||||||
        let command: Command = command_result.unwrap();
 | 
					        Command::Create(create_command) => {
 | 
				
			||||||
        response = match command {
 | 
					            handle_create(create_command)?;
 | 
				
			||||||
            Command::Create(create_command) => {
 | 
					            Ok(String::from("Table Created"))
 | 
				
			||||||
                let result_result = handle_create(create_command);
 | 
					 | 
				
			||||||
                if result_result.is_err() {
 | 
					 | 
				
			||||||
                    String::from("Error creating table.")
 | 
					 | 
				
			||||||
                } else {
 | 
					 | 
				
			||||||
                    String::from("Table created.")
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            Command::Insert(insert_command) => {
 | 
					 | 
				
			||||||
                let result = handle_insert(insert_command);
 | 
					 | 
				
			||||||
                if result.is_err() {
 | 
					 | 
				
			||||||
                    result.err().unwrap().to_string()
 | 
					 | 
				
			||||||
                } else {
 | 
					 | 
				
			||||||
                    String::from("Data inserted.")
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            Command::Select(select_command) => {
 | 
					 | 
				
			||||||
                return handle_select(select_command).unwrap();
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            _ => String::from("Invalid command"),
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    } else {
 | 
					        Command::Insert(insert_command) => {
 | 
				
			||||||
        response = command_result.err().unwrap().to_string();
 | 
					            handle_insert(insert_command)?;
 | 
				
			||||||
 | 
					            Ok(String::from("Row Inserted"))
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        Command::Select(select_command) => handle_select(select_command),
 | 
				
			||||||
 | 
					        _ => Err(anyhow!("Invalid command")),
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					 | 
				
			||||||
    return response;
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn handle_client(mut stream: TcpStream) {
 | 
					fn handle_client(mut stream: TcpStream) {
 | 
				
			||||||
| 
						 | 
					@ -163,7 +146,7 @@ fn handle_client(mut stream: TcpStream) {
 | 
				
			||||||
    while match stream.read(&mut data) {
 | 
					    while match stream.read(&mut data) {
 | 
				
			||||||
        Ok(_size) => {
 | 
					        Ok(_size) => {
 | 
				
			||||||
            let query_string = String::from_utf8(data.to_vec()).expect("A UTF-8 string");
 | 
					            let query_string = String::from_utf8(data.to_vec()).expect("A UTF-8 string");
 | 
				
			||||||
            let response: String = run_command(query_string);
 | 
					            let response: String = run_command(query_string).unwrap();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            let response_data_size = response.len().to_le_bytes();
 | 
					            let response_data_size = response.len().to_le_bytes();
 | 
				
			||||||
            stream.write_all(&response_data_size).unwrap(); // send length of message
 | 
					            stream.write_all(&response_data_size).unwrap(); // send length of message
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -26,6 +26,6 @@ impl TableDefinition {
 | 
				
			||||||
                1
 | 
					                1
 | 
				
			||||||
            };
 | 
					            };
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        return sum;
 | 
					        sum
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue