-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsql_search.go
78 lines (70 loc) · 1.84 KB
/
sql_search.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
package imapsql
import (
"database/sql"
"fmt"
"strconv"
)
func buildSearchStmt(withFlags, withoutFlags []string) string {
var stmt string
stmt += `
SELECT DISTINCT msgs.msgId
FROM msgs
LEFT JOIN flags ON msgs.msgId = flags.msgid
WHERE msgs.mboxId = ?
`
if len(withFlags) != 0 {
if len(withFlags) == 1 {
stmt += `AND flags.flag = ? `
} else {
stmt += `AND flags.flag IN (`
for i := range withFlags {
stmt += `?`
if i != len(withFlags)-1 {
stmt += `, `
}
}
stmt += `)`
}
}
if len(withoutFlags) != 0 {
stmt += `AND msgs.msgId NOT IN (` + buildSearchStmt(withoutFlags, nil) + `)`
}
if len(withFlags) > 1 {
stmt += `GROUP BY flags.msgId HAVING COUNT(*) = ` + strconv.Itoa(len(withFlags))
}
return stmt
}
func (m *Mailbox) getFlagSearchStmt(withFlags, withoutFlags []string) (*sql.Stmt, error) {
cacheKey := fmt.Sprint(len(withFlags), ":", len(withoutFlags))
m.parent.flagsSearchStmtsLck.RLock()
stmt := m.parent.flagsSearchStmtsCache[cacheKey]
m.parent.flagsSearchStmtsLck.RUnlock()
if stmt != nil {
return stmt, nil
}
stmtStr := buildSearchStmt(withFlags, withoutFlags)
stmt, err := m.parent.db.Prepare(stmtStr)
if err != nil {
return nil, err
}
if len(withFlags) < 3 && len(withoutFlags) < 3 {
m.parent.flagsSearchStmtsLck.Lock()
m.parent.flagsSearchStmtsCache[cacheKey] = stmt
m.parent.flagsSearchStmtsLck.Unlock()
}
return stmt, nil
}
func (m *Mailbox) buildFlagSearchQueryArgs(withFlags, withoutFlags []string) []interface{} {
queryArgs := make([]interface{}, 0, 2+len(withFlags)+1+len(withoutFlags))
queryArgs = append(queryArgs, m.id)
for _, flag := range withFlags {
queryArgs = append(queryArgs, flag)
}
if len(withoutFlags) != 0 {
queryArgs = append(queryArgs, m.id)
for _, flag := range withoutFlags {
queryArgs = append(queryArgs, flag)
}
}
return queryArgs
}