all repos — homestead @ 52549c0ea610306039e1f3f91c89e05ba4f41e72

Code for my website

internal/storage/sqlite/db/db.go (view raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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,
	}
}