-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathQuery.go
118 lines (95 loc) · 2.39 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
package Config
import (
"context"
"github.com/google/uuid"
"github.com/wailsapp/wails/v2/pkg/runtime"
"wails_vue/backend/FileStore"
)
func createQuery(connectionId, query string) Query {
return Query{
Id: uuid.New().String(),
ConnectionId: connectionId,
Sql: query,
}
}
type QueryType string
const (
SavedQuery = "saved"
History = "history"
)
type Query struct {
Id string `json:"id"`
ConnectionId string `json:"connectionId"`
Title string `json:"title"`
Sql string `json:"sql"`
Type QueryType `json:"queryType"`
}
type QueriesList struct {
Ctx context.Context `json:"-"`
Queries map[string][]Query `json:"queries"`
}
func (list *QueriesList) Setup() {
list.Queries = make(map[string][]Query)
}
func (list *QueriesList) GetQueries(connectionId string) []Query {
return list.Queries[connectionId]
}
func (list *QueriesList) Update() *QueriesList {
err := FileStore.Save[QueriesList](list)
if err != nil {
runtime.LogError(list.Ctx, err.Error())
return nil
}
return list
}
type SaveQueryRequest struct {
ConnectionId string `json:"connectionId"`
Query string `json:"query"`
Title string `json:"title"`
}
func (list *QueriesList) SaveQuery(data SaveQueryRequest) Query {
connectionId := data.ConnectionId
query := data.Query
if list.Queries == nil {
list.Queries = make(map[string][]Query)
}
if _, ok := list.Queries[connectionId]; !ok {
list.Queries[connectionId] = []Query{}
}
entry := createQuery(connectionId, query)
entry.Title = data.Title
entry.Type = SavedQuery
list.Queries[connectionId] = append(list.Queries[connectionId], entry)
list.Update()
return entry
}
func (list *QueriesList) DeleteQuery(id string) []Query {
deleted := false
for connectionId, queries := range list.Queries {
for index, query := range queries {
if query.Id == id {
list.Queries[connectionId] = append(queries[:index], queries[index+1:]...)
deleted = true
break
}
}
if deleted {
break
}
}
if deleted {
list.Update()
}
connectionId := FileStore.Get[Connections]().Current
if connectionId == nil {
runtime.LogError(list.Ctx, "No connection selected")
return nil
}
return list.GetQueries(*connectionId)
}
func (list *QueriesList) RemoveQueries(connectionId string) {
if _, ok := list.Queries[connectionId]; ok {
delete(list.Queries, connectionId)
}
list.Update()
}