-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgotables.go
285 lines (245 loc) · 7.47 KB
/
gotables.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
package gotables
import (
"bytes"
"encoding/json"
"errors"
"git.jereileu.ch/gotables/server/gt-server/fs"
"git.jereileu.ch/gotables/server/gt-server/server"
"git.jereileu.ch/gotables/server/gt-server/table"
"io"
"net/http"
"strconv"
)
type Config struct {
fs.Conf
Host string
}
type requestBody = server.Body
type Column = table.Column
type Table = table.Table
type TableU = table.TableU
func ConstructUrl(tbl string, db string, config Config) (string, error) {
out := ""
if config.HTTPSMode {
out += "https://"
} else {
out += "http://"
}
if config.Host != "" {
out += config.Host
} else {
return "", errors.New("invalid url: host needs to be set")
}
if config.Port != "" {
out += config.Port
}
if tbl != "" {
if db != "" {
out += "/"
out += db
out += "/"
out += tbl
} else {
return "", errors.New("invalid url: table set but db is not")
}
} else {
if db != "" {
out += "/"
out += db
}
}
return out, nil
}
func ConstructRequest(body requestBody, url string) (*http.Request, error) {
data, err := json.Marshal(body)
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(data))
return req, err
}
func DoRequest(req *http.Request) (Table, error) {
resp, err := http.DefaultClient.Do(req)
if err != nil {
return Table{}, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return Table{}, err
}
tblU := TableU{}
err = tblU.FromJ(body)
if err != nil {
return Table{}, err
}
tbl, err := tblU.ToT()
return tbl, err
}
func Request(query string, tbl string, db string, sessionId string, config Config) (Table, error) {
url, err := ConstructUrl(tbl, db, config)
if err != nil {
return Table{}, err
}
body := requestBody{
Query: query,
SessionId: sessionId,
}
req, err := ConstructRequest(body, url)
if err != nil {
return Table{}, err
}
return DoRequest(req)
}
func RunServer(config Config) {
server.Run(config.Conf)
}
func TestServer(config Config) error {
url, err := ConstructUrl("", "", config)
if err != nil {
return err
}
_, err = http.Get(url)
return err
}
// Root
func ShowDBs(sessionId string, config Config) (Table, error) {
query := "show"
return Request(query, "", "", sessionId, config)
}
// DB
func ShowTables(db string, sessionId string, config Config) (Table, error) {
query := "show"
return Request(query, "", db, sessionId, config)
}
func CreateDB(db string, sessionId string, config Config) (Table, error) {
query := "create"
return Request(query, "", db, sessionId, config)
}
func SetDBName(name string, db string, sessionId string, config Config) (Table, error) {
query := "set name " + name
return Request(query, "", db, sessionId, config)
}
func CopyDB(name string, db string, sessionId string, config Config) (Table, error) {
query := "copy " + name
return Request(query, "", db, sessionId, config)
}
func DeleteDB(db string, sessionId string, config Config) (Table, error) {
query := "delete"
return Request(query, "", db, sessionId, config)
}
// Table
func ShowTable(tbl string, db string, sessionId string, config Config) (Table, error) {
query := "show"
return Request(query, tbl, db, sessionId, config)
}
func ShowTableColumns(columns []string, tbl string, db string, sessionId string, config Config) (Table, error) {
colNames := ""
for i := 0; i < len(columns); i++ {
colNames += columns[i]
if i != len(columns)-1 {
colNames += ":"
}
}
query := "show " + colNames
return Request(query, tbl, db, sessionId, config)
}
func ShowTableConditions(conditions []string, columns []string, tbl string, db string, sessionId string, config Config) (Table, error) {
colNames := ""
for i := 0; i < len(columns); i++ {
colNames += columns[i]
if i != len(columns)-1 {
colNames += ":"
}
}
conditionString := ""
for i := 0; i < len(conditions); i++ {
conditionString += conditions[i]
if i != len(conditions)-1 {
conditionString += " "
}
}
query := "show " + colNames + " where " + conditionString
return Request(query, tbl, db, sessionId, config)
}
func CreateTable(tbl string, db string, sessionId string, config Config) (Table, error) {
query := "create"
return Request(query, tbl, db, sessionId, config)
}
func SetTableName(name string, tbl string, db string, sessionId string, config Config) (Table, error) {
query := "set name " + name
return Request(query, tbl, db, sessionId, config)
}
func CopyTable(name string, tbl string, db string, sessionId string, config Config) (Table, error) {
query := "copy " + name
return Request(query, tbl, db, sessionId, config)
}
func DeleteTable(tbl string, db string, sessionId string, config Config) (Table, error) {
query := "delete"
return Request(query, tbl, db, sessionId, config)
}
// Column
func ShowColumn(column string, tbl string, db string, sessionId string, config Config) (Table, error) {
query := "column show " + column
return Request(query, tbl, db, sessionId, config)
}
func ShowColumns(columns []string, tbl string, db string, sessionId string, config Config) (Table, error) {
colNames := ""
for i := 0; i < len(columns); i++ {
colNames += columns[i]
if i != len(columns)-1 {
colNames += ":"
}
}
query := "column show " + colNames
return Request(query, tbl, db, sessionId, config)
}
func CreateColumn(column Column, tbl string, db string, sessionId string, config Config) (Table, error) {
query := "column create " + column.Name + ":" + column.Type + ":" + column.Default
return Request(query, tbl, db, sessionId, config)
}
func SetColumnName(name string, column string, tbl string, db string, sessionId string, config Config) (Table, error) {
query := "column set name " + column + " " + name
return Request(query, tbl, db, sessionId, config)
}
func SetColumnDefault(def string, column string, tbl string, db string, sessionId string, config Config) (Table, error) {
query := "column set default " + column + " " + def
return Request(query, tbl, db, sessionId, config)
}
func CopyColumn(name string, column string, tbl string, db string, sessionId string, config Config) (Table, error) {
query := "column copy " + column + " " + name
return Request(query, tbl, db, sessionId, config)
}
func DeleteColumn(column string, tbl string, db string, sessionId string, config Config) (Table, error) {
query := "column delete " + column
return Request(query, tbl, db, sessionId, config)
}
// Row
func ShowRow(row int, tbl string, db string, sessionId string, config Config) (Table, error) {
query := "row show " + strconv.Itoa(row)
return Request(query, tbl, db, sessionId, config)
}
func CreateRow(values [][2]string, tbl string, db string, sessionId string, config Config) (Table, error) {
valueString := ""
for i := 0; i < len(values); i++ {
valueString += values[i][0] + ":" + values[i][1]
if i != len(values)-1 {
valueString += " "
}
}
query := "row create " + valueString
return Request(query, tbl, db, sessionId, config)
}
func SetRow(value string, colName string, row int, tbl string, db string, sessionId string, config Config) (Table, error) {
query := "row set " + strconv.Itoa(row) + ":" + colName + " " + value
return Request(query, tbl, db, sessionId, config)
}
func CopyRow(row int, tbl string, db string, sessionId string, config Config) (Table, error) {
query := "row copy " + strconv.Itoa(row)
return Request(query, tbl, db, sessionId, config)
}
func DeleteRow(row int, tbl string, db string, sessionId string, config Config) (Table, error) {
query := "row delete " + strconv.Itoa(row)
return Request(query, tbl, db, sessionId, config)
}
// User
// Backup