-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathexamples.cpp
208 lines (179 loc) · 5.85 KB
/
examples.cpp
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "MySql.hpp"
#include "MySqlException.hpp"
#include <cassert>
#include <iostream>
#include <memory>
#include <string>
#include <tuple>
#include <vector>
using std::basic_ostream;
using std::cin;
using std::cout;
using std::endl;
using std::get;
using std::ostream;
using std::string;
using std::tuple;
using std::unique_ptr;
using std::vector;
template <std::size_t> struct int_ {}; // compile-time counter
template <typename Char, typename Traits, typename Tuple, std::size_t I>
void printTuple(basic_ostream<Char, Traits>& out, Tuple const& t, int_<I>);
template <typename Char, typename Traits, typename Tuple>
void printTuple(basic_ostream<Char, Traits>& out, Tuple const& t, int_<0>);
template <typename Tuple>
void printTupleVector(const vector<Tuple>& v);
template <typename Char, typename Traits, typename... Args>
ostream& operator<<(basic_ostream<Char, Traits>& out, tuple<Args...> const& t);
template <typename T>
ostream& operator<<(ostream& out, const unique_ptr<T>& ptr);
int main(int argc, char* argv[]) {
string password;
if (argc == 1) {
cout << "Enter MySQL root password: " << endl;
cin >> password;
} else {
password = argv[1];
}
MySql conn("127.0.0.1", "root", password.c_str(), nullptr);
// Initialize a new test database
conn.runCommand("DROP DATABASE IF EXISTS test_mysql_cpp");
conn.runCommand("CREATE DATABASE test_mysql_cpp");
conn.runCommand("USE test_mysql_cpp");
conn.runCommand("DROP TABLE IF EXISTS user");
conn.runCommand(
"CREATE TABLE user ("
"id INT NOT NULL AUTO_INCREMENT,"
"PRIMARY KEY(id),"
"email VARCHAR(64) NOT NULL,"
"password CHAR(64) NOT NULL,"
"age INT)");
// ************
// Easy inserts
// ************
int ages[] = {27, 21, 26};
string emails[] = {
"bskari@yelp.com",
"brandon.skari@gmail.com",
"brandon@skari.org"};
string passwords[] = {
"peace",
"love",
"griffin"};
conn.runCommand(
"INSERT INTO user (email, password, age)"
" VALUES (?, ?, ?), (?, ?, ?), (?, ?, ?)",
emails[0], passwords[0], ages[0],
emails[1], passwords[1], ages[1],
emails[2], passwords[2], ages[2]);
typedef tuple<int, string, string, int> userTuple;
vector<userTuple> users;
// *****************************************
// All commands use safe prepared statements
// *****************************************
const string naughtyUser("brandon@skari.org'; DROP TABLE users; -- ");
conn.runQuery(&users, "SELECT * FROM user WHERE email = ?", naughtyUser);
assert(0 == users.size());
const char naughtyUser2[] = "something' OR '1' = 1' -- ";
const char* charPtr = naughtyUser2;
conn.runQuery(&users, "SELECT * FROM user WHERE email = ?", charPtr);
assert(0 == users.size());
// ***************************
// Automatically typed selects
// ***************************
conn.runQuery(&users, "SELECT * FROM user");
printTupleVector(users);
users.clear();
// ************************
// Dealing with NULL values
// ************************
conn.runCommand(
"INSERT INTO user (email, password, age) VALUES (?, ?, NULL)",
emails[0],
passwords[0]);
try {
// Trying to insert NULLs into a normal tuple will throw
conn.runQuery(&users, "SELECT * FROM user");
} catch (const MySqlException& e) {
cout << e.what() << endl;
}
// But, we can select into tuples with unique_ptr
typedef tuple<
unique_ptr<int>,
unique_ptr<string>,
unique_ptr<string>,
unique_ptr<int>
> autoPtrUserTuple;
vector<autoPtrUserTuple> autoPtrUsers;
conn.runQuery(&autoPtrUsers, "SELECT * FROM user");
printTupleVector(autoPtrUsers);
// *********************************************
// Raw pointers are gross, so this won't compile
// *********************************************
/*
vector<tuple<int*>> rawPointers;
conn.runQuery(&rawPointers, "SELECT age FROM user");
*/
// **************************************
// Look at all these type-based failures!
// **************************************
try {
// Wrong number of fields
vector<tuple<int>> selectAges;
conn.runQuery(&selectAges, "SELECT * FROM user");
} catch (const MySqlException& e) {
cout << e.what() << endl;
}
exit(EXIT_SUCCESS);
}
template<typename Char, typename Traits, typename Tuple, size_t I>
void printTuple(basic_ostream<Char, Traits>& out, Tuple const& t, int_<I>) {
printTuple(out, t, int_<I - 1>());
out << ", " << get<I>(t);
}
template<typename Char, typename Traits, typename Tuple>
void printTuple(basic_ostream<Char, Traits>& out, Tuple const& t, int_<0>) {
out << get<0>(t);
}
template <typename Tuple>
void printTupleVector(const vector<Tuple>& v) {
#if __GNUC__ >= 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
for (const auto& item : v)
{
cout << item << endl;
}
#elif __GNUC__ >= 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)
auto end = v.cend();
for (auto item(v.cbegin()); item != end; ++item) {
cout << *item<< endl;
}
#else
vector<Tuple>::const_iterator end(users.end());
for (
vector<Tuple>::const_iterator item(v.begin());
item != end;
++item
) {
cout << *item << endl;
}
#endif
}
template<typename Char, typename Traits, typename... Args>
ostream& operator<<(
basic_ostream<Char, Traits>& out,
tuple<Args...> const& t
) {
out << "(";
printTuple(out, t, int_<sizeof...(Args) - 1>());
out << ")";
return out;
}
template <typename T>
ostream& operator<<(ostream& out, const unique_ptr<T>& ptr) {
if (nullptr != ptr.get()) {
out << *ptr;
} else {
out << "NULL";
}
return out;
}