Skip to content

Commit b710fe2

Browse files
committed
feat: add gleam support to sqlite
1 parent 0418228 commit b710fe2

29 files changed

+599
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
course_definition_tester
2+
.history/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

compiled_starters/gleam/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.beam
2+
*.ez
3+
/build
4+
erl_crash.dump

compiled_starters/gleam/README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/sqlite.png)
2+
3+
This is a starting point for Gleam 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/sqlite.gleam`. Study
19+
and uncomment the relevant code, and push your changes to pass the first stage:
20+
21+
```sh
22+
git add .
23+
git commit -m "pass 1st stage" # any msg
24+
git push origin master
25+
```
26+
27+
Time to move on to the next stage!
28+
29+
# Stage 2 & beyond
30+
31+
Note: This section is for stages 2 and beyond.
32+
33+
1. Ensure you have `gleam (1.0)` installed locally
34+
1. Run `./your_sqlite3.sh` to run your program, which is implemented in
35+
`src/sqlite.gleam`.
36+
1. Commit your changes and run `git push origin master` to submit your solution
37+
to CodeCrafters. Test output will be streamed to your terminal.
38+
39+
# Sample Databases
40+
41+
To make it easy to test queries locally, we've added a sample database in the
42+
root of this repository: `sample.db`.
43+
44+
This contains two tables: `apples` & `oranges`. You can use this to test your
45+
implementation for the first 6 stages.
46+
47+
You can explore this database by running queries against it like this:
48+
49+
```sh
50+
$ sqlite3 sample.db "select id, name from apples"
51+
1|Granny Smith
52+
2|Fuji
53+
3|Honeycrisp
54+
4|Golden Delicious
55+
```
56+
57+
There are two other databases that you can use:
58+
59+
1. `superheroes.db`:
60+
- This is a small version of the test database used in the table-scan stage.
61+
- It contains one table: `superheroes`.
62+
- It is ~1MB in size.
63+
1. `companies.db`:
64+
- This is a small version of the test database used in the index-scan stage.
65+
- It contains one table: `companies`, and one index: `idx_companies_country`
66+
- It is ~7MB in size.
67+
68+
These aren't included in the repository because they're large in size. You can
69+
download them by running this script:
70+
71+
```sh
72+
./download_sample_databases.sh
73+
```
74+
75+
If the script doesn't work for some reason, you can download the databases
76+
directly from
77+
[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 Gleam version used to run your code
8+
# on Codecrafters.
9+
#
10+
# Available versions: gleam-1.0
11+
language_pack: gleam-1.0

compiled_starters/gleam/gleam.toml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name = "sqlite"
2+
version = "1.0.0"
3+
4+
# Fill out these fields if you intend to generate HTML documentation or publish
5+
# your project to the Hex package manager.
6+
#
7+
# description = ""
8+
# licences = ["Apache-2.0"]
9+
# repository = { type = "github", user = "username", repo = "project" }
10+
# links = [{ title = "Website", href = "https://gleam.run" }]
11+
#
12+
# For a full reference of all the available options, you can have a look at
13+
# https://gleam.run/writing-gleam/gleam-toml/.
14+
15+
[dependencies]
16+
gleam_stdlib = "~> 0.34 or ~> 1.0"
17+
gleam_erlang = "~> 0.25"
18+
gleam_otp = "~> 0.10"
19+
glisten = "~> 2.0"
20+
21+
[dev-dependencies]
22+
gleeunit = "~> 1.0"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# This file was generated by Gleam
2+
# You typically do not need to edit this file
3+
4+
packages = [
5+
{ name = "gleam_erlang", version = "0.25.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "054D571A7092D2A9727B3E5D183B7507DAB0DA41556EC9133606F09C15497373" },
6+
{ name = "gleam_otp", version = "0.10.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "0B04FE915ACECE539B317F9652CAADBBC0F000184D586AAAF2D94C100945D72B" },
7+
{ name = "gleam_stdlib", version = "0.36.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "C0D14D807FEC6F8A08A7C9EF8DFDE6AE5C10E40E21325B2B29365965D82EB3D4" },
8+
{ name = "gleeunit", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "72CDC3D3F719478F26C4E2C5FED3E657AC81EC14A47D2D2DEBB8693CA3220C3B" },
9+
{ name = "glisten", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib"], otp_app = "glisten", source = "hex", outer_checksum = "CF3A9383E9BA4A8CBAF2F7B799716290D02F2AC34E7A77556B49376B662B9314" },
10+
]
11+
12+
[requirements]
13+
gleam_erlang = { version = "~> 0.25" }
14+
gleam_otp = { version = "~> 0.10" }
15+
gleam_stdlib = { version = "~> 0.34 or ~> 1.0" }
16+
gleeunit = { version = "~> 1.0" }
17+
glisten = { version = "~> 2.0"}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import gleam/io
2+
import gleam/string
3+
import gleam/int
4+
import gleam/list
5+
import gleam/result
6+
7+
// You can use print statements as follows for debugging, they'll be visible when running tests.
8+
io.println("Logs from your program will appear here!")
9+
10+
// Uncomment this block to pass the first stage
11+
// pub fn main() {
12+
// let args = io.argv()
13+
//
14+
// case args {
15+
// [_, database_file_path, command] -> {
16+
// case command {
17+
// ".dbinfo" -> {
18+
// io.open(database_file_path, io.Read)
19+
// |> result.then(fn(file) {
20+
// io.seek(file, 16)
21+
// |> result.then(fn(_) {
22+
// io.read_bytes(file, 2)
23+
// |> result.then(fn(bytes) {
24+
// let page_size = bytes
25+
// |> list.reverse
26+
// |> int.from_bytes_be
27+
// io.println(string.concat(["database page size: ", int.to_string(page_size)]))
28+
// |> result.map(fn(_) { io.close(file) })
29+
// })
30+
// })
31+
// })
32+
// |> result.unwrap(crash)
33+
// }
34+
// _ -> io.println(string.concat(["Invalid command: ", command]))
35+
// }
36+
// }
37+
// _ -> io.println("Usage: gleam run database_file_path command")
38+
// }
39+
// }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
#
3+
# DON'T EDIT THIS!
4+
#
5+
# CodeCrafters uses this file to test your code. Don't make any changes here!
6+
#
7+
# DON'T EDIT THIS!
8+
exec gleam run -- "$@"

course-definition.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ languages:
4444
- slug: "zig"
4545
- slug: "java"
4646
release_status: "beta"
47+
- slug: "gleam"
48+
release_status: "beta"
4749

4850
marketing:
4951
difficulty: hard

0 commit comments

Comments
 (0)