Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Database.h #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 147 additions & 16 deletions Server/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ using namespace std;

const int BUFFER_SIZE=256;
int Database::nRows=0;
vector<int> Database::v=std::vector<int>();
string Database::str=string();
vector<vector<string> > Database::AllResult=vector<vector<string> >();
Database:: Database()
{
srand((unsigned long long)(new char));
Expand All @@ -27,18 +30,45 @@ Database:: ~Database(){
delete[]errormsg;
}


int ADDONE(void*, int, char**, char**){
Database::nRows++;
return 0;
}
int PUSHINT(void *arg,int nColumn, char** columnValue, char**columnName)
{
int ret=0;
for(int i=0;columnValue[0][i];i++){
ret=ret*10+(columnValue[0][i]-'0');
}
Database::v.push_back(ret);
return 0;
}
int GETLASTSTR(void *,int, char**columnValue, char**columnName)
{
Database::str.clear();
for(int i=0;columnValue[0][i];i++){
Database::str.push_back(columnValue[0][i]);
}
return 0;
}



int PUSHBACK(void *arg,int nColumn, char** columnValue, char**columnName)
int GETALL(void*,int nC, char** colValue, char ** colName)
{
v.push_back(columnValue[0]);
map<string,string> vs;
for(int i=0;i<nC;i++)
{
string name(colName[i]), value(colValue[i]);
vs.insert({name,value});
}
Database::AllResult.push_back(vs);
return 0;
}




int Database:: RandomID(const char* table){
int ID=rand();
char order[BUFFER_SIZE];
Expand All @@ -57,11 +87,9 @@ int Database:: RandomID(const char* table){
return ID;
}



void getTime(char* tms){
time_t t=time(0);
strftime(tms, 32, "%Y-%m-%d", localtime(&t));
strftime(tms, 32, "%Y-%m-%d %H:%M%S", localtime(&t));
cout<<"get time: "<<tms<<endl;
}

Expand Down Expand Up @@ -99,7 +127,7 @@ int Database::CreateGroup(const char* UserID, const char* GroupName)
int GroupID=RandomID("Group_Info");
char tms[64];
getTime(tms);
sprintf(order, "insert into Group_Info values(%d,'%s','0',%d,1,'%s')",
sprintf(order, "insert into Group_Info values(%d,'%s','0',%s, 1,'%s')",
GroupID, GroupName, UserID, tms);
val=sqlite3_exec(handler, order, NULL, NULL, &errormsg);
if(val){
Expand All @@ -114,6 +142,22 @@ int Database::CreateGroup(const char* UserID, const char* GroupName)
}
return GroupID;
}
int Database::GetAdmin(const char* GroupID){
Database::v.clear();
sprintf(order, "select AdminID from Group_info where GroupID=%s", GroupID);
printf("%s\n",order);
val=sqlite3_exec(handler, order, PUSHINT, NULL, &errormsg);
printf("size=%d,%d,%d",v.size(),v.back(),val);
if(val){
printf("find admin error! - - %s", errormsg);
return -1;
}
if(!v.size()){
printf("did not find admin of %s!", GroupID);
return 0;
}
return v.back();
}
int Database::AskFriend(const char* Fid,const char* Tid){
char tms[64];
getTime(tms);
Expand All @@ -139,7 +183,7 @@ int Database::Join(const char* UserID, const char* GroupID){
printf("didn't find group");
return 101;
}
sprintf(order, "insert into group_request values(%s,%s,%s)",
sprintf(order, "insert into group_request values(%s,%s,'%s')",
UserID, GroupID, tms);
val=sqlite3_exec(handler, order, NULL, NULL, &errormsg);
if(val){
Expand All @@ -148,6 +192,38 @@ int Database::Join(const char* UserID, const char* GroupID){
}
return 1;
}
int Database::Login(const char* UserID, const char* Password){
sprintf(order, "select * from User where UserID = %s and Password = '%s'", UserID, Password);
Database::nRows=0;
val=sqlite3_exec(handler, order, ADDONE, NULL, &errormsg);
if(val){
printf("login error: - - %s", errormsg);
return -1;
}
if(Database::nRows==0){
printf("wrong userid or password\n");
return 0;
}


sprintf(order,"select * from UnreadMessage where ReceiverID = %s",UserID);
val = sqlite3_exec(handler, order, GETALL, NULL, &errormsg);
if(val)
{
printf("send offline message error: - - %s",errormsg);
return -1;
}


sprintf(order,"delete * from UnreadMessage where ReceiverID = %s",UserID);
val = sqlite3_exec(handler,order,NULL,NULL,&errormsg);
if(val)
{
printf("remove offline message error: - - %s",errormsg);
return -1;
}
return 1;
}
int Database::Invite(const char* Uid, const char* Fid, const char* Gid){
char tms[64];
getTime(tms);
Expand All @@ -161,7 +237,7 @@ int Database::Invite(const char* Uid, const char* Fid, const char* Gid){
printf("didn't find group %s", Gid);
return 101;
}
sprintf(order, "insert into invite_request values (%s,%s,%s,%s)",
sprintf(order, "insert into invite_request values (%s,%s,%s,'%s')",
Uid, Fid, Gid, tms);
val=sqlite3_exec(handler, order, NULL, NULL, &errormsg);
if(val){
Expand All @@ -176,7 +252,12 @@ int Database::SaveOfflineMSG(const char* SendID, const char* RecvID, const char*
{
char tms[64];
getTime(tms);
sprintf(order,"insert into UnreadOffLineMessage values (%s,%s,%s,%d,%s,%s,%d)",SendID,RecvID,GroupID,type,tms,content,tag);
if(tag==0){
sprintf(order,"insert into UnreadMessage values (%s,%s,NULL,%d,'%s','%s',%d)",SendID,RecvID,type,tms,content,tag);
}
else{
sprintf(order, "insert into UnreadMessage values (%s,%s,%s,%d,'%s','%s',%d)", SendID, RecvID, GroupID, content, tag);
}
val = sqlite3_exec(handler,order,NULL,NULL,&errormsg);
if(val)
{
Expand All @@ -187,16 +268,66 @@ int Database::SaveOfflineMSG(const char* SendID, const char* RecvID, const char*
}




vector<int> Database::GetMember(const char* GroupID)
std::vector<int> Database::GetMember(const char* GroupID)
{
v.clear();
sprintf(order,"select UserID from GroupUser where GroupID = %s",GroupID);
val = sqlite3_exec(handler,order,PUSHBACK,NULL,errormsg);
Database::v.clear();
sprintf(order,"select UserID from Group_r where GroupID = %s",GroupID);
val = sqlite3_exec(handler,order,PUSHINT,NULL, &errormsg);
if(val)
{
printf("get member error: - - %s",errormsg);
}
return v;
}


string Database::GetUsername(const char* UserID){
sprintf(order, "select UserName from User where UserID=%s", UserID);
val=sqlite3_exec(handler, order, GETLASTSTR, NULL, &errormsg);
if(val){
printf("find username eror : - - %s", &errormsg);
return "";
}
return Database::str;
}


int Database::AcceptFriend(const char* ReceiveID, const char* AcceptID)
{
char tms[64];
getTime(tms);
sprintf(order, "insert into friend values (%s, %s, '%s')",
ReceiveID, AcceptID,tms);
val=sqlite3_exec(handler, order, NULL, NULL, &errormsg);
if(val){
printf("accept friend error: - - %s",errormsg);
return -1;
}
return 0;
}


int Database::DeleteFriend(const char* UserID,const char * FriendID)
{
sprintf(order,"delete from friend where (UserID1 = %s and UserID2 = FriendID) or (UserID1 = %s and UserID2 = %s)",UserID,FriendID);
val = sqlite3_exec(handler,order,NULL,NULL,&errormsg);
if(val)
{
printf("delete friend error: - - %s",errormsg);
return -1;
}
return 0;
}


int Database::QuitGroup(const char* UserID, const char * GroupID)
{
sprintf(order,"delete from Group_r where GroupID = %s and UserID = %s",GroupID,UserID);
val = sqlite3_exec(handler,order,NULL,NULL,&errormsg);
if(val)
{
printf("quit group error: - - %s",errormsg);
return -1;
}
return 0;
}
21 changes: 18 additions & 3 deletions Server/Database.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
#include <stdio.h>
#include <sqlite3.h>
#include <vector>
#include <string>
#include <map>

using std::string;
using std::vector;
using std::map;

class Database
{
public:
Database();
~Database();
static int nRows;
int Register(const char *,const char *);
int CreateGroup(const char*,const char*);
static vector<int>v;
static string str;
static vector< map<string,string> > AllResult;
int Register(const char *,const char *);
int Login(const char*, const char *);
std::string GetUsername(const char*);
int CreateGroup(const char*,const char*);
int GetAdmin(const char*);
int AcceptFriend(const char* recv, const char* send);
int AskFriend(const char*,const char*);
int Join(const char*,const char*);
int Invite(const char*,const char*,const char*);
int SaveOfflineMSG(const char*, const char*, const char*, const char*, int);
int Database::DeleteFriend(const char* UserID,const char * FriendID);
int Database::QuitGroup(const char* UserID, const char * GroupID);
int SaveOfflineMSG(const char*, const char*, const char*, const char*, int,int);
std::vector<int> GetMember(const char*);
private:
sqlite3 * handler;//句柄
Expand Down