Skip to content

Commit 50a88b1

Browse files
committed
修改:README内容
1 parent f940e8c commit 50a88b1

File tree

1 file changed

+115
-120
lines changed

1 file changed

+115
-120
lines changed

README.md

Lines changed: 115 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,138 +1,133 @@
11
# MySQL Connector/C++
22

3-
This is a release of MySQL Connector/C++, [the C++ interface](https://dev.mysql.com/doc/dev/connector-cpp/8.0/) for communicating with MySQL servers.
3+
## ABE支持
44

5-
For detailed information please visit the official [MySQL Connector/C++ documentation](https://dev.mysql.com/doc/dev/connector-cpp/8.0/).
5+
### 使用
66

7-
## Licensing
7+
要使用ABE功能,需引用abe_extern.h头文件。
88

9-
Please refer to files README and LICENSE, available in this repository, and [Legal Notices in documentation](https://dev.mysql.com/doc/connector-cpp/8.0/en/preface.html) for further details.
9+
开放接口和使用方法可参考如下示例:
1010

11-
## Download & Install
12-
13-
MySQL Connector/C++ can be installed from pre-compiled packages that can be downloaded from the [MySQL downloads page](https://dev.mysql.com/downloads/connector/cpp/).
14-
The process of installing of Connector/C++ from a binary distribution is described in [MySQL online manuals](https://dev.mysql.com/doc/connector-cpp/8.0/en/connector-cpp-installation-binary.html)
15-
16-
### Building from sources
17-
18-
MySQL Connector/C++ can be installed from the source. Please check [MySQL online manuals](https://dev.mysql.com/doc/connector-cpp/8.0/en/connector-cpp-installation-source.html)
19-
20-
### GitHub Repository
21-
22-
This repository contains the MySQL Connector/C++ source code as per latest released version. You should expect to see the same contents here and within the latest released Connector/C++ package.
23-
24-
## Sample Code
25-
26-
```
11+
```cpp
2712
#include <iostream>
28-
#include <mysqlx/xdevapi.h>
29-
30-
using ::std::cout;
31-
using ::std::endl;
32-
using namespace ::mysqlx;
33-
34-
35-
int main(int argc, const char* argv[])
36-
try {
37-
38-
const char *url = (argc > 1 ? argv[1] : "mysqlx://[email protected]");
39-
40-
cout << "Creating session on " << url
41-
<< " ..." << endl;
42-
43-
Session sess(url);
44-
45-
cout <<"Session accepted, creating collection..." <<endl;
46-
47-
Schema sch= sess.getSchema("test");
48-
Collection coll= sch.createCollection("c1", true);
49-
50-
cout <<"Inserting documents..." <<endl;
51-
52-
coll.remove("true").execute();
53-
54-
{
55-
DbDoc doc(R"({ "name": "foo", "age": 1 })");
56-
57-
Result add =
58-
coll.add(doc)
59-
.add(R"({ "name": "bar", "age": 2, "toys": [ "car", "ball" ] })")
60-
.add(R"({ "name": "bar", "age": 2, "toys": [ "car", "ball" ] })")
61-
.add(R"({
62-
"name": "baz",
63-
"age": 3,
64-
"date": { "day": 20, "month": "Apr" }
65-
})")
66-
.add(R"({ "_id": "myuuid-1", "name": "foo", "age": 7 })")
67-
.execute();
68-
69-
std::list<string> ids = add.getGeneratedIds();
70-
for (string id : ids)
71-
cout <<"- added doc with id: " << id <<endl;
72-
}
73-
74-
cout <<"Fetching documents..." <<endl;
75-
76-
DocResult docs = coll.find("age > 1 and name like 'ba%'").execute();
77-
78-
int i = 0;
79-
for (DbDoc doc : docs)
80-
{
81-
cout <<"doc#" <<i++ <<": " <<doc <<endl;
82-
83-
for (Field fld : doc)
84-
{
85-
cout << " field `" << fld << "`: " <<doc[fld] << endl;
86-
}
87-
88-
string name = doc["name"];
89-
cout << " name: " << name << endl;
90-
91-
if (doc.hasField("date") && Value::DOCUMENT == doc.fieldType("date"))
92-
{
93-
cout << "- date field" << endl;
94-
DbDoc date = doc["date"];
95-
for (Field fld : date)
96-
{
97-
cout << " date `" << fld << "`: " << date[fld] << endl;
98-
}
99-
string month = doc["date"]["month"];
100-
int day = date["day"];
101-
cout << " month: " << month << endl;
102-
cout << " day: " << day << endl;
103-
}
13+
#include <vector>
14+
#include<mysqlx/xdevapi.h>
15+
#include<mysqlx/xapi.h>
16+
#include<mysqlx/abe_extern.h>
17+
using namespace mysqlx;
18+
using std::cout;
19+
using std::endl;
20+
using std::vector;
21+
22+
//数据库连接
23+
Session get_connect(){
24+
//注意 mysqlcppconn8 默认使用的端口:33060,若连接3306端口会提示错误
25+
//使用用户名:testabe
26+
//用户 root 的密码是:123456
27+
//主机:172.17.0.9
28+
//端口:33060
29+
//数据库:company
30+
cout << "start connecting...\n";
31+
32+
SessionSettings option("172.17.0.9", 33060, "testabe", "123456");
33+
Session sess(option);
34+
35+
cout <<"Done!" <<endl;
36+
cout <<"Session accepted, creating collection..." <<endl;
37+
38+
sess.sql("use company").execute(); //使用数据库 webserver
39+
return sess;
40+
}
10441

105-
if (doc.hasField("toys") && Value::ARRAY == doc.fieldType("toys"))
106-
{
107-
cout << "- toys:" << endl;
108-
for (auto toy : doc["toys"])
109-
{
110-
cout << " " << toy << endl;
111-
}
42+
//正常用法
43+
void normal_example(Session &sess){
44+
cout << "normal query begin:" << endl;
45+
RowResult rs = sess.sql("select * from note").execute();
46+
for (auto it = rs.begin();it != rs.end();++it){
47+
cout << (*it).get(0).get<int>() <<" ";
48+
cout << (*it).get(1).get<string>(); //这个string是mysqlx的string,继承自std::u16string
49+
cout << endl;
11250
}
113-
114-
cout << endl;
115-
}
116-
cout <<"Done!" <<endl;
117-
}
118-
catch (const mysqlx::Error &err)
119-
{
120-
cout <<"ERROR: " <<err <<endl;
121-
return 1;
51+
cout << "normal query end." << endl;
12252
}
123-
catch (std::exception &ex)
124-
{
125-
cout <<"STD EXCEPTION: " <<ex.what() <<endl;
126-
return 1;
53+
void abe_example1(Session &sess) {
54+
/*
55+
* 1. 初始化abe环境
56+
* - 利用abe_parameters设置abe所需的参数
57+
* - 传入Session,创建abe_env对象
58+
* - 传入abe_parameters参数,初始化abe_env对象。 这一过程
59+
* 将会导入abe模块所需的abe密钥、RSA密钥(db/kms公钥,用户私钥)
60+
* 等信息。之后,将会执行预查询,包括获取用户名称、属性列表,如果
61+
* 之前没有导入abe用户密钥,还会通过预查询获取用户密钥,该密钥将
62+
* 经过验签、解密后存储到指定位置
63+
*
64+
*/
65+
abe_parameters params;
66+
params.abe_key_path = "/root/connector_demo/data/abe/abe_key";
67+
params.abe_pp_path = "/root/connector_demo/data/abe/abe_pp";
68+
params.db_cert_path = "/root/connector_demo/data/certs/dbcert.pem";
69+
params.kms_cert_path = "/root/connector_demo/data/certs/kmscert.pem";
70+
params.rsa_sk_path = "/root/connector_demo/data/prikey/testabe@%.pem";
71+
72+
abe_env env(sess);
73+
env.init(params);
74+
75+
cout << "abe query begin:" << endl;
76+
/*
77+
* 2. 执行abe查询,
78+
* - 查询执行的格式为:env.sql("sql query statement").execute();
79+
* 其中env.sql函数将会返回一个abe_query类型的对象,该对象存储了sql语句
80+
* 改写相关信息,改写和执行发生在abe_query类的execute方法。
81+
* - 加密sql示例:
82+
* insert into share2 values (2, abe_enc("hello,hello","attr1 and attr2"));
83+
* 其中,abe_enc为加密“函数”,第一个参数为原始明文,第二个参数为用户设置的abe访问策略
84+
* - 解密sql示例:
85+
* select id,title,abe_dec(data) from share;、
86+
* 其中abe_dec为解密“函数”,参数为要解密的列名
87+
* - 恢复明文
88+
* 查询得到的为密文,使用recover方法恢复明文,需要注意所有abe相关实现都是基于std::string
89+
* 所以在获取密文时需要使用get<std::string>()
90+
*/
91+
abe_query query = env.sql("select id,title,abe_dec(data) from share;");
92+
RowResult rs2 = query.execute();
93+
// cout << "real_sql = " << query.real_sql << endl;
94+
for (auto it = rs2.begin();it != rs2.end();++it){
95+
cout << (*it).get(0).get<int>() << "\t";
96+
cout << (*it).get(1).get<mysqlx::abi2::r0::string>() << "\t";
97+
auto ustr = (*it).get(2).get<std::string>();
98+
try{
99+
cout << env.recover(ustr);
100+
}catch(const Error &e){
101+
cout << "can't decrypt";
102+
}
103+
cout << endl;
104+
}
105+
cout << "abe query end." << endl;
106+
127107
}
128-
catch (const char *ex)
108+
int main()
129109
{
130-
cout <<"EXCEPTION: " <<ex <<endl;
131-
return 1;
110+
try{
111+
Session sess = get_connect();
112+
normal_example(sess);
113+
abe_example1(sess);
114+
115+
} catch (const Error& e) {
116+
cout << e.what() <<endl;
117+
}
118+
return 0;
132119
}
133-
134120
```
135121
122+
### 编译
123+
124+
本部分基于8.0.27分支开发,编译安装方式和8.0.27一致。
125+
126+
ABE相关头文件位于include/mysqlx目录下,相关cpp文件位于devapi目录下。
127+
128+
暂时使用的ABE加密库为openabe,需在文件devapi/abe/CMakeLists.txt中指定openabe等依赖的搜索路径。
129+
130+
136131
## Documentation
137132
138133
* [MySQL](http://www.mysql.com/)

0 commit comments

Comments
 (0)