Skip to main content

Introduction to Databas

Databas is a toy SQLite-like database system implemented in Rust for educational purposes. It demonstrates core database concepts including page-based storage, SQL parsing, and query execution.
This is a work-in-progress educational project. It is not intended for production use.

What is Databas?

Databas implements a simplified relational database system that uses many of the same concepts found in SQLite:
  • Page-based storage with a custom binary format
  • SQL parser that handles SELECT, INSERT, and CREATE TABLE statements
  • Command-line interface for interactive queries
  • B-tree data structures for efficient table storage
  • Page cache with clock replacement algorithm

Key features

SQL Parser

Hand-written lexer and parser supporting SELECT, INSERT, and CREATE TABLE statements with expressions, WHERE clauses, ORDER BY, and LIMIT.

Page-based Storage

Custom binary format with 4KB pages, magic number validation, and checksums. Supports both leaf and interior B-tree pages.

Type System

Supports INT, FLOAT, and TEXT column types with PRIMARY KEY and NULLABLE constraints.

Interactive CLI

Command-line interface with REPL mode for executing queries and viewing parsed SQL statements.

Architecture overview

Databas is organized as a Rust workspace with three main crates:

databas_cli

The command-line interface that provides:
  • Interactive REPL with >>> prompt
  • Single query execution mode
  • SQL statement parsing and display

databas_core

The core database engine implementing:
  • DatabaseHeader - Database metadata with magic number databas format1\0
  • DiskManager - File I/O operations for reading and writing pages
  • PageCache - In-memory cache with clock replacement and pin guards
  • TablePage - B-tree page structures (leaf and interior nodes)
  • Page checksums for data integrity

databas_sql_parser

The SQL parsing layer featuring:
  • Lexer - Tokenizes SQL statements
  • Parser - Builds abstract syntax trees using Pratt parsing
  • Support for expressions, operators, aggregates, and literals
  • Error handling with position tracking

Get started

Quickstart

Build from source and run your first queries

Architecture

Deep dive into the database internals

Example queries

Databas supports standard SQL syntax for table creation, data insertion, and queries:
CREATE TABLE products (id INT PRIMARY KEY, name TEXT, price FLOAT);
INSERT INTO products (id, name, price) VALUES (123, 'Cake', 45.67);
SELECT name, price FROM products WHERE price > 10.00 ORDER BY price DESC;

Why Databas?

This project is designed for learning:
  • Understand database internals - See how page-based storage actually works
  • Learn Rust - Practical example of systems programming with Rust
  • Study parsing - Hand-written lexer and parser implementation
  • Explore data structures - B-trees, page caches, and disk management
The codebase is intentionally kept simple and readable to serve as a learning resource.

Build docs developers (and LLMs) love