Add history record struct and collect timestamps for query history

This commit is contained in:
Dan Sosedoff
2015-01-04 18:11:13 -06:00
parent 8b3c5bd8e9
commit 58a9c2b2bf
4 changed files with 343 additions and 306 deletions

View File

@@ -11,7 +11,7 @@ import (
type Client struct {
db *sqlx.DB
history []string
history []HistoryRecord
connectionString string
}
@@ -46,7 +46,13 @@ func NewClient() (*Client, error) {
return nil, err
}
return &Client{db: db, connectionString: str}, nil
client := Client{
db: db,
connectionString: str,
history: NewHistory(),
}
return &client, nil
}
func NewClientFromUrl(url string) (*Client, error) {
@@ -60,7 +66,13 @@ func NewClientFromUrl(url string) (*Client, error) {
return nil, err
}
return &Client{db: db, connectionString: url}, nil
client := Client{
db: db,
connectionString: url,
history: NewHistory(),
}
return &client, nil
}
func (client *Client) Test() error {
@@ -116,8 +128,14 @@ func (client *Client) TableIndexes(table string) (*Result, error) {
}
func (client *Client) Query(query string) (*Result, error) {
client.recordQuery(query)
return client.query(query)
res, err := client.query(query)
// Save history records only if query did not fail
if err == nil {
client.history = append(client.history, NewHistoryRecord(query))
}
return res, err
}
func (client *Client) query(query string, args ...interface{}) (*Result, error) {
@@ -224,7 +242,3 @@ func (client *Client) fetchRows(q string) ([]string, error) {
return results, nil
}
func (client *Client) recordQuery(query string) {
client.history = append(client.history, query)
}