This project aims to create a basic version of SQLite in C++ that can perform essential database operations on .db files. The implementation focuses on understanding the structure of SQLite databases and interacting with them. The application supports the following features:
- Reading
.dbfiles - Retrieving the number of tables
- Determining the page size
- Counting rows in a table
- Reading data from a single column
- Reading data from multiple columns
- Filtering data with a
WHEREclause - Performing full-table scans
- Retrieving data using an index
The entire project is implemented in a single file:
- server.cpp: Contains all the classes, utility functions, and logic for interacting with SQLite
.dbfiles.
This class represents the SQLite database and handles operations like file opening, reading headers, and general database metadata operations.
- open(): Opens the
.dbfile in binary mode. - getPageSize(): Reads the page size from the database header.
- getTableCount(): Calculates the number of tables in the database.
This class focuses on table-level operations, such as row counts and column data retrieval.
- countRows(): Counts the number of rows in a specific table.
- readColumn(): Reads data from a single column.
- readMultipleColumns(): Reads data from multiple columns in a table.
This class processes and executes queries on the database.
- filterData(): Filters data using a
WHEREclause. - fullTableScan(): Retrieves all data from a table.
- retrieveUsingIndex(): Retrieves data using an index.
A utility class containing helper functions for general tasks like byte conversions and parsing.
- bigEndianToInt(const char bytes, int size)*: Converts big-endian byte arrays to integers.
- parseString(const char bytes, int size)*: Converts byte arrays to strings.
- Open Database File: Use the
Database::open()method to open and validate the SQLite.dbfile. - Retrieve Metadata: Call
Database::getPageSize()andDatabase::getTableCount()to understand the structure of the database. - Work with Tables: Use the
Tableclass to count rows and fetch column data. - Execute Queries: Use the
Queryclass for full-table scans,WHEREclauses, and index-based retrievals. - Utility Functions: Utilize the
Utilsclass for data conversions and parsing during database operations.
#include "server.cpp"
int main() {
string db_path = "example.db";
Database db(db_path);
if (db.open()) {
cout << "Page Size: " << db.getPageSize() << " bytes" << endl;
cout << "Number of Tables: " << db.getTableCount() << endl;
Table table("example_table");
cout << "Row Count: " << table.countRows(db) << endl;
Query query;
query.fullTableScan(db, "example_table");
} else {
cout << "Failed to open database file." << endl;
}
return 0;
}- Clone the repository.
- Compile
server.cppusing a C++ compiler (e.g.,g++):g++ -o sqlite_clone server.cpp
- Run the executable with the path to an SQLite
.dbfile:./sqlite_clone
- Implement support for writing data to the database.
- Add support for complex SQL queries.
- Include better error handling and validation for
.dbfiles. - Support additional SQLite features, such as transactions and schemas.
This project is built using standard C++ libraries and does not require any external dependencies.
- Ensure the
.dbfile is a valid SQLite database. - Currently, the project only supports reading and basic querying.