- Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathdevapi_test.cc
174 lines (140 loc) · 4.64 KB
/
devapi_test.cc
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* Copyright (c) 2015, 2024, Oracle and/or its affiliates.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2.0, as
* published by the Free Software Foundation.
*
* This program is designed to work with certain software (including
* but not limited to OpenSSL) that is licensed under separate terms, as
* designated in a particular file or component or in included license
* documentation. The authors of MySQL hereby grant you an additional
* permission to link the program and your derivative works with the
* separately licensed software that they have either included with
* the program or referenced in the documentation.
*
* Without limiting anything contained in the foregoing, this file,
* which is part of Connector/C++, is also subject to the
* Universal FOSS Exception, version 1.0, a copy of which can be found at
* https://oss.oracle.com/licenses/universal-foss-exception.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License, version 2.0, for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include<mysqlx/xdevapi.h>
#include<iostream>
using ::std::cout;
using ::std::endl;
usingnamespace ::mysqlx;
intmain(int argc, constchar* argv[])
try {
constchar *url = (argc > 1 ? argv[1] : "mysqlx://root@127.0.0.1");
cout << "Creating session on " << url
<< " ..." << endl;
Session sess(url);
{
RowResult res = sess.sql("show variables like 'version'").execute();
std::stringstream version;
version << res.fetchOne().get(1).get<string>();
int major_version;
version >> major_version;
if (major_version < 8)
{
cout << "Can work only with MySQL Server 8 or later" << endl;
cout << "Done!" << endl;
return0;
}
}
cout << "Session accepted, creating collection..." << endl;
Schema sch = sess.createSchema("test", true);
Collection coll= sch.createCollection("c1", true);
cout << "Inserting documents..." << endl;
coll.remove("true").execute();
{
Result add;
add= coll.add(R"({ "name": "foo", "age": 1 })").execute();
std::vector<string> ids = add.getGeneratedIds();
cout << "- added doc with id: " << ids[0] << endl;
add= coll.add(R"({ "name": "bar", "age": 2, "toys": [ "car", "ball" ] })")
.execute();
ids = add.getGeneratedIds();
if (ids.size() != 0)
cout << "- added doc with id: " << ids[0] << endl;
else
cout << "- added doc" << endl;
add= coll.add(R"({
"name": "baz",
"age": 3,
"date": { "day": 20, "month": "Apr" }
})").execute();
ids = add.getGeneratedIds();
if (ids.size() != 0)
cout << "- added doc with id: " << ids[0] << endl;
else
cout << "- added doc" << endl;
add= coll.add(R"({ "_id": "myuuid-1", "name": "foo", "age": 7 })")
.execute();
ids = add.getGeneratedIds();
if (ids.size() != 0)
cout << "- added doc with id: " << ids[0] << endl;
else
cout << "- added doc" << endl;
}
cout << "Fetching documents..." << endl;
DocResult docs = coll.find("age > 1 and name like 'ba%'").execute();
int i = 0;
for (DbDoc doc : docs)
{
cout << "doc#" << i++ << ": " << doc << endl;
for (Field fld : doc)
{
cout << " field `" << fld << "`: " << doc[fld] << endl;
}
string name = doc["name"];
cout << " name: " << name << endl;
if (doc.hasField("date") && Value::DOCUMENT == doc.fieldType("date"))
{
cout << "- date field" << endl;
DbDoc date = doc["date"];
for (Field fld : date)
{
cout << " date `" << fld << "`: " << date[fld] << endl;
}
string month = doc["date"]["month"];
int day = date["day"];
cout << " month: " << month << endl;
cout << " day: " << day << endl;
}
if (doc.hasField("toys") && Value::ARRAY == doc.fieldType("toys"))
{
cout << "- toys:" << endl;
for (auto toy : doc["toys"])
{
cout << "" << toy << endl;
}
}
cout << endl;
}
cout << "Done!" << endl;
}
catch (const mysqlx::Error &err)
{
cout << "ERROR: " << err << endl;
return1;
}
catch (std::exception &ex)
{
cout << "STD EXCEPTION: " << ex.what() << endl;
return1;
}
catch (constchar *ex)
{
cout << "EXCEPTION: " << ex << endl;
return1;
}