Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e97987b

Browse files
committed
增加对数据库的封装
1 parent 58718ac commit e97987b

File tree

12 files changed

+242022
-45
lines changed

12 files changed

+242022
-45
lines changed

PlayServer/DataBaseHelper.h

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#pragma once
2+
#include "Public.h"
3+
#include <map>
4+
#include <list>
5+
#include <memory>
6+
#include <vector>
7+
class _Table_;
8+
using PTable = std::shared_ptr<_Table_>;
9+
using KeyValue = std::map<Buffer, Buffer>;
10+
using Result = std::list<PTable>;
11+
class CDatabaseClient
12+
{
13+
public:
14+
CDatabaseClient(const CDatabaseClient&) =
15+
delete;
16+
CDatabaseClient& operator=(const
17+
CDatabaseClient&) = delete;
18+
public:
19+
CDatabaseClient() {}
20+
virtual ~CDatabaseClient() {}
21+
public:
22+
//连接
23+
virtual int Connect(const KeyValue& args) =
24+
0;
25+
//执行
26+
virtual int Exec(const Buffer& sql) = 0;
27+
//带结果的执行
28+
virtual int Exec(const Buffer& sql, Result&
29+
result, const _Table_& table) = 0;
30+
//开启事务
31+
virtual int StartTransaction() = 0;
32+
//提交事务
33+
virtual int CommitTransaction() = 0;
34+
//回滚事务
35+
virtual int RollbackTransaction() = 0;
36+
//关闭连接
37+
virtual int Close() = 0;
38+
//是否连接
39+
virtual bool IsConnected() = 0;
40+
};
41+
//表和列的基类的实现
42+
class _Field_;
43+
using PField = std::shared_ptr<_Field_>;
44+
using FieldArray = std::vector<PField>;
45+
using FieldMap = std::map<Buffer, PField>;
46+
class _Table_ {
47+
public:
48+
_Table_() {}
49+
virtual ~_Table_() {}
50+
//返回创建的SQL语句
51+
virtual Buffer Create() = 0;
52+
//删除表
53+
virtual Buffer Drop() = 0;
54+
//增删改查
55+
//TODO:参数进行优化
56+
virtual Buffer Insert(const _Table_& values)
57+
= 0;
58+
virtual Buffer Delete(const _Table_& values)
59+
= 0;
60+
//TODO:参数进行优化
61+
virtual Buffer Modify(const _Table_& values)
62+
= 0;
63+
virtual Buffer Query() = 0;
64+
//创建一个基于表的对象
65+
virtual PTable Copy()const = 0;
66+
virtual void ClearFieldUsed() = 0;
67+
public:
68+
//获取表的全名
69+
virtual operator const Buffer() const = 0;
70+
public:
71+
//表所属的DB的名称
72+
Buffer Database;
73+
Buffer Name;
74+
FieldArray FieldDefine;//列的定义(存储查询结果)
75+
FieldMap Fields;//列的定义映射表
76+
};
77+
enum {
78+
SQL_INSERT = 1,//插入的列
79+
SQL_MODIFY = 2,//修改的列
80+
SQL_CONDITION = 4//查询条件列
81+
};
82+
enum {
83+
NOT_NULL = 1,
84+
DEFAULT = 2,
85+
UNIQUE = 4,
86+
PRIMARY_KEY = 8,
87+
CHECK = 16,
88+
AUTOINCREMENT = 32
89+
};
90+
using SqlType = enum {
91+
TYPE_NULL = 0,
92+
TYPE_BOOL = 1,
93+
TYPE_INT = 2,
94+
TYPE_DATETIME = 4,
95+
TYPE_REAL = 8,
96+
TYPE_VARCHAR = 16,
97+
TYPE_TEXT = 32,
98+
TYPE_BLOB = 64
99+
};
100+
class _Field_
101+
{
102+
public:
103+
_Field_() {}
104+
_Field_(const _Field_& field) {
105+
Name = field.Name;
106+
Type = field.Type;
107+
Attr = field.Attr;
108+
Default = field.Default;
109+
Check = field.Check;
110+
}
111+
virtual _Field_& operator=(const _Field_&
112+
field) {
113+
if (this != &field) {
114+
Name = field.Name;
115+
Type = field.Type;
116+
Attr = field.Attr;
117+
Default = field.Default;
118+
Check = field.Check;
119+
}
120+
return *this;
121+
}
122+
virtual ~_Field_() {}
123+
public:
124+
virtual Buffer Create() = 0;
125+
virtual void LoadFromStr(const Buffer& str)
126+
= 0;
127+
//where 语句使用的
128+
virtual Buffer toEqualExp() const = 0;
129+
virtual Buffer toSqlStr() const = 0;
130+
//列的全名
131+
virtual operator const Buffer() const = 0;
132+
public:
133+
Buffer Name;
134+
Buffer Type;
135+
Buffer Size;
136+
unsigned Attr;
137+
Buffer Default;
138+
Buffer Check;
139+
public:
140+
//操作条件
141+
unsigned Condition;
142+
};

PlayServer/DatabaseHelper.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "DatabaseHelper.h"

PlayServer/PlayServer.vcxproj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,30 +75,38 @@
7575
<ImportGroup Label="PropertySheets" />
7676
<PropertyGroup Label="UserMacros" />
7777
<ItemGroup>
78+
<ClCompile Include="DatabaseHelper.cpp" />
7879
<ClCompile Include="HttpParser.cpp" />
7980
<ClCompile Include="http_parser.c" />
8081
<ClCompile Include="Loggere.cpp" />
8182
<ClCompile Include="main.cpp" />
8283
<ClCompile Include="Server.cpp" />
84+
<ClCompile Include="Sqlite3Client.cpp" />
85+
<ClCompile Include="sqlite3\sqlite3.c" />
8386
<ClCompile Include="Thread.cpp" />
8487
<ClCompile Include="ThreadPool.cpp" />
8588
</ItemGroup>
8689
<ItemGroup>
90+
<ClInclude Include="DataBaseHelper.h" />
8791
<ClInclude Include="EdoyunPlayerServer.h" />
8892
<ClInclude Include="HttpParser.h" />
8993
<ClInclude Include="http_parser.h" />
9094
<ClInclude Include="Loggere.h" />
95+
<ClInclude Include="Public.h" />
9196
<ClInclude Include="Server.h" />
9297
<ClInclude Include="Socket.h" />
9398
<ClInclude Include="Epoll.h" />
9499
<ClInclude Include="Function.h" />
95100
<ClInclude Include="Process.h" />
101+
<ClInclude Include="Sqlite3Client.h" />
102+
<ClInclude Include="sqlite3\sqlite3.h" />
103+
<ClInclude Include="sqlite3\sqlite3ext.h" />
96104
<ClInclude Include="Thread.h" />
97105
<ClInclude Include="ThreadPool.h" />
98106
</ItemGroup>
99107
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
100108
<Link>
101-
<LibraryDependencies>pthread;m;</LibraryDependencies>
109+
<LibraryDependencies>pthread;m;dl;</LibraryDependencies>
102110
</Link>
103111
</ItemDefinitionGroup>
104112
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<ClCompile Include="DatabaseHelper.cpp" />
5+
<ClCompile Include="HttpParser.cpp" />
6+
<ClCompile Include="http_parser.c" />
7+
<ClCompile Include="Loggere.cpp" />
8+
<ClCompile Include="main.cpp" />
9+
<ClCompile Include="Server.cpp" />
10+
<ClCompile Include="Sqlite3Client.cpp" />
11+
<ClCompile Include="Thread.cpp" />
12+
<ClCompile Include="ThreadPool.cpp" />
13+
<ClCompile Include="sqlite3\sqlite3.c">
14+
<Filter>sqllite3</Filter>
15+
</ClCompile>
16+
</ItemGroup>
17+
<ItemGroup>
18+
<ClInclude Include="DataBaseHelper.h" />
19+
<ClInclude Include="EdoyunPlayerServer.h" />
20+
<ClInclude Include="HttpParser.h" />
21+
<ClInclude Include="http_parser.h" />
22+
<ClInclude Include="Loggere.h" />
23+
<ClInclude Include="Public.h" />
24+
<ClInclude Include="Server.h" />
25+
<ClInclude Include="Socket.h" />
26+
<ClInclude Include="Epoll.h" />
27+
<ClInclude Include="Function.h" />
28+
<ClInclude Include="Process.h" />
29+
<ClInclude Include="Sqlite3Client.h" />
30+
<ClInclude Include="Thread.h" />
31+
<ClInclude Include="ThreadPool.h" />
32+
<ClInclude Include="sqlite3\sqlite3.h">
33+
<Filter>sqllite3</Filter>
34+
</ClInclude>
35+
<ClInclude Include="sqlite3\sqlite3ext.h">
36+
<Filter>sqllite3</Filter>
37+
</ClInclude>
38+
</ItemGroup>
39+
<ItemGroup>
40+
<Filter Include="sqllite3">
41+
<UniqueIdentifier>{d29c112b-49af-47a1-bdda-8b0e6f8df513}</UniqueIdentifier>
42+
</Filter>
43+
</ItemGroup>
44+
</Project>

