-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcncdb.go
275 lines (252 loc) · 6.75 KB
/
cncdb.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
// Copyright 2019 Tomas Machalek <[email protected]>
// Copyright 2019 Institute of the Czech National Corpus,
//
// Faculty of Arts, Charles University
// This file is part of CNC-MASM.
//
// CNC-MASM 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.
//
// CNC-MASM 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 CNC-MASM. If not, see <https://www.gnu.org/licenses/>.
package cncdb
import (
"database/sql"
"encoding/json"
"fmt"
"masm/v3/corpus"
"masm/v3/liveattrs/qs"
"time"
"github.com/go-sql-driver/mysql"
)
type DefaultViewOpts struct {
Attrs []string `json:"attrs"`
}
type CNCMySQLHandler struct {
conn *sql.DB
corporaTableName string
pcTableName string
}
func (c *CNCMySQLHandler) UpdateSize(transact *sql.Tx, corpus string, size int64) error {
_, err := transact.Exec(
fmt.Sprintf("UPDATE %s SET size = ? WHERE name = ?", c.corporaTableName),
size,
corpus,
)
return err
}
func (c *CNCMySQLHandler) UpdateDefaultViewOpts(transact *sql.Tx, corpus string, defaultViewOpts DefaultViewOpts) error {
data, err := json.Marshal(defaultViewOpts)
if err != nil {
return err
}
_, err = transact.Exec(
fmt.Sprintf("UPDATE %s SET default_view_opts = ? WHERE name = ?", c.corporaTableName),
string(data),
corpus,
)
return err
}
func (c *CNCMySQLHandler) SetLiveAttrs(
transact *sql.Tx,
corpus, bibIDStruct, bibIDAttr string,
) error {
if bibIDAttr != "" && bibIDStruct == "" || bibIDAttr == "" && bibIDStruct != "" {
return fmt.Errorf("SetLiveAttrs requires either both bibIDStruct, bibIDAttr empty or defined")
}
var err error
if bibIDAttr != "" {
_, err = transact.Exec(
fmt.Sprintf(
`UPDATE %s SET text_types_db = 'enabled', bib_id_struct = ?, bib_id_attr = ?
WHERE name = ?`, c.corporaTableName),
bibIDStruct,
bibIDAttr,
corpus,
)
} else {
_, err = transact.Exec(
fmt.Sprintf(
`UPDATE %s SET text_types_db = 'enabled', bib_id_struct = NULL, bib_id_attr = NULL
WHERE name = ?`, c.corporaTableName),
corpus,
)
}
return err
}
func (c *CNCMySQLHandler) UnsetLiveAttrs(transact *sql.Tx, corpus string) error {
_, err := transact.Exec(
fmt.Sprintf(
`UPDATE %s SET text_types_db = NULL, bib_id_struct = NULL, bib_id_attr = NULL
WHERE name = ?`, c.corporaTableName),
corpus,
)
return err
}
func (c *CNCMySQLHandler) UpdateDescription(transact *sql.Tx, corpus, descCs, descEn string) error {
var err error
if descCs != "" {
_, err = transact.Exec(
fmt.Sprintf("UPDATE %s SET description_cs = ? WHERE name = ?", c.corporaTableName),
descCs,
corpus,
)
}
if err != nil {
return err
}
if descEn != "" {
_, err = transact.Exec(
fmt.Sprintf("UPDATE %s SET description_en = ? WHERE name = ?", c.corporaTableName),
descEn,
corpus,
)
}
return err
}
func (c *CNCMySQLHandler) LoadInfo(corpusID string) (*corpus.DBInfo, error) {
var bibLabelStruct, bibLabelAttr, bibIDStruct, bibIDAttr sql.NullString
row := c.conn.QueryRow(
fmt.Sprintf(
"SELECT c.name, c.active, c.bib_label_struct, c.bib_label_attr, "+
" c.bib_id_struct, c.bib_id_attr, c.bib_group_duplicates, c.locale, "+
" p.name, rv.variant "+
"FROM %s AS c "+
"LEFT JOIN %s AS p ON p.id = c.parallel_corpus_id "+
"LEFT JOIN registry_variable AS rv ON rv.corpus_name = c.name "+
" AND rv.variant = 'omezeni' "+
"WHERE c.name = ? LIMIT 1", c.corporaTableName, c.pcTableName),
corpusID)
var ans corpus.DBInfo
var pcName sql.NullString
var locale sql.NullString
var variant sql.NullString
err := row.Scan(
&ans.Name,
&ans.Active,
&bibLabelStruct,
&bibLabelAttr,
&bibIDStruct,
&bibIDAttr,
&ans.BibGroupDuplicates,
&locale,
&pcName,
&variant,
)
if err != nil {
return nil, err
}
if bibLabelStruct.Valid && bibLabelAttr.Valid {
ans.BibLabelAttr = bibLabelStruct.String + "." + bibLabelAttr.String
}
if bibIDStruct.Valid && bibIDAttr.Valid {
ans.BibIDAttr = bibIDStruct.String + "." + bibIDAttr.String
}
if locale.Valid {
ans.Locale = locale.String
}
if pcName.Valid {
ans.ParallelCorpus = pcName.String
}
ans.HasLimitedVariant = variant.Valid
return &ans, nil
}
func (c *CNCMySQLHandler) GetSimpleQueryDefaultAttrs(corpusID string) ([]string, error) {
rows, err := c.conn.Query(
"SELECT pos_attr FROM kontext_simple_query_default_attrs WHERE corpus_name = ?",
corpusID,
)
if err != nil {
return nil, err
}
var attr string
attrs := make([]string, 0)
for rows.Next() {
err := rows.Scan(&attr)
if err != nil {
return nil, err
}
attrs = append(attrs, attr)
}
return attrs, nil
}
func (c *CNCMySQLHandler) GetCorpusTagsets(corpusID string) ([]qs.SupportedTagset, error) {
rows, err := c.conn.Query(
"SELECT tagset_name FROM corpus_tagset WHERE corpus_name = ?",
corpusID,
)
if err != nil {
return nil, fmt.Errorf("failed to get corpus tagsets: %w", err)
}
ans := make([]qs.SupportedTagset, 0, 5)
var val string
for rows.Next() {
err := rows.Scan(&val)
if err != nil {
return nil, fmt.Errorf("failed to get corpus tagsets: %w", err)
}
ans = append(ans, qs.SupportedTagset(val))
}
return ans, nil
}
func (c *CNCMySQLHandler) GetCorpusTagsetAttrs(corpusID string) ([]string, error) {
rows, err := c.conn.Query(
"SELECT pos_attr FROM corpus_tagset WHERE corpus_name = ? and pos_attr IS NOT NULL",
corpusID,
)
if err != nil {
return nil, fmt.Errorf("failed to get corpus tagset attrs: %w", err)
}
var attr string
attrs := make([]string, 0)
for rows.Next() {
err := rows.Scan(&attr)
if err != nil {
return nil, err
}
attrs = append(attrs, attr)
}
return attrs, nil
}
func (c *CNCMySQLHandler) StartTx() (*sql.Tx, error) {
return c.conn.Begin()
}
func (c *CNCMySQLHandler) CommitTx(transact *sql.Tx) error {
return transact.Commit()
}
func (c *CNCMySQLHandler) RollbackTx(transact *sql.Tx) error {
return transact.Rollback()
}
func (c *CNCMySQLHandler) Conn() *sql.DB {
return c.conn
}
func NewCNCMySQLHandler(
host,
user,
pass,
dbName,
corporaTableName,
pcTableName string) (*CNCMySQLHandler, error) {
conf := mysql.NewConfig()
conf.Net = "tcp"
conf.Addr = host
conf.User = user
conf.Passwd = pass
conf.DBName = dbName
conf.ParseTime = true
conf.Loc = time.Local
db, err := sql.Open("mysql", conf.FormatDSN())
if err != nil {
return nil, err
}
return &CNCMySQLHandler{
conn: db, corporaTableName: corporaTableName, pcTableName: pcTableName,
}, nil
}