-
Notifications
You must be signed in to change notification settings - Fork 5
/
sqlchain.go
100 lines (84 loc) · 1.97 KB
/
sqlchain.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package api
import (
"database/sql"
"fmt"
"strconv"
"github.com/Sirupsen/logrus"
"github.com/labstack/echo"
)
type SqlRecord struct {
Id *int64 `json:"id"`
SQL *string `json:"sql"`
CargoId *int64 `json:"cargo_id"`
Hash *string `json:"hash"`
User *string `json:"user"`
}
const (
SqlMigrate = `
CREATE TABLE sqls (
id INTEGER PRIMARY KEY AUTOINCREMENT,
sql VARCHAR,
cargo_id VARCHAR,
hash VARCHAR,
user VARCHAR
);
`
)
func getCargoSql(c echo.Context, token string) (result []SqlRecord) {
db, err := getDB(token)
if db == nil {
return
}
cId, _ := strconv.Atoi(c.QueryParam("cargo_id"))
where := ""
if cId != 0 {
where = fmt.Sprintf("WHERE cargo_id = %d", cId)
}
limit, _ := strconv.Atoi(c.QueryParam("limit"))
if limit == 0 {
limit = 20
}
sql := fmt.Sprintf("SELECT * FROM sqls %s ORDER BY id DESC LIMIT %d;", where, limit)
rows, err := db.Query(sql)
if err != nil {
logrus.Errorf("getCargoSql: %v", err)
return
}
defer rows.Close()
logrus.Debugf("getCargoSql: %s", sql)
for rows.Next() {
record := SqlRecord{}
err = rows.Scan(&record.Id, &record.SQL, &record.CargoId, &record.Hash, &record.User)
if err != nil {
logrus.Errorf("getCargoSql: %v", err)
return
}
result = append(result, record)
}
return
}
func putCargoSql(cId int64, sql string, user string, db *sql.DB) (err error) {
if db == nil {
return fmt.Errorf("db is nil")
}
var sqlHash string
err = db.QueryRow("SELECT last_request_hash();").Scan(&sqlHash)
if err != nil {
logrus.Errorf("SELECT last_request_hash failed: %v", err)
return
}
insSql := "INSERT INTO sqls(sql, cargo_id, hash, user) VALUES (?, ?, ?, ?);"
stmt, err := db.Prepare(insSql)
if err != nil {
logrus.Errorf("putCargoSql: %v", err)
return
}
defer stmt.Close()
logrus.Debugf("putCargoSql: %s %d %s\n\t %s %s", user, cId, insSql, sql, sqlHash)
_, err = stmt.Exec(sql, cId, sqlHash, user)
if err != nil {
logrus.Errorf("putCargoSql: %v", err)
return
}
return
}