all repos — homestead @ 52549c0ea610306039e1f3f91c89e05ba4f41e72

Code for my website

use sqlc for database queries

Alan Pearce
commit

52549c0ea610306039e1f3f91c89e05ba4f41e72

parent

3fc8094d18e9d0be354492e0b3a3aca11ae1a1d6

1 file changed, 26 insertions(+), 84 deletions(-)

changed files
M internal/storage/sqlite/writer.gointernal/storage/sqlite/writer.go
@@ -1,18 +1,21 @@
package sqlite import ( + "context" "database/sql" "fmt" "hash/fnv" "io" "mime" "net/http" + "os" "path/filepath" "time" "alin.ovh/homestead/internal/buffer" "alin.ovh/homestead/internal/content" "alin.ovh/homestead/internal/storage" + "alin.ovh/homestead/internal/storage/sqlite/db" "alin.ovh/x/log" "github.com/andybalholm/brotli" "github.com/klauspost/compress/gzip"
@@ -26,15 +29,9 @@
var encodings = []string{"gzip", "br", "zstd"} type Writer struct { - db *sql.DB - options *Options log *log.Logger - queries struct { - insertURL *sql.Stmt - insertFile *sql.Stmt - insertContent *sql.Stmt - } + queries *db.Queries } type Options struct {
@@ -58,69 +55,23 @@
return db, nil } -func NewWriter(db *sql.DB, logger *log.Logger, opts *Options) (*Writer, error) { - _, err := db.Exec(` - CREATE TABLE IF NOT EXISTS url ( - url_id INTEGER PRIMARY KEY, - path TEXT NOT NULL - ) STRICT; - CREATE UNIQUE INDEX IF NOT EXISTS url_path - ON url (path); +func NewWriter(conn *sql.DB, logger *log.Logger, opts *Options) (*Writer, error) { + q, err := os.ReadFile("schema.sql") + if err != nil { + return nil, fault.Wrap(err) + } - CREATE TABLE IF NOT EXISTS file ( - file_id INTEGER PRIMARY KEY, - url_id INTEGER NOT NULL, - content_type TEXT NOT NULL, - last_modified INTEGER NOT NULL, - title TEXT NOT NULL, - etag TEXT NOT NULL, - style_hash TEXT NOT NULL, - FOREIGN KEY (url_id) REFERENCES url (url_id) - ) STRICT; - CREATE UNIQUE INDEX IF NOT EXISTS file_url_content_type - ON file (url_id, content_type); - - CREATE TABLE IF NOT EXISTS content ( - content_id INTEGER PRIMARY KEY, - file_id INTEGER NOT NULL, - encoding TEXT NOT NULL, - body BLOB NOT NULL, - FOREIGN KEY (file_id) REFERENCES file (file_id) - ) STRICT; - CREATE UNIQUE INDEX IF NOT EXISTS file_content - ON content (file_id, encoding); - `) + _, err = conn.Exec(string(q)) if err != nil { return nil, fault.Wrap(err, fmsg.With("creating tables")) } w := &Writer{ - db: db, + queries: db.New(conn), log: logger, options: opts, } - w.queries.insertURL, err = db.Prepare(`INSERT INTO url (path) VALUES (?)`) - if err != nil { - return nil, fault.Wrap(err, fmsg.With("preparing insert URL statement")) - } - - w.queries.insertFile, err = db.Prepare(` - INSERT INTO file (url_id, content_type, last_modified, etag, style_hash, title) - VALUES (:url_id, :content_type, :last_modified, :etag, :style_hash, :title) - `) - if err != nil { - return nil, fault.Wrap(err, fmsg.With("preparing insert file statement")) - } - - w.queries.insertContent, err = db.Prepare(` - INSERT INTO content (file_id, encoding, body) - VALUES (:file_id, :encoding, :body) - `) - if err != nil { - return nil, fault.Wrap(err, fmsg.With("preparing insert content statement")) - } - return w, nil }
@@ -129,16 +80,11 @@ return nil
} func (s *Writer) storeURL(path string) (int64, error) { - r, err := s.queries.insertURL.Exec(path) + id, err := s.queries.InsertURL(context.TODO(), path) if err != nil { return 0, fault.Wrap(err, fmsg.With(fmt.Sprintf("inserting URL %s into database", path))) } - id, err := r.LastInsertId() - if err != nil { - return 0, fault.Wrap(err) - } - return id, nil }
@@ -153,32 +99,28 @@ "sniffed",
file.ContentType, ) } - r, err := s.queries.insertFile.Exec( - sql.Named("url_id", urlID), - sql.Named("content_type", file.ContentType), - sql.Named("last_modified", file.LastModified.Unix()), - sql.Named("etag", file.Etag), - sql.Named("style_hash", file.StyleHash), - sql.Named("title", file.Title), - ) + params := db.InsertFileParams{ + UrlID: urlID, + ContentType: file.ContentType, + LastModified: file.LastModified.Unix(), + Etag: file.Etag, + StyleHash: file.StyleHash, + Title: file.Title, + } + id, err := s.queries.InsertFile(context.TODO(), params) if err != nil { return 0, fault.Wrap(err, fmsg.With("inserting file into database")) } - id, err := r.LastInsertId() - if err != nil { - return 0, fault.Wrap(err) - } - return id, nil } func (s *Writer) storeEncoding(fileID int64, encoding string, data []byte) error { - _, err := s.queries.insertContent.Exec( - sql.Named("file_id", fileID), - sql.Named("encoding", encoding), - sql.Named("body", data), - ) + err := s.queries.InsertContent(context.TODO(), db.InsertContentParams{ + Fileid: fileID, + Encoding: encoding, + Body: data, + }) if err != nil { return fault.Wrap( err,