Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed .DS_Store
Binary file not shown.
32 changes: 32 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: CI

on:
push:
branches: [main]
pull_request:

permissions:
contents: read

jobs:
build-and-test:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}

steps:
- name: Check out repository
uses: actions/checkout@v6
with:
persist-credentials: false

- name: Configure
run: cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON

- name: Build
run: cmake --build build --config Release --parallel

- name: Test
run: ctest --test-dir build -C Release --output-on-failure
53 changes: 27 additions & 26 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
# Prerequisites
*.d
# Build output
/build/
/cmake-build-*/
CMakeCache.txt
CMakeFiles/
CTestTestfile.cmake
cmake_install.cmake
Makefile
Testing/

# Compiled Object files
*.slo
# Compiled artifacts
*.a
*.app
*.d
*.dll
*.dylib
*.exe
*.gch
*.lai
*.lib
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.out
*.pch

# Compiled Dynamic libraries
*.slo
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app
# Database files created by the CLI and tests
*.bin

cmake-build-debug
# Editors and operating systems
.DS_Store
.idea/
.vscode/
8 changes: 0 additions & 8 deletions .idea/.gitignore

This file was deleted.

1 change: 0 additions & 1 deletion .idea/.name

This file was deleted.

4 changes: 0 additions & 4 deletions .idea/misc.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

101 changes: 39 additions & 62 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,72 +1,49 @@
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project(project_name)
cmake_minimum_required(VERSION 3.20)

