minor file updates

This commit is contained in:
Nicholas Orlowsky 2023-08-08 14:55:46 -05:00 committed by Nicholas Orlowsky
parent 7000cdab31
commit dc89387128
6 changed files with 29 additions and 26 deletions

BIN
.DS_Store vendored

Binary file not shown.

View file

@ -8,12 +8,18 @@ This is a SQL database written in Rust. It will be based off of (and hopefully b
## Feature roadmap ## Feature roadmap
[ ] Table creation via CREATE with varchar & integer datatype [X] CREATE TABLE with varchar & integer datatypes
[ ] SELECT * query [X] INSERT INTO (non-batched)
[X] SELECT * query
[ ] DELETE command
[ ] SELECT (filtered columns) query [ ] SELECT (filtered columns) query
[ ] WHERE clause for SELECT and DELETE
[ ] Primary Keys via B+ Tree [ ] Primary Keys via B+ Tree
[ ] Foreign Keys [ ] Foreign Keys
@ -22,4 +28,4 @@ This is a SQL database written in Rust. It will be based off of (and hopefully b
[ ] Support [Postgres' messaging system](https://www.postgresql.org/docs/current/protocol-flow.html#id-1.10.6.7.3) (wire compatability) [ ] Support [Postgres' messaging system](https://www.postgresql.org/docs/current/protocol-flow.html#id-1.10.6.7.3) (wire compatability)
... other stuff is TBD ... other stuff is TBD

View file

@ -1,4 +1,3 @@
use std::collections::{HashMap, HashSet};
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};
@ -12,8 +11,6 @@ use parser::command::{CreateCommand, InsertCommand, SelectCommand};
pub use table::datatypes::Datatype; pub use table::datatypes::Datatype;
pub use table::table::{ColumnDefinition, TableDefinition}; pub use table::table::{ColumnDefinition, TableDefinition};
use crate::parser::command::InsertItem;
const BUFFER_SIZE: usize = 500; const BUFFER_SIZE: usize = 500;
fn handle_create(command: CreateCommand) -> ::anyhow::Result<TableDefinition> { fn handle_create(command: CreateCommand) -> ::anyhow::Result<TableDefinition> {
@ -122,7 +119,7 @@ fn handle_select(command: SelectCommand) -> ::anyhow::Result<String> {
fn run_command(query: String) -> String { fn run_command(query: String) -> String {
let response: String; let response: String;
if query.chars().nth(0).unwrap() == '\\' { 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 String::from("Slash commands are not yet supported in SQUIRREL");
} }
@ -143,7 +140,7 @@ fn run_command(query: String) -> String {
Command::Insert(insert_command) => { Command::Insert(insert_command) => {
let result = handle_insert(insert_command); let result = handle_insert(insert_command);
if result.is_err() { if result.is_err() {
String::from(result.err().unwrap().to_string()) result.err().unwrap().to_string()
} else { } else {
String::from("Data inserted.") String::from("Data inserted.")
} }
@ -161,16 +158,16 @@ fn run_command(query: String) -> String {
} }
fn handle_client(mut stream: TcpStream) { fn handle_client(mut stream: TcpStream) {
let mut data = [0 as u8; BUFFER_SIZE]; let mut data = [0_u8; BUFFER_SIZE];
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);
let mut response_data_size = response.len().to_le_bytes(); let response_data_size = response.len().to_le_bytes();
stream.write(&mut response_data_size).unwrap(); // send length of message stream.write_all(&response_data_size).unwrap(); // send length of message
stream.write(response.as_bytes()).unwrap(); // send message stream.write_all(response.as_bytes()).unwrap(); // send message
true true
} }
Err(_) => { Err(_) => {
@ -194,7 +191,6 @@ fn main() -> std::io::Result<()> {
for stream in listener.incoming() { for stream in listener.incoming() {
thread::spawn(|| { thread::spawn(|| {
handle_client(stream.expect("A valid stream")); handle_client(stream.expect("A valid stream"));
()
}); });
} }

View file

@ -78,7 +78,7 @@ pub fn tokenizer(text: String) -> Vec<String> {
} }
if !in_quotes && parts.contains(&cur_char) { if !in_quotes && parts.contains(&cur_char) {
if cur_str.len() != 0 { if !cur_str.is_empty() {
tokens.push(cur_str); tokens.push(cur_str);
cur_str = String::new(); cur_str = String::new();
} }
@ -90,7 +90,7 @@ pub fn tokenizer(text: String) -> Vec<String> {
} }
} }
return tokens; tokens
} }
impl Command { impl Command {
@ -204,7 +204,7 @@ impl Command {
} }
} }
return Err(anyhow!("Unexpected end of input")); Err(anyhow!("Unexpected end of input"))
} }
fn parse_select_command(tokens: &mut Vec<String>) -> ::anyhow::Result<Command> { fn parse_select_command(tokens: &mut Vec<String>) -> ::anyhow::Result<Command> {
@ -243,7 +243,7 @@ impl Command {
} }
} }
return Err(anyhow!("Unexpected end of input")); Err(anyhow!("Unexpected end of input"))
} }
fn parse_create_command(tokens: &mut Vec<String>) -> ::anyhow::Result<Command> { fn parse_create_command(tokens: &mut Vec<String>) -> ::anyhow::Result<Command> {
@ -280,7 +280,7 @@ impl Command {
state = CreateParserState::FindColumnDatatype; state = CreateParserState::FindColumnDatatype;
} }
CreateParserState::FindColumnDatatype => { CreateParserState::FindColumnDatatype => {
let dtype = Datatype::from_str(&token).unwrap(); let dtype = Datatype::from_str(token).unwrap();
if dtype.has_len() { if dtype.has_len() {
state = CreateParserState::FindColumnLength; state = CreateParserState::FindColumnLength;
} else { } else {
@ -330,7 +330,7 @@ impl Command {
} }
} }
return Err(anyhow!("Unexpected end of input")); Err(anyhow!("Unexpected end of input"))
} }
pub fn from_string(command_str: String) -> ::anyhow::Result<Command> { pub fn from_string(command_str: String) -> ::anyhow::Result<Command> {
@ -344,6 +344,7 @@ impl Command {
_ => Err(anyhow!("Unknown command '{}'", token)), _ => Err(anyhow!("Unknown command '{}'", token)),
}; };
} }
return Err(anyhow!("Unexpected end of statement"));
Err(anyhow!("Unexpected end of statement"))
} }
} }

View file

@ -33,11 +33,11 @@ impl Datatype {
// Remove dquotes // Remove dquotes
str_bytes.remove(0); str_bytes.remove(0);
str_bytes.remove(str_bytes.len() - 1); str_bytes.remove(str_bytes.len() - 1);
return Ok(str_bytes); Ok(str_bytes)
} }
Datatype::Integer => { Datatype::Integer => {
let val = data_val.parse::<u8>()?; let val = data_val.parse::<u8>()?;
return Ok(vec![val]); Ok(vec![val])
} }
} }
} }
@ -46,11 +46,11 @@ impl Datatype {
match self { match self {
Datatype::CharacterVarying => { Datatype::CharacterVarying => {
let str_val = String::from_utf8(data_val.to_vec())?; let str_val = String::from_utf8(data_val.to_vec())?;
return Ok(str_val); Ok(str_val)
} }
Datatype::Integer => { Datatype::Integer => {
let val = data_val.first().unwrap(); let val = data_val.first().unwrap();
return Ok(format!("{}", val)); Ok(format!("{}", val))
} }
} }
} }

View file

@ -1,2 +1,2 @@
pub mod table;
pub mod datatypes; pub mod datatypes;
pub mod table;