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 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); }
137 void init_after_connect();
144 void fork_prepare()
override;
145 void fork_parent()
override;
146 void fork_child()
override;
148 void check_connection();
157 static std::shared_ptr<MySQLConnection> create();
159 operator MYSQL*() {
return db; }
162 void open_url(
const std::string& url);
166 std::string
escape(
const char* str);
168 std::string
escape(
const std::string& str);
170 std::string
escape(
const std::vector<uint8_t>& str);
178 void exec_no_data(
const char* query);
180 void exec_no_data(
const std::string& query);
186 void exec_use(
const char* query, std::function<
void(
const mysql::Row&)> dest);
188 void exec_use(
const std::string& query, std::function<
void(
const mysql::Row&)> dest);
190 std::unique_ptr<Transaction>
transaction(
bool readonly=
false)
override;
191 bool has_table(
const std::string& name)
override;
192 std::string
get_setting(
const std::string& key)
override;
193 void set_setting(
const std::string& key,
const std::string& value)
override;
195 void execute(
const std::string& query)
override;
196 void explain(
const std::string& query, FILE* out)
override;
void execute(const std::string &query) override
Execute a query without reading its results.
MYSQL * db
Database connection.
Definition: mysql.h:130
Row fetch()
Fetch one row.
Definition: mysql.h:115
void drop_settings() override
Drop the settings table.
Report a MySQL error.
Definition: mysql.h:20
Result(Result &&o)
Implement move.
Definition: mysql.h:85
bool forked
Marker to catch attempts to reuse connections in forked processes.
Definition: mysql.h:132
std::unique_ptr< Transaction > transaction(bool readonly=false) override
Begin a transaction.
int get_last_insert_id()
Return LAST_INSERT_ID or LAST_INSER_ROWID or whatever is appropriate for the current database...
std::string escape(const char *str)
Escape a C string.
void drop_table_if_exists(const char *name)
Delete a table in the database if it exists, otherwise do nothing.
std::string get_setting(const std::string &key) override
Get a value from the settings table.
bool has_table(const std::string &name) override
Check if the database contains a table.
Common infrastructure for talking with SQL databases.
Error in case of failed database operations.
Definition: error.h:21
Date and time.
Definition: types.h:164
void explain(const std::string &query, FILE *out) override
Format and print the EXPLAIN output for the query to the given file.
#define WREPORT_THROWF_ATTRS(a, b)
Row expect_one_result()
Check that the function returned only one row, and return that row.
void exec_no_data_nothrow(const char *query) noexcept
Run a query throwing no exceptions, warning on stderr if it is not successful or if it gives a nonemp...
void set_setting(const std::string &key, const std::string &value) override
Set a value in the settings table.
Database connection.
Definition: mysql.h:126