Skip to content

hamzalafsioui/DBMS_API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

43 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ—„οΈ C# DBMS : Database Management System

A simple, educational Database Management System built from scratch in C#. This project demonstrates how a real database engine works internally from SQL parsing to data persistence on disk with multiple ways to interact with it (CLI, REST API, Web UI).

Version 1.0 β€” Supports SELECT, INSERT, UPDATE, DELETE, CREATE TABLE, DROP TABLE, and WHERE clause.


✨ Features

Feature Description
SQL Parser Tokenizes and parses raw SQL queries into structured objects
SQL Execution Engine Routes parsed queries to the appropriate handler
Buffer Pool Manager In-memory data management with dirty page tracking
Disk Manager Persistent storage using JSON serialization
TCP Server Multi-client TCP server with custom protocol on port 9090
Interactive CLI Client Console REPL for sending SQL queries to the server
REST API ASP.NET Core Minimal API bridge between HTTP and TCP
Web Frontend Browser-based SQL editor with table rendering

πŸ—οΈ Architecture Overview

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     HTTP POST /query     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Frontend   β”‚ ──────────────────────>  β”‚   REST API   β”‚
β”‚  (HTML/JS)   β”‚                          β”‚  (ASP.NET)   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                          β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
                                                 β”‚ TCP (custom protocol)
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     TCP (custom protocol)       β”‚
β”‚  CLI Client  β”‚ ───────────────────────────────>β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                                 β–Ό
                                          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                          β”‚   TCP Server      β”‚
                                          β”‚ ConnectionHandler β”‚
                                          β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                                   β”‚
                                              β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”
                                              β”‚SqlParser  β”‚ β†’ Tokenize & parse SQL
                                              β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
                                                   β”‚ SqlParser object
                                              β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                              β”‚ SqlExecution   β”‚ β†’ Route to handler
                                              β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                                   β”‚
                                          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                          β”‚  BufferPoolManager    β”‚ β†’ In-memory operations
                                          β”‚  (dirty page tracking)β”‚
                                          β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                                   β”‚ flush on write
                                              β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                              β”‚ DiskManager   β”‚ β†’ JSON file I/O
                                              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                                   β”‚
                                              databases_list/
                                                mydb.json

πŸ“ Project Structure

DBMS_API/
β”œβ”€β”€ Database/                  # πŸ”§ Core Database Server
β”‚   β”œβ”€β”€ Program.cs             #   Entry point β€” starts TCP server
β”‚   β”œβ”€β”€ ConnectionHandler.cs   #   TCP server & client connection manager
β”‚   β”œβ”€β”€ SqlParser.cs           #   SQL query tokenizer & parser
β”‚   β”œβ”€β”€ SqlExecution.cs        #   Query execution router
β”‚   β”œβ”€β”€ BufferPoolManager.cs   #   In-memory data + dirty page tracking
β”‚   β”œβ”€β”€ DiskManager.cs         #   JSON read/write to disk
β”‚   └── Database.csproj        #   Project file
β”‚
β”œβ”€β”€ Client/                    # πŸ’» CLI Client
β”‚   β”œβ”€β”€ Program.cs             #   Entry point β€” prompts for DB name
β”‚   β”œβ”€β”€ DbClient.cs            #   TCP client with REPL loop
β”‚   └── Client.csproj          #   Project file
β”‚
β”œβ”€β”€ Api/                       # 🌐 REST API Bridge
β”‚   β”œβ”€β”€ Program.cs             #   ASP.NET Minimal API (POST /query)
β”‚   β”œβ”€β”€ DbClientService.cs     #   TCP client service for API
β”‚   └── Api.csproj             #   Project file
β”‚
β”œβ”€β”€ Frontend/                  # 🎨 Web UI
β”‚   β”œβ”€β”€ index.html             #   Main page
β”‚   β”œβ”€β”€ style.css              #   Dark theme styling
β”‚   └── app.js                 #   Frontend logic (fetch + table render)
β”‚
β”œβ”€β”€ databases_list/            # πŸ“‚ Data storage (JSON files)
β”œβ”€β”€ queries.sql                # πŸ“ Sample SQL queries
β”œβ”€β”€ presentation.txt           # πŸ“„ Project presentation (French)
β”œβ”€β”€ DBMS.sln                   # Visual Studio solution file
└── README.md                  # This file

βš™οΈ Requirements

  • .NET 8.0 SDK or later

πŸš€ How to Use

1. Clone the Repository

git clone <repository-url>
cd DBMS_API

