-
Notifications
You must be signed in to change notification settings - Fork 10
/
sql_flags.go
82 lines (70 loc) · 1.87 KB
/
sql_flags.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package imapsql
import (
"database/sql"
)
func (b *Backend) buildFlagsAddStmt(flagsCount int) string {
return `
INSERT INTO flags
SELECT mboxId, msgId, column1 AS flag
FROM msgs
CROSS JOIN (` + b.db.valuesSubquery(flagsCount) + `) flagset
WHERE mboxId = ? AND msgId BETWEEN ? AND ?
ON CONFLICT DO NOTHING`
}
func (m *Mailbox) makeFlagsAddStmtArgs(flags []string, start, stop uint32) (params []interface{}) {
params = make([]interface{}, 0, 3+len(flags))
for _, flag := range flags {
params = append(params, flag)
}
params = append(params, m.id, start, stop)
return
}
func (b *Backend) getFlagsAddStmt(flagsCount int) (*sql.Stmt, error) {
str := b.buildFlagsAddStmt(flagsCount)
b.addFlagsStmtsLck.RLock()
stmt := b.addFlagsStmtsCache[str]
b.addFlagsStmtsLck.RUnlock()
if stmt != nil {
return stmt, nil
}
stmt, err := b.db.Prepare(str)
if err != nil {
return nil, err
}
b.addFlagsStmtsLck.Lock()
b.addFlagsStmtsCache[str] = stmt
b.addFlagsStmtsLck.Unlock()
return stmt, nil
}
func (b *Backend) buildFlagsRemStmt(flagsCount int) string {
return `
DELETE FROM flags
WHERE mboxId = ?
AND msgId BETWEEN ? AND ?
AND flag IN (` + b.db.valuesSubquery(flagsCount) + `)`
}
func (b *Backend) getFlagsRemStmt(flagsCount int) (*sql.Stmt, error) {
str := b.buildFlagsRemStmt(flagsCount)
b.remFlagsStmtsLck.RLock()
stmt := b.remFlagsStmtsCache[str]
b.remFlagsStmtsLck.RUnlock()
if stmt != nil {
return stmt, nil
}
stmt, err := b.db.Prepare(str)
if err != nil {
return nil, err
}
b.remFlagsStmtsLck.Lock()
b.remFlagsStmtsCache[str] = stmt
b.remFlagsStmtsLck.Unlock()
return stmt, nil
}
func (m *Mailbox) makeFlagsRemStmtArgs(flags []string, start, stop uint32) []interface{} {
params := make([]interface{}, 0, 3+len(flags))
params = append(params, m.id, start, stop)
for _, flag := range flags {
params = append(params, flag)
}
return params
}