Skip to content

A rudimentary SQLite DB file reader implementation to demonstrate how one might read SQLite B-trees in Elixir

License

Notifications You must be signed in to change notification settings

tugayac/codecrafters-sqlite-elixir

Repository files navigation

Overview

This repo implements a rudimentary SQLite DB file in Elixir to demonstrate how one might read B-trees in Elixir.

It was built for the "Build Your Own SQLite" Challenge and passes all the stages.

⚠️ This implementation is meant for demo purposes ONLY and is not meant for production use. There are a bunch of shortcuts taken and only a small subset of SQL keywords are supported. TODOs were left in the codebase as a reminder for later refactoring.

Sample Databases

There are two databases that you can use:

  1. superheroes.db:
    • This is a small version of the test database used in the table-scan stage.
    • It contains one table: superheroes.
    • It is ~1MB in size.
  2. companies.db:
    • This is a small version of the test database used in the index-scan stage.
    • It contains one table: companies, and one index: idx_companies_country
    • It is ~7MB in size.

These aren't included in the repository because they're large in size. You can download them by running this script:

./download_sample_databases.sh

If the script doesn't work for some reason, you can download the databases directly from codecrafters-io/sample-sqlite-databases.

Supported SQL Queries

The following SQL queries are supported:

  • You can get the total number of rows in a table (WHERE clauses are not supported): "SELECT COUNT(*) FROM superheroes"
  • You can query the sqlite_schema table: "SELECT tbl_name FROM sqlite_schema"
  • You must specify the columns to select (* is not supported): "SELECT id, name FROM companies"
  • You can only use a single column in the WHERE clause: "SELECT id, name FROM companies WHERE country = 'tonga'"

While CREATE TABLE and CREATE INDEX statements can be parsed by the parser, running those commands won't do anything.

See the lib/lexer.ex moduledocs for more details on supported SQL queries.

Sample Calls

You can try the following queries:

./your_program.sh superheroes.db .dbinfo
./your_program.sh superheroes.db .tables
./your_program.sh companies.db "SELECT id, name FROM companies WHERE country = 'tonga'"

License

You're free to use the code however you'd like (MIT License).

About

A rudimentary SQLite DB file reader implementation to demonstrate how one might read SQLite B-trees in Elixir

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors