HTTPSERVER: Fix 2

This commit is contained in:
2024-09-21 15:30:36 -04:00
parent ab284de163
commit 28cf0f1a93
22 changed files with 410 additions and 0 deletions
+61
View File
@@ -0,0 +1,61 @@
package main
import (
"bytes"
"encoding/binary"
"fmt"
"log"
"os"
"regexp"
// Available if you need it!
// "github.com/xwb1989/sqlparser"
)
// Usage: your_program.sh sample.db .dbinfo
func main() {
databaseFilePath := os.Args[1]
command := os.Args[2]
switch command {
case ".dbinfo":
databaseFile, err := os.Open(databaseFilePath)
if err != nil {
log.Fatal(err)
}
header := make([]byte, 100)
_, err = databaseFile.Read(header)
if err != nil {
log.Fatal(err)
}
var pageSize uint16
if err := binary.Read(bytes.NewReader(header[16:18]), binary.BigEndian, &pageSize); err != nil {
fmt.Println("Failed to read integer:", err)
return
}
// You can use print statements as follows for debugging, they'll be visible when running tests.
fmt.Println("Logs from your program will appear here!")
// Uncomment this to pass the first stage
fmt.Printf("database page size: %v", pageSize)
schemaBuffer := make([]byte, pageSize)
_, err = databaseFile.Read(schemaBuffer)
if err != nil {
log.Fatal(err)
}
re, _ := regexp.Compile("CREATE TABLE")
tables := re.FindAll(schemaBuffer, -1)
fmt.Printf("database page size: %v\n", pageSize)
fmt.Printf("number of tables: %v\n", len(tables))
default:
fmt.Println("Unknown command", command)
os.Exit(1)
}
}