Skip to content

Commit a78d985

Browse files
committed
Initial commit
0 parents  commit a78d985

File tree

9 files changed

+171
-0
lines changed

9 files changed

+171
-0
lines changed

.gitattributes

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

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.db

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 Go 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 `app/main.go`. Study and
19+
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 `go (1.16+)` installed locally
34+
1. Run `./your_sqlite3.sh` to run your program, which is implemented in
35+
`app/main.go`.
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).

app/main.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"encoding/binary"
6+
"fmt"
7+
"log"
8+
"os"
9+
// Available if you need it!
10+
// "github.com/xwb1989/sqlparser"
11+
)
12+
13+
// Usage: your_sqlite3.sh sample.db .dbinfo
14+
func main() {
15+
databaseFilePath := os.Args[1]
16+
command := os.Args[2]
17+
18+
switch command {
19+
case ".dbinfo":
20+
databaseFile, err := os.Open(databaseFilePath)
21+
if err != nil {
22+
log.Fatal(err)
23+
}
24+
25+
header := make([]byte, 100)
26+
27+
_, err = databaseFile.Read(header)
28+
if err != nil {
29+
log.Fatal(err)
30+
}
31+
32+
var pageSize uint16
33+
if err := binary.Read(bytes.NewReader(header[16:18]), binary.BigEndian, &pageSize); err != nil {
34+
fmt.Println("Failed to read integer:", err)
35+
return
36+
}
37+
// You can use print statements as follows for debugging, they'll be visible when running tests.
38+
fmt.Println("Logs from your program will appear here!")
39+
40+
// Uncomment this to pass the first stage
41+
// fmt.Printf("database page size: %v", pageSize)
42+
default:
43+
fmt.Println("Unknown command", command)
44+
os.Exit(1)
45+
}
46+
}

codecrafters.yml

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 Go version used to run your code
8+
# on Codecrafters.
9+
#
10+
# Available versions: go-1.22
11+
language_pack: go-1.22

download_sample_databases.sh

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."

go.mod

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// DON'T EDIT THIS!
2+
//
3+
// Codecrafters relies on this file being intact to run tests successfully. Any changes
4+
// here will not reflect when CodeCrafters tests your code, and might even cause build
5+
// failures.
6+
//
7+
// DON'T EDIT THIS!
8+
9+
module github/com/codecrafters-io/sqlite-starter-go
10+
11+
go 1.22
12+
13+
require github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2 h1:zzrxE1FKn5ryBNl9eKOeqQ58Y/Qpo3Q9QNxKHX5uzzQ=
2+
github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2/go.mod h1:hzfGeIUDq/j97IG+FhNqkowIyEcD88LrW6fyU3K3WqY=

your_sqlite3.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
set -e
9+
tmpFile=$(mktemp)
10+
go build -o "$tmpFile" $(dirname "$0")/app/*.go
11+
exec "$tmpFile" "$@"

0 commit comments

Comments
 (0)