-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
332 lines (277 loc) · 7.83 KB
/
query.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Copyright 2017 The Yugur RESTful API Authors. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package main
import (
"database/sql"
d "github.com/yugur/api/entry"
)
//---------------------------------------------------------
//---- Search Queries
//---------------------------------------------------------
func index() ([]*d.Entry, error) {
query := `SELECT *
FROM entries`
rows, err := db.Query(query)
if err != nil {
return nil, err
}
defer rows.Close()
entries, err := scanRows(rows)
if err != nil {
return entries, err
}
return entries, nil
}
// idSearch returns the matching entries for each id.
// Raises sql.ErrNoRows if there is no matching entry for any provided id.
func idSearch(ids ...string) ([]*d.Entry, error) {
var errNoRows error
entries := make([]*d.Entry, 0)
for _, id := range ids {
e := new(d.Entry)
row := db.QueryRow("SELECT * FROM entries WHERE entry_id = $1", id)
err := row.Scan(&e.ID, &e.Headword, &e.Wordtype, &e.Definition, &e.Headword_Language, &e.Definition_Language)
if err != nil {
errNoRows = err
continue
}
entries = append(entries, e)
}
return entries, errNoRows
}
func headwordSearch(word string) ([]*d.Entry, error) {
var query string
if len(word) == 1 {
query = `SELECT * FROM entries
WHERE SUBSTRING(LOWER(headword), 1, 1) = LOWER($1)`
} else {
query = `SELECT * FROM entries
WHERE headword = $1`
}
rows, err := db.Query(query, word)
if err != nil {
return nil, err
}
defer rows.Close()
entries, err := scanRows(rows)
if err != nil {
return entries, err
}
return entries, nil
}
func tagSearch(tag string) ([]*d.Entry, error) {
tagID, err := getTagID(tag)
if err != nil {
return nil, err
}
rows, err := db.Query("SELECT entries.entry_id, entries.headword, entries.wordtype, entries.definition, entries.hw_lang, entries.def_lang FROM (SELECT * FROM entry_tags WHERE tag_id = $1) AS entry_tags JOIN entries ON entry_tags.entry_id = entries.entry_id", tagID)
if err != nil {
return nil, err
}
defer rows.Close()
entries, err := scanRows(rows)
if err != nil {
return entries, err
}
return entries, nil
}
func wordtypeSearch(wordtype string) ([]*d.Entry, error) {
id, err := getWordtypeID(wordtype)
if err != nil {
return nil, err
}
query := `SELECT * FROM entries
WHERE wordtype = $1`
rows, err := db.Query(query, id)
if err != nil {
return nil, err
}
defer rows.Close()
entries, err := scanRows(rows)
if err != nil {
return entries, err
}
return entries, nil
}
func definitionSearch(token string) ([]*d.Entry, error) {
if token == "" {
return nil, nil
}
query := `SELECT * FROM entries
WHERE STRPOS(definition, $1) > 0`
rows, err := db.Query(query, token)
if err != nil {
return nil, err
}
defer rows.Close()
entries, err := scanRows(rows)
if err != nil {
return entries, err
}
return entries, nil
}
//---------------------------------------------------------
//---- Executable Queries
//---------------------------------------------------------
func insertEntry(entries ...*d.Entry) (int64, error) {
var rowsAffected int64
for _, entry := range entries {
var query string
var result sql.Result
var err error
if entry.ID == "" {
query = `INSERT INTO entries (headword, wordtype, definition, hw_lang, def_lang)
VALUES($1, $2, $3, $4, $5)`
result, err = db.Exec(
query,
entry.Headword,
entry.Wordtype,
entry.Definition,
entry.Headword_Language,
entry.Definition_Language)
} else {
query = `UPDATE entries
SET headword = $1, wordtype = $2, definition = $3, hw_lang = $4, def_lang = $5
WHERE entry_id = $6`
result, err = db.Exec(
query,
entry.Headword,
entry.Wordtype,
entry.Definition,
entry.Headword_Language,
entry.Definition_Language,
entry.ID)
}
if err != nil {
return rowsAffected, err
}
r, err := result.RowsAffected()
if err != nil {
return rowsAffected, err
}
rowsAffected += r
}
return rowsAffected, nil
}
func deleteEntry(ids ...string) (int64, error) {
var rowsAffected int64
for _, id := range ids {
query := `DELETE FROM entries
WHERE entry_id = $1`
result, err := db.Exec(query, id)
if err != nil {
return rowsAffected, err
}
r, err := result.RowsAffected()
if err != nil {
return rowsAffected, err
}
rowsAffected += r
}
return rowsAffected, nil
}
//---------------------------------------------------------
//---- Helper Functions
//---------------------------------------------------------
func getTagID(tag string) (string, error) {
var result string
row := db.QueryRow("SELECT tag_id FROM tags WHERE name = $1", tag)
err := row.Scan(&result)
return result, err
}
func getTagName(id string) (string, error) {
var result string
row := db.QueryRow("SELECT name FROM tags WHERE tag_id = $1", id)
err := row.Scan(&result)
return result, err
}
func getWordtypeID(name string) (string, error) {
var result string
row := db.QueryRow("SELECT wordtype_id FROM wordtypes WHERE name = $1", name)
err := row.Scan(&result)
return result, err
}
func getWordtypeName(id string) (string, error) {
var result string
row := db.QueryRow("SELECT name FROM wordtypes WHERE wordtype_id = $1", id)
err := row.Scan(&result)
return result, err
}
func getLocaleID(code string) (string, error) {
var result string
row := db.QueryRow("SELECT lang_id FROM languages WHERE code = $1", code)
err := row.Scan(&result)
return result, err
}
func getLocaleCode(id string) (string, error) {
var result string
row := db.QueryRow("SELECT code FROM languages WHERE lang_id = $1", id)
err := row.Scan(&result)
return result, err
}
// Given a variadic d.Entry(s) with database identifiers,
// returns list of same entries with human names instead
func asOutgoing(entries ...*d.Entry) ([]*d.Entry, error) {
for _, entry := range entries {
wordtype, err := getWordtypeName(entry.Wordtype)
if err != nil {
return entries, err
}
headwordLanguage, err := getLocaleCode(entry.Headword_Language)
if err != nil {
return entries, err
}
definitionLanguage, err := getLocaleCode(entry.Definition_Language)
if err != nil {
return entries, err
}
entry.Wordtype = wordtype
entry.Headword_Language = headwordLanguage
entry.Definition_Language = definitionLanguage
}
return entries, nil
}
// Given a variadic d.Entry(s) with human names,
// returns list of same entries with database identifiers instead
func asIncoming(entries ...*d.Entry) ([]*d.Entry, error) {
for _, entry := range entries {
wordtype, err := getWordtypeID(entry.Wordtype)
if err != nil {
return entries, err
}
headwordLanguage, err := getLocaleID(entry.Headword_Language)
if err != nil {
return entries, err
}
definitionLanguage, err := getLocaleID(entry.Definition_Language)
if err != nil {
return entries, err
}
entry.Wordtype = wordtype
entry.Headword_Language = headwordLanguage
entry.Definition_Language = definitionLanguage
}
return entries, nil
}
func scanRows(rows *sql.Rows) ([]*d.Entry, error) {
var entries []*d.Entry
for rows.Next() {
entry := new(d.Entry)
err := rows.Scan(
&entry.ID,
&entry.Headword,
&entry.Wordtype,
&entry.Definition,
&entry.Headword_Language,
&entry.Definition_Language)
if err != nil {
return entries, err
}
entries = append(entries, entry)
}
if err := rows.Err(); err != nil {
return entries, err
}
return entries, nil
}