mirror of
https://github.com/sosedoff/pgweb.git
synced 2024-12-14 19:21:46 +03:00
Do not record duplicate queries into history
This commit is contained in:
parent
1bc824e39b
commit
9b5764d4fb
@ -150,7 +150,7 @@ func (client *Client) Query(query string) (*Result, error) {
|
||||
res, err := client.query(query)
|
||||
|
||||
// Save history records only if query did not fail
|
||||
if err == nil {
|
||||
if err == nil && !client.hasHistoryRecord(query) {
|
||||
client.History = append(client.History, history.NewRecord(query))
|
||||
}
|
||||
|
||||
@ -223,3 +223,16 @@ func (client *Client) fetchRows(q string) ([]string, error) {
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func (client *Client) hasHistoryRecord(query string) bool {
|
||||
result := false
|
||||
|
||||
for _, record := range client.History {
|
||||
if record.Query == query {
|
||||
result = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
@ -244,11 +244,11 @@ func test_ResultCsv(t *testing.T) {
|
||||
}
|
||||
|
||||
func test_History(t *testing.T) {
|
||||
_, err := testClient.Query("SELECT * FROM books")
|
||||
_, err := testClient.Query("SELECT * FROM books WHERE id = 12345")
|
||||
query := testClient.History[len(testClient.History)-1].Query
|
||||
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, "SELECT * FROM books", query)
|
||||
assert.Equal(t, "SELECT * FROM books WHERE id = 12345", query)
|
||||
}
|
||||
|
||||
func test_HistoryError(t *testing.T) {
|
||||
@ -259,6 +259,16 @@ func test_HistoryError(t *testing.T) {
|
||||
assert.NotEqual(t, "SELECT * FROM books123", query)
|
||||
}
|
||||
|
||||
func test_HistoryUniqueness(t *testing.T) {
|
||||
client, _ := NewFromUrl("postgres://postgres@localhost/booktown?sslmode=disable")
|
||||
|
||||
client.Query("SELECT * FROM books WHERE id = 1")
|
||||
client.Query("SELECT * FROM books WHERE id = 1")
|
||||
|
||||
assert.Equal(t, 1, len(client.History))
|
||||
assert.Equal(t, "SELECT * FROM books WHERE id = 1", client.History[0].Query)
|
||||
}
|
||||
|
||||
func TestAll(t *testing.T) {
|
||||
if onWindows() {
|
||||
// Dont have access to windows machines at the moment...
|
||||
|
Loading…
Reference in New Issue
Block a user