Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit 9e8f600

Browse files
committed
webui: Add a clear history button to the execute SQL page
This adds a button to delete the history of recently executed SQL commands in the execute SQL page. The history is deleted on the server and on the client side.
1 parent d48dc02 commit 9e8f600

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

webui/execute.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,43 @@ func executePage(w http.ResponseWriter, r *http.Request) {
8787
}
8888
}
8989

90+
// execClearHistory deletes all items in the user's SQL history
91+
func execClearHistory(w http.ResponseWriter, r *http.Request) {
92+
// Retrieve session data (if any)
93+
var loggedInUser string
94+
var u interface{}
95+
var err error
96+
if com.Conf.Environment.Environment == "production" {
97+
sess, err := store.Get(r, "dbhub-user")
98+
if err != nil {
99+
w.WriteHeader(http.StatusBadRequest)
100+
return
101+
}
102+
u = sess.Values["UserName"]
103+
} else {
104+
u = com.Conf.Environment.UserOverride
105+
}
106+
if u != nil {
107+
loggedInUser = u.(string)
108+
}
109+
110+
// Retrieve user and database info
111+
dbOwner, dbName, _, err := com.GetODC(2, r) // 2 = Ignore "/x/execlivesql/" at the start of the URL
112+
if err != nil {
113+
w.WriteHeader(http.StatusBadRequest)
114+
fmt.Fprint(w, err)
115+
return
116+
}
117+
118+
// Delete items
119+
err = com.LiveSqlHistoryDeleteOld(loggedInUser, dbOwner, dbName, 0) // 0 means "keep 0 items"
120+
if err != nil {
121+
w.WriteHeader(http.StatusInternalServerError)
122+
fmt.Fprint(w, err)
123+
return
124+
}
125+
}
126+
90127
// execLiveSQL executes a user provided SQLite statement on a database.
91128
func execLiveSQL(w http.ResponseWriter, r *http.Request) {
92129
// Retrieve session data (if any)

webui/jsx/sql-terminal.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,20 @@ export default function SqlTerminal() {
172172
}
173173
}
174174

175+
// Clears the SQL history
176+
function clearHistory() {
177+
fetch("/x/execclearhistory/" + meta.owner + "/" + meta.database, {
178+
method: "post",
179+
}).then(response => {
180+
if (!response.ok) {
181+
return Promise.reject(response);
182+
}
183+
184+
setRecentCommands([]);
185+
}).catch(error => {
186+
});
187+
}
188+
175189
// This effect makes sure we scroll to the bottom of the command history whenever a new entry is added
176190
const performScrolldown = React.useRef(false);
177191
React.useEffect(() => {
@@ -224,6 +238,8 @@ export default function SqlTerminal() {
224238
<li><a href="#" onClick={() => setExecuteOnEnter(!executeOnEnter)}><input type="checkbox" checked={executeOnEnter ? "checked" : null} /> Execute on Enter</a></li>
225239
<li role="separator" className="divider"></li>
226240
<li><a href="#" onClick={() => formatSql()} data-cy="formatbtn">Format SQL</a></li>
241+
<li role="separator" className="divider"></li>
242+
<li><a href="#" onClick={() => clearHistory()} data-cy="clearhistorybtn">Clear history</a></li>
227243
</ul>
228244
</div>
229245
</div>

webui/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3226,6 +3226,7 @@ func main() {
32263226
http.Handle("/x/diffcommitlist/", gz.GzipHandler(logReq(diffCommitListHandler)))
32273227
http.Handle("/x/download/", gz.GzipHandler(logReq(downloadHandler)))
32283228
http.Handle("/x/downloadcsv/", gz.GzipHandler(logReq(downloadCSVHandler)))
3229+
http.Handle("/x/execclearhistory/", gz.GzipHandler(logReq(execClearHistory)))
32293230
http.Handle("/x/execlivesql/", gz.GzipHandler(logReq(execLiveSQL)))
32303231
http.Handle("/x/execsql/", gz.GzipHandler(logReq(visExecuteSQL)))
32313232
http.Handle("/x/forkdb/", gz.GzipHandler(logReq(forkDBHandler)))

0 commit comments

Comments
 (0)