-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentView.swift
More file actions
187 lines (129 loc) · 5.54 KB
/
ContentView.swift
File metadata and controls
187 lines (129 loc) · 5.54 KB
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
//
// ContentView.swift
// MiniSQL Client
//
// Created by Yuanlin Lin on 2021/6/13.
//
import SwiftUI
import Foundation
import AppKit
struct ContentView: View {
@State private var host: String = "localhost:3306"
@State private var command: String = mockQuery
@State private var data: [[String]] = mockData
@State private var fields: [Attribute] = mockFields
@State private var messages: [Message] = mockMsgs
@State private var tables: [String] = ["student2"]
@State private var indexes: [String] = ["test"]
@State private var showingAlert = false
@State private var alertTitle = ""
@State private var alertMsg = ""
@State private var page = 1;
func handleExecuted(err: String?, res: SQLServerResponse?) {
if(res != nil) {
var tempArray = [Message]()
for item in messages {
tempArray.append(item)
}
guard let res = res else {
return
}
if(res.messages.count > 20) {
tempArray.append(
Message(query: "Info", content: "Skip " + String(res.messages.count - 20) + " messages ...", time: Date())
)
}
for msg in res.messages.count > 20 ? res.messages[0...19] : res.messages[0...res.messages.count-1] {
tempArray.append(
Message(query: msg.command, content: msg.message, time: Date())
)
}
messages = tempArray
data = res.results
fields = res.fields
page = 1
} else {
messages.append(
Message(query: command.replacingOccurrences(of: "\n", with: ""),
content: err ?? "Encountered unexpected error Q_Q",
time: Date()
)
)
alertTitle = "Error Occured"
alertMsg = err ?? "Encountered unexpected error Q_Q"
showingAlert = true
}
}
var body: some View {
HSplitView {
List {
Text("Host")
.foregroundColor(.gray)
TextField("localhost:3306", text: $host)
.padding(.bottom)
Text("Tables")
.foregroundColor(.gray)
ForEach(tables, id: \.self) { table in
Text(table)
.onTapGesture {
command += table
}
}.padding(.bottom)
Text("Indexes")
.foregroundColor(.gray)
ForEach(indexes, id: \.self) { index in
Text(index)
}.padding(.bottom)
}
.listStyle(SidebarListStyle())
VStack(alignment: .leading) {
HStack {
Text("Query")
Button("select File") {
let panel = NSOpenPanel()
panel.allowsMultipleSelection = false
panel.canChooseDirectories = false
if panel.runModal() == .OK {
guard let url = panel.url else {
return
}
do {
command = try String(contentsOf: url, encoding: .utf8)
} catch {
alertTitle = "Error"
alertMsg = "Cannot open this file"
showingAlert = true
}
}
}
}
SQLEditor(command: $command)
HStack{
Spacer()
Button("Execute") {
executeSQL(url: host, query: command, callback: handleExecuted)
}
}
if(data.count > 0) {
Divider().padding()
HStack{
Text("Results")
Text("Total " + String(data.count) + " records").foregroundColor(.gray)
}
DataTable(fields: $fields, data: $data, page: $page)
}
Divider().padding()
HStack {
Text("Logs")
Spacer()
Button("Clear") {
messages = []
}
}
MessageTable(messages: $messages)
}.padding().alert(isPresented: $showingAlert) {
Alert(title: Text(alertTitle), message: Text(alertMsg), dismissButton: .default(Text("Close")))
}
}
}
}