A simple in-memory SQL-like database implemented in Rust from scratch.
The project supports creating and deleting tables and records, querying data, sorting it and limiting the number of returned records. Interaction with the database is handled through the CLI.
- Create tables
- Insert and delete records
SELECTqueries withWHEREconditionsORDER_BYandLIMITforSELECTqueriesi64,f64,boolandStringvalues- Saving command history to file
- Fetching commands from a file
- Command parser implemented from scratch
- User-friendly error handling
- Unit tests for the database
The database is implemented from scratch i.e. not relying on any outside engine or library for handling databases. SQL-like commands are parsed by a custom-written parser which directly executes the commands on the database. The data is stored in data structures in memory, so it's non-persistent. Instructions are represented internally by an enum, since Rust enums are very versatile, allowing for additional fields for each value, thus fitting nicely into the data format. Records in the tables are stored in a built-in B-Tree map, while tables, field types and values of record are stored as a built-in HashMap.
Allowed commands and their syntax is as follows:
CREATE table_name KEY key_name FIELDS field_name1: type1, field_name2: type2, ...INSERT field_name1 = value1, field_name2 = value2, ... INTO table_nameSELECT field_name1, field_name2, ... FROM table_name (WHERE condition) (ORDER_BY field_name) (LIMIT number)DELETE key_value FROM table_nameSAVE_AS file_nameREAD_FROM file_name
Make sure you have Rust and Cargo installed.
First, clone the repository
git clone https://github.com/scorelowless/rust-database.git
cd rust-databaseRun the application using Cargo, specifying the type of primary key used in the database with the -c option:
cargo run -- -c string
or
cargo run -- -c int
After that, the application is ready to accept commands through the CLI
Entirety of code was written without the use of AI tools. AI was only used to some extent for explaining bugs and fixes for them.