1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-26 06:01:29 +00:00

Merge pull request #1348 from sidbusy/develop

allows custom the TableName of Session
This commit is contained in:
astaxie 2015-09-06 21:22:05 +08:00
commit 9e969957de

View File

@ -51,7 +51,10 @@ import (
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
) )
var mysqlpder = &MysqlProvider{} var (
TableName = "session"
mysqlpder = &MysqlProvider{}
)
// mysql session store // mysql session store
type MysqlSessionStore struct { type MysqlSessionStore struct {
@ -110,7 +113,7 @@ func (st *MysqlSessionStore) SessionRelease(w http.ResponseWriter) {
if err != nil { if err != nil {
return return
} }
st.c.Exec("UPDATE session set `session_data`=?, `session_expiry`=? where session_key=?", st.c.Exec("UPDATE "+TableName+" set `session_data`=?, `session_expiry`=? where session_key=?",
b, time.Now().Unix(), st.sid) b, time.Now().Unix(), st.sid)
} }
@ -141,11 +144,11 @@ func (mp *MysqlProvider) SessionInit(maxlifetime int64, savePath string) error {
// get mysql session by sid // get mysql session by sid
func (mp *MysqlProvider) SessionRead(sid string) (session.SessionStore, error) { func (mp *MysqlProvider) SessionRead(sid string) (session.SessionStore, error) {
c := mp.connectInit() c := mp.connectInit()
row := c.QueryRow("select session_data from session where session_key=?", sid) row := c.QueryRow("select session_data from "+TableName+" where session_key=?", sid)
var sessiondata []byte var sessiondata []byte
err := row.Scan(&sessiondata) err := row.Scan(&sessiondata)
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
c.Exec("insert into session(`session_key`,`session_data`,`session_expiry`) values(?,?,?)", c.Exec("insert into "+TableName+"(`session_key`,`session_data`,`session_expiry`) values(?,?,?)",
sid, "", time.Now().Unix()) sid, "", time.Now().Unix())
} }
var kv map[interface{}]interface{} var kv map[interface{}]interface{}
@ -165,7 +168,7 @@ func (mp *MysqlProvider) SessionRead(sid string) (session.SessionStore, error) {
func (mp *MysqlProvider) SessionExist(sid string) bool { func (mp *MysqlProvider) SessionExist(sid string) bool {
c := mp.connectInit() c := mp.connectInit()
defer c.Close() defer c.Close()
row := c.QueryRow("select session_data from session where session_key=?", sid) row := c.QueryRow("select session_data from "+TableName+" where session_key=?", sid)
var sessiondata []byte var sessiondata []byte
err := row.Scan(&sessiondata) err := row.Scan(&sessiondata)
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
@ -178,13 +181,13 @@ func (mp *MysqlProvider) SessionExist(sid string) bool {
// generate new sid for mysql session // generate new sid for mysql session
func (mp *MysqlProvider) SessionRegenerate(oldsid, sid string) (session.SessionStore, error) { func (mp *MysqlProvider) SessionRegenerate(oldsid, sid string) (session.SessionStore, error) {
c := mp.connectInit() c := mp.connectInit()
row := c.QueryRow("select session_data from session where session_key=?", oldsid) row := c.QueryRow("select session_data from "+TableName+" where session_key=?", oldsid)
var sessiondata []byte var sessiondata []byte
err := row.Scan(&sessiondata) err := row.Scan(&sessiondata)
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
c.Exec("insert into session(`session_key`,`session_data`,`session_expiry`) values(?,?,?)", oldsid, "", time.Now().Unix()) c.Exec("insert into "+TableName+"(`session_key`,`session_data`,`session_expiry`) values(?,?,?)", oldsid, "", time.Now().Unix())
} }
c.Exec("update session set `session_key`=? where session_key=?", sid, oldsid) c.Exec("update "+TableName+" set `session_key`=? where session_key=?", sid, oldsid)
var kv map[interface{}]interface{} var kv map[interface{}]interface{}
if len(sessiondata) == 0 { if len(sessiondata) == 0 {
kv = make(map[interface{}]interface{}) kv = make(map[interface{}]interface{})
@ -201,7 +204,7 @@ func (mp *MysqlProvider) SessionRegenerate(oldsid, sid string) (session.SessionS
// delete mysql session by sid // delete mysql session by sid
func (mp *MysqlProvider) SessionDestroy(sid string) error { func (mp *MysqlProvider) SessionDestroy(sid string) error {
c := mp.connectInit() c := mp.connectInit()
c.Exec("DELETE FROM session where session_key=?", sid) c.Exec("DELETE FROM "+TableName+" where session_key=?", sid)
c.Close() c.Close()
return nil return nil
} }
@ -209,7 +212,7 @@ func (mp *MysqlProvider) SessionDestroy(sid string) error {
// delete expired values in mysql session // delete expired values in mysql session
func (mp *MysqlProvider) SessionGC() { func (mp *MysqlProvider) SessionGC() {
c := mp.connectInit() c := mp.connectInit()
c.Exec("DELETE from session where session_expiry < ?", time.Now().Unix()-mp.maxlifetime) c.Exec("DELETE from "+TableName+" where session_expiry < ?", time.Now().Unix()-mp.maxlifetime)
c.Close() c.Close()
return return
} }
@ -219,7 +222,7 @@ func (mp *MysqlProvider) SessionAll() int {
c := mp.connectInit() c := mp.connectInit()
defer c.Close() defer c.Close()
var total int var total int
err := c.QueryRow("SELECT count(*) as num from session").Scan(&total) err := c.QueryRow("SELECT count(*) as num from " + TableName).Scan(&total)
if err != nil { if err != nil {
return 0 return 0
} }