#pragma once
#include<string>
#include<MSAccess.hpp>
using namespace std;
class CMySession
{
public:
CMySession();
//資料操作
TMSQuery* QueryData(string strSQL);
void ConnectDatabase(TMSConnection* ptmsConnection);
bool WriteData(string strSQL);
bool DeleteData(string strSQL);
//商務邏輯
bool VerifyUserStatus();//檢驗使用者狀態
bool CreateSession(string strUserName,TMSConnection* ptmsConn);//建立會話
public:
~CMySession(void);
private:
string m_strUserName;
unsigned int m_uSessionID;
TMSConnection *m_ptmsConn;
};
#include "MySession.h"
void CMySession::ConnectDatabase(TMSConnection* ptmsConnection)
{
m_ptmsConn = ptmsConnection;
return;
}
//建立會話
bool CMySession::CreateSession(string strUserName,TMSConnection* ptmsConn)
{
m_strUserName = strUserName;
ConnectDatabase(ptmsConn);
string strSQL;
strSQL = "select * from T_log where user_name = '" + strUserName + "'";
TMSQuery* ptmsQuery = QueryData(strSQL);
if(ptmsQuery == NULL)
return false;
if(ptmsQuery->RecordCount == 0)
{
delete ptmsQuery;
ptmsQuery = NULL;
strSQL = "insert into T_log(user_name) values( '"+strUserName+"')";
if(!WriteData(strSQL))
{
return false;
}
strSQL = "select log_id from T_log where user_name = '"+strUserName+"'";
TMSQuery* ptmsQuery = QueryData(strSQL);
if(ptmsQuery == NULL)
return false;
m_uSessionID = ptmsQuery->FieldByName("log_id")->AsInteger;
ptmsQuery->SQL->Clear();
delete ptmsQuery;
ptmsQuery = NULL;
return true;
}
else
{
strSQL = "select * from T_log where user_name = '"+m_strUserName+"'";
TMSQuery* ptmsQuery = QueryData(strSQL);
if(ptmsQuery == NULL)
return false;
unsigned int uid;
uid = ptmsQuery->FieldByName("log_id")->AsInteger;
ptmsQuery->SQL->Clear();
ptmsQuery->Close();
delete ptmsQuery;
ptmsQuery = NULL;
char idBuf[10];
itoa(uid, idBuf, 10);
strSQL = "delete from T_log where log_id = ";
strSQL += idBuf;
if(!DeleteData(strSQL))
{
return false;
}
else
{
strSQL = "insert into T_log(user_name) values('"+m_strUserName+"')";
if(!WriteData(strSQL))//寫入Session
{
return false;
}
strSQL = "select log_id from T_log where user_name = '"+m_strUserName+"'";
TMSQuery* ptmsQuery = QueryData(strSQL);
if(ptmsQuery == NULL)
return false;
m_uSessionID = ptmsQuery->FieldByName("log_id")->AsInteger;
ptmsQuery->SQL->Clear();
delete ptmsQuery;
ptmsQuery = NULL;
return true;
}
}
}
//檢驗使用者狀態
bool CMySession::VerifyUserStatus()
{
string strSQL;
strSQL = "select * from T_log where log_id = " ;
char szBuffer[10];
itoa(m_uSessionID,szBuffer,10);
strSQL+=szBuffer;
TMSQuery* ptmsQuery = QueryData(strSQL);
if(ptmsQuery != NULL)
{
if(ptmsQuery->RecordCount == 0)
{
ptmsQuery->SQL->Clear();
ptmsQuery->Close();
delete ptmsQuery;
ptmsQuery = NULL;
return false;
}
else
{
ptmsQuery->SQL->Clear();
ptmsQuery->Close();
delete ptmsQuery;
ptmsQuery = NULL;
return true;
}
}
else
{
return false;
}
}
TMSQuery* CMySession::QueryData(string strSQL)
{
if(m_ptmsConn != NULL)
{
TMSQuery* ptmsQuery = new TMSQuery(NULL);
ptmsQuery->Connection = m_ptmsConn;
ptmsQuery->SQL->Add(strSQL.c_str());
ptmsQuery->Execute();
return ptmsQuery;
}
else
{
return NULL;
}
}
bool CMySession::DeleteData(string strSQL)
{
if(m_ptmsConn != NULL)
{
TMSQuery* ptmsQuery = new TMSQuery(NULL);
ptmsQuery->Connection = m_ptmsConn;
ptmsQuery->SQL->Add(strSQL.c_str());
ptmsQuery->Execute();
ptmsQuery->SQL->Clear();
ptmsQuery->Close();
delete ptmsQuery;
ptmsQuery = NULL;
return true;
}
else
{
return false;
}
}
bool CMySession::WriteData(string strSQL)
{
if(m_ptmsConn != NULL)
{
TMSQuery* ptmsQuery = new TMSQuery(NULL);
ptmsQuery->Connection = m_ptmsConn;
ptmsQuery->SQL->Add(strSQL.c_str());
ptmsQuery->Execute();
ptmsQuery->SQL->Clear();
ptmsQuery->Close();
delete ptmsQuery;
return true;
}
else
{
return false;
}
}
CMySession::CMySession()
{
m_strUserName = "";
m_uSessionID = 0;
m_ptmsConn = NULL;
}
CMySession::~CMySession(void)
{
//m_ptmsConn->Close();
}
其主要思路是:
在用戶端首先建立一個會話,(主要是在資料庫中查詢自己使用者的記錄,如果有,則刪除,然後再寫入,再擷取ID並儲存到m_uSessionID中),然後在客戶每次對資料庫進行操作的時候進行判斷,(從資料庫中查詢客戶的記錄,將記錄中的ID 和客戶的SessionID進行對比,如果相等則會話正常,否則,會話結束)!
思路簡單,但比較好用
,用法:
在登陸的時候產生對象Session
然後Session.Create(userName,pConn);
在每次操作資料庫的時候就Session.VerifyUserStatus()就OK了,呵呵!!!