-
Notifications
You must be signed in to change notification settings - Fork 13
/
dao.go
135 lines (120 loc) · 3.3 KB
/
dao.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package postgres
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
"strconv"
"time"
)
func newSessionDao(dsn string, tableName string) (*sessionDao, error) {
conn, err := sql.Open("postgres", dsn)
if err != nil {
return &sessionDao{}, err
}
return &sessionDao{
postgresConn: conn,
tableName: tableName,
}, nil
}
type sessionDao struct {
postgresConn *sql.DB
tableName string
}
// get session by sessionId
func (dao *sessionDao) getSessionBySessionId(sessionId string) (session map[string][]byte, err error) {
sqlStr := fmt.Sprintf("SELECT * FROM %s WHERE session_id=?", dao.tableName)
return dao.getRow(sqlStr, sessionId)
}
// count sessionId
func (dao *sessionDao) countSessions() int {
sqlStr := fmt.Sprintf("SELECT count(*) as total FROM %s", dao.tableName)
res, err := dao.getRow(sqlStr)
if err != nil {
return 0
}
total, _ := strconv.Atoi(string(res["total"]))
return total
}
// update session by sessionId
func (dao *sessionDao) updateBySessionId(sessionId string, contents string, lastActiveTime int64) (int64, error) {
sqlStr := fmt.Sprintf("UPDATE %s SET contents=?,last_active=? WHERE session_id=?", dao.tableName)
return dao.execute(sqlStr, contents, lastActiveTime, sessionId)
}
// delete session by sessionId
func (dao *sessionDao) deleteBySessionId(sessionId string) (int64, error) {
sqlStr := fmt.Sprintf("DELETE FROM %s WHERE session_id=?", dao.tableName)
return dao.execute(sqlStr, sessionId)
}
// delete session by maxLifeTime
func (dao *sessionDao) deleteSessionByMaxLifeTime(maxLifeTime int64) (int64, error) {
sqlStr := fmt.Sprintf("DELETE FROM %s WHERE last_active<=?", dao.tableName)
lastTime := time.Now().Unix() - maxLifeTime
return dao.execute(sqlStr, lastTime)
}
// insert new session
func (dao *sessionDao) insert(sessionId string, contents string, lastActiveTime int64) (int64, error) {
sqlStr := fmt.Sprintf("INSERT INTO %s (session_id, contents, last_active) VALUES (?,?,?)", dao.tableName)
return dao.execute(sqlStr, sessionId, contents, lastActiveTime)
}
// get rows
// return []map[string][]byte
func (dao *sessionDao) getRows(sql string, args ...interface{}) (results []map[string][]byte, err error) {
stmt, err := dao.postgresConn.Prepare(sql)
if err != nil {
return
}
defer stmt.Close()
rows, err := stmt.Query(args...)
if err != nil {
return
}
defer rows.Close()
cols := []string{}
cols, err = rows.Columns()
if err != nil {
return
}
vals := make([][]byte, len(cols))
scans := make([]interface{}, len(cols))
for i := range vals {
scans[i] = &vals[i]
}
results = []map[string][]byte{}
for rows.Next() {
err = rows.Scan(scans...)
if err != nil {
return
}
row := make(map[string][]byte)
for k, v := range vals {
key := cols[k]
row[key] = v
}
results = append(results, row)
}
return
}
// get row
func (dao *sessionDao) getRow(sql string, args ...interface{}) (res map[string][]byte, err error) {
rows, err := dao.getRows(sql, args...)
if err != nil {
return
}
if len(rows) > 0 {
return rows[0], nil
}
return
}
// execute(insert, update, delete)
func (dao *sessionDao) execute(sql string, args ...interface{}) (int64, error) {
stmt, err := dao.postgresConn.Prepare(sql)
if err != nil {
return 0, err
}
defer stmt.Close()
rows, err := stmt.Exec(args...)
if err != nil {
return 0, err
}
return rows.RowsAffected()
}