-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.go
75 lines (71 loc) · 1.6 KB
/
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
package main
import (
"fmt"
"strings"
// "strconv"
log "github.com/Sirupsen/logrus"
)
func SearchCommand(command string) {
if strings.Contains(command, "+") {
data := strings.Split(command, "+")
out := ""
if len(data) > 0 {
out += "%"
}
for _, d := range data {
if d != "" {
out += d + "%"
}
}
command = out
} else {
if !strings.HasPrefix(command, "%") && !strings.HasSuffix(command,"%") {
command = "%" + command + "%"
}
}
logs := []Log{}
err := db.Select(&logs, "SELECT * FROM log WHERE LOWER(command) LIKE $1 ORDER BY popularity DESC LIMIT 10", command)
if err != nil {
log.Fatal(err)
}
if len(logs) > 0 {
for _, logData := range logs {
// fmt.Println("[" + strconv.Itoa(logData.Popularity) + "] " + logData.Command)
fmt.Println(logData.Command)
}
} else {
fmt.Println("No result")
}
}
func SearchCommandLong(command string, limit int) {
if strings.Contains(command, "+") {
data := strings.Split(command, "+")
out := ""
if len(data) > 0 {
out += "%"
}
for _, d := range data {
if d != "" {
out += d + "%"
}
}
command = out
} else {
if !strings.HasPrefix(command, "%") && !strings.HasSuffix(command,"%") {
command = "%" + command + "%"
}
}
logs := []Log{}
err := db.Select(&logs, "SELECT * FROM log WHERE LOWER(command) LIKE $1 ORDER BY popularity DESC LIMIT $2", command, limit)
if err != nil {
log.Fatal(err)
}
if len(logs) > 0 {
for _, logData := range logs {
// fmt.Println("[" + strconv.Itoa(logData.Popularity) + "] " + logData.Command)
fmt.Println(logData.Command)
}
} else {
fmt.Println("No result")
}
}