-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathobs.go
More file actions
41 lines (35 loc) · 977 Bytes
/
obs.go
File metadata and controls
41 lines (35 loc) · 977 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package sqlite
import (
"container/ring"
"sync"
)
var lastQueriesRing = ring.New(30)
var lastQueriesMx sync.Mutex
// RememberQuery can be used to expose any SQL query to the internal `obs_lastqueries` virtual table.
// The last 30 queries will be displayed there.
// This is particularly useful when debugging automatically generated queries.
func RememberQuery(q string) {
lastQueriesMx.Lock()
lastQueriesRing.Value = q
lastQueriesRing = lastQueriesRing.Next()
lastQueriesMx.Unlock()
}
var ObsLastQueries = RegisterTable("obs_lastqueries", queryRecord{})
type queryRecord struct {
Query string `vtab:"query"`
}
func (queryRecord) Filter(_ int, _ Constraints) Iter[queryRecord] {
qs := make([]queryRecord, 0, 30)
lastQueriesMx.Lock()
start, p := lastQueriesRing, lastQueriesRing
for {
if p.Value != nil {
qs = append(qs, queryRecord{Query: p.Value.(string)})
}
p = p.Next()
if p == start {
lastQueriesMx.Unlock()
return FromArray(qs)
}
}
}