Skip to content

About

SQL-like database implemented from scratch in Rust

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

Rust database

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.

Features

  • Create tables
  • Insert and delete records
  • SELECT queries with WHERE conditions
  • ORDER_BY and LIMIT for SELECT queries
  • i64, f64, bool and String values
  • Saving command history to file
  • Fetching commands from a file
  • Command parser implemented from scratch
  • User-friendly error handling
  • Unit tests for the database

Implementation

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.

Instructions

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_name
  • SELECT field_name1, field_name2, ... FROM table_name (WHERE condition) (ORDER_BY field_name) (LIMIT number)
  • DELETE key_value FROM table_name
  • SAVE_AS file_name
  • READ_FROM file_name

Running

Make sure you have Rust and Cargo installed.

First, clone the repository

git clone https://github.com/scorelowless/rust-database.git
cd rust-database

Run 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

AI disclosure

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.

About

SQL-like database implemented from scratch in Rust

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages