libdballe  8.6
sql.h
Go to the documentation of this file.
1 
4 #ifndef DBALLE_SQL_H
5 #define DBALLE_SQL_H
6 
7 #include <dballe/core/error.h>
8 #include <dballe/fwd.h>
9 #include <dballe/sql/fwd.h>
10 #include <string>
11 #include <memory>
12 
14 #undef USE_REF_INT
15 
20 // #define TRACE_DB
21 
22 #ifdef TRACE_DB
23 #define TRACE(...) fprintf(stderr, __VA_ARGS__)
24 #define IFTRACE if (1)
25 #else
26 
27 #define TRACE(...) do { } while (0)
28 
29 #define IFTRACE if (0)
30 #endif
31 
34 namespace dballe {
35 class Datetime;
36 
37 namespace sql {
38 
40 enum class ServerType
41 {
42  MYSQL,
43  SQLITE,
44  ORACLE,
45  POSTGRES,
46 };
47 
49 const char* format_server_type(ServerType type);
50 
51 
53 {
54 protected:
55  std::string url;
56 
57 public:
65 
66  Connection();
67  virtual ~Connection();
68 
69  const std::string& get_url() const { return url; }
70 
77  virtual std::unique_ptr<Transaction> transaction(bool readonly=false) = 0;
78 
80  virtual bool has_table(const std::string& name) = 0;
81 
87  virtual std::string get_setting(const std::string& key) = 0;
88 
94  virtual void set_setting(const std::string& key, const std::string& value) = 0;
95 
97  virtual void drop_settings() = 0;
98 
100  virtual void add_datetime(Querybuf& qb, const Datetime& dt) const;
101 
103  virtual void execute(const std::string& query) = 0;
104 
106  virtual void explain(const std::string& query, FILE* out) = 0;
107 
109  static std::unique_ptr<Connection> create(const DBConnectOptions& options);
110 };
111 
122 {
123 public:
124  Transaction() {}
125  Transaction(const Transaction&) = delete;
126  Transaction& operator=(const Transaction&) = delete;
127  virtual ~Transaction();
128 
130  virtual void commit() = 0;
131 
133  virtual void rollback() = 0;
134 
136  virtual void rollback_nothrow() noexcept = 0;
137 
140  virtual void lock_table(const char* name) = 0;
141 };
142 
143 }
144 }
145 
146 #endif
Forward declarations for public dballe/sql names.
ServerType server_type
Type of SQL server we are connected to.
Definition: sql.h:64
Definition: cmdline.h:18
Definition: sql.h:52
ServerType
Supported SQL servers.
Definition: sql.h:40
A RAII transaction interface for SQL transactions.
Definition: sql.h:121
Date and time.
Definition: types.h:164
Options controlling how to connect to a database.
Definition: db.h:17
const char * format_server_type(ServerType type)
Return a string description for a ServerType value.
String buffer for composing database queries.
Definition: querybuf.h:15