PlayServer/Public.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#pragma once
2+
#include"Socket.h"
3+
#include<string.h>
4+
5+
class Buffer : public std::string
6+
{
7+
public:
8+
Buffer(size_t size) : std::string() { resize(size); }
9+
Buffer(const std::string& str) : std::string(str) { }
10+
Buffer(const char* str) : std::string(str) { }
11+
Buffer(const char* str, size_t length) : std::string(str) {
12+
resize(length);
13+
memcpy((char*)c_str(), str, length);
14+
}
15+
Buffer(const char* begin, const char* end) : std::string()
16+
{
17+
long int len = end - begin;
18+
if (len > 0)
19+
{
20+
resize(len);
21+
memcpy((char*)c_str(), begin, len);
22+
}
23+
}
24+
Buffer() : std::string() {}
25+
~Buffer() {}
26+
27+
public:
28+
//给非常量对象使用
29+
operator char* ()
30+
{
31+
return const_cast<char*>(c_str());
32+
}
33+
//给常量对象使用
34+
operator char* () const
35+
{
36+
return const_cast<char*>(c_str());
37+
}
38+
operator const char* ()
39+
{
40+
return c_str();
41+
}
42+
operator const char* () const
43+
{
44+
return c_str();
45+
}
46+
47+
private:
48+
49+
};

PlayServer/Socket.h

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,7 @@
66
#include<arpa/inet.h>
77
#include<string>
88
#include<fcntl.h>
9-
class Buffer : public std::string
10-
{
11-
public:
12-
Buffer(size_t size) : std::string() { resize(size); }
13-
Buffer(const std::string& str) : std::string(str) { }
14-
Buffer(const char* str) : std::string(str) { }
15-
Buffer(const char* str, size_t length) : std::string(str) {
16-
resize(length);
17-
memcpy((char*)c_str(), str, length);
18-
}
19-
Buffer(const char* begin, const char* end) : std::string()
20-
{
21-
long int len = end - begin;
22-
if (len > 0)
23-
{
24-
resize(len);
25-
memcpy((char*)c_str(), begin, len);
26-
}
27-
}
28-
Buffer() : std::string() {}
29-
~Buffer(){}
30-
31-
public:
32-
//给非常量对象使用
33-
operator char* ()
34-
{
35-
return const_cast<char*>(c_str());
36-
}
37-
//给常量对象使用
38-
operator char* () const
39-
{
40-
return const_cast<char*>(c_str());
41-
}
42-
operator const char* ()
43-
{
44-
return c_str();
45-
}
46-
operator const char* () const
47-
{
48-
return c_str();
49-
}
509

51-
private:
52-
53-
};
5410

5511
enum SOCKATTR {
5612
SOCK_ISSERVER = 1,//0是客户端 1是服务器

0 commit comments

Comments
 (0)