added button for delete of queries,
added collapse/expand results, added date time and duration for each query
This commit is contained in:
91
dbPrompt.go
91
dbPrompt.go
@ -37,6 +37,7 @@ type QueryResult struct {
|
||||
ID string `json:"id"`
|
||||
Query string `json:"query"`
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
Duration int64 `json:"duration,omitempty"` // Duration in milliseconds
|
||||
Error string `json:"error,omitempty"`
|
||||
Columns []string `json:"columns,omitempty"`
|
||||
Rows [][]interface{} `json:"rows,omitempty"`
|
||||
@ -83,7 +84,7 @@ func main() {
|
||||
// HTTP Handlers
|
||||
http.HandleFunc("/", serveIndex)
|
||||
http.HandleFunc("/query", handleQuery)
|
||||
http.HandleFunc("/history", handleHistory)
|
||||
http.HandleFunc("/history/", handleHistory)
|
||||
|
||||
log.Println("Starting server on :8080...")
|
||||
if err := http.ListenAndServe(":8080", nil); err != nil {
|
||||
@ -96,33 +97,68 @@ func serveIndex(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func handleHistory(w http.ResponseWriter, r *http.Request) {
|
||||
files, err := ioutil.ReadDir(historyDir)
|
||||
if err != nil {
|
||||
http.Error(w, "Could not read history", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
files, err := ioutil.ReadDir(historyDir)
|
||||
if err != nil {
|
||||
http.Error(w, "Could not read history", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
var history []QueryResult
|
||||
for _, file := range files {
|
||||
if filepath.Ext(file.Name()) == ".json" {
|
||||
data, err := ioutil.ReadFile(filepath.Join(historyDir, file.Name()))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
var qr QueryResult
|
||||
if err := json.Unmarshal(data, &qr); err == nil {
|
||||
history = append(history, qr)
|
||||
var history []QueryResult
|
||||
for _, file := range files {
|
||||
if filepath.Ext(file.Name()) == ".json" {
|
||||
data, err := ioutil.ReadFile(filepath.Join(historyDir, file.Name()))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
var qr QueryResult
|
||||
if err := json.Unmarshal(data, &qr); err == nil {
|
||||
history = append(history, qr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sort by timestamp, oldest first
|
||||
sort.Slice(history, func(i, j int) bool {
|
||||
return history[i].Timestamp < history[j].Timestamp
|
||||
})
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(history)
|
||||
|
||||
case http.MethodDelete:
|
||||
id := strings.TrimPrefix(r.URL.Path, "/history/")
|
||||
if id == "" {
|
||||
http.Error(w, "History ID is required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Basic sanitization to prevent directory traversal
|
||||
cleanID := filepath.Base(id)
|
||||
if cleanID != id || strings.Contains(cleanID, "..") {
|
||||
http.Error(w, "Invalid history ID", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
filePath := filepath.Join(historyDir, cleanID+".json")
|
||||
|
||||
if _, err := os.Stat(filePath); os.IsNotExist(err) {
|
||||
http.Error(w, "History item not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
if err := os.Remove(filePath); err != nil {
|
||||
log.Printf("Failed to delete history file %s: %v", filePath, err)
|
||||
http.Error(w, "Failed to delete history item", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
default:
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
|
||||
// Sort by timestamp, oldest first
|
||||
sort.Slice(history, func(i, j int) bool {
|
||||
return history[i].Timestamp < history[j].Timestamp
|
||||
})
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(history)
|
||||
}
|
||||
|
||||
func handleQuery(w http.ResponseWriter, r *http.Request) {
|
||||
@ -133,7 +169,8 @@ func handleQuery(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
query := strings.TrimSpace(req.Query)
|
||||
isSelect := strings.HasPrefix(strings.ToLower(query), "select") || strings.HasPrefix(strings.ToLower(query), "show") || strings.HasPrefix(strings.ToLower(query), "describe")
|
||||
query_lower := strings.ToLower(query)
|
||||
isSelect := strings.HasPrefix(query_lower, "select") || strings.HasPrefix(query_lower, "show") || strings.HasPrefix(query_lower, "describe") || strings.HasPrefix(query_lower, "desc")
|
||||
|
||||
result := QueryResult{
|
||||
ID: req.ID,
|
||||
@ -146,6 +183,8 @@ func handleQuery(w http.ResponseWriter, r *http.Request) {
|
||||
result.ID = strconv.FormatInt(result.Timestamp, 10)
|
||||
}
|
||||
|
||||
startTime := time.Now()
|
||||
|
||||
if isSelect {
|
||||
// It's a query that returns rows
|
||||
rows, err := db.Query(query)
|
||||
@ -198,6 +237,8 @@ func handleQuery(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
result.Duration = time.Since(startTime).Milliseconds()
|
||||
|
||||
// Save to history
|
||||
filePath := filepath.Join(historyDir, result.ID+".json")
|
||||
fileData, _ := json.MarshalIndent(result, "", " ")
|
||||
|
||||
Reference in New Issue
Block a user