summaryrefslogtreecommitdiffstats
path: root/pkg/database/sqlc/entries.sql.go
blob: eadc25ccb52224ca9d4001a074dd07ad625df163 (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.29.0
// source: entries.sql

package sqlc

import (
	"context"
)

const createEntryWithKindName = `-- name: CreateEntryWithKindName :one
INSERT INTO entries (title, kind, url, description) 
SELECT ?, kinds.id, ?, ?
FROM kinds
WHERE kinds.name = ?
RETURNING id, title, kind, url, description, timestamp
`

type CreateEntryWithKindNameParams struct {
	Title       string `json:"title"`
	Url         string `json:"url"`
	Description string `json:"description"`
	Name        string `json:"name"`
}

func (q *Queries) CreateEntryWithKindName(ctx context.Context, arg CreateEntryWithKindNameParams) (Entry, error) {
	row := q.db.QueryRowContext(ctx, createEntryWithKindName,
		arg.Title,
		arg.Url,
		arg.Description,
		arg.Name,
	)
	var i Entry
	err := row.Scan(
		&i.ID,
		&i.Title,
		&i.Kind,
		&i.Url,
		&i.Description,
		&i.Timestamp,
	)
	return i, err
}

const deleteEntry = `-- name: DeleteEntry :execrows
DELETE FROM entries
WHERE id = ?
`

func (q *Queries) DeleteEntry(ctx context.Context, id int64) (int64, error) {
	result, err := q.db.ExecContext(ctx, deleteEntry, id)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

const getEntries = `-- name: GetEntries :many
SELECT title, url, description, timestamp, kinds.name as kind_name, kinds.emoji as kind_emoji FROM entries
JOIN kinds ON entries.kind == kinds.id
ORDER BY timestamp DESC
`

type GetEntriesRow struct {
	Title       string `json:"title"`
	Url         string `json:"url"`
	Description string `json:"description"`
	Timestamp   string `json:"timestamp"`
	KindName    string `json:"kind_name"`
	KindEmoji   string `json:"kind_emoji"`
}

func (q *Queries) GetEntries(ctx context.Context) ([]GetEntriesRow, error) {
	rows, err := q.db.QueryContext(ctx, getEntries)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GetEntriesRow
	for rows.Next() {
		var i GetEntriesRow
		if err := rows.Scan(
			&i.Title,
			&i.Url,
			&i.Description,
			&i.Timestamp,
			&i.KindName,
			&i.KindEmoji,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getEntriesByKind = `-- name: GetEntriesByKind :many
SELECT title, url, description, timestamp, kinds.name as kind_name, kinds.emoji as kind_emoji FROM entries
JOIN kinds ON entries.kind == kinds.id
WHERE kinds.name = ?
ORDER BY timestamp DESC
`

type GetEntriesByKindRow struct {
	Title       string `json:"title"`
	Url         string `json:"url"`
	Description string `json:"description"`
	Timestamp   string `json:"timestamp"`
	KindName    string `json:"kind_name"`
	KindEmoji   string `json:"kind_emoji"`
}

func (q *Queries) GetEntriesByKind(ctx context.Context, name string) ([]GetEntriesByKindRow, error) {
	rows, err := q.db.QueryContext(ctx, getEntriesByKind, name)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GetEntriesByKindRow
	for rows.Next() {
		var i GetEntriesByKindRow
		if err := rows.Scan(
			&i.Title,
			&i.Url,
			&i.Description,
			&i.Timestamp,
			&i.KindName,
			&i.KindEmoji,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getEntryByUrl = `-- name: GetEntryByUrl :one
SELECT entries.id, title, url, description, timestamp, kinds.name as kind_name, kinds.emoji as kind_emoji FROM entries
JOIN kinds ON entries.kind == kinds.id
WHERE url = ?
LIMIT 1
`

type GetEntryByUrlRow struct {
	ID          int64  `json:"id"`
	Title       string `json:"title"`
	Url         string `json:"url"`
	Description string `json:"description"`
	Timestamp   string `json:"timestamp"`
	KindName    string `json:"kind_name"`
	KindEmoji   string `json:"kind_emoji"`
}

func (q *Queries) GetEntryByUrl(ctx context.Context, url string) (GetEntryByUrlRow, error) {
	row := q.db.QueryRowContext(ctx, getEntryByUrl, url)
	var i GetEntryByUrlRow
	err := row.Scan(
		&i.ID,
		&i.Title,
		&i.Url,
		&i.Description,
		&i.Timestamp,
		&i.KindName,
		&i.KindEmoji,
	)
	return i, err
}

const getEntryURLs = `-- name: GetEntryURLs :many
SELECT url FROM entries
ORDER BY timestamp DESC
`

func (q *Queries) GetEntryURLs(ctx context.Context) ([]string, error) {
	rows, err := q.db.QueryContext(ctx, getEntryURLs)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []string
	for rows.Next() {
		var url string
		if err := rows.Scan(&url); err != nil {
			return nil, err
		}
		items = append(items, url)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const updateEntryKind = `-- name: UpdateEntryKind :one
UPDATE entries
SET kind = (SELECT id FROM kinds WHERE kinds.name = ?)
WHERE entries.id = ? 
RETURNING id, title, kind, url, description, timestamp
`

type UpdateEntryKindParams struct {
	Name string `json:"name"`
	ID   int64  `json:"id"`
}

func (q *Queries) UpdateEntryKind(ctx context.Context, arg UpdateEntryKindParams) (Entry, error) {
	row := q.db.QueryRowContext(ctx, updateEntryKind, arg.Name, arg.ID)
	var i Entry
	err := row.Scan(
		&i.ID,
		&i.Title,
		&i.Kind,
		&i.Url,
		&i.Description,
		&i.Timestamp,
	)
	return i, err
}