Skip to content

flowlight0/simpledb-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

109 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

simpledb-rs

Coverage Status

This repository is a Rust implementation of SimpleDB from the book "Database Design and Implementation" by Edward Sciore.

Overview

The main goal of this repoistory is my own learning. The code from Chapter 3 to Chapter 15 of the book was originally implemented in Java, and I have re-implemented it in Rust.

Usage

Three binaries are available:

  • client: A SQL client for interacting with the database.
    • It receives three command line arguments:
      • db: The name of the database to connect to.
      • host: The host where the database server is running. If it is not specified, the client will connect to local DB with the embedded driver. Otherwise, it will connect to the remote DB server with the specified host and port.
      • port: The port on which the database server is listening (default: 50051). If the host is not specified, this argument is ignored.
  • server: The database server that handles client requests.
  • create_student_db: Creates a sample database for the student records.

You can run these binaries using cargo run --bin <binary_name>. For example, to run the client, use:

cargo run --bin client studentdb

Supported SQL Commands

The client supports the following SQL commands:

  • SELECT: Retrieve data from one or more tables. It supports some additional clauses and keywords:

    • WHERE: Filter records based on conditions.
    • ORDER BY: Sort the results by one or more columns.
    • GROUP BY: Group records based on one or more columns.
    • AS: Rename columns in the result set.
  • INSERT: Add new records to a table.

  • DELETE: Remove records from a table.

  • MODIFY: Update existing records in a table.

  • SHOW TABLES: List all tables in the database.

  • CREATE TABLE: Create a new table in the database.

  • CREATE INDEX: Create an index on a table.

For more details, please see the grammar in src/parser/grammar.lalrpop.

Example (embedded)

➜  simpledb-rs git:(main) ✗ cargo run --quiet --bin create_student_db # Create STUDENT DB for DEMO
Table STUDENT created.
STUDENT records inserted.
Table DEPT created.
DEPT records inserted.
Table COURSE created.
COURSE records inserted.
Table SECTION created.
SECTION records inserted.
Table ENROLL created.
ENROLL records inserted.
➜  simpledb-rs git:(main) ✗ cargo run --quiet --bin client studentdb # Connect to the local studentdb with embedded driver
SQL (studentdb)> show tables
   name |                                                                     schema
------------------------------------------------------------------------------------
tblcat  | slotsize I32, tblname VARCHAR(50)
fldcat  | type I32, length I32, offset I32, tblname VARCHAR(50), fldname VARCHAR(50)
idxcat  | index_name VARCHAR(255), table_name VARCHAR(255), field_name VARCHAR(255)
STUDENT | SId I32, MajorId I32, GradYear I32, SName VARCHAR(10)
DEPT    | DId I32, DName VARCHAR(8)
COURSE  | CId I32, DeptId I32, Title VARCHAR(20)
SECTION | SectId I32, CourseId I32, YearOffered I32, Prof VARCHAR(8)
ENROLL  | EId I32, StudentId I32, SectionId I32, Grade VARCHAR(2)

SQL (studentdb)> select * from STUDENT
         SId |      MajorId |     GradYear |      SName
-------------------------------------------------------
           1 |           10 |         2021 | joe
           2 |           20 |         2020 | amy
           3 |           10 |         2022 | max
           4 |           20 |         2022 | sue
           5 |           30 |         2020 | bob
           6 |           20 |         2020 | kim
           7 |           30 |         2021 | art
           8 |           20 |         2019 | pat
           9 |           10 |         2021 | lee

SQL (studentdb)> select SName, DName, Grade, YearOffered from STUDENT, DEPT, ENROLL, SECTION where SId = StudentId and SectId = SectionId and DId = MajorId and YearOffered = 2018
 YearOffered |      SName |    DName | Grade
--------------------------------------------
        2018 | joe        | compsci  | A
        2018 | sue        | math     | A
        2018 | kim        | math     | A

SQL (studentdb)> select * from ENROLL
         EId |    StudentId |    SectionId | Grade
--------------------------------------------------
          14 |            1 |           13 | A
          24 |            1 |           43 | C
          34 |            2 |           43 | B+
          44 |            4 |           33 | B
          54 |            4 |           53 | A
          64 |            6 |           53 | A

SQL (studentdb)> MODIFY ENROLL SET Grade = 'A+' WHERE StudentId = 6
1 records processed

SQL (studentdb)> select SName, DName, Grade, YearOffered from STUDENT, DEPT, ENROLL, SECTION where SId = StudentId and SectId = SectionId and DId = MajorId and YearOffered = 2018
 YearOffered |      SName |    DName | Grade
--------------------------------------------
        2018 | joe        | compsci  | A
        2018 | sue        | math     | A
        2018 | kim        | math     | A+

SQL (studentdb)> DELETE FROM STUDENT WHERE SName = 'joe'
1 records processed

SQL (studentdb)> select * from STUDENT
         SId |      MajorId |     GradYear |      SName
-------------------------------------------------------
           2 |           20 |         2020 | amy
           3 |           10 |         2022 | max
           4 |           20 |         2022 | sue
           5 |           30 |         2020 | bob
           6 |           20 |         2020 | kim
           7 |           30 |         2021 | art
           8 |           20 |         2019 | pat
           9 |           10 |         2021 | lee

SQL (studentdb)> exit

Example (remote)

In terminal A, start the server:

➜  simpledb-rs git:(main) ✗ cargo run --quiet --bin server # Start the server whose port is 50051

In terminal B, connect to the server:

➜  simpledb-rs git:(main) ✗ cargo run --quiet --bin client -- --host 127.0.0.1 studentdb # Connect to the local server with network driver
SQL ()> select * from COURSE
         CId |       DeptId |                Title
--------------------------------------------------
          12 |           10 | db systems
          22 |           10 | compilers
          32 |           20 | calculus
          42 |           20 | algebra
          52 |           30 | acting
          62 |           30 | elocution

SQL ()> exit

Interactive Client

The client binary now uses rustyline for input. This allows convenient line editing and command history. Previous statements are stored in a .simpledb_history file in the current directory and loaded automatically the next time the client runs.

Completed excercises

The following issues correspond to finished exercises from the book:

Useful links


About

Rust implementation of SimpleDB

Topics

Resources

License

Stars

Watchers

Forks

Contributors

Languages