forked from oniony/TMSU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
value.go
308 lines (250 loc) · 5.92 KB
/
value.go
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Copyright 2011-2018 Paul Ruane.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package database
import (
"database/sql"
"github.com/oniony/TMSU/entities"
"strings"
)
// Retrieves the count of values.
func ValueCount(tx *Tx) (uint, error) {
sql := `
SELECT count(1)
FROM value`
rows, err := tx.Query(sql)
if err != nil {
return 0, err
}
defer rows.Close()
return readCount(rows)
}
// Retrieves the complete set of values.
func Values(tx *Tx) (entities.Values, error) {
sql := `
SELECT id, name
FROM value
ORDER BY name`
rows, err := tx.Query(sql)
if err != nil {
return nil, err
}
defer rows.Close()
return readValues(rows, make(entities.Values, 0, 10))
}
// Retrieves a specific value.
func Value(tx *Tx, id entities.ValueId) (*entities.Value, error) {
sql := `
SELECT id, name
FROM value
WHERE id = ?`
rows, err := tx.Query(sql, id)
if err != nil {
return nil, err
}
defer rows.Close()
return readValue(rows)
}
// Retrieves a specific set of values.
func ValuesByIds(tx *Tx, ids entities.ValueIds) (entities.Values, error) {
if len(ids) == 0 {
return make(entities.Values, 0), nil
}
sql := `
SELECT id, name
FROM value
WHERE id IN (?`
sql += strings.Repeat(",?", len(ids)-1)
sql += ")"
params := make([]interface{}, len(ids))
for index, id := range ids {
params[index] = id
}
rows, err := tx.Query(sql, params...)
if err != nil {
return nil, err
}
defer rows.Close()
tags, err := readValues(rows, make(entities.Values, 0, len(ids)))
if err != nil {
return nil, err
}
return tags, nil
}
// Retrieves the set of unused values.
func UnusedValues(tx *Tx) (entities.Values, error) {
sql := `
SELECT id, name
FROM value
WHERE id NOT IN (SELECT distinct(value_id)
FROM file_tag)`
rows, err := tx.Query(sql)
if err != nil {
return nil, err
}
defer rows.Close()
return readValues(rows, make(entities.Values, 0, 10))
}
// Retrieves a specific value by name.
func ValueByName(tx *Tx, name string, ignoreCase bool) (*entities.Value, error) {
collation := collationFor(ignoreCase)
sql := `
SELECT id, name
FROM value
WHERE name ` + collation + ` = ?`
rows, err := tx.Query(sql, name)
if err != nil {
return nil, err
}
defer rows.Close()
return readValue(rows)
}
// Retrieves the set of values with the specified names.
func ValuesByNames(tx *Tx, names []string, ignoreCase bool) (entities.Values, error) {
if len(names) == 0 {
return make(entities.Values, 0), nil
}
collation := collationFor(ignoreCase)
sql := `
SELECT id, name
FROM value
WHERE name ` + collation + ` IN (?`
sql += strings.Repeat(",?", len(names)-1)
sql += ")"
if ignoreCase {
sql += `
COLLATE NOCASE`
}
params := make([]interface{}, len(names))
for index, name := range names {
params[index] = name
}
rows, err := tx.Query(sql, params...)
if err != nil {
return nil, err
}
defer rows.Close()
values, err := readValues(rows, make(entities.Values, 0, len(names)))
if err != nil {
return nil, err
}
return values, nil
}
// Retrieves the set of values for the specified tag.
func ValuesByTagId(tx *Tx, tagId entities.TagId) (entities.Values, error) {
sql := `
SELECT id, name
FROM value
WHERE id IN (SELECT value_id
FROM file_tag
WHERE tag_id = ?1)
ORDER BY name`
rows, err := tx.Query(sql, tagId)
if err != nil {
return nil, err
}
defer rows.Close()
return readValues(rows, make(entities.Values, 0, 10))
}
// Adds a value.
func InsertValue(tx *Tx, name string) (*entities.Value, error) {
sql := `
INSERT INTO value (name)
VALUES (?)`
result, err := tx.Exec(sql, name)
if err != nil {
return nil, err
}
id, err := result.LastInsertId()
if err != nil {
return nil, err
}
rowsAffected, err := result.RowsAffected()
if err != nil {
return nil, err
}
if rowsAffected != 1 {
panic("expected exactly one row to be affected.")
}
return &entities.Value{entities.ValueId(id), name}, nil
}
// Renames a value.
func RenameValue(tx *Tx, valueId entities.ValueId, newName string) (*entities.Value, error) {
sql := `
UPDATE value
SET name = ?
WHERE id = ?`
result, err := tx.Exec(sql, newName, valueId)
if err != nil {
return nil, err
}
rowsAffected, err := result.RowsAffected()
if err != nil {
return nil, err
}
if rowsAffected == 0 {
return nil, NoSuchValueError{valueId}
}
if rowsAffected > 1 {
panic("expected only one row to be affected.")
}
return &entities.Value{valueId, newName}, nil
}
// Deletes a value.
func DeleteValue(tx *Tx, valueId entities.ValueId) error {
sql := `
DELETE FROM value
WHERE id = ?`
result, err := tx.Exec(sql, valueId)
if err != nil {
return err
}
rowsAffected, err := result.RowsAffected()
if err != nil {
return err
}
if rowsAffected == 0 {
return NoSuchValueError{valueId}
}
if rowsAffected > 1 {
panic("expected only one row to be affected.")
}
return nil
}
// unexported
func readValue(rows *sql.Rows) (*entities.Value, error) {
if !rows.Next() {
return nil, nil
}
if rows.Err() != nil {
return nil, rows.Err()
}
var id entities.ValueId
var name string
err := rows.Scan(&id, &name)
if err != nil {
return nil, err
}
return &entities.Value{id, name}, nil
}
func readValues(rows *sql.Rows, values entities.Values) (entities.Values, error) {
for {
value, err := readValue(rows)
if err != nil {
return nil, err
}
if value == nil {
break
}
values = append(values, value)
}
return values, nil
}