-
Notifications
You must be signed in to change notification settings - Fork 115
/
implication.go
267 lines (220 loc) · 6.89 KB
/
implication.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
// 2011-2015 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"
)
// Retrieves the complete set of tag implications.
func Implications(tx *Tx) (entities.Implications, error) {
sql := `
SELECT tag.id, tag.name,
value.id, value.name,
implied_tag.id, implied_tag.name,
implied_value.id, implied_value.name
FROM implication
INNER JOIN tag tag ON implication.tag_id = tag.id
LEFT OUTER JOIN value value ON implication.value_id = value.id
INNER JOIN tag implied_tag ON implication.implied_tag_id = implied_tag.id
LEFT OUTER JOIN value implied_value ON implication.implied_value_id = implied_value.id
ORDER BY tag.name, value.name, implied_tag.name, implied_value.name`
rows, err := tx.Query(sql)
if err != nil {
return nil, err
}
defer rows.Close()
implications, err := readImplications(rows, make(entities.Implications, 0, 10))
if err != nil {
return nil, err
}
return implications, nil
}
// Retrieves the set of implications by the specified tag and value pairs.
func ImplicationsFor(tx *Tx, pairs entities.TagIdValueIdPairs) (entities.Implications, error) {
builder := NewBuilder()
builder.AppendSql(`
SELECT tag.id, tag.name,
value.id, value.name,
implied_tag.id, implied_tag.name,
implied_value.id, implied_value.name
FROM implication
INNER JOIN tag tag ON implication.tag_id = tag.id
LEFT OUTER JOIN value value ON implication.value_id = value.id
INNER JOIN tag implied_tag ON implication.implied_tag_id = implied_tag.id
LEFT OUTER JOIN value implied_value ON implication.implied_value_id = implied_value.id
WHERE `)
for index, pair := range pairs {
if index > 0 {
builder.AppendSql(" OR ")
}
builder.AppendSql("(implication.tag_id = ")
builder.AppendParam(pair.TagId)
builder.AppendSql(" AND implication.value_id IN (0, ")
builder.AppendParam(pair.ValueId)
builder.AppendSql("))")
}
builder.AppendSql(`ORDER BY tag.name, value.name, implied_tag.name, implied_value.name`)
rows, err := tx.Query(builder.Sql(), builder.Params()...)
if err != nil {
return nil, err
}
defer rows.Close()
implications, err := readImplications(rows, make(entities.Implications, 0, 10))
if err != nil {
return nil, err
}
return implications, nil
}
func ImplyingImplications(tx *Tx, pairs entities.TagIdValueIdPairs) (entities.Implications, error) {
sql := `
SELECT tag.id, tag.name,
value.id, value.name,
implying_tag.id, implying_tag.name,
implying_value.id, implying_value.name
FROM implication
INNER JOIN tag tag ON implication.tag_id = tag.id
LEFT OUTER JOIN value value ON implication.value_id = value.id
INNER JOIN tag implying_tag ON implication.implying_tag_id = implying_tag.id
LEFT OUTER JOIN value implying_value ON implication.implying_value_id = implying_value.id
WHERE `
params := make([]interface{}, len(pairs)*2)
for index, pair := range pairs {
if index > 0 {
sql += " OR "
}
sql += "(implication.implied_tag_id = ? AND implication.implied_value_id = ?)"
params[index*2] = pair.TagId
params[index*2+1] = pair.ValueId
}
sql += `
ORDER BY tag.name, value.name, implying_tag.name, implying_value.name`
rows, err := tx.Query(sql, params...)
if err != nil {
return nil, err
}
defer rows.Close()
implications, err := readImplications(rows, make(entities.Implications, 0, 10))
if err != nil {
return nil, err
}
return implications, nil
}
// Adds the specified implications
func AddImplication(tx *Tx, pair, impliedPair entities.TagIdValueIdPair) error {
sql := `
INSERT OR IGNORE INTO implication (tag_id, value_id, implied_tag_id, implied_value_id)
VALUES (?1, ?2, ?3, ?4)`
_, err := tx.Exec(sql, pair.TagId, pair.ValueId, impliedPair.TagId, impliedPair.ValueId)
if err != nil {
return err
}
return nil
}
// Deletes the specified implication
func DeleteImplication(tx *Tx, pair, impliedPair entities.TagIdValueIdPair) error {
sql := `
DELETE FROM implication
WHERE tag_id = ?1 AND
value_id = ?2 AND
implied_tag_id = ?3 AND
implied_value_id = ?4`
result, err := tx.Exec(sql, pair.TagId, pair.ValueId, impliedPair.TagId, impliedPair.ValueId)
if err != nil {
return err
}
rowsAffected, err := result.RowsAffected()
if err != nil {
return err
}
if rowsAffected == 0 {
return NoSuchImplicationError{pair, impliedPair}
}
if rowsAffected > 1 {
panic("expected exactly one row to be affected")
}
return nil
}
// Deletes implications for the specified tag id
func DeleteImplicationsByTagId(tx *Tx, tagId entities.TagId) error {
sql := `
DELETE FROM implication
WHERE tag_id = ?1 OR implied_tag_id = ?1`
_, err := tx.Exec(sql, tagId)
if err != nil {
return err
}
return nil
}
// Deletes implications for the specified value id
func DeleteImplicationsByValueId(tx *Tx, valueId entities.ValueId) error {
sql := `
DELETE FROM implication
WHERE value_id = ?1 OR implied_value_id = ?1`
_, err := tx.Exec(sql, valueId)
if err != nil {
return err
}
return nil
}
// unexported
func readImplication(rows *sql.Rows) (*entities.Implication, error) {
if !rows.Next() {
return nil, nil
}
if rows.Err() != nil {
return nil, rows.Err()
}
var implyingTagId entities.TagId
var implyingTagName string
var implyingValueId *entities.ValueId
var implyingValueName *string
var impliedTagId entities.TagId
var impliedTagName string
var impliedValueId *entities.ValueId
var impliedValueName *string
err := rows.Scan(&implyingTagId,
&implyingTagName,
&implyingValueId,
&implyingValueName,
&impliedTagId,
&impliedTagName,
&impliedValueId,
&impliedValueName)
if err != nil {
return nil, err
}
var implyingValue entities.Value
if implyingValueId != nil {
implyingValue = entities.Value{*implyingValueId, *implyingValueName}
}
var impliedValue entities.Value
if impliedValueId != nil {
impliedValue = entities.Value{*impliedValueId, *impliedValueName}
}
return &entities.Implication{entities.Tag{implyingTagId, implyingTagName},
implyingValue,
entities.Tag{impliedTagId, impliedTagName},
impliedValue}, nil
}
func readImplications(rows *sql.Rows, implications entities.Implications) (entities.Implications, error) {
for {
implication, err := readImplication(rows)
if err != nil {
return nil, err
}
if implication == nil {
break
}
implications = append(implications, implication)
}
return implications, nil
}