2. Start the Database Server

cd Database
dotnet run

You will see:

Starting DBMS Server...
Server Is Listening on: localhost:9090

3. Option A : Use the CLI Client

Open a new terminal:

cd Client
dotnet run

Enter a database name when prompted (e.g. testdb), then start typing SQL commands:

testdb >>> CREATE TABLE users (username VARCHAR,age INT,salary FLOAT);
OK: New Table Created !

testdb >>> INSERT INTO users (username,age,salary) VALUES ('hamza',24,100.0);
OK: New Row Has Been Inserted !

testdb >>> SELECT * FROM users;
[
  {
    "username": "hamza",
    "age": 24,
    "salary": 100.0
  }
]

testdb >>> exit

3. Option B : Use the REST API + Web UI

Start the API (in a new terminal, while the server is running):

cd Api
dotnet run

The API will start on http://localhost:5232.

Then open Frontend/index.html in your browser to use the web-based SQL editor.

You can also call the API directly:

curl -X POST http://localhost:5232/query \
  -H "Content-Type: application/json" \
  -d '{"dbName": "testdb", "query": "SELECT * FROM users;"}'

πŸ“‹ Supported SQL Commands

Command Syntax Example
CREATE TABLE CREATE TABLE name (col TYPE, ...); CREATE TABLE users (username VARCHAR,age INT,salary FLOAT);
INSERT INTO INSERT INTO name (cols) VALUES (vals); INSERT INTO users (username,age,salary) VALUES ('hamza',24,100.0);
SELECT * SELECT * FROM name; SELECT * FROM users;
SELECT columns SELECT col1,col2 FROM name; SELECT username,age FROM users;
SELECT WHERE SELECT * FROM name WHERE condition; SELECT * FROM users WHERE age > 20;
SELECT WHERE (compound) ... WHERE cond1 AND/OR cond2; SELECT * FROM users WHERE age > 20 AND salary < 1500.0;
UPDATE UPDATE name SET col=val [WHERE ...]; UPDATE users SET age = 25 WHERE username = 'hamza';
UPDATE (all rows) UPDATE name SET col=val; UPDATE users SET salary = 2000.0;
DELETE DELETE FROM name [WHERE ...]; DELETE FROM users WHERE age < 25;
DELETE (all rows) DELETE FROM name; DELETE FROM users;
DROP TABLE DROP TABLE name; DROP TABLE users;

πŸ” WHERE Clause

The WHERE clause can be used with SELECT, UPDATE, and DELETE.

Comparison Operators

Operator Meaning
= Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
IS NULL Value is null
IS NOT NULL Value is not null

Logical Operators

Operator Meaning
AND Both conditions must be true
OR At least one condition must be true

Examples

SELECT * FROM users WHERE age = 23;
SELECT * FROM users WHERE age > 20 AND salary < 1300.0;
SELECT username FROM users WHERE age < 20 OR salary > 1300.0;
UPDATE users SET age = 25 WHERE username = 'hamza';
UPDATE users SET age = 30, salary = 3000.0 WHERE age > 25;
DELETE FROM users WHERE salary < 1000.0;

πŸ“Š Data Types

Type Description Example
VARCHAR String values (use single quotes) 'hamza'
INT Integer values 24
FLOAT Decimal / floating-point values 100.0

πŸ”Œ Custom TCP Protocol

The server and clients communicate using a simple text-based protocol:

Format: key:>value\n

Message Direction Example
Database connection Client β†’ Server db:>testdb\n
Connection response Server β†’ Client message:>Connected to testdb Successfully !\nis_json:>0\ncon:>1\n
SQL query Client β†’ Server query:>SELECT * FROM users;\n
Query response Server β†’ Client messages:>[...]\nis_json:>1\n

πŸ“ Sample Queries

A comprehensive set of sample queries is available in queries.sql, covering:

  • Table creation (CREATE TABLE)
  • Data insertion (INSERT INTO)
  • Basic SELECT queries
  • WHERE with all comparison operators
  • Compound conditions with AND / OR

⚠️ Notes

  • This is an educational project β€” not all SQL features are supported.
  • Data is persisted as JSON files inside the databases_list/ folder.
  • Each database is stored as a single .json file with Tables (schema) and Rows (data).
  • The server supports multiple concurrent client connections via Task.Run.

πŸ“œ License

This project is for educational purposes.

About

The goal of this project is to design a mini DBMS with a complete architecture, including storage management, memory management, SQL execution, an API, and a frontend.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors