Skip to content

Commit 38f7597

Browse files
authored
Merge pull request #118 from codecrafters-io/add-c
[CC-1470] Add C23 to sqlite
2 parents e217944 + 90009e6 commit 38f7597

36 files changed

+774
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh
2+
#
3+
# This script is used to compile your program on CodeCrafters
4+
#
5+
# This runs before .codecrafters/run.sh
6+
#
7+
# Learn more: https://codecrafters.io/program-interface
8+
9+
set -e # Exit on failure
10+
11+
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake
12+
cmake --build ./build
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
#
3+
# This script is used to run your program on CodeCrafters
4+
#
5+
# This runs after .codecrafters/compile.sh
6+
#
7+
# Learn more: https://codecrafters.io/program-interface
8+
9+
set -e # Exit on failure
10+
11+
exec $(dirname $0)/build/sqlite "$@"

compiled_starters/c/.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

compiled_starters/c/.gitignore

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Database files used for testing
2+
*.db
3+
4+
# Prerequisites
5+
*.d
6+
7+
# Object files
8+
*.o
9+
*.ko
10+
*.obj
11+
*.elf
12+
13+
# Linker output
14+
*.ilk
15+
*.map
16+
*.exp
17+
18+
# Precompiled Headers
19+
*.gch
20+
*.pch
21+
22+
# Libraries
23+
*.lib
24+
*.a
25+
*.la
26+
*.lo
27+
28+
# Shared objects (inc. Windows DLLs)
29+
*.dll
30+
*.so
31+
*.so.*
32+
*.dylib
33+
34+
# Executables
35+
*.exe
36+
*.out
37+
*.app
38+
*.i*86
39+
*.x86_64
40+
*.hex
41+
42+
# Debug files
43+
*.dSYM/
44+
*.su
45+
*.idb
46+
*.pdb
47+
48+
# Kernel Module Compile Results
49+
*.mod*
50+
*.cmd
51+
.tmp_versions/
52+
modules.order
53+
Module.symvers
54+
Mkfile.old
55+
dkms.conf
56+
57+
build
58+
vcpkg_installed

compiled_starters/c/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
cmake_minimum_required(VERSION 3.13)
2+
3+
project(codecrafters-sqlite)
4+
5+
file(GLOB_RECURSE SOURCE_FILES src/*.c src/*.h)
6+
7+
set(CMAKE_C_STANDARD 23) # Enable the C23 standard
8+
9+
add_executable(sqlite ${SOURCE_FILES})

compiled_starters/c/README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/sqlite.png)
2+
3+
This is a starting point for C solutions to the
4+
["Build Your Own SQLite" Challenge](https://codecrafters.io/challenges/sqlite).
5+
6+
In this challenge, you'll build a barebones SQLite implementation that supports
7+
basic SQL queries like `SELECT`. Along the way we'll learn about
8+
[SQLite's file format](https://www.sqlite.org/fileformat.html), how indexed data
9+
is
10+
[stored in B-trees](https://jvns.ca/blog/2014/10/02/how-does-sqlite-work-part-2-btrees/)
11+
and more.
12+
13+
**Note**: If you're viewing this repo on GitHub, head over to
14+
[codecrafters.io](https://codecrafters.io) to try the challenge.
15+
16+
# Passing the first stage
17+
18+
The entry point for your SQLite implementation is in `src/main.c`. Study and
19+
uncomment the relevant code, and push your changes to pass the first stage:
20+
21+
```sh
22+
git commit -am "pass 1st stage" # any msg
23+
git push origin master
24+
```
25+
26+
Time to move on to the next stage!
27+
28+
# Stage 2 & beyond
29+
30+
Note: This section is for stages 2 and beyond.
31+
32+
1. Ensure you have `gcc` installed locally
33+
1. Run `./your_program.sh` to run your program, which is implemented in
34+
`src/main.c`.
35+
1. Commit your changes and run `git push origin master` to submit your solution
36+
to CodeCrafters. Test output will be streamed to your terminal.
37+
38+
# Sample Databases
39+
40+
To make it easy to test queries locally, we've added a sample database in the
41+
root of this repository: `sample.db`.
42+
43+
This contains two tables: `apples` & `oranges`. You can use this to test your
44+
implementation for the first 6 stages.
45+
46+
You can explore this database by running queries against it like this:
47+
48+
```sh
49+
$ sqlite3 sample.db "select id, name from apples"
50+
1|Granny Smith
51+
2|Fuji
52+
3|Honeycrisp
53+
4|Golden Delicious
54+
```
55+
56+
There are two other databases that you can use:
57+
58+
1. `superheroes.db`:
59+
- This is a small version of the test database used in the table-scan stage.
60+
- It contains one table: `superheroes`.
61+
- It is ~1MB in size.
62+
1. `companies.db`:
63+
- This is a small version of the test database used in the index-scan stage.
64+
- It contains one table: `companies`, and one index: `idx_companies_country`
65+
- It is ~7MB in size.
66+
67+
These aren't included in the repository because they're large in size. You can
68+
download them by running this script:
69+
70+
```sh
71+
./download_sample_databases.sh
72+
```
73+
74+
If the script doesn't work for some reason, you can download the databases
75+
directly from
76+
[codecrafters-io/sample-sqlite-databases](https://github.com/codecrafters-io/sample-sqlite-databases).
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Set this to true if you want debug logs.
2+
#
3+
# These can be VERY verbose, so we suggest turning them off
4+
# unless you really need them.
5+
debug: false
6+
7+
# Use this to change the C version used to run your code
8+
# on Codecrafters.
9+
#
10+
# Available versions: c-23
11+
language_pack: c-23
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/sh
2+
3+
echo "Downloading superheroes.db: ~1MB (used in stage 7)"
4+
curl -Lo superheroes.db https://raw.githubusercontent.com/codecrafters-io/sample-sqlite-databases/master/superheroes.db
5+
6+
echo "Downloading companies.db: ~7MB (used in stage 8)"
7+
curl -Lo companies.db https://raw.githubusercontent.com/codecrafters-io/sample-sqlite-databases/master/companies.db
8+
9+
echo "Sample databases downloaded."

compiled_starters/c/src/main.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <string.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
5+
int main(int argc, char *argv[]) {
6+
if (argc != 3) {
7+
fprintf(stderr, "Usage: ./your_program.sh <database path> <command>\n");
8+
return 1;
9+
}
10+
11+
const char *database_file_path = argv[1];
12+
const char *command = argv[2];
13+
14+
if (strcmp(command, ".dbinfo") == 0) {
15+
FILE *database_file = fopen(database_file_path, "rb");
16+
if (!database_file) {
17+
fprintf(stderr, "Failed to open the database file\n");
18+
return 1;
19+
}
20+
21+
fseek(database_file, 16, SEEK_SET); // Skip the first 16 bytes of the header
22+
unsigned char buffer[2];
23+
fread(buffer, 1, 2, database_file);
24+
unsigned short page_size = (buffer[1] | (buffer[0] << 8));
25+
26+
// You can use print statements as follows for debugging, they'll be visible when running tests.
27+
fprintf(stderr, "Logs from your program will appear here!\n");
28+
29+
// Uncomment this to pass the first stage
30+
// printf("database page size: %u\n", page_size);
31+
32+
fclose(database_file);
33+
} else {
34+
fprintf(stderr, "Unknown command %s\n", command);
35+
return 1;
36+
}
37+
38+
return 0;
39+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"default-registry": {
3+
"kind": "git",
4+
"baseline": "c4af3593e1f1aa9e14a560a09e45ea2cb0dfd74d",
5+
"repository": "https://github.com/microsoft/vcpkg"
6+
},
7+
"registries": [
8+
{
9+
"kind": "artifact",
10+
"location": "https://github.com/microsoft/vcpkg-ce-catalog/archive/refs/heads/main.zip",
11+
"name": "microsoft"
12+
}
13+
]
14+
}

0 commit comments

Comments
 (0)