diff --git a/Server/Database.cpp b/Server/Database.cpp index 65deb88..8baa104 100644 --- a/Server/Database.cpp +++ b/Server/Database.cpp @@ -9,6 +9,9 @@ using namespace std; const int BUFFER_SIZE=256; int Database::nRows=0; +vector Database::v=std::vector(); +string Database::str=string(); +vector > Database::AllResult=vector >(); Database:: Database() { srand((unsigned long long)(new char)); @@ -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 vs; + for(int i=0;i Database::GetMember(const char* GroupID) +std::vector 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; +} diff --git a/Server/Database.h b/Server/Database.h index 5558130..10b1b9f 100644 --- a/Server/Database.h +++ b/Server/Database.h @@ -1,6 +1,12 @@ #include #include #include +#include +#include + +using std::string; +using std::vector; +using std::map; class Database { @@ -8,12 +14,21 @@ class Database Database(); ~Database(); static int nRows; - int Register(const char *,const char *); - int CreateGroup(const char*,const char*); + static vectorv; + static string str; + static vector< map > 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 GetMember(const char*); private: sqlite3 * handler;//句柄