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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
| #include <mysql/mysql.h> #include <sspdlog/sspdlog.h> #include <oyoung/format.hpp>
namespace mysql { struct library { library(int argc, char **argv, char **groups) { if(mysql_library_init(argc, argv, groups)) { throw std::runtime_error("MYSQL Initialize failed"); } }
~library() { mysql_library_end(); } };
struct row {
row(MYSQL_ROW row): m_row(row) {}
~row() {}
operator bool() const { return m_row; }
std::string operator[](std::size_t index) const { return m_row[index]; }
row(const row&) = delete; row(row&& other): m_row(other.m_row) { other.m_row = nullptr; }
private: MYSQL_ROW m_row{nullptr}; };
struct field {
field(MYSQL_FIELD *field): m_field(field) {}
std::string name() const {
return m_field->name; }
std::string catalog() const { return std::string(m_field->catalog, m_field->catalog_length); }
~field() {
}
field(const field&) = delete; field(field&& other): m_field(other.m_field) { other.m_field = nullptr; }
protected: MYSQL_FIELD *m_field{nullptr}; };
struct fields : field {
fields(MYSQL_FIELD *f) : field(f) {}
field operator[](std::size_t index) { return m_field + index; } };
struct result { result() = default; result(MYSQL_RES *res): m_result(res) {}
~result() { if(m_result) { ::mysql_free_result(m_result); } }
result(const result&) = delete; result(result&& other): m_result(other.m_result) { other.m_result = nullptr; }
row fetch_row() { return mysql_fetch_row(m_result); }
std::uint64_t rows() const { return mysql_num_rows(m_result); }
unsigned field_count() const { return ::mysql_num_fields(m_result); }
fields fetch_fields() { return mysql_fetch_fields(m_result); }
private: MYSQL_RES *m_result {nullptr}; };
struct client { client() : m_client(mysql_init(nullptr)) {
}
bool good() const { return nullptr != m_client; }
bool not_good() const { return nullptr == m_client; }
bool connect(const std::string& host, const std::string& user, const std::string& password, const std::string& database, unsigned short port = 3306) { return mysql_real_connect(m_client, host.c_str(), user.c_str(), password.c_str(), database.c_str(), port, nullptr, 0) != nullptr; }
bool query(const std::string& sql) { return mysql_real_query(m_client, sql.c_str(), sql.length()) == 0; }
std::uint64_t affected_rows() const { return mysql_affected_rows(m_client); }
std::uint64_t inserted_id() const { return mysql_insert_id(m_client); }
result use_result() const { return result(mysql_use_result(m_client)); }
~client() {
}
std::string error() const { return mysql_error(m_client); }
private: MYSQL *m_client; }; }
int main(int argc, char**argv) try {
mysql::library library(argc, argv, nullptr); mysql::client client{};
if(client.not_good()) { SSPD_LOG_ERROR << "Create MySQL client failed"; return -1; }
if(!client.connect("172.17.0.2", "blog", "blog.123", "blog")) { SSPD_LOG_ERROR << "MySQL connect failed: " << client.error(); return -2; }
if(!client.query("INSERT INTO user set name='blog', email='blog@hotmail.com'")) { SSPD_LOG_ERROR << "MySQL query INSERT failed: " << client.error(); return -3; }
SSPD_LOG_INFO << "MySQL affected rows after insert: " << client.affected_rows(); SSPD_LOG_INFO << "MySQL last inserted ID: " << client.inserted_id();
if(!client.query("SELECT id, name, email FROM user")) { SSPD_LOG_ERROR << "MySQL query SELECT failed: " << client.error(); return -3; }
auto result = client.use_result();
auto field_count = result.field_count(); auto fields = result.fetch_fields();
while (auto row = result.fetch_row()) { for(auto i = 0; i < field_count; ++i) { SSPD_LOG_INFO << fields[i].name() << ": " << row[i]; } }
if(!client.query(oyoung::format("DELETE FROM user WHERE id=%1").arg(client.inserted_id()).to_string())) { SSPD_LOG_ERROR << "MySQL query DELETE failed: " << client.error(); return -3; }
SSPD_LOG_INFO << "MySQL affected rows after delete: " << client.affected_rows();
return 0; } catch(const std::exception& e) { SSPD_LOG_ERROR << e.what(); }
|