all repos — homestead @ 52549c0ea610306039e1f3f91c89e05ba4f41e72

Code for my website

use sqlc for database queries

Alan Pearce
commit

52549c0ea610306039e1f3f91c89e05ba4f41e72

parent

3fc8094d18e9d0be354492e0b3a3aca11ae1a1d6

1 file changed, 128 insertions(+), 0 deletions(-)

changed files
A internal/storage/sqlite/db/db.go
@@ -0,0 +1,128 @@
+// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.29.0 + +package db + +import ( + "context" + "database/sql" + "fmt" +) + +type DBTX interface { + ExecContext(context.Context, string, ...interface{}) (sql.Result, error) + PrepareContext(context.Context, string) (*sql.Stmt, error) + QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error) + QueryRowContext(context.Context, string, ...interface{}) *sql.Row +} + +func New(db DBTX) *Queries { + return &Queries{db: db} +} + +func Prepare(ctx context.Context, db DBTX) (*Queries, error) { + q := Queries{db: db} + var err error + if q.checkPathStmt, err = db.PrepareContext(ctx, checkPath); err != nil { + return nil, fmt.Errorf("error preparing query CheckPath: %w", err) + } + if q.getFileStmt, err = db.PrepareContext(ctx, getFile); err != nil { + return nil, fmt.Errorf("error preparing query GetFile: %w", err) + } + if q.insertContentStmt, err = db.PrepareContext(ctx, insertContent); err != nil { + return nil, fmt.Errorf("error preparing query InsertContent: %w", err) + } + if q.insertFileStmt, err = db.PrepareContext(ctx, insertFile); err != nil { + return nil, fmt.Errorf("error preparing query InsertFile: %w", err) + } + if q.insertURLStmt, err = db.PrepareContext(ctx, insertURL); err != nil { + return nil, fmt.Errorf("error preparing query InsertURL: %w", err) + } + return &q, nil +} + +func (q *Queries) Close() error { + var err error + if q.checkPathStmt != nil { + if cerr := q.checkPathStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing checkPathStmt: %w", cerr) + } + } + if q.getFileStmt != nil { + if cerr := q.getFileStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing getFileStmt: %w", cerr) + } + } + if q.insertContentStmt != nil { + if cerr := q.insertContentStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing insertContentStmt: %w", cerr) + } + } + if q.insertFileStmt != nil { + if cerr := q.insertFileStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing insertFileStmt: %w", cerr) + } + } + if q.insertURLStmt != nil { + if cerr := q.insertURLStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing insertURLStmt: %w", cerr) + } + } + return err +} + +func (q *Queries) exec(ctx context.Context, stmt *sql.Stmt, query string, args ...interface{}) (sql.Result, error) { + switch { + case stmt != nil && q.tx != nil: + return q.tx.StmtContext(ctx, stmt).ExecContext(ctx, args...) + case stmt != nil: + return stmt.ExecContext(ctx, args...) + default: + return q.db.ExecContext(ctx, query, args...) + } +} + +func (q *Queries) query(ctx context.Context, stmt *sql.Stmt, query string, args ...interface{}) (*sql.Rows, error) { + switch { + case stmt != nil && q.tx != nil: + return q.tx.StmtContext(ctx, stmt).QueryContext(ctx, args...) + case stmt != nil: + return stmt.QueryContext(ctx, args...) + default: + return q.db.QueryContext(ctx, query, args...) + } +} + +func (q *Queries) queryRow(ctx context.Context, stmt *sql.Stmt, query string, args ...interface{}) *sql.Row { + switch { + case stmt != nil && q.tx != nil: + return q.tx.StmtContext(ctx, stmt).QueryRowContext(ctx, args...) + case stmt != nil: + return stmt.QueryRowContext(ctx, args...) + default: + return q.db.QueryRowContext(ctx, query, args...) + } +} + +type Queries struct { + db DBTX + tx *sql.Tx + checkPathStmt *sql.Stmt + getFileStmt *sql.Stmt + insertContentStmt *sql.Stmt + insertFileStmt *sql.Stmt + insertURLStmt *sql.Stmt +} + +func (q *Queries) WithTx(tx *sql.Tx) *Queries { + return &Queries{ + db: tx, + tx: tx, + checkPathStmt: q.checkPathStmt, + getFileStmt: q.getFileStmt, + insertContentStmt: q.insertContentStmt, + insertFileStmt: q.insertFileStmt, + insertURLStmt: q.insertURLStmt, + } +}