SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
Database_mysqlconnector.h
Go to the documentation of this file.
1 /**
2  * @file Database_mysqlconnector.h
3  * @author Gereon Kremer <gereon.kremer@cs.rwth-aachen.de>
4  */
5 
6 #pragma once
7 
8 #include <mysql_connection.h>
9 #include <cppconn/driver.h>
10 #include <cppconn/exception.h>
11 #include <cppconn/resultset.h>
12 #include <cppconn/prepared_statement.h>
13 
14 #include "../logging.h"
15 
16 namespace benchmax {
17 
18 class DBAL {
19  std::unique_ptr<sql::Driver> driver;
20  std::unique_ptr<sql::Connection> conn;
21  std::string replacePlaceholders(const std::string& s) {
22  std::string res = s;
23  for (std::size_t i = 0; i < 10; i++) {
24  std::string pat = "%" + std::to_string(i) + "q";
25  while (true) {
26  std::size_t p = res.find(pat);
27  if (p == std::string::npos) break;
28  res = res.replace(p, pat.size(), "?");
29  }
30  }
31  return res;
32  }
33 public:
34  /// Indec type.
35  using Index = std::size_t;
36  /// Statement type.
37  using Statement = std::unique_ptr<sql::PreparedStatement>;
38  /// Results type.
39  using Results = std::unique_ptr<sql::ResultSet>;
40 
41  /// Connect to the given database.
42  bool connect(const std::string& db, const std::string& host, const std::string& user, const std::string& password) {
43  try {
44  driver = get_driver_instance();
45  conn = driver->connect("tcp://" + host + ":3306", user, password);
46  conn->setSchema(db);
47  return true;
48  } catch (sql::SQLException &e) {
49  return false;
50  }
51  }
52  /// Prepare a statement.
53  Statement prepare(const std::string& query) {
54  return Statement(conn->prepareStatement(replacePlaceholders(query)));
55  }
56  /// Execute a statement without arguments.
58  return Results(stmt->executeQuery());
59  }
60  /// Execute a statement with a single string.
61  Results execute(Statement& stmt, const std::string& a1) {
62  stmt->setString(1, a1);
63  return Results(stmt->executeQuery());
64  }
65  /// Execute a statement with a string and a number.
66  Results execute(Statement& stmt, const std::string& a1, std::size_t a2) {
67  stmt->setString(1, a1);
68  stmt->setUInt64(2, a2);
69  return Results(stmt->executeQuery());
70  }
71  /// Execute a statement with two strings and an index.
72  Results execute(Statement& stmt, const std::string& a1, const std::string& a2, const Index& a3) {
73  stmt->setString(1, a1);
74  stmt->setString(2, a2);
75  stmt->setUInt64(3, a3);
76  return Results(stmt->executeQuery());
77  }
78  /// Execute a statement with an int, two numbers and three indices.
79  Results execute(Statement& stmt, int a1, std::size_t a2, std::size_t a3, const Index& a4, const Index& a5, const Index& a6) {
80  stmt->setInt(1, a1);
81  stmt->setUInt64(2, a2);
82  stmt->setUInt64(3, a3);
83  stmt->setUInt64(4, a4);
84  stmt->setUInt64(5, a5);
85  stmt->setUInt64(6, a6);
86  return Results(stmt->executeQuery());
87  }
88  /// Insert some data and return the new index.
89  template<typename... Args>
90  Index insert(const std::string& query, const Args&... args) {
91  Statement s = prepare(query);
92  execute(s, args...);
93 
94  std::unique_ptr<sql::Statement> stmt(conn->createStatement());
95  Results res(stmt->executeQuery("SELECT @@identity AS id"));
96  res->next();
97  return res->getUInt64("id");
98  }
99  /// Select some data.
100  template<typename... Args>
101  Results select(const std::string& query, const Args&... args) {
102  Statement s = prepare(query);
103  return execute(s, args...);
104  }
105  /// Return the size of the results.
106  std::size_t size(const Results& res) {
107  return res->rowsCount();
108  }
109  /// Get the index of some name within the results.
110  Index getIndex(const Results& res, const std::string& name) {
111  res->next();
112  return res->getUInt64(name.c_str());
113  }
114 };
115 
116 }
std::size_t size(const Results &res)
Return the size of the results.
bool connect(const std::string &db, const std::string &host, const std::string &user, const std::string &password)
Connect to the given database.
std::unique_ptr< sql::Driver > driver
std::unique_ptr< sql::ResultSet > Results
Results type.
Statement prepare(const std::string &query)
Prepare a statement.
std::unique_ptr< sql::Connection > conn
Results execute(Statement &stmt, const std::string &a1, std::size_t a2)
Execute a statement with a string and a number.
std::unique_ptr< sql::PreparedStatement > Statement
Statement type.
Results execute(Statement &stmt)
Execute a statement without arguments.
std::size_t Index
Indec type.
std::string replacePlaceholders(const std::string &s)
Results execute(Statement &stmt, int a1, std::size_t a2, std::size_t a3, const Index &a4, const Index &a5, const Index &a6)
Execute a statement with an int, two numbers and three indices.
Index getIndex(const Results &res, const std::string &name)
Get the index of some name within the results.
Index insert(const std::string &query, const Args &... args)
Insert some data and return the new index.
Results execute(Statement &stmt, const std::string &a1, const std::string &a2, const Index &a3)
Execute a statement with two strings and an index.
Results execute(Statement &stmt, const std::string &a1)
Execute a statement with a single string.
Results select(const std::string &query, const Args &... args)
Select some data.