4 #ifndef DBALLE_SQL_MYSQL_H
5 #define DBALLE_SQL_MYSQL_H
15 struct MySQLStatement;
25 error_mysql(
const std::string& dbmsg,
const std::string& msg);
28 const char* what()
const noexcept
override {
return msg.c_str(); }
39 bool has_passwd =
false;
41 bool has_dbname =
false;
44 std::string unix_socket;
48 void parse_url(
const std::string& url);
50 std::string to_url()
const;
55 MYSQL_RES* res =
nullptr;
56 MYSQL_ROW row =
nullptr;
58 Row(MYSQL_RES* res, MYSQL_ROW row) : res(res), row(row) {}
60 operator bool()
const {
return row !=
nullptr; }
61 operator MYSQL_ROW() {
return row; }
62 operator const MYSQL_ROW()
const {
return row; }
64 int as_int(
unsigned col)
const {
return strtol(row[col], 0, 10); }
65 unsigned as_unsigned(
unsigned col)
const {
return strtoul(row[col], 0, 10); }
66 const char* as_cstring(
unsigned col)
const {
return row[col]; }
67 std::string as_string(
unsigned col)
const {
return std::string(row[col], mysql_fetch_lengths(res)[col]); }
68 std::vector<uint8_t> as_blob(
unsigned col)
const
70 return std::vector<uint8_t>(row[col], row[col] + mysql_fetch_lengths(res)[col]);
73 bool isnull(
unsigned col)
const {
return row[col] ==
nullptr; }
78 MYSQL_RES* res =
nullptr;
81 Result(MYSQL_RES* res) : res(res) {}
82 ~
Result() {
if (res) mysql_free_result(res); }
88 if (
this == &o)
return *
this;
89 if (res) mysql_free_result(res);
95 operator bool()
const {
return res !=
nullptr; }
97 operator MYSQL_RES*() {
return res; }
98 operator const MYSQL_RES*()
const {
return res; }
100 unsigned rowcount()
const {
return mysql_num_rows(res); }
101 unsigned colcount()
const {
return mysql_num_fields(res); }
135 void init_after_connect();
148 operator MYSQL*() {
return db; }
151 void open_url(
const std::string& url);
155 std::string
escape(
const char* str);
157 std::string
escape(
const std::string& str);
159 std::string
escape(
const std::vector<uint8_t>& str);
167 void exec_no_data(
const char* query);
169 void exec_no_data(
const std::string& query);
175 void exec_use(
const char* query, std::function<
void(
const mysql::Row&)> dest);
177 void exec_use(
const std::string& query, std::function<
void(
const mysql::Row&)> dest);
179 std::unique_ptr<Transaction>
transaction(
bool readonly=
false)
override;
180 bool has_table(
const std::string& name)
override;
181 std::string
get_setting(
const std::string& key)
override;
182 void set_setting(
const std::string& key,
const std::string& value)
override;
184 void execute(
const std::string& query)
override;
185 void explain(
const std::string& query, FILE* out)
override;