aboutsummaryrefslogtreecommitdiffstats
path: root/sql
diff options
context:
space:
mode:
authorMartin Polden <mpolden@mpolden.no>2019-12-26 15:27:39 +0100
committerMartin Polden <mpolden@mpolden.no>2019-12-26 15:27:39 +0100
commitdae3d2ba6d1f8092b1b18f417f5c37cdca66f772 (patch)
tree58f299666134b1abfc41caa8c3cac094d834703d /sql
parent648c405f64f1732d09389ae7b88b245b4f80d768 (diff)
Add ID to LogEntry
Diffstat (limited to 'sql')
-rw-r--r--sql/sql.go4
-rw-r--r--sql/sql_test.go24
2 files changed, 15 insertions, 13 deletions
diff --git a/sql/sql.go b/sql/sql.go
index b9c2add..c645b34 100644
--- a/sql/sql.go
+++ b/sql/sql.go
@@ -62,6 +62,7 @@ type Client struct {
// LogEntry represents an entry in the log.
type LogEntry struct {
+ ID int64 `db:"id"`
Time int64 `db:"time"`
RemoteAddr []byte `db:"remote_addr"`
Qtype uint16 `db:"type"`
@@ -90,7 +91,8 @@ func (c *Client) ReadLog(n int) ([]LogEntry, error) {
c.mu.RLock()
defer c.mu.RUnlock()
query := `
-SELECT time,
+SELECT log.id AS id,
+ time,
remote_addr.addr AS remote_addr,
type,
rr_question.name AS question,
diff --git a/sql/sql_test.go b/sql/sql_test.go
index 60a8f3c..6ed9c1e 100644
--- a/sql/sql_test.go
+++ b/sql/sql_test.go
@@ -74,13 +74,13 @@ func TestReadLog(t *testing.T) {
}
}
entries := []LogEntry{
- {Question: "bar.example.com", Qtype: 28, Answer: "2001:db8::3", Time: 1560641700, RemoteAddr: net.IPv4(192, 0, 2, 102)},
- {Question: "bar.example.com", Qtype: 28, Answer: "2001:db8::2", Time: 1560641700, RemoteAddr: net.IPv4(192, 0, 2, 102)},
- {Question: "bar.example.com", Qtype: 28, Answer: "2001:db8::1", Time: 1560639880, RemoteAddr: net.IPv4(192, 0, 2, 102)},
- {Question: "bar.example.com", Qtype: 1, Answer: "192.0.2.2", Time: 1560637120, RemoteAddr: net.IPv4(192, 0, 2, 102)},
- {Question: "bar.example.com", Qtype: 1, Answer: "192.0.2.2", Time: 1560637050, RemoteAddr: net.IPv4(192, 0, 2, 101)},
- {Question: "foo.example.com", Qtype: 1, Answer: "192.0.2.1", Time: 1560636980, RemoteAddr: net.IPv4(192, 0, 2, 100)},
- {Question: "foo.example.com", Qtype: 1, Answer: "192.0.2.1", Time: 1560636910, RemoteAddr: net.IPv4(192, 0, 2, 100)},
+ {ID: 6, Question: "bar.example.com", Qtype: 28, Answer: "2001:db8::3", Time: 1560641700, RemoteAddr: net.IPv4(192, 0, 2, 102)},
+ {ID: 6, Question: "bar.example.com", Qtype: 28, Answer: "2001:db8::2", Time: 1560641700, RemoteAddr: net.IPv4(192, 0, 2, 102)},
+ {ID: 5, Question: "bar.example.com", Qtype: 28, Answer: "2001:db8::1", Time: 1560639880, RemoteAddr: net.IPv4(192, 0, 2, 102)},
+ {ID: 4, Question: "bar.example.com", Qtype: 1, Answer: "192.0.2.2", Time: 1560637120, RemoteAddr: net.IPv4(192, 0, 2, 102)},
+ {ID: 3, Question: "bar.example.com", Qtype: 1, Answer: "192.0.2.2", Time: 1560637050, RemoteAddr: net.IPv4(192, 0, 2, 101)},
+ {ID: 2, Question: "foo.example.com", Qtype: 1, Answer: "192.0.2.1", Time: 1560636980, RemoteAddr: net.IPv4(192, 0, 2, 100)},
+ {ID: 1, Question: "foo.example.com", Qtype: 1, Answer: "192.0.2.1", Time: 1560636910, RemoteAddr: net.IPv4(192, 0, 2, 100)},
}
for n := 1; n <= len(entries); n++ {
want := entries[:n]
@@ -107,11 +107,11 @@ func TestDeleteLogBefore(t *testing.T) {
}
want := []LogEntry{
- {Question: "bar.example.com", Qtype: 28, Answer: "2001:db8::3", Time: 1560641700, RemoteAddr: net.IPv4(192, 0, 2, 102)},
- {Question: "bar.example.com", Qtype: 28, Answer: "2001:db8::2", Time: 1560641700, RemoteAddr: net.IPv4(192, 0, 2, 102)},
- {Question: "bar.example.com", Qtype: 28, Answer: "2001:db8::1", Time: 1560639880, RemoteAddr: net.IPv4(192, 0, 2, 102)},
- {Question: "bar.example.com", Qtype: 1, Answer: "192.0.2.2", Time: 1560637120, RemoteAddr: net.IPv4(192, 0, 2, 102)},
- {Question: "bar.example.com", Qtype: 1, Answer: "192.0.2.2", Time: 1560637050, RemoteAddr: net.IPv4(192, 0, 2, 101)},
+ {ID: 6, Question: "bar.example.com", Qtype: 28, Answer: "2001:db8::3", Time: 1560641700, RemoteAddr: net.IPv4(192, 0, 2, 102)},
+ {ID: 6, Question: "bar.example.com", Qtype: 28, Answer: "2001:db8::2", Time: 1560641700, RemoteAddr: net.IPv4(192, 0, 2, 102)},
+ {ID: 5, Question: "bar.example.com", Qtype: 28, Answer: "2001:db8::1", Time: 1560639880, RemoteAddr: net.IPv4(192, 0, 2, 102)},
+ {ID: 4, Question: "bar.example.com", Qtype: 1, Answer: "192.0.2.2", Time: 1560637120, RemoteAddr: net.IPv4(192, 0, 2, 102)},
+ {ID: 3, Question: "bar.example.com", Qtype: 1, Answer: "192.0.2.2", Time: 1560637050, RemoteAddr: net.IPv4(192, 0, 2, 101)},
}
n := 10
got, err := c.ReadLog(n)