-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument_versions.sql.go
More file actions
225 lines (198 loc) · 5.73 KB
/
document_versions.sql.go
File metadata and controls
225 lines (198 loc) · 5.73 KB
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
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.30.0
// source: document_versions.sql
package database
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const createDocument = `-- name: CreateDocument :one
INSERT INTO documents (name, current_version)
VALUES ($1, 1)
RETURNING id, name, current_version, created_at
`
// Document Queries
func (q *Queries) CreateDocument(ctx context.Context, name string) (Document, error) {
row := q.db.QueryRow(ctx, createDocument, name)
var i Document
err := row.Scan(
&i.ID,
&i.Name,
&i.CurrentVersion,
&i.CreatedAt,
)
return i, err
}
const createDocumentVersion = `-- name: CreateDocumentVersion :one
INSERT INTO document_versions (document_id, version, patch, inverted_patch)
VALUES ($1, $2, $3, $4)
RETURNING document_id, version, patch, inverted_patch, created_at
`
type CreateDocumentVersionParams struct {
DocumentID pgtype.UUID `json:"document_id"`
Version int32 `json:"version"`
Patch []byte `json:"patch"`
InvertedPatch []byte `json:"inverted_patch"`
}
// Document Version Queries
func (q *Queries) CreateDocumentVersion(ctx context.Context, arg CreateDocumentVersionParams) (DocumentVersion, error) {
row := q.db.QueryRow(ctx, createDocumentVersion,
arg.DocumentID,
arg.Version,
arg.Patch,
arg.InvertedPatch,
)
var i DocumentVersion
err := row.Scan(
&i.DocumentID,
&i.Version,
&i.Patch,
&i.InvertedPatch,
&i.CreatedAt,
)
return i, err
}
const deleteDocument = `-- name: DeleteDocument :exec
DELETE FROM documents
WHERE id = $1
`
func (q *Queries) DeleteDocument(ctx context.Context, id pgtype.UUID) error {
_, err := q.db.Exec(ctx, deleteDocument, id)
return err
}
const getDocument = `-- name: GetDocument :one
SELECT id, name, current_version, created_at
FROM documents
WHERE id = $1
`
func (q *Queries) GetDocument(ctx context.Context, id pgtype.UUID) (Document, error) {
row := q.db.QueryRow(ctx, getDocument, id)
var i Document
err := row.Scan(
&i.ID,
&i.Name,
&i.CurrentVersion,
&i.CreatedAt,
)
return i, err
}
const getDocumentVersion = `-- name: GetDocumentVersion :one
SELECT document_id, version, patch, inverted_patch, created_at
FROM document_versions
WHERE document_id = $1 AND version = $2
`
type GetDocumentVersionParams struct {
DocumentID pgtype.UUID `json:"document_id"`
Version int32 `json:"version"`
}
// Retrieve a specific version record for a document
// Parameters: document_id (UUID), version (INT)
// Returns: document_id, version, patch, inverted_patch, created_at
func (q *Queries) GetDocumentVersion(ctx context.Context, arg GetDocumentVersionParams) (DocumentVersion, error) {
row := q.db.QueryRow(ctx, getDocumentVersion, arg.DocumentID, arg.Version)
var i DocumentVersion
err := row.Scan(
&i.DocumentID,
&i.Version,
&i.Patch,
&i.InvertedPatch,
&i.CreatedAt,
)
return i, err
}
const getDocumentVersions = `-- name: GetDocumentVersions :many
SELECT document_id, version, patch, inverted_patch, created_at
FROM document_versions
WHERE document_id = $1
ORDER BY version ASC
`
// Retrieve all versions for a document, ordered by version ascending
// This is needed to reconstruct the document at any version
// Parameters: document_id (UUID)
// Returns: all columns, ordered by version ASC
func (q *Queries) GetDocumentVersions(ctx context.Context, documentID pgtype.UUID) ([]DocumentVersion, error) {
rows, err := q.db.Query(ctx, getDocumentVersions, documentID)
if err != nil {
return nil, err
}
defer rows.Close()
items := []DocumentVersion{}
for rows.Next() {
var i DocumentVersion
if err := rows.Scan(
&i.DocumentID,
&i.Version,
&i.Patch,
&i.InvertedPatch,
&i.CreatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getLatestVersion = `-- name: GetLatestVersion :one
SELECT MAX(version) as version
FROM document_versions
WHERE document_id = $1
`
// Get the latest version number for a document
// Parameters: document_id (UUID)
// Returns: the maximum version number
// Hint: Use MAX() aggregate function
func (q *Queries) GetLatestVersion(ctx context.Context, documentID pgtype.UUID) (interface{}, error) {
row := q.db.QueryRow(ctx, getLatestVersion, documentID)
var version interface{}
err := row.Scan(&version)
return version, err
}
const getVersionHistory = `-- name: GetVersionHistory :many
SELECT version, created_at
FROM document_versions
WHERE document_id = $1
ORDER BY version DESC
`
type GetVersionHistoryRow struct {
Version int32 `json:"version"`
CreatedAt pgtype.Timestamp `json:"created_at"`
}
// Retrieve version metadata (version number and created_at) for displaying history
// Parameters: document_id (UUID)
// Returns: version, created_at, ordered by version DESC (newest first)
func (q *Queries) GetVersionHistory(ctx context.Context, documentID pgtype.UUID) ([]GetVersionHistoryRow, error) {
rows, err := q.db.Query(ctx, getVersionHistory, documentID)
if err != nil {
return nil, err
}
defer rows.Close()
items := []GetVersionHistoryRow{}
for rows.Next() {
var i GetVersionHistoryRow
if err := rows.Scan(&i.Version, &i.CreatedAt); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const updateDocumentVersion = `-- name: UpdateDocumentVersion :exec
UPDATE documents
SET current_version = $2
WHERE id = $1
`
type UpdateDocumentVersionParams struct {
ID pgtype.UUID `json:"id"`
CurrentVersion int32 `json:"current_version"`
}
func (q *Queries) UpdateDocumentVersion(ctx context.Context, arg UpdateDocumentVersionParams) error {
_, err := q.db.Exec(ctx, updateDocumentVersion, arg.ID, arg.CurrentVersion)
return err
}