project(SQLInCPP VERSION 1.0.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(SQL_CORE_SOURCES
includes/binary_files/file_record.cpp
includes/binary_files/utilities.cpp
includes/parser/parser.cpp
includes/shunting_yard/shunting_yard.cpp
includes/sql/sql.cpp
includes/table/table.cpp
includes/tokenizer/ftokenize.cpp
includes/tokenizer/state_machine_functions.cpp
includes/tokenizer/stokenize.cpp
)

add_library(sql_core STATIC ${SQL_CORE_SOURCES})
target_include_directories(sql_core PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")

#. . . . . . . . . . . . . . . . . . . .
include(GNUInstallDirs)
if(MSVC)
add_compile_options(/W4)
add_link_options(/W4)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/bin)
target_compile_options(sql_core PRIVATE /W4)
else()
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
target_compile_options(sql_core PRIVATE -Wall -Wextra -Wpedantic)
endif()

add_executable(run_sql run_sql.cpp)
target_link_libraries(run_sql PRIVATE sql_core)

# place binaries and libraries according to GNU standards
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
include(CTest)

if(BUILD_TESTING)
add_executable(sql_tests tests/sql_tests.cpp)
target_link_libraries(sql_tests PRIVATE sql_core)
target_compile_definitions(sql_tests PRIVATE
SQL_TEST_FIXTURE_DIR="${CMAKE_CURRENT_SOURCE_DIR}/batch"
)

set(SQL_TEST_WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test-workspace")
file(MAKE_DIRECTORY "${SQL_TEST_WORKING_DIRECTORY}")

if(CMAKE_CXX_COMPILER_ID MATCHES GNU)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
add_test(NAME sql_behavior COMMAND sql_tests)
set_tests_properties(sql_behavior PROPERTIES
WORKING_DIRECTORY "${SQL_TEST_WORKING_DIRECTORY}"
)
endif()
#. . . . . . . . . . . . . . . . . . . .

# ------------------------------------
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
)

# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

# GoogleTest requires at least C++11
set(CMAKE_CXX_STANDARD 14)

enable_testing()


# ------------------------------------

#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Grab all cpp files from includes folder
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
set(INCLUDES_FOLDER includes)
FILE(GLOB_RECURSE SOURCE_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "${INCLUDES_FOLDER}/*.cpp" )
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

ADD_EXECUTABLE(run_sql
run_sql.cpp
${SOURCE_FILES}
)
ADD_EXECUTABLE(basic_test
_tests/_test_files/basic_test.cpp
${SOURCE_FILES}
)
ADD_EXECUTABLE(test_batch1
_tests/_test_files/test_batch1.cpp
${SOURCE_FILES}
)

ADD_EXECUTABLE(test_batch2
_tests/_test_files/test_batch2.cpp
${SOURCE_FILES}
)
TARGET_LINK_LIBRARIES(run_sql gtest)
TARGET_LINK_LIBRARIES(basic_test gtest)
TARGET_LINK_LIBRARIES(test_batch1 gtest)
TARGET_LINK_LIBRARIES(test_batch2 gtest)
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2026 Hanbin Tan

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
110 changes: 81 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,90 @@
# SQL_InCPP
A small version of what is called a relational database management system (DBMS),
as well as a DSL (Domain Specific Language) with which a user can interact with your database.
# SQL in C++

SQL in C++ is an educational relational database engine and command-line DSL implemented from first principles.
It parses a focused subset of SQL, persists records to binary files, indexes field values with B+ trees, and evaluates compound filters with a shunting-yard pipeline.

## Preview
![Command-line preview](image/preview.png)

![preview.png](./image/preview.png)
## Reserved keywords
## What it supports

- create
- table
- fields
- insert
- into
- values
- select
- from
- where
- \*
- and, or
- !=, = , <, >, >=, <=
- `CREATE TABLE` with an ordered field list.
- `INSERT INTO` with quoted or unquoted values.
- `SELECT *` and projected field lists.
- `WHERE` filters using `=`, `!=`, `<`, `<=`, `>`, and `>=`.
- Compound `AND` and `OR` expressions with parentheses.
- Binary persistence across separate CLI sessions.
- Batch execution from the sample command files in `batch/`.

- create table <TABLE_NAME> fields <FIELD_NAME> [, <FIELD_NAME> ...]
- insert into <TABLE_NAME> values <VALUE> [, <VALUE> ...]
- select <\* | FIELD_NAME> [, <FIELD_NAME> ...] from <TABLE_NAME> where <FIELD_NAME>
<RELATIONAL_OPERATOR> <VALUE> [<LOGICAL_OPERATOR> <FIELD_NAME><RELATIONAL_OPERATOR> <VALUE> ...]
This project is intentionally smaller than a production SQL database.
It does not implement joins, transactions, schemas, query planning, updates, or deletes.
Values are stored and ordered as strings, so numeric-looking values use lexical comparison semantics.

## Design
## Architecture

It can be found in https://github.com/hanbintan2003/SQL/blob/main/design.txt
| Component | Responsibility |
| --- | --- |
| `includes/tokenizer` | Converts command text into lexical tokens with a state machine. |
| `includes/parser` | Validates the supported grammar and builds a parse tree. |
| `includes/shunting_yard` | Converts compound filter expressions to postfix form. |
| `includes/map` | Implements B+ tree-backed maps and multimaps. |
| `includes/binary_files` | Reads and writes fixed-format binary records. |
| `includes/table` | Owns table persistence, indexes, projections, and selections. |
| `includes/sql` | Coordinates parsing and table operations through the public `SQL` interface. |

## Features
Additional implementation notes are available in [design.txt](design.txt).
The canonical repository is [hskl18/SQL](https://github.com/hskl18/SQL).

- it supports unlimited amount of whitespaces between the input statement
- it supports complex boolean query statements for selection
- it supports permanent storage where the created tables will be stored inside the binary files
- it has an interactive mode where the user can type into the command line and the input statement can be used to interact with the database
## Build

You need a C++17 compiler and CMake 3.20 or newer.

```bash
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build --parallel
```

The build has no downloaded test framework or other third-party dependency.

## Test

```bash
ctest --test-dir build --output-on-failure
```

The test executable verifies inserts, projections, compound filters, persistence, invalid input handling, and both repository batch fixtures.
Tests run in an isolated build directory, so they do not leave database files in the source tree.

## Run the CLI

```bash
./build/run_sql
```

Type `end` to exit and `cls` to clear the terminal.
The CLI writes each table to `<table>.bin` and `<table>_fields.bin` in the current working directory.

Example session:

```text
SQL>> create table student fields first, last, major, age
SQL>> insert into student values Ada, Lovelace, Math, 36
SQL>> insert into student values Grace, Hopper, CS, 30
SQL>> select first, major from student where major = CS
SQL>> end
```

## Query grammar

```text
create table <table> fields <field> [, <field> ...]
insert into <table> values <value> [, <value> ...]
select <* | field [, field ...]> from <table>
[where <field> <operator> <value>
[<and | or> <field> <operator> <value> ...]]
```

Quote values that contain spaces, such as `"Mary Ann"`.

## License

This project is available under the [MIT License](LICENSE).
Loading
Loading