-
Notifications
You must be signed in to change notification settings - Fork 1
/
NgxDataBase.hpp
153 lines (132 loc) · 4.37 KB
/
NgxDataBase.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#ifndef __NGXDATABASE_HPP__
#define __NGXDATABASE_HPP__
#include "HeaderPrecompilation.hpp"
#include <exception>
enum class DataBaseType{
MYSQL,
SQLITE
};
enum class style{
legacy,
modern
};
class NgxDataBase{
public:
NgxDataBase()=delete;
NgxDataBase(const char* _dir){
root_dir=_dir;
}
//函数对象传递,执行数据库操作
template<::style=style::modern>
rapidjson::Document operator() (const char*,const char*,bool&);
private:
std::string root_dir;
};
template<>
rapidjson::Document NgxDataBase::operator()<style::legacy>(const char* dname,const char* stm,bool& status)try{
rapidjson::Document result;
result.SetObject();
rapidjson::Document::AllocatorType &_dallocator = result.GetAllocator();
std::stringstream dataBasePath;
dataBasePath << root_dir.c_str()<<"/"<< dname << ".db";
SQLite::Database db(dataBasePath.str().c_str(), SQLite::OPEN_READWRITE);
SQLite::Statement query(db, stm);
//步数做为key进行查询,表示第几行
size_t _step = 0;
rapidjson::Value resultArray(rapidjson::kArrayType);
resultArray.SetArray();
while (query.executeStep())
{
rapidjson::Value one_element(rapidjson::kArrayType);
for (int index = 0; index < query.getColumnCount(); index++)
{
switch (query.getColumn(index).getType())
{
case SQLITE_TEXT:
{
one_element.PushBack(rapidjson::Value(query.getColumn(index).getString().c_str(), _dallocator).Move(), _dallocator);
break;
}
case SQLITE_INTEGER:
{
one_element.PushBack(rapidjson::Value(std::to_string(query.getColumn(index).getInt64()).c_str(), _dallocator).Move(), _dallocator);
break;
}
case SQLITE_FLOAT:
{
one_element.PushBack(rapidjson::Value(std::to_string(query.getColumn(index).getDouble()).c_str(), _dallocator).Move(), _dallocator);
break;
}
default:
{
one_element.PushBack(rapidjson::Value(""), _dallocator);
}
break;
}
}
result.AddMember(rapidjson::Value(std::to_string(_step).c_str(), _dallocator), one_element.Move(), _dallocator);
_step++;
}
status=true;
return result;
}
catch (std::exception &e)
{
rapidjson::Document result;
result.SetObject();
rapidjson::Document::AllocatorType &_dallocator = result.GetAllocator();
result.AddMember("errmsg", rapidjson::Value(e.what(), _dallocator), _dallocator);
status = false;
return result;
}
template<>
rapidjson::Document NgxDataBase::operator()<style::modern>(const char* dname,const char* stm,bool& status)try{
rapidjson::Document result;
result.SetObject();
rapidjson::Document::AllocatorType &_dallocator = result.GetAllocator();
std::stringstream dataBasePath;
dataBasePath << root_dir.c_str() <<"/"<< dname << ".db";
SQLite::Database db(dataBasePath.str().c_str(), SQLite::OPEN_READWRITE);
SQLite::Statement query(db, stm);
//最终结果是一个数组
rapidjson::Value resultArray(rapidjson::kArrayType);
resultArray.SetArray();
while (query.executeStep())
{
//一次查询获得的所有数据放入一个one_element中
rapidjson::Value one_element(rapidjson::kObjectType);
for (int index = 0; index < query.getColumnCount(); index++)
{
switch (query.getColumn(index).getType())
{
case SQLITE_TEXT :
one_element.AddMember(rapidjson::Value(query.getColumnName(index), _dallocator), rapidjson::Value(query.getColumn(index).getText(), _dallocator), _dallocator);
break;
case SQLITE_INTEGER:
one_element.AddMember(rapidjson::Value(query.getColumnName(index), _dallocator), rapidjson::Value(query.getColumn(index).getInt()).Move(), _dallocator);
break;
case SQLITE_FLOAT:
one_element.AddMember(rapidjson::Value(query.getColumnName(index), _dallocator), rapidjson::Value(query.getColumn(index).getDouble()).Move(), _dallocator);
break;
default:
one_element.AddMember(rapidjson::Value(query.getColumnName(index), _dallocator), rapidjson::Value(""), _dallocator);
break;
}
}
//将每次查询结果one_elemnt作为一个元素放入数组中
resultArray.PushBack(one_element.Move(), _dallocator);
}
result.AddMember("sqlite", resultArray.Move(), _dallocator);
status=true;
return result;
}
catch (std::exception &e)
{
rapidjson::Document result;
result.SetObject();
rapidjson::Document::AllocatorType &_dallocator = result.GetAllocator();
result.AddMember("errmsg", rapidjson::Value(e.what(), _dallocator), _dallocator);
status = false;
return result;
}
#endif